cx88-video.c 55 KB

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