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