cx88-video.c 55 KB

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