cx25821-video.c 49 KB

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