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_HVR1850)) {
  449. /* Configure audio routing */
  450. v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
  451. INPUT(input)->amux, 0, 0);
  452. if (INPUT(input)->amux == CX25840_AUDIO7)
  453. cx23885_flatiron_mux(dev, 1);
  454. else if (INPUT(input)->amux == CX25840_AUDIO6)
  455. cx23885_flatiron_mux(dev, 2);
  456. }
  457. return 0;
  458. }
  459. static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
  460. {
  461. dprintk(1, "%s(input=%d)\n", __func__, input);
  462. /* The baseband video core of the cx23885 has two audio inputs.
  463. * LR1 and LR2. In almost every single case so far only HVR1xxx
  464. * cards we've only ever supported LR1. Time to support LR2,
  465. * which is available via the optional white breakout header on
  466. * the board.
  467. * We'll use a could of existing enums in the card struct to allow
  468. * devs to specify which baseband input they need, or just default
  469. * to what we've always used.
  470. */
  471. if (INPUT(input)->amux == CX25840_AUDIO7)
  472. cx23885_flatiron_mux(dev, 1);
  473. else if (INPUT(input)->amux == CX25840_AUDIO6)
  474. cx23885_flatiron_mux(dev, 2);
  475. else {
  476. /* Not specifically defined, assume the default. */
  477. cx23885_flatiron_mux(dev, 1);
  478. }
  479. return 0;
  480. }
  481. /* ------------------------------------------------------------------ */
  482. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  483. struct cx23885_dmaqueue *q,
  484. struct cx23885_buffer *buf)
  485. {
  486. dprintk(1, "%s()\n", __func__);
  487. /* Stop the dma/fifo before we tamper with it's risc programs */
  488. cx_clear(VID_A_DMA_CTL, 0x11);
  489. /* setup fifo + format */
  490. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  491. buf->bpl, buf->risc.dma);
  492. /* reset counter */
  493. cx_write(VID_A_GPCNT_CTL, 3);
  494. q->count = 1;
  495. /* enable irq */
  496. cx23885_irq_add_enable(dev, 0x01);
  497. cx_set(VID_A_INT_MSK, 0x000011);
  498. /* start dma */
  499. cx_set(DEV_CNTRL2, (1<<5));
  500. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  501. return 0;
  502. }
  503. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  504. struct cx23885_dmaqueue *q)
  505. {
  506. struct cx23885_buffer *buf, *prev;
  507. struct list_head *item;
  508. dprintk(1, "%s()\n", __func__);
  509. if (!list_empty(&q->active)) {
  510. buf = list_entry(q->active.next, struct cx23885_buffer,
  511. vb.queue);
  512. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  513. buf, buf->vb.i);
  514. cx23885_start_video_dma(dev, q, buf);
  515. list_for_each(item, &q->active) {
  516. buf = list_entry(item, struct cx23885_buffer,
  517. vb.queue);
  518. buf->count = q->count++;
  519. }
  520. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  521. return 0;
  522. }
  523. prev = NULL;
  524. for (;;) {
  525. if (list_empty(&q->queued))
  526. return 0;
  527. buf = list_entry(q->queued.next, struct cx23885_buffer,
  528. vb.queue);
  529. if (NULL == prev) {
  530. list_move_tail(&buf->vb.queue, &q->active);
  531. cx23885_start_video_dma(dev, q, buf);
  532. buf->vb.state = VIDEOBUF_ACTIVE;
  533. buf->count = q->count++;
  534. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  535. dprintk(2, "[%p/%d] restart_queue - first active\n",
  536. buf, buf->vb.i);
  537. } else if (prev->vb.width == buf->vb.width &&
  538. prev->vb.height == buf->vb.height &&
  539. prev->fmt == buf->fmt) {
  540. list_move_tail(&buf->vb.queue, &q->active);
  541. buf->vb.state = VIDEOBUF_ACTIVE;
  542. buf->count = q->count++;
  543. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  544. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  545. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  546. buf, buf->vb.i);
  547. } else {
  548. return 0;
  549. }
  550. prev = buf;
  551. }
  552. }
  553. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  554. unsigned int *size)
  555. {
  556. struct cx23885_fh *fh = q->priv_data;
  557. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  558. if (0 == *count)
  559. *count = 32;
  560. if (*size * *count > vid_limit * 1024 * 1024)
  561. *count = (vid_limit * 1024 * 1024) / *size;
  562. return 0;
  563. }
  564. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  565. enum v4l2_field field)
  566. {
  567. struct cx23885_fh *fh = q->priv_data;
  568. struct cx23885_dev *dev = fh->dev;
  569. struct cx23885_buffer *buf =
  570. container_of(vb, struct cx23885_buffer, vb);
  571. int rc, init_buffer = 0;
  572. u32 line0_offset, line1_offset;
  573. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  574. int field_tff;
  575. BUG_ON(NULL == fh->fmt);
  576. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  577. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  578. return -EINVAL;
  579. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  580. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  581. return -EINVAL;
  582. if (buf->fmt != fh->fmt ||
  583. buf->vb.width != fh->width ||
  584. buf->vb.height != fh->height ||
  585. buf->vb.field != field) {
  586. buf->fmt = fh->fmt;
  587. buf->vb.width = fh->width;
  588. buf->vb.height = fh->height;
  589. buf->vb.field = field;
  590. init_buffer = 1;
  591. }
  592. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  593. init_buffer = 1;
  594. rc = videobuf_iolock(q, &buf->vb, NULL);
  595. if (0 != rc)
  596. goto fail;
  597. }
  598. if (init_buffer) {
  599. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  600. switch (buf->vb.field) {
  601. case V4L2_FIELD_TOP:
  602. cx23885_risc_buffer(dev->pci, &buf->risc,
  603. dma->sglist, 0, UNSET,
  604. buf->bpl, 0, buf->vb.height);
  605. break;
  606. case V4L2_FIELD_BOTTOM:
  607. cx23885_risc_buffer(dev->pci, &buf->risc,
  608. dma->sglist, UNSET, 0,
  609. buf->bpl, 0, buf->vb.height);
  610. break;
  611. case V4L2_FIELD_INTERLACED:
  612. if (dev->tvnorm & V4L2_STD_NTSC)
  613. /* NTSC or */
  614. field_tff = 1;
  615. else
  616. field_tff = 0;
  617. if (cx23885_boards[dev->board].force_bff)
  618. /* PAL / SECAM OR 888 in NTSC MODE */
  619. field_tff = 0;
  620. if (field_tff) {
  621. /* cx25840 transmits NTSC bottom field first */
  622. dprintk(1, "%s() Creating TFF/NTSC risc\n",
  623. __func__);
  624. line0_offset = buf->bpl;
  625. line1_offset = 0;
  626. } else {
  627. /* All other formats are top field first */
  628. dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
  629. __func__);
  630. line0_offset = 0;
  631. line1_offset = buf->bpl;
  632. }
  633. cx23885_risc_buffer(dev->pci, &buf->risc,
  634. dma->sglist, line0_offset,
  635. line1_offset,
  636. buf->bpl, buf->bpl,
  637. buf->vb.height >> 1);
  638. break;
  639. case V4L2_FIELD_SEQ_TB:
  640. cx23885_risc_buffer(dev->pci, &buf->risc,
  641. dma->sglist,
  642. 0, buf->bpl * (buf->vb.height >> 1),
  643. buf->bpl, 0,
  644. buf->vb.height >> 1);
  645. break;
  646. case V4L2_FIELD_SEQ_BT:
  647. cx23885_risc_buffer(dev->pci, &buf->risc,
  648. dma->sglist,
  649. buf->bpl * (buf->vb.height >> 1), 0,
  650. buf->bpl, 0,
  651. buf->vb.height >> 1);
  652. break;
  653. default:
  654. BUG();
  655. }
  656. }
  657. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  658. buf, buf->vb.i,
  659. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  660. (unsigned long)buf->risc.dma);
  661. buf->vb.state = VIDEOBUF_PREPARED;
  662. return 0;
  663. fail:
  664. cx23885_free_buffer(q, buf);
  665. return rc;
  666. }
  667. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  668. {
  669. struct cx23885_buffer *buf = container_of(vb,
  670. struct cx23885_buffer, vb);
  671. struct cx23885_buffer *prev;
  672. struct cx23885_fh *fh = vq->priv_data;
  673. struct cx23885_dev *dev = fh->dev;
  674. struct cx23885_dmaqueue *q = &dev->vidq;
  675. /* add jump to stopper */
  676. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  677. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  678. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  679. if (!list_empty(&q->queued)) {
  680. list_add_tail(&buf->vb.queue, &q->queued);
  681. buf->vb.state = VIDEOBUF_QUEUED;
  682. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  683. buf, buf->vb.i);
  684. } else if (list_empty(&q->active)) {
  685. list_add_tail(&buf->vb.queue, &q->active);
  686. cx23885_start_video_dma(dev, q, buf);
  687. buf->vb.state = VIDEOBUF_ACTIVE;
  688. buf->count = q->count++;
  689. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  690. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  691. buf, buf->vb.i);
  692. } else {
  693. prev = list_entry(q->active.prev, struct cx23885_buffer,
  694. vb.queue);
  695. if (prev->vb.width == buf->vb.width &&
  696. prev->vb.height == buf->vb.height &&
  697. prev->fmt == buf->fmt) {
  698. list_add_tail(&buf->vb.queue, &q->active);
  699. buf->vb.state = VIDEOBUF_ACTIVE;
  700. buf->count = q->count++;
  701. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  702. /* 64 bit bits 63-32 */
  703. prev->risc.jmp[2] = cpu_to_le32(0);
  704. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  705. buf, buf->vb.i);
  706. } else {
  707. list_add_tail(&buf->vb.queue, &q->queued);
  708. buf->vb.state = VIDEOBUF_QUEUED;
  709. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  710. buf, buf->vb.i);
  711. }
  712. }
  713. }
  714. static void buffer_release(struct videobuf_queue *q,
  715. struct videobuf_buffer *vb)
  716. {
  717. struct cx23885_buffer *buf = container_of(vb,
  718. struct cx23885_buffer, vb);
  719. cx23885_free_buffer(q, buf);
  720. }
  721. static struct videobuf_queue_ops cx23885_video_qops = {
  722. .buf_setup = buffer_setup,
  723. .buf_prepare = buffer_prepare,
  724. .buf_queue = buffer_queue,
  725. .buf_release = buffer_release,
  726. };
  727. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  728. {
  729. switch (fh->type) {
  730. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  731. return &fh->vidq;
  732. case V4L2_BUF_TYPE_VBI_CAPTURE:
  733. return &fh->vbiq;
  734. default:
  735. BUG();
  736. return NULL;
  737. }
  738. }
  739. static int get_resource(struct cx23885_fh *fh)
  740. {
  741. switch (fh->type) {
  742. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  743. return RESOURCE_VIDEO;
  744. case V4L2_BUF_TYPE_VBI_CAPTURE:
  745. return RESOURCE_VBI;
  746. default:
  747. BUG();
  748. return 0;
  749. }
  750. }
  751. static int video_open(struct file *file)
  752. {
  753. struct video_device *vdev = video_devdata(file);
  754. struct cx23885_dev *dev = video_drvdata(file);
  755. struct cx23885_fh *fh;
  756. enum v4l2_buf_type type = 0;
  757. int radio = 0;
  758. switch (vdev->vfl_type) {
  759. case VFL_TYPE_GRABBER:
  760. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  761. break;
  762. case VFL_TYPE_VBI:
  763. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  764. break;
  765. case VFL_TYPE_RADIO:
  766. radio = 1;
  767. break;
  768. }
  769. dprintk(1, "open dev=%s radio=%d type=%s\n",
  770. video_device_node_name(vdev), radio, v4l2_type_names[type]);
  771. /* allocate + initialize per filehandle data */
  772. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  773. if (NULL == fh)
  774. return -ENOMEM;
  775. file->private_data = fh;
  776. fh->dev = dev;
  777. fh->radio = radio;
  778. fh->type = type;
  779. fh->width = 320;
  780. fh->height = 240;
  781. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
  782. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  783. &dev->pci->dev, &dev->slock,
  784. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  785. V4L2_FIELD_INTERLACED,
  786. sizeof(struct cx23885_buffer),
  787. fh, NULL);
  788. videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
  789. &dev->pci->dev, &dev->slock,
  790. V4L2_BUF_TYPE_VBI_CAPTURE,
  791. V4L2_FIELD_SEQ_TB,
  792. sizeof(struct cx23885_buffer),
  793. fh, NULL);
  794. dprintk(1, "post videobuf_queue_init()\n");
  795. return 0;
  796. }
  797. static ssize_t video_read(struct file *file, char __user *data,
  798. size_t count, loff_t *ppos)
  799. {
  800. struct cx23885_fh *fh = file->private_data;
  801. switch (fh->type) {
  802. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  803. if (res_locked(fh->dev, RESOURCE_VIDEO))
  804. return -EBUSY;
  805. return videobuf_read_one(&fh->vidq, data, count, ppos,
  806. file->f_flags & O_NONBLOCK);
  807. case V4L2_BUF_TYPE_VBI_CAPTURE:
  808. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  809. return -EBUSY;
  810. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  811. file->f_flags & O_NONBLOCK);
  812. default:
  813. BUG();
  814. return 0;
  815. }
  816. }
  817. static unsigned int video_poll(struct file *file,
  818. struct poll_table_struct *wait)
  819. {
  820. struct cx23885_fh *fh = file->private_data;
  821. struct cx23885_buffer *buf;
  822. unsigned int rc = POLLERR;
  823. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  824. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  825. return POLLERR;
  826. return videobuf_poll_stream(file, &fh->vbiq, wait);
  827. }
  828. mutex_lock(&fh->vidq.vb_lock);
  829. if (res_check(fh, RESOURCE_VIDEO)) {
  830. /* streaming capture */
  831. if (list_empty(&fh->vidq.stream))
  832. goto done;
  833. buf = list_entry(fh->vidq.stream.next,
  834. struct cx23885_buffer, vb.stream);
  835. } else {
  836. /* read() capture */
  837. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  838. if (NULL == buf)
  839. goto done;
  840. }
  841. poll_wait(file, &buf->vb.done, wait);
  842. if (buf->vb.state == VIDEOBUF_DONE ||
  843. buf->vb.state == VIDEOBUF_ERROR)
  844. rc = POLLIN|POLLRDNORM;
  845. else
  846. rc = 0;
  847. done:
  848. mutex_unlock(&fh->vidq.vb_lock);
  849. return rc;
  850. }
  851. static int video_release(struct file *file)
  852. {
  853. struct cx23885_fh *fh = file->private_data;
  854. struct cx23885_dev *dev = fh->dev;
  855. /* turn off overlay */
  856. if (res_check(fh, RESOURCE_OVERLAY)) {
  857. /* FIXME */
  858. res_free(dev, fh, RESOURCE_OVERLAY);
  859. }
  860. /* stop video capture */
  861. if (res_check(fh, RESOURCE_VIDEO)) {
  862. videobuf_queue_cancel(&fh->vidq);
  863. res_free(dev, fh, RESOURCE_VIDEO);
  864. }
  865. if (fh->vidq.read_buf) {
  866. buffer_release(&fh->vidq, fh->vidq.read_buf);
  867. kfree(fh->vidq.read_buf);
  868. }
  869. /* stop vbi capture */
  870. if (res_check(fh, RESOURCE_VBI)) {
  871. if (fh->vbiq.streaming)
  872. videobuf_streamoff(&fh->vbiq);
  873. if (fh->vbiq.reading)
  874. videobuf_read_stop(&fh->vbiq);
  875. res_free(dev, fh, RESOURCE_VBI);
  876. }
  877. videobuf_mmap_free(&fh->vidq);
  878. videobuf_mmap_free(&fh->vbiq);
  879. file->private_data = NULL;
  880. kfree(fh);
  881. /* We are not putting the tuner to sleep here on exit, because
  882. * we want to use the mpeg encoder in another session to capture
  883. * tuner video. Closing this will result in no video to the encoder.
  884. */
  885. return 0;
  886. }
  887. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  888. {
  889. struct cx23885_fh *fh = file->private_data;
  890. return videobuf_mmap_mapper(get_queue(fh), vma);
  891. }
  892. /* ------------------------------------------------------------------ */
  893. /* VIDEO CTRL IOCTLS */
  894. int cx23885_get_control(struct cx23885_dev *dev,
  895. struct v4l2_control *ctl)
  896. {
  897. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
  898. call_all(dev, core, g_ctrl, ctl);
  899. return 0;
  900. }
  901. int cx23885_set_control(struct cx23885_dev *dev,
  902. struct v4l2_control *ctl)
  903. {
  904. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
  905. call_all(dev, core, s_ctrl, ctl);
  906. return 0;
  907. }
  908. static void init_controls(struct cx23885_dev *dev)
  909. {
  910. struct v4l2_control ctrl;
  911. int i;
  912. for (i = 0; i < CX23885_CTLS; i++) {
  913. ctrl.id = cx23885_ctls[i].v.id;
  914. ctrl.value = cx23885_ctls[i].v.default_value;
  915. cx23885_set_control(dev, &ctrl);
  916. }
  917. }
  918. /* ------------------------------------------------------------------ */
  919. /* VIDEO IOCTLS */
  920. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  921. struct v4l2_format *f)
  922. {
  923. struct cx23885_fh *fh = priv;
  924. f->fmt.pix.width = fh->width;
  925. f->fmt.pix.height = fh->height;
  926. f->fmt.pix.field = fh->vidq.field;
  927. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  928. f->fmt.pix.bytesperline =
  929. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  930. f->fmt.pix.sizeimage =
  931. f->fmt.pix.height * f->fmt.pix.bytesperline;
  932. return 0;
  933. }
  934. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  935. struct v4l2_format *f)
  936. {
  937. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  938. struct cx23885_fmt *fmt;
  939. enum v4l2_field field;
  940. unsigned int maxw, maxh;
  941. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  942. if (NULL == fmt)
  943. return -EINVAL;
  944. field = f->fmt.pix.field;
  945. maxw = norm_maxw(dev->tvnorm);
  946. maxh = norm_maxh(dev->tvnorm);
  947. if (V4L2_FIELD_ANY == field) {
  948. field = (f->fmt.pix.height > maxh/2)
  949. ? V4L2_FIELD_INTERLACED
  950. : V4L2_FIELD_BOTTOM;
  951. }
  952. switch (field) {
  953. case V4L2_FIELD_TOP:
  954. case V4L2_FIELD_BOTTOM:
  955. maxh = maxh / 2;
  956. break;
  957. case V4L2_FIELD_INTERLACED:
  958. break;
  959. default:
  960. return -EINVAL;
  961. }
  962. f->fmt.pix.field = field;
  963. v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
  964. &f->fmt.pix.height, 32, maxh, 0, 0);
  965. f->fmt.pix.bytesperline =
  966. (f->fmt.pix.width * fmt->depth) >> 3;
  967. f->fmt.pix.sizeimage =
  968. f->fmt.pix.height * f->fmt.pix.bytesperline;
  969. return 0;
  970. }
  971. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  972. struct v4l2_format *f)
  973. {
  974. struct cx23885_fh *fh = priv;
  975. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  976. struct v4l2_mbus_framefmt mbus_fmt;
  977. int err;
  978. dprintk(2, "%s()\n", __func__);
  979. err = vidioc_try_fmt_vid_cap(file, priv, f);
  980. if (0 != err)
  981. return err;
  982. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  983. fh->width = f->fmt.pix.width;
  984. fh->height = f->fmt.pix.height;
  985. fh->vidq.field = f->fmt.pix.field;
  986. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  987. fh->width, fh->height, fh->vidq.field);
  988. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  989. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  990. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  991. return 0;
  992. }
  993. static int vidioc_querycap(struct file *file, void *priv,
  994. struct v4l2_capability *cap)
  995. {
  996. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  997. strcpy(cap->driver, "cx23885");
  998. strlcpy(cap->card, cx23885_boards[dev->board].name,
  999. sizeof(cap->card));
  1000. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1001. cap->capabilities =
  1002. V4L2_CAP_VIDEO_CAPTURE |
  1003. V4L2_CAP_READWRITE |
  1004. V4L2_CAP_STREAMING |
  1005. V4L2_CAP_VBI_CAPTURE;
  1006. if (UNSET != dev->tuner_type)
  1007. cap->capabilities |= V4L2_CAP_TUNER;
  1008. return 0;
  1009. }
  1010. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1011. struct v4l2_fmtdesc *f)
  1012. {
  1013. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  1014. return -EINVAL;
  1015. strlcpy(f->description, formats[f->index].name,
  1016. sizeof(f->description));
  1017. f->pixelformat = formats[f->index].fourcc;
  1018. return 0;
  1019. }
  1020. static int vidioc_reqbufs(struct file *file, void *priv,
  1021. struct v4l2_requestbuffers *p)
  1022. {
  1023. struct cx23885_fh *fh = priv;
  1024. return videobuf_reqbufs(get_queue(fh), p);
  1025. }
  1026. static int vidioc_querybuf(struct file *file, void *priv,
  1027. struct v4l2_buffer *p)
  1028. {
  1029. struct cx23885_fh *fh = priv;
  1030. return videobuf_querybuf(get_queue(fh), p);
  1031. }
  1032. static int vidioc_qbuf(struct file *file, void *priv,
  1033. struct v4l2_buffer *p)
  1034. {
  1035. struct cx23885_fh *fh = priv;
  1036. return videobuf_qbuf(get_queue(fh), p);
  1037. }
  1038. static int vidioc_dqbuf(struct file *file, void *priv,
  1039. struct v4l2_buffer *p)
  1040. {
  1041. struct cx23885_fh *fh = priv;
  1042. return videobuf_dqbuf(get_queue(fh), p,
  1043. file->f_flags & O_NONBLOCK);
  1044. }
  1045. static int vidioc_streamon(struct file *file, void *priv,
  1046. enum v4l2_buf_type i)
  1047. {
  1048. struct cx23885_fh *fh = priv;
  1049. struct cx23885_dev *dev = fh->dev;
  1050. dprintk(1, "%s()\n", __func__);
  1051. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1052. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1053. return -EINVAL;
  1054. if (unlikely(i != fh->type))
  1055. return -EINVAL;
  1056. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  1057. return -EBUSY;
  1058. /* Don't start VBI streaming unless vida streaming
  1059. * has already started.
  1060. */
  1061. if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
  1062. ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
  1063. return -EINVAL;
  1064. return videobuf_streamon(get_queue(fh));
  1065. }
  1066. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  1067. {
  1068. struct cx23885_fh *fh = priv;
  1069. struct cx23885_dev *dev = fh->dev;
  1070. int err, res;
  1071. dprintk(1, "%s()\n", __func__);
  1072. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1073. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1074. return -EINVAL;
  1075. if (i != fh->type)
  1076. return -EINVAL;
  1077. res = get_resource(fh);
  1078. err = videobuf_streamoff(get_queue(fh));
  1079. if (err < 0)
  1080. return err;
  1081. res_free(dev, fh, res);
  1082. return 0;
  1083. }
  1084. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1085. {
  1086. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1087. dprintk(1, "%s()\n", __func__);
  1088. call_all(dev, core, g_std, id);
  1089. return 0;
  1090. }
  1091. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  1092. {
  1093. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1094. dprintk(1, "%s()\n", __func__);
  1095. mutex_lock(&dev->lock);
  1096. cx23885_set_tvnorm(dev, *tvnorms);
  1097. mutex_unlock(&dev->lock);
  1098. return 0;
  1099. }
  1100. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1101. {
  1102. static const char *iname[] = {
  1103. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1104. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1105. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1106. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1107. [CX23885_VMUX_SVIDEO] = "S-Video",
  1108. [CX23885_VMUX_COMPONENT] = "Component",
  1109. [CX23885_VMUX_TELEVISION] = "Television",
  1110. [CX23885_VMUX_CABLE] = "Cable TV",
  1111. [CX23885_VMUX_DVB] = "DVB",
  1112. [CX23885_VMUX_DEBUG] = "for debug only",
  1113. };
  1114. unsigned int n;
  1115. dprintk(1, "%s()\n", __func__);
  1116. n = i->index;
  1117. if (n >= MAX_CX23885_INPUT)
  1118. return -EINVAL;
  1119. if (0 == INPUT(n)->type)
  1120. return -EINVAL;
  1121. i->index = n;
  1122. i->type = V4L2_INPUT_TYPE_CAMERA;
  1123. strcpy(i->name, iname[INPUT(n)->type]);
  1124. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1125. (CX23885_VMUX_CABLE == INPUT(n)->type)) {
  1126. i->type = V4L2_INPUT_TYPE_TUNER;
  1127. i->std = CX23885_NORMS;
  1128. }
  1129. /* Two selectable audio inputs for non-tv inputs */
  1130. if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
  1131. i->audioset = 0x3;
  1132. if (dev->input == n) {
  1133. /* enum'd input matches our configured input.
  1134. * Ask the video decoder to process the call
  1135. * and give it an oppertunity to update the
  1136. * status field.
  1137. */
  1138. call_all(dev, video, g_input_status, &i->status);
  1139. }
  1140. return 0;
  1141. }
  1142. static int vidioc_enum_input(struct file *file, void *priv,
  1143. struct v4l2_input *i)
  1144. {
  1145. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1146. dprintk(1, "%s()\n", __func__);
  1147. return cx23885_enum_input(dev, i);
  1148. }
  1149. int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
  1150. {
  1151. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1152. *i = dev->input;
  1153. dprintk(1, "%s() returns %d\n", __func__, *i);
  1154. return 0;
  1155. }
  1156. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1157. {
  1158. return cx23885_get_input(file, priv, i);
  1159. }
  1160. int cx23885_set_input(struct file *file, void *priv, unsigned int i)
  1161. {
  1162. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1163. dprintk(1, "%s(%d)\n", __func__, i);
  1164. if (i >= MAX_CX23885_INPUT) {
  1165. dprintk(1, "%s() -EINVAL\n", __func__);
  1166. return -EINVAL;
  1167. }
  1168. if (INPUT(i)->type == 0)
  1169. return -EINVAL;
  1170. mutex_lock(&dev->lock);
  1171. cx23885_video_mux(dev, i);
  1172. /* By default establish the default audio input for the card also */
  1173. /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
  1174. cx23885_audio_mux(dev, i);
  1175. mutex_unlock(&dev->lock);
  1176. return 0;
  1177. }
  1178. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1179. {
  1180. return cx23885_set_input(file, priv, i);
  1181. }
  1182. static int vidioc_log_status(struct file *file, void *priv)
  1183. {
  1184. struct cx23885_fh *fh = priv;
  1185. struct cx23885_dev *dev = fh->dev;
  1186. printk(KERN_INFO
  1187. "%s/0: ============ START LOG STATUS ============\n",
  1188. dev->name);
  1189. call_all(dev, core, log_status);
  1190. printk(KERN_INFO
  1191. "%s/0: ============= END LOG STATUS =============\n",
  1192. dev->name);
  1193. return 0;
  1194. }
  1195. static int cx23885_query_audinput(struct file *file, void *priv,
  1196. struct v4l2_audio *i)
  1197. {
  1198. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1199. static const char *iname[] = {
  1200. [0] = "Baseband L/R 1",
  1201. [1] = "Baseband L/R 2",
  1202. };
  1203. unsigned int n;
  1204. dprintk(1, "%s()\n", __func__);
  1205. n = i->index;
  1206. if (n >= 2)
  1207. return -EINVAL;
  1208. memset(i, 0, sizeof(*i));
  1209. i->index = n;
  1210. strcpy(i->name, iname[n]);
  1211. i->capability = V4L2_AUDCAP_STEREO;
  1212. i->mode = V4L2_AUDMODE_AVL;
  1213. return 0;
  1214. }
  1215. static int vidioc_enum_audinput(struct file *file, void *priv,
  1216. struct v4l2_audio *i)
  1217. {
  1218. return cx23885_query_audinput(file, priv, i);
  1219. }
  1220. static int vidioc_g_audinput(struct file *file, void *priv,
  1221. struct v4l2_audio *i)
  1222. {
  1223. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1224. i->index = dev->audinput;
  1225. dprintk(1, "%s(input=%d)\n", __func__, i->index);
  1226. return cx23885_query_audinput(file, priv, i);
  1227. }
  1228. static int vidioc_s_audinput(struct file *file, void *priv,
  1229. struct v4l2_audio *i)
  1230. {
  1231. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1232. if (i->index >= 2)
  1233. return -EINVAL;
  1234. dprintk(1, "%s(%d)\n", __func__, i->index);
  1235. dev->audinput = i->index;
  1236. /* Skip the audio defaults from the cards struct, caller wants
  1237. * directly touch the audio mux hardware. */
  1238. cx23885_flatiron_mux(dev, dev->audinput + 1);
  1239. return 0;
  1240. }
  1241. static int vidioc_queryctrl(struct file *file, void *priv,
  1242. struct v4l2_queryctrl *qctrl)
  1243. {
  1244. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1245. if (unlikely(qctrl->id == 0))
  1246. return -EINVAL;
  1247. return cx23885_ctrl_query(qctrl);
  1248. }
  1249. static int vidioc_g_ctrl(struct file *file, void *priv,
  1250. struct v4l2_control *ctl)
  1251. {
  1252. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1253. return cx23885_get_control(dev, ctl);
  1254. }
  1255. static int vidioc_s_ctrl(struct file *file, void *priv,
  1256. struct v4l2_control *ctl)
  1257. {
  1258. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1259. return cx23885_set_control(dev, ctl);
  1260. }
  1261. static int vidioc_g_tuner(struct file *file, void *priv,
  1262. struct v4l2_tuner *t)
  1263. {
  1264. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1265. if (unlikely(UNSET == dev->tuner_type))
  1266. return -EINVAL;
  1267. if (0 != t->index)
  1268. return -EINVAL;
  1269. strcpy(t->name, "Television");
  1270. call_all(dev, tuner, g_tuner, t);
  1271. return 0;
  1272. }
  1273. static int vidioc_s_tuner(struct file *file, void *priv,
  1274. struct v4l2_tuner *t)
  1275. {
  1276. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1277. if (UNSET == dev->tuner_type)
  1278. return -EINVAL;
  1279. if (0 != t->index)
  1280. return -EINVAL;
  1281. /* Update the A/V core */
  1282. call_all(dev, tuner, s_tuner, t);
  1283. return 0;
  1284. }
  1285. static int vidioc_g_frequency(struct file *file, void *priv,
  1286. struct v4l2_frequency *f)
  1287. {
  1288. struct cx23885_fh *fh = priv;
  1289. struct cx23885_dev *dev = fh->dev;
  1290. if (unlikely(UNSET == dev->tuner_type))
  1291. return -EINVAL;
  1292. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1293. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1294. f->frequency = dev->freq;
  1295. call_all(dev, tuner, g_frequency, f);
  1296. return 0;
  1297. }
  1298. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1299. {
  1300. struct v4l2_control ctrl;
  1301. if (unlikely(UNSET == dev->tuner_type))
  1302. return -EINVAL;
  1303. if (unlikely(f->tuner != 0))
  1304. return -EINVAL;
  1305. mutex_lock(&dev->lock);
  1306. dev->freq = f->frequency;
  1307. /* I need to mute audio here */
  1308. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1309. ctrl.value = 1;
  1310. cx23885_set_control(dev, &ctrl);
  1311. call_all(dev, tuner, s_frequency, f);
  1312. /* When changing channels it is required to reset TVAUDIO */
  1313. msleep(100);
  1314. /* I need to unmute audio here */
  1315. ctrl.value = 0;
  1316. cx23885_set_control(dev, &ctrl);
  1317. mutex_unlock(&dev->lock);
  1318. return 0;
  1319. }
  1320. static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
  1321. struct v4l2_frequency *f)
  1322. {
  1323. struct v4l2_control ctrl;
  1324. struct videobuf_dvb_frontend *vfe;
  1325. struct dvb_frontend *fe;
  1326. struct analog_parameters params = {
  1327. .mode = V4L2_TUNER_ANALOG_TV,
  1328. .audmode = V4L2_TUNER_MODE_STEREO,
  1329. .std = dev->tvnorm,
  1330. .frequency = f->frequency
  1331. };
  1332. mutex_lock(&dev->lock);
  1333. dev->freq = f->frequency;
  1334. /* I need to mute audio here */
  1335. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1336. ctrl.value = 1;
  1337. cx23885_set_control(dev, &ctrl);
  1338. /* If HVR1850 */
  1339. dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
  1340. params.frequency, f->tuner, params.std);
  1341. vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
  1342. if (!vfe) {
  1343. mutex_unlock(&dev->lock);
  1344. return -EINVAL;
  1345. }
  1346. fe = vfe->dvb.frontend;
  1347. if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)
  1348. fe = &dev->ts1.analog_fe;
  1349. if (fe && fe->ops.tuner_ops.set_analog_params) {
  1350. call_all(dev, core, s_std, dev->tvnorm);
  1351. fe->ops.tuner_ops.set_analog_params(fe, &params);
  1352. }
  1353. else
  1354. printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
  1355. /* When changing channels it is required to reset TVAUDIO */
  1356. msleep(100);
  1357. /* I need to unmute audio here */
  1358. ctrl.value = 0;
  1359. cx23885_set_control(dev, &ctrl);
  1360. mutex_unlock(&dev->lock);
  1361. return 0;
  1362. }
  1363. int cx23885_set_frequency(struct file *file, void *priv,
  1364. struct v4l2_frequency *f)
  1365. {
  1366. struct cx23885_fh *fh = priv;
  1367. struct cx23885_dev *dev = fh->dev;
  1368. int ret;
  1369. switch (dev->board) {
  1370. case CX23885_BOARD_HAUPPAUGE_HVR1850:
  1371. ret = cx23885_set_freq_via_ops(dev, f);
  1372. break;
  1373. default:
  1374. ret = cx23885_set_freq(dev, f);
  1375. }
  1376. return ret;
  1377. }
  1378. static int vidioc_s_frequency(struct file *file, void *priv,
  1379. struct v4l2_frequency *f)
  1380. {
  1381. return cx23885_set_frequency(file, priv, f);
  1382. }
  1383. /* ----------------------------------------------------------- */
  1384. static void cx23885_vid_timeout(unsigned long data)
  1385. {
  1386. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1387. struct cx23885_dmaqueue *q = &dev->vidq;
  1388. struct cx23885_buffer *buf;
  1389. unsigned long flags;
  1390. spin_lock_irqsave(&dev->slock, flags);
  1391. while (!list_empty(&q->active)) {
  1392. buf = list_entry(q->active.next,
  1393. struct cx23885_buffer, vb.queue);
  1394. list_del(&buf->vb.queue);
  1395. buf->vb.state = VIDEOBUF_ERROR;
  1396. wake_up(&buf->vb.done);
  1397. printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
  1398. dev->name, buf, buf->vb.i,
  1399. (unsigned long)buf->risc.dma);
  1400. }
  1401. cx23885_restart_video_queue(dev, q);
  1402. spin_unlock_irqrestore(&dev->slock, flags);
  1403. }
  1404. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1405. {
  1406. u32 mask, count;
  1407. int handled = 0;
  1408. mask = cx_read(VID_A_INT_MSK);
  1409. if (0 == (status & mask))
  1410. return handled;
  1411. cx_write(VID_A_INT_STAT, status);
  1412. /* risc op code error, fifo overflow or line sync detection error */
  1413. if ((status & VID_BC_MSK_OPC_ERR) ||
  1414. (status & VID_BC_MSK_SYNC) ||
  1415. (status & VID_BC_MSK_OF)) {
  1416. if (status & VID_BC_MSK_OPC_ERR) {
  1417. dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
  1418. VID_BC_MSK_OPC_ERR);
  1419. printk(KERN_WARNING "%s: video risc op code error\n",
  1420. dev->name);
  1421. cx23885_sram_channel_dump(dev,
  1422. &dev->sram_channels[SRAM_CH01]);
  1423. }
  1424. if (status & VID_BC_MSK_SYNC)
  1425. dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
  1426. "video lines miss-match\n",
  1427. VID_BC_MSK_SYNC);
  1428. if (status & VID_BC_MSK_OF)
  1429. dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
  1430. VID_BC_MSK_OF);
  1431. }
  1432. /* Video */
  1433. if (status & VID_BC_MSK_RISCI1) {
  1434. spin_lock(&dev->slock);
  1435. count = cx_read(VID_A_GPCNT);
  1436. cx23885_video_wakeup(dev, &dev->vidq, count);
  1437. spin_unlock(&dev->slock);
  1438. handled++;
  1439. }
  1440. if (status & VID_BC_MSK_RISCI2) {
  1441. dprintk(2, "stopper video\n");
  1442. spin_lock(&dev->slock);
  1443. cx23885_restart_video_queue(dev, &dev->vidq);
  1444. spin_unlock(&dev->slock);
  1445. handled++;
  1446. }
  1447. /* Allow the VBI framework to process it's payload */
  1448. handled += cx23885_vbi_irq(dev, status);
  1449. return handled;
  1450. }
  1451. /* ----------------------------------------------------------- */
  1452. /* exported stuff */
  1453. static const struct v4l2_file_operations video_fops = {
  1454. .owner = THIS_MODULE,
  1455. .open = video_open,
  1456. .release = video_release,
  1457. .read = video_read,
  1458. .poll = video_poll,
  1459. .mmap = video_mmap,
  1460. .ioctl = video_ioctl2,
  1461. };
  1462. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1463. .vidioc_querycap = vidioc_querycap,
  1464. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1465. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1466. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1467. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1468. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1469. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1470. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1471. .vidioc_reqbufs = vidioc_reqbufs,
  1472. .vidioc_querybuf = vidioc_querybuf,
  1473. .vidioc_qbuf = vidioc_qbuf,
  1474. .vidioc_dqbuf = vidioc_dqbuf,
  1475. .vidioc_s_std = vidioc_s_std,
  1476. .vidioc_g_std = vidioc_g_std,
  1477. .vidioc_querystd = vidioc_g_std,
  1478. .vidioc_enum_input = vidioc_enum_input,
  1479. .vidioc_g_input = vidioc_g_input,
  1480. .vidioc_s_input = vidioc_s_input,
  1481. .vidioc_log_status = vidioc_log_status,
  1482. .vidioc_queryctrl = vidioc_queryctrl,
  1483. .vidioc_g_ctrl = vidioc_g_ctrl,
  1484. .vidioc_s_ctrl = vidioc_s_ctrl,
  1485. .vidioc_streamon = vidioc_streamon,
  1486. .vidioc_streamoff = vidioc_streamoff,
  1487. .vidioc_g_tuner = vidioc_g_tuner,
  1488. .vidioc_s_tuner = vidioc_s_tuner,
  1489. .vidioc_g_frequency = vidioc_g_frequency,
  1490. .vidioc_s_frequency = vidioc_s_frequency,
  1491. .vidioc_g_chip_ident = cx23885_g_chip_ident,
  1492. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1493. .vidioc_g_register = cx23885_g_register,
  1494. .vidioc_s_register = cx23885_s_register,
  1495. #endif
  1496. .vidioc_enumaudio = vidioc_enum_audinput,
  1497. .vidioc_g_audio = vidioc_g_audinput,
  1498. .vidioc_s_audio = vidioc_s_audinput,
  1499. };
  1500. static struct video_device cx23885_vbi_template;
  1501. static struct video_device cx23885_video_template = {
  1502. .name = "cx23885-video",
  1503. .fops = &video_fops,
  1504. .ioctl_ops = &video_ioctl_ops,
  1505. .tvnorms = CX23885_NORMS,
  1506. .current_norm = V4L2_STD_NTSC_M,
  1507. };
  1508. static const struct v4l2_file_operations radio_fops = {
  1509. .owner = THIS_MODULE,
  1510. .open = video_open,
  1511. .release = video_release,
  1512. .ioctl = video_ioctl2,
  1513. };
  1514. void cx23885_video_unregister(struct cx23885_dev *dev)
  1515. {
  1516. dprintk(1, "%s()\n", __func__);
  1517. cx23885_irq_remove(dev, 0x01);
  1518. if (dev->vbi_dev) {
  1519. if (video_is_registered(dev->vbi_dev))
  1520. video_unregister_device(dev->vbi_dev);
  1521. else
  1522. video_device_release(dev->vbi_dev);
  1523. dev->vbi_dev = NULL;
  1524. btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
  1525. }
  1526. if (dev->video_dev) {
  1527. if (video_is_registered(dev->video_dev))
  1528. video_unregister_device(dev->video_dev);
  1529. else
  1530. video_device_release(dev->video_dev);
  1531. dev->video_dev = NULL;
  1532. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1533. }
  1534. if (dev->audio_dev)
  1535. cx23885_audio_unregister(dev);
  1536. }
  1537. int cx23885_video_register(struct cx23885_dev *dev)
  1538. {
  1539. int err;
  1540. dprintk(1, "%s()\n", __func__);
  1541. spin_lock_init(&dev->slock);
  1542. /* Initialize VBI template */
  1543. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1544. sizeof(cx23885_vbi_template));
  1545. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1546. dev->tvnorm = cx23885_video_template.current_norm;
  1547. /* init video dma queues */
  1548. INIT_LIST_HEAD(&dev->vidq.active);
  1549. INIT_LIST_HEAD(&dev->vidq.queued);
  1550. dev->vidq.timeout.function = cx23885_vid_timeout;
  1551. dev->vidq.timeout.data = (unsigned long)dev;
  1552. init_timer(&dev->vidq.timeout);
  1553. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1554. VID_A_DMA_CTL, 0x11, 0x00);
  1555. /* init vbi dma queues */
  1556. INIT_LIST_HEAD(&dev->vbiq.active);
  1557. INIT_LIST_HEAD(&dev->vbiq.queued);
  1558. dev->vbiq.timeout.function = cx23885_vbi_timeout;
  1559. dev->vbiq.timeout.data = (unsigned long)dev;
  1560. init_timer(&dev->vbiq.timeout);
  1561. cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
  1562. VID_A_DMA_CTL, 0x22, 0x00);
  1563. cx23885_irq_add_enable(dev, 0x01);
  1564. if ((TUNER_ABSENT != dev->tuner_type) &&
  1565. ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
  1566. struct v4l2_subdev *sd = NULL;
  1567. if (dev->tuner_addr)
  1568. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1569. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1570. "tuner", dev->tuner_addr, NULL);
  1571. else
  1572. sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
  1573. &dev->i2c_bus[dev->tuner_bus].i2c_adap,
  1574. "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
  1575. if (sd) {
  1576. struct tuner_setup tun_setup;
  1577. memset(&tun_setup, 0, sizeof(tun_setup));
  1578. tun_setup.mode_mask = T_ANALOG_TV;
  1579. tun_setup.type = dev->tuner_type;
  1580. tun_setup.addr = v4l2_i2c_subdev_addr(sd);
  1581. tun_setup.tuner_callback = cx23885_tuner_callback;
  1582. v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
  1583. if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
  1584. struct xc2028_ctrl ctrl = {
  1585. .fname = XC2028_DEFAULT_FIRMWARE,
  1586. .max_len = 64
  1587. };
  1588. struct v4l2_priv_tun_config cfg = {
  1589. .tuner = dev->tuner_type,
  1590. .priv = &ctrl
  1591. };
  1592. v4l2_subdev_call(sd, tuner, s_config, &cfg);
  1593. }
  1594. }
  1595. }
  1596. /* register Video device */
  1597. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1598. &cx23885_video_template, "video");
  1599. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1600. video_nr[dev->nr]);
  1601. if (err < 0) {
  1602. printk(KERN_INFO "%s: can't register video device\n",
  1603. dev->name);
  1604. goto fail_unreg;
  1605. }
  1606. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1607. dev->name, video_device_node_name(dev->video_dev));
  1608. /* register VBI device */
  1609. dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
  1610. &cx23885_vbi_template, "vbi");
  1611. err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1612. vbi_nr[dev->nr]);
  1613. if (err < 0) {
  1614. printk(KERN_INFO "%s: can't register vbi device\n",
  1615. dev->name);
  1616. goto fail_unreg;
  1617. }
  1618. printk(KERN_INFO "%s: registered device %s\n",
  1619. dev->name, video_device_node_name(dev->vbi_dev));
  1620. /* Register ALSA audio device */
  1621. dev->audio_dev = cx23885_audio_register(dev);
  1622. /* initial device configuration */
  1623. mutex_lock(&dev->lock);
  1624. cx23885_set_tvnorm(dev, dev->tvnorm);
  1625. init_controls(dev);
  1626. cx23885_video_mux(dev, 0);
  1627. cx23885_audio_mux(dev, 0);
  1628. mutex_unlock(&dev->lock);
  1629. return 0;
  1630. fail_unreg:
  1631. cx23885_video_unregister(dev);
  1632. return err;
  1633. }