cx25821-video.c 48 KB

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