/drivers/media/video/cx23885/cx23885-video.c

https://bitbucket.org/abioy/linux · C · 1550 lines · 1260 code · 212 blank · 78 comment · 164 complexity · bc841fb814c904c080df17bc72a3df25 MD5 · raw file

  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  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. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kmod.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/kthread.h>
  32. #include <asm/div64.h>
  33. #include "cx23885.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include "cx23885-ioctl.h"
  37. #include "tuner-xc2028.h"
  38. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  39. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  40. MODULE_LICENSE("GPL");
  41. /* ------------------------------------------------------------------ */
  42. static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  43. static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  44. static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  45. module_param_array(video_nr, int, NULL, 0444);
  46. module_param_array(vbi_nr, int, NULL, 0444);
  47. module_param_array(radio_nr, int, NULL, 0444);
  48. MODULE_PARM_DESC(video_nr, "video device numbers");
  49. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  50. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  51. static unsigned int video_debug;
  52. module_param(video_debug, int, 0644);
  53. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  54. static unsigned int irq_debug;
  55. module_param(irq_debug, int, 0644);
  56. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  57. static unsigned int vid_limit = 16;
  58. module_param(vid_limit, int, 0644);
  59. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  60. #define dprintk(level, fmt, arg...)\
  61. do { if (video_debug >= level)\
  62. printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. #define FORMAT_FLAGS_PACKED 0x01
  67. static struct cx23885_fmt formats[] = {
  68. {
  69. .name = "8 bpp, gray",
  70. .fourcc = V4L2_PIX_FMT_GREY,
  71. .depth = 8,
  72. .flags = FORMAT_FLAGS_PACKED,
  73. }, {
  74. .name = "15 bpp RGB, le",
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .depth = 16,
  77. .flags = FORMAT_FLAGS_PACKED,
  78. }, {
  79. .name = "15 bpp RGB, be",
  80. .fourcc = V4L2_PIX_FMT_RGB555X,
  81. .depth = 16,
  82. .flags = FORMAT_FLAGS_PACKED,
  83. }, {
  84. .name = "16 bpp RGB, le",
  85. .fourcc = V4L2_PIX_FMT_RGB565,
  86. .depth = 16,
  87. .flags = FORMAT_FLAGS_PACKED,
  88. }, {
  89. .name = "16 bpp RGB, be",
  90. .fourcc = V4L2_PIX_FMT_RGB565X,
  91. .depth = 16,
  92. .flags = FORMAT_FLAGS_PACKED,
  93. }, {
  94. .name = "24 bpp RGB, le",
  95. .fourcc = V4L2_PIX_FMT_BGR24,
  96. .depth = 24,
  97. .flags = FORMAT_FLAGS_PACKED,
  98. }, {
  99. .name = "32 bpp RGB, le",
  100. .fourcc = V4L2_PIX_FMT_BGR32,
  101. .depth = 32,
  102. .flags = FORMAT_FLAGS_PACKED,
  103. }, {
  104. .name = "32 bpp RGB, be",
  105. .fourcc = V4L2_PIX_FMT_RGB32,
  106. .depth = 32,
  107. .flags = FORMAT_FLAGS_PACKED,
  108. }, {
  109. .name = "4:2:2, packed, YUYV",
  110. .fourcc = V4L2_PIX_FMT_YUYV,
  111. .depth = 16,
  112. .flags = FORMAT_FLAGS_PACKED,
  113. }, {
  114. .name = "4:2:2, packed, UYVY",
  115. .fourcc = V4L2_PIX_FMT_UYVY,
  116. .depth = 16,
  117. .flags = FORMAT_FLAGS_PACKED,
  118. },
  119. };
  120. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  121. {
  122. unsigned int i;
  123. for (i = 0; i < ARRAY_SIZE(formats); i++)
  124. if (formats[i].fourcc == fourcc)
  125. return formats+i;
  126. printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  127. return NULL;
  128. }
  129. /* ------------------------------------------------------------------- */
  130. static const struct v4l2_queryctrl no_ctl = {
  131. .name = "42",
  132. .flags = V4L2_CTRL_FLAG_DISABLED,
  133. };
  134. static struct cx23885_ctrl cx23885_ctls[] = {
  135. /* --- video --- */
  136. {
  137. .v = {
  138. .id = V4L2_CID_BRIGHTNESS,
  139. .name = "Brightness",
  140. .minimum = 0x00,
  141. .maximum = 0xff,
  142. .step = 1,
  143. .default_value = 0x7f,
  144. .type = V4L2_CTRL_TYPE_INTEGER,
  145. },
  146. .off = 128,
  147. .reg = LUMA_CTRL,
  148. .mask = 0x00ff,
  149. .shift = 0,
  150. }, {
  151. .v = {
  152. .id = V4L2_CID_CONTRAST,
  153. .name = "Contrast",
  154. .minimum = 0,
  155. .maximum = 0xff,
  156. .step = 1,
  157. .default_value = 0x3f,
  158. .type = V4L2_CTRL_TYPE_INTEGER,
  159. },
  160. .off = 0,
  161. .reg = LUMA_CTRL,
  162. .mask = 0xff00,
  163. .shift = 8,
  164. }, {
  165. .v = {
  166. .id = V4L2_CID_HUE,
  167. .name = "Hue",
  168. .minimum = 0,
  169. .maximum = 0xff,
  170. .step = 1,
  171. .default_value = 0x7f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .off = 128,
  175. .reg = CHROMA_CTRL,
  176. .mask = 0xff0000,
  177. .shift = 16,
  178. }, {
  179. /* strictly, this only describes only U saturation.
  180. * V saturation is handled specially through code.
  181. */
  182. .v = {
  183. .id = V4L2_CID_SATURATION,
  184. .name = "Saturation",
  185. .minimum = 0,
  186. .maximum = 0xff,
  187. .step = 1,
  188. .default_value = 0x7f,
  189. .type = V4L2_CTRL_TYPE_INTEGER,
  190. },
  191. .off = 0,
  192. .reg = CHROMA_CTRL,
  193. .mask = 0x00ff,
  194. .shift = 0,
  195. }, {
  196. /* --- audio --- */
  197. .v = {
  198. .id = V4L2_CID_AUDIO_MUTE,
  199. .name = "Mute",
  200. .minimum = 0,
  201. .maximum = 1,
  202. .default_value = 1,
  203. .type = V4L2_CTRL_TYPE_BOOLEAN,
  204. },
  205. .reg = PATH1_CTL1,
  206. .mask = (0x1f << 24),
  207. .shift = 24,
  208. }, {
  209. .v = {
  210. .id = V4L2_CID_AUDIO_VOLUME,
  211. .name = "Volume",
  212. .minimum = 0,
  213. .maximum = 0x3f,
  214. .step = 1,
  215. .default_value = 0x3f,
  216. .type = V4L2_CTRL_TYPE_INTEGER,
  217. },
  218. .reg = PATH1_VOL_CTL,
  219. .mask = 0xff,
  220. .shift = 0,
  221. }
  222. };
  223. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  224. /* Must be sorted from low to high control ID! */
  225. static const u32 cx23885_user_ctrls[] = {
  226. V4L2_CID_USER_CLASS,
  227. V4L2_CID_BRIGHTNESS,
  228. V4L2_CID_CONTRAST,
  229. V4L2_CID_SATURATION,
  230. V4L2_CID_HUE,
  231. V4L2_CID_AUDIO_VOLUME,
  232. V4L2_CID_AUDIO_MUTE,
  233. 0
  234. };
  235. static const u32 *ctrl_classes[] = {
  236. cx23885_user_ctrls,
  237. NULL
  238. };
  239. static void cx23885_video_wakeup(struct cx23885_dev *dev,
  240. struct cx23885_dmaqueue *q, u32 count)
  241. {
  242. struct cx23885_buffer *buf;
  243. int bc;
  244. for (bc = 0;; bc++) {
  245. if (list_empty(&q->active))
  246. break;
  247. buf = list_entry(q->active.next,
  248. struct cx23885_buffer, vb.queue);
  249. /* count comes from the hw and is is 16bit wide --
  250. * this trick handles wrap-arounds correctly for
  251. * up to 32767 buffers in flight... */
  252. if ((s16) (count - buf->count) < 0)
  253. break;
  254. do_gettimeofday(&buf->vb.ts);
  255. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  256. count, buf->count);
  257. buf->vb.state = VIDEOBUF_DONE;
  258. list_del(&buf->vb.queue);
  259. wake_up(&buf->vb.done);
  260. }
  261. if (list_empty(&q->active))
  262. del_timer(&q->timeout);
  263. else
  264. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  265. if (bc != 1)
  266. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  267. __func__, bc);
  268. }
  269. static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  270. {
  271. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  272. __func__,
  273. (unsigned int)norm,
  274. v4l2_norm_to_name(norm));
  275. dev->tvnorm = norm;
  276. call_all(dev, core, s_std, norm);
  277. return 0;
  278. }
  279. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  280. struct pci_dev *pci,
  281. struct video_device *template,
  282. char *type)
  283. {
  284. struct video_device *vfd;
  285. dprintk(1, "%s()\n", __func__);
  286. vfd = video_device_alloc();
  287. if (NULL == vfd)
  288. return NULL;
  289. *vfd = *template;
  290. vfd->v4l2_dev = &dev->v4l2_dev;
  291. vfd->release = video_device_release;
  292. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
  293. dev->name, type, cx23885_boards[dev->board].name);
  294. video_set_drvdata(vfd, dev);
  295. return vfd;
  296. }
  297. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  298. {
  299. int i;
  300. if (qctrl->id < V4L2_CID_BASE ||
  301. qctrl->id >= V4L2_CID_LASTP1)
  302. return -EINVAL;
  303. for (i = 0; i < CX23885_CTLS; i++)
  304. if (cx23885_ctls[i].v.id == qctrl->id)
  305. break;
  306. if (i == CX23885_CTLS) {
  307. *qctrl = no_ctl;
  308. return 0;
  309. }
  310. *qctrl = cx23885_ctls[i].v;
  311. return 0;
  312. }
  313. /* ------------------------------------------------------------------- */
  314. /* resource management */
  315. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  316. unsigned int bit)
  317. {
  318. dprintk(1, "%s()\n", __func__);
  319. if (fh->resources & bit)
  320. /* have it already allocated */
  321. return 1;
  322. /* is it free? */
  323. mutex_lock(&dev->lock);
  324. if (dev->resources & bit) {
  325. /* no, someone else uses it */
  326. mutex_unlock(&dev->lock);
  327. return 0;
  328. }
  329. /* it's free, grab it */
  330. fh->resources |= bit;
  331. dev->resources |= bit;
  332. dprintk(1, "res: get %d\n", bit);
  333. mutex_unlock(&dev->lock);
  334. return 1;
  335. }
  336. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  337. {
  338. return fh->resources & bit;
  339. }
  340. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  341. {
  342. return dev->resources & bit;
  343. }
  344. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  345. unsigned int bits)
  346. {
  347. BUG_ON((fh->resources & bits) != bits);
  348. dprintk(1, "%s()\n", __func__);
  349. mutex_lock(&dev->lock);
  350. fh->resources &= ~bits;
  351. dev->resources &= ~bits;
  352. dprintk(1, "res: put %d\n", bits);
  353. mutex_unlock(&dev->lock);
  354. }
  355. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  356. {
  357. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  358. __func__,
  359. input, INPUT(input)->vmux,
  360. INPUT(input)->gpio0, INPUT(input)->gpio1,
  361. INPUT(input)->gpio2, INPUT(input)->gpio3);
  362. dev->input = input;
  363. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  364. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
  365. /* Select Analog TV */
  366. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  367. cx23885_gpio_clear(dev, GPIO_0);
  368. }
  369. /* Tell the internal A/V decoder */
  370. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  371. INPUT(input)->vmux, 0, 0);
  372. return 0;
  373. }
  374. /* ------------------------------------------------------------------ */
  375. static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  376. unsigned int height, enum v4l2_field field)
  377. {
  378. dprintk(1, "%s()\n", __func__);
  379. return 0;
  380. }
  381. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  382. struct cx23885_dmaqueue *q,
  383. struct cx23885_buffer *buf)
  384. {
  385. dprintk(1, "%s()\n", __func__);
  386. /* setup fifo + format */
  387. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  388. buf->bpl, buf->risc.dma);
  389. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  390. /* reset counter */
  391. cx_write(VID_A_GPCNT_CTL, 3);
  392. q->count = 1;
  393. /* enable irq */
  394. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  395. cx_set(VID_A_INT_MSK, 0x000011);
  396. /* start dma */
  397. cx_set(DEV_CNTRL2, (1<<5));
  398. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  399. return 0;
  400. }
  401. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  402. struct cx23885_dmaqueue *q)
  403. {
  404. struct cx23885_buffer *buf, *prev;
  405. struct list_head *item;
  406. dprintk(1, "%s()\n", __func__);
  407. if (!list_empty(&q->active)) {
  408. buf = list_entry(q->active.next, struct cx23885_buffer,
  409. vb.queue);
  410. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  411. buf, buf->vb.i);
  412. cx23885_start_video_dma(dev, q, buf);
  413. list_for_each(item, &q->active) {
  414. buf = list_entry(item, struct cx23885_buffer,
  415. vb.queue);
  416. buf->count = q->count++;
  417. }
  418. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  419. return 0;
  420. }
  421. prev = NULL;
  422. for (;;) {
  423. if (list_empty(&q->queued))
  424. return 0;
  425. buf = list_entry(q->queued.next, struct cx23885_buffer,
  426. vb.queue);
  427. if (NULL == prev) {
  428. list_move_tail(&buf->vb.queue, &q->active);
  429. cx23885_start_video_dma(dev, q, buf);
  430. buf->vb.state = VIDEOBUF_ACTIVE;
  431. buf->count = q->count++;
  432. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  433. dprintk(2, "[%p/%d] restart_queue - first active\n",
  434. buf, buf->vb.i);
  435. } else if (prev->vb.width == buf->vb.width &&
  436. prev->vb.height == buf->vb.height &&
  437. prev->fmt == buf->fmt) {
  438. list_move_tail(&buf->vb.queue, &q->active);
  439. buf->vb.state = VIDEOBUF_ACTIVE;
  440. buf->count = q->count++;
  441. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  442. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  443. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  444. buf, buf->vb.i);
  445. } else {
  446. return 0;
  447. }
  448. prev = buf;
  449. }
  450. }
  451. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  452. unsigned int *size)
  453. {
  454. struct cx23885_fh *fh = q->priv_data;
  455. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  456. if (0 == *count)
  457. *count = 32;
  458. while (*size * *count > vid_limit * 1024 * 1024)
  459. (*count)--;
  460. return 0;
  461. }
  462. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  463. enum v4l2_field field)
  464. {
  465. struct cx23885_fh *fh = q->priv_data;
  466. struct cx23885_dev *dev = fh->dev;
  467. struct cx23885_buffer *buf =
  468. container_of(vb, struct cx23885_buffer, vb);
  469. int rc, init_buffer = 0;
  470. u32 line0_offset, line1_offset;
  471. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  472. BUG_ON(NULL == fh->fmt);
  473. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  474. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  475. return -EINVAL;
  476. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  477. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  478. return -EINVAL;
  479. if (buf->fmt != fh->fmt ||
  480. buf->vb.width != fh->width ||
  481. buf->vb.height != fh->height ||
  482. buf->vb.field != field) {
  483. buf->fmt = fh->fmt;
  484. buf->vb.width = fh->width;
  485. buf->vb.height = fh->height;
  486. buf->vb.field = field;
  487. init_buffer = 1;
  488. }
  489. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  490. init_buffer = 1;
  491. rc = videobuf_iolock(q, &buf->vb, NULL);
  492. if (0 != rc)
  493. goto fail;
  494. }
  495. if (init_buffer) {
  496. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  497. switch (buf->vb.field) {
  498. case V4L2_FIELD_TOP:
  499. cx23885_risc_buffer(dev->pci, &buf->risc,
  500. dma->sglist, 0, UNSET,
  501. buf->bpl, 0, buf->vb.height);
  502. break;
  503. case V4L2_FIELD_BOTTOM:
  504. cx23885_risc_buffer(dev->pci, &buf->risc,
  505. dma->sglist, UNSET, 0,
  506. buf->bpl, 0, buf->vb.height);
  507. break;
  508. case V4L2_FIELD_INTERLACED:
  509. if (dev->tvnorm & V4L2_STD_NTSC) {
  510. /* cx25840 transmits NTSC bottom field first */
  511. dprintk(1, "%s() Creating NTSC risc\n",
  512. __func__);
  513. line0_offset = buf->bpl;
  514. line1_offset = 0;
  515. } else {
  516. /* All other formats are top field first */
  517. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  518. __func__);
  519. line0_offset = 0;
  520. line1_offset = buf->bpl;
  521. }
  522. cx23885_risc_buffer(dev->pci, &buf->risc,
  523. dma->sglist, line0_offset,
  524. line1_offset,
  525. buf->bpl, buf->bpl,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_TB:
  529. cx23885_risc_buffer(dev->pci, &buf->risc,
  530. dma->sglist,
  531. 0, buf->bpl * (buf->vb.height >> 1),
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. case V4L2_FIELD_SEQ_BT:
  536. cx23885_risc_buffer(dev->pci, &buf->risc,
  537. dma->sglist,
  538. buf->bpl * (buf->vb.height >> 1), 0,
  539. buf->bpl, 0,
  540. buf->vb.height >> 1);
  541. break;
  542. default:
  543. BUG();
  544. }
  545. }
  546. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  547. buf, buf->vb.i,
  548. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  549. (unsigned long)buf->risc.dma);
  550. buf->vb.state = VIDEOBUF_PREPARED;
  551. return 0;
  552. fail:
  553. cx23885_free_buffer(q, buf);
  554. return rc;
  555. }
  556. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  557. {
  558. struct cx23885_buffer *buf = container_of(vb,
  559. struct cx23885_buffer, vb);
  560. struct cx23885_buffer *prev;
  561. struct cx23885_fh *fh = vq->priv_data;
  562. struct cx23885_dev *dev = fh->dev;
  563. struct cx23885_dmaqueue *q = &dev->vidq;
  564. /* add jump to stopper */
  565. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  566. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  567. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  568. if (!list_empty(&q->queued)) {
  569. list_add_tail(&buf->vb.queue, &q->queued);
  570. buf->vb.state = VIDEOBUF_QUEUED;
  571. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  572. buf, buf->vb.i);
  573. } else if (list_empty(&q->active)) {
  574. list_add_tail(&buf->vb.queue, &q->active);
  575. cx23885_start_video_dma(dev, q, buf);
  576. buf->vb.state = VIDEOBUF_ACTIVE;
  577. buf->count = q->count++;
  578. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  579. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  580. buf, buf->vb.i);
  581. } else {
  582. prev = list_entry(q->active.prev, struct cx23885_buffer,
  583. vb.queue);
  584. if (prev->vb.width == buf->vb.width &&
  585. prev->vb.height == buf->vb.height &&
  586. prev->fmt == buf->fmt) {
  587. list_add_tail(&buf->vb.queue, &q->active);
  588. buf->vb.state = VIDEOBUF_ACTIVE;
  589. buf->count = q->count++;
  590. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  591. /* 64 bit bits 63-32 */
  592. prev->risc.jmp[2] = cpu_to_le32(0);
  593. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  594. buf, buf->vb.i);
  595. } else {
  596. list_add_tail(&buf->vb.queue, &q->queued);
  597. buf->vb.state = VIDEOBUF_QUEUED;
  598. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  599. buf, buf->vb.i);
  600. }
  601. }
  602. }
  603. static void buffer_release(struct videobuf_queue *q,
  604. struct videobuf_buffer *vb)
  605. {
  606. struct cx23885_buffer *buf = container_of(vb,
  607. struct cx23885_buffer, vb);
  608. cx23885_free_buffer(q, buf);
  609. }
  610. static struct videobuf_queue_ops cx23885_video_qops = {
  611. .buf_setup = buffer_setup,
  612. .buf_prepare = buffer_prepare,
  613. .buf_queue = buffer_queue,
  614. .buf_release = buffer_release,
  615. };
  616. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  617. {
  618. switch (fh->type) {
  619. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  620. return &fh->vidq;
  621. case V4L2_BUF_TYPE_VBI_CAPTURE:
  622. return &fh->vbiq;
  623. default:
  624. BUG();
  625. return NULL;
  626. }
  627. }
  628. static int get_resource(struct cx23885_fh *fh)
  629. {
  630. switch (fh->type) {
  631. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  632. return RESOURCE_VIDEO;
  633. case V4L2_BUF_TYPE_VBI_CAPTURE:
  634. return RESOURCE_VBI;
  635. default:
  636. BUG();
  637. return 0;
  638. }
  639. }
  640. static int video_open(struct file *file)
  641. {
  642. struct video_device *vdev = video_devdata(file);
  643. struct cx23885_dev *dev = video_drvdata(file);
  644. struct cx23885_fh *fh;
  645. enum v4l2_buf_type type = 0;
  646. int radio = 0;
  647. switch (vdev->vfl_type) {
  648. case VFL_TYPE_GRABBER:
  649. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  650. break;
  651. case VFL_TYPE_VBI:
  652. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  653. break;
  654. case VFL_TYPE_RADIO:
  655. radio = 1;
  656. break;
  657. }
  658. dprintk(1, "open dev=%s radio=%d type=%s\n",
  659. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  660. /* allocate + initialize per filehandle data */
  661. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  662. if (NULL == fh)
  663. return -ENOMEM;
  664. lock_kernel();
  665. file->private_data = fh;
  666. fh->dev = dev;
  667. fh->radio = radio;
  668. fh->type = type;
  669. fh->width = 320;
  670. fh->height = 240;
  671. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  672. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  673. &dev->pci->dev, &dev->slock,
  674. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  675. V4L2_FIELD_INTERLACED,
  676. sizeof(struct cx23885_buffer),
  677. fh);
  678. dprintk(1, "post videobuf_queue_init()\n");
  679. unlock_kernel();
  680. return 0;
  681. }
  682. static ssize_t video_read(struct file *file, char __user *data,
  683. size_t count, loff_t *ppos)
  684. {
  685. struct cx23885_fh *fh = file->private_data;
  686. switch (fh->type) {
  687. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  688. if (res_locked(fh->dev, RESOURCE_VIDEO))
  689. return -EBUSY;
  690. return videobuf_read_one(&fh->vidq, data, count, ppos,
  691. file->f_flags & O_NONBLOCK);
  692. case V4L2_BUF_TYPE_VBI_CAPTURE:
  693. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  694. return -EBUSY;
  695. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  696. file->f_flags & O_NONBLOCK);
  697. default:
  698. BUG();
  699. return 0;
  700. }
  701. }
  702. static unsigned int video_poll(struct file *file,
  703. struct poll_table_struct *wait)
  704. {
  705. struct cx23885_fh *fh = file->private_data;
  706. struct cx23885_buffer *buf;
  707. unsigned int rc = POLLERR;
  708. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  709. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  710. return POLLERR;
  711. return videobuf_poll_stream(file, &fh->vbiq, wait);
  712. }
  713. mutex_lock(&fh->vidq.vb_lock);
  714. if (res_check(fh, RESOURCE_VIDEO)) {
  715. /* streaming capture */
  716. if (list_empty(&fh->vidq.stream))
  717. goto done;
  718. buf = list_entry(fh->vidq.stream.next,
  719. struct cx23885_buffer, vb.stream);
  720. } else {
  721. /* read() capture */
  722. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  723. if (NULL == buf)
  724. goto done;
  725. }
  726. poll_wait(file, &buf->vb.done, wait);
  727. if (buf->vb.state == VIDEOBUF_DONE ||
  728. buf->vb.state == VIDEOBUF_ERROR)
  729. rc = POLLIN|POLLRDNORM;
  730. else
  731. rc = 0;
  732. done:
  733. mutex_unlock(&fh->vidq.vb_lock);
  734. return rc;
  735. }
  736. static int video_release(struct file *file)
  737. {
  738. struct cx23885_fh *fh = file->private_data;
  739. struct cx23885_dev *dev = fh->dev;
  740. /* turn off overlay */
  741. if (res_check(fh, RESOURCE_OVERLAY)) {
  742. /* FIXME */
  743. res_free(dev, fh, RESOURCE_OVERLAY);
  744. }
  745. /* stop video capture */
  746. if (res_check(fh, RESOURCE_VIDEO)) {
  747. videobuf_queue_cancel(&fh->vidq);
  748. res_free(dev, fh, RESOURCE_VIDEO);
  749. }
  750. if (fh->vidq.read_buf) {
  751. buffer_release(&fh->vidq, fh->vidq.read_buf);
  752. kfree(fh->vidq.read_buf);
  753. }
  754. /* stop vbi capture */
  755. if (res_check(fh, RESOURCE_VBI)) {
  756. if (fh->vbiq.streaming)
  757. videobuf_streamoff(&fh->vbiq);
  758. if (fh->vbiq.reading)
  759. videobuf_read_stop(&fh->vbiq);
  760. res_free(dev, fh, RESOURCE_VBI);
  761. }
  762. videobuf_mmap_free(&fh->vidq);
  763. file->private_data = NULL;
  764. kfree(fh);
  765. /* We are not putting the tuner to sleep here on exit, because
  766. * we want to use the mpeg encoder in another session to capture
  767. * tuner video. Closing this will result in no video to the encoder.
  768. */
  769. return 0;
  770. }
  771. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  772. {
  773. struct cx23885_fh *fh = file->private_data;
  774. return videobuf_mmap_mapper(get_queue(fh), vma);
  775. }
  776. /* ------------------------------------------------------------------ */
  777. /* VIDEO CTRL IOCTLS */
  778. static int cx23885_get_control(struct cx23885_dev *dev,
  779. struct v4l2_control *ctl)
  780. {
  781. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  782. call_all(dev, core, g_ctrl, ctl);
  783. return 0;
  784. }
  785. static int cx23885_set_control(struct cx23885_dev *dev,
  786. struct v4l2_control *ctl)
  787. {
  788. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  789. " (disabled - no action)\n", __func__);
  790. return 0;
  791. }
  792. static void init_controls(struct cx23885_dev *dev)
  793. {
  794. struct v4l2_control ctrl;
  795. int i;
  796. for (i = 0; i < CX23885_CTLS; i++) {
  797. ctrl.id = cx23885_ctls[i].v.id;
  798. ctrl.value = cx23885_ctls[i].v.default_value;
  799. cx23885_set_control(dev, &ctrl);
  800. }
  801. }
  802. /* ------------------------------------------------------------------ */
  803. /* VIDEO IOCTLS */
  804. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  805. struct v4l2_format *f)
  806. {
  807. struct cx23885_fh *fh = priv;
  808. f->fmt.pix.width = fh->width;
  809. f->fmt.pix.height = fh->height;
  810. f->fmt.pix.field = fh->vidq.field;
  811. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  812. f->fmt.pix.bytesperline =
  813. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  814. f->fmt.pix.sizeimage =
  815. f->fmt.pix.height * f->fmt.pix.bytesperline;
  816. return 0;
  817. }
  818. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  819. struct v4l2_format *f)
  820. {
  821. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  822. struct cx23885_fmt *fmt;
  823. enum v4l2_field field;
  824. unsigned int maxw, maxh;
  825. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  826. if (NULL == fmt)
  827. return -EINVAL;
  828. field = f->fmt.pix.field;
  829. maxw = norm_maxw(dev->tvnorm);
  830. maxh = norm_maxh(dev->tvnorm);
  831. if (V4L2_FIELD_ANY == field) {
  832. field = (f->fmt.pix.height > maxh/2)
  833. ? V4L2_FIELD_INTERLACED
  834. : V4L2_FIELD_BOTTOM;
  835. }
  836. switch (field) {
  837. case V4L2_FIELD_TOP:
  838. case V4L2_FIELD_BOTTOM:
  839. maxh = maxh / 2;
  840. break;
  841. case V4L2_FIELD_INTERLACED:
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. f->fmt.pix.field = field;
  847. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  848. &f->fmt.pix.height, 32, maxh, 0, 0);
  849. f->fmt.pix.bytesperline =
  850. (f->fmt.pix.width * fmt->depth) >> 3;
  851. f->fmt.pix.sizeimage =
  852. f->fmt.pix.height * f->fmt.pix.bytesperline;
  853. return 0;
  854. }
  855. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  856. struct v4l2_format *f)
  857. {
  858. struct cx23885_fh *fh = priv;
  859. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  860. int err;
  861. dprintk(2, "%s()\n", __func__);
  862. err = vidioc_try_fmt_vid_cap(file, priv, f);
  863. if (0 != err)
  864. return err;
  865. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  866. fh->width = f->fmt.pix.width;
  867. fh->height = f->fmt.pix.height;
  868. fh->vidq.field = f->fmt.pix.field;
  869. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  870. fh->width, fh->height, fh->vidq.field);
  871. call_all(dev, video, s_fmt, f);
  872. return 0;
  873. }
  874. static int vidioc_querycap(struct file *file, void *priv,
  875. struct v4l2_capability *cap)
  876. {
  877. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  878. strcpy(cap->driver, "cx23885");
  879. strlcpy(cap->card, cx23885_boards[dev->board].name,
  880. sizeof(cap->card));
  881. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  882. cap->version = CX23885_VERSION_CODE;
  883. cap->capabilities =
  884. V4L2_CAP_VIDEO_CAPTURE |
  885. V4L2_CAP_READWRITE |
  886. V4L2_CAP_STREAMING |
  887. V4L2_CAP_VBI_CAPTURE;
  888. if (UNSET != dev->tuner_type)
  889. cap->capabilities |= V4L2_CAP_TUNER;
  890. return 0;
  891. }
  892. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  893. struct v4l2_fmtdesc *f)
  894. {
  895. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  896. return -EINVAL;
  897. strlcpy(f->description, formats[f->index].name,
  898. sizeof(f->description));
  899. f->pixelformat = formats[f->index].fourcc;
  900. return 0;
  901. }
  902. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  903. static int vidiocgmbuf(struct file *file, void *priv,
  904. struct video_mbuf *mbuf)
  905. {
  906. struct cx23885_fh *fh = priv;
  907. struct videobuf_queue *q;
  908. struct v4l2_requestbuffers req;
  909. unsigned int i;
  910. int err;
  911. q = get_queue(fh);
  912. memset(&req, 0, sizeof(req));
  913. req.type = q->type;
  914. req.count = 8;
  915. req.memory = V4L2_MEMORY_MMAP;
  916. err = videobuf_reqbufs(q, &req);
  917. if (err < 0)
  918. return err;
  919. mbuf->frames = req.count;
  920. mbuf->size = 0;
  921. for (i = 0; i < mbuf->frames; i++) {
  922. mbuf->offsets[i] = q->bufs[i]->boff;
  923. mbuf->size += q->bufs[i]->bsize;
  924. }
  925. return 0;
  926. }
  927. #endif
  928. static int vidioc_reqbufs(struct file *file, void *priv,
  929. struct v4l2_requestbuffers *p)
  930. {
  931. struct cx23885_fh *fh = priv;
  932. return videobuf_reqbufs(get_queue(fh), p);
  933. }
  934. static int vidioc_querybuf(struct file *file, void *priv,
  935. struct v4l2_buffer *p)
  936. {
  937. struct cx23885_fh *fh = priv;
  938. return videobuf_querybuf(get_queue(fh), p);
  939. }
  940. static int vidioc_qbuf(struct file *file, void *priv,
  941. struct v4l2_buffer *p)
  942. {
  943. struct cx23885_fh *fh = priv;
  944. return videobuf_qbuf(get_queue(fh), p);
  945. }
  946. static int vidioc_dqbuf(struct file *file, void *priv,
  947. struct v4l2_buffer *p)
  948. {
  949. struct cx23885_fh *fh = priv;
  950. return videobuf_dqbuf(get_queue(fh), p,
  951. file->f_flags & O_NONBLOCK);
  952. }
  953. static int vidioc_streamon(struct file *file, void *priv,
  954. enum v4l2_buf_type i)
  955. {
  956. struct cx23885_fh *fh = priv;
  957. struct cx23885_dev *dev = fh->dev;
  958. dprintk(1, "%s()\n", __func__);
  959. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  960. return -EINVAL;
  961. if (unlikely(i != fh->type))
  962. return -EINVAL;
  963. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  964. return -EBUSY;
  965. return videobuf_streamon(get_queue(fh));
  966. }
  967. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  968. {
  969. struct cx23885_fh *fh = priv;
  970. struct cx23885_dev *dev = fh->dev;
  971. int err, res;
  972. dprintk(1, "%s()\n", __func__);
  973. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  974. return -EINVAL;
  975. if (i != fh->type)
  976. return -EINVAL;
  977. res = get_resource(fh);
  978. err = videobuf_streamoff(get_queue(fh));
  979. if (err < 0)
  980. return err;
  981. res_free(dev, fh, res);
  982. return 0;
  983. }
  984. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  985. {
  986. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  987. dprintk(1, "%s()\n", __func__);
  988. mutex_lock(&dev->lock);
  989. cx23885_set_tvnorm(dev, *tvnorms);
  990. mutex_unlock(&dev->lock);
  991. return 0;
  992. }
  993. static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  994. {
  995. static const char *iname[] = {
  996. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  997. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  998. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  999. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1000. [CX23885_VMUX_SVIDEO] = "S-Video",
  1001. [CX23885_VMUX_COMPONENT] = "Component",
  1002. [CX23885_VMUX_TELEVISION] = "Television",
  1003. [CX23885_VMUX_CABLE] = "Cable TV",
  1004. [CX23885_VMUX_DVB] = "DVB",
  1005. [CX23885_VMUX_DEBUG] = "for debug only",
  1006. };
  1007. unsigned int n;
  1008. dprintk(1, "%s()\n", __func__);
  1009. n = i->index;
  1010. if (n >= 4)
  1011. return -EINVAL;
  1012. if (0 == INPUT(n)->type)
  1013. return -EINVAL;
  1014. memset(i, 0, sizeof(*i));
  1015. i->index = n;
  1016. i->type = V4L2_INPUT_TYPE_CAMERA;
  1017. strcpy(i->name, iname[INPUT(n)->type]);
  1018. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1019. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1020. i->type = V4L2_INPUT_TYPE_TUNER;
  1021. i->std = CX23885_NORMS;
  1022. return 0;
  1023. }
  1024. static int vidioc_enum_input(struct file *file, void *priv,
  1025. struct v4l2_input *i)
  1026. {
  1027. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1028. dprintk(1, "%s()\n", __func__);
  1029. return cx23885_enum_input(dev, i);
  1030. }
  1031. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1032. {
  1033. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1034. *i = dev->input;
  1035. dprintk(1, "%s() returns %d\n", __func__, *i);
  1036. return 0;
  1037. }
  1038. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1039. {
  1040. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1041. dprintk(1, "%s(%d)\n", __func__, i);
  1042. if (i >= 4) {
  1043. dprintk(1, "%s() -EINVAL\n", __func__);
  1044. return -EINVAL;
  1045. }
  1046. mutex_lock(&dev->lock);
  1047. cx23885_video_mux(dev, i);
  1048. mutex_unlock(&dev->lock);
  1049. return 0;
  1050. }
  1051. static int vidioc_queryctrl(struct file *file, void *priv,
  1052. struct v4l2_queryctrl *qctrl)
  1053. {
  1054. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1055. if (unlikely(qctrl->id == 0))
  1056. return -EINVAL;
  1057. return cx23885_ctrl_query(qctrl);
  1058. }
  1059. static int vidioc_g_ctrl(struct file *file, void *priv,
  1060. struct v4l2_control *ctl)
  1061. {
  1062. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1063. return cx23885_get_control(dev, ctl);
  1064. }
  1065. static int vidioc_s_ctrl(struct file *file, void *priv,
  1066. struct v4l2_control *ctl)
  1067. {
  1068. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1069. return cx23885_set_control(dev, ctl);
  1070. }
  1071. static int vidioc_g_tuner(struct file *file, void *priv,
  1072. struct v4l2_tuner *t)
  1073. {
  1074. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1075. if (unlikely(UNSET == dev->tuner_type))
  1076. return -EINVAL;
  1077. if (0 != t->index)
  1078. return -EINVAL;
  1079. strcpy(t->name, "Television");
  1080. t->type = V4L2_TUNER_ANALOG_TV;
  1081. t->capability = V4L2_TUNER_CAP_NORM;
  1082. t->rangehigh = 0xffffffffUL;
  1083. t->signal = 0xffff ; /* LOCKED */
  1084. return 0;
  1085. }
  1086. static int vidioc_s_tuner(struct file *file, void *priv,
  1087. struct v4l2_tuner *t)
  1088. {
  1089. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1090. if (UNSET == dev->tuner_type)
  1091. return -EINVAL;
  1092. if (0 != t->index)
  1093. return -EINVAL;
  1094. return 0;
  1095. }
  1096. static int vidioc_g_frequency(struct file *file, void *priv,
  1097. struct v4l2_frequency *f)
  1098. {
  1099. struct cx23885_fh *fh = priv;
  1100. struct cx23885_dev *dev = fh->dev;
  1101. if (unlikely(UNSET == dev->tuner_type))
  1102. return -EINVAL;
  1103. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1104. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1105. f->frequency = dev->freq;
  1106. call_all(dev, tuner, g_frequency, f);
  1107. return 0;
  1108. }
  1109. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1110. {
  1111. if (unlikely(UNSET == dev->tuner_type))
  1112. return -EINVAL;
  1113. if (unlikely(f->tuner != 0))
  1114. return -EINVAL;
  1115. mutex_lock(&dev->lock);
  1116. dev->freq = f->frequency;
  1117. call_all(dev, tuner, s_frequency, f);
  1118. /* When changing channels it is required to reset TVAUDIO */
  1119. msleep(10);
  1120. mutex_unlock(&dev->lock);
  1121. return 0;
  1122. }
  1123. static int vidioc_s_frequency(struct file *file, void *priv,
  1124. struct v4l2_frequency *f)
  1125. {
  1126. struct cx23885_fh *fh = priv;
  1127. struct cx23885_dev *dev = fh->dev;
  1128. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1129. return -EINVAL;
  1130. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1131. return -EINVAL;
  1132. return
  1133. cx23885_set_freq(dev, f);
  1134. }
  1135. /* ----------------------------------------------------------- */
  1136. static void cx23885_vid_timeout(unsigned long data)
  1137. {
  1138. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1139. struct cx23885_dmaqueue *q = &dev->vidq;
  1140. struct cx23885_buffer *buf;
  1141. unsigned long flags;
  1142. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1143. cx_clear(VID_A_DMA_CTL, 0x11);
  1144. spin_lock_irqsave(&dev->slock, flags);
  1145. while (!list_empty(&q->active)) {
  1146. buf = list_entry(q->active.next,
  1147. struct cx23885_buffer, vb.queue);
  1148. list_del(&buf->vb.queue);
  1149. buf->vb.state = VIDEOBUF_ERROR;
  1150. wake_up(&buf->vb.done);
  1151. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1152. dev->name, buf, buf->vb.i,
  1153. (unsigned long)buf->risc.dma);
  1154. }
  1155. cx23885_restart_video_queue(dev, q);
  1156. spin_unlock_irqrestore(&dev->slock, flags);
  1157. }
  1158. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1159. {
  1160. u32 mask, count;
  1161. int handled = 0;
  1162. mask = cx_read(VID_A_INT_MSK);
  1163. if (0 == (status & mask))
  1164. return handled;
  1165. cx_write(VID_A_INT_STAT, status);
  1166. dprintk(2, "%s() status = 0x%08x\n", __func__, status);
  1167. /* risc op code error */
  1168. if (status & (1 << 16)) {
  1169. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1170. dev->name);
  1171. cx_clear(VID_A_DMA_CTL, 0x11);
  1172. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1173. }
  1174. /* risc1 y */
  1175. if (status & 0x01) {
  1176. spin_lock(&dev->slock);
  1177. count = cx_read(VID_A_GPCNT);
  1178. cx23885_video_wakeup(dev, &dev->vidq, count);
  1179. spin_unlock(&dev->slock);
  1180. handled++;
  1181. }
  1182. /* risc2 y */
  1183. if (status & 0x10) {
  1184. dprintk(2, "stopper video\n");
  1185. spin_lock(&dev->slock);
  1186. cx23885_restart_video_queue(dev, &dev->vidq);
  1187. spin_unlock(&dev->slock);
  1188. handled++;
  1189. }
  1190. return handled;
  1191. }
  1192. /* ----------------------------------------------------------- */
  1193. /* exported stuff */
  1194. static const struct v4l2_file_operations video_fops = {
  1195. .owner = THIS_MODULE,
  1196. .open = video_open,
  1197. .release = video_release,
  1198. .read = video_read,
  1199. .poll = video_poll,
  1200. .mmap = video_mmap,
  1201. .ioctl = video_ioctl2,
  1202. };
  1203. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1204. .vidioc_querycap = vidioc_querycap,
  1205. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1206. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1207. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1208. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1209. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1210. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1211. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1212. .vidioc_reqbufs = vidioc_reqbufs,
  1213. .vidioc_querybuf = vidioc_querybuf,
  1214. .vidioc_qbuf = vidioc_qbuf,
  1215. .vidioc_dqbuf = vidioc_dqbuf,
  1216. .vidioc_s_std = vidioc_s_std,
  1217. .vidioc_enum_input = vidioc_enum_input,
  1218. .vidioc_g_input = vidioc_g_input,
  1219. .vidioc_s_input = vidioc_s_input,
  1220. .vidioc_queryctrl = vidioc_queryctrl,
  1221. .vidioc_g_ctrl = vidioc_g_ctrl,
  1222. .vidioc_s_ctrl = vidioc_s_ctrl,
  1223. .vidioc_streamon = vidioc_streamon,
  1224. .vidioc_streamoff = vidioc_streamoff,
  1225. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1226. .vidiocgmbuf = vidiocgmbuf,
  1227. #endif
  1228. .vidioc_g_tuner = vidioc_g_tuner,
  1229. .vidioc_s_tuner = vidioc_s_tuner,
  1230. .vidioc_g_frequency = vidioc_g_frequency,
  1231. .vidioc_s_frequency = vidioc_s_frequency,
  1232. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1233. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1234. .vidioc_g_register = cx23885_g_register,
  1235. .vidioc_s_register = cx23885_s_register,
  1236. #endif
  1237. };
  1238. static struct video_device cx23885_vbi_template;
  1239. static struct video_device cx23885_video_template = {
  1240. .name = "cx23885-video",
  1241. .fops = &video_fops,
  1242. .ioctl_ops = &video_ioctl_ops,
  1243. .tvnorms = CX23885_NORMS,
  1244. .current_norm = V4L2_STD_NTSC_M,
  1245. };
  1246. static const struct v4l2_file_operations radio_fops = {
  1247. .owner = THIS_MODULE,
  1248. .open = video_open,
  1249. .release = video_release,
  1250. .ioctl = video_ioctl2,
  1251. };
  1252. void cx23885_video_unregister(struct cx23885_dev *dev)
  1253. {
  1254. dprintk(1, "%s()\n", __func__);
  1255. cx_clear(PCI_INT_MSK, 1);
  1256. if (dev->video_dev) {
  1257. if (video_is_registered(dev->video_dev))
  1258. video_unregister_device(dev->video_dev);
  1259. else
  1260. video_device_release(dev->video_dev);
  1261. dev->video_dev = NULL;
  1262. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1263. }
  1264. }
  1265. int cx23885_video_register(struct cx23885_dev *dev)
  1266. {
  1267. int err;
  1268. dprintk(1, "%s()\n", __func__);
  1269. spin_lock_init(&dev->slock);
  1270. /* Initialize VBI template */
  1271. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1272. sizeof(cx23885_vbi_template));
  1273. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1274. dev->tvnorm = cx23885_video_template.current_norm;
  1275. /* init video dma queues */
  1276. INIT_LIST_HEAD(&dev->vidq.active);
  1277. INIT_LIST_HEAD(&dev->vidq.queued);
  1278. dev->vidq.timeout.function = cx23885_vid_timeout;
  1279. dev->vidq.timeout.data = (unsigned long)dev;
  1280. init_timer(&dev->vidq.timeout);
  1281. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1282. VID_A_DMA_CTL, 0x11, 0x00);
  1283. /* Don't enable VBI yet */
  1284. cx_set(PCI_INT_MSK, 1);
  1285. if (TUNER_ABSENT != dev->tuner_type) {
  1286. struct v4l2_subdev *sd = NULL;
  1287. if (dev->tuner_addr)
  1288. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1289. &dev->i2c_bus[1].i2c_adap,
  1290. "tuner", "tuner", dev->tuner_addr, NULL);
  1291. else
  1292. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1293. &dev->i2c_bus[1].i2c_adap,
  1294. "tuner", "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1295. if (sd) {
  1296. struct tuner_setup tun_setup;
  1297. memset(&tun_setup, 0, sizeof(tun_setup));
  1298. tun_setup.mode_mask = T_ANALOG_TV;
  1299. tun_setup.type = dev->tuner_type;
  1300. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1301. tun_setup.tuner_callback = cx23885_tuner_callback;
  1302. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1303. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1304. struct xc2028_ctrl ctrl = {
  1305. .fname = XC2028_DEFAULT_FIRMWARE,
  1306. .max_len = 64
  1307. };
  1308. struct v4l2_priv_tun_config cfg = {
  1309. .tuner = dev->tuner_type,
  1310. .priv = &ctrl
  1311. };
  1312. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1313. }
  1314. }
  1315. }
  1316. /* register v4l devices */
  1317. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1318. &cx23885_video_template, "video");
  1319. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1320. video_nr[dev->nr]);
  1321. if (err < 0) {
  1322. printk(KERN_INFO "%s: can't register video device\n",
  1323. dev->name);
  1324. goto fail_unreg;
  1325. }
  1326. printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
  1327. dev->name, video_device_node_name(dev->video_dev));
  1328. /* initial device configuration */
  1329. mutex_lock(&dev->lock);
  1330. cx23885_set_tvnorm(dev, dev->tvnorm);
  1331. init_controls(dev);
  1332. cx23885_video_mux(dev, 0);
  1333. mutex_unlock(&dev->lock);
  1334. return 0;
  1335. fail_unreg:
  1336. cx23885_video_unregister(dev);
  1337. return err;
  1338. }