cx23885-video.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kmod.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/kthread.h>
  31. #include <asm/div64.h>
  32. #include "cx23885.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include "cx23885-ioctl.h"
  36. #include "tuner-xc2028.h"
  37. #include <media/cx25840.h>
  38. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  39. MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
  40. MODULE_LICENSE("GPL");
  41. /* ------------------------------------------------------------------ */
  42. static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  43. static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  44. static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
  45. module_param_array(video_nr, int, NULL, 0444);
  46. module_param_array(vbi_nr, int, NULL, 0444);
  47. module_param_array(radio_nr, int, NULL, 0444);
  48. MODULE_PARM_DESC(video_nr, "video device numbers");
  49. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  50. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  51. static unsigned int video_debug;
  52. module_param(video_debug, int, 0644);
  53. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  54. static unsigned int irq_debug;
  55. module_param(irq_debug, int, 0644);
  56. MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
  57. static unsigned int vid_limit = 16;
  58. module_param(vid_limit, int, 0644);
  59. MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
  60. #define dprintk(level, fmt, arg...)\
  61. do { if (video_debug >= level)\
  62. printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. #define FORMAT_FLAGS_PACKED 0x01
  67. #if 0
  68. static struct cx23885_fmt formats[] = {
  69. {
  70. .name = "8 bpp, gray",
  71. .fourcc = V4L2_PIX_FMT_GREY,
  72. .depth = 8,
  73. .flags = FORMAT_FLAGS_PACKED,
  74. }, {
  75. .name = "15 bpp RGB, le",
  76. .fourcc = V4L2_PIX_FMT_RGB555,
  77. .depth = 16,
  78. .flags = FORMAT_FLAGS_PACKED,
  79. }, {
  80. .name = "15 bpp RGB, be",
  81. .fourcc = V4L2_PIX_FMT_RGB555X,
  82. .depth = 16,
  83. .flags = FORMAT_FLAGS_PACKED,
  84. }, {
  85. .name = "16 bpp RGB, le",
  86. .fourcc = V4L2_PIX_FMT_RGB565,
  87. .depth = 16,
  88. .flags = FORMAT_FLAGS_PACKED,
  89. }, {
  90. .name = "16 bpp RGB, be",
  91. .fourcc = V4L2_PIX_FMT_RGB565X,
  92. .depth = 16,
  93. .flags = FORMAT_FLAGS_PACKED,
  94. }, {
  95. .name = "24 bpp RGB, le",
  96. .fourcc = V4L2_PIX_FMT_BGR24,
  97. .depth = 24,
  98. .flags = FORMAT_FLAGS_PACKED,
  99. }, {
  100. .name = "32 bpp RGB, le",
  101. .fourcc = V4L2_PIX_FMT_BGR32,
  102. .depth = 32,
  103. .flags = FORMAT_FLAGS_PACKED,
  104. }, {
  105. .name = "32 bpp RGB, be",
  106. .fourcc = V4L2_PIX_FMT_RGB32,
  107. .depth = 32,
  108. .flags = FORMAT_FLAGS_PACKED,
  109. }, {
  110. .name = "4:2:2, packed, YUYV",
  111. .fourcc = V4L2_PIX_FMT_YUYV,
  112. .depth = 16,
  113. .flags = FORMAT_FLAGS_PACKED,
  114. }, {
  115. .name = "4:2:2, packed, UYVY",
  116. .fourcc = V4L2_PIX_FMT_UYVY,
  117. .depth = 16,
  118. .flags = FORMAT_FLAGS_PACKED,
  119. },
  120. };
  121. #else
  122. static struct cx23885_fmt formats[] = {
  123. {
  124. #if 0
  125. .name = "4:2:2, packed, UYVY",
  126. .fourcc = V4L2_PIX_FMT_UYVY,
  127. .depth = 16,
  128. .flags = FORMAT_FLAGS_PACKED,
  129. }, {
  130. #endif
  131. .name = "4:2:2, packed, YUYV",
  132. .fourcc = V4L2_PIX_FMT_YUYV,
  133. .depth = 16,
  134. .flags = FORMAT_FLAGS_PACKED,
  135. }
  136. };
  137. #endif
  138. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  139. {
  140. unsigned int i;
  141. for (i = 0; i < ARRAY_SIZE(formats); i++)
  142. if (formats[i].fourcc == fourcc)
  143. return formats+i;
  144. printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
  145. (fourcc & 0xff),
  146. ((fourcc >> 8) & 0xff),
  147. ((fourcc >> 16) & 0xff),
  148. ((fourcc >> 24) & 0xff)
  149. );
  150. return NULL;
  151. }
  152. /* ------------------------------------------------------------------- */
  153. static const struct v4l2_queryctrl no_ctl = {
  154. .name = "42",
  155. .flags = V4L2_CTRL_FLAG_DISABLED,
  156. };
  157. static struct cx23885_ctrl cx23885_ctls[] = {
  158. /* --- video --- */
  159. {
  160. .v = {
  161. .id = V4L2_CID_BRIGHTNESS,
  162. .name = "Brightness",
  163. .minimum = 0x00,
  164. .maximum = 0xff,
  165. .step = 1,
  166. .default_value = 0x7f,
  167. .type = V4L2_CTRL_TYPE_INTEGER,
  168. },
  169. .off = 128,
  170. .reg = LUMA_CTRL,
  171. .mask = 0x00ff,
  172. .shift = 0,
  173. }, {
  174. .v = {
  175. .id = V4L2_CID_CONTRAST,
  176. .name = "Contrast",
  177. .minimum = 0,
  178. .maximum = 0x7f,
  179. .step = 1,
  180. .default_value = 0x3f,
  181. .type = V4L2_CTRL_TYPE_INTEGER,
  182. },
  183. .off = 0,
  184. .reg = LUMA_CTRL,
  185. .mask = 0xff00,
  186. .shift = 8,
  187. }, {
  188. .v = {
  189. .id = V4L2_CID_HUE,
  190. .name = "Hue",
  191. .minimum = -127,
  192. .maximum = 128,
  193. .step = 1,
  194. .default_value = 0x0,
  195. .type = V4L2_CTRL_TYPE_INTEGER,
  196. },
  197. .off = 128,
  198. .reg = CHROMA_CTRL,
  199. .mask = 0xff0000,
  200. .shift = 16,
  201. }, {
  202. /* strictly, this only describes only U saturation.
  203. * V saturation is handled specially through code.
  204. */
  205. .v = {
  206. .id = V4L2_CID_SATURATION,
  207. .name = "Saturation",
  208. .minimum = 0,
  209. .maximum = 0x7f,
  210. .step = 1,
  211. .default_value = 0x3f,
  212. .type = V4L2_CTRL_TYPE_INTEGER,
  213. },
  214. .off = 0,
  215. .reg = CHROMA_CTRL,
  216. .mask = 0x00ff,
  217. .shift = 0,
  218. }, {
  219. /* --- audio --- */
  220. .v = {
  221. .id = V4L2_CID_AUDIO_MUTE,
  222. .name = "Mute",
  223. .minimum = 0,
  224. .maximum = 1,
  225. .default_value = 1,
  226. .type = V4L2_CTRL_TYPE_BOOLEAN,
  227. },
  228. .reg = PATH1_CTL1,
  229. .mask = (0x1f << 24),
  230. .shift = 24,
  231. }, {
  232. .v = {
  233. .id = V4L2_CID_AUDIO_VOLUME,
  234. .name = "Volume",
  235. .minimum = 0,
  236. .maximum = 65535,
  237. .step = 65535 / 100,
  238. .default_value = 65535,
  239. .type = V4L2_CTRL_TYPE_INTEGER,
  240. },
  241. .reg = PATH1_VOL_CTL,
  242. .mask = 0xff,
  243. .shift = 0,
  244. }
  245. };
  246. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  247. /* Must be sorted from low to high control ID! */
  248. static const u32 cx23885_user_ctrls[] = {
  249. V4L2_CID_USER_CLASS,
  250. V4L2_CID_BRIGHTNESS,
  251. V4L2_CID_CONTRAST,
  252. V4L2_CID_SATURATION,
  253. V4L2_CID_HUE,
  254. V4L2_CID_AUDIO_VOLUME,
  255. V4L2_CID_AUDIO_MUTE,
  256. 0
  257. };
  258. static const u32 *ctrl_classes[] = {
  259. cx23885_user_ctrls,
  260. NULL
  261. };
  262. void cx23885_video_wakeup(struct cx23885_dev *dev,
  263. struct cx23885_dmaqueue *q, u32 count)
  264. {
  265. struct cx23885_buffer *buf;
  266. int bc;
  267. for (bc = 0;; bc++) {
  268. if (list_empty(&q->active))
  269. break;
  270. buf = list_entry(q->active.next,
  271. struct cx23885_buffer, vb.queue);
  272. /* count comes from the hw and is is 16bit wide --
  273. * this trick handles wrap-arounds correctly for
  274. * up to 32767 buffers in flight... */
  275. if ((s16) (count - buf->count) < 0)
  276. break;
  277. do_gettimeofday(&buf->vb.ts);
  278. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  279. count, buf->count);
  280. buf->vb.state = VIDEOBUF_DONE;
  281. list_del(&buf->vb.queue);
  282. wake_up(&buf->vb.done);
  283. }
  284. if (list_empty(&q->active))
  285. del_timer(&q->timeout);
  286. else
  287. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  288. if (bc != 1)
  289. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  290. __func__, bc);
  291. }
  292. int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  293. {
  294. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  295. __func__,
  296. (unsigned int)norm,
  297. v4l2_norm_to_name(norm));
  298. dev->tvnorm = norm;
  299. call_all(dev, core, s_std, norm);
  300. return 0;
  301. }
  302. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  303. struct pci_dev *pci,
  304. struct video_device *template,
  305. char *type)
  306. {
  307. struct video_device *vfd;
  308. dprintk(1, "%s()\n", __func__);
  309. vfd = video_device_alloc();
  310. if (NULL == vfd)
  311. return NULL;
  312. *vfd = *template;
  313. vfd->v4l2_dev = &dev->v4l2_dev;
  314. vfd->release = video_device_release;
  315. snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
  316. cx23885_boards[dev->board].name, type);
  317. video_set_drvdata(vfd, dev);
  318. return vfd;
  319. }
  320. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  321. {
  322. int i;
  323. if (qctrl->id < V4L2_CID_BASE ||
  324. qctrl->id >= V4L2_CID_LASTP1)
  325. return -EINVAL;
  326. for (i = 0; i < CX23885_CTLS; i++)
  327. if (cx23885_ctls[i].v.id == qctrl->id)
  328. break;
  329. if (i == CX23885_CTLS) {
  330. *qctrl = no_ctl;
  331. return 0;
  332. }
  333. *qctrl = cx23885_ctls[i].v;
  334. return 0;
  335. }
  336. /* ------------------------------------------------------------------- */
  337. /* resource management */
  338. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  339. unsigned int bit)
  340. {
  341. dprintk(1, "%s()\n", __func__);
  342. if (fh->resources & bit)
  343. /* have it already allocated */
  344. return 1;
  345. /* is it free? */
  346. mutex_lock(&dev->lock);
  347. if (dev->resources & bit) {
  348. /* no, someone else uses it */
  349. mutex_unlock(&dev->lock);
  350. return 0;
  351. }
  352. /* it's free, grab it */
  353. fh->resources |= bit;
  354. dev->resources |= bit;
  355. dprintk(1, "res: get %d\n", bit);
  356. mutex_unlock(&dev->lock);
  357. return 1;
  358. }
  359. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  360. {
  361. return fh->resources & bit;
  362. }
  363. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  364. {
  365. return dev->resources & bit;
  366. }
  367. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  368. unsigned int bits)
  369. {
  370. BUG_ON((fh->resources & bits) != bits);
  371. dprintk(1, "%s()\n", __func__);
  372. mutex_lock(&dev->lock);
  373. fh->resources &= ~bits;
  374. dev->resources &= ~bits;
  375. dprintk(1, "res: put %d\n", bits);
  376. mutex_unlock(&dev->lock);
  377. }
  378. static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
  379. {
  380. /* 8 bit registers, 8 bit values */
  381. u8 buf[] = { reg, data };
  382. struct i2c_msg msg = { .addr = 0x98 >> 1,
  383. .flags = 0, .buf = buf, .len = 2 };
  384. return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
  385. }
  386. static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
  387. {
  388. /* 8 bit registers, 8 bit values */
  389. int ret;
  390. u8 b0[] = { reg };
  391. u8 b1[] = { 0 };
  392. struct i2c_msg msg[] = {
  393. { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
  394. { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
  395. };
  396. ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
  397. if (ret != 2)
  398. printk(KERN_ERR "%s() error\n", __func__);
  399. return b1[0];
  400. }
  401. static void cx23885_flatiron_dump(struct cx23885_dev *dev)
  402. {
  403. int i;
  404. dprintk(1, "Flatiron dump\n");
  405. for (i = 0; i < 0x24; i++) {
  406. dprintk(1, "FI[%02x] = %02x\n", i,
  407. cx23885_flatiron_read(dev, i));
  408. }
  409. }
  410. static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
  411. {
  412. u8 val;
  413. dprintk(1, "%s(input = %d)\n", __func__, input);
  414. if (input == 1)
  415. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
  416. else if (input == 2)
  417. val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
  418. else
  419. return -EINVAL;
  420. val |= 0x20; /* Enable clock to delta-sigma and dec filter */
  421. cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
  422. /* Wake up */
  423. cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
  424. if (video_debug)
  425. cx23885_flatiron_dump(dev);
  426. return 0;
  427. }
  428. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  429. {
  430. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  431. __func__,
  432. input, INPUT(input)->vmux,
  433. INPUT(input)->gpio0, INPUT(input)->gpio1,
  434. INPUT(input)->gpio2, INPUT(input)->gpio3);
  435. dev->input = input;
  436. if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
  437. dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
  438. dev->board == CX23885_BOARD_MYGICA_X8507) {
  439. /* Select Analog TV */
  440. if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
  441. cx23885_gpio_clear(dev, GPIO_0);
  442. }
  443. /* Tell the internal A/V decoder */
  444. v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
  445. INPUT(input)->vmux, 0, 0);
  446. if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
  447. (dev->board == CX23885_BOARD_MPX885) ||
  448. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
  449. (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)) {
  450. /* Configure audio routing */
  451. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  452. INPUT(input)->amux, 0, 0);
  453. if (INPUT(input)->amux == CX25840_AUDIO7)
  454. cx23885_flatiron_mux(dev, 1);
  455. else if (INPUT(input)->amux == CX25840_AUDIO6)
  456. cx23885_flatiron_mux(dev, 2);
  457. }
  458. return 0;
  459. }
  460. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  461. {
  462. dprintk(1, "%s(input=%d)\n", __func__, input);
  463. /* The baseband video core of the cx23885 has two audio inputs.
  464. * LR1 and LR2. In almost every single case so far only HVR1xxx
  465. * cards we've only ever supported LR1. Time to support LR2,
  466. * which is available via the optional white breakout header on
  467. * the board.
  468. * We'll use a could of existing enums in the card struct to allow
  469. * devs to specify which baseband input they need, or just default
  470. * to what we've always used.
  471. */
  472. if (INPUT(input)->amux == CX25840_AUDIO7)
  473. cx23885_flatiron_mux(dev, 1);
  474. else if (INPUT(input)->amux == CX25840_AUDIO6)
  475. cx23885_flatiron_mux(dev, 2);
  476. else {
  477. /* Not specifically defined, assume the default. */
  478. cx23885_flatiron_mux(dev, 1);
  479. }
  480. return 0;
  481. }
  482. /* ------------------------------------------------------------------ */
  483. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  484. struct cx23885_dmaqueue *q,
  485. struct cx23885_buffer *buf)
  486. {
  487. dprintk(1, "%s()\n", __func__);
  488. /* Stop the dma/fifo before we tamper with it's risc programs */
  489. cx_clear(VID_A_DMA_CTL, 0x11);
  490. /* setup fifo + format */
  491. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  492. buf->bpl, buf->risc.dma);
  493. /* reset counter */
  494. cx_write(VID_A_GPCNT_CTL, 3);
  495. q->count = 1;
  496. /* enable irq */
  497. cx23885_irq_add_enable(dev, 0x01);
  498. cx_set(VID_A_INT_MSK, 0x000011);
  499. /* start dma */
  500. cx_set(DEV_CNTRL2, (1<<5));
  501. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  502. return 0;
  503. }
  504. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  505. struct cx23885_dmaqueue *q)
  506. {
  507. struct cx23885_buffer *buf, *prev;
  508. struct list_head *item;
  509. dprintk(1, "%s()\n", __func__);
  510. if (!list_empty(&q->active)) {
  511. buf = list_entry(q->active.next, struct cx23885_buffer,
  512. vb.queue);
  513. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  514. buf, buf->vb.i);
  515. cx23885_start_video_dma(dev, q, buf);
  516. list_for_each(item, &q->active) {
  517. buf = list_entry(item, struct cx23885_buffer,
  518. vb.queue);
  519. buf->count = q->count++;
  520. }
  521. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  522. return 0;
  523. }
  524. prev = NULL;
  525. for (;;) {
  526. if (list_empty(&q->queued))
  527. return 0;
  528. buf = list_entry(q->queued.next, struct cx23885_buffer,
  529. vb.queue);
  530. if (NULL == prev) {
  531. list_move_tail(&buf->vb.queue, &q->active);
  532. cx23885_start_video_dma(dev, q, buf);
  533. buf->vb.state = VIDEOBUF_ACTIVE;
  534. buf->count = q->count++;
  535. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  536. dprintk(2, "[%p/%d] restart_queue - first active\n",
  537. buf, buf->vb.i);
  538. } else if (prev->vb.width == buf->vb.width &&
  539. prev->vb.height == buf->vb.height &&
  540. prev->fmt == buf->fmt) {
  541. list_move_tail(&buf->vb.queue, &q->active);
  542. buf->vb.state = VIDEOBUF_ACTIVE;
  543. buf->count = q->count++;
  544. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  545. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  546. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  547. buf, buf->vb.i);
  548. } else {
  549. return 0;
  550. }
  551. prev = buf;
  552. }
  553. }
  554. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  555. unsigned int *size)
  556. {
  557. struct cx23885_fh *fh = q->priv_data;
  558. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  559. if (0 == *count)
  560. *count = 32;
  561. if (*size * *count > vid_limit * 1024 * 1024)
  562. *count = (vid_limit * 1024 * 1024) / *size;
  563. return 0;
  564. }
  565. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  566. enum v4l2_field field)
  567. {
  568. struct cx23885_fh *fh = q->priv_data;
  569. struct cx23885_dev *dev = fh->dev;
  570. struct cx23885_buffer *buf =
  571. container_of(vb, struct cx23885_buffer, vb);
  572. int rc, init_buffer = 0;
  573. u32 line0_offset, line1_offset;
  574. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  575. int field_tff;
  576. BUG_ON(NULL == fh->fmt);
  577. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  578. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  579. return -EINVAL;
  580. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  581. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  582. return -EINVAL;
  583. if (buf->fmt != fh->fmt ||
  584. buf->vb.width != fh->width ||
  585. buf->vb.height != fh->height ||
  586. buf->vb.field != field) {
  587. buf->fmt = fh->fmt;
  588. buf->vb.width = fh->width;
  589. buf->vb.height = fh->height;
  590. buf->vb.field = field;
  591. init_buffer = 1;
  592. }
  593. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  594. init_buffer = 1;
  595. rc = videobuf_iolock(q, &buf->vb, NULL);
  596. if (0 != rc)
  597. goto fail;
  598. }
  599. if (init_buffer) {
  600. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  601. switch (buf->vb.field) {
  602. case V4L2_FIELD_TOP:
  603. cx23885_risc_buffer(dev->pci, &buf->risc,
  604. dma->sglist, 0, UNSET,
  605. buf->bpl, 0, buf->vb.height);
  606. break;
  607. case V4L2_FIELD_BOTTOM:
  608. cx23885_risc_buffer(dev->pci, &buf->risc,
  609. dma->sglist, UNSET, 0,
  610. buf->bpl, 0, buf->vb.height);
  611. break;
  612. case V4L2_FIELD_INTERLACED:
  613. if (dev->tvnorm & V4L2_STD_NTSC)
  614. /* NTSC or */
  615. field_tff = 1;
  616. else
  617. field_tff = 0;
  618. if (cx23885_boards[dev->board].force_bff)
  619. /* PAL / SECAM OR 888 in NTSC MODE */
  620. field_tff = 0;
  621. if (field_tff) {
  622. /* cx25840 transmits NTSC bottom field first */
  623. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  624. __func__);
  625. line0_offset = buf->bpl;
  626. line1_offset = 0;
  627. } else {
  628. /* All other formats are top field first */
  629. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  630. __func__);
  631. line0_offset = 0;
  632. line1_offset = buf->bpl;
  633. }
  634. cx23885_risc_buffer(dev->pci, &buf->risc,
  635. dma->sglist, line0_offset,
  636. line1_offset,
  637. buf->bpl, buf->bpl,
  638. buf->vb.height >> 1);
  639. break;
  640. case V4L2_FIELD_SEQ_TB:
  641. cx23885_risc_buffer(dev->pci, &buf->risc,
  642. dma->sglist,
  643. 0, buf->bpl * (buf->vb.height >> 1),
  644. buf->bpl, 0,
  645. buf->vb.height >> 1);
  646. break;
  647. case V4L2_FIELD_SEQ_BT:
  648. cx23885_risc_buffer(dev->pci, &buf->risc,
  649. dma->sglist,
  650. buf->bpl * (buf->vb.height >> 1), 0,
  651. buf->bpl, 0,
  652. buf->vb.height >> 1);
  653. break;
  654. default:
  655. BUG();
  656. }
  657. }
  658. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  659. buf, buf->vb.i,
  660. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  661. (unsigned long)buf->risc.dma);
  662. buf->vb.state = VIDEOBUF_PREPARED;
  663. return 0;
  664. fail:
  665. cx23885_free_buffer(q, buf);
  666. return rc;
  667. }
  668. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  669. {
  670. struct cx23885_buffer *buf = container_of(vb,
  671. struct cx23885_buffer, vb);
  672. struct cx23885_buffer *prev;
  673. struct cx23885_fh *fh = vq->priv_data;
  674. struct cx23885_dev *dev = fh->dev;
  675. struct cx23885_dmaqueue *q = &dev->vidq;
  676. /* add jump to stopper */
  677. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  678. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  679. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  680. if (!list_empty(&q->queued)) {
  681. list_add_tail(&buf->vb.queue, &q->queued);
  682. buf->vb.state = VIDEOBUF_QUEUED;
  683. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  684. buf, buf->vb.i);
  685. } else if (list_empty(&q->active)) {
  686. list_add_tail(&buf->vb.queue, &q->active);
  687. cx23885_start_video_dma(dev, q, buf);
  688. buf->vb.state = VIDEOBUF_ACTIVE;
  689. buf->count = q->count++;
  690. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  691. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  692. buf, buf->vb.i);
  693. } else {
  694. prev = list_entry(q->active.prev, struct cx23885_buffer,
  695. vb.queue);
  696. if (prev->vb.width == buf->vb.width &&
  697. prev->vb.height == buf->vb.height &&
  698. prev->fmt == buf->fmt) {
  699. list_add_tail(&buf->vb.queue, &q->active);
  700. buf->vb.state = VIDEOBUF_ACTIVE;
  701. buf->count = q->count++;
  702. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  703. /* 64 bit bits 63-32 */
  704. prev->risc.jmp[2] = cpu_to_le32(0);
  705. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  706. buf, buf->vb.i);
  707. } else {
  708. list_add_tail(&buf->vb.queue, &q->queued);
  709. buf->vb.state = VIDEOBUF_QUEUED;
  710. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  711. buf, buf->vb.i);
  712. }
  713. }
  714. }
  715. static void buffer_release(struct videobuf_queue *q,
  716. struct videobuf_buffer *vb)
  717. {
  718. struct cx23885_buffer *buf = container_of(vb,
  719. struct cx23885_buffer, vb);
  720. cx23885_free_buffer(q, buf);
  721. }
  722. static struct videobuf_queue_ops cx23885_video_qops = {
  723. .buf_setup = buffer_setup,
  724. .buf_prepare = buffer_prepare,
  725. .buf_queue = buffer_queue,
  726. .buf_release = buffer_release,
  727. };
  728. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  729. {
  730. switch (fh->type) {
  731. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  732. return &fh->vidq;
  733. case V4L2_BUF_TYPE_VBI_CAPTURE:
  734. return &fh->vbiq;
  735. default:
  736. BUG();
  737. return NULL;
  738. }
  739. }
  740. static int get_resource(struct cx23885_fh *fh)
  741. {
  742. switch (fh->type) {
  743. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  744. return RESOURCE_VIDEO;
  745. case V4L2_BUF_TYPE_VBI_CAPTURE:
  746. return RESOURCE_VBI;
  747. default:
  748. BUG();
  749. return 0;
  750. }
  751. }
  752. static int video_open(struct file *file)
  753. {
  754. struct video_device *vdev = video_devdata(file);
  755. struct cx23885_dev *dev = video_drvdata(file);
  756. struct cx23885_fh *fh;
  757. enum v4l2_buf_type type = 0;
  758. int radio = 0;
  759. switch (vdev->vfl_type) {
  760. case VFL_TYPE_GRABBER:
  761. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  762. break;
  763. case VFL_TYPE_VBI:
  764. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  765. break;
  766. case VFL_TYPE_RADIO:
  767. radio = 1;
  768. break;
  769. }
  770. dprintk(1, "open dev=%s radio=%d type=%s\n",
  771. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  772. /* allocate + initialize per filehandle data */
  773. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  774. if (NULL == fh)
  775. return -ENOMEM;
  776. file->private_data = fh;
  777. fh->dev = dev;
  778. fh->radio = radio;
  779. fh->type = type;
  780. fh->width = 320;
  781. fh->height = 240;
  782. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  783. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  784. &dev->pci->dev, &dev->slock,
  785. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  786. V4L2_FIELD_INTERLACED,
  787. sizeof(struct cx23885_buffer),
  788. fh, NULL);
  789. videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
  790. &dev->pci->dev, &dev->slock,
  791. V4L2_BUF_TYPE_VBI_CAPTURE,
  792. V4L2_FIELD_SEQ_TB,
  793. sizeof(struct cx23885_buffer),
  794. fh, NULL);
  795. dprintk(1, "post videobuf_queue_init()\n");
  796. return 0;
  797. }
  798. static ssize_t video_read(struct file *file, char __user *data,
  799. size_t count, loff_t *ppos)
  800. {
  801. struct cx23885_fh *fh = file->private_data;
  802. switch (fh->type) {
  803. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  804. if (res_locked(fh->dev, RESOURCE_VIDEO))
  805. return -EBUSY;
  806. return videobuf_read_one(&fh->vidq, data, count, ppos,
  807. file->f_flags & O_NONBLOCK);
  808. case V4L2_BUF_TYPE_VBI_CAPTURE:
  809. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  810. return -EBUSY;
  811. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  812. file->f_flags & O_NONBLOCK);
  813. default:
  814. BUG();
  815. return 0;
  816. }
  817. }
  818. static unsigned int video_poll(struct file *file,
  819. struct poll_table_struct *wait)
  820. {
  821. struct cx23885_fh *fh = file->private_data;
  822. struct cx23885_buffer *buf;
  823. unsigned int rc = POLLERR;
  824. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  825. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  826. return POLLERR;
  827. return videobuf_poll_stream(file, &fh->vbiq, wait);
  828. }
  829. mutex_lock(&fh->vidq.vb_lock);
  830. if (res_check(fh, RESOURCE_VIDEO)) {
  831. /* streaming capture */
  832. if (list_empty(&fh->vidq.stream))
  833. goto done;
  834. buf = list_entry(fh->vidq.stream.next,
  835. struct cx23885_buffer, vb.stream);
  836. } else {
  837. /* read() capture */
  838. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  839. if (NULL == buf)
  840. goto done;
  841. }
  842. poll_wait(file, &buf->vb.done, wait);
  843. if (buf->vb.state == VIDEOBUF_DONE ||
  844. buf->vb.state == VIDEOBUF_ERROR)
  845. rc = POLLIN|POLLRDNORM;
  846. else
  847. rc = 0;
  848. done:
  849. mutex_unlock(&fh->vidq.vb_lock);
  850. return rc;
  851. }
  852. static int video_release(struct file *file)
  853. {
  854. struct cx23885_fh *fh = file->private_data;
  855. struct cx23885_dev *dev = fh->dev;
  856. /* turn off overlay */
  857. if (res_check(fh, RESOURCE_OVERLAY)) {
  858. /* FIXME */
  859. res_free(dev, fh, RESOURCE_OVERLAY);
  860. }
  861. /* stop video capture */
  862. if (res_check(fh, RESOURCE_VIDEO)) {
  863. videobuf_queue_cancel(&fh->vidq);
  864. res_free(dev, fh, RESOURCE_VIDEO);
  865. }
  866. if (fh->vidq.read_buf) {
  867. buffer_release(&fh->vidq, fh->vidq.read_buf);
  868. kfree(fh->vidq.read_buf);
  869. }
  870. /* stop vbi capture */
  871. if (res_check(fh, RESOURCE_VBI)) {
  872. if (fh->vbiq.streaming)
  873. videobuf_streamoff(&fh->vbiq);
  874. if (fh->vbiq.reading)
  875. videobuf_read_stop(&fh->vbiq);
  876. res_free(dev, fh, RESOURCE_VBI);
  877. }
  878. videobuf_mmap_free(&fh->vidq);
  879. videobuf_mmap_free(&fh->vbiq);
  880. file->private_data = NULL;
  881. kfree(fh);
  882. /* We are not putting the tuner to sleep here on exit, because
  883. * we want to use the mpeg encoder in another session to capture
  884. * tuner video. Closing this will result in no video to the encoder.
  885. */
  886. return 0;
  887. }
  888. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  889. {
  890. struct cx23885_fh *fh = file->private_data;
  891. return videobuf_mmap_mapper(get_queue(fh), vma);
  892. }
  893. /* ------------------------------------------------------------------ */
  894. /* VIDEO CTRL IOCTLS */
  895. int cx23885_get_control(struct cx23885_dev *dev,
  896. struct v4l2_control *ctl)
  897. {
  898. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  899. call_all(dev, core, g_ctrl, ctl);
  900. return 0;
  901. }
  902. int cx23885_set_control(struct cx23885_dev *dev,
  903. struct v4l2_control *ctl)
  904. {
  905. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
  906. call_all(dev, core, s_ctrl, ctl);
  907. return 0;
  908. }
  909. static void init_controls(struct cx23885_dev *dev)
  910. {
  911. struct v4l2_control ctrl;
  912. int i;
  913. for (i = 0; i < CX23885_CTLS; i++) {
  914. ctrl.id = cx23885_ctls[i].v.id;
  915. ctrl.value = cx23885_ctls[i].v.default_value;
  916. cx23885_set_control(dev, &ctrl);
  917. }
  918. }
  919. /* ------------------------------------------------------------------ */
  920. /* VIDEO IOCTLS */
  921. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  922. struct v4l2_format *f)
  923. {
  924. struct cx23885_fh *fh = priv;
  925. f->fmt.pix.width = fh->width;
  926. f->fmt.pix.height = fh->height;
  927. f->fmt.pix.field = fh->vidq.field;
  928. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  929. f->fmt.pix.bytesperline =
  930. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  931. f->fmt.pix.sizeimage =
  932. f->fmt.pix.height * f->fmt.pix.bytesperline;
  933. return 0;
  934. }
  935. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  936. struct v4l2_format *f)
  937. {
  938. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  939. struct cx23885_fmt *fmt;
  940. enum v4l2_field field;
  941. unsigned int maxw, maxh;
  942. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  943. if (NULL == fmt)
  944. return -EINVAL;
  945. field = f->fmt.pix.field;
  946. maxw = norm_maxw(dev->tvnorm);
  947. maxh = norm_maxh(dev->tvnorm);
  948. if (V4L2_FIELD_ANY == field) {
  949. field = (f->fmt.pix.height > maxh/2)
  950. ? V4L2_FIELD_INTERLACED
  951. : V4L2_FIELD_BOTTOM;
  952. }
  953. switch (field) {
  954. case V4L2_FIELD_TOP:
  955. case V4L2_FIELD_BOTTOM:
  956. maxh = maxh / 2;
  957. break;
  958. case V4L2_FIELD_INTERLACED:
  959. break;
  960. default:
  961. return -EINVAL;
  962. }
  963. f->fmt.pix.field = field;
  964. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  965. &f->fmt.pix.height, 32, maxh, 0, 0);
  966. f->fmt.pix.bytesperline =
  967. (f->fmt.pix.width * fmt->depth) >> 3;
  968. f->fmt.pix.sizeimage =
  969. f->fmt.pix.height * f->fmt.pix.bytesperline;
  970. return 0;
  971. }
  972. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  973. struct v4l2_format *f)
  974. {
  975. struct cx23885_fh *fh = priv;
  976. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  977. struct v4l2_mbus_framefmt mbus_fmt;
  978. int err;
  979. dprintk(2, "%s()\n", __func__);
  980. err = vidioc_try_fmt_vid_cap(file, priv, f);
  981. if (0 != err)
  982. return err;
  983. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  984. fh->width = f->fmt.pix.width;
  985. fh->height = f->fmt.pix.height;
  986. fh->vidq.field = f->fmt.pix.field;
  987. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  988. fh->width, fh->height, fh->vidq.field);
  989. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  990. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  991. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  992. return 0;
  993. }
  994. static int vidioc_querycap(struct file *file, void *priv,
  995. struct v4l2_capability *cap)
  996. {
  997. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  998. strcpy(cap->driver, "cx23885");
  999. strlcpy(cap->card, cx23885_boards[dev->board].name,
  1000. sizeof(cap->card));
  1001. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1002. cap->capabilities =
  1003. V4L2_CAP_VIDEO_CAPTURE |
  1004. V4L2_CAP_READWRITE |
  1005. V4L2_CAP_STREAMING |
  1006. V4L2_CAP_VBI_CAPTURE;
  1007. if (UNSET != dev->tuner_type)
  1008. cap->capabilities |= V4L2_CAP_TUNER;
  1009. return 0;
  1010. }
  1011. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1012. struct v4l2_fmtdesc *f)
  1013. {
  1014. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  1015. return -EINVAL;
  1016. strlcpy(f->description, formats[f->index].name,
  1017. sizeof(f->description));
  1018. f->pixelformat = formats[f->index].fourcc;
  1019. return 0;
  1020. }
  1021. static int vidioc_reqbufs(struct file *file, void *priv,
  1022. struct v4l2_requestbuffers *p)
  1023. {
  1024. struct cx23885_fh *fh = priv;
  1025. return videobuf_reqbufs(get_queue(fh), p);
  1026. }
  1027. static int vidioc_querybuf(struct file *file, void *priv,
  1028. struct v4l2_buffer *p)
  1029. {
  1030. struct cx23885_fh *fh = priv;
  1031. return videobuf_querybuf(get_queue(fh), p);
  1032. }
  1033. static int vidioc_qbuf(struct file *file, void *priv,
  1034. struct v4l2_buffer *p)
  1035. {
  1036. struct cx23885_fh *fh = priv;
  1037. return videobuf_qbuf(get_queue(fh), p);
  1038. }
  1039. static int vidioc_dqbuf(struct file *file, void *priv,
  1040. struct v4l2_buffer *p)
  1041. {
  1042. struct cx23885_fh *fh = priv;
  1043. return videobuf_dqbuf(get_queue(fh), p,
  1044. file->f_flags & O_NONBLOCK);
  1045. }
  1046. static int vidioc_streamon(struct file *file, void *priv,
  1047. enum v4l2_buf_type i)
  1048. {
  1049. struct cx23885_fh *fh = priv;
  1050. struct cx23885_dev *dev = fh->dev;
  1051. dprintk(1, "%s()\n", __func__);
  1052. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1053. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1054. return -EINVAL;
  1055. if (unlikely(i != fh->type))
  1056. return -EINVAL;
  1057. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  1058. return -EBUSY;
  1059. /* Don't start VBI streaming unless vida streaming
  1060. * has already started.
  1061. */
  1062. if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
  1063. ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
  1064. return -EINVAL;
  1065. return videobuf_streamon(get_queue(fh));
  1066. }
  1067. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1068. {
  1069. struct cx23885_fh *fh = priv;
  1070. struct cx23885_dev *dev = fh->dev;
  1071. int err, res;
  1072. dprintk(1, "%s()\n", __func__);
  1073. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1074. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1075. return -EINVAL;
  1076. if (i != fh->type)
  1077. return -EINVAL;
  1078. res = get_resource(fh);
  1079. err = videobuf_streamoff(get_queue(fh));
  1080. if (err < 0)
  1081. return err;
  1082. res_free(dev, fh, res);
  1083. return 0;
  1084. }
  1085. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1086. {
  1087. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1088. dprintk(1, "%s()\n", __func__);
  1089. call_all(dev, core, g_std, id);
  1090. return 0;
  1091. }
  1092. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  1093. {
  1094. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1095. dprintk(1, "%s()\n", __func__);
  1096. mutex_lock(&dev->lock);
  1097. cx23885_set_tvnorm(dev, *tvnorms);
  1098. mutex_unlock(&dev->lock);
  1099. return 0;
  1100. }
  1101. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1102. {
  1103. static const char *iname[] = {
  1104. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1105. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1106. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1107. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1108. [CX23885_VMUX_SVIDEO] = "S-Video",
  1109. [CX23885_VMUX_COMPONENT] = "Component",
  1110. [CX23885_VMUX_TELEVISION] = "Television",
  1111. [CX23885_VMUX_CABLE] = "Cable TV",
  1112. [CX23885_VMUX_DVB] = "DVB",
  1113. [CX23885_VMUX_DEBUG] = "for debug only",
  1114. };
  1115. unsigned int n;
  1116. dprintk(1, "%s()\n", __func__);
  1117. n = i->index;
  1118. if (n >= MAX_CX23885_INPUT)
  1119. return -EINVAL;
  1120. if (0 == INPUT(n)->type)
  1121. return -EINVAL;
  1122. i->index = n;
  1123. i->type = V4L2_INPUT_TYPE_CAMERA;
  1124. strcpy(i->name, iname[INPUT(n)->type]);
  1125. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1126. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1127. i->type = V4L2_INPUT_TYPE_TUNER;
  1128. i->std = CX23885_NORMS;
  1129. }
  1130. /* Two selectable audio inputs for non-tv inputs */
  1131. if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
  1132. i->audioset = 0x3;
  1133. if (dev->input == n) {
  1134. /* enum'd input matches our configured input.
  1135. * Ask the video decoder to process the call
  1136. * and give it an oppertunity to update the
  1137. * status field.
  1138. */
  1139. call_all(dev, video, g_input_status, &i->status);
  1140. }
  1141. return 0;
  1142. }
  1143. static int vidioc_enum_input(struct file *file, void *priv,
  1144. struct v4l2_input *i)
  1145. {
  1146. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1147. dprintk(1, "%s()\n", __func__);
  1148. return cx23885_enum_input(dev, i);
  1149. }
  1150. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  1151. {
  1152. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1153. *i = dev->input;
  1154. dprintk(1, "%s() returns %d\n", __func__, *i);
  1155. return 0;
  1156. }
  1157. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1158. {
  1159. return cx23885_get_input(file, priv, i);
  1160. }
  1161. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  1162. {
  1163. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1164. dprintk(1, "%s(%d)\n", __func__, i);
  1165. if (i >= MAX_CX23885_INPUT) {
  1166. dprintk(1, "%s() -EINVAL\n", __func__);
  1167. return -EINVAL;
  1168. }
  1169. if (INPUT(i)->type == 0)
  1170. return -EINVAL;
  1171. mutex_lock(&dev->lock);
  1172. cx23885_video_mux(dev, i);
  1173. /* By default establish the default audio input for the card also */
  1174. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  1175. cx23885_audio_mux(dev, i);
  1176. mutex_unlock(&dev->lock);
  1177. return 0;
  1178. }
  1179. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1180. {
  1181. return cx23885_set_input(file, priv, i);
  1182. }
  1183. static int vidioc_log_status(struct file *file, void *priv)
  1184. {
  1185. struct cx23885_fh *fh = priv;
  1186. struct cx23885_dev *dev = fh->dev;
  1187. printk(KERN_INFO
  1188. "%s/0: ============ START LOG STATUS ============\n",
  1189. dev->name);
  1190. call_all(dev, core, log_status);
  1191. printk(KERN_INFO
  1192. "%s/0: ============= END LOG STATUS =============\n",
  1193. dev->name);
  1194. return 0;
  1195. }
  1196. static int cx23885_query_audinput(struct file *file, void *priv,
  1197. struct v4l2_audio *i)
  1198. {
  1199. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1200. static const char *iname[] = {
  1201. [0] = "Baseband L/R 1",
  1202. [1] = "Baseband L/R 2",
  1203. };
  1204. unsigned int n;
  1205. dprintk(1, "%s()\n", __func__);
  1206. n = i->index;
  1207. if (n >= 2)
  1208. return -EINVAL;
  1209. memset(i, 0, sizeof(*i));
  1210. i->index = n;
  1211. strcpy(i->name, iname[n]);
  1212. i->capability = V4L2_AUDCAP_STEREO;
  1213. i->mode = V4L2_AUDMODE_AVL;
  1214. return 0;
  1215. }
  1216. static int vidioc_enum_audinput(struct file *file, void *priv,
  1217. struct v4l2_audio *i)
  1218. {
  1219. return cx23885_query_audinput(file, priv, i);
  1220. }
  1221. static int vidioc_g_audinput(struct file *file, void *priv,
  1222. struct v4l2_audio *i)
  1223. {
  1224. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1225. i->index = dev->audinput;
  1226. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  1227. return cx23885_query_audinput(file, priv, i);
  1228. }
  1229. static int vidioc_s_audinput(struct file *file, void *priv,
  1230. struct v4l2_audio *i)
  1231. {
  1232. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1233. if (i->index >= 2)
  1234. return -EINVAL;
  1235. dprintk(1, "%s(%d)\n", __func__, i->index);
  1236. dev->audinput = i->index;
  1237. /* Skip the audio defaults from the cards struct, caller wants
  1238. * directly touch the audio mux hardware. */
  1239. cx23885_flatiron_mux(dev, dev->audinput + 1);
  1240. return 0;
  1241. }
  1242. static int vidioc_queryctrl(struct file *file, void *priv,
  1243. struct v4l2_queryctrl *qctrl)
  1244. {
  1245. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1246. if (unlikely(qctrl->id == 0))
  1247. return -EINVAL;
  1248. return cx23885_ctrl_query(qctrl);
  1249. }
  1250. static int vidioc_g_ctrl(struct file *file, void *priv,
  1251. struct v4l2_control *ctl)
  1252. {
  1253. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1254. return cx23885_get_control(dev, ctl);
  1255. }
  1256. static int vidioc_s_ctrl(struct file *file, void *priv,
  1257. struct v4l2_control *ctl)
  1258. {
  1259. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1260. return cx23885_set_control(dev, ctl);
  1261. }
  1262. static int vidioc_g_tuner(struct file *file, void *priv,
  1263. struct v4l2_tuner *t)
  1264. {
  1265. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1266. if (unlikely(UNSET == dev->tuner_type))
  1267. return -EINVAL;
  1268. if (0 != t->index)
  1269. return -EINVAL;
  1270. strcpy(t->name, "Television");
  1271. call_all(dev, tuner, g_tuner, t);
  1272. return 0;
  1273. }
  1274. static int vidioc_s_tuner(struct file *file, void *priv,
  1275. struct v4l2_tuner *t)
  1276. {
  1277. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1278. if (UNSET == dev->tuner_type)
  1279. return -EINVAL;
  1280. if (0 != t->index)
  1281. return -EINVAL;
  1282. /* Update the A/V core */
  1283. call_all(dev, tuner, s_tuner, t);
  1284. return 0;
  1285. }
  1286. static int vidioc_g_frequency(struct file *file, void *priv,
  1287. struct v4l2_frequency *f)
  1288. {
  1289. struct cx23885_fh *fh = priv;
  1290. struct cx23885_dev *dev = fh->dev;
  1291. if (unlikely(UNSET == dev->tuner_type))
  1292. return -EINVAL;
  1293. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1294. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1295. f->frequency = dev->freq;
  1296. call_all(dev, tuner, g_frequency, f);
  1297. return 0;
  1298. }
  1299. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1300. {
  1301. struct v4l2_control ctrl;
  1302. if (unlikely(UNSET == dev->tuner_type))
  1303. return -EINVAL;
  1304. if (unlikely(f->tuner != 0))
  1305. return -EINVAL;
  1306. mutex_lock(&dev->lock);
  1307. dev->freq = f->frequency;
  1308. /* I need to mute audio here */
  1309. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1310. ctrl.value = 1;
  1311. cx23885_set_control(dev, &ctrl);
  1312. call_all(dev, tuner, s_frequency, f);
  1313. /* When changing channels it is required to reset TVAUDIO */
  1314. msleep(100);
  1315. /* I need to unmute audio here */
  1316. ctrl.value = 0;
  1317. cx23885_set_control(dev, &ctrl);
  1318. mutex_unlock(&dev->lock);
  1319. return 0;
  1320. }
  1321. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  1322. struct v4l2_frequency *f)
  1323. {
  1324. struct v4l2_control ctrl;
  1325. struct videobuf_dvb_frontend *vfe;
  1326. struct dvb_frontend *fe;
  1327. struct analog_parameters params = {
  1328. .mode = V4L2_TUNER_ANALOG_TV,
  1329. .audmode = V4L2_TUNER_MODE_STEREO,
  1330. .std = dev->tvnorm,
  1331. .frequency = f->frequency
  1332. };
  1333. mutex_lock(&dev->lock);
  1334. dev->freq = f->frequency;
  1335. /* I need to mute audio here */
  1336. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1337. ctrl.value = 1;
  1338. cx23885_set_control(dev, &ctrl);
  1339. /* If HVR1850 */
  1340. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  1341. params.frequency, f->tuner, params.std);
  1342. vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
  1343. if (!vfe) {
  1344. mutex_unlock(&dev->lock);
  1345. return -EINVAL;
  1346. }
  1347. fe = vfe->dvb.frontend;
  1348. if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)
  1349. fe = &dev->ts1.analog_fe;
  1350. if (fe && fe->ops.tuner_ops.set_analog_params) {
  1351. call_all(dev, core, s_std, dev->tvnorm);
  1352. fe->ops.tuner_ops.set_analog_params(fe, &params);
  1353. }
  1354. else
  1355. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  1356. /* When changing channels it is required to reset TVAUDIO */
  1357. msleep(100);
  1358. /* I need to unmute audio here */
  1359. ctrl.value = 0;
  1360. cx23885_set_control(dev, &ctrl);
  1361. mutex_unlock(&dev->lock);
  1362. return 0;
  1363. }
  1364. int cx23885_set_frequency(struct file *file, void *priv,
  1365. struct v4l2_frequency *f)
  1366. {
  1367. struct cx23885_fh *fh = priv;
  1368. struct cx23885_dev *dev = fh->dev;
  1369. int ret;
  1370. switch (dev->board) {
  1371. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  1372. ret = cx23885_set_freq_via_ops(dev, f);
  1373. break;
  1374. default:
  1375. ret = cx23885_set_freq(dev, f);
  1376. }
  1377. return ret;
  1378. }
  1379. static int vidioc_s_frequency(struct file *file, void *priv,
  1380. struct v4l2_frequency *f)
  1381. {
  1382. return cx23885_set_frequency(file, priv, f);
  1383. }
  1384. /* ----------------------------------------------------------- */
  1385. static void cx23885_vid_timeout(unsigned long data)
  1386. {
  1387. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1388. struct cx23885_dmaqueue *q = &dev->vidq;
  1389. struct cx23885_buffer *buf;
  1390. unsigned long flags;
  1391. spin_lock_irqsave(&dev->slock, flags);
  1392. while (!list_empty(&q->active)) {
  1393. buf = list_entry(q->active.next,
  1394. struct cx23885_buffer, vb.queue);
  1395. list_del(&buf->vb.queue);
  1396. buf->vb.state = VIDEOBUF_ERROR;
  1397. wake_up(&buf->vb.done);
  1398. printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
  1399. dev->name, buf, buf->vb.i,
  1400. (unsigned long)buf->risc.dma);
  1401. }
  1402. cx23885_restart_video_queue(dev, q);
  1403. spin_unlock_irqrestore(&dev->slock, flags);
  1404. }
  1405. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1406. {
  1407. u32 mask, count;
  1408. int handled = 0;
  1409. mask = cx_read(VID_A_INT_MSK);
  1410. if (0 == (status & mask))
  1411. return handled;
  1412. cx_write(VID_A_INT_STAT, status);
  1413. /* risc op code error, fifo overflow or line sync detection error */
  1414. if ((status & VID_BC_MSK_OPC_ERR) ||
  1415. (status & VID_BC_MSK_SYNC) ||
  1416. (status & VID_BC_MSK_OF)) {
  1417. if (status & VID_BC_MSK_OPC_ERR) {
  1418. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  1419. VID_BC_MSK_OPC_ERR);
  1420. printk(KERN_WARNING "%s: video risc op code error\n",
  1421. dev->name);
  1422. cx23885_sram_channel_dump(dev,
  1423. &dev->sram_channels[SRAM_CH01]);
  1424. }
  1425. if (status & VID_BC_MSK_SYNC)
  1426. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  1427. "video lines miss-match\n",
  1428. VID_BC_MSK_SYNC);
  1429. if (status & VID_BC_MSK_OF)
  1430. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  1431. VID_BC_MSK_OF);
  1432. }
  1433. /* Video */
  1434. if (status & VID_BC_MSK_RISCI1) {
  1435. spin_lock(&dev->slock);
  1436. count = cx_read(VID_A_GPCNT);
  1437. cx23885_video_wakeup(dev, &dev->vidq, count);
  1438. spin_unlock(&dev->slock);
  1439. handled++;
  1440. }
  1441. if (status & VID_BC_MSK_RISCI2) {
  1442. dprintk(2, "stopper video\n");
  1443. spin_lock(&dev->slock);
  1444. cx23885_restart_video_queue(dev, &dev->vidq);
  1445. spin_unlock(&dev->slock);
  1446. handled++;
  1447. }
  1448. /* Allow the VBI framework to process it's payload */
  1449. handled += cx23885_vbi_irq(dev, status);
  1450. return handled;
  1451. }
  1452. /* ----------------------------------------------------------- */
  1453. /* exported stuff */
  1454. static const struct v4l2_file_operations video_fops = {
  1455. .owner = THIS_MODULE,
  1456. .open = video_open,
  1457. .release = video_release,
  1458. .read = video_read,
  1459. .poll = video_poll,
  1460. .mmap = video_mmap,
  1461. .ioctl = video_ioctl2,
  1462. };
  1463. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1464. .vidioc_querycap = vidioc_querycap,
  1465. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1466. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1467. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1468. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1469. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1470. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1471. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1472. .vidioc_reqbufs = vidioc_reqbufs,
  1473. .vidioc_querybuf = vidioc_querybuf,
  1474. .vidioc_qbuf = vidioc_qbuf,
  1475. .vidioc_dqbuf = vidioc_dqbuf,
  1476. .vidioc_s_std = vidioc_s_std,
  1477. .vidioc_g_std = vidioc_g_std,
  1478. .vidioc_querystd = vidioc_g_std,
  1479. .vidioc_enum_input = vidioc_enum_input,
  1480. .vidioc_g_input = vidioc_g_input,
  1481. .vidioc_s_input = vidioc_s_input,
  1482. .vidioc_log_status = vidioc_log_status,
  1483. .vidioc_queryctrl = vidioc_queryctrl,
  1484. .vidioc_g_ctrl = vidioc_g_ctrl,
  1485. .vidioc_s_ctrl = vidioc_s_ctrl,
  1486. .vidioc_streamon = vidioc_streamon,
  1487. .vidioc_streamoff = vidioc_streamoff,
  1488. .vidioc_g_tuner = vidioc_g_tuner,
  1489. .vidioc_s_tuner = vidioc_s_tuner,
  1490. .vidioc_g_frequency = vidioc_g_frequency,
  1491. .vidioc_s_frequency = vidioc_s_frequency,
  1492. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1493. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1494. .vidioc_g_register = cx23885_g_register,
  1495. .vidioc_s_register = cx23885_s_register,
  1496. #endif
  1497. .vidioc_enumaudio = vidioc_enum_audinput,
  1498. .vidioc_g_audio = vidioc_g_audinput,
  1499. .vidioc_s_audio = vidioc_s_audinput,
  1500. };
  1501. static struct video_device cx23885_vbi_template;
  1502. static struct video_device cx23885_video_template = {
  1503. .name = "cx23885-video",
  1504. .fops = &video_fops,
  1505. .ioctl_ops = &video_ioctl_ops,
  1506. .tvnorms = CX23885_NORMS,
  1507. .current_norm = V4L2_STD_NTSC_M,
  1508. };
  1509. static const struct v4l2_file_operations radio_fops = {
  1510. .owner = THIS_MODULE,
  1511. .open = video_open,
  1512. .release = video_release,
  1513. .ioctl = video_ioctl2,
  1514. };
  1515. void cx23885_video_unregister(struct cx23885_dev *dev)
  1516. {
  1517. dprintk(1, "%s()\n", __func__);
  1518. cx23885_irq_remove(dev, 0x01);
  1519. if (dev->vbi_dev) {
  1520. if (video_is_registered(dev->vbi_dev))
  1521. video_unregister_device(dev->vbi_dev);
  1522. else
  1523. video_device_release(dev->vbi_dev);
  1524. dev->vbi_dev = NULL;
  1525. btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
  1526. }
  1527. if (dev->video_dev) {
  1528. if (video_is_registered(dev->video_dev))
  1529. video_unregister_device(dev->video_dev);
  1530. else
  1531. video_device_release(dev->video_dev);
  1532. dev->video_dev = NULL;
  1533. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1534. }
  1535. if (dev->audio_dev)
  1536. cx23885_audio_unregister(dev);
  1537. }
  1538. int cx23885_video_register(struct cx23885_dev *dev)
  1539. {
  1540. int err;
  1541. dprintk(1, "%s()\n", __func__);
  1542. spin_lock_init(&dev->slock);
  1543. /* Initialize VBI template */
  1544. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1545. sizeof(cx23885_vbi_template));
  1546. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1547. dev->tvnorm = cx23885_video_template.current_norm;
  1548. /* init video dma queues */
  1549. INIT_LIST_HEAD(&dev->vidq.active);
  1550. INIT_LIST_HEAD(&dev->vidq.queued);
  1551. dev->vidq.timeout.function = cx23885_vid_timeout;
  1552. dev->vidq.timeout.data = (unsigned long)dev;
  1553. init_timer(&dev->vidq.timeout);
  1554. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1555. VID_A_DMA_CTL, 0x11, 0x00);
  1556. /* init vbi dma queues */
  1557. INIT_LIST_HEAD(&dev->vbiq.active);
  1558. INIT_LIST_HEAD(&dev->vbiq.queued);
  1559. dev->vbiq.timeout.function = cx23885_vbi_timeout;
  1560. dev->vbiq.timeout.data = (unsigned long)dev;
  1561. init_timer(&dev->vbiq.timeout);
  1562. cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
  1563. VID_A_DMA_CTL, 0x22, 0x00);
  1564. cx23885_irq_add_enable(dev, 0x01);
  1565. if ((TUNER_ABSENT != dev->tuner_type) &&
  1566. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  1567. struct v4l2_subdev *sd = NULL;
  1568. if (dev->tuner_addr)
  1569. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1570. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1571. "tuner", dev->tuner_addr, NULL);
  1572. else
  1573. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1574. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1575. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1576. if (sd) {
  1577. struct tuner_setup tun_setup;
  1578. memset(&tun_setup, 0, sizeof(tun_setup));
  1579. tun_setup.mode_mask = T_ANALOG_TV;
  1580. tun_setup.type = dev->tuner_type;
  1581. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1582. tun_setup.tuner_callback = cx23885_tuner_callback;
  1583. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1584. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1585. struct xc2028_ctrl ctrl = {
  1586. .fname = XC2028_DEFAULT_FIRMWARE,
  1587. .max_len = 64
  1588. };
  1589. struct v4l2_priv_tun_config cfg = {
  1590. .tuner = dev->tuner_type,
  1591. .priv = &ctrl
  1592. };
  1593. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1594. }
  1595. }
  1596. }
  1597. /* register Video device */
  1598. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1599. &cx23885_video_template, "video");
  1600. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1601. video_nr[dev->nr]);
  1602. if (err < 0) {
  1603. printk(KERN_INFO "%s: can't register video device\n",
  1604. dev->name);
  1605. goto fail_unreg;
  1606. }
  1607. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1608. dev->name, video_device_node_name(dev->video_dev));
  1609. /* register VBI device */
  1610. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1611. &cx23885_vbi_template, "vbi");
  1612. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1613. vbi_nr[dev->nr]);
  1614. if (err < 0) {
  1615. printk(KERN_INFO "%s: can't register vbi device\n",
  1616. dev->name);
  1617. goto fail_unreg;
  1618. }
  1619. printk(KERN_INFO "%s: registered device %s\n",
  1620. dev->name, video_device_node_name(dev->vbi_dev));
  1621. /* Register ALSA audio device */
  1622. dev->audio_dev = cx23885_audio_register(dev);
  1623. /* initial device configuration */
  1624. mutex_lock(&dev->lock);
  1625. cx23885_set_tvnorm(dev, dev->tvnorm);
  1626. init_controls(dev);
  1627. cx23885_video_mux(dev, 0);
  1628. cx23885_audio_mux(dev, 0);
  1629. mutex_unlock(&dev->lock);
  1630. return 0;
  1631. fail_unreg:
  1632. cx23885_video_unregister(dev);
  1633. return err;
  1634. }