cx25821-video.c 49 KB

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