cx88-video.c 55 KB

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