cx88-video.c 56 KB

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