cx88-video.c 53 KB

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