cx25821-video.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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 || INPUT(i)->type == 0)
  957. return -EINVAL;
  958. mutex_lock(&dev->lock);
  959. cx25821_video_mux(dev, i);
  960. mutex_unlock(&dev->lock);
  961. return 0;
  962. }
  963. #ifdef CONFIG_VIDEO_ADV_DEBUG
  964. int cx25821_vidioc_g_register(struct file *file, void *fh,
  965. struct v4l2_dbg_register *reg)
  966. {
  967. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  968. if (!v4l2_chip_match_host(&reg->match))
  969. return -EINVAL;
  970. cx25821_call_all(dev, core, g_register, reg);
  971. return 0;
  972. }
  973. int cx25821_vidioc_s_register(struct file *file, void *fh,
  974. const struct v4l2_dbg_register *reg)
  975. {
  976. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  977. if (!v4l2_chip_match_host(&reg->match))
  978. return -EINVAL;
  979. cx25821_call_all(dev, core, s_register, reg);
  980. return 0;
  981. }
  982. #endif
  983. /*****************************************************************************/
  984. static const struct v4l2_queryctrl no_ctl = {
  985. .name = "42",
  986. .flags = V4L2_CTRL_FLAG_DISABLED,
  987. };
  988. static struct v4l2_queryctrl cx25821_ctls[] = {
  989. /* --- video --- */
  990. {
  991. .id = V4L2_CID_BRIGHTNESS,
  992. .name = "Brightness",
  993. .minimum = 0,
  994. .maximum = 10000,
  995. .step = 1,
  996. .default_value = 6200,
  997. .type = V4L2_CTRL_TYPE_INTEGER,
  998. }, {
  999. .id = V4L2_CID_CONTRAST,
  1000. .name = "Contrast",
  1001. .minimum = 0,
  1002. .maximum = 10000,
  1003. .step = 1,
  1004. .default_value = 5000,
  1005. .type = V4L2_CTRL_TYPE_INTEGER,
  1006. }, {
  1007. .id = V4L2_CID_SATURATION,
  1008. .name = "Saturation",
  1009. .minimum = 0,
  1010. .maximum = 10000,
  1011. .step = 1,
  1012. .default_value = 5000,
  1013. .type = V4L2_CTRL_TYPE_INTEGER,
  1014. }, {
  1015. .id = V4L2_CID_HUE,
  1016. .name = "Hue",
  1017. .minimum = 0,
  1018. .maximum = 10000,
  1019. .step = 1,
  1020. .default_value = 5000,
  1021. .type = V4L2_CTRL_TYPE_INTEGER,
  1022. }
  1023. };
  1024. static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
  1025. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  1026. {
  1027. int i;
  1028. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  1029. return -EINVAL;
  1030. for (i = 0; i < CX25821_CTLS; i++)
  1031. if (cx25821_ctls[i].id == qctrl->id)
  1032. break;
  1033. if (i == CX25821_CTLS) {
  1034. *qctrl = no_ctl;
  1035. return 0;
  1036. }
  1037. *qctrl = cx25821_ctls[i];
  1038. return 0;
  1039. }
  1040. int cx25821_vidioc_queryctrl(struct file *file, void *priv,
  1041. struct v4l2_queryctrl *qctrl)
  1042. {
  1043. return cx25821_ctrl_query(qctrl);
  1044. }
  1045. /* ------------------------------------------------------------------ */
  1046. /* VIDEO CTRL IOCTLS */
  1047. static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
  1048. {
  1049. unsigned int i;
  1050. for (i = 0; i < CX25821_CTLS; i++)
  1051. if (cx25821_ctls[i].id == id)
  1052. return cx25821_ctls + i;
  1053. return NULL;
  1054. }
  1055. int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
  1056. struct v4l2_control *ctl)
  1057. {
  1058. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1059. struct cx25821_fh *fh = priv;
  1060. const struct v4l2_queryctrl *ctrl;
  1061. ctrl = ctrl_by_id(ctl->id);
  1062. if (NULL == ctrl)
  1063. return -EINVAL;
  1064. switch (ctl->id) {
  1065. case V4L2_CID_BRIGHTNESS:
  1066. ctl->value = dev->channels[fh->channel_id].ctl_bright;
  1067. break;
  1068. case V4L2_CID_HUE:
  1069. ctl->value = dev->channels[fh->channel_id].ctl_hue;
  1070. break;
  1071. case V4L2_CID_CONTRAST:
  1072. ctl->value = dev->channels[fh->channel_id].ctl_contrast;
  1073. break;
  1074. case V4L2_CID_SATURATION:
  1075. ctl->value = dev->channels[fh->channel_id].ctl_saturation;
  1076. break;
  1077. }
  1078. return 0;
  1079. }
  1080. int cx25821_set_control(struct cx25821_dev *dev,
  1081. struct v4l2_control *ctl, int chan_num)
  1082. {
  1083. int err;
  1084. const struct v4l2_queryctrl *ctrl;
  1085. err = -EINVAL;
  1086. ctrl = ctrl_by_id(ctl->id);
  1087. if (NULL == ctrl)
  1088. return err;
  1089. switch (ctrl->type) {
  1090. case V4L2_CTRL_TYPE_BOOLEAN:
  1091. case V4L2_CTRL_TYPE_MENU:
  1092. case V4L2_CTRL_TYPE_INTEGER:
  1093. if (ctl->value < ctrl->minimum)
  1094. ctl->value = ctrl->minimum;
  1095. if (ctl->value > ctrl->maximum)
  1096. ctl->value = ctrl->maximum;
  1097. break;
  1098. default:
  1099. /* nothing */ ;
  1100. }
  1101. switch (ctl->id) {
  1102. case V4L2_CID_BRIGHTNESS:
  1103. dev->channels[chan_num].ctl_bright = ctl->value;
  1104. medusa_set_brightness(dev, ctl->value, chan_num);
  1105. break;
  1106. case V4L2_CID_HUE:
  1107. dev->channels[chan_num].ctl_hue = ctl->value;
  1108. medusa_set_hue(dev, ctl->value, chan_num);
  1109. break;
  1110. case V4L2_CID_CONTRAST:
  1111. dev->channels[chan_num].ctl_contrast = ctl->value;
  1112. medusa_set_contrast(dev, ctl->value, chan_num);
  1113. break;
  1114. case V4L2_CID_SATURATION:
  1115. dev->channels[chan_num].ctl_saturation = ctl->value;
  1116. medusa_set_saturation(dev, ctl->value, chan_num);
  1117. break;
  1118. }
  1119. err = 0;
  1120. return err;
  1121. }
  1122. static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
  1123. {
  1124. struct v4l2_control ctrl;
  1125. int i;
  1126. for (i = 0; i < CX25821_CTLS; i++) {
  1127. ctrl.id = cx25821_ctls[i].id;
  1128. ctrl.value = cx25821_ctls[i].default_value;
  1129. cx25821_set_control(dev, &ctrl, chan_num);
  1130. }
  1131. }
  1132. int cx25821_vidioc_cropcap(struct file *file, void *priv,
  1133. struct v4l2_cropcap *cropcap)
  1134. {
  1135. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1136. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1137. return -EINVAL;
  1138. cropcap->bounds.top = 0;
  1139. cropcap->bounds.left = 0;
  1140. cropcap->bounds.width = 720;
  1141. cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
  1142. cropcap->pixelaspect.numerator =
  1143. dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
  1144. cropcap->pixelaspect.denominator =
  1145. dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
  1146. cropcap->defrect = cropcap->bounds;
  1147. return 0;
  1148. }
  1149. int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
  1150. {
  1151. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  1152. struct cx25821_fh *fh = priv;
  1153. int err;
  1154. if (fh) {
  1155. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  1156. fh->prio);
  1157. if (0 != err)
  1158. return err;
  1159. }
  1160. /* cx25821_vidioc_s_crop not supported */
  1161. return -EINVAL;
  1162. }
  1163. int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1164. {
  1165. /* cx25821_vidioc_g_crop not supported */
  1166. return -EINVAL;
  1167. }
  1168. int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  1169. {
  1170. if (tvnorm == V4L2_STD_PAL_BG) {
  1171. if (width == 352 || width == 720)
  1172. return 1;
  1173. else
  1174. return 0;
  1175. }
  1176. if (tvnorm == V4L2_STD_NTSC_M) {
  1177. if (width == 320 || width == 352 || width == 720)
  1178. return 1;
  1179. else
  1180. return 0;
  1181. }
  1182. return 0;
  1183. }
  1184. int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  1185. {
  1186. if (tvnorm == V4L2_STD_PAL_BG) {
  1187. if (height == 576 || height == 288)
  1188. return 1;
  1189. else
  1190. return 0;
  1191. }
  1192. if (tvnorm == V4L2_STD_NTSC_M) {
  1193. if (height == 480 || height == 240)
  1194. return 1;
  1195. else
  1196. return 0;
  1197. }
  1198. return 0;
  1199. }
  1200. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  1201. unsigned long arg)
  1202. {
  1203. struct cx25821_fh *fh = file->private_data;
  1204. struct cx25821_dev *dev = fh->dev;
  1205. int command = 0;
  1206. struct upstream_user_struct *data_from_user;
  1207. data_from_user = (struct upstream_user_struct *)arg;
  1208. if (!data_from_user) {
  1209. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1210. return 0;
  1211. }
  1212. command = data_from_user->command;
  1213. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1214. return 0;
  1215. dev->input_filename = data_from_user->input_filename;
  1216. dev->input_audiofilename = data_from_user->input_filename;
  1217. dev->vid_stdname = data_from_user->vid_stdname;
  1218. dev->pixel_format = data_from_user->pixel_format;
  1219. dev->channel_select = data_from_user->channel_select;
  1220. dev->command = data_from_user->command;
  1221. switch (command) {
  1222. case UPSTREAM_START_VIDEO:
  1223. cx25821_start_upstream_video_ch1(dev, data_from_user);
  1224. break;
  1225. case UPSTREAM_STOP_VIDEO:
  1226. cx25821_stop_upstream_video_ch1(dev);
  1227. break;
  1228. }
  1229. return 0;
  1230. }
  1231. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  1232. unsigned long arg)
  1233. {
  1234. struct cx25821_fh *fh = file->private_data;
  1235. struct cx25821_dev *dev = fh->dev;
  1236. int command = 0;
  1237. struct upstream_user_struct *data_from_user;
  1238. data_from_user = (struct upstream_user_struct *)arg;
  1239. if (!data_from_user) {
  1240. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1241. return 0;
  1242. }
  1243. command = data_from_user->command;
  1244. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1245. return 0;
  1246. dev->input_filename_ch2 = data_from_user->input_filename;
  1247. dev->input_audiofilename = data_from_user->input_filename;
  1248. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  1249. dev->pixel_format_ch2 = data_from_user->pixel_format;
  1250. dev->channel_select_ch2 = data_from_user->channel_select;
  1251. dev->command_ch2 = data_from_user->command;
  1252. switch (command) {
  1253. case UPSTREAM_START_VIDEO:
  1254. cx25821_start_upstream_video_ch2(dev, data_from_user);
  1255. break;
  1256. case UPSTREAM_STOP_VIDEO:
  1257. cx25821_stop_upstream_video_ch2(dev);
  1258. break;
  1259. }
  1260. return 0;
  1261. }
  1262. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  1263. unsigned long arg)
  1264. {
  1265. struct cx25821_fh *fh = file->private_data;
  1266. struct cx25821_dev *dev = fh->dev;
  1267. int command = 0;
  1268. struct upstream_user_struct *data_from_user;
  1269. data_from_user = (struct upstream_user_struct *)arg;
  1270. if (!data_from_user) {
  1271. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1272. return 0;
  1273. }
  1274. command = data_from_user->command;
  1275. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  1276. return 0;
  1277. dev->input_filename = data_from_user->input_filename;
  1278. dev->input_audiofilename = data_from_user->input_filename;
  1279. dev->vid_stdname = data_from_user->vid_stdname;
  1280. dev->pixel_format = data_from_user->pixel_format;
  1281. dev->channel_select = data_from_user->channel_select;
  1282. dev->command = data_from_user->command;
  1283. switch (command) {
  1284. case UPSTREAM_START_AUDIO:
  1285. cx25821_start_upstream_audio(dev, data_from_user);
  1286. break;
  1287. case UPSTREAM_STOP_AUDIO:
  1288. cx25821_stop_upstream_audio(dev);
  1289. break;
  1290. }
  1291. return 0;
  1292. }
  1293. static long video_ioctl_set(struct file *file, unsigned int cmd,
  1294. unsigned long arg)
  1295. {
  1296. struct cx25821_fh *fh = file->private_data;
  1297. struct cx25821_dev *dev = fh->dev;
  1298. struct downstream_user_struct *data_from_user;
  1299. int command;
  1300. int width = 720;
  1301. int selected_channel = 0;
  1302. int pix_format = 0;
  1303. int i = 0;
  1304. int cif_enable = 0;
  1305. int cif_width = 0;
  1306. data_from_user = (struct downstream_user_struct *)arg;
  1307. if (!data_from_user) {
  1308. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  1309. return 0;
  1310. }
  1311. command = data_from_user->command;
  1312. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  1313. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  1314. && command != REG_WRITE && command != MEDUSA_READ
  1315. && command != MEDUSA_WRITE) {
  1316. return 0;
  1317. }
  1318. switch (command) {
  1319. case SET_VIDEO_STD:
  1320. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  1321. dev->tvnorm = V4L2_STD_PAL_BG;
  1322. else
  1323. dev->tvnorm = V4L2_STD_NTSC_M;
  1324. medusa_set_videostandard(dev);
  1325. break;
  1326. case SET_PIXEL_FORMAT:
  1327. selected_channel = data_from_user->decoder_select;
  1328. pix_format = data_from_user->pixel_format;
  1329. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1330. selected_channel -= 4;
  1331. selected_channel = selected_channel % 8;
  1332. }
  1333. if (selected_channel >= 0)
  1334. cx25821_set_pixel_format(dev, selected_channel,
  1335. pix_format);
  1336. break;
  1337. case ENABLE_CIF_RESOLUTION:
  1338. selected_channel = data_from_user->decoder_select;
  1339. cif_enable = data_from_user->cif_resolution_enable;
  1340. cif_width = data_from_user->cif_width;
  1341. if (cif_enable) {
  1342. if (dev->tvnorm & V4L2_STD_PAL_BG
  1343. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1344. width = 352;
  1345. } else {
  1346. width = cif_width;
  1347. if (cif_width != 320 && cif_width != 352)
  1348. width = 320;
  1349. }
  1350. }
  1351. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1352. selected_channel -= 4;
  1353. selected_channel = selected_channel % 8;
  1354. }
  1355. if (selected_channel <= 7 && selected_channel >= 0) {
  1356. dev->channels[selected_channel].use_cif_resolution =
  1357. cif_enable;
  1358. dev->channels[selected_channel].cif_width = width;
  1359. } else {
  1360. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1361. dev->channels[i].use_cif_resolution =
  1362. cif_enable;
  1363. dev->channels[i].cif_width = width;
  1364. }
  1365. }
  1366. medusa_set_resolution(dev, width, selected_channel);
  1367. break;
  1368. case REG_READ:
  1369. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1370. break;
  1371. case REG_WRITE:
  1372. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1373. break;
  1374. case MEDUSA_READ:
  1375. cx25821_i2c_read(&dev->i2c_bus[0],
  1376. (u16) data_from_user->reg_address,
  1377. &data_from_user->reg_data);
  1378. break;
  1379. case MEDUSA_WRITE:
  1380. cx25821_i2c_write(&dev->i2c_bus[0],
  1381. (u16) data_from_user->reg_address,
  1382. data_from_user->reg_data);
  1383. break;
  1384. }
  1385. return 0;
  1386. }
  1387. static long cx25821_video_ioctl(struct file *file,
  1388. unsigned int cmd, unsigned long arg)
  1389. {
  1390. int ret = 0;
  1391. struct cx25821_fh *fh = file->private_data;
  1392. /* check to see if it's the video upstream */
  1393. if (fh->channel_id == SRAM_CH09) {
  1394. ret = video_ioctl_upstream9(file, cmd, arg);
  1395. return ret;
  1396. } else if (fh->channel_id == SRAM_CH10) {
  1397. ret = video_ioctl_upstream10(file, cmd, arg);
  1398. return ret;
  1399. } else if (fh->channel_id == SRAM_CH11) {
  1400. ret = video_ioctl_upstream11(file, cmd, arg);
  1401. ret = video_ioctl_set(file, cmd, arg);
  1402. return ret;
  1403. }
  1404. return video_ioctl2(file, cmd, arg);
  1405. }
  1406. /* exported stuff */
  1407. static const struct v4l2_file_operations video_fops = {
  1408. .owner = THIS_MODULE,
  1409. .open = video_open,
  1410. .release = video_release,
  1411. .read = video_read,
  1412. .poll = video_poll,
  1413. .mmap = cx25821_video_mmap,
  1414. .ioctl = cx25821_video_ioctl,
  1415. };
  1416. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1417. .vidioc_querycap = cx25821_vidioc_querycap,
  1418. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1419. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1420. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1421. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1422. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1423. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1424. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1425. .vidioc_dqbuf = vidioc_dqbuf,
  1426. .vidioc_g_std = cx25821_vidioc_g_std,
  1427. .vidioc_s_std = cx25821_vidioc_s_std,
  1428. .vidioc_cropcap = cx25821_vidioc_cropcap,
  1429. .vidioc_s_crop = cx25821_vidioc_s_crop,
  1430. .vidioc_g_crop = cx25821_vidioc_g_crop,
  1431. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1432. .vidioc_g_input = cx25821_vidioc_g_input,
  1433. .vidioc_s_input = cx25821_vidioc_s_input,
  1434. .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
  1435. .vidioc_s_ctrl = vidioc_s_ctrl,
  1436. .vidioc_queryctrl = cx25821_vidioc_queryctrl,
  1437. .vidioc_streamon = vidioc_streamon,
  1438. .vidioc_streamoff = vidioc_streamoff,
  1439. .vidioc_log_status = vidioc_log_status,
  1440. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1441. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1442. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1443. .vidioc_g_register = cx25821_vidioc_g_register,
  1444. .vidioc_s_register = cx25821_vidioc_s_register,
  1445. #endif
  1446. };
  1447. static const struct video_device cx25821_video_device = {
  1448. .name = "cx25821-video",
  1449. .fops = &video_fops,
  1450. .minor = -1,
  1451. .ioctl_ops = &video_ioctl_ops,
  1452. .tvnorms = CX25821_NORMS,
  1453. };
  1454. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1455. {
  1456. cx_clear(PCI_INT_MSK, 1);
  1457. if (dev->channels[chan_num].video_dev) {
  1458. if (video_is_registered(dev->channels[chan_num].video_dev))
  1459. video_unregister_device(
  1460. dev->channels[chan_num].video_dev);
  1461. else
  1462. video_device_release(
  1463. dev->channels[chan_num].video_dev);
  1464. dev->channels[chan_num].video_dev = NULL;
  1465. btcx_riscmem_free(dev->pci,
  1466. &dev->channels[chan_num].vidq.stopper);
  1467. pr_warn("device %d released!\n", chan_num);
  1468. }
  1469. }
  1470. int cx25821_video_register(struct cx25821_dev *dev)
  1471. {
  1472. int err;
  1473. int i;
  1474. spin_lock_init(&dev->slock);
  1475. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1476. if (i == SRAM_CH08) /* audio channel */
  1477. continue;
  1478. cx25821_init_controls(dev, i);
  1479. cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
  1480. dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
  1481. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  1482. dev->channels[i].video_dev = NULL;
  1483. dev->channels[i].resources = 0;
  1484. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  1485. INIT_LIST_HEAD(&dev->channels[i].vidq.active);
  1486. INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
  1487. dev->channels[i].timeout_data.dev = dev;
  1488. dev->channels[i].timeout_data.channel =
  1489. &cx25821_sram_channels[i];
  1490. dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
  1491. dev->channels[i].vidq.timeout.data =
  1492. (unsigned long)&dev->channels[i].timeout_data;
  1493. init_timer(&dev->channels[i].vidq.timeout);
  1494. /* register v4l devices */
  1495. dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
  1496. &cx25821_video_device, "video");
  1497. err = video_register_device(dev->channels[i].video_dev,
  1498. VFL_TYPE_GRABBER, video_nr[dev->nr]);
  1499. if (err < 0)
  1500. goto fail_unreg;
  1501. }
  1502. /* set PCI interrupt */
  1503. cx_set(PCI_INT_MSK, 0xff);
  1504. /* initial device configuration */
  1505. mutex_lock(&dev->lock);
  1506. dev->tvnorm = V4L2_STD_NTSC_M,
  1507. cx25821_set_tvnorm(dev, dev->tvnorm);
  1508. mutex_unlock(&dev->lock);
  1509. return 0;
  1510. fail_unreg:
  1511. cx25821_video_unregister(dev, i);
  1512. return err;
  1513. }