cx23885-video.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. * Driver for the Conexant CX23885 PCIe bridge
  3. *
  4. * Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
  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. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  35. /* Include V4L1 specific functions. Should be removed soon */
  36. #include <linux/videodev.h>
  37. #endif
  38. MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
  39. MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
  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/0: " fmt, dev->name, ## arg);\
  63. } while (0)
  64. /* ------------------------------------------------------------------- */
  65. /* static data */
  66. #define FORMAT_FLAGS_PACKED 0x01
  67. static struct cx23885_fmt formats[] = {
  68. {
  69. .name = "8 bpp, gray",
  70. .fourcc = V4L2_PIX_FMT_GREY,
  71. .depth = 8,
  72. .flags = FORMAT_FLAGS_PACKED,
  73. }, {
  74. .name = "15 bpp RGB, le",
  75. .fourcc = V4L2_PIX_FMT_RGB555,
  76. .depth = 16,
  77. .flags = FORMAT_FLAGS_PACKED,
  78. }, {
  79. .name = "15 bpp RGB, be",
  80. .fourcc = V4L2_PIX_FMT_RGB555X,
  81. .depth = 16,
  82. .flags = FORMAT_FLAGS_PACKED,
  83. }, {
  84. .name = "16 bpp RGB, le",
  85. .fourcc = V4L2_PIX_FMT_RGB565,
  86. .depth = 16,
  87. .flags = FORMAT_FLAGS_PACKED,
  88. }, {
  89. .name = "16 bpp RGB, be",
  90. .fourcc = V4L2_PIX_FMT_RGB565X,
  91. .depth = 16,
  92. .flags = FORMAT_FLAGS_PACKED,
  93. }, {
  94. .name = "24 bpp RGB, le",
  95. .fourcc = V4L2_PIX_FMT_BGR24,
  96. .depth = 24,
  97. .flags = FORMAT_FLAGS_PACKED,
  98. }, {
  99. .name = "32 bpp RGB, le",
  100. .fourcc = V4L2_PIX_FMT_BGR32,
  101. .depth = 32,
  102. .flags = FORMAT_FLAGS_PACKED,
  103. }, {
  104. .name = "32 bpp RGB, be",
  105. .fourcc = V4L2_PIX_FMT_RGB32,
  106. .depth = 32,
  107. .flags = FORMAT_FLAGS_PACKED,
  108. }, {
  109. .name = "4:2:2, packed, YUYV",
  110. .fourcc = V4L2_PIX_FMT_YUYV,
  111. .depth = 16,
  112. .flags = FORMAT_FLAGS_PACKED,
  113. }, {
  114. .name = "4:2:2, packed, UYVY",
  115. .fourcc = V4L2_PIX_FMT_UYVY,
  116. .depth = 16,
  117. .flags = FORMAT_FLAGS_PACKED,
  118. },
  119. };
  120. static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
  121. {
  122. unsigned int i;
  123. for (i = 0; i < ARRAY_SIZE(formats); i++)
  124. if (formats[i].fourcc == fourcc)
  125. return formats+i;
  126. printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __FUNCTION__, fourcc);
  127. return NULL;
  128. }
  129. /* ------------------------------------------------------------------- */
  130. static const struct v4l2_queryctrl no_ctl = {
  131. .name = "42",
  132. .flags = V4L2_CTRL_FLAG_DISABLED,
  133. };
  134. static struct cx23885_ctrl cx23885_ctls[] = {
  135. /* --- video --- */
  136. {
  137. .v = {
  138. .id = V4L2_CID_BRIGHTNESS,
  139. .name = "Brightness",
  140. .minimum = 0x00,
  141. .maximum = 0xff,
  142. .step = 1,
  143. .default_value = 0x7f,
  144. .type = V4L2_CTRL_TYPE_INTEGER,
  145. },
  146. .off = 128,
  147. .reg = LUMA_CTRL,
  148. .mask = 0x00ff,
  149. .shift = 0,
  150. }, {
  151. .v = {
  152. .id = V4L2_CID_CONTRAST,
  153. .name = "Contrast",
  154. .minimum = 0,
  155. .maximum = 0xff,
  156. .step = 1,
  157. .default_value = 0x3f,
  158. .type = V4L2_CTRL_TYPE_INTEGER,
  159. },
  160. .off = 0,
  161. .reg = LUMA_CTRL,
  162. .mask = 0xff00,
  163. .shift = 8,
  164. }, {
  165. .v = {
  166. .id = V4L2_CID_HUE,
  167. .name = "Hue",
  168. .minimum = 0,
  169. .maximum = 0xff,
  170. .step = 1,
  171. .default_value = 0x7f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .off = 128,
  175. .reg = CHROMA_CTRL,
  176. .mask = 0xff0000,
  177. .shift = 16,
  178. }, {
  179. /* strictly, this only describes only U saturation.
  180. * V saturation is handled specially through code.
  181. */
  182. .v = {
  183. .id = V4L2_CID_SATURATION,
  184. .name = "Saturation",
  185. .minimum = 0,
  186. .maximum = 0xff,
  187. .step = 1,
  188. .default_value = 0x7f,
  189. .type = V4L2_CTRL_TYPE_INTEGER,
  190. },
  191. .off = 0,
  192. .reg = CHROMA_CTRL,
  193. .mask = 0x00ff,
  194. .shift = 0,
  195. }, {
  196. /* --- audio --- */
  197. .v = {
  198. .id = V4L2_CID_AUDIO_MUTE,
  199. .name = "Mute",
  200. .minimum = 0,
  201. .maximum = 1,
  202. .default_value = 1,
  203. .type = V4L2_CTRL_TYPE_BOOLEAN,
  204. },
  205. .reg = PATH1_CTL1,
  206. .mask = (0x1f << 24),
  207. .shift = 24,
  208. }, {
  209. .v = {
  210. .id = V4L2_CID_AUDIO_VOLUME,
  211. .name = "Volume",
  212. .minimum = 0,
  213. .maximum = 0x3f,
  214. .step = 1,
  215. .default_value = 0x3f,
  216. .type = V4L2_CTRL_TYPE_INTEGER,
  217. },
  218. .reg = PATH1_VOL_CTL,
  219. .mask = 0xff,
  220. .shift = 0,
  221. }
  222. };
  223. static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
  224. const u32 cx23885_user_ctrls[] = {
  225. V4L2_CID_USER_CLASS,
  226. V4L2_CID_BRIGHTNESS,
  227. V4L2_CID_CONTRAST,
  228. V4L2_CID_SATURATION,
  229. V4L2_CID_HUE,
  230. V4L2_CID_AUDIO_VOLUME,
  231. V4L2_CID_AUDIO_MUTE,
  232. 0
  233. };
  234. EXPORT_SYMBOL(cx23885_user_ctrls);
  235. static const u32 *ctrl_classes[] = {
  236. cx23885_user_ctrls,
  237. NULL
  238. };
  239. void cx23885_video_wakeup(struct cx23885_dev *dev,
  240. struct cx23885_dmaqueue *q, u32 count)
  241. {
  242. struct cx23885_buffer *buf;
  243. int bc;
  244. for (bc = 0;; bc++) {
  245. if (list_empty(&q->active))
  246. break;
  247. buf = list_entry(q->active.next,
  248. struct cx23885_buffer, vb.queue);
  249. /* count comes from the hw and is is 16bit wide --
  250. * this trick handles wrap-arounds correctly for
  251. * up to 32767 buffers in flight... */
  252. if ((s16) (count - buf->count) < 0)
  253. break;
  254. do_gettimeofday(&buf->vb.ts);
  255. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  256. count, buf->count);
  257. buf->vb.state = VIDEOBUF_DONE;
  258. list_del(&buf->vb.queue);
  259. wake_up(&buf->vb.done);
  260. }
  261. if (list_empty(&q->active)) {
  262. del_timer(&q->timeout);
  263. } else {
  264. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  265. }
  266. if (bc != 1)
  267. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  268. __FUNCTION__, bc);
  269. }
  270. int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  271. {
  272. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  273. __FUNCTION__,
  274. (unsigned int)norm,
  275. v4l2_norm_to_name(norm));
  276. dev->tvnorm = norm;
  277. /* Tell the analog tuner/demods */
  278. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
  279. /* Tell the internal A/V decoder */
  280. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
  281. return 0;
  282. }
  283. struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  284. struct pci_dev *pci,
  285. struct video_device *template,
  286. char *type)
  287. {
  288. struct video_device *vfd;
  289. dprintk(1, "%s()\n", __FUNCTION__);
  290. vfd = video_device_alloc();
  291. if (NULL == vfd)
  292. return NULL;
  293. *vfd = *template;
  294. vfd->minor = -1;
  295. vfd->dev = &pci->dev;
  296. vfd->release = video_device_release;
  297. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
  298. dev->name, type, cx23885_boards[dev->board].name);
  299. return vfd;
  300. }
  301. int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  302. {
  303. int i;
  304. if (qctrl->id < V4L2_CID_BASE ||
  305. qctrl->id >= V4L2_CID_LASTP1)
  306. return -EINVAL;
  307. for (i = 0; i < CX23885_CTLS; i++)
  308. if (cx23885_ctls[i].v.id == qctrl->id)
  309. break;
  310. if (i == CX23885_CTLS) {
  311. *qctrl = no_ctl;
  312. return 0;
  313. }
  314. *qctrl = cx23885_ctls[i].v;
  315. return 0;
  316. }
  317. EXPORT_SYMBOL(cx23885_ctrl_query);
  318. /* ------------------------------------------------------------------- */
  319. /* resource management */
  320. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  321. unsigned int bit)
  322. {
  323. dprintk(1, "%s()\n", __FUNCTION__);
  324. if (fh->resources & bit)
  325. /* have it already allocated */
  326. return 1;
  327. /* is it free? */
  328. mutex_lock(&dev->lock);
  329. if (dev->resources & bit) {
  330. /* no, someone else uses it */
  331. mutex_unlock(&dev->lock);
  332. return 0;
  333. }
  334. /* it's free, grab it */
  335. fh->resources |= bit;
  336. dev->resources |= bit;
  337. dprintk(1, "res: get %d\n", bit);
  338. mutex_unlock(&dev->lock);
  339. return 1;
  340. }
  341. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  342. {
  343. return (fh->resources & bit);
  344. }
  345. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  346. {
  347. return (dev->resources & bit);
  348. }
  349. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  350. unsigned int bits)
  351. {
  352. BUG_ON((fh->resources & bits) != bits);
  353. dprintk(1, "%s()\n", __FUNCTION__);
  354. mutex_lock(&dev->lock);
  355. fh->resources &= ~bits;
  356. dev->resources &= ~bits;
  357. dprintk(1, "res: put %d\n", bits);
  358. mutex_unlock(&dev->lock);
  359. }
  360. int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  361. {
  362. struct v4l2_routing route;
  363. memset(&route, 0, sizeof(route));
  364. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  365. __FUNCTION__,
  366. input, INPUT(input)->vmux,
  367. INPUT(input)->gpio0, INPUT(input)->gpio1,
  368. INPUT(input)->gpio2, INPUT(input)->gpio3);
  369. dev->input = input;
  370. route.input = INPUT(input)->vmux;
  371. /* Tell the internal A/V decoder */
  372. cx23885_call_i2c_clients(&dev->i2c_bus[2],
  373. VIDIOC_INT_S_VIDEO_ROUTING, &route);
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(cx23885_video_mux);
  377. /* ------------------------------------------------------------------ */
  378. int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  379. unsigned int height, enum v4l2_field field)
  380. {
  381. dprintk(1, "%s()\n", __FUNCTION__);
  382. return 0;
  383. }
  384. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  385. struct cx23885_dmaqueue *q,
  386. struct cx23885_buffer *buf)
  387. {
  388. dprintk(1, "%s()\n", __FUNCTION__);
  389. /* setup fifo + format */
  390. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  391. buf->bpl, buf->risc.dma);
  392. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  393. /* reset counter */
  394. cx_write(VID_A_GPCNT_CTL, 3);
  395. q->count = 1;
  396. /* enable irq */
  397. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  398. cx_set(VID_A_INT_MSK, 0x000011);
  399. /* start dma */
  400. cx_set(DEV_CNTRL2, (1<<5));
  401. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  402. return 0;
  403. }
  404. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  405. struct cx23885_dmaqueue *q)
  406. {
  407. struct cx23885_buffer *buf, *prev;
  408. struct list_head *item;
  409. dprintk(1, "%s()\n", __FUNCTION__);
  410. if (!list_empty(&q->active)) {
  411. buf = list_entry(q->active.next, struct cx23885_buffer,
  412. vb.queue);
  413. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  414. buf, buf->vb.i);
  415. cx23885_start_video_dma(dev, q, buf);
  416. list_for_each(item, &q->active) {
  417. buf = list_entry(item, struct cx23885_buffer,
  418. vb.queue);
  419. buf->count = q->count++;
  420. }
  421. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  422. return 0;
  423. }
  424. prev = NULL;
  425. for (;;) {
  426. if (list_empty(&q->queued))
  427. return 0;
  428. buf = list_entry(q->queued.next, struct cx23885_buffer,
  429. vb.queue);
  430. if (NULL == prev) {
  431. list_move_tail(&buf->vb.queue, &q->active);
  432. cx23885_start_video_dma(dev, q, buf);
  433. buf->vb.state = VIDEOBUF_ACTIVE;
  434. buf->count = q->count++;
  435. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  436. dprintk(2, "[%p/%d] restart_queue - first active\n",
  437. buf, buf->vb.i);
  438. } else if (prev->vb.width == buf->vb.width &&
  439. prev->vb.height == buf->vb.height &&
  440. prev->fmt == buf->fmt) {
  441. list_move_tail(&buf->vb.queue, &q->active);
  442. buf->vb.state = VIDEOBUF_ACTIVE;
  443. buf->count = q->count++;
  444. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  445. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  446. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  447. buf, buf->vb.i);
  448. } else {
  449. return 0;
  450. }
  451. prev = buf;
  452. }
  453. }
  454. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  455. unsigned int *size)
  456. {
  457. struct cx23885_fh *fh = q->priv_data;
  458. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  459. if (0 == *count)
  460. *count = 32;
  461. while (*size * *count > vid_limit * 1024 * 1024)
  462. (*count)--;
  463. return 0;
  464. }
  465. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  466. enum v4l2_field field)
  467. {
  468. struct cx23885_fh *fh = q->priv_data;
  469. struct cx23885_dev *dev = fh->dev;
  470. struct cx23885_buffer *buf =
  471. container_of(vb, struct cx23885_buffer, vb);
  472. int rc, init_buffer = 0;
  473. u32 line0_offset, line1_offset;
  474. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  475. BUG_ON(NULL == fh->fmt);
  476. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  477. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  478. return -EINVAL;
  479. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  480. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  481. return -EINVAL;
  482. if (buf->fmt != fh->fmt ||
  483. buf->vb.width != fh->width ||
  484. buf->vb.height != fh->height ||
  485. buf->vb.field != field) {
  486. buf->fmt = fh->fmt;
  487. buf->vb.width = fh->width;
  488. buf->vb.height = fh->height;
  489. buf->vb.field = field;
  490. init_buffer = 1;
  491. }
  492. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  493. init_buffer = 1;
  494. rc = videobuf_iolock(q, &buf->vb, NULL);
  495. if (0 != rc)
  496. goto fail;
  497. }
  498. if (init_buffer) {
  499. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  500. switch (buf->vb.field) {
  501. case V4L2_FIELD_TOP:
  502. cx23885_risc_buffer(dev->pci, &buf->risc,
  503. dma->sglist, 0, UNSET,
  504. buf->bpl, 0, buf->vb.height);
  505. break;
  506. case V4L2_FIELD_BOTTOM:
  507. cx23885_risc_buffer(dev->pci, &buf->risc,
  508. dma->sglist, UNSET, 0,
  509. buf->bpl, 0, buf->vb.height);
  510. break;
  511. case V4L2_FIELD_INTERLACED:
  512. if (dev->tvnorm & V4L2_STD_NTSC) {
  513. /* cx25840 transmits NTSC bottom field first */
  514. dprintk(1, "%s() Creating NTSC risc\n",
  515. __FUNCTION__);
  516. line0_offset = buf->bpl;
  517. line1_offset = 0;
  518. } else {
  519. /* All other formats are top field first */
  520. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  521. __FUNCTION__);
  522. line0_offset = 0;
  523. line1_offset = buf->bpl;
  524. }
  525. cx23885_risc_buffer(dev->pci, &buf->risc,
  526. dma->sglist, line0_offset,
  527. line1_offset,
  528. buf->bpl, buf->bpl,
  529. buf->vb.height >> 1);
  530. break;
  531. case V4L2_FIELD_SEQ_TB:
  532. cx23885_risc_buffer(dev->pci, &buf->risc,
  533. dma->sglist,
  534. 0, buf->bpl * (buf->vb.height >> 1),
  535. buf->bpl, 0,
  536. buf->vb.height >> 1);
  537. break;
  538. case V4L2_FIELD_SEQ_BT:
  539. cx23885_risc_buffer(dev->pci, &buf->risc,
  540. dma->sglist,
  541. buf->bpl * (buf->vb.height >> 1), 0,
  542. buf->bpl, 0,
  543. buf->vb.height >> 1);
  544. break;
  545. default:
  546. BUG();
  547. }
  548. }
  549. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  550. buf, buf->vb.i,
  551. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  552. (unsigned long)buf->risc.dma);
  553. buf->vb.state = VIDEOBUF_PREPARED;
  554. return 0;
  555. fail:
  556. cx23885_free_buffer(q, buf);
  557. return rc;
  558. }
  559. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  560. {
  561. struct cx23885_buffer *buf = container_of(vb,
  562. struct cx23885_buffer, vb);
  563. struct cx23885_buffer *prev;
  564. struct cx23885_fh *fh = vq->priv_data;
  565. struct cx23885_dev *dev = fh->dev;
  566. struct cx23885_dmaqueue *q = &dev->vidq;
  567. /* add jump to stopper */
  568. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  569. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  570. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  571. if (!list_empty(&q->queued)) {
  572. list_add_tail(&buf->vb.queue, &q->queued);
  573. buf->vb.state = VIDEOBUF_QUEUED;
  574. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  575. buf, buf->vb.i);
  576. } else if (list_empty(&q->active)) {
  577. list_add_tail(&buf->vb.queue, &q->active);
  578. cx23885_start_video_dma(dev, q, buf);
  579. buf->vb.state = VIDEOBUF_ACTIVE;
  580. buf->count = q->count++;
  581. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  582. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  583. buf, buf->vb.i);
  584. } else {
  585. prev = list_entry(q->active.prev, struct cx23885_buffer,
  586. vb.queue);
  587. if (prev->vb.width == buf->vb.width &&
  588. prev->vb.height == buf->vb.height &&
  589. prev->fmt == buf->fmt) {
  590. list_add_tail(&buf->vb.queue, &q->active);
  591. buf->vb.state = VIDEOBUF_ACTIVE;
  592. buf->count = q->count++;
  593. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  594. /* 64 bit bits 63-32 */
  595. prev->risc.jmp[2] = cpu_to_le32(0);
  596. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  597. buf, buf->vb.i);
  598. } else {
  599. list_add_tail(&buf->vb.queue, &q->queued);
  600. buf->vb.state = VIDEOBUF_QUEUED;
  601. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  602. buf, buf->vb.i);
  603. }
  604. }
  605. }
  606. static void buffer_release(struct videobuf_queue *q,
  607. struct videobuf_buffer *vb)
  608. {
  609. struct cx23885_buffer *buf = container_of(vb,
  610. struct cx23885_buffer, vb);
  611. cx23885_free_buffer(q, buf);
  612. }
  613. static struct videobuf_queue_ops cx23885_video_qops = {
  614. .buf_setup = buffer_setup,
  615. .buf_prepare = buffer_prepare,
  616. .buf_queue = buffer_queue,
  617. .buf_release = buffer_release,
  618. };
  619. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  620. {
  621. switch (fh->type) {
  622. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  623. return &fh->vidq;
  624. case V4L2_BUF_TYPE_VBI_CAPTURE:
  625. return &fh->vbiq;
  626. default:
  627. BUG();
  628. return NULL;
  629. }
  630. }
  631. static int get_resource(struct cx23885_fh *fh)
  632. {
  633. switch (fh->type) {
  634. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  635. return RESOURCE_VIDEO;
  636. case V4L2_BUF_TYPE_VBI_CAPTURE:
  637. return RESOURCE_VBI;
  638. default:
  639. BUG();
  640. return 0;
  641. }
  642. }
  643. static int video_open(struct inode *inode, struct file *file)
  644. {
  645. int minor = iminor(inode);
  646. struct cx23885_dev *h, *dev = NULL;
  647. struct cx23885_fh *fh;
  648. struct list_head *list;
  649. enum v4l2_buf_type type = 0;
  650. int radio = 0;
  651. list_for_each(list, &cx23885_devlist) {
  652. h = list_entry(list, struct cx23885_dev, devlist);
  653. if (h->video_dev->minor == minor) {
  654. dev = h;
  655. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  656. }
  657. if (h->vbi_dev &&
  658. h->vbi_dev->minor == minor) {
  659. dev = h;
  660. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  661. }
  662. if (h->radio_dev &&
  663. h->radio_dev->minor == minor) {
  664. radio = 1;
  665. dev = h;
  666. }
  667. }
  668. if (NULL == dev)
  669. return -ENODEV;
  670. dprintk(1, "open minor=%d radio=%d type=%s\n",
  671. minor, radio, v4l2_type_names[type]);
  672. /* allocate + initialize per filehandle data */
  673. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  674. if (NULL == fh)
  675. return -ENOMEM;
  676. file->private_data = fh;
  677. fh->dev = dev;
  678. fh->radio = radio;
  679. fh->type = type;
  680. fh->width = 320;
  681. fh->height = 240;
  682. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  683. videobuf_queue_pci_init(&fh->vidq, &cx23885_video_qops,
  684. dev->pci, &dev->slock,
  685. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  686. V4L2_FIELD_INTERLACED,
  687. sizeof(struct cx23885_buffer),
  688. fh);
  689. dprintk(1, "post videobuf_queue_init()\n");
  690. return 0;
  691. }
  692. static ssize_t video_read(struct file *file, char __user *data,
  693. size_t count, loff_t *ppos)
  694. {
  695. struct cx23885_fh *fh = file->private_data;
  696. switch (fh->type) {
  697. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  698. if (res_locked(fh->dev, RESOURCE_VIDEO))
  699. return -EBUSY;
  700. return videobuf_read_one(&fh->vidq, data, count, ppos,
  701. file->f_flags & O_NONBLOCK);
  702. case V4L2_BUF_TYPE_VBI_CAPTURE:
  703. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  704. return -EBUSY;
  705. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  706. file->f_flags & O_NONBLOCK);
  707. default:
  708. BUG();
  709. return 0;
  710. }
  711. }
  712. static unsigned int video_poll(struct file *file,
  713. struct poll_table_struct *wait)
  714. {
  715. struct cx23885_fh *fh = file->private_data;
  716. struct cx23885_buffer *buf;
  717. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  718. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  719. return POLLERR;
  720. return videobuf_poll_stream(file, &fh->vbiq, wait);
  721. }
  722. if (res_check(fh, RESOURCE_VIDEO)) {
  723. /* streaming capture */
  724. if (list_empty(&fh->vidq.stream))
  725. return POLLERR;
  726. buf = list_entry(fh->vidq.stream.next,
  727. struct cx23885_buffer, vb.stream);
  728. } else {
  729. /* read() capture */
  730. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  731. if (NULL == buf)
  732. return POLLERR;
  733. }
  734. poll_wait(file, &buf->vb.done, wait);
  735. if (buf->vb.state == VIDEOBUF_DONE ||
  736. buf->vb.state == VIDEOBUF_ERROR)
  737. return POLLIN|POLLRDNORM;
  738. return 0;
  739. }
  740. static int video_release(struct inode *inode, struct file *file)
  741. {
  742. struct cx23885_fh *fh = file->private_data;
  743. struct cx23885_dev *dev = fh->dev;
  744. /* turn off overlay */
  745. if (res_check(fh, RESOURCE_OVERLAY)) {
  746. /* FIXME */
  747. res_free(dev, fh, RESOURCE_OVERLAY);
  748. }
  749. /* stop video capture */
  750. if (res_check(fh, RESOURCE_VIDEO)) {
  751. videobuf_queue_cancel(&fh->vidq);
  752. res_free(dev, fh, RESOURCE_VIDEO);
  753. }
  754. if (fh->vidq.read_buf) {
  755. buffer_release(&fh->vidq, fh->vidq.read_buf);
  756. kfree(fh->vidq.read_buf);
  757. }
  758. /* stop vbi capture */
  759. if (res_check(fh, RESOURCE_VBI)) {
  760. if (fh->vbiq.streaming)
  761. videobuf_streamoff(&fh->vbiq);
  762. if (fh->vbiq.reading)
  763. videobuf_read_stop(&fh->vbiq);
  764. res_free(dev, fh, RESOURCE_VBI);
  765. }
  766. videobuf_mmap_free(&fh->vidq);
  767. file->private_data = NULL;
  768. kfree(fh);
  769. /* We are not putting the tuner to sleep here on exit, because
  770. * we want to use the mpeg encoder in another session to capture
  771. * tuner video. Closing this will result in no video to the encoder.
  772. */
  773. return 0;
  774. }
  775. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  776. {
  777. struct cx23885_fh *fh = file->private_data;
  778. return videobuf_mmap_mapper(get_queue(fh), vma);
  779. }
  780. /* ------------------------------------------------------------------ */
  781. /* VIDEO CTRL IOCTLS */
  782. int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
  783. {
  784. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __FUNCTION__);
  785. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
  786. return 0;
  787. }
  788. EXPORT_SYMBOL(cx23885_get_control);
  789. int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
  790. {
  791. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  792. " (disabled - no action)\n", __FUNCTION__);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(cx23885_set_control);
  796. static void init_controls(struct cx23885_dev *dev)
  797. {
  798. struct v4l2_control ctrl;
  799. int i;
  800. for (i = 0; i < CX23885_CTLS; i++) {
  801. ctrl.id = cx23885_ctls[i].v.id;
  802. ctrl.value = cx23885_ctls[i].v.default_value;
  803. cx23885_set_control(dev, &ctrl);
  804. }
  805. }
  806. /* ------------------------------------------------------------------ */
  807. /* VIDEO IOCTLS */
  808. static int vidioc_g_fmt_cap(struct file *file, void *priv,
  809. struct v4l2_format *f)
  810. {
  811. struct cx23885_fh *fh = priv;
  812. f->fmt.pix.width = fh->width;
  813. f->fmt.pix.height = fh->height;
  814. f->fmt.pix.field = fh->vidq.field;
  815. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  816. f->fmt.pix.bytesperline =
  817. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  818. f->fmt.pix.sizeimage =
  819. f->fmt.pix.height * f->fmt.pix.bytesperline;
  820. return 0;
  821. }
  822. static int vidioc_try_fmt_cap(struct file *file, void *priv,
  823. struct v4l2_format *f)
  824. {
  825. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  826. struct cx23885_fmt *fmt;
  827. enum v4l2_field field;
  828. unsigned int maxw, maxh;
  829. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  830. if (NULL == fmt)
  831. return -EINVAL;
  832. field = f->fmt.pix.field;
  833. maxw = norm_maxw(dev->tvnorm);
  834. maxh = norm_maxh(dev->tvnorm);
  835. if (V4L2_FIELD_ANY == field) {
  836. field = (f->fmt.pix.height > maxh/2)
  837. ? V4L2_FIELD_INTERLACED
  838. : V4L2_FIELD_BOTTOM;
  839. }
  840. switch (field) {
  841. case V4L2_FIELD_TOP:
  842. case V4L2_FIELD_BOTTOM:
  843. maxh = maxh / 2;
  844. break;
  845. case V4L2_FIELD_INTERLACED:
  846. break;
  847. default:
  848. return -EINVAL;
  849. }
  850. f->fmt.pix.field = field;
  851. if (f->fmt.pix.height < 32)
  852. f->fmt.pix.height = 32;
  853. if (f->fmt.pix.height > maxh)
  854. f->fmt.pix.height = maxh;
  855. if (f->fmt.pix.width < 48)
  856. f->fmt.pix.width = 48;
  857. if (f->fmt.pix.width > maxw)
  858. f->fmt.pix.width = maxw;
  859. f->fmt.pix.width &= ~0x03;
  860. f->fmt.pix.bytesperline =
  861. (f->fmt.pix.width * fmt->depth) >> 3;
  862. f->fmt.pix.sizeimage =
  863. f->fmt.pix.height * f->fmt.pix.bytesperline;
  864. return 0;
  865. }
  866. static int vidioc_s_fmt_cap(struct file *file, void *priv,
  867. struct v4l2_format *f)
  868. {
  869. struct cx23885_fh *fh = priv;
  870. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  871. int err;
  872. dprintk(2, "%s()\n", __FUNCTION__);
  873. err = vidioc_try_fmt_cap(file, priv, f);
  874. if (0 != err)
  875. return err;
  876. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  877. fh->width = f->fmt.pix.width;
  878. fh->height = f->fmt.pix.height;
  879. fh->vidq.field = f->fmt.pix.field;
  880. dprintk(2, "%s() width=%d height=%d field=%d\n", __FUNCTION__,
  881. fh->width, fh->height, fh->vidq.field);
  882. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
  883. return 0;
  884. }
  885. static int vidioc_querycap(struct file *file, void *priv,
  886. struct v4l2_capability *cap)
  887. {
  888. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  889. strcpy(cap->driver, "cx23885");
  890. strlcpy(cap->card, cx23885_boards[dev->board].name,
  891. sizeof(cap->card));
  892. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  893. cap->version = CX23885_VERSION_CODE;
  894. cap->capabilities =
  895. V4L2_CAP_VIDEO_CAPTURE |
  896. V4L2_CAP_READWRITE |
  897. V4L2_CAP_STREAMING |
  898. V4L2_CAP_VBI_CAPTURE;
  899. if (UNSET != dev->tuner_type)
  900. cap->capabilities |= V4L2_CAP_TUNER;
  901. return 0;
  902. }
  903. static int vidioc_enum_fmt_cap(struct file *file, void *priv,
  904. struct v4l2_fmtdesc *f)
  905. {
  906. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  907. return -EINVAL;
  908. strlcpy(f->description, formats[f->index].name,
  909. sizeof(f->description));
  910. f->pixelformat = formats[f->index].fourcc;
  911. return 0;
  912. }
  913. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  914. static int vidiocgmbuf(struct file *file, void *priv,
  915. struct video_mbuf *mbuf)
  916. {
  917. struct cx23885_fh *fh = priv;
  918. struct videobuf_queue *q;
  919. struct v4l2_requestbuffers req;
  920. unsigned int i;
  921. int err;
  922. q = get_queue(fh);
  923. memset(&req, 0, sizeof(req));
  924. req.type = q->type;
  925. req.count = 8;
  926. req.memory = V4L2_MEMORY_MMAP;
  927. err = videobuf_reqbufs(q, &req);
  928. if (err < 0)
  929. return err;
  930. mbuf->frames = req.count;
  931. mbuf->size = 0;
  932. for (i = 0; i < mbuf->frames; i++) {
  933. mbuf->offsets[i] = q->bufs[i]->boff;
  934. mbuf->size += q->bufs[i]->bsize;
  935. }
  936. return 0;
  937. }
  938. #endif
  939. static int vidioc_reqbufs(struct file *file, void *priv,
  940. struct v4l2_requestbuffers *p)
  941. {
  942. struct cx23885_fh *fh = priv;
  943. return (videobuf_reqbufs(get_queue(fh), p));
  944. }
  945. static int vidioc_querybuf(struct file *file, void *priv,
  946. struct v4l2_buffer *p)
  947. {
  948. struct cx23885_fh *fh = priv;
  949. return (videobuf_querybuf(get_queue(fh), p));
  950. }
  951. static int vidioc_qbuf(struct file *file, void *priv,
  952. struct v4l2_buffer *p)
  953. {
  954. struct cx23885_fh *fh = priv;
  955. return (videobuf_qbuf(get_queue(fh), p));
  956. }
  957. static int vidioc_dqbuf(struct file *file, void *priv,
  958. struct v4l2_buffer *p)
  959. {
  960. struct cx23885_fh *fh = priv;
  961. return (videobuf_dqbuf(get_queue(fh), p,
  962. file->f_flags & O_NONBLOCK));
  963. }
  964. static int vidioc_streamon(struct file *file, void *priv,
  965. enum v4l2_buf_type i)
  966. {
  967. struct cx23885_fh *fh = priv;
  968. struct cx23885_dev *dev = fh->dev;
  969. dprintk(1, "%s()\n", __FUNCTION__);
  970. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  971. return -EINVAL;
  972. if (unlikely(i != fh->type))
  973. return -EINVAL;
  974. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  975. return -EBUSY;
  976. return videobuf_streamon(get_queue(fh));
  977. }
  978. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  979. {
  980. struct cx23885_fh *fh = priv;
  981. struct cx23885_dev *dev = fh->dev;
  982. int err, res;
  983. dprintk(1, "%s()\n", __FUNCTION__);
  984. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  985. return -EINVAL;
  986. if (i != fh->type)
  987. return -EINVAL;
  988. res = get_resource(fh);
  989. err = videobuf_streamoff(get_queue(fh));
  990. if (err < 0)
  991. return err;
  992. res_free(dev, fh, res);
  993. return 0;
  994. }
  995. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  996. {
  997. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  998. dprintk(1, "%s()\n", __FUNCTION__);
  999. mutex_lock(&dev->lock);
  1000. cx23885_set_tvnorm(dev, *tvnorms);
  1001. mutex_unlock(&dev->lock);
  1002. return 0;
  1003. }
  1004. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1005. {
  1006. static const char *iname[] = {
  1007. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1008. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1009. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1010. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1011. [CX23885_VMUX_SVIDEO] = "S-Video",
  1012. [CX23885_VMUX_TELEVISION] = "Television",
  1013. [CX23885_VMUX_CABLE] = "Cable TV",
  1014. [CX23885_VMUX_DVB] = "DVB",
  1015. [CX23885_VMUX_DEBUG] = "for debug only",
  1016. };
  1017. unsigned int n;
  1018. dprintk(1, "%s()\n", __FUNCTION__);
  1019. n = i->index;
  1020. if (n >= 4)
  1021. return -EINVAL;
  1022. if (0 == INPUT(n)->type)
  1023. return -EINVAL;
  1024. memset(i, 0, sizeof(*i));
  1025. i->index = n;
  1026. i->type = V4L2_INPUT_TYPE_CAMERA;
  1027. strcpy(i->name, iname[INPUT(n)->type]);
  1028. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1029. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1030. i->type = V4L2_INPUT_TYPE_TUNER;
  1031. i->std = CX23885_NORMS;
  1032. return 0;
  1033. }
  1034. EXPORT_SYMBOL(cx23885_enum_input);
  1035. static int vidioc_enum_input(struct file *file, void *priv,
  1036. struct v4l2_input *i)
  1037. {
  1038. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1039. dprintk(1, "%s()\n", __FUNCTION__);
  1040. return cx23885_enum_input(dev, i);
  1041. }
  1042. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1043. {
  1044. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1045. *i = dev->input;
  1046. dprintk(1, "%s() returns %d\n", __FUNCTION__, *i);
  1047. return 0;
  1048. }
  1049. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1050. {
  1051. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1052. dprintk(1, "%s(%d)\n", __FUNCTION__, i);
  1053. if (i >= 4) {
  1054. dprintk(1, "%s() -EINVAL\n", __FUNCTION__);
  1055. return -EINVAL;
  1056. }
  1057. mutex_lock(&dev->lock);
  1058. cx23885_video_mux(dev, i);
  1059. mutex_unlock(&dev->lock);
  1060. return 0;
  1061. }
  1062. static int vidioc_queryctrl(struct file *file, void *priv,
  1063. struct v4l2_queryctrl *qctrl)
  1064. {
  1065. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1066. if (unlikely(qctrl->id == 0))
  1067. return -EINVAL;
  1068. return cx23885_ctrl_query(qctrl);
  1069. }
  1070. static int vidioc_g_ctrl(struct file *file, void *priv,
  1071. struct v4l2_control *ctl)
  1072. {
  1073. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1074. return cx23885_get_control(dev, ctl);
  1075. }
  1076. static int vidioc_s_ctrl(struct file *file, void *priv,
  1077. struct v4l2_control *ctl)
  1078. {
  1079. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1080. return cx23885_set_control(dev, ctl);
  1081. }
  1082. static int vidioc_g_tuner(struct file *file, void *priv,
  1083. struct v4l2_tuner *t)
  1084. {
  1085. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1086. if (unlikely(UNSET == dev->tuner_type))
  1087. return -EINVAL;
  1088. if (0 != t->index)
  1089. return -EINVAL;
  1090. strcpy(t->name, "Television");
  1091. t->type = V4L2_TUNER_ANALOG_TV;
  1092. t->capability = V4L2_TUNER_CAP_NORM;
  1093. t->rangehigh = 0xffffffffUL;
  1094. t->signal = 0xffff ; /* LOCKED */
  1095. return 0;
  1096. }
  1097. static int vidioc_s_tuner(struct file *file, void *priv,
  1098. struct v4l2_tuner *t)
  1099. {
  1100. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1101. if (UNSET == dev->tuner_type)
  1102. return -EINVAL;
  1103. if (0 != t->index)
  1104. return -EINVAL;
  1105. return 0;
  1106. }
  1107. static int vidioc_g_frequency(struct file *file, void *priv,
  1108. struct v4l2_frequency *f)
  1109. {
  1110. struct cx23885_fh *fh = priv;
  1111. struct cx23885_dev *dev = fh->dev;
  1112. if (unlikely(UNSET == dev->tuner_type))
  1113. return -EINVAL;
  1114. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1115. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1116. f->frequency = dev->freq;
  1117. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
  1118. return 0;
  1119. }
  1120. int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1121. {
  1122. if (unlikely(UNSET == dev->tuner_type))
  1123. return -EINVAL;
  1124. if (unlikely(f->tuner != 0))
  1125. return -EINVAL;
  1126. mutex_lock(&dev->lock);
  1127. dev->freq = f->frequency;
  1128. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
  1129. /* When changing channels it is required to reset TVAUDIO */
  1130. msleep(10);
  1131. mutex_unlock(&dev->lock);
  1132. return 0;
  1133. }
  1134. EXPORT_SYMBOL(cx23885_set_freq);
  1135. static int vidioc_s_frequency(struct file *file, void *priv,
  1136. struct v4l2_frequency *f)
  1137. {
  1138. struct cx23885_fh *fh = priv;
  1139. struct cx23885_dev *dev = fh->dev;
  1140. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1141. return -EINVAL;
  1142. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1143. return -EINVAL;
  1144. return
  1145. cx23885_set_freq(dev, f);
  1146. }
  1147. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1148. static int vidioc_g_register(struct file *file, void *fh,
  1149. struct v4l2_register *reg)
  1150. {
  1151. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1152. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1153. return -EINVAL;
  1154. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
  1155. return 0;
  1156. }
  1157. static int vidioc_s_register(struct file *file, void *fh,
  1158. struct v4l2_register *reg)
  1159. {
  1160. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1161. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1162. return -EINVAL;
  1163. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
  1164. return 0;
  1165. }
  1166. #endif
  1167. /* ----------------------------------------------------------- */
  1168. static void cx23885_vid_timeout(unsigned long data)
  1169. {
  1170. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1171. struct cx23885_dmaqueue *q = &dev->vidq;
  1172. struct cx23885_buffer *buf;
  1173. unsigned long flags;
  1174. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1175. cx_clear(VID_A_DMA_CTL, 0x11);
  1176. spin_lock_irqsave(&dev->slock, flags);
  1177. while (!list_empty(&q->active)) {
  1178. buf = list_entry(q->active.next,
  1179. struct cx23885_buffer, vb.queue);
  1180. list_del(&buf->vb.queue);
  1181. buf->vb.state = VIDEOBUF_ERROR;
  1182. wake_up(&buf->vb.done);
  1183. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1184. dev->name, buf, buf->vb.i,
  1185. (unsigned long)buf->risc.dma);
  1186. }
  1187. cx23885_restart_video_queue(dev, q);
  1188. spin_unlock_irqrestore(&dev->slock, flags);
  1189. }
  1190. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1191. {
  1192. u32 mask, count;
  1193. int handled = 0;
  1194. mask = cx_read(VID_A_INT_MSK);
  1195. if (0 == (status & mask))
  1196. return handled;
  1197. cx_write(VID_A_INT_STAT, status);
  1198. dprintk(2, "%s() status = 0x%08x\n", __FUNCTION__, status);
  1199. /* risc op code error */
  1200. if (status & (1 << 16)) {
  1201. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1202. dev->name);
  1203. cx_clear(VID_A_DMA_CTL, 0x11);
  1204. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1205. }
  1206. /* risc1 y */
  1207. if (status & 0x01) {
  1208. spin_lock(&dev->slock);
  1209. count = cx_read(VID_A_GPCNT);
  1210. cx23885_video_wakeup(dev, &dev->vidq, count);
  1211. spin_unlock(&dev->slock);
  1212. handled++;
  1213. }
  1214. /* risc2 y */
  1215. if (status & 0x10) {
  1216. dprintk(2, "stopper video\n");
  1217. spin_lock(&dev->slock);
  1218. cx23885_restart_video_queue(dev, &dev->vidq);
  1219. spin_unlock(&dev->slock);
  1220. handled++;
  1221. }
  1222. return handled;
  1223. }
  1224. /* ----------------------------------------------------------- */
  1225. /* exported stuff */
  1226. static const struct file_operations video_fops = {
  1227. .owner = THIS_MODULE,
  1228. .open = video_open,
  1229. .release = video_release,
  1230. .read = video_read,
  1231. .poll = video_poll,
  1232. .mmap = video_mmap,
  1233. .ioctl = video_ioctl2,
  1234. .compat_ioctl = v4l_compat_ioctl32,
  1235. .llseek = no_llseek,
  1236. };
  1237. static struct video_device cx23885_vbi_template;
  1238. static struct video_device cx23885_video_template = {
  1239. .name = "cx23885-video",
  1240. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1241. .fops = &video_fops,
  1242. .minor = -1,
  1243. .vidioc_querycap = vidioc_querycap,
  1244. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1245. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1246. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1247. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1248. .vidioc_g_fmt_vbi = cx23885_vbi_fmt,
  1249. .vidioc_try_fmt_vbi = cx23885_vbi_fmt,
  1250. .vidioc_s_fmt_vbi = cx23885_vbi_fmt,
  1251. .vidioc_reqbufs = vidioc_reqbufs,
  1252. .vidioc_querybuf = vidioc_querybuf,
  1253. .vidioc_qbuf = vidioc_qbuf,
  1254. .vidioc_dqbuf = vidioc_dqbuf,
  1255. .vidioc_s_std = vidioc_s_std,
  1256. .vidioc_enum_input = vidioc_enum_input,
  1257. .vidioc_g_input = vidioc_g_input,
  1258. .vidioc_s_input = vidioc_s_input,
  1259. .vidioc_queryctrl = vidioc_queryctrl,
  1260. .vidioc_g_ctrl = vidioc_g_ctrl,
  1261. .vidioc_s_ctrl = vidioc_s_ctrl,
  1262. .vidioc_streamon = vidioc_streamon,
  1263. .vidioc_streamoff = vidioc_streamoff,
  1264. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1265. .vidiocgmbuf = vidiocgmbuf,
  1266. #endif
  1267. .vidioc_g_tuner = vidioc_g_tuner,
  1268. .vidioc_s_tuner = vidioc_s_tuner,
  1269. .vidioc_g_frequency = vidioc_g_frequency,
  1270. .vidioc_s_frequency = vidioc_s_frequency,
  1271. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1272. .vidioc_g_register = vidioc_g_register,
  1273. .vidioc_s_register = vidioc_s_register,
  1274. #endif
  1275. .tvnorms = CX23885_NORMS,
  1276. .current_norm = V4L2_STD_NTSC_M,
  1277. };
  1278. static const struct file_operations radio_fops = {
  1279. .owner = THIS_MODULE,
  1280. .open = video_open,
  1281. .release = video_release,
  1282. .ioctl = video_ioctl2,
  1283. .compat_ioctl = v4l_compat_ioctl32,
  1284. .llseek = no_llseek,
  1285. };
  1286. void cx23885_video_unregister(struct cx23885_dev *dev)
  1287. {
  1288. dprintk(1, "%s()\n", __FUNCTION__);
  1289. cx_clear(PCI_INT_MSK, 1);
  1290. if (dev->video_dev) {
  1291. if (-1 != dev->video_dev->minor)
  1292. video_unregister_device(dev->video_dev);
  1293. else
  1294. video_device_release(dev->video_dev);
  1295. dev->video_dev = NULL;
  1296. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1297. }
  1298. }
  1299. int cx23885_video_register(struct cx23885_dev *dev)
  1300. {
  1301. int err;
  1302. dprintk(1, "%s()\n", __FUNCTION__);
  1303. spin_lock_init(&dev->slock);
  1304. /* Initialize VBI template */
  1305. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1306. sizeof(cx23885_vbi_template));
  1307. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1308. cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1309. dev->tvnorm = cx23885_video_template.current_norm;
  1310. /* init video dma queues */
  1311. INIT_LIST_HEAD(&dev->vidq.active);
  1312. INIT_LIST_HEAD(&dev->vidq.queued);
  1313. dev->vidq.timeout.function = cx23885_vid_timeout;
  1314. dev->vidq.timeout.data = (unsigned long)dev;
  1315. init_timer(&dev->vidq.timeout);
  1316. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1317. VID_A_DMA_CTL, 0x11, 0x00);
  1318. /* Don't enable VBI yet */
  1319. cx_set(PCI_INT_MSK, 1);
  1320. /* register v4l devices */
  1321. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1322. &cx23885_video_template, "video");
  1323. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1324. video_nr[dev->nr]);
  1325. if (err < 0) {
  1326. printk(KERN_INFO "%s: can't register video device\n",
  1327. dev->name);
  1328. goto fail_unreg;
  1329. }
  1330. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1331. dev->name, dev->video_dev->minor & 0x1f);
  1332. /* initial device configuration */
  1333. mutex_lock(&dev->lock);
  1334. cx23885_set_tvnorm(dev, dev->tvnorm);
  1335. init_controls(dev);
  1336. cx23885_video_mux(dev, 0);
  1337. mutex_unlock(&dev->lock);
  1338. return 0;
  1339. fail_unreg:
  1340. cx23885_video_unregister(dev);
  1341. return err;
  1342. }