cx88-video.c 53 KB

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