cx88-video.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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@infradead.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. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/list.h>
  29. #include <linux/module.h>
  30. #include <linux/kmod.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/delay.h>
  36. #include <linux/kthread.h>
  37. #include <asm/div64.h>
  38. #include "cx88.h"
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ioctl.h>
  41. #include <media/v4l2-event.h>
  42. #include <media/wm8775.h>
  43. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  44. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  45. MODULE_LICENSE("GPL");
  46. MODULE_VERSION(CX88_VERSION);
  47. /* ------------------------------------------------------------------ */
  48. static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  49. static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  50. static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  51. module_param_array(video_nr, int, NULL, 0444);
  52. module_param_array(vbi_nr, int, NULL, 0444);
  53. module_param_array(radio_nr, int, NULL, 0444);
  54. MODULE_PARM_DESC(video_nr,"video device numbers");
  55. MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
  56. MODULE_PARM_DESC(radio_nr,"radio device numbers");
  57. static unsigned int video_debug;
  58. module_param(video_debug,int,0644);
  59. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  60. static unsigned int irq_debug;
  61. module_param(irq_debug,int,0644);
  62. MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  63. static unsigned int vid_limit = 16;
  64. module_param(vid_limit,int,0644);
  65. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
  66. #define dprintk(level,fmt, arg...) if (video_debug >= level) \
  67. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  68. /* ------------------------------------------------------------------- */
  69. /* static data */
  70. static const struct cx8800_fmt formats[] = {
  71. {
  72. .name = "8 bpp, gray",
  73. .fourcc = V4L2_PIX_FMT_GREY,
  74. .cxformat = ColorFormatY8,
  75. .depth = 8,
  76. .flags = FORMAT_FLAGS_PACKED,
  77. },{
  78. .name = "15 bpp RGB, le",
  79. .fourcc = V4L2_PIX_FMT_RGB555,
  80. .cxformat = ColorFormatRGB15,
  81. .depth = 16,
  82. .flags = FORMAT_FLAGS_PACKED,
  83. },{
  84. .name = "15 bpp RGB, be",
  85. .fourcc = V4L2_PIX_FMT_RGB555X,
  86. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  87. .depth = 16,
  88. .flags = FORMAT_FLAGS_PACKED,
  89. },{
  90. .name = "16 bpp RGB, le",
  91. .fourcc = V4L2_PIX_FMT_RGB565,
  92. .cxformat = ColorFormatRGB16,
  93. .depth = 16,
  94. .flags = FORMAT_FLAGS_PACKED,
  95. },{
  96. .name = "16 bpp RGB, be",
  97. .fourcc = V4L2_PIX_FMT_RGB565X,
  98. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  99. .depth = 16,
  100. .flags = FORMAT_FLAGS_PACKED,
  101. },{
  102. .name = "24 bpp RGB, le",
  103. .fourcc = V4L2_PIX_FMT_BGR24,
  104. .cxformat = ColorFormatRGB24,
  105. .depth = 24,
  106. .flags = FORMAT_FLAGS_PACKED,
  107. },{
  108. .name = "32 bpp RGB, le",
  109. .fourcc = V4L2_PIX_FMT_BGR32,
  110. .cxformat = ColorFormatRGB32,
  111. .depth = 32,
  112. .flags = FORMAT_FLAGS_PACKED,
  113. },{
  114. .name = "32 bpp RGB, be",
  115. .fourcc = V4L2_PIX_FMT_RGB32,
  116. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
  117. .depth = 32,
  118. .flags = FORMAT_FLAGS_PACKED,
  119. },{
  120. .name = "4:2:2, packed, YUYV",
  121. .fourcc = V4L2_PIX_FMT_YUYV,
  122. .cxformat = ColorFormatYUY2,
  123. .depth = 16,
  124. .flags = FORMAT_FLAGS_PACKED,
  125. },{
  126. .name = "4:2:2, packed, UYVY",
  127. .fourcc = V4L2_PIX_FMT_UYVY,
  128. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  129. .depth = 16,
  130. .flags = FORMAT_FLAGS_PACKED,
  131. },
  132. };
  133. static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
  134. {
  135. unsigned int i;
  136. for (i = 0; i < ARRAY_SIZE(formats); i++)
  137. if (formats[i].fourcc == fourcc)
  138. return formats+i;
  139. return NULL;
  140. }
  141. /* ------------------------------------------------------------------- */
  142. struct cx88_ctrl {
  143. /* control information */
  144. u32 id;
  145. s32 minimum;
  146. s32 maximum;
  147. u32 step;
  148. s32 default_value;
  149. /* control register information */
  150. u32 off;
  151. u32 reg;
  152. u32 sreg;
  153. u32 mask;
  154. u32 shift;
  155. };
  156. static const struct cx88_ctrl cx8800_vid_ctls[] = {
  157. /* --- video --- */
  158. {
  159. .id = V4L2_CID_BRIGHTNESS,
  160. .minimum = 0x00,
  161. .maximum = 0xff,
  162. .step = 1,
  163. .default_value = 0x7f,
  164. .off = 128,
  165. .reg = MO_CONTR_BRIGHT,
  166. .mask = 0x00ff,
  167. .shift = 0,
  168. },{
  169. .id = V4L2_CID_CONTRAST,
  170. .minimum = 0,
  171. .maximum = 0xff,
  172. .step = 1,
  173. .default_value = 0x3f,
  174. .off = 0,
  175. .reg = MO_CONTR_BRIGHT,
  176. .mask = 0xff00,
  177. .shift = 8,
  178. },{
  179. .id = V4L2_CID_HUE,
  180. .minimum = 0,
  181. .maximum = 0xff,
  182. .step = 1,
  183. .default_value = 0x7f,
  184. .off = 128,
  185. .reg = MO_HUE,
  186. .mask = 0x00ff,
  187. .shift = 0,
  188. },{
  189. /* strictly, this only describes only U saturation.
  190. * V saturation is handled specially through code.
  191. */
  192. .id = V4L2_CID_SATURATION,
  193. .minimum = 0,
  194. .maximum = 0xff,
  195. .step = 1,
  196. .default_value = 0x7f,
  197. .off = 0,
  198. .reg = MO_UV_SATURATION,
  199. .mask = 0x00ff,
  200. .shift = 0,
  201. }, {
  202. .id = V4L2_CID_SHARPNESS,
  203. .minimum = 0,
  204. .maximum = 4,
  205. .step = 1,
  206. .default_value = 0x0,
  207. .off = 0,
  208. /* NOTE: the value is converted and written to both even
  209. and odd registers in the code */
  210. .reg = MO_FILTER_ODD,
  211. .mask = 7 << 7,
  212. .shift = 7,
  213. }, {
  214. .id = V4L2_CID_CHROMA_AGC,
  215. .minimum = 0,
  216. .maximum = 1,
  217. .default_value = 0x1,
  218. .reg = MO_INPUT_FORMAT,
  219. .mask = 1 << 10,
  220. .shift = 10,
  221. }, {
  222. .id = V4L2_CID_COLOR_KILLER,
  223. .minimum = 0,
  224. .maximum = 1,
  225. .default_value = 0x1,
  226. .reg = MO_INPUT_FORMAT,
  227. .mask = 1 << 9,
  228. .shift = 9,
  229. }, {
  230. .id = V4L2_CID_BAND_STOP_FILTER,
  231. .minimum = 0,
  232. .maximum = 1,
  233. .step = 1,
  234. .default_value = 0x0,
  235. .off = 0,
  236. .reg = MO_HTOTAL,
  237. .mask = 3 << 11,
  238. .shift = 11,
  239. }
  240. };
  241. static const struct cx88_ctrl cx8800_aud_ctls[] = {
  242. {
  243. /* --- audio --- */
  244. .id = V4L2_CID_AUDIO_MUTE,
  245. .minimum = 0,
  246. .maximum = 1,
  247. .default_value = 1,
  248. .reg = AUD_VOL_CTL,
  249. .sreg = SHADOW_AUD_VOL_CTL,
  250. .mask = (1 << 6),
  251. .shift = 6,
  252. },{
  253. .id = V4L2_CID_AUDIO_VOLUME,
  254. .minimum = 0,
  255. .maximum = 0x3f,
  256. .step = 1,
  257. .default_value = 0x3f,
  258. .reg = AUD_VOL_CTL,
  259. .sreg = SHADOW_AUD_VOL_CTL,
  260. .mask = 0x3f,
  261. .shift = 0,
  262. },{
  263. .id = V4L2_CID_AUDIO_BALANCE,
  264. .minimum = 0,
  265. .maximum = 0x7f,
  266. .step = 1,
  267. .default_value = 0x40,
  268. .reg = AUD_BAL_CTL,
  269. .sreg = SHADOW_AUD_BAL_CTL,
  270. .mask = 0x7f,
  271. .shift = 0,
  272. }
  273. };
  274. enum {
  275. CX8800_VID_CTLS = ARRAY_SIZE(cx8800_vid_ctls),
  276. CX8800_AUD_CTLS = ARRAY_SIZE(cx8800_aud_ctls),
  277. };
  278. /* ------------------------------------------------------------------- */
  279. /* resource management */
  280. static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
  281. {
  282. struct cx88_core *core = dev->core;
  283. if (fh->resources & bit)
  284. /* have it already allocated */
  285. return 1;
  286. /* is it free? */
  287. mutex_lock(&core->lock);
  288. if (dev->resources & bit) {
  289. /* no, someone else uses it */
  290. mutex_unlock(&core->lock);
  291. return 0;
  292. }
  293. /* it's free, grab it */
  294. fh->resources |= bit;
  295. dev->resources |= bit;
  296. dprintk(1,"res: get %d\n",bit);
  297. mutex_unlock(&core->lock);
  298. return 1;
  299. }
  300. static
  301. int res_check(struct cx8800_fh *fh, unsigned int bit)
  302. {
  303. return (fh->resources & bit);
  304. }
  305. static
  306. int res_locked(struct cx8800_dev *dev, unsigned int bit)
  307. {
  308. return (dev->resources & bit);
  309. }
  310. static
  311. void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
  312. {
  313. struct cx88_core *core = dev->core;
  314. BUG_ON((fh->resources & bits) != bits);
  315. mutex_lock(&core->lock);
  316. fh->resources &= ~bits;
  317. dev->resources &= ~bits;
  318. dprintk(1,"res: put %d\n",bits);
  319. mutex_unlock(&core->lock);
  320. }
  321. /* ------------------------------------------------------------------ */
  322. int cx88_video_mux(struct cx88_core *core, unsigned int input)
  323. {
  324. /* struct cx88_core *core = dev->core; */
  325. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  326. input, INPUT(input).vmux,
  327. INPUT(input).gpio0,INPUT(input).gpio1,
  328. INPUT(input).gpio2,INPUT(input).gpio3);
  329. core->input = input;
  330. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
  331. cx_write(MO_GP3_IO, INPUT(input).gpio3);
  332. cx_write(MO_GP0_IO, INPUT(input).gpio0);
  333. cx_write(MO_GP1_IO, INPUT(input).gpio1);
  334. cx_write(MO_GP2_IO, INPUT(input).gpio2);
  335. switch (INPUT(input).type) {
  336. case CX88_VMUX_SVIDEO:
  337. cx_set(MO_AFECFG_IO, 0x00000001);
  338. cx_set(MO_INPUT_FORMAT, 0x00010010);
  339. cx_set(MO_FILTER_EVEN, 0x00002020);
  340. cx_set(MO_FILTER_ODD, 0x00002020);
  341. break;
  342. default:
  343. cx_clear(MO_AFECFG_IO, 0x00000001);
  344. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  345. cx_clear(MO_FILTER_EVEN, 0x00002020);
  346. cx_clear(MO_FILTER_ODD, 0x00002020);
  347. break;
  348. }
  349. /* if there are audioroutes defined, we have an external
  350. ADC to deal with audio */
  351. if (INPUT(input).audioroute) {
  352. /* The wm8775 module has the "2" route hardwired into
  353. the initialization. Some boards may use different
  354. routes for different inputs. HVR-1300 surely does */
  355. if (core->sd_wm8775) {
  356. call_all(core, audio, s_routing,
  357. INPUT(input).audioroute, 0, 0);
  358. }
  359. /* cx2388's C-ADC is connected to the tuner only.
  360. When used with S-Video, that ADC is busy dealing with
  361. chroma, so an external must be used for baseband audio */
  362. if (INPUT(input).type != CX88_VMUX_TELEVISION &&
  363. INPUT(input).type != CX88_VMUX_CABLE) {
  364. /* "I2S ADC mode" */
  365. core->tvaudio = WW_I2SADC;
  366. cx88_set_tvaudio(core);
  367. } else {
  368. /* Normal mode */
  369. cx_write(AUD_I2SCNTL, 0x0);
  370. cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
  371. }
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(cx88_video_mux);
  376. /* ------------------------------------------------------------------ */
  377. static int start_video_dma(struct cx8800_dev *dev,
  378. struct cx88_dmaqueue *q,
  379. struct cx88_buffer *buf)
  380. {
  381. struct cx88_core *core = dev->core;
  382. /* setup fifo + format */
  383. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  384. buf->bpl, buf->risc.dma);
  385. cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
  386. cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
  387. /* reset counter */
  388. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  389. q->count = 1;
  390. /* enable irqs */
  391. cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
  392. /* Enables corresponding bits at PCI_INT_STAT:
  393. bits 0 to 4: video, audio, transport stream, VIP, Host
  394. bit 7: timer
  395. bits 8 and 9: DMA complete for: SRC, DST
  396. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  397. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  398. */
  399. cx_set(MO_VID_INTMSK, 0x0f0011);
  400. /* enable capture */
  401. cx_set(VID_CAPTURE_CONTROL,0x06);
  402. /* start dma */
  403. cx_set(MO_DEV_CNTRL2, (1<<5));
  404. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  405. return 0;
  406. }
  407. #ifdef CONFIG_PM
  408. static int stop_video_dma(struct cx8800_dev *dev)
  409. {
  410. struct cx88_core *core = dev->core;
  411. /* stop dma */
  412. cx_clear(MO_VID_DMACNTRL, 0x11);
  413. /* disable capture */
  414. cx_clear(VID_CAPTURE_CONTROL,0x06);
  415. /* disable irqs */
  416. cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
  417. cx_clear(MO_VID_INTMSK, 0x0f0011);
  418. return 0;
  419. }
  420. #endif
  421. static int restart_video_queue(struct cx8800_dev *dev,
  422. struct cx88_dmaqueue *q)
  423. {
  424. struct cx88_core *core = dev->core;
  425. struct cx88_buffer *buf, *prev;
  426. if (!list_empty(&q->active)) {
  427. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  428. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  429. buf, buf->vb.i);
  430. start_video_dma(dev, q, buf);
  431. list_for_each_entry(buf, &q->active, vb.queue)
  432. buf->count = q->count++;
  433. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  434. return 0;
  435. }
  436. prev = NULL;
  437. for (;;) {
  438. if (list_empty(&q->queued))
  439. return 0;
  440. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  441. if (NULL == prev) {
  442. list_move_tail(&buf->vb.queue, &q->active);
  443. start_video_dma(dev, q, buf);
  444. buf->vb.state = VIDEOBUF_ACTIVE;
  445. buf->count = q->count++;
  446. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  447. dprintk(2,"[%p/%d] restart_queue - first active\n",
  448. buf,buf->vb.i);
  449. } else if (prev->vb.width == buf->vb.width &&
  450. prev->vb.height == buf->vb.height &&
  451. prev->fmt == buf->fmt) {
  452. list_move_tail(&buf->vb.queue, &q->active);
  453. buf->vb.state = VIDEOBUF_ACTIVE;
  454. buf->count = q->count++;
  455. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  456. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  457. buf,buf->vb.i);
  458. } else {
  459. return 0;
  460. }
  461. prev = buf;
  462. }
  463. }
  464. /* ------------------------------------------------------------------ */
  465. static int
  466. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  467. {
  468. struct cx8800_fh *fh = q->priv_data;
  469. struct cx8800_dev *dev = fh->dev;
  470. *size = dev->fmt->depth * dev->width * dev->height >> 3;
  471. if (0 == *count)
  472. *count = 32;
  473. if (*size * *count > vid_limit * 1024 * 1024)
  474. *count = (vid_limit * 1024 * 1024) / *size;
  475. return 0;
  476. }
  477. static int
  478. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  479. enum v4l2_field field)
  480. {
  481. struct cx8800_fh *fh = q->priv_data;
  482. struct cx8800_dev *dev = fh->dev;
  483. struct cx88_core *core = dev->core;
  484. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  485. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  486. int rc, init_buffer = 0;
  487. BUG_ON(NULL == dev->fmt);
  488. if (dev->width < 48 || dev->width > norm_maxw(core->tvnorm) ||
  489. dev->height < 32 || dev->height > norm_maxh(core->tvnorm))
  490. return -EINVAL;
  491. buf->vb.size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  492. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  493. return -EINVAL;
  494. if (buf->fmt != dev->fmt ||
  495. buf->vb.width != dev->width ||
  496. buf->vb.height != dev->height ||
  497. buf->vb.field != field) {
  498. buf->fmt = dev->fmt;
  499. buf->vb.width = dev->width;
  500. buf->vb.height = dev->height;
  501. buf->vb.field = field;
  502. init_buffer = 1;
  503. }
  504. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  505. init_buffer = 1;
  506. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  507. goto fail;
  508. }
  509. if (init_buffer) {
  510. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  511. switch (buf->vb.field) {
  512. case V4L2_FIELD_TOP:
  513. cx88_risc_buffer(dev->pci, &buf->risc,
  514. dma->sglist, 0, UNSET,
  515. buf->bpl, 0, buf->vb.height);
  516. break;
  517. case V4L2_FIELD_BOTTOM:
  518. cx88_risc_buffer(dev->pci, &buf->risc,
  519. dma->sglist, UNSET, 0,
  520. buf->bpl, 0, buf->vb.height);
  521. break;
  522. case V4L2_FIELD_INTERLACED:
  523. cx88_risc_buffer(dev->pci, &buf->risc,
  524. dma->sglist, 0, buf->bpl,
  525. buf->bpl, buf->bpl,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_TB:
  529. cx88_risc_buffer(dev->pci, &buf->risc,
  530. dma->sglist,
  531. 0, buf->bpl * (buf->vb.height >> 1),
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. case V4L2_FIELD_SEQ_BT:
  536. cx88_risc_buffer(dev->pci, &buf->risc,
  537. dma->sglist,
  538. buf->bpl * (buf->vb.height >> 1), 0,
  539. buf->bpl, 0,
  540. buf->vb.height >> 1);
  541. break;
  542. default:
  543. BUG();
  544. }
  545. }
  546. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  547. buf, buf->vb.i,
  548. dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
  549. (unsigned long)buf->risc.dma);
  550. buf->vb.state = VIDEOBUF_PREPARED;
  551. return 0;
  552. fail:
  553. cx88_free_buffer(q,buf);
  554. return rc;
  555. }
  556. static void
  557. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  558. {
  559. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  560. struct cx88_buffer *prev;
  561. struct cx8800_fh *fh = vq->priv_data;
  562. struct cx8800_dev *dev = fh->dev;
  563. struct cx88_core *core = dev->core;
  564. struct cx88_dmaqueue *q = &dev->vidq;
  565. /* add jump to stopper */
  566. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  567. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  568. if (!list_empty(&q->queued)) {
  569. list_add_tail(&buf->vb.queue,&q->queued);
  570. buf->vb.state = VIDEOBUF_QUEUED;
  571. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  572. buf, buf->vb.i);
  573. } else if (list_empty(&q->active)) {
  574. list_add_tail(&buf->vb.queue,&q->active);
  575. start_video_dma(dev, q, buf);
  576. buf->vb.state = VIDEOBUF_ACTIVE;
  577. buf->count = q->count++;
  578. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  579. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  580. buf, buf->vb.i);
  581. } else {
  582. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  583. if (prev->vb.width == buf->vb.width &&
  584. prev->vb.height == buf->vb.height &&
  585. prev->fmt == buf->fmt) {
  586. list_add_tail(&buf->vb.queue,&q->active);
  587. buf->vb.state = VIDEOBUF_ACTIVE;
  588. buf->count = q->count++;
  589. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  590. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  591. buf, buf->vb.i);
  592. } else {
  593. list_add_tail(&buf->vb.queue,&q->queued);
  594. buf->vb.state = VIDEOBUF_QUEUED;
  595. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  596. buf, buf->vb.i);
  597. }
  598. }
  599. }
  600. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  601. {
  602. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  603. cx88_free_buffer(q,buf);
  604. }
  605. static const struct videobuf_queue_ops cx8800_video_qops = {
  606. .buf_setup = buffer_setup,
  607. .buf_prepare = buffer_prepare,
  608. .buf_queue = buffer_queue,
  609. .buf_release = buffer_release,
  610. };
  611. /* ------------------------------------------------------------------ */
  612. /* ------------------------------------------------------------------ */
  613. static struct videobuf_queue *get_queue(struct file *file)
  614. {
  615. struct video_device *vdev = video_devdata(file);
  616. struct cx8800_fh *fh = file->private_data;
  617. switch (vdev->vfl_type) {
  618. case VFL_TYPE_GRABBER:
  619. return &fh->vidq;
  620. case VFL_TYPE_VBI:
  621. return &fh->vbiq;
  622. default:
  623. BUG();
  624. return NULL;
  625. }
  626. }
  627. static int get_resource(struct file *file)
  628. {
  629. struct video_device *vdev = video_devdata(file);
  630. switch (vdev->vfl_type) {
  631. case VFL_TYPE_GRABBER:
  632. return RESOURCE_VIDEO;
  633. case VFL_TYPE_VBI:
  634. return RESOURCE_VBI;
  635. default:
  636. BUG();
  637. return 0;
  638. }
  639. }
  640. static int video_open(struct file *file)
  641. {
  642. struct video_device *vdev = video_devdata(file);
  643. struct cx8800_dev *dev = video_drvdata(file);
  644. struct cx88_core *core = dev->core;
  645. struct cx8800_fh *fh;
  646. enum v4l2_buf_type type = 0;
  647. int radio = 0;
  648. switch (vdev->vfl_type) {
  649. case VFL_TYPE_GRABBER:
  650. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  651. break;
  652. case VFL_TYPE_VBI:
  653. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  654. break;
  655. case VFL_TYPE_RADIO:
  656. radio = 1;
  657. break;
  658. }
  659. dprintk(1, "open dev=%s radio=%d type=%s\n",
  660. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  661. /* allocate + initialize per filehandle data */
  662. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  663. if (unlikely(!fh))
  664. return -ENOMEM;
  665. v4l2_fh_init(&fh->fh, vdev);
  666. file->private_data = fh;
  667. fh->dev = dev;
  668. mutex_lock(&core->lock);
  669. videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
  670. &dev->pci->dev, &dev->slock,
  671. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  672. V4L2_FIELD_INTERLACED,
  673. sizeof(struct cx88_buffer),
  674. fh, NULL);
  675. videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
  676. &dev->pci->dev, &dev->slock,
  677. V4L2_BUF_TYPE_VBI_CAPTURE,
  678. V4L2_FIELD_SEQ_TB,
  679. sizeof(struct cx88_buffer),
  680. fh, NULL);
  681. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  682. dprintk(1,"video_open: setting radio device\n");
  683. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  684. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  685. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  686. cx_write(MO_GP2_IO, core->board.radio.gpio2);
  687. if (core->board.radio.audioroute) {
  688. if (core->sd_wm8775) {
  689. call_all(core, audio, s_routing,
  690. core->board.radio.audioroute, 0, 0);
  691. }
  692. /* "I2S ADC mode" */
  693. core->tvaudio = WW_I2SADC;
  694. cx88_set_tvaudio(core);
  695. } else {
  696. /* FM Mode */
  697. core->tvaudio = WW_FM;
  698. cx88_set_tvaudio(core);
  699. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  700. }
  701. call_all(core, tuner, s_radio);
  702. }
  703. core->users++;
  704. mutex_unlock(&core->lock);
  705. v4l2_fh_add(&fh->fh);
  706. return 0;
  707. }
  708. static ssize_t
  709. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  710. {
  711. struct video_device *vdev = video_devdata(file);
  712. struct cx8800_fh *fh = file->private_data;
  713. switch (vdev->vfl_type) {
  714. case VFL_TYPE_GRABBER:
  715. if (res_locked(fh->dev,RESOURCE_VIDEO))
  716. return -EBUSY;
  717. return videobuf_read_one(&fh->vidq, data, count, ppos,
  718. file->f_flags & O_NONBLOCK);
  719. case VFL_TYPE_VBI:
  720. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  721. return -EBUSY;
  722. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  723. file->f_flags & O_NONBLOCK);
  724. default:
  725. BUG();
  726. return 0;
  727. }
  728. }
  729. static unsigned int
  730. video_poll(struct file *file, struct poll_table_struct *wait)
  731. {
  732. struct video_device *vdev = video_devdata(file);
  733. struct cx8800_fh *fh = file->private_data;
  734. struct cx88_buffer *buf;
  735. unsigned int rc = v4l2_ctrl_poll(file, wait);
  736. if (vdev->vfl_type == VFL_TYPE_VBI) {
  737. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  738. return rc | POLLERR;
  739. return rc | videobuf_poll_stream(file, &fh->vbiq, wait);
  740. }
  741. mutex_lock(&fh->vidq.vb_lock);
  742. if (res_check(fh,RESOURCE_VIDEO)) {
  743. /* streaming capture */
  744. if (list_empty(&fh->vidq.stream))
  745. goto done;
  746. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  747. } else {
  748. /* read() capture */
  749. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  750. if (NULL == buf)
  751. goto done;
  752. }
  753. poll_wait(file, &buf->vb.done, wait);
  754. if (buf->vb.state == VIDEOBUF_DONE ||
  755. buf->vb.state == VIDEOBUF_ERROR)
  756. rc |= POLLIN|POLLRDNORM;
  757. done:
  758. mutex_unlock(&fh->vidq.vb_lock);
  759. return rc;
  760. }
  761. static int video_release(struct file *file)
  762. {
  763. struct cx8800_fh *fh = file->private_data;
  764. struct cx8800_dev *dev = fh->dev;
  765. /* turn off overlay */
  766. if (res_check(fh, RESOURCE_OVERLAY)) {
  767. /* FIXME */
  768. res_free(dev,fh,RESOURCE_OVERLAY);
  769. }
  770. /* stop video capture */
  771. if (res_check(fh, RESOURCE_VIDEO)) {
  772. videobuf_queue_cancel(&fh->vidq);
  773. res_free(dev,fh,RESOURCE_VIDEO);
  774. }
  775. if (fh->vidq.read_buf) {
  776. buffer_release(&fh->vidq,fh->vidq.read_buf);
  777. kfree(fh->vidq.read_buf);
  778. }
  779. /* stop vbi capture */
  780. if (res_check(fh, RESOURCE_VBI)) {
  781. videobuf_stop(&fh->vbiq);
  782. res_free(dev,fh,RESOURCE_VBI);
  783. }
  784. videobuf_mmap_free(&fh->vidq);
  785. videobuf_mmap_free(&fh->vbiq);
  786. mutex_lock(&dev->core->lock);
  787. v4l2_fh_del(&fh->fh);
  788. v4l2_fh_exit(&fh->fh);
  789. file->private_data = NULL;
  790. kfree(fh);
  791. dev->core->users--;
  792. if (!dev->core->users)
  793. call_all(dev->core, core, s_power, 0);
  794. mutex_unlock(&dev->core->lock);
  795. return 0;
  796. }
  797. static int
  798. video_mmap(struct file *file, struct vm_area_struct * vma)
  799. {
  800. return videobuf_mmap_mapper(get_queue(file), vma);
  801. }
  802. /* ------------------------------------------------------------------ */
  803. /* VIDEO CTRL IOCTLS */
  804. static int cx8800_s_vid_ctrl(struct v4l2_ctrl *ctrl)
  805. {
  806. struct cx88_core *core =
  807. container_of(ctrl->handler, struct cx88_core, video_hdl);
  808. const struct cx88_ctrl *cc = ctrl->priv;
  809. u32 value, mask;
  810. mask = cc->mask;
  811. switch (ctrl->id) {
  812. case V4L2_CID_SATURATION:
  813. /* special v_sat handling */
  814. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  815. if (core->tvnorm & V4L2_STD_SECAM) {
  816. /* For SECAM, both U and V sat should be equal */
  817. value = value << 8 | value;
  818. } else {
  819. /* Keeps U Saturation proportional to V Sat */
  820. value = (value * 0x5a) / 0x7f << 8 | value;
  821. }
  822. mask = 0xffff;
  823. break;
  824. case V4L2_CID_SHARPNESS:
  825. /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
  826. value = (ctrl->val < 1 ? 0 : ((ctrl->val + 3) << 7));
  827. /* needs to be set for both fields */
  828. cx_andor(MO_FILTER_EVEN, mask, value);
  829. break;
  830. case V4L2_CID_CHROMA_AGC:
  831. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  832. break;
  833. default:
  834. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  835. break;
  836. }
  837. dprintk(1, "set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  838. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  839. mask, cc->sreg ? " [shadowed]" : "");
  840. if (cc->sreg)
  841. cx_sandor(cc->sreg, cc->reg, mask, value);
  842. else
  843. cx_andor(cc->reg, mask, value);
  844. return 0;
  845. }
  846. static int cx8800_s_aud_ctrl(struct v4l2_ctrl *ctrl)
  847. {
  848. struct cx88_core *core =
  849. container_of(ctrl->handler, struct cx88_core, audio_hdl);
  850. const struct cx88_ctrl *cc = ctrl->priv;
  851. u32 value,mask;
  852. /* Pass changes onto any WM8775 */
  853. if (core->sd_wm8775) {
  854. switch (ctrl->id) {
  855. case V4L2_CID_AUDIO_MUTE:
  856. wm8775_s_ctrl(core, ctrl->id, ctrl->val);
  857. break;
  858. case V4L2_CID_AUDIO_VOLUME:
  859. wm8775_s_ctrl(core, ctrl->id, (ctrl->val) ?
  860. (0x90 + ctrl->val) << 8 : 0);
  861. break;
  862. case V4L2_CID_AUDIO_BALANCE:
  863. wm8775_s_ctrl(core, ctrl->id, ctrl->val << 9);
  864. break;
  865. default:
  866. break;
  867. }
  868. }
  869. mask = cc->mask;
  870. switch (ctrl->id) {
  871. case V4L2_CID_AUDIO_BALANCE:
  872. value = (ctrl->val < 0x40) ? (0x7f - ctrl->val) : (ctrl->val - 0x40);
  873. break;
  874. case V4L2_CID_AUDIO_VOLUME:
  875. value = 0x3f - (ctrl->val & 0x3f);
  876. break;
  877. default:
  878. value = ((ctrl->val - cc->off) << cc->shift) & cc->mask;
  879. break;
  880. }
  881. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  882. ctrl->id, ctrl->name, ctrl->val, cc->reg, value,
  883. mask, cc->sreg ? " [shadowed]" : "");
  884. if (cc->sreg)
  885. cx_sandor(cc->sreg, cc->reg, mask, value);
  886. else
  887. cx_andor(cc->reg, mask, value);
  888. return 0;
  889. }
  890. /* ------------------------------------------------------------------ */
  891. /* VIDEO IOCTLS */
  892. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  893. struct v4l2_format *f)
  894. {
  895. struct cx8800_fh *fh = priv;
  896. struct cx8800_dev *dev = fh->dev;
  897. f->fmt.pix.width = dev->width;
  898. f->fmt.pix.height = dev->height;
  899. f->fmt.pix.field = fh->vidq.field;
  900. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  901. f->fmt.pix.bytesperline =
  902. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  903. f->fmt.pix.sizeimage =
  904. f->fmt.pix.height * f->fmt.pix.bytesperline;
  905. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  906. return 0;
  907. }
  908. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  909. struct v4l2_format *f)
  910. {
  911. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  912. const struct cx8800_fmt *fmt;
  913. enum v4l2_field field;
  914. unsigned int maxw, maxh;
  915. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  916. if (NULL == fmt)
  917. return -EINVAL;
  918. field = f->fmt.pix.field;
  919. maxw = norm_maxw(core->tvnorm);
  920. maxh = norm_maxh(core->tvnorm);
  921. if (V4L2_FIELD_ANY == field) {
  922. field = (f->fmt.pix.height > maxh/2)
  923. ? V4L2_FIELD_INTERLACED
  924. : V4L2_FIELD_BOTTOM;
  925. }
  926. switch (field) {
  927. case V4L2_FIELD_TOP:
  928. case V4L2_FIELD_BOTTOM:
  929. maxh = maxh / 2;
  930. break;
  931. case V4L2_FIELD_INTERLACED:
  932. break;
  933. default:
  934. return -EINVAL;
  935. }
  936. f->fmt.pix.field = field;
  937. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  938. &f->fmt.pix.height, 32, maxh, 0, 0);
  939. f->fmt.pix.bytesperline =
  940. (f->fmt.pix.width * fmt->depth) >> 3;
  941. f->fmt.pix.sizeimage =
  942. f->fmt.pix.height * f->fmt.pix.bytesperline;
  943. return 0;
  944. }
  945. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  946. struct v4l2_format *f)
  947. {
  948. struct cx8800_fh *fh = priv;
  949. struct cx8800_dev *dev = fh->dev;
  950. int err = vidioc_try_fmt_vid_cap (file,priv,f);
  951. if (0 != err)
  952. return err;
  953. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  954. dev->width = f->fmt.pix.width;
  955. dev->height = f->fmt.pix.height;
  956. fh->vidq.field = f->fmt.pix.field;
  957. return 0;
  958. }
  959. void cx88_querycap(struct file *file, struct cx88_core *core,
  960. struct v4l2_capability *cap)
  961. {
  962. struct video_device *vdev = video_devdata(file);
  963. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  964. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  965. if (UNSET != core->board.tuner_type)
  966. cap->device_caps |= V4L2_CAP_TUNER;
  967. switch (vdev->vfl_type) {
  968. case VFL_TYPE_RADIO:
  969. cap->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  970. break;
  971. case VFL_TYPE_GRABBER:
  972. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  973. break;
  974. case VFL_TYPE_VBI:
  975. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  976. break;
  977. }
  978. cap->capabilities = cap->device_caps | V4L2_CAP_VIDEO_CAPTURE |
  979. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_DEVICE_CAPS;
  980. if (core->board.radio.type == CX88_RADIO)
  981. cap->capabilities |= V4L2_CAP_RADIO;
  982. }
  983. EXPORT_SYMBOL(cx88_querycap);
  984. static int vidioc_querycap(struct file *file, void *priv,
  985. struct v4l2_capability *cap)
  986. {
  987. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  988. struct cx88_core *core = dev->core;
  989. strcpy(cap->driver, "cx8800");
  990. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  991. cx88_querycap(file, core, cap);
  992. return 0;
  993. }
  994. static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
  995. struct v4l2_fmtdesc *f)
  996. {
  997. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  998. return -EINVAL;
  999. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  1000. f->pixelformat = formats[f->index].fourcc;
  1001. return 0;
  1002. }
  1003. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  1004. {
  1005. return videobuf_reqbufs(get_queue(file), p);
  1006. }
  1007. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1008. {
  1009. return videobuf_querybuf(get_queue(file), p);
  1010. }
  1011. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1012. {
  1013. return videobuf_qbuf(get_queue(file), p);
  1014. }
  1015. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1016. {
  1017. return videobuf_dqbuf(get_queue(file), p,
  1018. file->f_flags & O_NONBLOCK);
  1019. }
  1020. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1021. {
  1022. struct video_device *vdev = video_devdata(file);
  1023. struct cx8800_fh *fh = priv;
  1024. struct cx8800_dev *dev = fh->dev;
  1025. if ((vdev->vfl_type == VFL_TYPE_GRABBER && i != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1026. (vdev->vfl_type == VFL_TYPE_VBI && i != V4L2_BUF_TYPE_VBI_CAPTURE))
  1027. return -EINVAL;
  1028. if (unlikely(!res_get(dev, fh, get_resource(file))))
  1029. return -EBUSY;
  1030. return videobuf_streamon(get_queue(file));
  1031. }
  1032. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1033. {
  1034. struct video_device *vdev = video_devdata(file);
  1035. struct cx8800_fh *fh = priv;
  1036. struct cx8800_dev *dev = fh->dev;
  1037. int err, res;
  1038. if ((vdev->vfl_type == VFL_TYPE_GRABBER && i != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1039. (vdev->vfl_type == VFL_TYPE_VBI && i != V4L2_BUF_TYPE_VBI_CAPTURE))
  1040. return -EINVAL;
  1041. res = get_resource(file);
  1042. err = videobuf_streamoff(get_queue(file));
  1043. if (err < 0)
  1044. return err;
  1045. res_free(dev,fh,res);
  1046. return 0;
  1047. }
  1048. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
  1049. {
  1050. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1051. *tvnorm = core->tvnorm;
  1052. return 0;
  1053. }
  1054. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
  1055. {
  1056. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1057. mutex_lock(&core->lock);
  1058. cx88_set_tvnorm(core, tvnorms);
  1059. mutex_unlock(&core->lock);
  1060. return 0;
  1061. }
  1062. /* only one input in this sample driver */
  1063. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1064. {
  1065. static const char * const iname[] = {
  1066. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1067. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1068. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1069. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1070. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1071. [ CX88_VMUX_TELEVISION ] = "Television",
  1072. [ CX88_VMUX_CABLE ] = "Cable TV",
  1073. [ CX88_VMUX_DVB ] = "DVB",
  1074. [ CX88_VMUX_DEBUG ] = "for debug only",
  1075. };
  1076. unsigned int n = i->index;
  1077. if (n >= 4)
  1078. return -EINVAL;
  1079. if (0 == INPUT(n).type)
  1080. return -EINVAL;
  1081. i->type = V4L2_INPUT_TYPE_CAMERA;
  1082. strcpy(i->name,iname[INPUT(n).type]);
  1083. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  1084. (CX88_VMUX_CABLE == INPUT(n).type)) {
  1085. i->type = V4L2_INPUT_TYPE_TUNER;
  1086. }
  1087. i->std = CX88_NORMS;
  1088. return 0;
  1089. }
  1090. EXPORT_SYMBOL(cx88_enum_input);
  1091. static int vidioc_enum_input (struct file *file, void *priv,
  1092. struct v4l2_input *i)
  1093. {
  1094. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1095. return cx88_enum_input (core,i);
  1096. }
  1097. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1098. {
  1099. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1100. *i = core->input;
  1101. return 0;
  1102. }
  1103. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1104. {
  1105. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1106. if (i >= 4)
  1107. return -EINVAL;
  1108. if (0 == INPUT(i).type)
  1109. return -EINVAL;
  1110. mutex_lock(&core->lock);
  1111. cx88_newstation(core);
  1112. cx88_video_mux(core,i);
  1113. mutex_unlock(&core->lock);
  1114. return 0;
  1115. }
  1116. static int vidioc_g_tuner (struct file *file, void *priv,
  1117. struct v4l2_tuner *t)
  1118. {
  1119. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1120. u32 reg;
  1121. if (unlikely(UNSET == core->board.tuner_type))
  1122. return -EINVAL;
  1123. if (0 != t->index)
  1124. return -EINVAL;
  1125. strcpy(t->name, "Television");
  1126. t->capability = V4L2_TUNER_CAP_NORM;
  1127. t->rangehigh = 0xffffffffUL;
  1128. call_all(core, tuner, g_tuner, t);
  1129. cx88_get_stereo(core ,t);
  1130. reg = cx_read(MO_DEVICE_STATUS);
  1131. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1132. return 0;
  1133. }
  1134. static int vidioc_s_tuner (struct file *file, void *priv,
  1135. const struct v4l2_tuner *t)
  1136. {
  1137. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1138. if (UNSET == core->board.tuner_type)
  1139. return -EINVAL;
  1140. if (0 != t->index)
  1141. return -EINVAL;
  1142. cx88_set_stereo(core, t->audmode, 1);
  1143. return 0;
  1144. }
  1145. static int vidioc_g_frequency (struct file *file, void *priv,
  1146. struct v4l2_frequency *f)
  1147. {
  1148. struct cx8800_fh *fh = priv;
  1149. struct cx88_core *core = fh->dev->core;
  1150. if (unlikely(UNSET == core->board.tuner_type))
  1151. return -EINVAL;
  1152. if (f->tuner)
  1153. return -EINVAL;
  1154. f->frequency = core->freq;
  1155. call_all(core, tuner, g_frequency, f);
  1156. return 0;
  1157. }
  1158. int cx88_set_freq (struct cx88_core *core,
  1159. const struct v4l2_frequency *f)
  1160. {
  1161. struct v4l2_frequency new_freq = *f;
  1162. if (unlikely(UNSET == core->board.tuner_type))
  1163. return -EINVAL;
  1164. if (unlikely(f->tuner != 0))
  1165. return -EINVAL;
  1166. mutex_lock(&core->lock);
  1167. cx88_newstation(core);
  1168. call_all(core, tuner, s_frequency, f);
  1169. call_all(core, tuner, g_frequency, &new_freq);
  1170. core->freq = new_freq.frequency;
  1171. /* When changing channels it is required to reset TVAUDIO */
  1172. msleep (10);
  1173. cx88_set_tvaudio(core);
  1174. mutex_unlock(&core->lock);
  1175. return 0;
  1176. }
  1177. EXPORT_SYMBOL(cx88_set_freq);
  1178. static int vidioc_s_frequency (struct file *file, void *priv,
  1179. const struct v4l2_frequency *f)
  1180. {
  1181. struct cx8800_fh *fh = priv;
  1182. struct cx88_core *core = fh->dev->core;
  1183. return cx88_set_freq(core, f);
  1184. }
  1185. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1186. struct v4l2_dbg_chip_ident *chip)
  1187. {
  1188. if (!v4l2_chip_match_host(&chip->match))
  1189. return -EINVAL;
  1190. chip->revision = 0;
  1191. chip->ident = V4L2_IDENT_UNKNOWN;
  1192. return 0;
  1193. }
  1194. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1195. static int vidioc_g_register (struct file *file, void *fh,
  1196. struct v4l2_dbg_register *reg)
  1197. {
  1198. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1199. if (!v4l2_chip_match_host(&reg->match))
  1200. return -EINVAL;
  1201. /* cx2388x has a 24-bit register space */
  1202. reg->val = cx_read(reg->reg & 0xffffff);
  1203. reg->size = 4;
  1204. return 0;
  1205. }
  1206. static int vidioc_s_register (struct file *file, void *fh,
  1207. const struct v4l2_dbg_register *reg)
  1208. {
  1209. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1210. if (!v4l2_chip_match_host(&reg->match))
  1211. return -EINVAL;
  1212. cx_write(reg->reg & 0xffffff, reg->val);
  1213. return 0;
  1214. }
  1215. #endif
  1216. /* ----------------------------------------------------------- */
  1217. /* RADIO ESPECIFIC IOCTLS */
  1218. /* ----------------------------------------------------------- */
  1219. static int radio_g_tuner (struct file *file, void *priv,
  1220. struct v4l2_tuner *t)
  1221. {
  1222. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1223. if (unlikely(t->index > 0))
  1224. return -EINVAL;
  1225. strcpy(t->name, "Radio");
  1226. call_all(core, tuner, g_tuner, t);
  1227. return 0;
  1228. }
  1229. static int radio_s_tuner (struct file *file, void *priv,
  1230. const struct v4l2_tuner *t)
  1231. {
  1232. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1233. if (0 != t->index)
  1234. return -EINVAL;
  1235. call_all(core, tuner, s_tuner, t);
  1236. return 0;
  1237. }
  1238. /* ----------------------------------------------------------- */
  1239. static void cx8800_vid_timeout(unsigned long data)
  1240. {
  1241. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1242. struct cx88_core *core = dev->core;
  1243. struct cx88_dmaqueue *q = &dev->vidq;
  1244. struct cx88_buffer *buf;
  1245. unsigned long flags;
  1246. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1247. cx_clear(MO_VID_DMACNTRL, 0x11);
  1248. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1249. spin_lock_irqsave(&dev->slock,flags);
  1250. while (!list_empty(&q->active)) {
  1251. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1252. list_del(&buf->vb.queue);
  1253. buf->vb.state = VIDEOBUF_ERROR;
  1254. wake_up(&buf->vb.done);
  1255. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1256. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1257. }
  1258. restart_video_queue(dev,q);
  1259. spin_unlock_irqrestore(&dev->slock,flags);
  1260. }
  1261. static const char *cx88_vid_irqs[32] = {
  1262. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1263. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1264. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1265. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1266. "opc_err", "par_err", "rip_err", "pci_abort",
  1267. };
  1268. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1269. {
  1270. struct cx88_core *core = dev->core;
  1271. u32 status, mask, count;
  1272. status = cx_read(MO_VID_INTSTAT);
  1273. mask = cx_read(MO_VID_INTMSK);
  1274. if (0 == (status & mask))
  1275. return;
  1276. cx_write(MO_VID_INTSTAT, status);
  1277. if (irq_debug || (status & mask & ~0xff))
  1278. cx88_print_irqbits(core->name, "irq vid",
  1279. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1280. status, mask);
  1281. /* risc op code error */
  1282. if (status & (1 << 16)) {
  1283. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1284. cx_clear(MO_VID_DMACNTRL, 0x11);
  1285. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1286. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1287. }
  1288. /* risc1 y */
  1289. if (status & 0x01) {
  1290. spin_lock(&dev->slock);
  1291. count = cx_read(MO_VIDY_GPCNT);
  1292. cx88_wakeup(core, &dev->vidq, count);
  1293. spin_unlock(&dev->slock);
  1294. }
  1295. /* risc1 vbi */
  1296. if (status & 0x08) {
  1297. spin_lock(&dev->slock);
  1298. count = cx_read(MO_VBI_GPCNT);
  1299. cx88_wakeup(core, &dev->vbiq, count);
  1300. spin_unlock(&dev->slock);
  1301. }
  1302. /* risc2 y */
  1303. if (status & 0x10) {
  1304. dprintk(2,"stopper video\n");
  1305. spin_lock(&dev->slock);
  1306. restart_video_queue(dev,&dev->vidq);
  1307. spin_unlock(&dev->slock);
  1308. }
  1309. /* risc2 vbi */
  1310. if (status & 0x80) {
  1311. dprintk(2,"stopper vbi\n");
  1312. spin_lock(&dev->slock);
  1313. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1314. spin_unlock(&dev->slock);
  1315. }
  1316. }
  1317. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1318. {
  1319. struct cx8800_dev *dev = dev_id;
  1320. struct cx88_core *core = dev->core;
  1321. u32 status;
  1322. int loop, handled = 0;
  1323. for (loop = 0; loop < 10; loop++) {
  1324. status = cx_read(MO_PCI_INTSTAT) &
  1325. (core->pci_irqmask | PCI_INT_VIDINT);
  1326. if (0 == status)
  1327. goto out;
  1328. cx_write(MO_PCI_INTSTAT, status);
  1329. handled = 1;
  1330. if (status & core->pci_irqmask)
  1331. cx88_core_irq(core,status);
  1332. if (status & PCI_INT_VIDINT)
  1333. cx8800_vid_irq(dev);
  1334. }
  1335. if (10 == loop) {
  1336. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1337. core->name);
  1338. cx_write(MO_PCI_INTMSK,0);
  1339. }
  1340. out:
  1341. return IRQ_RETVAL(handled);
  1342. }
  1343. /* ----------------------------------------------------------- */
  1344. /* exported stuff */
  1345. static const struct v4l2_file_operations video_fops =
  1346. {
  1347. .owner = THIS_MODULE,
  1348. .open = video_open,
  1349. .release = video_release,
  1350. .read = video_read,
  1351. .poll = video_poll,
  1352. .mmap = video_mmap,
  1353. .unlocked_ioctl = video_ioctl2,
  1354. };
  1355. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1356. .vidioc_querycap = vidioc_querycap,
  1357. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1358. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1359. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1360. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1361. .vidioc_reqbufs = vidioc_reqbufs,
  1362. .vidioc_querybuf = vidioc_querybuf,
  1363. .vidioc_qbuf = vidioc_qbuf,
  1364. .vidioc_dqbuf = vidioc_dqbuf,
  1365. .vidioc_g_std = vidioc_g_std,
  1366. .vidioc_s_std = vidioc_s_std,
  1367. .vidioc_enum_input = vidioc_enum_input,
  1368. .vidioc_g_input = vidioc_g_input,
  1369. .vidioc_s_input = vidioc_s_input,
  1370. .vidioc_streamon = vidioc_streamon,
  1371. .vidioc_streamoff = vidioc_streamoff,
  1372. .vidioc_g_tuner = vidioc_g_tuner,
  1373. .vidioc_s_tuner = vidioc_s_tuner,
  1374. .vidioc_g_frequency = vidioc_g_frequency,
  1375. .vidioc_s_frequency = vidioc_s_frequency,
  1376. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1377. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1378. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1379. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1380. .vidioc_g_register = vidioc_g_register,
  1381. .vidioc_s_register = vidioc_s_register,
  1382. #endif
  1383. };
  1384. static const struct video_device cx8800_video_template = {
  1385. .name = "cx8800-video",
  1386. .fops = &video_fops,
  1387. .ioctl_ops = &video_ioctl_ops,
  1388. .tvnorms = CX88_NORMS,
  1389. };
  1390. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  1391. .vidioc_querycap = vidioc_querycap,
  1392. .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
  1393. .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
  1394. .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
  1395. .vidioc_reqbufs = vidioc_reqbufs,
  1396. .vidioc_querybuf = vidioc_querybuf,
  1397. .vidioc_qbuf = vidioc_qbuf,
  1398. .vidioc_dqbuf = vidioc_dqbuf,
  1399. .vidioc_g_std = vidioc_g_std,
  1400. .vidioc_s_std = vidioc_s_std,
  1401. .vidioc_enum_input = vidioc_enum_input,
  1402. .vidioc_g_input = vidioc_g_input,
  1403. .vidioc_s_input = vidioc_s_input,
  1404. .vidioc_streamon = vidioc_streamon,
  1405. .vidioc_streamoff = vidioc_streamoff,
  1406. .vidioc_g_tuner = vidioc_g_tuner,
  1407. .vidioc_s_tuner = vidioc_s_tuner,
  1408. .vidioc_g_frequency = vidioc_g_frequency,
  1409. .vidioc_s_frequency = vidioc_s_frequency,
  1410. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1411. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1412. .vidioc_g_register = vidioc_g_register,
  1413. .vidioc_s_register = vidioc_s_register,
  1414. #endif
  1415. };
  1416. static const struct video_device cx8800_vbi_template = {
  1417. .name = "cx8800-vbi",
  1418. .fops = &video_fops,
  1419. .ioctl_ops = &vbi_ioctl_ops,
  1420. .tvnorms = CX88_NORMS,
  1421. };
  1422. static const struct v4l2_file_operations radio_fops =
  1423. {
  1424. .owner = THIS_MODULE,
  1425. .open = video_open,
  1426. .poll = v4l2_ctrl_poll,
  1427. .release = video_release,
  1428. .unlocked_ioctl = video_ioctl2,
  1429. };
  1430. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1431. .vidioc_querycap = vidioc_querycap,
  1432. .vidioc_g_tuner = radio_g_tuner,
  1433. .vidioc_s_tuner = radio_s_tuner,
  1434. .vidioc_g_frequency = vidioc_g_frequency,
  1435. .vidioc_s_frequency = vidioc_s_frequency,
  1436. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1437. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1438. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1439. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1440. .vidioc_g_register = vidioc_g_register,
  1441. .vidioc_s_register = vidioc_s_register,
  1442. #endif
  1443. };
  1444. static const struct video_device cx8800_radio_template = {
  1445. .name = "cx8800-radio",
  1446. .fops = &radio_fops,
  1447. .ioctl_ops = &radio_ioctl_ops,
  1448. };
  1449. static const struct v4l2_ctrl_ops cx8800_ctrl_vid_ops = {
  1450. .s_ctrl = cx8800_s_vid_ctrl,
  1451. };
  1452. static const struct v4l2_ctrl_ops cx8800_ctrl_aud_ops = {
  1453. .s_ctrl = cx8800_s_aud_ctrl,
  1454. };
  1455. /* ----------------------------------------------------------- */
  1456. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1457. {
  1458. if (dev->radio_dev) {
  1459. if (video_is_registered(dev->radio_dev))
  1460. video_unregister_device(dev->radio_dev);
  1461. else
  1462. video_device_release(dev->radio_dev);
  1463. dev->radio_dev = NULL;
  1464. }
  1465. if (dev->vbi_dev) {
  1466. if (video_is_registered(dev->vbi_dev))
  1467. video_unregister_device(dev->vbi_dev);
  1468. else
  1469. video_device_release(dev->vbi_dev);
  1470. dev->vbi_dev = NULL;
  1471. }
  1472. if (dev->video_dev) {
  1473. if (video_is_registered(dev->video_dev))
  1474. video_unregister_device(dev->video_dev);
  1475. else
  1476. video_device_release(dev->video_dev);
  1477. dev->video_dev = NULL;
  1478. }
  1479. }
  1480. static int cx8800_initdev(struct pci_dev *pci_dev,
  1481. const struct pci_device_id *pci_id)
  1482. {
  1483. struct cx8800_dev *dev;
  1484. struct cx88_core *core;
  1485. int err;
  1486. int i;
  1487. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1488. if (NULL == dev)
  1489. return -ENOMEM;
  1490. /* pci init */
  1491. dev->pci = pci_dev;
  1492. if (pci_enable_device(pci_dev)) {
  1493. err = -EIO;
  1494. goto fail_free;
  1495. }
  1496. core = cx88_core_get(dev->pci);
  1497. if (NULL == core) {
  1498. err = -EINVAL;
  1499. goto fail_free;
  1500. }
  1501. dev->core = core;
  1502. /* print pci info */
  1503. dev->pci_rev = pci_dev->revision;
  1504. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1505. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1506. "latency: %d, mmio: 0x%llx\n", core->name,
  1507. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1508. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1509. pci_set_master(pci_dev);
  1510. if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
  1511. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1512. err = -EIO;
  1513. goto fail_core;
  1514. }
  1515. /* initialize driver struct */
  1516. spin_lock_init(&dev->slock);
  1517. core->tvnorm = V4L2_STD_NTSC_M;
  1518. /* init video dma queues */
  1519. INIT_LIST_HEAD(&dev->vidq.active);
  1520. INIT_LIST_HEAD(&dev->vidq.queued);
  1521. dev->vidq.timeout.function = cx8800_vid_timeout;
  1522. dev->vidq.timeout.data = (unsigned long)dev;
  1523. init_timer(&dev->vidq.timeout);
  1524. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1525. MO_VID_DMACNTRL,0x11,0x00);
  1526. /* init vbi dma queues */
  1527. INIT_LIST_HEAD(&dev->vbiq.active);
  1528. INIT_LIST_HEAD(&dev->vbiq.queued);
  1529. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1530. dev->vbiq.timeout.data = (unsigned long)dev;
  1531. init_timer(&dev->vbiq.timeout);
  1532. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1533. MO_VID_DMACNTRL,0x88,0x00);
  1534. /* get irq */
  1535. err = request_irq(pci_dev->irq, cx8800_irq,
  1536. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1537. if (err < 0) {
  1538. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1539. core->name,pci_dev->irq);
  1540. goto fail_core;
  1541. }
  1542. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1543. for (i = 0; i < CX8800_AUD_CTLS; i++) {
  1544. const struct cx88_ctrl *cc = &cx8800_aud_ctls[i];
  1545. struct v4l2_ctrl *vc;
  1546. vc = v4l2_ctrl_new_std(&core->audio_hdl, &cx8800_ctrl_aud_ops,
  1547. cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
  1548. if (vc == NULL) {
  1549. err = core->audio_hdl.error;
  1550. goto fail_core;
  1551. }
  1552. vc->priv = (void *)cc;
  1553. }
  1554. for (i = 0; i < CX8800_VID_CTLS; i++) {
  1555. const struct cx88_ctrl *cc = &cx8800_vid_ctls[i];
  1556. struct v4l2_ctrl *vc;
  1557. vc = v4l2_ctrl_new_std(&core->video_hdl, &cx8800_ctrl_vid_ops,
  1558. cc->id, cc->minimum, cc->maximum, cc->step, cc->default_value);
  1559. if (vc == NULL) {
  1560. err = core->video_hdl.error;
  1561. goto fail_core;
  1562. }
  1563. vc->priv = (void *)cc;
  1564. if (vc->id == V4L2_CID_CHROMA_AGC)
  1565. core->chroma_agc = vc;
  1566. }
  1567. v4l2_ctrl_add_handler(&core->video_hdl, &core->audio_hdl, NULL);
  1568. /* load and configure helper modules */
  1569. if (core->board.audio_chip == V4L2_IDENT_WM8775) {
  1570. struct i2c_board_info wm8775_info = {
  1571. .type = "wm8775",
  1572. .addr = 0x36 >> 1,
  1573. .platform_data = &core->wm8775_data,
  1574. };
  1575. struct v4l2_subdev *sd;
  1576. if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
  1577. core->wm8775_data.is_nova_s = true;
  1578. else
  1579. core->wm8775_data.is_nova_s = false;
  1580. sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
  1581. &wm8775_info, NULL);
  1582. if (sd != NULL) {
  1583. core->sd_wm8775 = sd;
  1584. sd->grp_id = WM8775_GID;
  1585. }
  1586. }
  1587. if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
  1588. /* This probes for a tda9874 as is used on some
  1589. Pixelview Ultra boards. */
  1590. v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
  1591. "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
  1592. }
  1593. switch (core->boardnr) {
  1594. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1595. case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
  1596. static const struct i2c_board_info rtc_info = {
  1597. I2C_BOARD_INFO("isl1208", 0x6f)
  1598. };
  1599. request_module("rtc-isl1208");
  1600. core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
  1601. }
  1602. /* break intentionally omitted */
  1603. case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
  1604. request_module("ir-kbd-i2c");
  1605. }
  1606. /* Sets device info at pci_dev */
  1607. pci_set_drvdata(pci_dev, dev);
  1608. dev->width = 320;
  1609. dev->height = 240;
  1610. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1611. /* initial device configuration */
  1612. mutex_lock(&core->lock);
  1613. cx88_set_tvnorm(core, core->tvnorm);
  1614. v4l2_ctrl_handler_setup(&core->video_hdl);
  1615. v4l2_ctrl_handler_setup(&core->audio_hdl);
  1616. cx88_video_mux(core, 0);
  1617. /* register v4l devices */
  1618. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1619. &cx8800_video_template,"video");
  1620. video_set_drvdata(dev->video_dev, dev);
  1621. dev->video_dev->ctrl_handler = &core->video_hdl;
  1622. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1623. video_nr[core->nr]);
  1624. if (err < 0) {
  1625. printk(KERN_ERR "%s/0: can't register video device\n",
  1626. core->name);
  1627. goto fail_unreg;
  1628. }
  1629. printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
  1630. core->name, video_device_node_name(dev->video_dev));
  1631. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1632. video_set_drvdata(dev->vbi_dev, dev);
  1633. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1634. vbi_nr[core->nr]);
  1635. if (err < 0) {
  1636. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1637. core->name);
  1638. goto fail_unreg;
  1639. }
  1640. printk(KERN_INFO "%s/0: registered device %s\n",
  1641. core->name, video_device_node_name(dev->vbi_dev));
  1642. if (core->board.radio.type == CX88_RADIO) {
  1643. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1644. &cx8800_radio_template,"radio");
  1645. video_set_drvdata(dev->radio_dev, dev);
  1646. dev->radio_dev->ctrl_handler = &core->audio_hdl;
  1647. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1648. radio_nr[core->nr]);
  1649. if (err < 0) {
  1650. printk(KERN_ERR "%s/0: can't register radio device\n",
  1651. core->name);
  1652. goto fail_unreg;
  1653. }
  1654. printk(KERN_INFO "%s/0: registered device %s\n",
  1655. core->name, video_device_node_name(dev->radio_dev));
  1656. }
  1657. /* start tvaudio thread */
  1658. if (core->board.tuner_type != TUNER_ABSENT) {
  1659. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1660. if (IS_ERR(core->kthread)) {
  1661. err = PTR_ERR(core->kthread);
  1662. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1663. core->name, err);
  1664. }
  1665. }
  1666. mutex_unlock(&core->lock);
  1667. return 0;
  1668. fail_unreg:
  1669. cx8800_unregister_video(dev);
  1670. free_irq(pci_dev->irq, dev);
  1671. mutex_unlock(&core->lock);
  1672. fail_core:
  1673. cx88_core_put(core,dev->pci);
  1674. fail_free:
  1675. kfree(dev);
  1676. return err;
  1677. }
  1678. static void cx8800_finidev(struct pci_dev *pci_dev)
  1679. {
  1680. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1681. struct cx88_core *core = dev->core;
  1682. /* stop thread */
  1683. if (core->kthread) {
  1684. kthread_stop(core->kthread);
  1685. core->kthread = NULL;
  1686. }
  1687. if (core->ir)
  1688. cx88_ir_stop(core);
  1689. cx88_shutdown(core); /* FIXME */
  1690. pci_disable_device(pci_dev);
  1691. /* unregister stuff */
  1692. free_irq(pci_dev->irq, dev);
  1693. cx8800_unregister_video(dev);
  1694. pci_set_drvdata(pci_dev, NULL);
  1695. /* free memory */
  1696. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1697. cx88_core_put(core,dev->pci);
  1698. kfree(dev);
  1699. }
  1700. #ifdef CONFIG_PM
  1701. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1702. {
  1703. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1704. struct cx88_core *core = dev->core;
  1705. unsigned long flags;
  1706. /* stop video+vbi capture */
  1707. spin_lock_irqsave(&dev->slock, flags);
  1708. if (!list_empty(&dev->vidq.active)) {
  1709. printk("%s/0: suspend video\n", core->name);
  1710. stop_video_dma(dev);
  1711. del_timer(&dev->vidq.timeout);
  1712. }
  1713. if (!list_empty(&dev->vbiq.active)) {
  1714. printk("%s/0: suspend vbi\n", core->name);
  1715. cx8800_stop_vbi_dma(dev);
  1716. del_timer(&dev->vbiq.timeout);
  1717. }
  1718. spin_unlock_irqrestore(&dev->slock, flags);
  1719. if (core->ir)
  1720. cx88_ir_stop(core);
  1721. /* FIXME -- shutdown device */
  1722. cx88_shutdown(core);
  1723. pci_save_state(pci_dev);
  1724. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1725. pci_disable_device(pci_dev);
  1726. dev->state.disabled = 1;
  1727. }
  1728. return 0;
  1729. }
  1730. static int cx8800_resume(struct pci_dev *pci_dev)
  1731. {
  1732. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1733. struct cx88_core *core = dev->core;
  1734. unsigned long flags;
  1735. int err;
  1736. if (dev->state.disabled) {
  1737. err=pci_enable_device(pci_dev);
  1738. if (err) {
  1739. printk(KERN_ERR "%s/0: can't enable device\n",
  1740. core->name);
  1741. return err;
  1742. }
  1743. dev->state.disabled = 0;
  1744. }
  1745. err= pci_set_power_state(pci_dev, PCI_D0);
  1746. if (err) {
  1747. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1748. pci_disable_device(pci_dev);
  1749. dev->state.disabled = 1;
  1750. return err;
  1751. }
  1752. pci_restore_state(pci_dev);
  1753. /* FIXME: re-initialize hardware */
  1754. cx88_reset(core);
  1755. if (core->ir)
  1756. cx88_ir_start(core);
  1757. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1758. /* restart video+vbi capture */
  1759. spin_lock_irqsave(&dev->slock, flags);
  1760. if (!list_empty(&dev->vidq.active)) {
  1761. printk("%s/0: resume video\n", core->name);
  1762. restart_video_queue(dev,&dev->vidq);
  1763. }
  1764. if (!list_empty(&dev->vbiq.active)) {
  1765. printk("%s/0: resume vbi\n", core->name);
  1766. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1767. }
  1768. spin_unlock_irqrestore(&dev->slock, flags);
  1769. return 0;
  1770. }
  1771. #endif
  1772. /* ----------------------------------------------------------- */
  1773. static const struct pci_device_id cx8800_pci_tbl[] = {
  1774. {
  1775. .vendor = 0x14f1,
  1776. .device = 0x8800,
  1777. .subvendor = PCI_ANY_ID,
  1778. .subdevice = PCI_ANY_ID,
  1779. },{
  1780. /* --- end of list --- */
  1781. }
  1782. };
  1783. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1784. static struct pci_driver cx8800_pci_driver = {
  1785. .name = "cx8800",
  1786. .id_table = cx8800_pci_tbl,
  1787. .probe = cx8800_initdev,
  1788. .remove = cx8800_finidev,
  1789. #ifdef CONFIG_PM
  1790. .suspend = cx8800_suspend,
  1791. .resume = cx8800_resume,
  1792. #endif
  1793. };
  1794. static int __init cx8800_init(void)
  1795. {
  1796. printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %s loaded\n",
  1797. CX88_VERSION);
  1798. return pci_register_driver(&cx8800_pci_driver);
  1799. }
  1800. static void __exit cx8800_fini(void)
  1801. {
  1802. pci_unregister_driver(&cx8800_pci_driver);
  1803. }
  1804. module_init(cx8800_init);
  1805. module_exit(cx8800_fini);