cx88-video.c 56 KB

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