cx88-video.c 53 KB

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