cx231xx-vbi.c 18 KB

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