cx88-video.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. /*
  2. *
  3. * device driver for Conexant 2388x based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@kernel.org>
  9. * - Multituner support
  10. * - video_ioctl2 conversion
  11. * - PAL/M fixes
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include "cx88.h"
  24. #include <linux/init.h>
  25. #include <linux/list.h>
  26. #include <linux/module.h>
  27. #include <linux/kmod.h>
  28. #include <linux/kernel.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/delay.h>
  33. #include <linux/kthread.h>
  34. #include <asm/div64.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-ioctl.h>
  37. #include <media/v4l2-event.h>
  38. #include <media/i2c/wm8775.h>
  39. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  40. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  41. MODULE_LICENSE("GPL");
  42. MODULE_VERSION(CX88_VERSION);
  43. /* ------------------------------------------------------------------ */
  44. static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  45. static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  46. static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  47. module_param_array(video_nr, int, NULL, 0444);
  48. module_param_array(vbi_nr, int, NULL, 0444);
  49. module_param_array(radio_nr, int, NULL, 0444);
  50. MODULE_PARM_DESC(video_nr, "video device numbers");
  51. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  52. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  53. static unsigned int video_debug;
  54. module_param(video_debug, int, 0644);
  55. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  56. static unsigned int irq_debug;
  57. module_param(irq_debug, int, 0644);
  58. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  59. #define dprintk(level, fmt, arg...) do { \
  60. if (video_debug >= level) \
  61. printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
  62. __func__, ##arg); \
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. static const struct cx8800_fmt formats[] = {
  67. {
  68. .name = "8 bpp, gray",
  69. .fourcc = V4L2_PIX_FMT_GREY,
  70. .cxformat = ColorFormatY8,
  71. .depth = 8,
  72. .flags = FORMAT_FLAGS_PACKED,
  73. }, {
  74. .name = "15 bpp RGB, le",
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .cxformat = ColorFormatRGB15,
  77. .depth = 16,
  78. .flags = FORMAT_FLAGS_PACKED,
  79. }, {
  80. .name = "15 bpp RGB, be",
  81. .fourcc = V4L2_PIX_FMT_RGB555X,
  82. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  83. .depth = 16,
  84. .flags = FORMAT_FLAGS_PACKED,
  85. }, {
  86. .name = "16 bpp RGB, le",
  87. .fourcc = V4L2_PIX_FMT_RGB565,
  88. .cxformat = ColorFormatRGB16,
  89. .depth = 16,
  90. .flags = FORMAT_FLAGS_PACKED,
  91. }, {
  92. .name = "16 bpp RGB, be",
  93. .fourcc = V4L2_PIX_FMT_RGB565X,
  94. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  95. .depth = 16,
  96. .flags = FORMAT_FLAGS_PACKED,
  97. }, {
  98. .name = "24 bpp RGB, le",
  99. .fourcc = V4L2_PIX_FMT_BGR24,
  100. .cxformat = ColorFormatRGB24,
  101. .depth = 24,
  102. .flags = FORMAT_FLAGS_PACKED,
  103. }, {
  104. .name = "32 bpp RGB, le",
  105. .fourcc = V4L2_PIX_FMT_BGR32,
  106. .cxformat = ColorFormatRGB32,
  107. .depth = 32,
  108. .flags = FORMAT_FLAGS_PACKED,
  109. }, {
  110. .name = "32 bpp RGB, be",
  111. .fourcc = V4L2_PIX_FMT_RGB32,
  112. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP |
  113. ColorFormatWSWAP,
  114. .depth = 32,
  115. .flags = FORMAT_FLAGS_PACKED,
  116. }, {
  117. .name = "4:2:2, packed, YUYV",
  118. .fourcc = V4L2_PIX_FMT_YUYV,
  119. .cxformat = ColorFormatYUY2,
  120. .depth = 16,
  121. .flags = FORMAT_FLAGS_PACKED,
  122. }, {
  123. .name = "4:2:2, packed, UYVY",
  124. .fourcc = V4L2_PIX_FMT_UYVY,
  125. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  126. .depth = 16,
  127. .flags = FORMAT_FLAGS_PACKED,
  128. },
  129. };
  130. static const struct cx8800_fmt *format_by_fourcc(unsigned int fourcc)
  131. {
  132. unsigned int i;
  133. for (i = 0; i < ARRAY_SIZE(formats); i++)
  134. if (formats[i].fourcc == fourcc)
  135. return formats + i;
  136. return NULL;
  137. }
  138. /* ------------------------------------------------------------------- */
  139. struct cx88_ctrl {
  140. /* control information */
  141. u32 id;
  142. s32 minimum;
  143. s32 maximum;
  144. u32 step;
  145. s32 default_value;
  146. /* control register information */
  147. u32 off;
  148. u32 reg;
  149. u32 sreg;
  150. u32 mask;
  151. u32 shift;
  152. };
  153. static const struct cx88_ctrl cx8800_vid_ctls[] = {
  154. /* --- video --- */
  155. {
  156. .id = V4L2_CID_BRIGHTNESS,
  157. .minimum = 0x00,
  158. .maximum = 0xff,
  159. .step = 1,
  160. .default_value = 0x7f,
  161. .off = 128,
  162. .reg = MO_CONTR_BRIGHT,
  163. .mask = 0x00ff,
  164. .shift = 0,
  165. }, {
  166. .id = V4L2_CID_CONTRAST,
  167. .minimum = 0,
  168. .maximum = 0xff,
  169. .step = 1,
  170. .default_value = 0x3f,
  171. .off = 0,
  172. .reg = MO_CONTR_BRIGHT,
  173. .mask = 0xff00,
  174. .shift = 8,
  175. }, {
  176. .id = V4L2_CID_HUE,
  177. .minimum = 0,
  178. .maximum = 0xff,
  179. .step = 1,
  180. .default_value = 0x7f,
  181. .off = 128,
  182. .reg = MO_HUE,
  183. .mask = 0x00ff,
  184. .shift = 0,
  185. }, {
  186. /* strictly, this only describes only U saturation.
  187. * V saturation is handled specially through code.
  188. */
  189. .id = V4L2_CID_SATURATION,
  190. .minimum = 0,
  191. .maximum = 0xff,
  192. .step = 1,
  193. .default_value = 0x7f,
  194. .off = 0,
  195. .reg = MO_UV_SATURATION,
  196. .mask = 0x00ff,
  197. .shift = 0,
  198. }, {
  199. .id = V4L2_CID_SHARPNESS,
  200. .minimum = 0,
  201. .maximum = 4,
  202. .step = 1,
  203. .default_value = 0x0,
  204. .off = 0,
  205. /*
  206. * NOTE: the value is converted and written to both even
  207. * and odd registers in the code
  208. */
  209. .reg = MO_FILTER_ODD,
  210. .mask = 7 << 7,
  211. .shift = 7,
  212. }, {
  213. .id = V4L2_CID_CHROMA_AGC,
  214. .minimum = 0,
  215. .maximum = 1,
  216. .default_value = 0x1,
  217. .reg = MO_INPUT_FORMAT,
  218. .mask = 1 << 10,
  219. .shift = 10,
  220. }, {
  221. .id = V4L2_CID_COLOR_KILLER,
  222. .minimum = 0,
  223. .maximum = 1,
  224. .default_value = 0x1,
  225. .reg = MO_INPUT_FORMAT,
  226. .mask = 1 << 9,
  227. .shift = 9,
  228. }, {
  229. .id = V4L2_CID_BAND_STOP_FILTER,
  230. .minimum = 0,
  231. .maximum = 1,
  232. .step = 1,
  233. .default_value = 0x0,
  234. .off = 0,
  235. .reg = MO_HTOTAL,
  236. .mask = 3 << 11,
  237. .shift = 11,
  238. }
  239. };
  240. static const struct cx88_ctrl cx8800_aud_ctls[] = {
  241. {
  242. /* --- audio --- */
  243. .id = V4L2_CID_AUDIO_MUTE,
  244. .minimum = 0,
  245. .maximum = 1,
  246. .default_value = 1,
  247. .reg = AUD_VOL_CTL,
  248. .sreg = SHADOW_AUD_VOL_CTL,
  249. .mask = (1 << 6),
  250. .shift = 6,
  251. }, {
  252. .id = V4L2_CID_AUDIO_VOLUME,
  253. .minimum = 0,
  254. .maximum = 0x3f,
  255. .step = 1,
  256. .default_value = 0x3f,
  257. .reg = AUD_VOL_CTL,
  258. .sreg = SHADOW_AUD_VOL_CTL,
  259. .mask = 0x3f,
  260. .shift = 0,
  261. }, {
  262. .id = V4L2_CID_AUDIO_BALANCE,
  263. .minimum = 0,
  264. .maximum = 0x7f,
  265. .step = 1,
  266. .default_value = 0x40,
  267. .reg = AUD_BAL_CTL,
  268. .sreg = SHADOW_AUD_BAL_CTL,
  269. .mask = 0x7f,
  270. .shift = 0,
  271. }
  272. };
  273. enum {
  274. CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
  275. CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
  276. };
  277. /* ------------------------------------------------------------------ */
  278. int cx88_video_mux(struct cx88_core *core, unsigned int input)
  279. {
  280. /* struct cx88_core *core = dev->core; */
  281. dprintk(1, "video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  282. input, INPUT(input).vmux,
  283. INPUT(input).gpio0, INPUT(input).gpio1,
  284. INPUT(input).gpio2, INPUT(input).gpio3);
  285. core->input = input;
  286. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
  287. cx_write(MO_GP3_IO, INPUT(input).gpio3);
  288. cx_write(MO_GP0_IO, INPUT(input).gpio0);
  289. cx_write(MO_GP1_IO, INPUT(input).gpio1);
  290. cx_write(MO_GP2_IO, INPUT(input).gpio2);
  291. switch (INPUT(input).type) {
  292. case CX88_VMUX_SVIDEO:
  293. cx_set(MO_AFECFG_IO, 0x00000001);
  294. cx_set(MO_INPUT_FORMAT, 0x00010010);
  295. cx_set(MO_FILTER_EVEN, 0x00002020);
  296. cx_set(MO_FILTER_ODD, 0x00002020);
  297. break;
  298. default:
  299. cx_clear(MO_AFECFG_IO, 0x00000001);
  300. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  301. cx_clear(MO_FILTER_EVEN, 0x00002020);
  302. cx_clear(MO_FILTER_ODD, 0x00002020);
  303. break;
  304. }
  305. /*
  306. * if there are audioroutes defined, we have an external
  307. * ADC to deal with audio
  308. */
  309. if (INPUT(input).audioroute) {
  310. /*
  311. * The wm8775 module has the "2" route hardwired into
  312. * the initialization. Some boards may use different
  313. * routes for different inputs. HVR-1300 surely does
  314. */
  315. if (core->sd_wm8775) {
  316. call_all(core, audio, s_routing,
  317. INPUT(input).audioroute, 0, 0);
  318. }
  319. /*
  320. * cx2388's C-ADC is connected to the tuner only.
  321. * When used with S-Video, that ADC is busy dealing with
  322. * chroma, so an external must be used for baseband audio
  323. */
  324. if (INPUT(input).type != CX88_VMUX_TELEVISION &&
  325. INPUT(input).type != CX88_VMUX_CABLE) {
  326. /* "I2S ADC mode" */
  327. core->tvaudio = WW_I2SADC;
  328. cx88_set_tvaudio(core);
  329. } else {
  330. /* Normal mode */
  331. cx_write(AUD_I2SCNTL, 0x0);
  332. cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
  333. }
  334. }
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(cx88_video_mux);
  338. /* ------------------------------------------------------------------ */
  339. static int start_video_dma(struct cx8800_dev *dev,
  340. struct cx88_dmaqueue *q,
  341. struct cx88_buffer *buf)
  342. {
  343. struct cx88_core *core = dev->core;
  344. /* setup fifo + format */
  345. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  346. buf->bpl, buf->risc.dma);
  347. cx88_set_scale(core, core->width, core->height, core->field);
  348. cx_write(MO_COLOR_CTRL, dev->fmt->cxformat | ColorFormatGamma);
  349. /* reset counter */
  350. cx_write(MO_VIDY_GPCNTRL, GP_COUNT_CONTROL_RESET);
  351. q->count = 0;
  352. /* enable irqs */
  353. cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
  354. /*
  355. * Enables corresponding bits at PCI_INT_STAT:
  356. * bits 0 to 4: video, audio, transport stream, VIP, Host
  357. * bit 7: timer
  358. * bits 8 and 9: DMA complete for: SRC, DST
  359. * bits 10 and 11: BERR signal asserted for RISC: RD, WR
  360. * bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  361. */
  362. cx_set(MO_VID_INTMSK, 0x0f0011);
  363. /* enable capture */
  364. cx_set(VID_CAPTURE_CONTROL, 0x06);
  365. /* start dma */
  366. cx_set(MO_DEV_CNTRL2, (1 << 5));
  367. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  368. return 0;
  369. }
  370. #ifdef CONFIG_PM
  371. static int stop_video_dma(struct cx8800_dev *dev)
  372. {
  373. struct cx88_core *core = dev->core;
  374. /* stop dma */
  375. cx_clear(MO_VID_DMACNTRL, 0x11);
  376. /* disable capture */
  377. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  378. /* disable irqs */
  379. cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
  380. cx_clear(MO_VID_INTMSK, 0x0f0011);
  381. return 0;
  382. }
  383. static int restart_video_queue(struct cx8800_dev *dev,
  384. struct cx88_dmaqueue *q)
  385. {
  386. struct cx88_buffer *buf;
  387. if (!list_empty(&q->active)) {
  388. buf = list_entry(q->active.next, struct cx88_buffer, list);
  389. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  390. buf, buf->vb.vb2_buf.index);
  391. start_video_dma(dev, q, buf);
  392. }
  393. return 0;
  394. }
  395. #endif
  396. /* ------------------------------------------------------------------ */
  397. static int queue_setup(struct vb2_queue *q,
  398. unsigned int *num_buffers, unsigned int *num_planes,
  399. unsigned int sizes[], struct device *alloc_devs[])
  400. {
  401. struct cx8800_dev *dev = q->drv_priv;
  402. struct cx88_core *core = dev->core;
  403. *num_planes = 1;
  404. sizes[0] = (dev->fmt->depth * core->width * core->height) >> 3;
  405. return 0;
  406. }
  407. static int buffer_prepare(struct vb2_buffer *vb)
  408. {
  409. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  410. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  411. struct cx88_core *core = dev->core;
  412. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  413. struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
  414. buf->bpl = core->width * dev->fmt->depth >> 3;
  415. if (vb2_plane_size(vb, 0) < core->height * buf->bpl)
  416. return -EINVAL;
  417. vb2_set_plane_payload(vb, 0, core->height * buf->bpl);
  418. switch (core->field) {
  419. case V4L2_FIELD_TOP:
  420. cx88_risc_buffer(dev->pci, &buf->risc,
  421. sgt->sgl, 0, UNSET,
  422. buf->bpl, 0, core->height);
  423. break;
  424. case V4L2_FIELD_BOTTOM:
  425. cx88_risc_buffer(dev->pci, &buf->risc,
  426. sgt->sgl, UNSET, 0,
  427. buf->bpl, 0, core->height);
  428. break;
  429. case V4L2_FIELD_SEQ_TB:
  430. cx88_risc_buffer(dev->pci, &buf->risc,
  431. sgt->sgl,
  432. 0, buf->bpl * (core->height >> 1),
  433. buf->bpl, 0,
  434. core->height >> 1);
  435. break;
  436. case V4L2_FIELD_SEQ_BT:
  437. cx88_risc_buffer(dev->pci, &buf->risc,
  438. sgt->sgl,
  439. buf->bpl * (core->height >> 1), 0,
  440. buf->bpl, 0,
  441. core->height >> 1);
  442. break;
  443. case V4L2_FIELD_INTERLACED:
  444. default:
  445. cx88_risc_buffer(dev->pci, &buf->risc,
  446. sgt->sgl, 0, buf->bpl,
  447. buf->bpl, buf->bpl,
  448. core->height >> 1);
  449. break;
  450. }
  451. dprintk(2,
  452. "[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  453. buf, buf->vb.vb2_buf.index,
  454. core->width, core->height, dev->fmt->depth, dev->fmt->name,
  455. (unsigned long)buf->risc.dma);
  456. return 0;
  457. }
  458. static void buffer_finish(struct vb2_buffer *vb)
  459. {
  460. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  461. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  462. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  463. struct cx88_riscmem *risc = &buf->risc;
  464. if (risc->cpu)
  465. pci_free_consistent(dev->pci, risc->size, risc->cpu, risc->dma);
  466. memset(risc, 0, sizeof(*risc));
  467. }
  468. static void buffer_queue(struct vb2_buffer *vb)
  469. {
  470. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  471. struct cx8800_dev *dev = vb->vb2_queue->drv_priv;
  472. struct cx88_buffer *buf = container_of(vbuf, struct cx88_buffer, vb);
  473. struct cx88_buffer *prev;
  474. struct cx88_dmaqueue *q = &dev->vidq;
  475. /* add jump to start */
  476. buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 8);
  477. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
  478. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 8);
  479. if (list_empty(&q->active)) {
  480. list_add_tail(&buf->list, &q->active);
  481. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  482. buf, buf->vb.vb2_buf.index);
  483. } else {
  484. buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
  485. prev = list_entry(q->active.prev, struct cx88_buffer, list);
  486. list_add_tail(&buf->list, &q->active);
  487. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  488. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  489. buf, buf->vb.vb2_buf.index);
  490. }
  491. }
  492. static int start_streaming(struct vb2_queue *q, unsigned int count)
  493. {
  494. struct cx8800_dev *dev = q->drv_priv;
  495. struct cx88_dmaqueue *dmaq = &dev->vidq;
  496. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  497. struct cx88_buffer, list);
  498. start_video_dma(dev, dmaq, buf);
  499. return 0;
  500. }
  501. static void stop_streaming(struct vb2_queue *q)
  502. {
  503. struct cx8800_dev *dev = q->drv_priv;
  504. struct cx88_core *core = dev->core;
  505. struct cx88_dmaqueue *dmaq = &dev->vidq;
  506. unsigned long flags;
  507. cx_clear(MO_VID_DMACNTRL, 0x11);
  508. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  509. spin_lock_irqsave(&dev->slock, flags);
  510. while (!list_empty(&dmaq->active)) {
  511. struct cx88_buffer *buf = list_entry(dmaq->active.next,
  512. struct cx88_buffer, list);
  513. list_del(&buf->list);
  514. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  515. }
  516. spin_unlock_irqrestore(&dev->slock, flags);
  517. }
  518. static const struct vb2_ops cx8800_video_qops = {
  519. .queue_setup = queue_setup,
  520. .buf_prepare = buffer_prepare,
  521. .buf_finish = buffer_finish,
  522. .buf_queue = buffer_queue,
  523. .wait_prepare = vb2_ops_wait_prepare,
  524. .wait_finish = vb2_ops_wait_finish,
  525. .start_streaming = start_streaming,
  526. .stop_streaming = stop_streaming,
  527. };
  528. /* ------------------------------------------------------------------ */
  529. static int radio_open(struct file *file)
  530. {
  531. struct cx8800_dev *dev = video_drvdata(file);
  532. struct cx88_core *core = dev->core;
  533. int ret = v4l2_fh_open(file);
  534. if (ret)
  535. return ret;
  536. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  537. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  538. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  539. cx_write(MO_GP2_IO, core->board.radio.gpio2);
  540. if (core->board.radio.audioroute) {
  541. if (core->sd_wm8775) {
  542. call_all(core, audio, s_routing,
  543. core->board.radio.audioroute, 0, 0);
  544. }
  545. /* "I2S ADC mode" */
  546. core->tvaudio = WW_I2SADC;
  547. cx88_set_tvaudio(core);
  548. } else {
  549. /* FM Mode */
  550. core->tvaudio = WW_FM;
  551. cx88_set_tvaudio(core);
  552. cx88_set_stereo(core, V4L2_TUNER_MODE_STEREO, 1);
  553. }
  554. call_all(core, tuner, s_radio);
  555. return 0;
  556. }
  557. /* ------------------------------------------------------------------ */
  558. /* VIDEO CTRL IOCTLS */
  559. static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
  560. {
  561. struct cx88_core *core =
  562. container_of(ctrl->handler, struct cx88_core, video_hdl);
  563. const struct cx88_ctrl *cc = ctrl->priv;
  564. u32 value, mask;
  565. mask = cc->mask;
  566. switch (ctrl->id) {
  567. case V4L2_CID_SATURATION:
  568. /* special v_sat handling */
  569. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  570. if (core->tvnorm & V4L2_STD_SECAM) {
  571. /* For SECAM, both U and V sat should be equal */
  572. value = value << 8 | value;
  573. } else {
  574. /* Keeps U Saturation proportional to V Sat */
  575. value = (value * 0x5a) / 0x7f << 8 | value;
  576. }
  577. mask = 0xffff;
  578. break;
  579. case V4L2_CID_SHARPNESS:
  580. /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
  581. value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
  582. /* needs to be set for both fields */
  583. cx_andor(MO_FILTER_EVEN, mask, value);
  584. break;
  585. case V4L2_CID_CHROMA_AGC:
  586. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  587. break;
  588. default:
  589. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  590. break;
  591. }
  592. dprintk(1,
  593. "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  594. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  595. mask, cc->sreg ? " [shadowed]" : "");
  596. if (cc->sreg)
  597. cx_sandor(cc->sreg, cc->reg, mask, value);
  598. else
  599. cx_andor(cc->reg, mask, value);
  600. return 0;
  601. }
  602. static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
  603. {
  604. struct cx88_core *core =
  605. container_of(ctrl->handler, struct cx88_core, audio_hdl);
  606. const struct cx88_ctrl *cc = ctrl->priv;
  607. u32 value, mask;
  608. /* Pass changes onto any WM8775 */
  609. if (core->sd_wm8775) {
  610. switch (ctrl->id) {
  611. case V4L2_CID_AUDIO_MUTE:
  612. wm8775_s_ctrl(core, ctrl->id, ctrl->val);
  613. break;
  614. case V4L2_CID_AUDIO_VOLUME:
  615. wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
  616. (0x90 + ctrl->val) << 8 : 0);
  617. break;
  618. case V4L2_CID_AUDIO_BALANCE:
  619. wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
  620. break;
  621. default:
  622. break;
  623. }
  624. }
  625. mask = cc->mask;
  626. switch (ctrl->id) {
  627. case V4L2_CID_AUDIO_BALANCE:
  628. value = (ctrl->val < 0x40) ?
  629. (0x7f - ctrl->val) : (ctrl->val - 0x40);
  630. break;
  631. case V4L2_CID_AUDIO_VOLUME:
  632. value = 0x3f - (ctrl->val & 0x3f);
  633. break;
  634. default:
  635. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  636. break;
  637. }
  638. dprintk(1,
  639. "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  640. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  641. mask, cc->sreg ? " [shadowed]" : "");
  642. if (cc->sreg)
  643. cx_sandor(cc->sreg, cc->reg, mask, value);
  644. else
  645. cx_andor(cc->reg, mask, value);
  646. return 0;
  647. }
  648. /* ------------------------------------------------------------------ */
  649. /* VIDEO IOCTLS */
  650. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  651. struct v4l2_format *f)
  652. {
  653. struct cx8800_dev *dev = video_drvdata(file);
  654. struct cx88_core *core = dev->core;
  655. f->fmt.pix.width = core->width;
  656. f->fmt.pix.height = core->height;
  657. f->fmt.pix.field = core->field;
  658. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  659. f->fmt.pix.bytesperline =
  660. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  661. f->fmt.pix.sizeimage =
  662. f->fmt.pix.height * f->fmt.pix.bytesperline;
  663. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  664. return 0;
  665. }
  666. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  667. struct v4l2_format *f)
  668. {
  669. struct cx8800_dev *dev = video_drvdata(file);
  670. struct cx88_core *core = dev->core;
  671. const struct cx8800_fmt *fmt;
  672. enum v4l2_field field;
  673. unsigned int maxw, maxh;
  674. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  675. if (!fmt)
  676. return -EINVAL;
  677. maxw = norm_maxw(core->tvnorm);
  678. maxh = norm_maxh(core->tvnorm);
  679. field = f->fmt.pix.field;
  680. switch (field) {
  681. case V4L2_FIELD_TOP:
  682. case V4L2_FIELD_BOTTOM:
  683. case V4L2_FIELD_INTERLACED:
  684. case V4L2_FIELD_SEQ_BT:
  685. case V4L2_FIELD_SEQ_TB:
  686. break;
  687. default:
  688. field = (f->fmt.pix.height > maxh / 2)
  689. ? V4L2_FIELD_INTERLACED
  690. : V4L2_FIELD_BOTTOM;
  691. break;
  692. }
  693. if (V4L2_FIELD_HAS_T_OR_B(field))
  694. maxh /= 2;
  695. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  696. &f->fmt.pix.height, 32, maxh, 0, 0);
  697. f->fmt.pix.field = field;
  698. f->fmt.pix.bytesperline =
  699. (f->fmt.pix.width * fmt->depth) >> 3;
  700. f->fmt.pix.sizeimage =
  701. f->fmt.pix.height * f->fmt.pix.bytesperline;
  702. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  703. return 0;
  704. }
  705. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  706. struct v4l2_format *f)
  707. {
  708. struct cx8800_dev *dev = video_drvdata(file);
  709. struct cx88_core *core = dev->core;
  710. int err = vidioc_try_fmt_vid_cap(file, priv, f);
  711. if (err != 0)
  712. return err;
  713. if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq))
  714. return -EBUSY;
  715. if (core->dvbdev && vb2_is_busy(&core->dvbdev->vb2_mpegq))
  716. return -EBUSY;
  717. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  718. core->width = f->fmt.pix.width;
  719. core->height = f->fmt.pix.height;
  720. core->field = f->fmt.pix.field;
  721. return 0;
  722. }
  723. int cx88_querycap(struct file *file, struct cx88_core *core,
  724. struct v4l2_capability *cap)
  725. {
  726. struct video_device *vdev = video_devdata(file);
  727. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  728. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  729. if (core->board.tuner_type != UNSET)
  730. cap->device_caps |= V4L2_CAP_TUNER;
  731. switch (vdev->vfl_type) {
  732. case VFL_TYPE_RADIO:
  733. cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  734. break;
  735. case VFL_TYPE_GRABBER:
  736. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  737. break;
  738. case VFL_TYPE_VBI:
  739. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  740. break;
  741. default:
  742. return -EINVAL;
  743. }
  744. cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
  745. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  746. if (core->board.radio.type == CX88_RADIO)
  747. cap->capabilities |= V4L2_CAP_RADIO;
  748. return 0;
  749. }
  750. EXPORT_SYMBOL(cx88_querycap);
  751. static int vidioc_querycap(struct file *file, void *priv,
  752. struct v4l2_capability *cap)
  753. {
  754. struct cx8800_dev *dev = video_drvdata(file);
  755. struct cx88_core *core = dev->core;
  756. strcpy(cap->driver, "cx8800");
  757. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  758. return cx88_querycap(file, core, cap);
  759. }
  760. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  761. struct v4l2_fmtdesc *f)
  762. {
  763. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  764. return -EINVAL;
  765. strlcpy(f->description, formats[f->index].name, sizeof(f->description));
  766. f->pixelformat = formats[f->index].fourcc;
  767. return 0;
  768. }
  769. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
  770. {
  771. struct cx8800_dev *dev = video_drvdata(file);
  772. struct cx88_core *core = dev->core;
  773. *tvnorm = core->tvnorm;
  774. return 0;
  775. }
  776. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  777. {
  778. struct cx8800_dev *dev = video_drvdata(file);
  779. struct cx88_core *core = dev->core;
  780. return cx88_set_tvnorm(core, tvnorms);
  781. }
  782. /* only one input in this sample driver */
  783. int cx88_enum_input(struct cx88_core *core, struct v4l2_input *i)
  784. {
  785. static const char * const iname[] = {
  786. [CX88_VMUX_COMPOSITE1] = "Composite1",
  787. [CX88_VMUX_COMPOSITE2] = "Composite2",
  788. [CX88_VMUX_COMPOSITE3] = "Composite3",
  789. [CX88_VMUX_COMPOSITE4] = "Composite4",
  790. [CX88_VMUX_SVIDEO] = "S-Video",
  791. [CX88_VMUX_TELEVISION] = "Television",
  792. [CX88_VMUX_CABLE] = "Cable TV",
  793. [CX88_VMUX_DVB] = "DVB",
  794. [CX88_VMUX_DEBUG] = "for debug only",
  795. };
  796. unsigned int n = i->index;
  797. if (n >= 4)
  798. return -EINVAL;
  799. if (!INPUT(n).type)
  800. return -EINVAL;
  801. i->type = V4L2_INPUT_TYPE_CAMERA;
  802. strcpy(i->name, iname[INPUT(n).type]);
  803. if ((INPUT(n).type == CX88_VMUX_TELEVISION) ||
  804. (INPUT(n).type == CX88_VMUX_CABLE))
  805. i->type = V4L2_INPUT_TYPE_TUNER;
  806. i->std = CX88_NORMS;
  807. return 0;
  808. }
  809. EXPORT_SYMBOL(cx88_enum_input);
  810. static int vidioc_enum_input(struct file *file, void *priv,
  811. struct v4l2_input *i)
  812. {
  813. struct cx8800_dev *dev = video_drvdata(file);
  814. struct cx88_core *core = dev->core;
  815. return cx88_enum_input(core, i);
  816. }
  817. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  818. {
  819. struct cx8800_dev *dev = video_drvdata(file);
  820. struct cx88_core *core = dev->core;
  821. *i = core->input;
  822. return 0;
  823. }
  824. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  825. {
  826. struct cx8800_dev *dev = video_drvdata(file);
  827. struct cx88_core *core = dev->core;
  828. if (i >= 4)
  829. return -EINVAL;
  830. if (!INPUT(i).type)
  831. return -EINVAL;
  832. cx88_newstation(core);
  833. cx88_video_mux(core, i);
  834. return 0;
  835. }
  836. static int vidioc_g_tuner(struct file *file, void *priv,
  837. struct v4l2_tuner *t)
  838. {
  839. struct cx8800_dev *dev = video_drvdata(file);
  840. struct cx88_core *core = dev->core;
  841. u32 reg;
  842. if (unlikely(core->board.tuner_type == UNSET))
  843. return -EINVAL;
  844. if (t->index != 0)
  845. return -EINVAL;
  846. strcpy(t->name, "Television");
  847. t->capability = V4L2_TUNER_CAP_NORM;
  848. t->rangehigh = 0xffffffffUL;
  849. call_all(core, tuner, g_tuner, t);
  850. cx88_get_stereo(core, t);
  851. reg = cx_read(MO_DEVICE_STATUS);
  852. t->signal = (reg & (1 << 5)) ? 0xffff : 0x0000;
  853. return 0;
  854. }
  855. static int vidioc_s_tuner(struct file *file, void *priv,
  856. const struct v4l2_tuner *t)
  857. {
  858. struct cx8800_dev *dev = video_drvdata(file);
  859. struct cx88_core *core = dev->core;
  860. if (core->board.tuner_type == UNSET)
  861. return -EINVAL;
  862. if (t->index != 0)
  863. return -EINVAL;
  864. cx88_set_stereo(core, t->audmode, 1);
  865. return 0;
  866. }
  867. static int vidioc_g_frequency(struct file *file, void *priv,
  868. struct v4l2_frequency *f)
  869. {
  870. struct cx8800_dev *dev = video_drvdata(file);
  871. struct cx88_core *core = dev->core;
  872. if (unlikely(core->board.tuner_type == UNSET))
  873. return -EINVAL;
  874. if (f->tuner)
  875. return -EINVAL;
  876. f->frequency = core->freq;
  877. call_all(core, tuner, g_frequency, f);
  878. return 0;
  879. }
  880. int cx88_set_freq(struct cx88_core *core,
  881. const struct v4l2_frequency *f)
  882. {
  883. struct v4l2_frequency new_freq = *f;
  884. if (unlikely(core->board.tuner_type == UNSET))
  885. return -EINVAL;
  886. if (unlikely(f->tuner != 0))
  887. return -EINVAL;
  888. cx88_newstation(core);
  889. call_all(core, tuner, s_frequency, f);
  890. call_all(core, tuner, g_frequency, &new_freq);
  891. core->freq = new_freq.frequency;
  892. /* When changing channels it is required to reset TVAUDIO */
  893. usleep_range(10000, 20000);
  894. cx88_set_tvaudio(core);
  895. return 0;
  896. }
  897. EXPORT_SYMBOL(cx88_set_freq);
  898. static int vidioc_s_frequency(struct file *file, void *priv,
  899. const struct v4l2_frequency *f)
  900. {
  901. struct cx8800_dev *dev = video_drvdata(file);
  902. struct cx88_core *core = dev->core;
  903. return cx88_set_freq(core, f);
  904. }
  905. #ifdef CONFIG_VIDEO_ADV_DEBUG
  906. static int vidioc_g_register(struct file *file, void *fh,
  907. struct v4l2_dbg_register *reg)
  908. {
  909. struct cx8800_dev *dev = video_drvdata(file);
  910. struct cx88_core *core = dev->core;
  911. /* cx2388x has a 24-bit register space */
  912. reg->val = cx_read(reg->reg & 0xfffffc);
  913. reg->size = 4;
  914. return 0;
  915. }
  916. static int vidioc_s_register(struct file *file, void *fh,
  917. const struct v4l2_dbg_register *reg)
  918. {
  919. struct cx8800_dev *dev = video_drvdata(file);
  920. struct cx88_core *core = dev->core;
  921. cx_write(reg->reg & 0xfffffc, reg->val);
  922. return 0;
  923. }
  924. #endif
  925. /* ----------------------------------------------------------- */
  926. /* RADIO ESPECIFIC IOCTLS */
  927. /* ----------------------------------------------------------- */
  928. static int radio_g_tuner(struct file *file, void *priv,
  929. struct v4l2_tuner *t)
  930. {
  931. struct cx8800_dev *dev = video_drvdata(file);
  932. struct cx88_core *core = dev->core;
  933. if (unlikely(t->index > 0))
  934. return -EINVAL;
  935. strcpy(t->name, "Radio");
  936. call_all(core, tuner, g_tuner, t);
  937. return 0;
  938. }
  939. static int radio_s_tuner(struct file *file, void *priv,
  940. const struct v4l2_tuner *t)
  941. {
  942. struct cx8800_dev *dev = video_drvdata(file);
  943. struct cx88_core *core = dev->core;
  944. if (t->index != 0)
  945. return -EINVAL;
  946. call_all(core, tuner, s_tuner, t);
  947. return 0;
  948. }
  949. /* ----------------------------------------------------------- */
  950. static const char *cx88_vid_irqs[32] = {
  951. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  952. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  953. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  954. "y_sync", "u_sync", "v_sync", "vbi_sync",
  955. "opc_err", "par_err", "rip_err", "pci_abort",
  956. };
  957. static void cx8800_vid_irq(struct cx8800_dev *dev)
  958. {
  959. struct cx88_core *core = dev->core;
  960. u32 status, mask, count;
  961. status = cx_read(MO_VID_INTSTAT);
  962. mask = cx_read(MO_VID_INTMSK);
  963. if (0 == (status & mask))
  964. return;
  965. cx_write(MO_VID_INTSTAT, status);
  966. if (irq_debug || (status & mask & ~0xff))
  967. cx88_print_irqbits("irq vid",
  968. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  969. status, mask);
  970. /* risc op code error */
  971. if (status & (1 << 16)) {
  972. pr_warn("video risc op code error\n");
  973. cx_clear(MO_VID_DMACNTRL, 0x11);
  974. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  975. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  976. }
  977. /* risc1 y */
  978. if (status & 0x01) {
  979. spin_lock(&dev->slock);
  980. count = cx_read(MO_VIDY_GPCNT);
  981. cx88_wakeup(core, &dev->vidq, count);
  982. spin_unlock(&dev->slock);
  983. }
  984. /* risc1 vbi */
  985. if (status & 0x08) {
  986. spin_lock(&dev->slock);
  987. count = cx_read(MO_VBI_GPCNT);
  988. cx88_wakeup(core, &dev->vbiq, count);
  989. spin_unlock(&dev->slock);
  990. }
  991. }
  992. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  993. {
  994. struct cx8800_dev *dev = dev_id;
  995. struct cx88_core *core = dev->core;
  996. u32 status;
  997. int loop, handled = 0;
  998. for (loop = 0; loop < 10; loop++) {
  999. status = cx_read(MO_PCI_INTSTAT) &
  1000. (core->pci_irqmask | PCI_INT_VIDINT);
  1001. if (status == 0)
  1002. goto out;
  1003. cx_write(MO_PCI_INTSTAT, status);
  1004. handled = 1;
  1005. if (status & core->pci_irqmask)
  1006. cx88_core_irq(core, status);
  1007. if (status & PCI_INT_VIDINT)
  1008. cx8800_vid_irq(dev);
  1009. }
  1010. if (loop == 10) {
  1011. pr_warn("irq loop -- clearing mask\n");
  1012. cx_write(MO_PCI_INTMSK, 0);
  1013. }
  1014. out:
  1015. return IRQ_RETVAL(handled);
  1016. }
  1017. /* ----------------------------------------------------------- */
  1018. /* exported stuff */
  1019. static const struct v4l2_file_operations video_fops = {
  1020. .owner = THIS_MODULE,
  1021. .open = v4l2_fh_open,
  1022. .release = vb2_fop_release,
  1023. .read = vb2_fop_read,
  1024. .poll = vb2_fop_poll,
  1025. .mmap = vb2_fop_mmap,
  1026. .unlocked_ioctl = video_ioctl2,
  1027. };
  1028. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1029. .vidioc_querycap = vidioc_querycap,
  1030. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1031. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1032. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1033. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1034. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1035. .vidioc_querybuf = vb2_ioctl_querybuf,
  1036. .vidioc_qbuf = vb2_ioctl_qbuf,
  1037. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1038. .vidioc_g_std = vidioc_g_std,
  1039. .vidioc_s_std = vidioc_s_std,
  1040. .vidioc_enum_input = vidioc_enum_input,
  1041. .vidioc_g_input = vidioc_g_input,
  1042. .vidioc_s_input = vidioc_s_input,
  1043. .vidioc_streamon = vb2_ioctl_streamon,
  1044. .vidioc_streamoff = vb2_ioctl_streamoff,
  1045. .vidioc_g_tuner = vidioc_g_tuner,
  1046. .vidioc_s_tuner = vidioc_s_tuner,
  1047. .vidioc_g_frequency = vidioc_g_frequency,
  1048. .vidioc_s_frequency = vidioc_s_frequency,
  1049. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1050. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1051. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1052. .vidioc_g_register = vidioc_g_register,
  1053. .vidioc_s_register = vidioc_s_register,
  1054. #endif
  1055. };
  1056. static const struct video_device cx8800_video_template = {
  1057. .name = "cx8800-video",
  1058. .fops = &video_fops,
  1059. .ioctl_ops = &video_ioctl_ops,
  1060. .tvnorms = CX88_NORMS,
  1061. };
  1062. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1063. .vidioc_querycap = vidioc_querycap,
  1064. .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
  1065. .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
  1066. .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
  1067. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1068. .vidioc_querybuf = vb2_ioctl_querybuf,
  1069. .vidioc_qbuf = vb2_ioctl_qbuf,
  1070. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1071. .vidioc_g_std = vidioc_g_std,
  1072. .vidioc_s_std = vidioc_s_std,
  1073. .vidioc_enum_input = vidioc_enum_input,
  1074. .vidioc_g_input = vidioc_g_input,
  1075. .vidioc_s_input = vidioc_s_input,
  1076. .vidioc_streamon = vb2_ioctl_streamon,
  1077. .vidioc_streamoff = vb2_ioctl_streamoff,
  1078. .vidioc_g_tuner = vidioc_g_tuner,
  1079. .vidioc_s_tuner = vidioc_s_tuner,
  1080. .vidioc_g_frequency = vidioc_g_frequency,
  1081. .vidioc_s_frequency = vidioc_s_frequency,
  1082. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1083. .vidioc_g_register = vidioc_g_register,
  1084. .vidioc_s_register = vidioc_s_register,
  1085. #endif
  1086. };
  1087. static const struct video_device cx8800_vbi_template = {
  1088. .name = "cx8800-vbi",
  1089. .fops = &video_fops,
  1090. .ioctl_ops = &vbi_ioctl_ops,
  1091. .tvnorms = CX88_NORMS,
  1092. };
  1093. static const struct v4l2_file_operations radio_fops = {
  1094. .owner = THIS_MODULE,
  1095. .open = radio_open,
  1096. .poll = v4l2_ctrl_poll,
  1097. .release = v4l2_fh_release,
  1098. .unlocked_ioctl = video_ioctl2,
  1099. };
  1100. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1101. .vidioc_querycap = vidioc_querycap,
  1102. .vidioc_g_tuner = radio_g_tuner,
  1103. .vidioc_s_tuner = radio_s_tuner,
  1104. .vidioc_g_frequency = vidioc_g_frequency,
  1105. .vidioc_s_frequency = vidioc_s_frequency,
  1106. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1107. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1108. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1109. .vidioc_g_register = vidioc_g_register,
  1110. .vidioc_s_register = vidioc_s_register,
  1111. #endif
  1112. };
  1113. static const struct video_device cx8800_radio_template = {
  1114. .name = "cx8800-radio",
  1115. .fops = &radio_fops,
  1116. .ioctl_ops = &radio_ioctl_ops,
  1117. };
  1118. static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
  1119. .s_ctrl = cx8800_s_vid_ctrl,
  1120. };
  1121. static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
  1122. .s_ctrl = cx8800_s_aud_ctrl,
  1123. };
  1124. /* ----------------------------------------------------------- */
  1125. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1126. {
  1127. video_unregister_device(&dev->radio_dev);
  1128. video_unregister_device(&dev->vbi_dev);
  1129. video_unregister_device(&dev->video_dev);
  1130. }
  1131. static int cx8800_initdev(struct pci_dev *pci_dev,
  1132. const struct pci_device_id *pci_id)
  1133. {
  1134. struct cx8800_dev *dev;
  1135. struct cx88_core *core;
  1136. struct vb2_queue *q;
  1137. int err;
  1138. int i;
  1139. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1140. if (!dev)
  1141. return -ENOMEM;
  1142. /* pci init */
  1143. dev->pci = pci_dev;
  1144. if (pci_enable_device(pci_dev)) {
  1145. err = -EIO;
  1146. goto fail_free;
  1147. }
  1148. core = cx88_core_get(dev->pci);
  1149. if (!core) {
  1150. err = -EINVAL;
  1151. goto fail_disable;
  1152. }
  1153. dev->core = core;
  1154. /* print pci info */
  1155. dev->pci_rev = pci_dev->revision;
  1156. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1157. pr_info("found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
  1158. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1159. dev->pci_lat,
  1160. (unsigned long long)pci_resource_start(pci_dev, 0));
  1161. pci_set_master(pci_dev);
  1162. err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  1163. if (err) {
  1164. pr_err("Oops: no 32bit PCI DMA ???\n");
  1165. goto fail_core;
  1166. }
  1167. /* initialize driver struct */
  1168. spin_lock_init(&dev->slock);
  1169. /* init video dma queues */
  1170. INIT_LIST_HEAD(&dev->vidq.active);
  1171. /* init vbi dma queues */
  1172. INIT_LIST_HEAD(&dev->vbiq.active);
  1173. /* get irq */
  1174. err = request_irq(pci_dev->irq, cx8800_irq,
  1175. IRQF_SHARED, core->name, dev);
  1176. if (err < 0) {
  1177. pr_err("can't get IRQ %d\n", pci_dev->irq);
  1178. goto fail_core;
  1179. }
  1180. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1181. for (i = 0; i < CX8800_AUD_CTLS; i++) {
  1182. const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
  1183. struct v4l2_ctrl *vc;
  1184. vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
  1185. cc->id, cc->minimum, cc->maximum,
  1186. cc->step, cc->default_value);
  1187. if (!vc) {
  1188. err = core->audio_hdl.error;
  1189. goto fail_irq;
  1190. }
  1191. vc->priv = (void *)cc;
  1192. }
  1193. for (i = 0; i < CX8800_VID_CTLS; i++) {
  1194. const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
  1195. struct v4l2_ctrl *vc;
  1196. vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
  1197. cc->id, cc->minimum, cc->maximum,
  1198. cc->step, cc->default_value);
  1199. if (!vc) {
  1200. err = core->video_hdl.error;
  1201. goto fail_irq;
  1202. }
  1203. vc->priv = (void *)cc;
  1204. if (vc->id == V4L2_CID_CHROMA_AGC)
  1205. core->chroma_agc = vc;
  1206. }
  1207. v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
  1208. /* load and configure helper modules */
  1209. if (core->board.audio_chip == CX88_AUDIO_WM8775) {
  1210. struct i2c_board_info wm8775_info = {
  1211. .type = "wm8775",
  1212. .addr = 0x36 >> 1,
  1213. .platform_data = &core->wm8775_data,
  1214. };
  1215. struct v4l2_subdev *sd;
  1216. if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
  1217. core->wm8775_data.is_nova_s = true;
  1218. else
  1219. core->wm8775_data.is_nova_s = false;
  1220. sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
  1221. &wm8775_info, NULL);
  1222. if (sd) {
  1223. core->sd_wm8775 = sd;
  1224. sd->grp_id = WM8775_GID;
  1225. }
  1226. }
  1227. if (core->board.audio_chip == CX88_AUDIO_TVAUDIO) {
  1228. /*
  1229. * This probes for a tda9874 as is used on some
  1230. * Pixelview Ultra boards.
  1231. */
  1232. v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
  1233. "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
  1234. }
  1235. switch (core->boardnr) {
  1236. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1237. case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
  1238. static const struct i2c_board_info rtc_info = {
  1239. I2C_BOARD_INFO("isl1208", 0x6f)
  1240. };
  1241. request_module("rtc-isl1208");
  1242. core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
  1243. }
  1244. /* fall-through */
  1245. case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
  1246. request_module("ir-kbd-i2c");
  1247. }
  1248. /* Sets device info at pci_dev */
  1249. pci_set_drvdata(pci_dev, dev);
  1250. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1251. /* Maintain a reference so cx88-blackbird can query the 8800 device. */
  1252. core->v4ldev = dev;
  1253. /* initial device configuration */
  1254. mutex_lock(&core->lock);
  1255. cx88_set_tvnorm(core, V4L2_STD_NTSC_M);
  1256. v4l2_ctrl_handler_setup(&core->video_hdl);
  1257. v4l2_ctrl_handler_setup(&core->audio_hdl);
  1258. cx88_video_mux(core, 0);
  1259. q = &dev->vb2_vidq;
  1260. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1261. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1262. q->gfp_flags = GFP_DMA32;
  1263. q->min_buffers_needed = 2;
  1264. q->drv_priv = dev;
  1265. q->buf_struct_size = sizeof(struct cx88_buffer);
  1266. q->ops = &cx8800_video_qops;
  1267. q->mem_ops = &vb2_dma_sg_memops;
  1268. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1269. q->lock = &core->lock;
  1270. q->dev = &dev->pci->dev;
  1271. err = vb2_queue_init(q);
  1272. if (err < 0)
  1273. goto fail_unreg;
  1274. q = &dev->vb2_vbiq;
  1275. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1276. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
  1277. q->gfp_flags = GFP_DMA32;
  1278. q->min_buffers_needed = 2;
  1279. q->drv_priv = dev;
  1280. q->buf_struct_size = sizeof(struct cx88_buffer);
  1281. q->ops = &cx8800_vbi_qops;
  1282. q->mem_ops = &vb2_dma_sg_memops;
  1283. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1284. q->lock = &core->lock;
  1285. q->dev = &dev->pci->dev;
  1286. err = vb2_queue_init(q);
  1287. if (err < 0)
  1288. goto fail_unreg;
  1289. /* register v4l devices */
  1290. cx88_vdev_init(core, dev->pci, &dev->video_dev,
  1291. &cx8800_video_template, "video");
  1292. video_set_drvdata(&dev->video_dev, dev);
  1293. dev->video_dev.ctrl_handler = &core->video_hdl;
  1294. dev->video_dev.queue = &dev->vb2_vidq;
  1295. err = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER,
  1296. video_nr[core->nr]);
  1297. if (err < 0) {
  1298. pr_err("can't register video device\n");
  1299. goto fail_unreg;
  1300. }
  1301. pr_info("registered device %s [v4l2]\n",
  1302. video_device_node_name(&dev->video_dev));
  1303. cx88_vdev_init(core, dev->pci, &dev->vbi_dev,
  1304. &cx8800_vbi_template, "vbi");
  1305. video_set_drvdata(&dev->vbi_dev, dev);
  1306. dev->vbi_dev.queue = &dev->vb2_vbiq;
  1307. err = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
  1308. vbi_nr[core->nr]);
  1309. if (err < 0) {
  1310. pr_err("can't register vbi device\n");
  1311. goto fail_unreg;
  1312. }
  1313. pr_info("registered device %s\n",
  1314. video_device_node_name(&dev->vbi_dev));
  1315. if (core->board.radio.type == CX88_RADIO) {
  1316. cx88_vdev_init(core, dev->pci, &dev->radio_dev,
  1317. &cx8800_radio_template, "radio");
  1318. video_set_drvdata(&dev->radio_dev, dev);
  1319. dev->radio_dev.ctrl_handler = &core->audio_hdl;
  1320. err = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1321. radio_nr[core->nr]);
  1322. if (err < 0) {
  1323. pr_err("can't register radio device\n");
  1324. goto fail_unreg;
  1325. }
  1326. pr_info("registered device %s\n",
  1327. video_device_node_name(&dev->radio_dev));
  1328. }
  1329. /* start tvaudio thread */
  1330. if (core->board.tuner_type != UNSET) {
  1331. core->kthread = kthread_run(cx88_audio_thread,
  1332. core, "cx88 tvaudio");
  1333. if (IS_ERR(core->kthread)) {
  1334. err = PTR_ERR(core->kthread);
  1335. pr_err("failed to create cx88 audio thread, err=%d\n",
  1336. err);
  1337. }
  1338. }
  1339. mutex_unlock(&core->lock);
  1340. return 0;
  1341. fail_unreg:
  1342. cx8800_unregister_video(dev);
  1343. mutex_unlock(&core->lock);
  1344. fail_irq:
  1345. free_irq(pci_dev->irq, dev);
  1346. fail_core:
  1347. core->v4ldev = NULL;
  1348. cx88_core_put(core, dev->pci);
  1349. fail_disable:
  1350. pci_disable_device(pci_dev);
  1351. fail_free:
  1352. kfree(dev);
  1353. return err;
  1354. }
  1355. static void cx8800_finidev(struct pci_dev *pci_dev)
  1356. {
  1357. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1358. struct cx88_core *core = dev->core;
  1359. /* stop thread */
  1360. if (core->kthread) {
  1361. kthread_stop(core->kthread);
  1362. core->kthread = NULL;
  1363. }
  1364. if (core->ir)
  1365. cx88_ir_stop(core);
  1366. cx88_shutdown(core); /* FIXME */
  1367. /* unregister stuff */
  1368. free_irq(pci_dev->irq, dev);
  1369. cx8800_unregister_video(dev);
  1370. pci_disable_device(pci_dev);
  1371. core->v4ldev = NULL;
  1372. /* free memory */
  1373. cx88_core_put(core, dev->pci);
  1374. kfree(dev);
  1375. }
  1376. #ifdef CONFIG_PM
  1377. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1378. {
  1379. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1380. struct cx88_core *core = dev->core;
  1381. unsigned long flags;
  1382. /* stop video+vbi capture */
  1383. spin_lock_irqsave(&dev->slock, flags);
  1384. if (!list_empty(&dev->vidq.active)) {
  1385. pr_info("suspend video\n");
  1386. stop_video_dma(dev);
  1387. }
  1388. if (!list_empty(&dev->vbiq.active)) {
  1389. pr_info("suspend vbi\n");
  1390. cx8800_stop_vbi_dma(dev);
  1391. }
  1392. spin_unlock_irqrestore(&dev->slock, flags);
  1393. if (core->ir)
  1394. cx88_ir_stop(core);
  1395. /* FIXME -- shutdown device */
  1396. cx88_shutdown(core);
  1397. pci_save_state(pci_dev);
  1398. if (pci_set_power_state(pci_dev,
  1399. pci_choose_state(pci_dev, state)) != 0) {
  1400. pci_disable_device(pci_dev);
  1401. dev->state.disabled = 1;
  1402. }
  1403. return 0;
  1404. }
  1405. static int cx8800_resume(struct pci_dev *pci_dev)
  1406. {
  1407. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1408. struct cx88_core *core = dev->core;
  1409. unsigned long flags;
  1410. int err;
  1411. if (dev->state.disabled) {
  1412. err = pci_enable_device(pci_dev);
  1413. if (err) {
  1414. pr_err("can't enable device\n");
  1415. return err;
  1416. }
  1417. dev->state.disabled = 0;
  1418. }
  1419. err = pci_set_power_state(pci_dev, PCI_D0);
  1420. if (err) {
  1421. pr_err("can't set power state\n");
  1422. pci_disable_device(pci_dev);
  1423. dev->state.disabled = 1;
  1424. return err;
  1425. }
  1426. pci_restore_state(pci_dev);
  1427. /* FIXME: re-initialize hardware */
  1428. cx88_reset(core);
  1429. if (core->ir)
  1430. cx88_ir_start(core);
  1431. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1432. /* restart video+vbi capture */
  1433. spin_lock_irqsave(&dev->slock, flags);
  1434. if (!list_empty(&dev->vidq.active)) {
  1435. pr_info("resume video\n");
  1436. restart_video_queue(dev, &dev->vidq);
  1437. }
  1438. if (!list_empty(&dev->vbiq.active)) {
  1439. pr_info("resume vbi\n");
  1440. cx8800_restart_vbi_queue(dev, &dev->vbiq);
  1441. }
  1442. spin_unlock_irqrestore(&dev->slock, flags);
  1443. return 0;
  1444. }
  1445. #endif
  1446. /* ----------------------------------------------------------- */
  1447. static const struct pci_device_id cx8800_pci_tbl[] = {
  1448. {
  1449. .vendor = 0x14f1,
  1450. .device = 0x8800,
  1451. .subvendor = PCI_ANY_ID,
  1452. .subdevice = PCI_ANY_ID,
  1453. }, {
  1454. /* --- end of list --- */
  1455. }
  1456. };
  1457. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1458. static struct pci_driver cx8800_pci_driver = {
  1459. .name = "cx8800",
  1460. .id_table = cx8800_pci_tbl,
  1461. .probe = cx8800_initdev,
  1462. .remove = cx8800_finidev,
  1463. #ifdef CONFIG_PM
  1464. .suspend = cx8800_suspend,
  1465. .resume = cx8800_resume,
  1466. #endif
  1467. };
  1468. module_pci_driver(cx8800_pci_driver);