cx88-video.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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. list_for_each_entry(h, &cx8800_devlist, devlist) {
  688. if (h->video_dev->minor == minor) {
  689. dev = h;
  690. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  691. }
  692. if (h->vbi_dev->minor == minor) {
  693. dev = h;
  694. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  695. }
  696. if (h->radio_dev &&
  697. h->radio_dev->minor == minor) {
  698. radio = 1;
  699. dev = h;
  700. }
  701. }
  702. if (NULL == dev)
  703. return -ENODEV;
  704. core = dev->core;
  705. dprintk(1,"open minor=%d radio=%d type=%s\n",
  706. minor,radio,v4l2_type_names[type]);
  707. /* allocate + initialize per filehandle data */
  708. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  709. if (NULL == fh)
  710. return -ENOMEM;
  711. file->private_data = fh;
  712. fh->dev = dev;
  713. fh->radio = radio;
  714. fh->type = type;
  715. fh->width = 320;
  716. fh->height = 240;
  717. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  718. videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
  719. &dev->pci->dev, &dev->slock,
  720. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  721. V4L2_FIELD_INTERLACED,
  722. sizeof(struct cx88_buffer),
  723. fh);
  724. videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
  725. &dev->pci->dev, &dev->slock,
  726. V4L2_BUF_TYPE_VBI_CAPTURE,
  727. V4L2_FIELD_SEQ_TB,
  728. sizeof(struct cx88_buffer),
  729. fh);
  730. if (fh->radio) {
  731. dprintk(1,"video_open: setting radio device\n");
  732. cx_write(MO_GP3_IO, core->board.radio.gpio3);
  733. cx_write(MO_GP0_IO, core->board.radio.gpio0);
  734. cx_write(MO_GP1_IO, core->board.radio.gpio1);
  735. cx_write(MO_GP2_IO, core->board.radio.gpio2);
  736. core->tvaudio = WW_FM;
  737. cx88_set_tvaudio(core);
  738. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  739. cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
  740. }
  741. return 0;
  742. }
  743. static ssize_t
  744. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  745. {
  746. struct cx8800_fh *fh = file->private_data;
  747. switch (fh->type) {
  748. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  749. if (res_locked(fh->dev,RESOURCE_VIDEO))
  750. return -EBUSY;
  751. return videobuf_read_one(&fh->vidq, data, count, ppos,
  752. file->f_flags & O_NONBLOCK);
  753. case V4L2_BUF_TYPE_VBI_CAPTURE:
  754. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  755. return -EBUSY;
  756. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  757. file->f_flags & O_NONBLOCK);
  758. default:
  759. BUG();
  760. return 0;
  761. }
  762. }
  763. static unsigned int
  764. video_poll(struct file *file, struct poll_table_struct *wait)
  765. {
  766. struct cx8800_fh *fh = file->private_data;
  767. struct cx88_buffer *buf;
  768. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  769. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  770. return POLLERR;
  771. return videobuf_poll_stream(file, &fh->vbiq, wait);
  772. }
  773. if (res_check(fh,RESOURCE_VIDEO)) {
  774. /* streaming capture */
  775. if (list_empty(&fh->vidq.stream))
  776. return POLLERR;
  777. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  778. } else {
  779. /* read() capture */
  780. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  781. if (NULL == buf)
  782. return POLLERR;
  783. }
  784. poll_wait(file, &buf->vb.done, wait);
  785. if (buf->vb.state == VIDEOBUF_DONE ||
  786. buf->vb.state == VIDEOBUF_ERROR)
  787. return POLLIN|POLLRDNORM;
  788. return 0;
  789. }
  790. static int video_release(struct inode *inode, struct file *file)
  791. {
  792. struct cx8800_fh *fh = file->private_data;
  793. struct cx8800_dev *dev = fh->dev;
  794. /* turn off overlay */
  795. if (res_check(fh, RESOURCE_OVERLAY)) {
  796. /* FIXME */
  797. res_free(dev,fh,RESOURCE_OVERLAY);
  798. }
  799. /* stop video capture */
  800. if (res_check(fh, RESOURCE_VIDEO)) {
  801. videobuf_queue_cancel(&fh->vidq);
  802. res_free(dev,fh,RESOURCE_VIDEO);
  803. }
  804. if (fh->vidq.read_buf) {
  805. buffer_release(&fh->vidq,fh->vidq.read_buf);
  806. kfree(fh->vidq.read_buf);
  807. }
  808. /* stop vbi capture */
  809. if (res_check(fh, RESOURCE_VBI)) {
  810. videobuf_stop(&fh->vbiq);
  811. res_free(dev,fh,RESOURCE_VBI);
  812. }
  813. videobuf_mmap_free(&fh->vidq);
  814. videobuf_mmap_free(&fh->vbiq);
  815. file->private_data = NULL;
  816. kfree(fh);
  817. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  818. return 0;
  819. }
  820. static int
  821. video_mmap(struct file *file, struct vm_area_struct * vma)
  822. {
  823. struct cx8800_fh *fh = file->private_data;
  824. return videobuf_mmap_mapper(get_queue(fh), vma);
  825. }
  826. /* ------------------------------------------------------------------ */
  827. /* VIDEO CTRL IOCTLS */
  828. int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
  829. {
  830. struct cx88_ctrl *c = NULL;
  831. u32 value;
  832. int i;
  833. for (i = 0; i < CX8800_CTLS; i++)
  834. if (cx8800_ctls[i].v.id == ctl->id)
  835. c = &cx8800_ctls[i];
  836. if (unlikely(NULL == c))
  837. return -EINVAL;
  838. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  839. switch (ctl->id) {
  840. case V4L2_CID_AUDIO_BALANCE:
  841. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  842. : (0x7f - (value & 0x7f));
  843. break;
  844. case V4L2_CID_AUDIO_VOLUME:
  845. ctl->value = 0x3f - (value & 0x3f);
  846. break;
  847. default:
  848. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  849. break;
  850. }
  851. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  852. ctl->id, c->v.name, ctl->value, c->reg,
  853. value,c->mask, c->sreg ? " [shadowed]" : "");
  854. return 0;
  855. }
  856. EXPORT_SYMBOL(cx88_get_control);
  857. int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
  858. {
  859. struct cx88_ctrl *c = NULL;
  860. u32 value,mask;
  861. int i;
  862. for (i = 0; i < CX8800_CTLS; i++) {
  863. if (cx8800_ctls[i].v.id == ctl->id) {
  864. c = &cx8800_ctls[i];
  865. }
  866. }
  867. if (unlikely(NULL == c))
  868. return -EINVAL;
  869. if (ctl->value < c->v.minimum)
  870. ctl->value = c->v.minimum;
  871. if (ctl->value > c->v.maximum)
  872. ctl->value = c->v.maximum;
  873. mask=c->mask;
  874. switch (ctl->id) {
  875. case V4L2_CID_AUDIO_BALANCE:
  876. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  877. break;
  878. case V4L2_CID_AUDIO_VOLUME:
  879. value = 0x3f - (ctl->value & 0x3f);
  880. break;
  881. case V4L2_CID_SATURATION:
  882. /* special v_sat handling */
  883. value = ((ctl->value - c->off) << c->shift) & c->mask;
  884. if (core->tvnorm & V4L2_STD_SECAM) {
  885. /* For SECAM, both U and V sat should be equal */
  886. value=value<<8|value;
  887. } else {
  888. /* Keeps U Saturation proportional to V Sat */
  889. value=(value*0x5a)/0x7f<<8|value;
  890. }
  891. mask=0xffff;
  892. break;
  893. case V4L2_CID_CHROMA_AGC:
  894. /* Do not allow chroma AGC to be enabled for SECAM */
  895. value = ((ctl->value - c->off) << c->shift) & c->mask;
  896. if (core->tvnorm & V4L2_STD_SECAM && value)
  897. return -EINVAL;
  898. break;
  899. default:
  900. value = ((ctl->value - c->off) << c->shift) & c->mask;
  901. break;
  902. }
  903. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  904. ctl->id, c->v.name, ctl->value, c->reg, value,
  905. mask, c->sreg ? " [shadowed]" : "");
  906. if (c->sreg) {
  907. cx_sandor(c->sreg, c->reg, mask, value);
  908. } else {
  909. cx_andor(c->reg, mask, value);
  910. }
  911. return 0;
  912. }
  913. EXPORT_SYMBOL(cx88_set_control);
  914. static void init_controls(struct cx88_core *core)
  915. {
  916. struct v4l2_control ctrl;
  917. int i;
  918. for (i = 0; i < CX8800_CTLS; i++) {
  919. ctrl.id=cx8800_ctls[i].v.id;
  920. ctrl.value=cx8800_ctls[i].v.default_value;
  921. cx88_set_control(core, &ctrl);
  922. }
  923. }
  924. /* ------------------------------------------------------------------ */
  925. /* VIDEO IOCTLS */
  926. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  927. struct v4l2_format *f)
  928. {
  929. struct cx8800_fh *fh = priv;
  930. f->fmt.pix.width = fh->width;
  931. f->fmt.pix.height = fh->height;
  932. f->fmt.pix.field = fh->vidq.field;
  933. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  934. f->fmt.pix.bytesperline =
  935. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  936. f->fmt.pix.sizeimage =
  937. f->fmt.pix.height * f->fmt.pix.bytesperline;
  938. return 0;
  939. }
  940. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  941. struct v4l2_format *f)
  942. {
  943. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  944. struct cx8800_fmt *fmt;
  945. enum v4l2_field field;
  946. unsigned int maxw, maxh;
  947. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  948. if (NULL == fmt)
  949. return -EINVAL;
  950. field = f->fmt.pix.field;
  951. maxw = norm_maxw(core->tvnorm);
  952. maxh = norm_maxh(core->tvnorm);
  953. if (V4L2_FIELD_ANY == field) {
  954. field = (f->fmt.pix.height > maxh/2)
  955. ? V4L2_FIELD_INTERLACED
  956. : V4L2_FIELD_BOTTOM;
  957. }
  958. switch (field) {
  959. case V4L2_FIELD_TOP:
  960. case V4L2_FIELD_BOTTOM:
  961. maxh = maxh / 2;
  962. break;
  963. case V4L2_FIELD_INTERLACED:
  964. break;
  965. default:
  966. return -EINVAL;
  967. }
  968. f->fmt.pix.field = field;
  969. if (f->fmt.pix.height < 32)
  970. f->fmt.pix.height = 32;
  971. if (f->fmt.pix.height > maxh)
  972. f->fmt.pix.height = maxh;
  973. if (f->fmt.pix.width < 48)
  974. f->fmt.pix.width = 48;
  975. if (f->fmt.pix.width > maxw)
  976. f->fmt.pix.width = maxw;
  977. f->fmt.pix.width &= ~0x03;
  978. f->fmt.pix.bytesperline =
  979. (f->fmt.pix.width * fmt->depth) >> 3;
  980. f->fmt.pix.sizeimage =
  981. f->fmt.pix.height * f->fmt.pix.bytesperline;
  982. return 0;
  983. }
  984. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  985. struct v4l2_format *f)
  986. {
  987. struct cx8800_fh *fh = priv;
  988. int err = vidioc_try_fmt_vid_cap (file,priv,f);
  989. if (0 != err)
  990. return err;
  991. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  992. fh->width = f->fmt.pix.width;
  993. fh->height = f->fmt.pix.height;
  994. fh->vidq.field = f->fmt.pix.field;
  995. return 0;
  996. }
  997. static int vidioc_querycap (struct file *file, void *priv,
  998. struct v4l2_capability *cap)
  999. {
  1000. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1001. struct cx88_core *core = dev->core;
  1002. strcpy(cap->driver, "cx8800");
  1003. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  1004. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  1005. cap->version = CX88_VERSION_CODE;
  1006. cap->capabilities =
  1007. V4L2_CAP_VIDEO_CAPTURE |
  1008. V4L2_CAP_READWRITE |
  1009. V4L2_CAP_STREAMING |
  1010. V4L2_CAP_VBI_CAPTURE;
  1011. if (UNSET != core->board.tuner_type)
  1012. cap->capabilities |= V4L2_CAP_TUNER;
  1013. return 0;
  1014. }
  1015. static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
  1016. struct v4l2_fmtdesc *f)
  1017. {
  1018. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  1019. return -EINVAL;
  1020. strlcpy(f->description,formats[f->index].name,sizeof(f->description));
  1021. f->pixelformat = formats[f->index].fourcc;
  1022. return 0;
  1023. }
  1024. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1025. static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
  1026. {
  1027. struct cx8800_fh *fh = priv;
  1028. return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
  1029. }
  1030. #endif
  1031. static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
  1032. {
  1033. struct cx8800_fh *fh = priv;
  1034. return (videobuf_reqbufs(get_queue(fh), p));
  1035. }
  1036. static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1037. {
  1038. struct cx8800_fh *fh = priv;
  1039. return (videobuf_querybuf(get_queue(fh), p));
  1040. }
  1041. static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1042. {
  1043. struct cx8800_fh *fh = priv;
  1044. return (videobuf_qbuf(get_queue(fh), p));
  1045. }
  1046. static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
  1047. {
  1048. struct cx8800_fh *fh = priv;
  1049. return (videobuf_dqbuf(get_queue(fh), p,
  1050. file->f_flags & O_NONBLOCK));
  1051. }
  1052. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  1053. {
  1054. struct cx8800_fh *fh = priv;
  1055. struct cx8800_dev *dev = fh->dev;
  1056. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  1057. return -EINVAL;
  1058. if (unlikely(i != fh->type))
  1059. return -EINVAL;
  1060. if (unlikely(!res_get(dev,fh,get_ressource(fh))))
  1061. return -EBUSY;
  1062. return videobuf_streamon(get_queue(fh));
  1063. }
  1064. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1065. {
  1066. struct cx8800_fh *fh = priv;
  1067. struct cx8800_dev *dev = fh->dev;
  1068. int err, res;
  1069. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1070. return -EINVAL;
  1071. if (i != fh->type)
  1072. return -EINVAL;
  1073. res = get_ressource(fh);
  1074. err = videobuf_streamoff(get_queue(fh));
  1075. if (err < 0)
  1076. return err;
  1077. res_free(dev,fh,res);
  1078. return 0;
  1079. }
  1080. static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
  1081. {
  1082. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1083. mutex_lock(&core->lock);
  1084. cx88_set_tvnorm(core,*tvnorms);
  1085. mutex_unlock(&core->lock);
  1086. return 0;
  1087. }
  1088. /* only one input in this sample driver */
  1089. int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
  1090. {
  1091. static const char *iname[] = {
  1092. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1093. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1094. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1095. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1096. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1097. [ CX88_VMUX_TELEVISION ] = "Television",
  1098. [ CX88_VMUX_CABLE ] = "Cable TV",
  1099. [ CX88_VMUX_DVB ] = "DVB",
  1100. [ CX88_VMUX_DEBUG ] = "for debug only",
  1101. };
  1102. unsigned int n;
  1103. n = i->index;
  1104. if (n >= 4)
  1105. return -EINVAL;
  1106. if (0 == INPUT(n).type)
  1107. return -EINVAL;
  1108. memset(i,0,sizeof(*i));
  1109. i->index = n;
  1110. i->type = V4L2_INPUT_TYPE_CAMERA;
  1111. strcpy(i->name,iname[INPUT(n).type]);
  1112. if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
  1113. (CX88_VMUX_CABLE == INPUT(n).type))
  1114. i->type = V4L2_INPUT_TYPE_TUNER;
  1115. i->std = CX88_NORMS;
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL(cx88_enum_input);
  1119. static int vidioc_enum_input (struct file *file, void *priv,
  1120. struct v4l2_input *i)
  1121. {
  1122. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1123. return cx88_enum_input (core,i);
  1124. }
  1125. static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
  1126. {
  1127. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1128. *i = core->input;
  1129. return 0;
  1130. }
  1131. static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
  1132. {
  1133. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1134. if (i >= 4)
  1135. return -EINVAL;
  1136. mutex_lock(&core->lock);
  1137. cx88_newstation(core);
  1138. cx88_video_mux(core,i);
  1139. mutex_unlock(&core->lock);
  1140. return 0;
  1141. }
  1142. static int vidioc_queryctrl (struct file *file, void *priv,
  1143. struct v4l2_queryctrl *qctrl)
  1144. {
  1145. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1146. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1147. if (unlikely(qctrl->id == 0))
  1148. return -EINVAL;
  1149. return cx8800_ctrl_query(core, qctrl);
  1150. }
  1151. static int vidioc_g_ctrl (struct file *file, void *priv,
  1152. struct v4l2_control *ctl)
  1153. {
  1154. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1155. return
  1156. cx88_get_control(core,ctl);
  1157. }
  1158. static int vidioc_s_ctrl (struct file *file, void *priv,
  1159. struct v4l2_control *ctl)
  1160. {
  1161. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1162. return
  1163. cx88_set_control(core,ctl);
  1164. }
  1165. static int vidioc_g_tuner (struct file *file, void *priv,
  1166. struct v4l2_tuner *t)
  1167. {
  1168. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1169. u32 reg;
  1170. if (unlikely(UNSET == core->board.tuner_type))
  1171. return -EINVAL;
  1172. if (0 != t->index)
  1173. return -EINVAL;
  1174. strcpy(t->name, "Television");
  1175. t->type = V4L2_TUNER_ANALOG_TV;
  1176. t->capability = V4L2_TUNER_CAP_NORM;
  1177. t->rangehigh = 0xffffffffUL;
  1178. cx88_get_stereo(core ,t);
  1179. reg = cx_read(MO_DEVICE_STATUS);
  1180. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1181. return 0;
  1182. }
  1183. static int vidioc_s_tuner (struct file *file, void *priv,
  1184. struct v4l2_tuner *t)
  1185. {
  1186. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1187. if (UNSET == core->board.tuner_type)
  1188. return -EINVAL;
  1189. if (0 != t->index)
  1190. return -EINVAL;
  1191. cx88_set_stereo(core, t->audmode, 1);
  1192. return 0;
  1193. }
  1194. static int vidioc_g_frequency (struct file *file, void *priv,
  1195. struct v4l2_frequency *f)
  1196. {
  1197. struct cx8800_fh *fh = priv;
  1198. struct cx88_core *core = fh->dev->core;
  1199. if (unlikely(UNSET == core->board.tuner_type))
  1200. return -EINVAL;
  1201. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1202. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1203. f->frequency = core->freq;
  1204. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1205. return 0;
  1206. }
  1207. int cx88_set_freq (struct cx88_core *core,
  1208. struct v4l2_frequency *f)
  1209. {
  1210. if (unlikely(UNSET == core->board.tuner_type))
  1211. return -EINVAL;
  1212. if (unlikely(f->tuner != 0))
  1213. return -EINVAL;
  1214. mutex_lock(&core->lock);
  1215. core->freq = f->frequency;
  1216. cx88_newstation(core);
  1217. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1218. /* When changing channels it is required to reset TVAUDIO */
  1219. msleep (10);
  1220. cx88_set_tvaudio(core);
  1221. mutex_unlock(&core->lock);
  1222. return 0;
  1223. }
  1224. EXPORT_SYMBOL(cx88_set_freq);
  1225. static int vidioc_s_frequency (struct file *file, void *priv,
  1226. struct v4l2_frequency *f)
  1227. {
  1228. struct cx8800_fh *fh = priv;
  1229. struct cx88_core *core = fh->dev->core;
  1230. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1231. return -EINVAL;
  1232. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1233. return -EINVAL;
  1234. return
  1235. cx88_set_freq (core,f);
  1236. }
  1237. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1238. static int vidioc_g_register (struct file *file, void *fh,
  1239. struct v4l2_register *reg)
  1240. {
  1241. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1242. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1243. return -EINVAL;
  1244. /* cx2388x has a 24-bit register space */
  1245. reg->val = cx_read(reg->reg&0xffffff);
  1246. return 0;
  1247. }
  1248. static int vidioc_s_register (struct file *file, void *fh,
  1249. struct v4l2_register *reg)
  1250. {
  1251. struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
  1252. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1253. return -EINVAL;
  1254. cx_write(reg->reg&0xffffff, reg->val);
  1255. return 0;
  1256. }
  1257. #endif
  1258. /* ----------------------------------------------------------- */
  1259. /* RADIO ESPECIFIC IOCTLS */
  1260. /* ----------------------------------------------------------- */
  1261. static int radio_querycap (struct file *file, void *priv,
  1262. struct v4l2_capability *cap)
  1263. {
  1264. struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
  1265. struct cx88_core *core = dev->core;
  1266. strcpy(cap->driver, "cx8800");
  1267. strlcpy(cap->card, core->board.name, sizeof(cap->card));
  1268. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1269. cap->version = CX88_VERSION_CODE;
  1270. cap->capabilities = V4L2_CAP_TUNER;
  1271. return 0;
  1272. }
  1273. static int radio_g_tuner (struct file *file, void *priv,
  1274. struct v4l2_tuner *t)
  1275. {
  1276. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1277. if (unlikely(t->index > 0))
  1278. return -EINVAL;
  1279. strcpy(t->name, "Radio");
  1280. t->type = V4L2_TUNER_RADIO;
  1281. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1282. return 0;
  1283. }
  1284. static int radio_enum_input (struct file *file, void *priv,
  1285. struct v4l2_input *i)
  1286. {
  1287. if (i->index != 0)
  1288. return -EINVAL;
  1289. strcpy(i->name,"Radio");
  1290. i->type = V4L2_INPUT_TYPE_TUNER;
  1291. return 0;
  1292. }
  1293. static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
  1294. {
  1295. if (unlikely(a->index))
  1296. return -EINVAL;
  1297. memset(a,0,sizeof(*a));
  1298. strcpy(a->name,"Radio");
  1299. return 0;
  1300. }
  1301. /* FIXME: Should add a standard for radio */
  1302. static int radio_s_tuner (struct file *file, void *priv,
  1303. struct v4l2_tuner *t)
  1304. {
  1305. struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
  1306. if (0 != t->index)
  1307. return -EINVAL;
  1308. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1309. return 0;
  1310. }
  1311. static int radio_s_audio (struct file *file, void *fh,
  1312. struct v4l2_audio *a)
  1313. {
  1314. return 0;
  1315. }
  1316. static int radio_s_input (struct file *file, void *fh, unsigned int i)
  1317. {
  1318. return 0;
  1319. }
  1320. static int radio_queryctrl (struct file *file, void *priv,
  1321. struct v4l2_queryctrl *c)
  1322. {
  1323. int i;
  1324. if (c->id < V4L2_CID_BASE ||
  1325. c->id >= V4L2_CID_LASTP1)
  1326. return -EINVAL;
  1327. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1328. for (i = 0; i < CX8800_CTLS; i++)
  1329. if (cx8800_ctls[i].v.id == c->id)
  1330. break;
  1331. *c = cx8800_ctls[i].v;
  1332. } else
  1333. *c = no_ctl;
  1334. return 0;
  1335. }
  1336. /* ----------------------------------------------------------- */
  1337. static void cx8800_vid_timeout(unsigned long data)
  1338. {
  1339. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1340. struct cx88_core *core = dev->core;
  1341. struct cx88_dmaqueue *q = &dev->vidq;
  1342. struct cx88_buffer *buf;
  1343. unsigned long flags;
  1344. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1345. cx_clear(MO_VID_DMACNTRL, 0x11);
  1346. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1347. spin_lock_irqsave(&dev->slock,flags);
  1348. while (!list_empty(&q->active)) {
  1349. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1350. list_del(&buf->vb.queue);
  1351. buf->vb.state = VIDEOBUF_ERROR;
  1352. wake_up(&buf->vb.done);
  1353. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1354. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1355. }
  1356. restart_video_queue(dev,q);
  1357. spin_unlock_irqrestore(&dev->slock,flags);
  1358. }
  1359. static char *cx88_vid_irqs[32] = {
  1360. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1361. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1362. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1363. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1364. "opc_err", "par_err", "rip_err", "pci_abort",
  1365. };
  1366. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1367. {
  1368. struct cx88_core *core = dev->core;
  1369. u32 status, mask, count;
  1370. status = cx_read(MO_VID_INTSTAT);
  1371. mask = cx_read(MO_VID_INTMSK);
  1372. if (0 == (status & mask))
  1373. return;
  1374. cx_write(MO_VID_INTSTAT, status);
  1375. if (irq_debug || (status & mask & ~0xff))
  1376. cx88_print_irqbits(core->name, "irq vid",
  1377. cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
  1378. status, mask);
  1379. /* risc op code error */
  1380. if (status & (1 << 16)) {
  1381. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1382. cx_clear(MO_VID_DMACNTRL, 0x11);
  1383. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1384. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1385. }
  1386. /* risc1 y */
  1387. if (status & 0x01) {
  1388. spin_lock(&dev->slock);
  1389. count = cx_read(MO_VIDY_GPCNT);
  1390. cx88_wakeup(core, &dev->vidq, count);
  1391. spin_unlock(&dev->slock);
  1392. }
  1393. /* risc1 vbi */
  1394. if (status & 0x08) {
  1395. spin_lock(&dev->slock);
  1396. count = cx_read(MO_VBI_GPCNT);
  1397. cx88_wakeup(core, &dev->vbiq, count);
  1398. spin_unlock(&dev->slock);
  1399. }
  1400. /* risc2 y */
  1401. if (status & 0x10) {
  1402. dprintk(2,"stopper video\n");
  1403. spin_lock(&dev->slock);
  1404. restart_video_queue(dev,&dev->vidq);
  1405. spin_unlock(&dev->slock);
  1406. }
  1407. /* risc2 vbi */
  1408. if (status & 0x80) {
  1409. dprintk(2,"stopper vbi\n");
  1410. spin_lock(&dev->slock);
  1411. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1412. spin_unlock(&dev->slock);
  1413. }
  1414. }
  1415. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1416. {
  1417. struct cx8800_dev *dev = dev_id;
  1418. struct cx88_core *core = dev->core;
  1419. u32 status;
  1420. int loop, handled = 0;
  1421. for (loop = 0; loop < 10; loop++) {
  1422. status = cx_read(MO_PCI_INTSTAT) &
  1423. (core->pci_irqmask | PCI_INT_VIDINT);
  1424. if (0 == status)
  1425. goto out;
  1426. cx_write(MO_PCI_INTSTAT, status);
  1427. handled = 1;
  1428. if (status & core->pci_irqmask)
  1429. cx88_core_irq(core,status);
  1430. if (status & PCI_INT_VIDINT)
  1431. cx8800_vid_irq(dev);
  1432. };
  1433. if (10 == loop) {
  1434. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1435. core->name);
  1436. cx_write(MO_PCI_INTMSK,0);
  1437. }
  1438. out:
  1439. return IRQ_RETVAL(handled);
  1440. }
  1441. /* ----------------------------------------------------------- */
  1442. /* exported stuff */
  1443. static const struct file_operations video_fops =
  1444. {
  1445. .owner = THIS_MODULE,
  1446. .open = video_open,
  1447. .release = video_release,
  1448. .read = video_read,
  1449. .poll = video_poll,
  1450. .mmap = video_mmap,
  1451. .ioctl = video_ioctl2,
  1452. .compat_ioctl = v4l_compat_ioctl32,
  1453. .llseek = no_llseek,
  1454. };
  1455. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1456. .vidioc_querycap = vidioc_querycap,
  1457. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1458. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1459. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1460. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1461. .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
  1462. .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
  1463. .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
  1464. .vidioc_reqbufs = vidioc_reqbufs,
  1465. .vidioc_querybuf = vidioc_querybuf,
  1466. .vidioc_qbuf = vidioc_qbuf,
  1467. .vidioc_dqbuf = vidioc_dqbuf,
  1468. .vidioc_s_std = vidioc_s_std,
  1469. .vidioc_enum_input = vidioc_enum_input,
  1470. .vidioc_g_input = vidioc_g_input,
  1471. .vidioc_s_input = vidioc_s_input,
  1472. .vidioc_queryctrl = vidioc_queryctrl,
  1473. .vidioc_g_ctrl = vidioc_g_ctrl,
  1474. .vidioc_s_ctrl = vidioc_s_ctrl,
  1475. .vidioc_streamon = vidioc_streamon,
  1476. .vidioc_streamoff = vidioc_streamoff,
  1477. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1478. .vidiocgmbuf = vidiocgmbuf,
  1479. #endif
  1480. .vidioc_g_tuner = vidioc_g_tuner,
  1481. .vidioc_s_tuner = vidioc_s_tuner,
  1482. .vidioc_g_frequency = vidioc_g_frequency,
  1483. .vidioc_s_frequency = vidioc_s_frequency,
  1484. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1485. .vidioc_g_register = vidioc_g_register,
  1486. .vidioc_s_register = vidioc_s_register,
  1487. #endif
  1488. };
  1489. static struct video_device cx8800_vbi_template;
  1490. static struct video_device cx8800_video_template = {
  1491. .name = "cx8800-video",
  1492. .fops = &video_fops,
  1493. .minor = -1,
  1494. .ioctl_ops = &video_ioctl_ops,
  1495. .tvnorms = CX88_NORMS,
  1496. .current_norm = V4L2_STD_NTSC_M,
  1497. };
  1498. static const struct file_operations radio_fops =
  1499. {
  1500. .owner = THIS_MODULE,
  1501. .open = video_open,
  1502. .release = video_release,
  1503. .ioctl = video_ioctl2,
  1504. .compat_ioctl = v4l_compat_ioctl32,
  1505. .llseek = no_llseek,
  1506. };
  1507. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1508. .vidioc_querycap = radio_querycap,
  1509. .vidioc_g_tuner = radio_g_tuner,
  1510. .vidioc_enum_input = radio_enum_input,
  1511. .vidioc_g_audio = radio_g_audio,
  1512. .vidioc_s_tuner = radio_s_tuner,
  1513. .vidioc_s_audio = radio_s_audio,
  1514. .vidioc_s_input = radio_s_input,
  1515. .vidioc_queryctrl = radio_queryctrl,
  1516. .vidioc_g_ctrl = vidioc_g_ctrl,
  1517. .vidioc_s_ctrl = vidioc_s_ctrl,
  1518. .vidioc_g_frequency = vidioc_g_frequency,
  1519. .vidioc_s_frequency = vidioc_s_frequency,
  1520. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1521. .vidioc_g_register = vidioc_g_register,
  1522. .vidioc_s_register = vidioc_s_register,
  1523. #endif
  1524. };
  1525. static struct video_device cx8800_radio_template = {
  1526. .name = "cx8800-radio",
  1527. .fops = &radio_fops,
  1528. .minor = -1,
  1529. .ioctl_ops = &radio_ioctl_ops,
  1530. };
  1531. /* ----------------------------------------------------------- */
  1532. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1533. {
  1534. if (dev->radio_dev) {
  1535. if (-1 != dev->radio_dev->minor)
  1536. video_unregister_device(dev->radio_dev);
  1537. else
  1538. video_device_release(dev->radio_dev);
  1539. dev->radio_dev = NULL;
  1540. }
  1541. if (dev->vbi_dev) {
  1542. if (-1 != dev->vbi_dev->minor)
  1543. video_unregister_device(dev->vbi_dev);
  1544. else
  1545. video_device_release(dev->vbi_dev);
  1546. dev->vbi_dev = NULL;
  1547. }
  1548. if (dev->video_dev) {
  1549. if (-1 != dev->video_dev->minor)
  1550. video_unregister_device(dev->video_dev);
  1551. else
  1552. video_device_release(dev->video_dev);
  1553. dev->video_dev = NULL;
  1554. }
  1555. }
  1556. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1557. const struct pci_device_id *pci_id)
  1558. {
  1559. struct cx8800_dev *dev;
  1560. struct cx88_core *core;
  1561. int err;
  1562. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1563. if (NULL == dev)
  1564. return -ENOMEM;
  1565. /* pci init */
  1566. dev->pci = pci_dev;
  1567. if (pci_enable_device(pci_dev)) {
  1568. err = -EIO;
  1569. goto fail_free;
  1570. }
  1571. core = cx88_core_get(dev->pci);
  1572. if (NULL == core) {
  1573. err = -EINVAL;
  1574. goto fail_free;
  1575. }
  1576. dev->core = core;
  1577. /* print pci info */
  1578. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1579. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1580. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1581. "latency: %d, mmio: 0x%llx\n", core->name,
  1582. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1583. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1584. pci_set_master(pci_dev);
  1585. if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
  1586. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1587. err = -EIO;
  1588. goto fail_core;
  1589. }
  1590. /* Initialize VBI template */
  1591. memcpy( &cx8800_vbi_template, &cx8800_video_template,
  1592. sizeof(cx8800_vbi_template) );
  1593. strcpy(cx8800_vbi_template.name,"cx8800-vbi");
  1594. /* initialize driver struct */
  1595. spin_lock_init(&dev->slock);
  1596. core->tvnorm = cx8800_video_template.current_norm;
  1597. /* init video dma queues */
  1598. INIT_LIST_HEAD(&dev->vidq.active);
  1599. INIT_LIST_HEAD(&dev->vidq.queued);
  1600. dev->vidq.timeout.function = cx8800_vid_timeout;
  1601. dev->vidq.timeout.data = (unsigned long)dev;
  1602. init_timer(&dev->vidq.timeout);
  1603. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1604. MO_VID_DMACNTRL,0x11,0x00);
  1605. /* init vbi dma queues */
  1606. INIT_LIST_HEAD(&dev->vbiq.active);
  1607. INIT_LIST_HEAD(&dev->vbiq.queued);
  1608. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1609. dev->vbiq.timeout.data = (unsigned long)dev;
  1610. init_timer(&dev->vbiq.timeout);
  1611. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1612. MO_VID_DMACNTRL,0x88,0x00);
  1613. /* get irq */
  1614. err = request_irq(pci_dev->irq, cx8800_irq,
  1615. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1616. if (err < 0) {
  1617. printk(KERN_ERR "%s/0: can't get IRQ %d\n",
  1618. core->name,pci_dev->irq);
  1619. goto fail_core;
  1620. }
  1621. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1622. /* load and configure helper modules */
  1623. if (core->board.audio_chip == V4L2_IDENT_WM8775)
  1624. request_module("wm8775");
  1625. switch (core->boardnr) {
  1626. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  1627. case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
  1628. request_module("rtc-isl1208");
  1629. /* break intentionally omitted */
  1630. case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
  1631. request_module("ir-kbd-i2c");
  1632. }
  1633. /* register v4l devices */
  1634. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1635. &cx8800_video_template,"video");
  1636. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1637. video_nr[core->nr]);
  1638. if (err < 0) {
  1639. printk(KERN_ERR "%s/0: can't register video device\n",
  1640. core->name);
  1641. goto fail_unreg;
  1642. }
  1643. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1644. core->name,dev->video_dev->minor & 0x1f);
  1645. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1646. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1647. vbi_nr[core->nr]);
  1648. if (err < 0) {
  1649. printk(KERN_ERR "%s/0: can't register vbi device\n",
  1650. core->name);
  1651. goto fail_unreg;
  1652. }
  1653. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1654. core->name,dev->vbi_dev->minor & 0x1f);
  1655. if (core->board.radio.type == CX88_RADIO) {
  1656. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1657. &cx8800_radio_template,"radio");
  1658. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1659. radio_nr[core->nr]);
  1660. if (err < 0) {
  1661. printk(KERN_ERR "%s/0: can't register radio device\n",
  1662. core->name);
  1663. goto fail_unreg;
  1664. }
  1665. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1666. core->name,dev->radio_dev->minor & 0x1f);
  1667. }
  1668. /* everything worked */
  1669. list_add_tail(&dev->devlist,&cx8800_devlist);
  1670. pci_set_drvdata(pci_dev,dev);
  1671. /* initial device configuration */
  1672. mutex_lock(&core->lock);
  1673. cx88_set_tvnorm(core,core->tvnorm);
  1674. init_controls(core);
  1675. cx88_video_mux(core,0);
  1676. mutex_unlock(&core->lock);
  1677. /* start tvaudio thread */
  1678. if (core->board.tuner_type != TUNER_ABSENT) {
  1679. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1680. if (IS_ERR(core->kthread)) {
  1681. err = PTR_ERR(core->kthread);
  1682. printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
  1683. core->name, err);
  1684. }
  1685. }
  1686. return 0;
  1687. fail_unreg:
  1688. cx8800_unregister_video(dev);
  1689. free_irq(pci_dev->irq, dev);
  1690. fail_core:
  1691. cx88_core_put(core,dev->pci);
  1692. fail_free:
  1693. kfree(dev);
  1694. return err;
  1695. }
  1696. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1697. {
  1698. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1699. struct cx88_core *core = dev->core;
  1700. /* stop thread */
  1701. if (core->kthread) {
  1702. kthread_stop(core->kthread);
  1703. core->kthread = NULL;
  1704. }
  1705. if (core->ir)
  1706. cx88_ir_stop(core, core->ir);
  1707. cx88_shutdown(core); /* FIXME */
  1708. pci_disable_device(pci_dev);
  1709. /* unregister stuff */
  1710. free_irq(pci_dev->irq, dev);
  1711. cx8800_unregister_video(dev);
  1712. pci_set_drvdata(pci_dev, NULL);
  1713. /* free memory */
  1714. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1715. list_del(&dev->devlist);
  1716. cx88_core_put(core,dev->pci);
  1717. kfree(dev);
  1718. }
  1719. #ifdef CONFIG_PM
  1720. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1721. {
  1722. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1723. struct cx88_core *core = dev->core;
  1724. /* stop video+vbi capture */
  1725. spin_lock(&dev->slock);
  1726. if (!list_empty(&dev->vidq.active)) {
  1727. printk("%s/0: suspend video\n", core->name);
  1728. stop_video_dma(dev);
  1729. del_timer(&dev->vidq.timeout);
  1730. }
  1731. if (!list_empty(&dev->vbiq.active)) {
  1732. printk("%s/0: suspend vbi\n", core->name);
  1733. cx8800_stop_vbi_dma(dev);
  1734. del_timer(&dev->vbiq.timeout);
  1735. }
  1736. spin_unlock(&dev->slock);
  1737. if (core->ir)
  1738. cx88_ir_stop(core, core->ir);
  1739. /* FIXME -- shutdown device */
  1740. cx88_shutdown(core);
  1741. pci_save_state(pci_dev);
  1742. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1743. pci_disable_device(pci_dev);
  1744. dev->state.disabled = 1;
  1745. }
  1746. return 0;
  1747. }
  1748. static int cx8800_resume(struct pci_dev *pci_dev)
  1749. {
  1750. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1751. struct cx88_core *core = dev->core;
  1752. int err;
  1753. if (dev->state.disabled) {
  1754. err=pci_enable_device(pci_dev);
  1755. if (err) {
  1756. printk(KERN_ERR "%s/0: can't enable device\n",
  1757. core->name);
  1758. return err;
  1759. }
  1760. dev->state.disabled = 0;
  1761. }
  1762. err= pci_set_power_state(pci_dev, PCI_D0);
  1763. if (err) {
  1764. printk(KERN_ERR "%s/0: can't set power state\n", core->name);
  1765. pci_disable_device(pci_dev);
  1766. dev->state.disabled = 1;
  1767. return err;
  1768. }
  1769. pci_restore_state(pci_dev);
  1770. /* FIXME: re-initialize hardware */
  1771. cx88_reset(core);
  1772. if (core->ir)
  1773. cx88_ir_start(core, core->ir);
  1774. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1775. /* restart video+vbi capture */
  1776. spin_lock(&dev->slock);
  1777. if (!list_empty(&dev->vidq.active)) {
  1778. printk("%s/0: resume video\n", core->name);
  1779. restart_video_queue(dev,&dev->vidq);
  1780. }
  1781. if (!list_empty(&dev->vbiq.active)) {
  1782. printk("%s/0: resume vbi\n", core->name);
  1783. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1784. }
  1785. spin_unlock(&dev->slock);
  1786. return 0;
  1787. }
  1788. #endif
  1789. /* ----------------------------------------------------------- */
  1790. static struct pci_device_id cx8800_pci_tbl[] = {
  1791. {
  1792. .vendor = 0x14f1,
  1793. .device = 0x8800,
  1794. .subvendor = PCI_ANY_ID,
  1795. .subdevice = PCI_ANY_ID,
  1796. },{
  1797. /* --- end of list --- */
  1798. }
  1799. };
  1800. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1801. static struct pci_driver cx8800_pci_driver = {
  1802. .name = "cx8800",
  1803. .id_table = cx8800_pci_tbl,
  1804. .probe = cx8800_initdev,
  1805. .remove = __devexit_p(cx8800_finidev),
  1806. #ifdef CONFIG_PM
  1807. .suspend = cx8800_suspend,
  1808. .resume = cx8800_resume,
  1809. #endif
  1810. };
  1811. static int cx8800_init(void)
  1812. {
  1813. printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1814. (CX88_VERSION_CODE >> 16) & 0xff,
  1815. (CX88_VERSION_CODE >> 8) & 0xff,
  1816. CX88_VERSION_CODE & 0xff);
  1817. #ifdef SNAPSHOT
  1818. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1819. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1820. #endif
  1821. return pci_register_driver(&cx8800_pci_driver);
  1822. }
  1823. static void cx8800_fini(void)
  1824. {
  1825. pci_unregister_driver(&cx8800_pci_driver);
  1826. }
  1827. module_init(cx8800_init);
  1828. module_exit(cx8800_fini);
  1829. /* ----------------------------------------------------------- */
  1830. /*
  1831. * Local variables:
  1832. * c-basic-offset: 8
  1833. * End:
  1834. * 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
  1835. */