cx23885-video.c 49 KB

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