cx25821-video.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. * Parts adapted/taken from Eduardo Moscoso Rubino
  8. * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include "cx25821-video.h"
  28. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  29. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  32. module_param_array(video_nr, int, NULL, 0444);
  33. MODULE_PARM_DESC(video_nr, "video device numbers");
  34. static unsigned int video_debug = VIDEO_DEBUG;
  35. module_param(video_debug, int, 0644);
  36. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  37. static unsigned int irq_debug;
  38. module_param(irq_debug, int, 0644);
  39. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  40. static unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. #define FORMAT_FLAGS_PACKED 0x01
  44. static const struct cx25821_fmt formats[] = {
  45. {
  46. .name = "4:1:1, packed, Y41P",
  47. .fourcc = V4L2_PIX_FMT_Y41P,
  48. .depth = 12,
  49. .flags = FORMAT_FLAGS_PACKED,
  50. }, {
  51. .name = "4:2:2, packed, YUYV",
  52. .fourcc = V4L2_PIX_FMT_YUYV,
  53. .depth = 16,
  54. .flags = FORMAT_FLAGS_PACKED,
  55. },
  56. };
  57. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  58. {
  59. unsigned int i;
  60. for (i = 0; i < ARRAY_SIZE(formats); i++)
  61. if (formats[i].fourcc == fourcc)
  62. return formats + i;
  63. return NULL;
  64. }
  65. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  66. u32 count)
  67. {
  68. struct cx25821_buffer *buf;
  69. int bc;
  70. for (bc = 0;; bc++) {
  71. if (list_empty(&q->active)) {
  72. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  73. break;
  74. }
  75. buf = list_entry(q->active.next, struct cx25821_buffer,
  76. vb.queue);
  77. /* count comes from the hw and it is 16bit wide --
  78. * this trick handles wrap-arounds correctly for
  79. * up to 32767 buffers in flight... */
  80. if ((s16) (count - buf->count) < 0)
  81. break;
  82. v4l2_get_timestamp(&buf->vb.ts);
  83. buf->vb.state = VIDEOBUF_DONE;
  84. list_del(&buf->vb.queue);
  85. wake_up(&buf->vb.done);
  86. }
  87. if (list_empty(&q->active))
  88. del_timer(&q->timeout);
  89. else
  90. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  91. if (bc != 1)
  92. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  93. }
  94. static int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
  95. {
  96. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  97. __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
  98. dev->tvnorm = norm;
  99. /* Tell the internal A/V decoder */
  100. cx25821_call_all(dev, core, s_std, norm);
  101. return 0;
  102. }
  103. static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
  104. {
  105. struct v4l2_routing route;
  106. memset(&route, 0, sizeof(route));
  107. dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  108. __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
  109. INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
  110. dev->input = input;
  111. route.input = INPUT(input)->vmux;
  112. /* Tell the internal A/V decoder */
  113. cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
  114. return 0;
  115. }
  116. int cx25821_start_video_dma(struct cx25821_dev *dev,
  117. struct cx25821_dmaqueue *q,
  118. struct cx25821_buffer *buf,
  119. const struct sram_channel *channel)
  120. {
  121. int tmp = 0;
  122. /* setup fifo + format */
  123. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  124. /* reset counter */
  125. cx_write(channel->gpcnt_ctl, 3);
  126. q->count = 1;
  127. /* enable irq */
  128. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  129. cx_set(channel->int_msk, 0x11);
  130. /* start dma */
  131. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  132. /* make sure upstream setting if any is reversed */
  133. tmp = cx_read(VID_CH_MODE_SEL);
  134. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  135. return 0;
  136. }
  137. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  138. struct cx25821_dmaqueue *q,
  139. const struct sram_channel *channel)
  140. {
  141. struct cx25821_buffer *buf, *prev;
  142. struct list_head *item;
  143. if (!list_empty(&q->active)) {
  144. buf = list_entry(q->active.next, struct cx25821_buffer,
  145. vb.queue);
  146. cx25821_start_video_dma(dev, q, buf, channel);
  147. list_for_each(item, &q->active) {
  148. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  149. buf->count = q->count++;
  150. }
  151. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  152. return 0;
  153. }
  154. prev = NULL;
  155. for (;;) {
  156. if (list_empty(&q->queued))
  157. return 0;
  158. buf = list_entry(q->queued.next, struct cx25821_buffer,
  159. vb.queue);
  160. if (NULL == prev) {
  161. list_move_tail(&buf->vb.queue, &q->active);
  162. cx25821_start_video_dma(dev, q, buf, channel);
  163. buf->vb.state = VIDEOBUF_ACTIVE;
  164. buf->count = q->count++;
  165. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  166. } else if (prev->vb.width == buf->vb.width &&
  167. prev->vb.height == buf->vb.height &&
  168. prev->fmt == buf->fmt) {
  169. list_move_tail(&buf->vb.queue, &q->active);
  170. buf->vb.state = VIDEOBUF_ACTIVE;
  171. buf->count = q->count++;
  172. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  173. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  174. } else {
  175. return 0;
  176. }
  177. prev = buf;
  178. }
  179. }
  180. static void cx25821_vid_timeout(unsigned long data)
  181. {
  182. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  183. struct cx25821_dev *dev = timeout_data->dev;
  184. const struct sram_channel *channel = timeout_data->channel;
  185. struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
  186. struct cx25821_buffer *buf;
  187. unsigned long flags;
  188. /* cx25821_sram_channel_dump(dev, channel); */
  189. cx_clear(channel->dma_ctl, 0x11);
  190. spin_lock_irqsave(&dev->slock, flags);
  191. while (!list_empty(&q->active)) {
  192. buf = list_entry(q->active.next, struct cx25821_buffer,
  193. vb.queue);
  194. list_del(&buf->vb.queue);
  195. buf->vb.state = VIDEOBUF_ERROR;
  196. wake_up(&buf->vb.done);
  197. }
  198. cx25821_restart_video_queue(dev, q, channel);
  199. spin_unlock_irqrestore(&dev->slock, flags);
  200. }
  201. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  202. {
  203. u32 count = 0;
  204. int handled = 0;
  205. u32 mask;
  206. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  207. mask = cx_read(channel->int_msk);
  208. if (0 == (status & mask))
  209. return handled;
  210. cx_write(channel->int_stat, status);
  211. /* risc op code error */
  212. if (status & (1 << 16)) {
  213. pr_warn("%s, %s: video risc op code error\n",
  214. dev->name, channel->name);
  215. cx_clear(channel->dma_ctl, 0x11);
  216. cx25821_sram_channel_dump(dev, channel);
  217. }
  218. /* risc1 y */
  219. if (status & FLD_VID_DST_RISC1) {
  220. spin_lock(&dev->slock);
  221. count = cx_read(channel->gpcnt);
  222. cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
  223. count);
  224. spin_unlock(&dev->slock);
  225. handled++;
  226. }
  227. /* risc2 y */
  228. if (status & 0x10) {
  229. dprintk(2, "stopper video\n");
  230. spin_lock(&dev->slock);
  231. cx25821_restart_video_queue(dev,
  232. &dev->channels[channel->i].dma_vidq, channel);
  233. spin_unlock(&dev->slock);
  234. handled++;
  235. }
  236. return handled;
  237. }
  238. static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  239. unsigned int *size)
  240. {
  241. struct cx25821_channel *chan = q->priv_data;
  242. *size = chan->fmt->depth * chan->width * chan->height >> 3;
  243. if (0 == *count)
  244. *count = 32;
  245. if (*size * *count > vid_limit * 1024 * 1024)
  246. *count = (vid_limit * 1024 * 1024) / *size;
  247. return 0;
  248. }
  249. static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  250. enum v4l2_field field)
  251. {
  252. struct cx25821_channel *chan = q->priv_data;
  253. struct cx25821_dev *dev = chan->dev;
  254. struct cx25821_buffer *buf =
  255. container_of(vb, struct cx25821_buffer, vb);
  256. int rc, init_buffer = 0;
  257. u32 line0_offset;
  258. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  259. int bpl_local = LINE_SIZE_D1;
  260. BUG_ON(NULL == chan->fmt);
  261. if (chan->width < 48 || chan->width > 720 ||
  262. chan->height < 32 || chan->height > 576)
  263. return -EINVAL;
  264. buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
  265. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  266. return -EINVAL;
  267. if (buf->fmt != chan->fmt ||
  268. buf->vb.width != chan->width ||
  269. buf->vb.height != chan->height || buf->vb.field != field) {
  270. buf->fmt = chan->fmt;
  271. buf->vb.width = chan->width;
  272. buf->vb.height = chan->height;
  273. buf->vb.field = field;
  274. init_buffer = 1;
  275. }
  276. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  277. init_buffer = 1;
  278. rc = videobuf_iolock(q, &buf->vb, NULL);
  279. if (0 != rc) {
  280. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  281. goto fail;
  282. }
  283. }
  284. dprintk(1, "init_buffer=%d\n", init_buffer);
  285. if (init_buffer) {
  286. if (chan->pixel_formats == PIXEL_FRMT_411)
  287. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  288. else
  289. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  290. if (chan->pixel_formats == PIXEL_FRMT_411) {
  291. bpl_local = buf->bpl;
  292. } else {
  293. bpl_local = buf->bpl; /* Default */
  294. if (chan->use_cif_resolution) {
  295. if (dev->tvnorm & V4L2_STD_625_50)
  296. bpl_local = 352 << 1;
  297. else
  298. bpl_local = chan->cif_width << 1;
  299. }
  300. }
  301. switch (buf->vb.field) {
  302. case V4L2_FIELD_TOP:
  303. cx25821_risc_buffer(dev->pci, &buf->risc,
  304. dma->sglist, 0, UNSET,
  305. buf->bpl, 0, buf->vb.height);
  306. break;
  307. case V4L2_FIELD_BOTTOM:
  308. cx25821_risc_buffer(dev->pci, &buf->risc,
  309. dma->sglist, UNSET, 0,
  310. buf->bpl, 0, buf->vb.height);
  311. break;
  312. case V4L2_FIELD_INTERLACED:
  313. /* All other formats are top field first */
  314. line0_offset = 0;
  315. dprintk(1, "top field first\n");
  316. cx25821_risc_buffer(dev->pci, &buf->risc,
  317. dma->sglist, line0_offset,
  318. bpl_local, bpl_local, bpl_local,
  319. buf->vb.height >> 1);
  320. break;
  321. case V4L2_FIELD_SEQ_TB:
  322. cx25821_risc_buffer(dev->pci, &buf->risc,
  323. dma->sglist,
  324. 0, buf->bpl * (buf->vb.height >> 1),
  325. buf->bpl, 0, buf->vb.height >> 1);
  326. break;
  327. case V4L2_FIELD_SEQ_BT:
  328. cx25821_risc_buffer(dev->pci, &buf->risc,
  329. dma->sglist,
  330. buf->bpl * (buf->vb.height >> 1), 0,
  331. buf->bpl, 0, buf->vb.height >> 1);
  332. break;
  333. default:
  334. BUG();
  335. }
  336. }
  337. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  338. buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
  339. chan->fmt->name, (unsigned long)buf->risc.dma);
  340. buf->vb.state = VIDEOBUF_PREPARED;
  341. return 0;
  342. fail:
  343. cx25821_free_buffer(q, buf);
  344. return rc;
  345. }
  346. static void cx25821_buffer_release(struct videobuf_queue *q,
  347. struct videobuf_buffer *vb)
  348. {
  349. struct cx25821_buffer *buf =
  350. container_of(vb, struct cx25821_buffer, vb);
  351. cx25821_free_buffer(q, buf);
  352. }
  353. static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  354. {
  355. struct cx25821_channel *chan = video_drvdata(file);
  356. return videobuf_mmap_mapper(&chan->vidq, vma);
  357. }
  358. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  359. {
  360. struct cx25821_buffer *buf =
  361. container_of(vb, struct cx25821_buffer, vb);
  362. struct cx25821_buffer *prev;
  363. struct cx25821_channel *chan = vq->priv_data;
  364. struct cx25821_dev *dev = chan->dev;
  365. struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
  366. /* add jump to stopper */
  367. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  368. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  369. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  370. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  371. if (!list_empty(&q->queued)) {
  372. list_add_tail(&buf->vb.queue, &q->queued);
  373. buf->vb.state = VIDEOBUF_QUEUED;
  374. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  375. buf->vb.i);
  376. } else if (list_empty(&q->active)) {
  377. list_add_tail(&buf->vb.queue, &q->active);
  378. cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
  379. buf->vb.state = VIDEOBUF_ACTIVE;
  380. buf->count = q->count++;
  381. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  382. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  383. buf, buf->vb.i, buf->count, q->count);
  384. } else {
  385. prev = list_entry(q->active.prev, struct cx25821_buffer,
  386. vb.queue);
  387. if (prev->vb.width == buf->vb.width
  388. && prev->vb.height == buf->vb.height
  389. && prev->fmt == buf->fmt) {
  390. list_add_tail(&buf->vb.queue, &q->active);
  391. buf->vb.state = VIDEOBUF_ACTIVE;
  392. buf->count = q->count++;
  393. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  394. /* 64 bit bits 63-32 */
  395. prev->risc.jmp[2] = cpu_to_le32(0);
  396. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  397. buf, buf->vb.i, buf->count);
  398. } else {
  399. list_add_tail(&buf->vb.queue, &q->queued);
  400. buf->vb.state = VIDEOBUF_QUEUED;
  401. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  402. buf->vb.i);
  403. }
  404. }
  405. if (list_empty(&q->active))
  406. dprintk(2, "active queue empty!\n");
  407. }
  408. static struct videobuf_queue_ops cx25821_video_qops = {
  409. .buf_setup = cx25821_buffer_setup,
  410. .buf_prepare = cx25821_buffer_prepare,
  411. .buf_queue = buffer_queue,
  412. .buf_release = cx25821_buffer_release,
  413. };
  414. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  415. loff_t *ppos)
  416. {
  417. struct v4l2_fh *fh = file->private_data;
  418. struct cx25821_channel *chan = video_drvdata(file);
  419. struct cx25821_dev *dev = chan->dev;
  420. int err = 0;
  421. if (mutex_lock_interruptible(&dev->lock))
  422. return -ERESTARTSYS;
  423. if (chan->streaming_fh && chan->streaming_fh != fh) {
  424. err = -EBUSY;
  425. goto unlock;
  426. }
  427. chan->streaming_fh = fh;
  428. err = videobuf_read_one(&chan->vidq, data, count, ppos,
  429. file->f_flags & O_NONBLOCK);
  430. unlock:
  431. mutex_unlock(&dev->lock);
  432. return err;
  433. }
  434. static unsigned int video_poll(struct file *file,
  435. struct poll_table_struct *wait)
  436. {
  437. struct cx25821_channel *chan = video_drvdata(file);
  438. unsigned long req_events = poll_requested_events(wait);
  439. unsigned int res = v4l2_ctrl_poll(file, wait);
  440. if (req_events & (POLLIN | POLLRDNORM))
  441. res |= videobuf_poll_stream(file, &chan->vidq, wait);
  442. return res;
  443. /* This doesn't belong in poll(). This can be done
  444. * much better with vb2. We keep this code here as a
  445. * reminder.
  446. if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
  447. struct cx25821_dev *dev = chan->dev;
  448. if (dev && chan->use_cif_resolution) {
  449. u8 cam_id = *((char *)buf->vb.baddr + 3);
  450. memcpy((char *)buf->vb.baddr,
  451. (char *)buf->vb.baddr + (chan->width * 2),
  452. (chan->width * 2));
  453. *((char *)buf->vb.baddr + 3) = cam_id;
  454. }
  455. }
  456. */
  457. }
  458. static int video_release(struct file *file)
  459. {
  460. struct cx25821_channel *chan = video_drvdata(file);
  461. struct v4l2_fh *fh = file->private_data;
  462. struct cx25821_dev *dev = chan->dev;
  463. const struct sram_channel *sram_ch =
  464. dev->channels[0].sram_channels;
  465. mutex_lock(&dev->lock);
  466. /* stop the risc engine and fifo */
  467. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  468. /* stop video capture */
  469. if (chan->streaming_fh == fh) {
  470. videobuf_queue_cancel(&chan->vidq);
  471. chan->streaming_fh = NULL;
  472. }
  473. if (chan->vidq.read_buf) {
  474. cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
  475. kfree(chan->vidq.read_buf);
  476. }
  477. videobuf_mmap_free(&chan->vidq);
  478. mutex_unlock(&dev->lock);
  479. return v4l2_fh_release(file);
  480. }
  481. /* VIDEO IOCTLS */
  482. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  483. struct v4l2_format *f)
  484. {
  485. struct cx25821_channel *chan = video_drvdata(file);
  486. f->fmt.pix.width = chan->width;
  487. f->fmt.pix.height = chan->height;
  488. f->fmt.pix.field = chan->vidq.field;
  489. f->fmt.pix.pixelformat = chan->fmt->fourcc;
  490. f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
  491. f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
  492. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  493. f->fmt.pix.priv = 0;
  494. return 0;
  495. }
  496. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  497. struct v4l2_format *f)
  498. {
  499. struct cx25821_channel *chan = video_drvdata(file);
  500. struct cx25821_dev *dev = chan->dev;
  501. const struct cx25821_fmt *fmt;
  502. enum v4l2_field field = f->fmt.pix.field;
  503. unsigned int maxw, maxh;
  504. unsigned w;
  505. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  506. if (NULL == fmt)
  507. return -EINVAL;
  508. maxw = 720;
  509. maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  510. w = f->fmt.pix.width;
  511. if (field != V4L2_FIELD_BOTTOM)
  512. field = V4L2_FIELD_TOP;
  513. if (w < 352) {
  514. w = 176;
  515. f->fmt.pix.height = maxh / 4;
  516. } else if (w < 720) {
  517. w = 352;
  518. f->fmt.pix.height = maxh / 2;
  519. } else {
  520. w = 720;
  521. f->fmt.pix.height = maxh;
  522. field = V4L2_FIELD_INTERLACED;
  523. }
  524. f->fmt.pix.field = field;
  525. f->fmt.pix.width = w;
  526. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  527. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  528. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  529. f->fmt.pix.priv = 0;
  530. return 0;
  531. }
  532. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  533. {
  534. struct cx25821_channel *chan = video_drvdata(file);
  535. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  536. return -EINVAL;
  537. if (chan->streaming_fh && chan->streaming_fh != priv)
  538. return -EBUSY;
  539. chan->streaming_fh = priv;
  540. return videobuf_streamon(&chan->vidq);
  541. }
  542. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  543. {
  544. struct cx25821_channel *chan = video_drvdata(file);
  545. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  546. return -EINVAL;
  547. if (chan->streaming_fh && chan->streaming_fh != priv)
  548. return -EBUSY;
  549. if (chan->streaming_fh == NULL)
  550. return 0;
  551. chan->streaming_fh = NULL;
  552. return videobuf_streamoff(&chan->vidq);
  553. }
  554. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  555. struct v4l2_format *f)
  556. {
  557. struct cx25821_channel *chan = video_drvdata(file);
  558. struct cx25821_dev *dev = chan->dev;
  559. struct v4l2_mbus_framefmt mbus_fmt;
  560. int err;
  561. int pix_format = PIXEL_FRMT_422;
  562. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  563. if (0 != err)
  564. return err;
  565. chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  566. chan->vidq.field = f->fmt.pix.field;
  567. chan->width = f->fmt.pix.width;
  568. chan->height = f->fmt.pix.height;
  569. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  570. pix_format = PIXEL_FRMT_411;
  571. else
  572. pix_format = PIXEL_FRMT_422;
  573. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  574. /* check if cif resolution */
  575. if (chan->width == 320 || chan->width == 352)
  576. chan->use_cif_resolution = 1;
  577. else
  578. chan->use_cif_resolution = 0;
  579. chan->cif_width = chan->width;
  580. medusa_set_resolution(dev, chan->width, SRAM_CH00);
  581. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  582. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  583. return 0;
  584. }
  585. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  586. {
  587. int ret_val = 0;
  588. struct cx25821_channel *chan = video_drvdata(file);
  589. ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
  590. p->sequence = chan->dma_vidq.count;
  591. return ret_val;
  592. }
  593. static int vidioc_log_status(struct file *file, void *priv)
  594. {
  595. struct cx25821_channel *chan = video_drvdata(file);
  596. struct cx25821_dev *dev = chan->dev;
  597. const struct sram_channel *sram_ch = chan->sram_channels;
  598. u32 tmp = 0;
  599. cx25821_call_all(dev, core, log_status);
  600. tmp = cx_read(sram_ch->dma_ctl);
  601. pr_info("Video input 0 is %s\n",
  602. (tmp & 0x11) ? "streaming" : "stopped");
  603. return 0;
  604. }
  605. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  606. struct v4l2_capability *cap)
  607. {
  608. struct cx25821_channel *chan = video_drvdata(file);
  609. struct cx25821_dev *dev = chan->dev;
  610. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  611. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  612. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  613. strcpy(cap->driver, "cx25821");
  614. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  615. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  616. if (chan->id >= VID_CHANNEL_NUM)
  617. cap->device_caps = cap_output;
  618. else
  619. cap->device_caps = cap_input;
  620. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  621. return 0;
  622. }
  623. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  624. struct v4l2_fmtdesc *f)
  625. {
  626. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  627. return -EINVAL;
  628. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  629. f->pixelformat = formats[f->index].fourcc;
  630. return 0;
  631. }
  632. static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  633. struct v4l2_requestbuffers *p)
  634. {
  635. struct cx25821_channel *chan = video_drvdata(file);
  636. return videobuf_reqbufs(&chan->vidq, p);
  637. }
  638. static int cx25821_vidioc_querybuf(struct file *file, void *priv,
  639. struct v4l2_buffer *p)
  640. {
  641. struct cx25821_channel *chan = video_drvdata(file);
  642. return videobuf_querybuf(&chan->vidq, p);
  643. }
  644. static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  645. {
  646. struct cx25821_channel *chan = video_drvdata(file);
  647. return videobuf_qbuf(&chan->vidq, p);
  648. }
  649. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  650. {
  651. struct cx25821_channel *chan = video_drvdata(file);
  652. *tvnorms = chan->dev->tvnorm;
  653. return 0;
  654. }
  655. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  656. {
  657. struct cx25821_channel *chan = video_drvdata(file);
  658. struct cx25821_dev *dev = chan->dev;
  659. if (dev->tvnorm == tvnorms)
  660. return 0;
  661. cx25821_set_tvnorm(dev, tvnorms);
  662. chan->width = 720;
  663. chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
  664. medusa_set_videostandard(dev);
  665. return 0;
  666. }
  667. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  668. struct v4l2_input *i)
  669. {
  670. static const char * const iname[] = {
  671. [CX25821_VMUX_COMPOSITE] = "Composite",
  672. [CX25821_VMUX_SVIDEO] = "S-Video",
  673. [CX25821_VMUX_DEBUG] = "for debug only",
  674. };
  675. struct cx25821_channel *chan = video_drvdata(file);
  676. struct cx25821_dev *dev = chan->dev;
  677. unsigned int n;
  678. n = i->index;
  679. if (n >= CX25821_NR_INPUT)
  680. return -EINVAL;
  681. if (0 == INPUT(n)->type)
  682. return -EINVAL;
  683. i->type = V4L2_INPUT_TYPE_CAMERA;
  684. strcpy(i->name, iname[INPUT(n)->type]);
  685. i->std = CX25821_NORMS;
  686. return 0;
  687. }
  688. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  689. {
  690. struct cx25821_channel *chan = video_drvdata(file);
  691. struct cx25821_dev *dev = chan->dev;
  692. *i = dev->input;
  693. return 0;
  694. }
  695. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  696. {
  697. struct cx25821_channel *chan = video_drvdata(file);
  698. struct cx25821_dev *dev = chan->dev;
  699. if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
  700. return -EINVAL;
  701. cx25821_video_mux(dev, i);
  702. return 0;
  703. }
  704. #ifdef CONFIG_VIDEO_ADV_DEBUG
  705. int cx25821_vidioc_g_register(struct file *file, void *fh,
  706. struct v4l2_dbg_register *reg)
  707. {
  708. struct cx25821_channel *chan = video_drvdata(file);
  709. struct cx25821_dev *dev = chan->dev;
  710. if (!v4l2_chip_match_host(&reg->match))
  711. return -EINVAL;
  712. cx25821_call_all(dev, core, g_register, reg);
  713. return 0;
  714. }
  715. int cx25821_vidioc_s_register(struct file *file, void *fh,
  716. const struct v4l2_dbg_register *reg)
  717. {
  718. struct cx25821_channel *chan = video_drvdata(file);
  719. struct cx25821_dev *dev = chan->dev;
  720. if (!v4l2_chip_match_host(&reg->match))
  721. return -EINVAL;
  722. cx25821_call_all(dev, core, s_register, reg);
  723. return 0;
  724. }
  725. #endif
  726. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  727. {
  728. struct cx25821_channel *chan =
  729. container_of(ctrl->handler, struct cx25821_channel, hdl);
  730. struct cx25821_dev *dev = chan->dev;
  731. switch (ctrl->id) {
  732. case V4L2_CID_BRIGHTNESS:
  733. medusa_set_brightness(dev, ctrl->val, chan->id);
  734. break;
  735. case V4L2_CID_HUE:
  736. medusa_set_hue(dev, ctrl->val, chan->id);
  737. break;
  738. case V4L2_CID_CONTRAST:
  739. medusa_set_contrast(dev, ctrl->val, chan->id);
  740. break;
  741. case V4L2_CID_SATURATION:
  742. medusa_set_saturation(dev, ctrl->val, chan->id);
  743. break;
  744. default:
  745. return -EINVAL;
  746. }
  747. return 0;
  748. }
  749. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  750. unsigned long arg)
  751. {
  752. struct cx25821_channel *chan = video_drvdata(file);
  753. struct cx25821_dev *dev = chan->dev;
  754. int command = 0;
  755. struct upstream_user_struct *data_from_user;
  756. data_from_user = (struct upstream_user_struct *)arg;
  757. if (!data_from_user) {
  758. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  759. return 0;
  760. }
  761. command = data_from_user->command;
  762. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  763. return 0;
  764. dev->input_filename = data_from_user->input_filename;
  765. dev->input_audiofilename = data_from_user->input_filename;
  766. dev->vid_stdname = data_from_user->vid_stdname;
  767. dev->pixel_format = data_from_user->pixel_format;
  768. dev->channel_select = data_from_user->channel_select;
  769. dev->command = data_from_user->command;
  770. switch (command) {
  771. case UPSTREAM_START_VIDEO:
  772. cx25821_start_upstream_video_ch1(dev, data_from_user);
  773. break;
  774. case UPSTREAM_STOP_VIDEO:
  775. cx25821_stop_upstream_video_ch1(dev);
  776. break;
  777. }
  778. return 0;
  779. }
  780. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  781. unsigned long arg)
  782. {
  783. struct cx25821_channel *chan = video_drvdata(file);
  784. struct cx25821_dev *dev = chan->dev;
  785. int command = 0;
  786. struct upstream_user_struct *data_from_user;
  787. data_from_user = (struct upstream_user_struct *)arg;
  788. if (!data_from_user) {
  789. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  790. return 0;
  791. }
  792. command = data_from_user->command;
  793. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  794. return 0;
  795. dev->input_filename_ch2 = data_from_user->input_filename;
  796. dev->input_audiofilename = data_from_user->input_filename;
  797. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  798. dev->pixel_format_ch2 = data_from_user->pixel_format;
  799. dev->channel_select_ch2 = data_from_user->channel_select;
  800. dev->command_ch2 = data_from_user->command;
  801. switch (command) {
  802. case UPSTREAM_START_VIDEO:
  803. cx25821_start_upstream_video_ch2(dev, data_from_user);
  804. break;
  805. case UPSTREAM_STOP_VIDEO:
  806. cx25821_stop_upstream_video_ch2(dev);
  807. break;
  808. }
  809. return 0;
  810. }
  811. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  812. unsigned long arg)
  813. {
  814. struct cx25821_channel *chan = video_drvdata(file);
  815. struct cx25821_dev *dev = chan->dev;
  816. int command = 0;
  817. struct upstream_user_struct *data_from_user;
  818. data_from_user = (struct upstream_user_struct *)arg;
  819. if (!data_from_user) {
  820. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  821. return 0;
  822. }
  823. command = data_from_user->command;
  824. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  825. return 0;
  826. dev->input_filename = data_from_user->input_filename;
  827. dev->input_audiofilename = data_from_user->input_filename;
  828. dev->vid_stdname = data_from_user->vid_stdname;
  829. dev->pixel_format = data_from_user->pixel_format;
  830. dev->channel_select = data_from_user->channel_select;
  831. dev->command = data_from_user->command;
  832. switch (command) {
  833. case UPSTREAM_START_AUDIO:
  834. cx25821_start_upstream_audio(dev, data_from_user);
  835. break;
  836. case UPSTREAM_STOP_AUDIO:
  837. cx25821_stop_upstream_audio(dev);
  838. break;
  839. }
  840. return 0;
  841. }
  842. static long video_ioctl_set(struct file *file, unsigned int cmd,
  843. unsigned long arg)
  844. {
  845. struct cx25821_channel *chan = video_drvdata(file);
  846. struct cx25821_dev *dev = chan->dev;
  847. struct downstream_user_struct *data_from_user;
  848. int command;
  849. int width = 720;
  850. int selected_channel = 0;
  851. int pix_format = 0;
  852. int i = 0;
  853. int cif_enable = 0;
  854. int cif_width = 0;
  855. data_from_user = (struct downstream_user_struct *)arg;
  856. if (!data_from_user) {
  857. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  858. return 0;
  859. }
  860. command = data_from_user->command;
  861. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  862. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  863. && command != REG_WRITE && command != MEDUSA_READ
  864. && command != MEDUSA_WRITE) {
  865. return 0;
  866. }
  867. switch (command) {
  868. case SET_VIDEO_STD:
  869. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  870. dev->tvnorm = V4L2_STD_PAL_BG;
  871. else
  872. dev->tvnorm = V4L2_STD_NTSC_M;
  873. medusa_set_videostandard(dev);
  874. break;
  875. case SET_PIXEL_FORMAT:
  876. selected_channel = data_from_user->decoder_select;
  877. pix_format = data_from_user->pixel_format;
  878. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  879. selected_channel -= 4;
  880. selected_channel = selected_channel % 8;
  881. }
  882. if (selected_channel >= 0)
  883. cx25821_set_pixel_format(dev, selected_channel,
  884. pix_format);
  885. break;
  886. case ENABLE_CIF_RESOLUTION:
  887. selected_channel = data_from_user->decoder_select;
  888. cif_enable = data_from_user->cif_resolution_enable;
  889. cif_width = data_from_user->cif_width;
  890. if (cif_enable) {
  891. if (dev->tvnorm & V4L2_STD_PAL_BG
  892. || dev->tvnorm & V4L2_STD_PAL_DK) {
  893. width = 352;
  894. } else {
  895. width = cif_width;
  896. if (cif_width != 320 && cif_width != 352)
  897. width = 320;
  898. }
  899. }
  900. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  901. selected_channel -= 4;
  902. selected_channel = selected_channel % 8;
  903. }
  904. if (selected_channel <= 7 && selected_channel >= 0) {
  905. dev->channels[selected_channel].use_cif_resolution =
  906. cif_enable;
  907. dev->channels[selected_channel].cif_width = width;
  908. } else {
  909. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  910. dev->channels[i].use_cif_resolution =
  911. cif_enable;
  912. dev->channels[i].cif_width = width;
  913. }
  914. }
  915. medusa_set_resolution(dev, width, selected_channel);
  916. break;
  917. case REG_READ:
  918. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  919. break;
  920. case REG_WRITE:
  921. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  922. break;
  923. case MEDUSA_READ:
  924. cx25821_i2c_read(&dev->i2c_bus[0],
  925. (u16) data_from_user->reg_address,
  926. &data_from_user->reg_data);
  927. break;
  928. case MEDUSA_WRITE:
  929. cx25821_i2c_write(&dev->i2c_bus[0],
  930. (u16) data_from_user->reg_address,
  931. data_from_user->reg_data);
  932. break;
  933. }
  934. return 0;
  935. }
  936. static long cx25821_video_ioctl(struct file *file,
  937. unsigned int cmd, unsigned long arg)
  938. {
  939. struct cx25821_channel *chan = video_drvdata(file);
  940. int ret = 0;
  941. /* check to see if it's the video upstream */
  942. if (chan->id == SRAM_CH09) {
  943. ret = video_ioctl_upstream9(file, cmd, arg);
  944. return ret;
  945. } else if (chan->id == SRAM_CH10) {
  946. ret = video_ioctl_upstream10(file, cmd, arg);
  947. return ret;
  948. } else if (chan->id == SRAM_CH11) {
  949. ret = video_ioctl_upstream11(file, cmd, arg);
  950. ret = video_ioctl_set(file, cmd, arg);
  951. return ret;
  952. }
  953. return video_ioctl2(file, cmd, arg);
  954. }
  955. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  956. .s_ctrl = cx25821_s_ctrl,
  957. };
  958. static const struct v4l2_file_operations video_fops = {
  959. .owner = THIS_MODULE,
  960. .open = v4l2_fh_open,
  961. .release = video_release,
  962. .read = video_read,
  963. .poll = video_poll,
  964. .mmap = cx25821_video_mmap,
  965. .unlocked_ioctl = cx25821_video_ioctl,
  966. };
  967. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  968. .vidioc_querycap = cx25821_vidioc_querycap,
  969. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  970. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  971. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  972. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  973. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  974. .vidioc_querybuf = cx25821_vidioc_querybuf,
  975. .vidioc_qbuf = cx25821_vidioc_qbuf,
  976. .vidioc_dqbuf = vidioc_dqbuf,
  977. .vidioc_g_std = cx25821_vidioc_g_std,
  978. .vidioc_s_std = cx25821_vidioc_s_std,
  979. .vidioc_enum_input = cx25821_vidioc_enum_input,
  980. .vidioc_g_input = cx25821_vidioc_g_input,
  981. .vidioc_s_input = cx25821_vidioc_s_input,
  982. .vidioc_streamon = vidioc_streamon,
  983. .vidioc_streamoff = vidioc_streamoff,
  984. .vidioc_log_status = vidioc_log_status,
  985. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  986. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  987. #ifdef CONFIG_VIDEO_ADV_DEBUG
  988. .vidioc_g_register = cx25821_vidioc_g_register,
  989. .vidioc_s_register = cx25821_vidioc_s_register,
  990. #endif
  991. };
  992. static const struct video_device cx25821_video_device = {
  993. .name = "cx25821-video",
  994. .fops = &video_fops,
  995. .release = video_device_release_empty,
  996. .minor = -1,
  997. .ioctl_ops = &video_ioctl_ops,
  998. .tvnorms = CX25821_NORMS,
  999. };
  1000. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1001. {
  1002. cx_clear(PCI_INT_MSK, 1);
  1003. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  1004. video_unregister_device(&dev->channels[chan_num].vdev);
  1005. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  1006. btcx_riscmem_free(dev->pci,
  1007. &dev->channels[chan_num].dma_vidq.stopper);
  1008. }
  1009. }
  1010. int cx25821_video_register(struct cx25821_dev *dev)
  1011. {
  1012. int err;
  1013. int i;
  1014. /* initial device configuration */
  1015. dev->tvnorm = V4L2_STD_NTSC_M,
  1016. cx25821_set_tvnorm(dev, dev->tvnorm);
  1017. spin_lock_init(&dev->slock);
  1018. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1019. struct cx25821_channel *chan = &dev->channels[i];
  1020. struct video_device *vdev = &chan->vdev;
  1021. struct v4l2_ctrl_handler *hdl = &chan->hdl;
  1022. if (i == SRAM_CH08) /* audio channel */
  1023. continue;
  1024. v4l2_ctrl_handler_init(hdl, 4);
  1025. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1026. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  1027. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1028. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  1029. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1030. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  1031. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1032. V4L2_CID_HUE, 0, 10000, 1, 5000);
  1033. if (hdl->error) {
  1034. err = hdl->error;
  1035. goto fail_unreg;
  1036. }
  1037. err = v4l2_ctrl_handler_setup(hdl);
  1038. if (err)
  1039. goto fail_unreg;
  1040. cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
  1041. chan->sram_channels->dma_ctl, 0x11, 0);
  1042. chan->sram_channels = &cx25821_sram_channels[i];
  1043. chan->width = 720;
  1044. if (dev->tvnorm & V4L2_STD_625_50)
  1045. chan->height = 576;
  1046. else
  1047. chan->height = 480;
  1048. if (chan->pixel_formats == PIXEL_FRMT_411)
  1049. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
  1050. else
  1051. chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
  1052. cx_write(chan->sram_channels->int_stat, 0xffffffff);
  1053. INIT_LIST_HEAD(&chan->dma_vidq.active);
  1054. INIT_LIST_HEAD(&chan->dma_vidq.queued);
  1055. chan->timeout_data.dev = dev;
  1056. chan->timeout_data.channel = &cx25821_sram_channels[i];
  1057. chan->dma_vidq.timeout.function = cx25821_vid_timeout;
  1058. chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
  1059. init_timer(&chan->dma_vidq.timeout);
  1060. videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
  1061. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1062. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  1063. chan, &dev->lock);
  1064. /* register v4l devices */
  1065. *vdev = cx25821_video_device;
  1066. vdev->v4l2_dev = &dev->v4l2_dev;
  1067. vdev->ctrl_handler = hdl;
  1068. vdev->lock = &dev->lock;
  1069. set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
  1070. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  1071. video_set_drvdata(vdev, chan);
  1072. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  1073. video_nr[dev->nr]);
  1074. if (err < 0)
  1075. goto fail_unreg;
  1076. }
  1077. /* set PCI interrupt */
  1078. cx_set(PCI_INT_MSK, 0xff);
  1079. return 0;
  1080. fail_unreg:
  1081. while (i >= 0)
  1082. cx25821_video_unregister(dev, i--);
  1083. return err;
  1084. }