cx25821-video.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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. unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
  44. #define FORMAT_FLAGS_PACKED 0x01
  45. struct cx25821_fmt formats[] = {
  46. {
  47. .name = "8 bpp, gray",
  48. .fourcc = V4L2_PIX_FMT_GREY,
  49. .depth = 8,
  50. .flags = FORMAT_FLAGS_PACKED,
  51. }, {
  52. .name = "4:1:1, packed, Y41P",
  53. .fourcc = V4L2_PIX_FMT_Y41P,
  54. .depth = 12,
  55. .flags = FORMAT_FLAGS_PACKED,
  56. }, {
  57. .name = "4:2:2, packed, YUYV",
  58. .fourcc = V4L2_PIX_FMT_YUYV,
  59. .depth = 16,
  60. .flags = FORMAT_FLAGS_PACKED,
  61. }, {
  62. .name = "4:2:2, packed, UYVY",
  63. .fourcc = V4L2_PIX_FMT_UYVY,
  64. .depth = 16,
  65. .flags = FORMAT_FLAGS_PACKED,
  66. }, {
  67. .name = "4:2:0, YUV",
  68. .fourcc = V4L2_PIX_FMT_YUV420,
  69. .depth = 12,
  70. .flags = FORMAT_FLAGS_PACKED,
  71. },
  72. };
  73. int cx25821_get_format_size(void)
  74. {
  75. return ARRAY_SIZE(formats);
  76. }
  77. struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  78. {
  79. unsigned int i;
  80. if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
  81. return formats + 1;
  82. for (i = 0; i < ARRAY_SIZE(formats); i++)
  83. if (formats[i].fourcc == fourcc)
  84. return formats + i;
  85. pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  86. return NULL;
  87. }
  88. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  89. u32 count)
  90. {
  91. struct cx25821_buffer *buf;
  92. int bc;
  93. for (bc = 0;; bc++) {
  94. if (list_empty(&q->active)) {
  95. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  96. break;
  97. }
  98. buf = list_entry(q->active.next, struct cx25821_buffer,
  99. vb.queue);
  100. /* count comes from the hw and it is 16bit wide --
  101. * this trick handles wrap-arounds correctly for
  102. * up to 32767 buffers in flight... */
  103. if ((s16) (count - buf->count) < 0)
  104. break;
  105. v4l2_get_timestamp(&buf->vb.ts);
  106. buf->vb.state = VIDEOBUF_DONE;
  107. list_del(&buf->vb.queue);
  108. wake_up(&buf->vb.done);
  109. }
  110. if (list_empty(&q->active))
  111. del_timer(&q->timeout);
  112. else
  113. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  114. if (bc != 1)
  115. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  116. }
  117. int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
  118. {
  119. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  120. __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
  121. dev->tvnorm = norm;
  122. /* Tell the internal A/V decoder */
  123. cx25821_call_all(dev, core, s_std, norm);
  124. return 0;
  125. }
  126. struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
  127. struct pci_dev *pci,
  128. const struct video_device *template,
  129. char *type)
  130. {
  131. struct video_device *vfd;
  132. dprintk(1, "%s()\n", __func__);
  133. vfd = video_device_alloc();
  134. if (NULL == vfd)
  135. return NULL;
  136. *vfd = *template;
  137. vfd->v4l2_dev = &dev->v4l2_dev;
  138. vfd->release = video_device_release;
  139. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
  140. cx25821_boards[dev->board].name);
  141. video_set_drvdata(vfd, dev);
  142. return vfd;
  143. }
  144. /*
  145. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  146. {
  147. int i;
  148. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  149. return -EINVAL;
  150. for (i = 0; i < CX25821_CTLS; i++)
  151. if (cx25821_ctls[i].v.id == qctrl->id)
  152. break;
  153. if (i == CX25821_CTLS) {
  154. *qctrl = no_ctl;
  155. return 0;
  156. }
  157. *qctrl = cx25821_ctls[i].v;
  158. return 0;
  159. }
  160. */
  161. /* resource management */
  162. int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
  163. unsigned int bit)
  164. {
  165. dprintk(1, "%s()\n", __func__);
  166. if (fh->resources & bit)
  167. /* have it already allocated */
  168. return 1;
  169. /* is it free? */
  170. mutex_lock(&dev->lock);
  171. if (dev->channels[fh->channel_id].resources & bit) {
  172. /* no, someone else uses it */
  173. mutex_unlock(&dev->lock);
  174. return 0;
  175. }
  176. /* it's free, grab it */
  177. fh->resources |= bit;
  178. dev->channels[fh->channel_id].resources |= bit;
  179. dprintk(1, "res: get %d\n", bit);
  180. mutex_unlock(&dev->lock);
  181. return 1;
  182. }
  183. int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
  184. {
  185. return fh->resources & bit;
  186. }
  187. int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
  188. {
  189. return fh->dev->channels[fh->channel_id].resources & bit;
  190. }
  191. void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
  192. unsigned int bits)
  193. {
  194. BUG_ON((fh->resources & bits) != bits);
  195. dprintk(1, "%s()\n", __func__);
  196. mutex_lock(&dev->lock);
  197. fh->resources &= ~bits;
  198. dev->channels[fh->channel_id].resources &= ~bits;
  199. dprintk(1, "res: put %d\n", bits);
  200. mutex_unlock(&dev->lock);
  201. }
  202. int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
  203. {
  204. struct v4l2_routing route;
  205. memset(&route, 0, sizeof(route));
  206. dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  207. __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
  208. INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
  209. dev->input = input;
  210. route.input = INPUT(input)->vmux;
  211. /* Tell the internal A/V decoder */
  212. cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
  213. return 0;
  214. }
  215. int cx25821_start_video_dma(struct cx25821_dev *dev,
  216. struct cx25821_dmaqueue *q,
  217. struct cx25821_buffer *buf,
  218. const struct sram_channel *channel)
  219. {
  220. int tmp = 0;
  221. /* setup fifo + format */
  222. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  223. /* reset counter */
  224. cx_write(channel->gpcnt_ctl, 3);
  225. q->count = 1;
  226. /* enable irq */
  227. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  228. cx_set(channel->int_msk, 0x11);
  229. /* start dma */
  230. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  231. /* make sure upstream setting if any is reversed */
  232. tmp = cx_read(VID_CH_MODE_SEL);
  233. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  234. return 0;
  235. }
  236. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  237. struct cx25821_dmaqueue *q,
  238. const struct sram_channel *channel)
  239. {
  240. struct cx25821_buffer *buf, *prev;
  241. struct list_head *item;
  242. if (!list_empty(&q->active)) {
  243. buf = list_entry(q->active.next, struct cx25821_buffer,
  244. vb.queue);
  245. cx25821_start_video_dma(dev, q, buf, channel);
  246. list_for_each(item, &q->active) {
  247. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  248. buf->count = q->count++;
  249. }
  250. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  251. return 0;
  252. }
  253. prev = NULL;
  254. for (;;) {
  255. if (list_empty(&q->queued))
  256. return 0;
  257. buf = list_entry(q->queued.next, struct cx25821_buffer,
  258. vb.queue);
  259. if (NULL == prev) {
  260. list_move_tail(&buf->vb.queue, &q->active);
  261. cx25821_start_video_dma(dev, q, buf, channel);
  262. buf->vb.state = VIDEOBUF_ACTIVE;
  263. buf->count = q->count++;
  264. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  265. } else if (prev->vb.width == buf->vb.width &&
  266. prev->vb.height == buf->vb.height &&
  267. prev->fmt == buf->fmt) {
  268. list_move_tail(&buf->vb.queue, &q->active);
  269. buf->vb.state = VIDEOBUF_ACTIVE;
  270. buf->count = q->count++;
  271. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  272. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  273. } else {
  274. return 0;
  275. }
  276. prev = buf;
  277. }
  278. }
  279. static void cx25821_vid_timeout(unsigned long data)
  280. {
  281. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  282. struct cx25821_dev *dev = timeout_data->dev;
  283. const struct sram_channel *channel = timeout_data->channel;
  284. struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
  285. struct cx25821_buffer *buf;
  286. unsigned long flags;
  287. /* cx25821_sram_channel_dump(dev, channel); */
  288. cx_clear(channel->dma_ctl, 0x11);
  289. spin_lock_irqsave(&dev->slock, flags);
  290. while (!list_empty(&q->active)) {
  291. buf = list_entry(q->active.next, struct cx25821_buffer,
  292. vb.queue);
  293. list_del(&buf->vb.queue);
  294. buf->vb.state = VIDEOBUF_ERROR;
  295. wake_up(&buf->vb.done);
  296. }
  297. cx25821_restart_video_queue(dev, q, channel);
  298. spin_unlock_irqrestore(&dev->slock, flags);
  299. }
  300. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  301. {
  302. u32 count = 0;
  303. int handled = 0;
  304. u32 mask;
  305. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  306. mask = cx_read(channel->int_msk);
  307. if (0 == (status & mask))
  308. return handled;
  309. cx_write(channel->int_stat, status);
  310. /* risc op code error */
  311. if (status & (1 << 16)) {
  312. pr_warn("%s, %s: video risc op code error\n",
  313. dev->name, channel->name);
  314. cx_clear(channel->dma_ctl, 0x11);
  315. cx25821_sram_channel_dump(dev, channel);
  316. }
  317. /* risc1 y */
  318. if (status & FLD_VID_DST_RISC1) {
  319. spin_lock(&dev->slock);
  320. count = cx_read(channel->gpcnt);
  321. cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
  322. count);
  323. spin_unlock(&dev->slock);
  324. handled++;
  325. }
  326. /* risc2 y */
  327. if (status & 0x10) {
  328. dprintk(2, "stopper video\n");
  329. spin_lock(&dev->slock);
  330. cx25821_restart_video_queue(dev,
  331. &dev->channels[channel->i].vidq, channel);
  332. spin_unlock(&dev->slock);
  333. handled++;
  334. }
  335. return handled;
  336. }
  337. int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  338. unsigned int *size)
  339. {
  340. struct cx25821_fh *fh = q->priv_data;
  341. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  342. if (0 == *count)
  343. *count = 32;
  344. if (*size * *count > vid_limit * 1024 * 1024)
  345. *count = (vid_limit * 1024 * 1024) / *size;
  346. return 0;
  347. }
  348. int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  349. enum v4l2_field field)
  350. {
  351. struct cx25821_fh *fh = q->priv_data;
  352. struct cx25821_dev *dev = fh->dev;
  353. struct cx25821_buffer *buf =
  354. container_of(vb, struct cx25821_buffer, vb);
  355. int rc, init_buffer = 0;
  356. u32 line0_offset;
  357. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  358. int bpl_local = LINE_SIZE_D1;
  359. int channel_opened = fh->channel_id;
  360. BUG_ON(NULL == fh->fmt);
  361. if (fh->width < 48 || fh->width > 720 ||
  362. fh->height < 32 || fh->height > 576)
  363. return -EINVAL;
  364. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  365. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  366. return -EINVAL;
  367. if (buf->fmt != fh->fmt ||
  368. buf->vb.width != fh->width ||
  369. buf->vb.height != fh->height || buf->vb.field != field) {
  370. buf->fmt = fh->fmt;
  371. buf->vb.width = fh->width;
  372. buf->vb.height = fh->height;
  373. buf->vb.field = field;
  374. init_buffer = 1;
  375. }
  376. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  377. init_buffer = 1;
  378. rc = videobuf_iolock(q, &buf->vb, NULL);
  379. if (0 != rc) {
  380. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  381. goto fail;
  382. }
  383. }
  384. dprintk(1, "init_buffer=%d\n", init_buffer);
  385. if (init_buffer) {
  386. channel_opened = dev->channel_opened;
  387. if (channel_opened < 0 || channel_opened > 7)
  388. channel_opened = 7;
  389. if (dev->channels[channel_opened].pixel_formats ==
  390. PIXEL_FRMT_411)
  391. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  392. else
  393. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  394. if (dev->channels[channel_opened].pixel_formats ==
  395. PIXEL_FRMT_411) {
  396. bpl_local = buf->bpl;
  397. } else {
  398. bpl_local = buf->bpl; /* Default */
  399. if (channel_opened >= 0 && channel_opened <= 7) {
  400. if (dev->channels[channel_opened]
  401. .use_cif_resolution) {
  402. if (dev->tvnorm & V4L2_STD_PAL_BG ||
  403. dev->tvnorm & V4L2_STD_PAL_DK)
  404. bpl_local = 352 << 1;
  405. else
  406. bpl_local = dev->channels[
  407. channel_opened].
  408. cif_width << 1;
  409. }
  410. }
  411. }
  412. switch (buf->vb.field) {
  413. case V4L2_FIELD_TOP:
  414. cx25821_risc_buffer(dev->pci, &buf->risc,
  415. dma->sglist, 0, UNSET,
  416. buf->bpl, 0, buf->vb.height);
  417. break;
  418. case V4L2_FIELD_BOTTOM:
  419. cx25821_risc_buffer(dev->pci, &buf->risc,
  420. dma->sglist, UNSET, 0,
  421. buf->bpl, 0, buf->vb.height);
  422. break;
  423. case V4L2_FIELD_INTERLACED:
  424. /* All other formats are top field first */
  425. line0_offset = 0;
  426. dprintk(1, "top field first\n");
  427. cx25821_risc_buffer(dev->pci, &buf->risc,
  428. dma->sglist, line0_offset,
  429. bpl_local, bpl_local, bpl_local,
  430. buf->vb.height >> 1);
  431. break;
  432. case V4L2_FIELD_SEQ_TB:
  433. cx25821_risc_buffer(dev->pci, &buf->risc,
  434. dma->sglist,
  435. 0, buf->bpl * (buf->vb.height >> 1),
  436. buf->bpl, 0, buf->vb.height >> 1);
  437. break;
  438. case V4L2_FIELD_SEQ_BT:
  439. cx25821_risc_buffer(dev->pci, &buf->risc,
  440. dma->sglist,
  441. buf->bpl * (buf->vb.height >> 1), 0,
  442. buf->bpl, 0, buf->vb.height >> 1);
  443. break;
  444. default:
  445. BUG();
  446. }
  447. }
  448. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  449. buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
  450. fh->fmt->name, (unsigned long)buf->risc.dma);
  451. buf->vb.state = VIDEOBUF_PREPARED;
  452. return 0;
  453. fail:
  454. cx25821_free_buffer(q, buf);
  455. return rc;
  456. }
  457. void cx25821_buffer_release(struct videobuf_queue *q,
  458. struct videobuf_buffer *vb)
  459. {
  460. struct cx25821_buffer *buf =
  461. container_of(vb, struct cx25821_buffer, vb);
  462. cx25821_free_buffer(q, buf);
  463. }
  464. struct videobuf_queue *get_queue(struct cx25821_fh *fh)
  465. {
  466. switch (fh->type) {
  467. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  468. return &fh->vidq;
  469. default:
  470. BUG();
  471. return NULL;
  472. }
  473. }
  474. int cx25821_get_resource(struct cx25821_fh *fh, int resource)
  475. {
  476. switch (fh->type) {
  477. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  478. return resource;
  479. default:
  480. BUG();
  481. return 0;
  482. }
  483. }
  484. int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  485. {
  486. struct cx25821_fh *fh = file->private_data;
  487. return videobuf_mmap_mapper(get_queue(fh), vma);
  488. }
  489. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  490. {
  491. struct cx25821_buffer *buf =
  492. container_of(vb, struct cx25821_buffer, vb);
  493. struct cx25821_buffer *prev;
  494. struct cx25821_fh *fh = vq->priv_data;
  495. struct cx25821_dev *dev = fh->dev;
  496. struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
  497. /* add jump to stopper */
  498. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  499. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  500. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  501. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  502. if (!list_empty(&q->queued)) {
  503. list_add_tail(&buf->vb.queue, &q->queued);
  504. buf->vb.state = VIDEOBUF_QUEUED;
  505. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  506. buf->vb.i);
  507. } else if (list_empty(&q->active)) {
  508. list_add_tail(&buf->vb.queue, &q->active);
  509. cx25821_start_video_dma(dev, q, buf,
  510. dev->channels[fh->channel_id].sram_channels);
  511. buf->vb.state = VIDEOBUF_ACTIVE;
  512. buf->count = q->count++;
  513. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  514. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  515. buf, buf->vb.i, buf->count, q->count);
  516. } else {
  517. prev = list_entry(q->active.prev, struct cx25821_buffer,
  518. vb.queue);
  519. if (prev->vb.width == buf->vb.width
  520. && prev->vb.height == buf->vb.height
  521. && prev->fmt == buf->fmt) {
  522. list_add_tail(&buf->vb.queue, &q->active);
  523. buf->vb.state = VIDEOBUF_ACTIVE;
  524. buf->count = q->count++;
  525. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  526. /* 64 bit bits 63-32 */
  527. prev->risc.jmp[2] = cpu_to_le32(0);
  528. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  529. buf, buf->vb.i, buf->count);
  530. } else {
  531. list_add_tail(&buf->vb.queue, &q->queued);
  532. buf->vb.state = VIDEOBUF_QUEUED;
  533. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  534. buf->vb.i);
  535. }
  536. }
  537. if (list_empty(&q->active))
  538. dprintk(2, "active queue empty!\n");
  539. }
  540. static struct videobuf_queue_ops cx25821_video_qops = {
  541. .buf_setup = cx25821_buffer_setup,
  542. .buf_prepare = cx25821_buffer_prepare,
  543. .buf_queue = buffer_queue,
  544. .buf_release = cx25821_buffer_release,
  545. };
  546. static int video_open(struct file *file)
  547. {
  548. struct video_device *vdev = video_devdata(file);
  549. struct cx25821_dev *dev = video_drvdata(file);
  550. struct cx25821_fh *fh;
  551. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  552. u32 pix_format;
  553. int ch_id;
  554. dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
  555. v4l2_type_names[type]);
  556. for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
  557. if (dev->channels[ch_id].video_dev == vdev)
  558. break;
  559. /* Can't happen */
  560. if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
  561. return -ENODEV;
  562. /* allocate + initialize per filehandle data */
  563. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  564. if (NULL == fh)
  565. return -ENOMEM;
  566. file->private_data = fh;
  567. fh->dev = dev;
  568. fh->type = type;
  569. fh->width = 720;
  570. fh->channel_id = ch_id;
  571. if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
  572. fh->height = 576;
  573. else
  574. fh->height = 480;
  575. dev->channel_opened = fh->channel_id;
  576. if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
  577. pix_format = V4L2_PIX_FMT_Y41P;
  578. else
  579. pix_format = V4L2_PIX_FMT_YUYV;
  580. fh->fmt = cx25821_format_by_fourcc(pix_format);
  581. v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
  582. videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
  583. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  584. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  585. fh, NULL);
  586. dprintk(1, "post videobuf_queue_init()\n");
  587. return 0;
  588. }
  589. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  590. loff_t *ppos)
  591. {
  592. struct cx25821_fh *fh = file->private_data;
  593. switch (fh->type) {
  594. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  595. if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
  596. return -EBUSY;
  597. return videobuf_read_one(&fh->vidq, data, count, ppos,
  598. file->f_flags & O_NONBLOCK);
  599. default:
  600. BUG();
  601. return 0;
  602. }
  603. }
  604. static unsigned int video_poll(struct file *file,
  605. struct poll_table_struct *wait)
  606. {
  607. struct cx25821_fh *fh = file->private_data;
  608. struct cx25821_buffer *buf;
  609. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  610. /* streaming capture */
  611. if (list_empty(&fh->vidq.stream))
  612. return POLLERR;
  613. buf = list_entry(fh->vidq.stream.next,
  614. struct cx25821_buffer, vb.stream);
  615. } else {
  616. /* read() capture */
  617. buf = (struct cx25821_buffer *)fh->vidq.read_buf;
  618. if (NULL == buf)
  619. return POLLERR;
  620. }
  621. poll_wait(file, &buf->vb.done, wait);
  622. if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
  623. if (buf->vb.state == VIDEOBUF_DONE) {
  624. struct cx25821_dev *dev = fh->dev;
  625. if (dev && dev->channels[fh->channel_id]
  626. .use_cif_resolution) {
  627. u8 cam_id = *((char *)buf->vb.baddr + 3);
  628. memcpy((char *)buf->vb.baddr,
  629. (char *)buf->vb.baddr + (fh->width * 2),
  630. (fh->width * 2));
  631. *((char *)buf->vb.baddr + 3) = cam_id;
  632. }
  633. }
  634. return POLLIN | POLLRDNORM;
  635. }
  636. return 0;
  637. }
  638. static int video_release(struct file *file)
  639. {
  640. struct cx25821_fh *fh = file->private_data;
  641. struct cx25821_dev *dev = fh->dev;
  642. const struct sram_channel *sram_ch =
  643. dev->channels[0].sram_channels;
  644. /* stop the risc engine and fifo */
  645. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  646. /* stop video capture */
  647. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  648. videobuf_queue_cancel(&fh->vidq);
  649. cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
  650. }
  651. if (fh->vidq.read_buf) {
  652. cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
  653. kfree(fh->vidq.read_buf);
  654. }
  655. videobuf_mmap_free(&fh->vidq);
  656. v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
  657. file->private_data = NULL;
  658. kfree(fh);
  659. return 0;
  660. }
  661. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  662. {
  663. struct cx25821_fh *fh = priv;
  664. struct cx25821_dev *dev = fh->dev;
  665. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  666. return -EINVAL;
  667. if (unlikely(i != fh->type))
  668. return -EINVAL;
  669. if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
  670. RESOURCE_VIDEO0))))
  671. return -EBUSY;
  672. return videobuf_streamon(get_queue(fh));
  673. }
  674. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  675. {
  676. struct cx25821_fh *fh = priv;
  677. struct cx25821_dev *dev = fh->dev;
  678. int err, res;
  679. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  680. return -EINVAL;
  681. if (i != fh->type)
  682. return -EINVAL;
  683. res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
  684. err = videobuf_streamoff(get_queue(fh));
  685. if (err < 0)
  686. return err;
  687. cx25821_res_free(dev, fh, res);
  688. return 0;
  689. }
  690. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  691. struct v4l2_format *f)
  692. {
  693. struct cx25821_fh *fh = priv;
  694. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  695. struct v4l2_mbus_framefmt mbus_fmt;
  696. int err;
  697. int pix_format = PIXEL_FRMT_422;
  698. if (fh) {
  699. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  700. fh->prio);
  701. if (0 != err)
  702. return err;
  703. }
  704. dprintk(2, "%s()\n", __func__);
  705. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  706. if (0 != err)
  707. return err;
  708. fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  709. fh->vidq.field = f->fmt.pix.field;
  710. /* check if width and height is valid based on set standard */
  711. if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
  712. fh->width = f->fmt.pix.width;
  713. if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
  714. fh->height = f->fmt.pix.height;
  715. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  716. pix_format = PIXEL_FRMT_411;
  717. else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
  718. pix_format = PIXEL_FRMT_422;
  719. else
  720. return -EINVAL;
  721. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  722. /* check if cif resolution */
  723. if (fh->width == 320 || fh->width == 352)
  724. dev->channels[fh->channel_id].use_cif_resolution = 1;
  725. else
  726. dev->channels[fh->channel_id].use_cif_resolution = 0;
  727. dev->channels[fh->channel_id].cif_width = fh->width;
  728. medusa_set_resolution(dev, fh->width, SRAM_CH00);
  729. dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
  730. fh->height, fh->vidq.field);
  731. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  732. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  733. return 0;
  734. }
  735. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  736. {
  737. int ret_val = 0;
  738. struct cx25821_fh *fh = priv;
  739. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  740. ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
  741. p->sequence = dev->channels[fh->channel_id].vidq.count;
  742. return ret_val;
  743. }
  744. static int vidioc_log_status(struct file *file, void *priv)
  745. {
  746. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  747. struct cx25821_fh *fh = priv;
  748. const struct sram_channel *sram_ch =
  749. dev->channels[fh->channel_id].sram_channels;
  750. u32 tmp = 0;
  751. cx25821_call_all(dev, core, log_status);
  752. tmp = cx_read(sram_ch->dma_ctl);
  753. pr_info("Video input 0 is %s\n",
  754. (tmp & 0x11) ? "streaming" : "stopped");
  755. return 0;
  756. }
  757. static int vidioc_s_ctrl(struct file *file, void *priv,
  758. struct v4l2_control *ctl)
  759. {
  760. struct cx25821_fh *fh = priv;
  761. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  762. int err;
  763. if (fh) {
  764. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  765. fh->prio);
  766. if (0 != err)
  767. return err;
  768. }
  769. return cx25821_set_control(dev, ctl, fh->channel_id);
  770. }
  771. /* VIDEO IOCTLS */
  772. int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  773. struct v4l2_format *f)
  774. {
  775. struct cx25821_fh *fh = priv;
  776. f->fmt.pix.width = fh->width;
  777. f->fmt.pix.height = fh->height;
  778. f->fmt.pix.field = fh->vidq.field;
  779. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  780. f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
  781. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  782. return 0;
  783. }
  784. int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  785. struct v4l2_format *f)
  786. {
  787. struct cx25821_fmt *fmt;
  788. enum v4l2_field field;
  789. unsigned int maxw, maxh;
  790. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  791. if (NULL == fmt)
  792. return -EINVAL;
  793. field = f->fmt.pix.field;
  794. maxw = 720;
  795. maxh = 576;
  796. if (V4L2_FIELD_ANY == field) {
  797. if (f->fmt.pix.height > maxh / 2)
  798. field = V4L2_FIELD_INTERLACED;
  799. else
  800. field = V4L2_FIELD_TOP;
  801. }
  802. switch (field) {
  803. case V4L2_FIELD_TOP:
  804. case V4L2_FIELD_BOTTOM:
  805. maxh = maxh / 2;
  806. break;
  807. case V4L2_FIELD_INTERLACED:
  808. break;
  809. default:
  810. return -EINVAL;
  811. }
  812. f->fmt.pix.field = field;
  813. if (f->fmt.pix.height < 32)
  814. f->fmt.pix.height = 32;
  815. if (f->fmt.pix.height > maxh)
  816. f->fmt.pix.height = maxh;
  817. if (f->fmt.pix.width < 48)
  818. f->fmt.pix.width = 48;
  819. if (f->fmt.pix.width > maxw)
  820. f->fmt.pix.width = maxw;
  821. f->fmt.pix.width &= ~0x03;
  822. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  823. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  824. return 0;
  825. }
  826. int cx25821_vidioc_querycap(struct file *file, void *priv,
  827. struct v4l2_capability *cap)
  828. {
  829. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  830. struct cx25821_fh *fh = priv;
  831. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  832. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  833. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  834. strcpy(cap->driver, "cx25821");
  835. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  836. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  837. if (fh->channel_id >= VID_CHANNEL_NUM)
  838. cap->device_caps = cap_output;
  839. else
  840. cap->device_caps = cap_input;
  841. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  842. return 0;
  843. }
  844. int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  845. struct v4l2_fmtdesc *f)
  846. {
  847. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  848. return -EINVAL;
  849. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  850. f->pixelformat = formats[f->index].fourcc;
  851. return 0;
  852. }
  853. int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  854. struct v4l2_requestbuffers *p)
  855. {
  856. struct cx25821_fh *fh = priv;
  857. return videobuf_reqbufs(get_queue(fh), p);
  858. }
  859. int cx25821_vidioc_querybuf(struct file *file, void *priv,
  860. struct v4l2_buffer *p)
  861. {
  862. struct cx25821_fh *fh = priv;
  863. return videobuf_querybuf(get_queue(fh), p);
  864. }
  865. int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  866. {
  867. struct cx25821_fh *fh = priv;
  868. return videobuf_qbuf(get_queue(fh), p);
  869. }
  870. int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  871. {
  872. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  873. struct cx25821_fh *fh = f;
  874. *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
  875. return 0;
  876. }
  877. int cx25821_vidioc_s_priority(struct file *file, void *f,
  878. enum v4l2_priority prio)
  879. {
  880. struct cx25821_fh *fh = f;
  881. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  882. return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
  883. prio);
  884. }
  885. int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  886. {
  887. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  888. *tvnorms = dev->tvnorm;
  889. return 0;
  890. }
  891. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  892. {
  893. struct cx25821_fh *fh = priv;
  894. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  895. int err;
  896. dprintk(1, "%s()\n", __func__);
  897. if (fh) {
  898. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  899. fh->prio);
  900. if (0 != err)
  901. return err;
  902. }
  903. if (dev->tvnorm == tvnorms)
  904. return 0;
  905. mutex_lock(&dev->lock);
  906. cx25821_set_tvnorm(dev, tvnorms);
  907. mutex_unlock(&dev->lock);
  908. medusa_set_videostandard(dev);
  909. return 0;
  910. }
  911. int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
  912. {
  913. static const char * const iname[] = {
  914. [CX25821_VMUX_COMPOSITE] = "Composite",
  915. [CX25821_VMUX_SVIDEO] = "S-Video",
  916. [CX25821_VMUX_DEBUG] = "for debug only",
  917. };
  918. unsigned int n;
  919. dprintk(1, "%s()\n", __func__);
  920. n = i->index;
  921. if (n >= 2)
  922. return -EINVAL;
  923. if (0 == INPUT(n)->type)
  924. return -EINVAL;
  925. i->type = V4L2_INPUT_TYPE_CAMERA;
  926. strcpy(i->name, iname[INPUT(n)->type]);
  927. i->std = CX25821_NORMS;
  928. return 0;
  929. }
  930. int cx25821_vidioc_enum_input(struct file *file, void *priv,
  931. struct v4l2_input *i)
  932. {
  933. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  934. dprintk(1, "%s()\n", __func__);
  935. return cx25821_enum_input(dev, i);
  936. }
  937. int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  938. {
  939. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  940. *i = dev->input;
  941. dprintk(1, "%s(): returns %d\n", __func__, *i);
  942. return 0;
  943. }
  944. int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  945. {
  946. struct cx25821_fh *fh = priv;
  947. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  948. int err;
  949. dprintk(1, "%s(%d)\n", __func__, i);
  950. if (fh) {
  951. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  952. fh->prio);
  953. if (0 != err)
  954. return err;
  955. }
  956. if (i >= CX25821_NR_INPUT) {
  957. dprintk(1, "%s(): -EINVAL\n", __func__);
  958. return -EINVAL;
  959. }
  960. mutex_lock(&dev->lock);
  961. cx25821_video_mux(dev, i);
  962. mutex_unlock(&dev->lock);
  963. return 0;
  964. }
  965. #ifdef CONFIG_VIDEO_ADV_DEBUG
  966. int cx25821_vidioc_g_register(struct file *file, void *fh,
  967. struct v4l2_dbg_register *reg)
  968. {
  969. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  970. if (!v4l2_chip_match_host(&reg->match))
  971. return -EINVAL;
  972. cx25821_call_all(dev, core, g_register, reg);
  973. return 0;
  974. }
  975. int cx25821_vidioc_s_register(struct file *file, void *fh,
  976. const struct v4l2_dbg_register *reg)
  977. {
  978. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  979. if (!v4l2_chip_match_host(&reg->match))
  980. return -EINVAL;
  981. cx25821_call_all(dev, core, s_register, reg);
  982. return 0;
  983. }
  984. #endif
  985. /*****************************************************************************/
  986. static const struct v4l2_queryctrl no_ctl = {
  987. .name = "42",
  988. .flags = V4L2_CTRL_FLAG_DISABLED,
  989. };
  990. static struct v4l2_queryctrl cx25821_ctls[] = {
  991. /* --- video --- */
  992. {
  993. .id = V4L2_CID_BRIGHTNESS,
  994. .name = "Brightness",
  995. .minimum = 0,
  996. .maximum = 10000,
  997. .step = 1,
  998. .default_value = 6200,
  999. .type = V4L2_CTRL_TYPE_INTEGER,
  1000. }, {
  1001. .id = V4L2_CID_CONTRAST,
  1002. .name = "Contrast",
  1003. .minimum = 0,
  1004. .maximum = 10000,
  1005. .step = 1,
  1006. .default_value = 5000,
  1007. .type = V4L2_CTRL_TYPE_INTEGER,
  1008. }, {
  1009. .id = V4L2_CID_SATURATION,
  1010. .name = "Saturation",
  1011. .minimum = 0,
  1012. .maximum = 10000,
  1013. .step = 1,
  1014. .default_value = 5000,
  1015. .type = V4L2_CTRL_TYPE_INTEGER,
  1016. }, {
  1017. .id = V4L2_CID_HUE,
  1018. .name = "Hue",
  1019. .minimum = 0,
  1020. .maximum = 10000,
  1021. .step = 1,
  1022. .default_value = 5000,
  1023. .type = V4L2_CTRL_TYPE_INTEGER,
  1024. }
  1025. };
  1026. static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
  1027. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  1028. {
  1029. int i;
  1030. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  1031. return -EINVAL;
  1032. for (i = 0; i < CX25821_CTLS; i++)
  1033. if (cx25821_ctls[i].id == qctrl->id)
  1034. break;
  1035. if (i == CX25821_CTLS) {
  1036. *qctrl = no_ctl;
  1037. return 0;
  1038. }
  1039. *qctrl = cx25821_ctls[i];
  1040. return 0;
  1041. }
  1042. int cx25821_vidioc_queryctrl(struct file *file, void *priv,
  1043. struct v4l2_queryctrl *qctrl)
  1044. {
  1045. return cx25821_ctrl_query(qctrl);
  1046. }
  1047. /* ------------------------------------------------------------------ */
  1048. /* VIDEO CTRL IOCTLS */
  1049. static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
  1050. {
  1051. unsigned int i;
  1052. for (i = 0; i < CX25821_CTLS; i++)
  1053. if (cx25821_ctls[i].id == id)
  1054. return cx25821_ctls + i;
  1055. return NULL;
  1056. }
  1057. int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
  1058. struct v4l2_control *ctl)
  1059. {
  1060. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1061. struct cx25821_fh *fh = priv;
  1062. const struct v4l2_queryctrl *ctrl;
  1063. ctrl = ctrl_by_id(ctl->id);
  1064. if (NULL == ctrl)
  1065. return -EINVAL;
  1066. switch (ctl->id) {
  1067. case V4L2_CID_BRIGHTNESS:
  1068. ctl->value = dev->channels[fh->channel_id].ctl_bright;
  1069. break;
  1070. case V4L2_CID_HUE:
  1071. ctl->value = dev->channels[fh->channel_id].ctl_hue;
  1072. break;
  1073. case V4L2_CID_CONTRAST:
  1074. ctl->value = dev->channels[fh->channel_id].ctl_contrast;
  1075. break;
  1076. case V4L2_CID_SATURATION:
  1077. ctl->value = dev->channels[fh->channel_id].ctl_saturation;
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. int cx25821_set_control(struct cx25821_dev *dev,
  1083. struct v4l2_control *ctl, int chan_num)
  1084. {
  1085. int err;
  1086. const struct v4l2_queryctrl *ctrl;
  1087. err = -EINVAL;
  1088. ctrl = ctrl_by_id(ctl->id);
  1089. if (NULL == ctrl)
  1090. return err;
  1091. switch (ctrl->type) {
  1092. case V4L2_CTRL_TYPE_BOOLEAN:
  1093. case V4L2_CTRL_TYPE_MENU:
  1094. case V4L2_CTRL_TYPE_INTEGER:
  1095. if (ctl->value < ctrl->minimum)
  1096. ctl->value = ctrl->minimum;
  1097. if (ctl->value > ctrl->maximum)
  1098. ctl->value = ctrl->maximum;
  1099. break;
  1100. default:
  1101. /* nothing */ ;
  1102. }
  1103. switch (ctl->id) {
  1104. case V4L2_CID_BRIGHTNESS:
  1105. dev->channels[chan_num].ctl_bright = ctl->value;
  1106. medusa_set_brightness(dev, ctl->value, chan_num);
  1107. break;
  1108. case V4L2_CID_HUE:
  1109. dev->channels[chan_num].ctl_hue = ctl->value;
  1110. medusa_set_hue(dev, ctl->value, chan_num);
  1111. break;
  1112. case V4L2_CID_CONTRAST:
  1113. dev->channels[chan_num].ctl_contrast = ctl->value;
  1114. medusa_set_contrast(dev, ctl->value, chan_num);
  1115. break;
  1116. case V4L2_CID_SATURATION:
  1117. dev->channels[chan_num].ctl_saturation = ctl->value;
  1118. medusa_set_saturation(dev, ctl->value, chan_num);
  1119. break;
  1120. }
  1121. err = 0;
  1122. return err;
  1123. }
  1124. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
  1125. {
  1126. struct v4l2_control ctrl;
  1127. int i;
  1128. for (i = 0; i < CX25821_CTLS; i++) {
  1129. ctrl.id = cx25821_ctls[i].id;
  1130. ctrl.value = cx25821_ctls[i].default_value;
  1131. cx25821_set_control(dev, &ctrl, chan_num);
  1132. }
  1133. }
  1134. int cx25821_vidioc_cropcap(struct file *file, void *priv,
  1135. struct v4l2_cropcap *cropcap)
  1136. {
  1137. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1138. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1139. return -EINVAL;
  1140. cropcap->bounds.top = 0;
  1141. cropcap->bounds.left = 0;
  1142. cropcap->bounds.width = 720;
  1143. cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
  1144. cropcap->pixelaspect.numerator =
  1145. dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
  1146. cropcap->pixelaspect.denominator =
  1147. dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
  1148. cropcap->defrect = cropcap->bounds;
  1149. return 0;
  1150. }
  1151. int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
  1152. {
  1153. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1154. struct cx25821_fh *fh = priv;
  1155. int err;
  1156. if (fh) {
  1157. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  1158. fh->prio);
  1159. if (0 != err)
  1160. return err;
  1161. }
  1162. /* cx25821_vidioc_s_crop not supported */
  1163. return -EINVAL;
  1164. }
  1165. int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1166. {
  1167. /* cx25821_vidioc_g_crop not supported */
  1168. return -EINVAL;
  1169. }
  1170. int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  1171. {
  1172. if (tvnorm == V4L2_STD_PAL_BG) {
  1173. if (width == 352 || width == 720)
  1174. return 1;
  1175. else
  1176. return 0;
  1177. }
  1178. if (tvnorm == V4L2_STD_NTSC_M) {
  1179. if (width == 320 || width == 352 || width == 720)
  1180. return 1;
  1181. else
  1182. return 0;
  1183. }
  1184. return 0;
  1185. }
  1186. int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  1187. {
  1188. if (tvnorm == V4L2_STD_PAL_BG) {
  1189. if (height == 576 || height == 288)
  1190. return 1;
  1191. else
  1192. return 0;
  1193. }
  1194. if (tvnorm == V4L2_STD_NTSC_M) {
  1195. if (height == 480 || height == 240)
  1196. return 1;
  1197. else
  1198. return 0;
  1199. }
  1200. return 0;
  1201. }
  1202. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  1203. unsigned long arg)
  1204. {
  1205. struct cx25821_fh *fh = file->private_data;
  1206. struct cx25821_dev *dev = fh->dev;
  1207. int command = 0;
  1208. struct upstream_user_struct *data_from_user;
  1209. data_from_user = (struct upstream_user_struct *)arg;
  1210. if (!data_from_user) {
  1211. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1212. return 0;
  1213. }
  1214. command = data_from_user->command;
  1215. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1216. return 0;
  1217. dev->input_filename = data_from_user->input_filename;
  1218. dev->input_audiofilename = data_from_user->input_filename;
  1219. dev->vid_stdname = data_from_user->vid_stdname;
  1220. dev->pixel_format = data_from_user->pixel_format;
  1221. dev->channel_select = data_from_user->channel_select;
  1222. dev->command = data_from_user->command;
  1223. switch (command) {
  1224. case UPSTREAM_START_VIDEO:
  1225. cx25821_start_upstream_video_ch1(dev, data_from_user);
  1226. break;
  1227. case UPSTREAM_STOP_VIDEO:
  1228. cx25821_stop_upstream_video_ch1(dev);
  1229. break;
  1230. }
  1231. return 0;
  1232. }
  1233. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  1234. unsigned long arg)
  1235. {
  1236. struct cx25821_fh *fh = file->private_data;
  1237. struct cx25821_dev *dev = fh->dev;
  1238. int command = 0;
  1239. struct upstream_user_struct *data_from_user;
  1240. data_from_user = (struct upstream_user_struct *)arg;
  1241. if (!data_from_user) {
  1242. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1243. return 0;
  1244. }
  1245. command = data_from_user->command;
  1246. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1247. return 0;
  1248. dev->input_filename_ch2 = data_from_user->input_filename;
  1249. dev->input_audiofilename = data_from_user->input_filename;
  1250. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  1251. dev->pixel_format_ch2 = data_from_user->pixel_format;
  1252. dev->channel_select_ch2 = data_from_user->channel_select;
  1253. dev->command_ch2 = data_from_user->command;
  1254. switch (command) {
  1255. case UPSTREAM_START_VIDEO:
  1256. cx25821_start_upstream_video_ch2(dev, data_from_user);
  1257. break;
  1258. case UPSTREAM_STOP_VIDEO:
  1259. cx25821_stop_upstream_video_ch2(dev);
  1260. break;
  1261. }
  1262. return 0;
  1263. }
  1264. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  1265. unsigned long arg)
  1266. {
  1267. struct cx25821_fh *fh = file->private_data;
  1268. struct cx25821_dev *dev = fh->dev;
  1269. int command = 0;
  1270. struct upstream_user_struct *data_from_user;
  1271. data_from_user = (struct upstream_user_struct *)arg;
  1272. if (!data_from_user) {
  1273. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1274. return 0;
  1275. }
  1276. command = data_from_user->command;
  1277. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  1278. return 0;
  1279. dev->input_filename = data_from_user->input_filename;
  1280. dev->input_audiofilename = data_from_user->input_filename;
  1281. dev->vid_stdname = data_from_user->vid_stdname;
  1282. dev->pixel_format = data_from_user->pixel_format;
  1283. dev->channel_select = data_from_user->channel_select;
  1284. dev->command = data_from_user->command;
  1285. switch (command) {
  1286. case UPSTREAM_START_AUDIO:
  1287. cx25821_start_upstream_audio(dev, data_from_user);
  1288. break;
  1289. case UPSTREAM_STOP_AUDIO:
  1290. cx25821_stop_upstream_audio(dev);
  1291. break;
  1292. }
  1293. return 0;
  1294. }
  1295. static long video_ioctl_set(struct file *file, unsigned int cmd,
  1296. unsigned long arg)
  1297. {
  1298. struct cx25821_fh *fh = file->private_data;
  1299. struct cx25821_dev *dev = fh->dev;
  1300. struct downstream_user_struct *data_from_user;
  1301. int command;
  1302. int width = 720;
  1303. int selected_channel = 0;
  1304. int pix_format = 0;
  1305. int i = 0;
  1306. int cif_enable = 0;
  1307. int cif_width = 0;
  1308. data_from_user = (struct downstream_user_struct *)arg;
  1309. if (!data_from_user) {
  1310. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  1311. return 0;
  1312. }
  1313. command = data_from_user->command;
  1314. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  1315. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  1316. && command != REG_WRITE && command != MEDUSA_READ
  1317. && command != MEDUSA_WRITE) {
  1318. return 0;
  1319. }
  1320. switch (command) {
  1321. case SET_VIDEO_STD:
  1322. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  1323. dev->tvnorm = V4L2_STD_PAL_BG;
  1324. else
  1325. dev->tvnorm = V4L2_STD_NTSC_M;
  1326. medusa_set_videostandard(dev);
  1327. break;
  1328. case SET_PIXEL_FORMAT:
  1329. selected_channel = data_from_user->decoder_select;
  1330. pix_format = data_from_user->pixel_format;
  1331. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1332. selected_channel -= 4;
  1333. selected_channel = selected_channel % 8;
  1334. }
  1335. if (selected_channel >= 0)
  1336. cx25821_set_pixel_format(dev, selected_channel,
  1337. pix_format);
  1338. break;
  1339. case ENABLE_CIF_RESOLUTION:
  1340. selected_channel = data_from_user->decoder_select;
  1341. cif_enable = data_from_user->cif_resolution_enable;
  1342. cif_width = data_from_user->cif_width;
  1343. if (cif_enable) {
  1344. if (dev->tvnorm & V4L2_STD_PAL_BG
  1345. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1346. width = 352;
  1347. } else {
  1348. width = cif_width;
  1349. if (cif_width != 320 && cif_width != 352)
  1350. width = 320;
  1351. }
  1352. }
  1353. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1354. selected_channel -= 4;
  1355. selected_channel = selected_channel % 8;
  1356. }
  1357. if (selected_channel <= 7 && selected_channel >= 0) {
  1358. dev->channels[selected_channel].use_cif_resolution =
  1359. cif_enable;
  1360. dev->channels[selected_channel].cif_width = width;
  1361. } else {
  1362. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1363. dev->channels[i].use_cif_resolution =
  1364. cif_enable;
  1365. dev->channels[i].cif_width = width;
  1366. }
  1367. }
  1368. medusa_set_resolution(dev, width, selected_channel);
  1369. break;
  1370. case REG_READ:
  1371. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1372. break;
  1373. case REG_WRITE:
  1374. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1375. break;
  1376. case MEDUSA_READ:
  1377. cx25821_i2c_read(&dev->i2c_bus[0],
  1378. (u16) data_from_user->reg_address,
  1379. &data_from_user->reg_data);
  1380. break;
  1381. case MEDUSA_WRITE:
  1382. cx25821_i2c_write(&dev->i2c_bus[0],
  1383. (u16) data_from_user->reg_address,
  1384. data_from_user->reg_data);
  1385. break;
  1386. }
  1387. return 0;
  1388. }
  1389. static long cx25821_video_ioctl(struct file *file,
  1390. unsigned int cmd, unsigned long arg)
  1391. {
  1392. int ret = 0;
  1393. struct cx25821_fh *fh = file->private_data;
  1394. /* check to see if it's the video upstream */
  1395. if (fh->channel_id == SRAM_CH09) {
  1396. ret = video_ioctl_upstream9(file, cmd, arg);
  1397. return ret;
  1398. } else if (fh->channel_id == SRAM_CH10) {
  1399. ret = video_ioctl_upstream10(file, cmd, arg);
  1400. return ret;
  1401. } else if (fh->channel_id == SRAM_CH11) {
  1402. ret = video_ioctl_upstream11(file, cmd, arg);
  1403. ret = video_ioctl_set(file, cmd, arg);
  1404. return ret;
  1405. }
  1406. return video_ioctl2(file, cmd, arg);
  1407. }
  1408. /* exported stuff */
  1409. static const struct v4l2_file_operations video_fops = {
  1410. .owner = THIS_MODULE,
  1411. .open = video_open,
  1412. .release = video_release,
  1413. .read = video_read,
  1414. .poll = video_poll,
  1415. .mmap = cx25821_video_mmap,
  1416. .ioctl = cx25821_video_ioctl,
  1417. };
  1418. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1419. .vidioc_querycap = cx25821_vidioc_querycap,
  1420. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1421. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1422. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1423. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1424. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1425. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1426. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1427. .vidioc_dqbuf = vidioc_dqbuf,
  1428. .vidioc_g_std = cx25821_vidioc_g_std,
  1429. .vidioc_s_std = cx25821_vidioc_s_std,
  1430. .vidioc_cropcap = cx25821_vidioc_cropcap,
  1431. .vidioc_s_crop = cx25821_vidioc_s_crop,
  1432. .vidioc_g_crop = cx25821_vidioc_g_crop,
  1433. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1434. .vidioc_g_input = cx25821_vidioc_g_input,
  1435. .vidioc_s_input = cx25821_vidioc_s_input,
  1436. .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
  1437. .vidioc_s_ctrl = vidioc_s_ctrl,
  1438. .vidioc_queryctrl = cx25821_vidioc_queryctrl,
  1439. .vidioc_streamon = vidioc_streamon,
  1440. .vidioc_streamoff = vidioc_streamoff,
  1441. .vidioc_log_status = vidioc_log_status,
  1442. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1443. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1444. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1445. .vidioc_g_register = cx25821_vidioc_g_register,
  1446. .vidioc_s_register = cx25821_vidioc_s_register,
  1447. #endif
  1448. };
  1449. static const struct video_device cx25821_video_device = {
  1450. .name = "cx25821-video",
  1451. .fops = &video_fops,
  1452. .minor = -1,
  1453. .ioctl_ops = &video_ioctl_ops,
  1454. .tvnorms = CX25821_NORMS,
  1455. };
  1456. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1457. {
  1458. cx_clear(PCI_INT_MSK, 1);
  1459. if (dev->channels[chan_num].video_dev) {
  1460. if (video_is_registered(dev->channels[chan_num].video_dev))
  1461. video_unregister_device(
  1462. dev->channels[chan_num].video_dev);
  1463. else
  1464. video_device_release(
  1465. dev->channels[chan_num].video_dev);
  1466. dev->channels[chan_num].video_dev = NULL;
  1467. btcx_riscmem_free(dev->pci,
  1468. &dev->channels[chan_num].vidq.stopper);
  1469. pr_warn("device %d released!\n", chan_num);
  1470. }
  1471. }
  1472. int cx25821_video_register(struct cx25821_dev *dev)
  1473. {
  1474. int err;
  1475. int i;
  1476. spin_lock_init(&dev->slock);
  1477. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1478. if (i == SRAM_CH08) /* audio channel */
  1479. continue;
  1480. cx25821_init_controls(dev, i);
  1481. cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
  1482. dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
  1483. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  1484. dev->channels[i].video_dev = NULL;
  1485. dev->channels[i].resources = 0;
  1486. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  1487. INIT_LIST_HEAD(&dev->channels[i].vidq.active);
  1488. INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
  1489. dev->channels[i].timeout_data.dev = dev;
  1490. dev->channels[i].timeout_data.channel =
  1491. &cx25821_sram_channels[i];
  1492. dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
  1493. dev->channels[i].vidq.timeout.data =
  1494. (unsigned long)&dev->channels[i].timeout_data;
  1495. init_timer(&dev->channels[i].vidq.timeout);
  1496. /* register v4l devices */
  1497. dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
  1498. &cx25821_video_device, "video");
  1499. err = video_register_device(dev->channels[i].video_dev,
  1500. VFL_TYPE_GRABBER, video_nr[dev->nr]);
  1501. if (err < 0)
  1502. goto fail_unreg;
  1503. }
  1504. /* set PCI interrupt */
  1505. cx_set(PCI_INT_MSK, 0xff);
  1506. /* initial device configuration */
  1507. mutex_lock(&dev->lock);
  1508. dev->tvnorm = V4L2_STD_NTSC_M,
  1509. cx25821_set_tvnorm(dev, dev->tvnorm);
  1510. mutex_unlock(&dev->lock);
  1511. return 0;
  1512. fail_unreg:
  1513. cx25821_video_unregister(dev, i);
  1514. return err;
  1515. }