cx25821-video.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
  7. * Parts adapted/taken from Eduardo Moscoso Rubino
  8. * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include "cx25821-video.h"
  28. MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
  29. MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
  30. MODULE_LICENSE("GPL");
  31. static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
  32. module_param_array(video_nr, int, NULL, 0444);
  33. MODULE_PARM_DESC(video_nr, "video device numbers");
  34. static unsigned int video_debug = VIDEO_DEBUG;
  35. module_param(video_debug, int, 0644);
  36. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  37. static unsigned int irq_debug;
  38. module_param(irq_debug, int, 0644);
  39. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  40. static unsigned int vid_limit = 16;
  41. module_param(vid_limit, int, 0644);
  42. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  43. #define FORMAT_FLAGS_PACKED 0x01
  44. static const struct cx25821_fmt formats[] = {
  45. {
  46. .name = "8 bpp, gray",
  47. .fourcc = V4L2_PIX_FMT_GREY,
  48. .depth = 8,
  49. .flags = FORMAT_FLAGS_PACKED,
  50. }, {
  51. .name = "4:1:1, packed, Y41P",
  52. .fourcc = V4L2_PIX_FMT_Y41P,
  53. .depth = 12,
  54. .flags = FORMAT_FLAGS_PACKED,
  55. }, {
  56. .name = "4:2:2, packed, YUYV",
  57. .fourcc = V4L2_PIX_FMT_YUYV,
  58. .depth = 16,
  59. .flags = FORMAT_FLAGS_PACKED,
  60. }, {
  61. .name = "4:2:2, packed, UYVY",
  62. .fourcc = V4L2_PIX_FMT_UYVY,
  63. .depth = 16,
  64. .flags = FORMAT_FLAGS_PACKED,
  65. }, {
  66. .name = "4:2:0, YUV",
  67. .fourcc = V4L2_PIX_FMT_YUV420,
  68. .depth = 12,
  69. .flags = FORMAT_FLAGS_PACKED,
  70. },
  71. };
  72. static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
  73. {
  74. unsigned int i;
  75. if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
  76. return formats + 1;
  77. for (i = 0; i < ARRAY_SIZE(formats); i++)
  78. if (formats[i].fourcc == fourcc)
  79. return formats + i;
  80. pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
  81. return NULL;
  82. }
  83. void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
  84. u32 count)
  85. {
  86. struct cx25821_buffer *buf;
  87. int bc;
  88. for (bc = 0;; bc++) {
  89. if (list_empty(&q->active)) {
  90. dprintk(1, "bc=%d (=0: active empty)\n", bc);
  91. break;
  92. }
  93. buf = list_entry(q->active.next, struct cx25821_buffer,
  94. vb.queue);
  95. /* count comes from the hw and it is 16bit wide --
  96. * this trick handles wrap-arounds correctly for
  97. * up to 32767 buffers in flight... */
  98. if ((s16) (count - buf->count) < 0)
  99. break;
  100. v4l2_get_timestamp(&buf->vb.ts);
  101. buf->vb.state = VIDEOBUF_DONE;
  102. list_del(&buf->vb.queue);
  103. wake_up(&buf->vb.done);
  104. }
  105. if (list_empty(&q->active))
  106. del_timer(&q->timeout);
  107. else
  108. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  109. if (bc != 1)
  110. pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
  111. }
  112. static int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
  113. {
  114. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  115. __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
  116. dev->tvnorm = norm;
  117. /* Tell the internal A/V decoder */
  118. cx25821_call_all(dev, core, s_std, norm);
  119. return 0;
  120. }
  121. /*
  122. static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
  123. {
  124. int i;
  125. if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
  126. return -EINVAL;
  127. for (i = 0; i < CX25821_CTLS; i++)
  128. if (cx25821_ctls[i].v.id == qctrl->id)
  129. break;
  130. if (i == CX25821_CTLS) {
  131. *qctrl = no_ctl;
  132. return 0;
  133. }
  134. *qctrl = cx25821_ctls[i].v;
  135. return 0;
  136. }
  137. */
  138. /* resource management */
  139. int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
  140. unsigned int bit)
  141. {
  142. dprintk(1, "%s()\n", __func__);
  143. if (fh->resources & bit)
  144. /* have it already allocated */
  145. return 1;
  146. /* is it free? */
  147. mutex_lock(&dev->lock);
  148. if (dev->channels[fh->channel_id].resources & bit) {
  149. /* no, someone else uses it */
  150. mutex_unlock(&dev->lock);
  151. return 0;
  152. }
  153. /* it's free, grab it */
  154. fh->resources |= bit;
  155. dev->channels[fh->channel_id].resources |= bit;
  156. dprintk(1, "res: get %d\n", bit);
  157. mutex_unlock(&dev->lock);
  158. return 1;
  159. }
  160. int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
  161. {
  162. return fh->resources & bit;
  163. }
  164. int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
  165. {
  166. return fh->dev->channels[fh->channel_id].resources & bit;
  167. }
  168. void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
  169. unsigned int bits)
  170. {
  171. BUG_ON((fh->resources & bits) != bits);
  172. dprintk(1, "%s()\n", __func__);
  173. mutex_lock(&dev->lock);
  174. fh->resources &= ~bits;
  175. dev->channels[fh->channel_id].resources &= ~bits;
  176. dprintk(1, "res: put %d\n", bits);
  177. mutex_unlock(&dev->lock);
  178. }
  179. static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
  180. {
  181. struct v4l2_routing route;
  182. memset(&route, 0, sizeof(route));
  183. dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  184. __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
  185. INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
  186. dev->input = input;
  187. route.input = INPUT(input)->vmux;
  188. /* Tell the internal A/V decoder */
  189. cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
  190. return 0;
  191. }
  192. int cx25821_start_video_dma(struct cx25821_dev *dev,
  193. struct cx25821_dmaqueue *q,
  194. struct cx25821_buffer *buf,
  195. const struct sram_channel *channel)
  196. {
  197. int tmp = 0;
  198. /* setup fifo + format */
  199. cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
  200. /* reset counter */
  201. cx_write(channel->gpcnt_ctl, 3);
  202. q->count = 1;
  203. /* enable irq */
  204. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
  205. cx_set(channel->int_msk, 0x11);
  206. /* start dma */
  207. cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
  208. /* make sure upstream setting if any is reversed */
  209. tmp = cx_read(VID_CH_MODE_SEL);
  210. cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
  211. return 0;
  212. }
  213. static int cx25821_restart_video_queue(struct cx25821_dev *dev,
  214. struct cx25821_dmaqueue *q,
  215. const struct sram_channel *channel)
  216. {
  217. struct cx25821_buffer *buf, *prev;
  218. struct list_head *item;
  219. if (!list_empty(&q->active)) {
  220. buf = list_entry(q->active.next, struct cx25821_buffer,
  221. vb.queue);
  222. cx25821_start_video_dma(dev, q, buf, channel);
  223. list_for_each(item, &q->active) {
  224. buf = list_entry(item, struct cx25821_buffer, vb.queue);
  225. buf->count = q->count++;
  226. }
  227. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  228. return 0;
  229. }
  230. prev = NULL;
  231. for (;;) {
  232. if (list_empty(&q->queued))
  233. return 0;
  234. buf = list_entry(q->queued.next, struct cx25821_buffer,
  235. vb.queue);
  236. if (NULL == prev) {
  237. list_move_tail(&buf->vb.queue, &q->active);
  238. cx25821_start_video_dma(dev, q, buf, channel);
  239. buf->vb.state = VIDEOBUF_ACTIVE;
  240. buf->count = q->count++;
  241. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  242. } else if (prev->vb.width == buf->vb.width &&
  243. prev->vb.height == buf->vb.height &&
  244. prev->fmt == buf->fmt) {
  245. list_move_tail(&buf->vb.queue, &q->active);
  246. buf->vb.state = VIDEOBUF_ACTIVE;
  247. buf->count = q->count++;
  248. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  249. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  250. } else {
  251. return 0;
  252. }
  253. prev = buf;
  254. }
  255. }
  256. static void cx25821_vid_timeout(unsigned long data)
  257. {
  258. struct cx25821_data *timeout_data = (struct cx25821_data *)data;
  259. struct cx25821_dev *dev = timeout_data->dev;
  260. const struct sram_channel *channel = timeout_data->channel;
  261. struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
  262. struct cx25821_buffer *buf;
  263. unsigned long flags;
  264. /* cx25821_sram_channel_dump(dev, channel); */
  265. cx_clear(channel->dma_ctl, 0x11);
  266. spin_lock_irqsave(&dev->slock, flags);
  267. while (!list_empty(&q->active)) {
  268. buf = list_entry(q->active.next, struct cx25821_buffer,
  269. vb.queue);
  270. list_del(&buf->vb.queue);
  271. buf->vb.state = VIDEOBUF_ERROR;
  272. wake_up(&buf->vb.done);
  273. }
  274. cx25821_restart_video_queue(dev, q, channel);
  275. spin_unlock_irqrestore(&dev->slock, flags);
  276. }
  277. int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
  278. {
  279. u32 count = 0;
  280. int handled = 0;
  281. u32 mask;
  282. const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
  283. mask = cx_read(channel->int_msk);
  284. if (0 == (status & mask))
  285. return handled;
  286. cx_write(channel->int_stat, status);
  287. /* risc op code error */
  288. if (status & (1 << 16)) {
  289. pr_warn("%s, %s: video risc op code error\n",
  290. dev->name, channel->name);
  291. cx_clear(channel->dma_ctl, 0x11);
  292. cx25821_sram_channel_dump(dev, channel);
  293. }
  294. /* risc1 y */
  295. if (status & FLD_VID_DST_RISC1) {
  296. spin_lock(&dev->slock);
  297. count = cx_read(channel->gpcnt);
  298. cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
  299. count);
  300. spin_unlock(&dev->slock);
  301. handled++;
  302. }
  303. /* risc2 y */
  304. if (status & 0x10) {
  305. dprintk(2, "stopper video\n");
  306. spin_lock(&dev->slock);
  307. cx25821_restart_video_queue(dev,
  308. &dev->channels[channel->i].vidq, channel);
  309. spin_unlock(&dev->slock);
  310. handled++;
  311. }
  312. return handled;
  313. }
  314. static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  315. unsigned int *size)
  316. {
  317. struct cx25821_fh *fh = q->priv_data;
  318. *size = fh->fmt->depth * fh->width * fh->height >> 3;
  319. if (0 == *count)
  320. *count = 32;
  321. if (*size * *count > vid_limit * 1024 * 1024)
  322. *count = (vid_limit * 1024 * 1024) / *size;
  323. return 0;
  324. }
  325. static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  326. enum v4l2_field field)
  327. {
  328. struct cx25821_fh *fh = q->priv_data;
  329. struct cx25821_dev *dev = fh->dev;
  330. struct cx25821_buffer *buf =
  331. container_of(vb, struct cx25821_buffer, vb);
  332. int rc, init_buffer = 0;
  333. u32 line0_offset;
  334. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  335. int bpl_local = LINE_SIZE_D1;
  336. int channel_opened = fh->channel_id;
  337. BUG_ON(NULL == fh->fmt);
  338. if (fh->width < 48 || fh->width > 720 ||
  339. fh->height < 32 || fh->height > 576)
  340. return -EINVAL;
  341. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  342. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  343. return -EINVAL;
  344. if (buf->fmt != fh->fmt ||
  345. buf->vb.width != fh->width ||
  346. buf->vb.height != fh->height || buf->vb.field != field) {
  347. buf->fmt = fh->fmt;
  348. buf->vb.width = fh->width;
  349. buf->vb.height = fh->height;
  350. buf->vb.field = field;
  351. init_buffer = 1;
  352. }
  353. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  354. init_buffer = 1;
  355. rc = videobuf_iolock(q, &buf->vb, NULL);
  356. if (0 != rc) {
  357. printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
  358. goto fail;
  359. }
  360. }
  361. dprintk(1, "init_buffer=%d\n", init_buffer);
  362. if (init_buffer) {
  363. channel_opened = dev->channel_opened;
  364. if (channel_opened < 0 || channel_opened > 7)
  365. channel_opened = 7;
  366. if (dev->channels[channel_opened].pixel_formats ==
  367. PIXEL_FRMT_411)
  368. buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
  369. else
  370. buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
  371. if (dev->channels[channel_opened].pixel_formats ==
  372. PIXEL_FRMT_411) {
  373. bpl_local = buf->bpl;
  374. } else {
  375. bpl_local = buf->bpl; /* Default */
  376. if (channel_opened >= 0 && channel_opened <= 7) {
  377. if (dev->channels[channel_opened]
  378. .use_cif_resolution) {
  379. if (dev->tvnorm & V4L2_STD_PAL_BG ||
  380. dev->tvnorm & V4L2_STD_PAL_DK)
  381. bpl_local = 352 << 1;
  382. else
  383. bpl_local = dev->channels[
  384. channel_opened].
  385. cif_width << 1;
  386. }
  387. }
  388. }
  389. switch (buf->vb.field) {
  390. case V4L2_FIELD_TOP:
  391. cx25821_risc_buffer(dev->pci, &buf->risc,
  392. dma->sglist, 0, UNSET,
  393. buf->bpl, 0, buf->vb.height);
  394. break;
  395. case V4L2_FIELD_BOTTOM:
  396. cx25821_risc_buffer(dev->pci, &buf->risc,
  397. dma->sglist, UNSET, 0,
  398. buf->bpl, 0, buf->vb.height);
  399. break;
  400. case V4L2_FIELD_INTERLACED:
  401. /* All other formats are top field first */
  402. line0_offset = 0;
  403. dprintk(1, "top field first\n");
  404. cx25821_risc_buffer(dev->pci, &buf->risc,
  405. dma->sglist, line0_offset,
  406. bpl_local, bpl_local, bpl_local,
  407. buf->vb.height >> 1);
  408. break;
  409. case V4L2_FIELD_SEQ_TB:
  410. cx25821_risc_buffer(dev->pci, &buf->risc,
  411. dma->sglist,
  412. 0, buf->bpl * (buf->vb.height >> 1),
  413. buf->bpl, 0, buf->vb.height >> 1);
  414. break;
  415. case V4L2_FIELD_SEQ_BT:
  416. cx25821_risc_buffer(dev->pci, &buf->risc,
  417. dma->sglist,
  418. buf->bpl * (buf->vb.height >> 1), 0,
  419. buf->bpl, 0, buf->vb.height >> 1);
  420. break;
  421. default:
  422. BUG();
  423. }
  424. }
  425. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  426. buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
  427. fh->fmt->name, (unsigned long)buf->risc.dma);
  428. buf->vb.state = VIDEOBUF_PREPARED;
  429. return 0;
  430. fail:
  431. cx25821_free_buffer(q, buf);
  432. return rc;
  433. }
  434. static void cx25821_buffer_release(struct videobuf_queue *q,
  435. struct videobuf_buffer *vb)
  436. {
  437. struct cx25821_buffer *buf =
  438. container_of(vb, struct cx25821_buffer, vb);
  439. cx25821_free_buffer(q, buf);
  440. }
  441. static struct videobuf_queue *get_queue(struct cx25821_fh *fh)
  442. {
  443. switch (fh->type) {
  444. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  445. return &fh->vidq;
  446. default:
  447. BUG();
  448. return NULL;
  449. }
  450. }
  451. static int cx25821_get_resource(struct cx25821_fh *fh, int resource)
  452. {
  453. switch (fh->type) {
  454. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  455. return resource;
  456. default:
  457. BUG();
  458. return 0;
  459. }
  460. }
  461. static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
  462. {
  463. struct cx25821_fh *fh = file->private_data;
  464. return videobuf_mmap_mapper(get_queue(fh), vma);
  465. }
  466. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  467. {
  468. struct cx25821_buffer *buf =
  469. container_of(vb, struct cx25821_buffer, vb);
  470. struct cx25821_buffer *prev;
  471. struct cx25821_fh *fh = vq->priv_data;
  472. struct cx25821_dev *dev = fh->dev;
  473. struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
  474. /* add jump to stopper */
  475. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  476. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  477. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  478. dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  479. if (!list_empty(&q->queued)) {
  480. list_add_tail(&buf->vb.queue, &q->queued);
  481. buf->vb.state = VIDEOBUF_QUEUED;
  482. dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  483. buf->vb.i);
  484. } else if (list_empty(&q->active)) {
  485. list_add_tail(&buf->vb.queue, &q->active);
  486. cx25821_start_video_dma(dev, q, buf,
  487. dev->channels[fh->channel_id].sram_channels);
  488. buf->vb.state = VIDEOBUF_ACTIVE;
  489. buf->count = q->count++;
  490. mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  491. dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  492. buf, buf->vb.i, buf->count, q->count);
  493. } else {
  494. prev = list_entry(q->active.prev, struct cx25821_buffer,
  495. vb.queue);
  496. if (prev->vb.width == buf->vb.width
  497. && prev->vb.height == buf->vb.height
  498. && prev->fmt == buf->fmt) {
  499. list_add_tail(&buf->vb.queue, &q->active);
  500. buf->vb.state = VIDEOBUF_ACTIVE;
  501. buf->count = q->count++;
  502. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  503. /* 64 bit bits 63-32 */
  504. prev->risc.jmp[2] = cpu_to_le32(0);
  505. dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  506. buf, buf->vb.i, buf->count);
  507. } else {
  508. list_add_tail(&buf->vb.queue, &q->queued);
  509. buf->vb.state = VIDEOBUF_QUEUED;
  510. dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  511. buf->vb.i);
  512. }
  513. }
  514. if (list_empty(&q->active))
  515. dprintk(2, "active queue empty!\n");
  516. }
  517. static struct videobuf_queue_ops cx25821_video_qops = {
  518. .buf_setup = cx25821_buffer_setup,
  519. .buf_prepare = cx25821_buffer_prepare,
  520. .buf_queue = buffer_queue,
  521. .buf_release = cx25821_buffer_release,
  522. };
  523. static int video_open(struct file *file)
  524. {
  525. struct video_device *vdev = video_devdata(file);
  526. struct cx25821_dev *dev = video_drvdata(file);
  527. struct cx25821_fh *fh;
  528. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  529. u32 pix_format;
  530. int ch_id;
  531. for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
  532. if (&dev->channels[ch_id].vdev == vdev)
  533. break;
  534. /* Can't happen */
  535. if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
  536. return -ENODEV;
  537. /* allocate + initialize per filehandle data */
  538. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  539. if (NULL == fh)
  540. return -ENOMEM;
  541. file->private_data = fh;
  542. fh->dev = dev;
  543. fh->type = type;
  544. fh->width = 720;
  545. fh->channel_id = ch_id;
  546. if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
  547. fh->height = 576;
  548. else
  549. fh->height = 480;
  550. dev->channel_opened = fh->channel_id;
  551. if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
  552. pix_format = V4L2_PIX_FMT_Y41P;
  553. else
  554. pix_format = V4L2_PIX_FMT_YUYV;
  555. fh->fmt = cx25821_format_by_fourcc(pix_format);
  556. v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
  557. videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
  558. &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  559. V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
  560. fh, NULL);
  561. dprintk(1, "post videobuf_queue_init()\n");
  562. return 0;
  563. }
  564. static ssize_t video_read(struct file *file, char __user * data, size_t count,
  565. loff_t *ppos)
  566. {
  567. struct cx25821_fh *fh = file->private_data;
  568. switch (fh->type) {
  569. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  570. if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
  571. return -EBUSY;
  572. return videobuf_read_one(&fh->vidq, data, count, ppos,
  573. file->f_flags & O_NONBLOCK);
  574. default:
  575. BUG();
  576. return 0;
  577. }
  578. }
  579. static unsigned int video_poll(struct file *file,
  580. struct poll_table_struct *wait)
  581. {
  582. struct cx25821_fh *fh = file->private_data;
  583. struct cx25821_buffer *buf;
  584. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  585. /* streaming capture */
  586. if (list_empty(&fh->vidq.stream))
  587. return POLLERR;
  588. buf = list_entry(fh->vidq.stream.next,
  589. struct cx25821_buffer, vb.stream);
  590. } else {
  591. /* read() capture */
  592. buf = (struct cx25821_buffer *)fh->vidq.read_buf;
  593. if (NULL == buf)
  594. return POLLERR;
  595. }
  596. poll_wait(file, &buf->vb.done, wait);
  597. if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
  598. if (buf->vb.state == VIDEOBUF_DONE) {
  599. struct cx25821_dev *dev = fh->dev;
  600. if (dev && dev->channels[fh->channel_id]
  601. .use_cif_resolution) {
  602. u8 cam_id = *((char *)buf->vb.baddr + 3);
  603. memcpy((char *)buf->vb.baddr,
  604. (char *)buf->vb.baddr + (fh->width * 2),
  605. (fh->width * 2));
  606. *((char *)buf->vb.baddr + 3) = cam_id;
  607. }
  608. }
  609. return POLLIN | POLLRDNORM;
  610. }
  611. return 0;
  612. }
  613. static int video_release(struct file *file)
  614. {
  615. struct cx25821_fh *fh = file->private_data;
  616. struct cx25821_dev *dev = fh->dev;
  617. const struct sram_channel *sram_ch =
  618. dev->channels[0].sram_channels;
  619. /* stop the risc engine and fifo */
  620. cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
  621. /* stop video capture */
  622. if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
  623. videobuf_queue_cancel(&fh->vidq);
  624. cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
  625. }
  626. if (fh->vidq.read_buf) {
  627. cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
  628. kfree(fh->vidq.read_buf);
  629. }
  630. videobuf_mmap_free(&fh->vidq);
  631. v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
  632. file->private_data = NULL;
  633. kfree(fh);
  634. return 0;
  635. }
  636. /* VIDEO IOCTLS */
  637. static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  638. struct v4l2_format *f)
  639. {
  640. struct cx25821_fh *fh = priv;
  641. f->fmt.pix.width = fh->width;
  642. f->fmt.pix.height = fh->height;
  643. f->fmt.pix.field = fh->vidq.field;
  644. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  645. f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
  646. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  647. return 0;
  648. }
  649. static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  650. struct v4l2_format *f)
  651. {
  652. const struct cx25821_fmt *fmt;
  653. enum v4l2_field field;
  654. unsigned int maxw, maxh;
  655. fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  656. if (NULL == fmt)
  657. return -EINVAL;
  658. field = f->fmt.pix.field;
  659. maxw = 720;
  660. maxh = 576;
  661. if (V4L2_FIELD_ANY == field) {
  662. if (f->fmt.pix.height > maxh / 2)
  663. field = V4L2_FIELD_INTERLACED;
  664. else
  665. field = V4L2_FIELD_TOP;
  666. }
  667. switch (field) {
  668. case V4L2_FIELD_TOP:
  669. case V4L2_FIELD_BOTTOM:
  670. maxh = maxh / 2;
  671. break;
  672. case V4L2_FIELD_INTERLACED:
  673. break;
  674. default:
  675. return -EINVAL;
  676. }
  677. f->fmt.pix.field = field;
  678. if (f->fmt.pix.height < 32)
  679. f->fmt.pix.height = 32;
  680. if (f->fmt.pix.height > maxh)
  681. f->fmt.pix.height = maxh;
  682. if (f->fmt.pix.width < 48)
  683. f->fmt.pix.width = 48;
  684. if (f->fmt.pix.width > maxw)
  685. f->fmt.pix.width = maxw;
  686. f->fmt.pix.width &= ~0x03;
  687. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  688. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  689. return 0;
  690. }
  691. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  692. {
  693. struct cx25821_fh *fh = priv;
  694. struct cx25821_dev *dev = fh->dev;
  695. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  696. return -EINVAL;
  697. if (unlikely(i != fh->type))
  698. return -EINVAL;
  699. if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
  700. RESOURCE_VIDEO0))))
  701. return -EBUSY;
  702. return videobuf_streamon(get_queue(fh));
  703. }
  704. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  705. {
  706. struct cx25821_fh *fh = priv;
  707. struct cx25821_dev *dev = fh->dev;
  708. int err, res;
  709. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  710. return -EINVAL;
  711. if (i != fh->type)
  712. return -EINVAL;
  713. res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
  714. err = videobuf_streamoff(get_queue(fh));
  715. if (err < 0)
  716. return err;
  717. cx25821_res_free(dev, fh, res);
  718. return 0;
  719. }
  720. static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
  721. {
  722. if (tvnorm == V4L2_STD_PAL_BG) {
  723. if (width == 352 || width == 720)
  724. return 1;
  725. else
  726. return 0;
  727. }
  728. if (tvnorm == V4L2_STD_NTSC_M) {
  729. if (width == 320 || width == 352 || width == 720)
  730. return 1;
  731. else
  732. return 0;
  733. }
  734. return 0;
  735. }
  736. static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
  737. {
  738. if (tvnorm == V4L2_STD_PAL_BG) {
  739. if (height == 576 || height == 288)
  740. return 1;
  741. else
  742. return 0;
  743. }
  744. if (tvnorm == V4L2_STD_NTSC_M) {
  745. if (height == 480 || height == 240)
  746. return 1;
  747. else
  748. return 0;
  749. }
  750. return 0;
  751. }
  752. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  753. struct v4l2_format *f)
  754. {
  755. struct cx25821_fh *fh = priv;
  756. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  757. struct v4l2_mbus_framefmt mbus_fmt;
  758. int err;
  759. int pix_format = PIXEL_FRMT_422;
  760. if (fh) {
  761. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  762. fh->prio);
  763. if (0 != err)
  764. return err;
  765. }
  766. err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
  767. if (0 != err)
  768. return err;
  769. fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
  770. fh->vidq.field = f->fmt.pix.field;
  771. /* check if width and height is valid based on set standard */
  772. if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
  773. fh->width = f->fmt.pix.width;
  774. if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
  775. fh->height = f->fmt.pix.height;
  776. if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
  777. pix_format = PIXEL_FRMT_411;
  778. else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
  779. pix_format = PIXEL_FRMT_422;
  780. else
  781. return -EINVAL;
  782. cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
  783. /* check if cif resolution */
  784. if (fh->width == 320 || fh->width == 352)
  785. dev->channels[fh->channel_id].use_cif_resolution = 1;
  786. else
  787. dev->channels[fh->channel_id].use_cif_resolution = 0;
  788. dev->channels[fh->channel_id].cif_width = fh->width;
  789. medusa_set_resolution(dev, fh->width, SRAM_CH00);
  790. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  791. cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  792. return 0;
  793. }
  794. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  795. {
  796. int ret_val = 0;
  797. struct cx25821_fh *fh = priv;
  798. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  799. ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
  800. p->sequence = dev->channels[fh->channel_id].vidq.count;
  801. return ret_val;
  802. }
  803. static int vidioc_log_status(struct file *file, void *priv)
  804. {
  805. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  806. struct cx25821_fh *fh = priv;
  807. const struct sram_channel *sram_ch =
  808. dev->channels[fh->channel_id].sram_channels;
  809. u32 tmp = 0;
  810. cx25821_call_all(dev, core, log_status);
  811. tmp = cx_read(sram_ch->dma_ctl);
  812. pr_info("Video input 0 is %s\n",
  813. (tmp & 0x11) ? "streaming" : "stopped");
  814. return 0;
  815. }
  816. static int cx25821_vidioc_querycap(struct file *file, void *priv,
  817. struct v4l2_capability *cap)
  818. {
  819. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  820. struct cx25821_fh *fh = priv;
  821. const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
  822. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  823. const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
  824. strcpy(cap->driver, "cx25821");
  825. strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
  826. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  827. if (fh->channel_id >= VID_CHANNEL_NUM)
  828. cap->device_caps = cap_output;
  829. else
  830. cap->device_caps = cap_input;
  831. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  832. return 0;
  833. }
  834. static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  835. struct v4l2_fmtdesc *f)
  836. {
  837. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  838. return -EINVAL;
  839. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  840. f->pixelformat = formats[f->index].fourcc;
  841. return 0;
  842. }
  843. static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
  844. struct v4l2_requestbuffers *p)
  845. {
  846. struct cx25821_fh *fh = priv;
  847. return videobuf_reqbufs(get_queue(fh), p);
  848. }
  849. static int cx25821_vidioc_querybuf(struct file *file, void *priv,
  850. struct v4l2_buffer *p)
  851. {
  852. struct cx25821_fh *fh = priv;
  853. return videobuf_querybuf(get_queue(fh), p);
  854. }
  855. static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  856. {
  857. struct cx25821_fh *fh = priv;
  858. return videobuf_qbuf(get_queue(fh), p);
  859. }
  860. static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  861. {
  862. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  863. struct cx25821_fh *fh = f;
  864. *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
  865. return 0;
  866. }
  867. static int cx25821_vidioc_s_priority(struct file *file, void *f,
  868. enum v4l2_priority prio)
  869. {
  870. struct cx25821_fh *fh = f;
  871. struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
  872. return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
  873. prio);
  874. }
  875. static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  876. {
  877. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  878. *tvnorms = dev->tvnorm;
  879. return 0;
  880. }
  881. int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  882. {
  883. struct cx25821_fh *fh = priv;
  884. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  885. int err;
  886. if (fh) {
  887. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  888. fh->prio);
  889. if (0 != err)
  890. return err;
  891. }
  892. if (dev->tvnorm == tvnorms)
  893. return 0;
  894. mutex_lock(&dev->lock);
  895. cx25821_set_tvnorm(dev, tvnorms);
  896. mutex_unlock(&dev->lock);
  897. medusa_set_videostandard(dev);
  898. return 0;
  899. }
  900. static int cx25821_vidioc_enum_input(struct file *file, void *priv,
  901. struct v4l2_input *i)
  902. {
  903. static const char * const iname[] = {
  904. [CX25821_VMUX_COMPOSITE] = "Composite",
  905. [CX25821_VMUX_SVIDEO] = "S-Video",
  906. [CX25821_VMUX_DEBUG] = "for debug only",
  907. };
  908. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  909. unsigned int n;
  910. n = i->index;
  911. if (n >= CX25821_NR_INPUT)
  912. return -EINVAL;
  913. if (0 == INPUT(n)->type)
  914. return -EINVAL;
  915. i->type = V4L2_INPUT_TYPE_CAMERA;
  916. strcpy(i->name, iname[INPUT(n)->type]);
  917. i->std = CX25821_NORMS;
  918. return 0;
  919. }
  920. static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  921. {
  922. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  923. *i = dev->input;
  924. return 0;
  925. }
  926. static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  927. {
  928. struct cx25821_fh *fh = priv;
  929. struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
  930. int err;
  931. if (fh) {
  932. err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
  933. fh->prio);
  934. if (0 != err)
  935. return err;
  936. }
  937. if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
  938. return -EINVAL;
  939. mutex_lock(&dev->lock);
  940. cx25821_video_mux(dev, i);
  941. mutex_unlock(&dev->lock);
  942. return 0;
  943. }
  944. #ifdef CONFIG_VIDEO_ADV_DEBUG
  945. int cx25821_vidioc_g_register(struct file *file, void *fh,
  946. struct v4l2_dbg_register *reg)
  947. {
  948. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  949. if (!v4l2_chip_match_host(&reg->match))
  950. return -EINVAL;
  951. cx25821_call_all(dev, core, g_register, reg);
  952. return 0;
  953. }
  954. int cx25821_vidioc_s_register(struct file *file, void *fh,
  955. const struct v4l2_dbg_register *reg)
  956. {
  957. struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
  958. if (!v4l2_chip_match_host(&reg->match))
  959. return -EINVAL;
  960. cx25821_call_all(dev, core, s_register, reg);
  961. return 0;
  962. }
  963. #endif
  964. static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
  965. {
  966. struct cx25821_channel *chan =
  967. container_of(ctrl->handler, struct cx25821_channel, hdl);
  968. struct cx25821_dev *dev = chan->dev;
  969. switch (ctrl->id) {
  970. case V4L2_CID_BRIGHTNESS:
  971. medusa_set_brightness(dev, ctrl->val, chan->id);
  972. break;
  973. case V4L2_CID_HUE:
  974. medusa_set_hue(dev, ctrl->val, chan->id);
  975. break;
  976. case V4L2_CID_CONTRAST:
  977. medusa_set_contrast(dev, ctrl->val, chan->id);
  978. break;
  979. case V4L2_CID_SATURATION:
  980. medusa_set_saturation(dev, ctrl->val, chan->id);
  981. break;
  982. default:
  983. return -EINVAL;
  984. }
  985. return 0;
  986. }
  987. static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
  988. unsigned long arg)
  989. {
  990. struct cx25821_fh *fh = file->private_data;
  991. struct cx25821_dev *dev = fh->dev;
  992. int command = 0;
  993. struct upstream_user_struct *data_from_user;
  994. data_from_user = (struct upstream_user_struct *)arg;
  995. if (!data_from_user) {
  996. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  997. return 0;
  998. }
  999. command = data_from_user->command;
  1000. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1001. return 0;
  1002. dev->input_filename = data_from_user->input_filename;
  1003. dev->input_audiofilename = data_from_user->input_filename;
  1004. dev->vid_stdname = data_from_user->vid_stdname;
  1005. dev->pixel_format = data_from_user->pixel_format;
  1006. dev->channel_select = data_from_user->channel_select;
  1007. dev->command = data_from_user->command;
  1008. switch (command) {
  1009. case UPSTREAM_START_VIDEO:
  1010. cx25821_start_upstream_video_ch1(dev, data_from_user);
  1011. break;
  1012. case UPSTREAM_STOP_VIDEO:
  1013. cx25821_stop_upstream_video_ch1(dev);
  1014. break;
  1015. }
  1016. return 0;
  1017. }
  1018. static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
  1019. unsigned long arg)
  1020. {
  1021. struct cx25821_fh *fh = file->private_data;
  1022. struct cx25821_dev *dev = fh->dev;
  1023. int command = 0;
  1024. struct upstream_user_struct *data_from_user;
  1025. data_from_user = (struct upstream_user_struct *)arg;
  1026. if (!data_from_user) {
  1027. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1028. return 0;
  1029. }
  1030. command = data_from_user->command;
  1031. if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
  1032. return 0;
  1033. dev->input_filename_ch2 = data_from_user->input_filename;
  1034. dev->input_audiofilename = data_from_user->input_filename;
  1035. dev->vid_stdname_ch2 = data_from_user->vid_stdname;
  1036. dev->pixel_format_ch2 = data_from_user->pixel_format;
  1037. dev->channel_select_ch2 = data_from_user->channel_select;
  1038. dev->command_ch2 = data_from_user->command;
  1039. switch (command) {
  1040. case UPSTREAM_START_VIDEO:
  1041. cx25821_start_upstream_video_ch2(dev, data_from_user);
  1042. break;
  1043. case UPSTREAM_STOP_VIDEO:
  1044. cx25821_stop_upstream_video_ch2(dev);
  1045. break;
  1046. }
  1047. return 0;
  1048. }
  1049. static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
  1050. unsigned long arg)
  1051. {
  1052. struct cx25821_fh *fh = file->private_data;
  1053. struct cx25821_dev *dev = fh->dev;
  1054. int command = 0;
  1055. struct upstream_user_struct *data_from_user;
  1056. data_from_user = (struct upstream_user_struct *)arg;
  1057. if (!data_from_user) {
  1058. pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
  1059. return 0;
  1060. }
  1061. command = data_from_user->command;
  1062. if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
  1063. return 0;
  1064. dev->input_filename = data_from_user->input_filename;
  1065. dev->input_audiofilename = data_from_user->input_filename;
  1066. dev->vid_stdname = data_from_user->vid_stdname;
  1067. dev->pixel_format = data_from_user->pixel_format;
  1068. dev->channel_select = data_from_user->channel_select;
  1069. dev->command = data_from_user->command;
  1070. switch (command) {
  1071. case UPSTREAM_START_AUDIO:
  1072. cx25821_start_upstream_audio(dev, data_from_user);
  1073. break;
  1074. case UPSTREAM_STOP_AUDIO:
  1075. cx25821_stop_upstream_audio(dev);
  1076. break;
  1077. }
  1078. return 0;
  1079. }
  1080. static long video_ioctl_set(struct file *file, unsigned int cmd,
  1081. unsigned long arg)
  1082. {
  1083. struct cx25821_fh *fh = file->private_data;
  1084. struct cx25821_dev *dev = fh->dev;
  1085. struct downstream_user_struct *data_from_user;
  1086. int command;
  1087. int width = 720;
  1088. int selected_channel = 0;
  1089. int pix_format = 0;
  1090. int i = 0;
  1091. int cif_enable = 0;
  1092. int cif_width = 0;
  1093. data_from_user = (struct downstream_user_struct *)arg;
  1094. if (!data_from_user) {
  1095. pr_err("%s(): User data is INVALID. Returning\n", __func__);
  1096. return 0;
  1097. }
  1098. command = data_from_user->command;
  1099. if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
  1100. && command != ENABLE_CIF_RESOLUTION && command != REG_READ
  1101. && command != REG_WRITE && command != MEDUSA_READ
  1102. && command != MEDUSA_WRITE) {
  1103. return 0;
  1104. }
  1105. switch (command) {
  1106. case SET_VIDEO_STD:
  1107. if (!strcmp(data_from_user->vid_stdname, "PAL"))
  1108. dev->tvnorm = V4L2_STD_PAL_BG;
  1109. else
  1110. dev->tvnorm = V4L2_STD_NTSC_M;
  1111. medusa_set_videostandard(dev);
  1112. break;
  1113. case SET_PIXEL_FORMAT:
  1114. selected_channel = data_from_user->decoder_select;
  1115. pix_format = data_from_user->pixel_format;
  1116. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1117. selected_channel -= 4;
  1118. selected_channel = selected_channel % 8;
  1119. }
  1120. if (selected_channel >= 0)
  1121. cx25821_set_pixel_format(dev, selected_channel,
  1122. pix_format);
  1123. break;
  1124. case ENABLE_CIF_RESOLUTION:
  1125. selected_channel = data_from_user->decoder_select;
  1126. cif_enable = data_from_user->cif_resolution_enable;
  1127. cif_width = data_from_user->cif_width;
  1128. if (cif_enable) {
  1129. if (dev->tvnorm & V4L2_STD_PAL_BG
  1130. || dev->tvnorm & V4L2_STD_PAL_DK) {
  1131. width = 352;
  1132. } else {
  1133. width = cif_width;
  1134. if (cif_width != 320 && cif_width != 352)
  1135. width = 320;
  1136. }
  1137. }
  1138. if (!(selected_channel <= 7 && selected_channel >= 0)) {
  1139. selected_channel -= 4;
  1140. selected_channel = selected_channel % 8;
  1141. }
  1142. if (selected_channel <= 7 && selected_channel >= 0) {
  1143. dev->channels[selected_channel].use_cif_resolution =
  1144. cif_enable;
  1145. dev->channels[selected_channel].cif_width = width;
  1146. } else {
  1147. for (i = 0; i < VID_CHANNEL_NUM; i++) {
  1148. dev->channels[i].use_cif_resolution =
  1149. cif_enable;
  1150. dev->channels[i].cif_width = width;
  1151. }
  1152. }
  1153. medusa_set_resolution(dev, width, selected_channel);
  1154. break;
  1155. case REG_READ:
  1156. data_from_user->reg_data = cx_read(data_from_user->reg_address);
  1157. break;
  1158. case REG_WRITE:
  1159. cx_write(data_from_user->reg_address, data_from_user->reg_data);
  1160. break;
  1161. case MEDUSA_READ:
  1162. cx25821_i2c_read(&dev->i2c_bus[0],
  1163. (u16) data_from_user->reg_address,
  1164. &data_from_user->reg_data);
  1165. break;
  1166. case MEDUSA_WRITE:
  1167. cx25821_i2c_write(&dev->i2c_bus[0],
  1168. (u16) data_from_user->reg_address,
  1169. data_from_user->reg_data);
  1170. break;
  1171. }
  1172. return 0;
  1173. }
  1174. static long cx25821_video_ioctl(struct file *file,
  1175. unsigned int cmd, unsigned long arg)
  1176. {
  1177. int ret = 0;
  1178. struct cx25821_fh *fh = file->private_data;
  1179. /* check to see if it's the video upstream */
  1180. if (fh->channel_id == SRAM_CH09) {
  1181. ret = video_ioctl_upstream9(file, cmd, arg);
  1182. return ret;
  1183. } else if (fh->channel_id == SRAM_CH10) {
  1184. ret = video_ioctl_upstream10(file, cmd, arg);
  1185. return ret;
  1186. } else if (fh->channel_id == SRAM_CH11) {
  1187. ret = video_ioctl_upstream11(file, cmd, arg);
  1188. ret = video_ioctl_set(file, cmd, arg);
  1189. return ret;
  1190. }
  1191. return video_ioctl2(file, cmd, arg);
  1192. }
  1193. static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
  1194. .s_ctrl = cx25821_s_ctrl,
  1195. };
  1196. static const struct v4l2_file_operations video_fops = {
  1197. .owner = THIS_MODULE,
  1198. .open = video_open,
  1199. .release = video_release,
  1200. .read = video_read,
  1201. .poll = video_poll,
  1202. .mmap = cx25821_video_mmap,
  1203. .ioctl = cx25821_video_ioctl,
  1204. };
  1205. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1206. .vidioc_querycap = cx25821_vidioc_querycap,
  1207. .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
  1208. .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
  1209. .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
  1210. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1211. .vidioc_reqbufs = cx25821_vidioc_reqbufs,
  1212. .vidioc_querybuf = cx25821_vidioc_querybuf,
  1213. .vidioc_qbuf = cx25821_vidioc_qbuf,
  1214. .vidioc_dqbuf = vidioc_dqbuf,
  1215. .vidioc_g_std = cx25821_vidioc_g_std,
  1216. .vidioc_s_std = cx25821_vidioc_s_std,
  1217. .vidioc_enum_input = cx25821_vidioc_enum_input,
  1218. .vidioc_g_input = cx25821_vidioc_g_input,
  1219. .vidioc_s_input = cx25821_vidioc_s_input,
  1220. .vidioc_streamon = vidioc_streamon,
  1221. .vidioc_streamoff = vidioc_streamoff,
  1222. .vidioc_log_status = vidioc_log_status,
  1223. .vidioc_g_priority = cx25821_vidioc_g_priority,
  1224. .vidioc_s_priority = cx25821_vidioc_s_priority,
  1225. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1226. .vidioc_g_register = cx25821_vidioc_g_register,
  1227. .vidioc_s_register = cx25821_vidioc_s_register,
  1228. #endif
  1229. };
  1230. static const struct video_device cx25821_video_device = {
  1231. .name = "cx25821-video",
  1232. .fops = &video_fops,
  1233. .release = video_device_release_empty,
  1234. .minor = -1,
  1235. .ioctl_ops = &video_ioctl_ops,
  1236. .tvnorms = CX25821_NORMS,
  1237. };
  1238. void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
  1239. {
  1240. cx_clear(PCI_INT_MSK, 1);
  1241. if (video_is_registered(&dev->channels[chan_num].vdev)) {
  1242. video_unregister_device(&dev->channels[chan_num].vdev);
  1243. v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
  1244. btcx_riscmem_free(dev->pci,
  1245. &dev->channels[chan_num].vidq.stopper);
  1246. }
  1247. }
  1248. int cx25821_video_register(struct cx25821_dev *dev)
  1249. {
  1250. int err;
  1251. int i;
  1252. spin_lock_init(&dev->slock);
  1253. for (i = 0; i < VID_CHANNEL_NUM; ++i) {
  1254. struct video_device *vdev = &dev->channels[i].vdev;
  1255. struct v4l2_ctrl_handler *hdl = &dev->channels[i].hdl;
  1256. if (i == SRAM_CH08) /* audio channel */
  1257. continue;
  1258. v4l2_ctrl_handler_init(hdl, 4);
  1259. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1260. V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
  1261. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1262. V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
  1263. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1264. V4L2_CID_SATURATION, 0, 10000, 1, 5000);
  1265. v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
  1266. V4L2_CID_HUE, 0, 10000, 1, 5000);
  1267. if (hdl->error) {
  1268. err = hdl->error;
  1269. goto fail_unreg;
  1270. }
  1271. cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
  1272. dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
  1273. dev->channels[i].sram_channels = &cx25821_sram_channels[i];
  1274. dev->channels[i].resources = 0;
  1275. cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
  1276. INIT_LIST_HEAD(&dev->channels[i].vidq.active);
  1277. INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
  1278. dev->channels[i].timeout_data.dev = dev;
  1279. dev->channels[i].timeout_data.channel =
  1280. &cx25821_sram_channels[i];
  1281. dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
  1282. dev->channels[i].vidq.timeout.data =
  1283. (unsigned long)&dev->channels[i].timeout_data;
  1284. init_timer(&dev->channels[i].vidq.timeout);
  1285. /* register v4l devices */
  1286. *vdev = cx25821_video_device;
  1287. vdev->v4l2_dev = &dev->v4l2_dev;
  1288. vdev->ctrl_handler = hdl;
  1289. snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
  1290. video_set_drvdata(vdev, dev);
  1291. err = video_register_device(vdev, VFL_TYPE_GRABBER,
  1292. video_nr[dev->nr]);
  1293. if (err < 0)
  1294. goto fail_unreg;
  1295. }
  1296. /* set PCI interrupt */
  1297. cx_set(PCI_INT_MSK, 0xff);
  1298. /* initial device configuration */
  1299. mutex_lock(&dev->lock);
  1300. dev->tvnorm = V4L2_STD_NTSC_M,
  1301. cx25821_set_tvnorm(dev, dev->tvnorm);
  1302. mutex_unlock(&dev->lock);
  1303. return 0;
  1304. fail_unreg:
  1305. while (i >= 0)
  1306. cx25821_video_unregister(dev, i--);
  1307. return err;
  1308. }