cx23885-video.c 40 KB

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