cx25821-video.c 49 KB

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