cx231xx-vbi.c 17 KB

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