cx23885-video.c 39 KB

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