cx88-video.c 52 KB

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