cx88-video.c 54 KB

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