cx231xx-vbi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
  3. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  4. Based on cx88 driver
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/usb.h>
  23. #include <linux/i2c.h>
  24. #include <linux/mm.h>
  25. #include <linux/mutex.h>
  26. #include <media/v4l2-common.h>
  27. #include <media/v4l2-ioctl.h>
  28. #include <media/v4l2-chip-ident.h>
  29. #include <media/msp3400.h>
  30. #include <media/tuner.h>
  31. #include "cx231xx.h"
  32. #include "cx231xx-vbi.h"
  33. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  34. {
  35. char *errmsg = "Unknown";
  36. switch (status) {
  37. case -ENOENT:
  38. errmsg = "unlinked synchronuously";
  39. break;
  40. case -ECONNRESET:
  41. errmsg = "unlinked asynchronuously";
  42. break;
  43. case -ENOSR:
  44. errmsg = "Buffer error (overrun)";
  45. break;
  46. case -EPIPE:
  47. errmsg = "Stalled (device not responding)";
  48. break;
  49. case -EOVERFLOW:
  50. errmsg = "Babble (bad cable?)";
  51. break;
  52. case -EPROTO:
  53. errmsg = "Bit-stuff error (bad cable?)";
  54. break;
  55. case -EILSEQ:
  56. errmsg = "CRC/Timeout (could be anything)";
  57. break;
  58. case -ETIME:
  59. errmsg = "Device does not respond";
  60. break;
  61. }
  62. if (packet < 0) {
  63. cx231xx_err(DRIVER_NAME "URB status %d [%s].\n", status,
  64. errmsg);
  65. } else {
  66. cx231xx_err(DRIVER_NAME "URB packet %d, status %d [%s].\n",
  67. packet, status, errmsg);
  68. }
  69. }
  70. /*
  71. * Controls the isoc copy of each urb packet
  72. */
  73. static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
  74. {
  75. struct cx231xx_buffer *buf;
  76. struct cx231xx_dmaqueue *dma_q = urb->context;
  77. int rc = 1;
  78. unsigned char *p_buffer;
  79. u32 bytes_parsed = 0, buffer_size = 0;
  80. u8 sav_eav = 0;
  81. if (!dev)
  82. return 0;
  83. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  84. return 0;
  85. if (urb->status < 0) {
  86. print_err_status(dev, -1, urb->status);
  87. if (urb->status == -ENOENT)
  88. return 0;
  89. }
  90. buf = dev->vbi_mode.isoc_ctl.buf;
  91. /* get buffer pointer and length */
  92. p_buffer = urb->transfer_buffer;
  93. buffer_size = urb->actual_length;
  94. if (buffer_size > 0) {
  95. bytes_parsed = 0;
  96. if (dma_q->is_partial_line) {
  97. /* Handle the case where we were working on a partial
  98. line */
  99. sav_eav = dma_q->last_sav;
  100. } else {
  101. /* Check for a SAV/EAV overlapping the
  102. buffer boundary */
  103. sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
  104. dma_q->partial_buf,
  105. &bytes_parsed);
  106. }
  107. sav_eav &= 0xF0;
  108. /* Get the first line if we have some portion of an SAV/EAV from
  109. the last buffer or a partial line */
  110. if (sav_eav) {
  111. bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
  112. sav_eav, /* SAV/EAV */
  113. p_buffer + bytes_parsed, /* p_buffer */
  114. buffer_size - bytes_parsed); /* buffer size */
  115. }
  116. /* Now parse data that is completely in this buffer */
  117. dma_q->is_partial_line = 0;
  118. while (bytes_parsed < buffer_size) {
  119. u32 bytes_used = 0;
  120. sav_eav = cx231xx_find_next_SAV_EAV(
  121. p_buffer + bytes_parsed, /* p_buffer */
  122. buffer_size - bytes_parsed, /* buffer size */
  123. &bytes_used); /* bytes used to get SAV/EAV */
  124. bytes_parsed += bytes_used;
  125. sav_eav &= 0xF0;
  126. if (sav_eav && (bytes_parsed < buffer_size)) {
  127. bytes_parsed += cx231xx_get_vbi_line(dev,
  128. dma_q, sav_eav, /* SAV/EAV */
  129. p_buffer+bytes_parsed, /* p_buffer */
  130. buffer_size-bytes_parsed);/*buf size*/
  131. }
  132. }
  133. /* Save the last four bytes of the buffer so we can
  134. check the buffer boundary condition next time */
  135. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  136. bytes_parsed = 0;
  137. }
  138. return rc;
  139. }
  140. /* ------------------------------------------------------------------
  141. Vbi buf operations
  142. ------------------------------------------------------------------*/
  143. static int
  144. vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  145. unsigned int *size)
  146. {
  147. struct cx231xx_fh *fh = vq->priv_data;
  148. struct cx231xx *dev = fh->dev;
  149. u32 height = 0;
  150. height = ((dev->norm & V4L2_STD_625_50) ?
  151. PAL_VBI_LINES : NTSC_VBI_LINES);
  152. *size = (dev->width * height * 2);
  153. if (0 == *count)
  154. *count = CX231XX_DEF_VBI_BUF;
  155. if (*count < CX231XX_MIN_BUF)
  156. *count = CX231XX_MIN_BUF;
  157. return 0;
  158. }
  159. /* This is called *without* dev->slock held; please keep it that way */
  160. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  161. {
  162. struct cx231xx_fh *fh = vq->priv_data;
  163. struct cx231xx *dev = fh->dev;
  164. unsigned long flags = 0;
  165. if (in_interrupt())
  166. BUG();
  167. /* We used to wait for the buffer to finish here, but this didn't work
  168. because, as we were keeping the state as VIDEOBUF_QUEUED,
  169. videobuf_queue_cancel marked it as finished for us.
  170. (Also, it could wedge forever if the hardware was misconfigured.)
  171. This should be safe; by the time we get here, the buffer isn't
  172. queued anymore. If we ever start marking the buffers as
  173. VIDEOBUF_ACTIVE, it won't be, though.
  174. */
  175. spin_lock_irqsave(&dev->vbi_mode.slock, flags);
  176. if (dev->vbi_mode.isoc_ctl.buf == buf)
  177. dev->vbi_mode.isoc_ctl.buf = NULL;
  178. spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
  179. videobuf_vmalloc_free(&buf->vb);
  180. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  181. }
  182. static int
  183. vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  184. enum v4l2_field field)
  185. {
  186. struct cx231xx_fh *fh = vq->priv_data;
  187. struct cx231xx_buffer *buf =
  188. container_of(vb, struct cx231xx_buffer, vb);
  189. struct cx231xx *dev = fh->dev;
  190. int rc = 0, urb_init = 0;
  191. u32 height = 0;
  192. height = ((dev->norm & V4L2_STD_625_50) ?
  193. PAL_VBI_LINES : NTSC_VBI_LINES);
  194. buf->vb.size = ((dev->width << 1) * height);
  195. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  196. return -EINVAL;
  197. buf->vb.width = dev->width;
  198. buf->vb.height = height;
  199. buf->vb.field = field;
  200. buf->vb.field = V4L2_FIELD_SEQ_TB;
  201. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  202. rc = videobuf_iolock(vq, &buf->vb, NULL);
  203. if (rc < 0)
  204. goto fail;
  205. }
  206. if (!dev->vbi_mode.isoc_ctl.num_bufs)
  207. urb_init = 1;
  208. if (urb_init) {
  209. rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
  210. CX231XX_NUM_VBI_BUFS,
  211. dev->vbi_mode.alt_max_pkt_size[0],
  212. cx231xx_isoc_vbi_copy);
  213. if (rc < 0)
  214. goto fail;
  215. }
  216. buf->vb.state = VIDEOBUF_PREPARED;
  217. return 0;
  218. fail:
  219. free_buffer(vq, buf);
  220. return rc;
  221. }
  222. static void
  223. vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  224. {
  225. struct cx231xx_buffer *buf =
  226. container_of(vb, struct cx231xx_buffer, vb);
  227. struct cx231xx_fh *fh = vq->priv_data;
  228. struct cx231xx *dev = fh->dev;
  229. struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
  230. buf->vb.state = VIDEOBUF_QUEUED;
  231. list_add_tail(&buf->vb.queue, &vidq->active);
  232. }
  233. static void vbi_buffer_release(struct videobuf_queue *vq,
  234. struct videobuf_buffer *vb)
  235. {
  236. struct cx231xx_buffer *buf =
  237. container_of(vb, struct cx231xx_buffer, vb);
  238. free_buffer(vq, buf);
  239. }
  240. struct videobuf_queue_ops cx231xx_vbi_qops = {
  241. .buf_setup = vbi_buffer_setup,
  242. .buf_prepare = vbi_buffer_prepare,
  243. .buf_queue = vbi_buffer_queue,
  244. .buf_release = vbi_buffer_release,
  245. };
  246. /* ------------------------------------------------------------------
  247. URB control
  248. ------------------------------------------------------------------*/
  249. /*
  250. * IRQ callback, called by URB callback
  251. */
  252. static void cx231xx_irq_vbi_callback(struct urb *urb)
  253. {
  254. struct cx231xx_dmaqueue *dma_q = urb->context;
  255. struct cx231xx_video_mode *vmode =
  256. container_of(dma_q, struct cx231xx_video_mode, vidq);
  257. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  258. int rc;
  259. switch (urb->status) {
  260. case 0: /* success */
  261. case -ETIMEDOUT: /* NAK */
  262. break;
  263. case -ECONNRESET: /* kill */
  264. case -ENOENT:
  265. case -ESHUTDOWN:
  266. return;
  267. default: /* error */
  268. cx231xx_err(DRIVER_NAME "urb completition error %d.\n",
  269. urb->status);
  270. break;
  271. }
  272. /* Copy data from URB */
  273. spin_lock(&dev->vbi_mode.slock);
  274. rc = dev->vbi_mode.isoc_ctl.isoc_copy(dev, urb);
  275. spin_unlock(&dev->vbi_mode.slock);
  276. /* Reset status */
  277. urb->status = 0;
  278. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  279. if (urb->status) {
  280. cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n",
  281. urb->status);
  282. }
  283. }
  284. /*
  285. * Stop and Deallocate URBs
  286. */
  287. void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
  288. {
  289. struct urb *urb;
  290. int i;
  291. cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_uninit_vbi_isoc\n");
  292. dev->vbi_mode.isoc_ctl.nfields = -1;
  293. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  294. urb = dev->vbi_mode.isoc_ctl.urb[i];
  295. if (urb) {
  296. if (!irqs_disabled())
  297. usb_kill_urb(urb);
  298. else
  299. usb_unlink_urb(urb);
  300. if (dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
  301. kfree(dev->vbi_mode.isoc_ctl.
  302. transfer_buffer[i]);
  303. dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
  304. NULL;
  305. }
  306. usb_free_urb(urb);
  307. dev->vbi_mode.isoc_ctl.urb[i] = NULL;
  308. }
  309. dev->vbi_mode.isoc_ctl.transfer_buffer[i] = NULL;
  310. }
  311. kfree(dev->vbi_mode.isoc_ctl.urb);
  312. kfree(dev->vbi_mode.isoc_ctl.transfer_buffer);
  313. dev->vbi_mode.isoc_ctl.urb = NULL;
  314. dev->vbi_mode.isoc_ctl.transfer_buffer = NULL;
  315. dev->vbi_mode.isoc_ctl.num_bufs = 0;
  316. cx231xx_capture_start(dev, 0, Vbi);
  317. }
  318. EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
  319. /*
  320. * Allocate URBs and start IRQ
  321. */
  322. int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
  323. int num_bufs, int max_pkt_size,
  324. int (*isoc_copy) (struct cx231xx *dev,
  325. struct urb *urb))
  326. {
  327. struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
  328. int i;
  329. int sb_size, pipe;
  330. struct urb *urb;
  331. int rc;
  332. cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n");
  333. /* De-allocates all pending stuff */
  334. cx231xx_uninit_vbi_isoc(dev);
  335. /* clear if any halt */
  336. usb_clear_halt(dev->udev,
  337. usb_rcvbulkpipe(dev->udev,
  338. dev->vbi_mode.end_point_addr));
  339. dev->vbi_mode.isoc_ctl.isoc_copy = isoc_copy;
  340. dev->vbi_mode.isoc_ctl.num_bufs = num_bufs;
  341. dma_q->pos = 0;
  342. dma_q->is_partial_line = 0;
  343. dma_q->last_sav = 0;
  344. dma_q->current_field = -1;
  345. dma_q->bytes_left_in_line = dev->width << 1;
  346. dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
  347. PAL_VBI_LINES : NTSC_VBI_LINES);
  348. dma_q->lines_completed = 0;
  349. for (i = 0; i < 8; i++)
  350. dma_q->partial_buf[i] = 0;
  351. dev->vbi_mode.isoc_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
  352. GFP_KERNEL);
  353. if (!dev->vbi_mode.isoc_ctl.urb) {
  354. cx231xx_errdev("cannot alloc memory for usb buffers\n");
  355. return -ENOMEM;
  356. }
  357. dev->vbi_mode.isoc_ctl.transfer_buffer =
  358. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  359. if (!dev->vbi_mode.isoc_ctl.transfer_buffer) {
  360. cx231xx_errdev("cannot allocate memory for usbtransfer\n");
  361. kfree(dev->vbi_mode.isoc_ctl.urb);
  362. return -ENOMEM;
  363. }
  364. dev->vbi_mode.isoc_ctl.max_pkt_size = max_pkt_size;
  365. dev->vbi_mode.isoc_ctl.buf = NULL;
  366. sb_size = max_packets * dev->vbi_mode.isoc_ctl.max_pkt_size;
  367. /* allocate urbs and transfer buffers */
  368. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  369. urb = usb_alloc_urb(0, GFP_KERNEL);
  370. if (!urb) {
  371. cx231xx_err(DRIVER_NAME
  372. ": cannot alloc isoc_ctl.urb %i\n", i);
  373. cx231xx_uninit_vbi_isoc(dev);
  374. return -ENOMEM;
  375. }
  376. dev->vbi_mode.isoc_ctl.urb[i] = urb;
  377. urb->transfer_flags = 0;
  378. dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
  379. kzalloc(sb_size, GFP_KERNEL);
  380. if (!dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
  381. cx231xx_err(DRIVER_NAME
  382. ": unable to allocate %i bytes for transfer"
  383. " buffer %i%s\n", sb_size, i,
  384. in_interrupt() ? " while in int" : "");
  385. cx231xx_uninit_vbi_isoc(dev);
  386. return -ENOMEM;
  387. }
  388. pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
  389. usb_fill_bulk_urb(urb, dev->udev, pipe,
  390. dev->vbi_mode.isoc_ctl.transfer_buffer[i],
  391. sb_size, cx231xx_irq_vbi_callback, dma_q);
  392. }
  393. init_waitqueue_head(&dma_q->wq);
  394. /* submit urbs and enables IRQ */
  395. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  396. rc = usb_submit_urb(dev->vbi_mode.isoc_ctl.urb[i], GFP_ATOMIC);
  397. if (rc) {
  398. cx231xx_err(DRIVER_NAME
  399. ": submit of urb %i failed (error=%i)\n", i,
  400. rc);
  401. cx231xx_uninit_vbi_isoc(dev);
  402. return rc;
  403. }
  404. }
  405. cx231xx_capture_start(dev, 1, Vbi);
  406. return 0;
  407. }
  408. EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
  409. u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  410. u8 sav_eav, u8 *p_buffer, u32 buffer_size)
  411. {
  412. u32 bytes_copied = 0;
  413. int current_field = -1;
  414. switch (sav_eav) {
  415. case SAV_VBI_FIELD1:
  416. current_field = 1;
  417. break;
  418. case SAV_VBI_FIELD2:
  419. current_field = 2;
  420. break;
  421. default:
  422. break;
  423. }
  424. if (current_field < 0)
  425. return bytes_copied;
  426. dma_q->last_sav = sav_eav;
  427. bytes_copied =
  428. cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
  429. current_field);
  430. return bytes_copied;
  431. }
  432. /*
  433. * Announces that a buffer were filled and request the next
  434. */
  435. static inline void vbi_buffer_filled(struct cx231xx *dev,
  436. struct cx231xx_dmaqueue *dma_q,
  437. struct cx231xx_buffer *buf)
  438. {
  439. /* Advice that buffer was filled */
  440. /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
  441. buf->vb.state = VIDEOBUF_DONE;
  442. buf->vb.field_count++;
  443. do_gettimeofday(&buf->vb.ts);
  444. dev->vbi_mode.isoc_ctl.buf = NULL;
  445. list_del(&buf->vb.queue);
  446. wake_up(&buf->vb.done);
  447. }
  448. u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  449. u8 *p_line, u32 length, int field_number)
  450. {
  451. u32 bytes_to_copy;
  452. struct cx231xx_buffer *buf;
  453. u32 _line_size = dev->width * 2;
  454. if (dma_q->current_field != field_number)
  455. cx231xx_reset_vbi_buffer(dev, dma_q);
  456. /* get the buffer pointer */
  457. buf = dev->vbi_mode.isoc_ctl.buf;
  458. /* Remember the field number for next time */
  459. dma_q->current_field = field_number;
  460. bytes_to_copy = dma_q->bytes_left_in_line;
  461. if (bytes_to_copy > length)
  462. bytes_to_copy = length;
  463. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  464. dma_q->bytes_left_in_line -= bytes_to_copy;
  465. dma_q->is_partial_line =
  466. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  467. return 0;
  468. }
  469. dma_q->is_partial_line = 1;
  470. /* If we don't have a buffer, just return the number of bytes we would
  471. have copied if we had a buffer. */
  472. if (!buf) {
  473. dma_q->bytes_left_in_line -= bytes_to_copy;
  474. dma_q->is_partial_line =
  475. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  476. return bytes_to_copy;
  477. }
  478. /* copy the data to video buffer */
  479. cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
  480. dma_q->pos += bytes_to_copy;
  481. dma_q->bytes_left_in_line -= bytes_to_copy;
  482. if (dma_q->bytes_left_in_line == 0) {
  483. dma_q->bytes_left_in_line = _line_size;
  484. dma_q->lines_completed++;
  485. dma_q->is_partial_line = 0;
  486. if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
  487. vbi_buffer_filled(dev, dma_q, buf);
  488. dma_q->pos = 0;
  489. buf = NULL;
  490. dma_q->lines_completed = 0;
  491. }
  492. }
  493. return bytes_to_copy;
  494. }
  495. /*
  496. * video-buf generic routine to get the next available buffer
  497. */
  498. static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
  499. struct cx231xx_buffer **buf)
  500. {
  501. struct cx231xx_video_mode *vmode =
  502. container_of(dma_q, struct cx231xx_video_mode, vidq);
  503. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  504. char *outp;
  505. if (list_empty(&dma_q->active)) {
  506. cx231xx_err(DRIVER_NAME ": No active queue to serve\n");
  507. dev->vbi_mode.isoc_ctl.buf = NULL;
  508. *buf = NULL;
  509. return;
  510. }
  511. /* Get the next buffer */
  512. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  513. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  514. outp = videobuf_to_vmalloc(&(*buf)->vb);
  515. memset(outp, 0, (*buf)->vb.size);
  516. dev->vbi_mode.isoc_ctl.buf = *buf;
  517. return;
  518. }
  519. void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
  520. struct cx231xx_dmaqueue *dma_q)
  521. {
  522. struct cx231xx_buffer *buf;
  523. buf = dev->vbi_mode.isoc_ctl.buf;
  524. if (buf == NULL) {
  525. /* first try to get the buffer */
  526. get_next_vbi_buf(dma_q, &buf);
  527. dma_q->pos = 0;
  528. dma_q->current_field = -1;
  529. }
  530. dma_q->bytes_left_in_line = dev->width << 1;
  531. dma_q->lines_completed = 0;
  532. }
  533. int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  534. u8 *p_buffer, u32 bytes_to_copy)
  535. {
  536. u8 *p_out_buffer = NULL;
  537. u32 current_line_bytes_copied = 0;
  538. struct cx231xx_buffer *buf;
  539. u32 _line_size = dev->width << 1;
  540. void *startwrite;
  541. int offset, lencopy;
  542. buf = dev->vbi_mode.isoc_ctl.buf;
  543. if (buf == NULL)
  544. return -EINVAL;
  545. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  546. if (dma_q->bytes_left_in_line != _line_size) {
  547. current_line_bytes_copied =
  548. _line_size - dma_q->bytes_left_in_line;
  549. }
  550. offset = (dma_q->lines_completed * _line_size) +
  551. current_line_bytes_copied;
  552. /* prepare destination address */
  553. startwrite = p_out_buffer + offset;
  554. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  555. bytes_to_copy : dma_q->bytes_left_in_line;
  556. memcpy(startwrite, p_buffer, lencopy);
  557. return 0;
  558. }
  559. u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
  560. struct cx231xx_dmaqueue *dma_q)
  561. {
  562. u32 height = 0;
  563. height = ((dev->norm & V4L2_STD_625_50) ?
  564. PAL_VBI_LINES : NTSC_VBI_LINES);
  565. return (dma_q->lines_completed == height) ? 1 : 0;
  566. }