cx88-video.c 55 KB

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