cx88-video.c 55 KB

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