cx23885-video.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. static 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. static const u32 *ctrl_classes[] = {
  236. cx23885_user_ctrls,
  237. NULL
  238. };
  239. static void cx23885_video_wakeup(struct cx23885_dev *dev,
  240. struct cx23885_dmaqueue *q, u32 count)
  241. {
  242. struct cx23885_buffer *buf;
  243. int bc;
  244. for (bc = 0;; bc++) {
  245. if (list_empty(&q->active))
  246. break;
  247. buf = list_entry(q->active.next,
  248. struct cx23885_buffer, vb.queue);
  249. /* count comes from the hw and is is 16bit wide --
  250. * this trick handles wrap-arounds correctly for
  251. * up to 32767 buffers in flight... */
  252. if ((s16) (count - buf->count) < 0)
  253. break;
  254. do_gettimeofday(&buf->vb.ts);
  255. dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
  256. count, buf->count);
  257. buf->vb.state = VIDEOBUF_DONE;
  258. list_del(&buf->vb.queue);
  259. wake_up(&buf->vb.done);
  260. }
  261. if (list_empty(&q->active))
  262. del_timer(&q->timeout);
  263. else
  264. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  265. if (bc != 1)
  266. printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
  267. __func__, bc);
  268. }
  269. static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
  270. {
  271. dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
  272. __func__,
  273. (unsigned int)norm,
  274. v4l2_norm_to_name(norm));
  275. dev->tvnorm = norm;
  276. /* Tell the analog tuner/demods */
  277. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
  278. /* Tell the internal A/V decoder */
  279. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
  280. return 0;
  281. }
  282. static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
  283. struct pci_dev *pci,
  284. struct video_device *template,
  285. char *type)
  286. {
  287. struct video_device *vfd;
  288. dprintk(1, "%s()\n", __func__);
  289. vfd = video_device_alloc();
  290. if (NULL == vfd)
  291. return NULL;
  292. *vfd = *template;
  293. vfd->minor = -1;
  294. vfd->parent = &pci->dev;
  295. vfd->release = video_device_release;
  296. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
  297. dev->name, type, cx23885_boards[dev->board].name);
  298. return vfd;
  299. }
  300. static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
  301. {
  302. int i;
  303. if (qctrl->id < V4L2_CID_BASE ||
  304. qctrl->id >= V4L2_CID_LASTP1)
  305. return -EINVAL;
  306. for (i = 0; i < CX23885_CTLS; i++)
  307. if (cx23885_ctls[i].v.id == qctrl->id)
  308. break;
  309. if (i == CX23885_CTLS) {
  310. *qctrl = no_ctl;
  311. return 0;
  312. }
  313. *qctrl = cx23885_ctls[i].v;
  314. return 0;
  315. }
  316. /* ------------------------------------------------------------------- */
  317. /* resource management */
  318. static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
  319. unsigned int bit)
  320. {
  321. dprintk(1, "%s()\n", __func__);
  322. if (fh->resources & bit)
  323. /* have it already allocated */
  324. return 1;
  325. /* is it free? */
  326. mutex_lock(&dev->lock);
  327. if (dev->resources & bit) {
  328. /* no, someone else uses it */
  329. mutex_unlock(&dev->lock);
  330. return 0;
  331. }
  332. /* it's free, grab it */
  333. fh->resources |= bit;
  334. dev->resources |= bit;
  335. dprintk(1, "res: get %d\n", bit);
  336. mutex_unlock(&dev->lock);
  337. return 1;
  338. }
  339. static int res_check(struct cx23885_fh *fh, unsigned int bit)
  340. {
  341. return fh->resources & bit;
  342. }
  343. static int res_locked(struct cx23885_dev *dev, unsigned int bit)
  344. {
  345. return dev->resources & bit;
  346. }
  347. static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
  348. unsigned int bits)
  349. {
  350. BUG_ON((fh->resources & bits) != bits);
  351. dprintk(1, "%s()\n", __func__);
  352. mutex_lock(&dev->lock);
  353. fh->resources &= ~bits;
  354. dev->resources &= ~bits;
  355. dprintk(1, "res: put %d\n", bits);
  356. mutex_unlock(&dev->lock);
  357. }
  358. static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
  359. {
  360. struct v4l2_routing route;
  361. memset(&route, 0, sizeof(route));
  362. dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
  363. __func__,
  364. input, INPUT(input)->vmux,
  365. INPUT(input)->gpio0, INPUT(input)->gpio1,
  366. INPUT(input)->gpio2, INPUT(input)->gpio3);
  367. dev->input = input;
  368. route.input = INPUT(input)->vmux;
  369. /* Tell the internal A/V decoder */
  370. cx23885_call_i2c_clients(&dev->i2c_bus[2],
  371. VIDIOC_INT_S_VIDEO_ROUTING, &route);
  372. return 0;
  373. }
  374. /* ------------------------------------------------------------------ */
  375. static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
  376. unsigned int height, enum v4l2_field field)
  377. {
  378. dprintk(1, "%s()\n", __func__);
  379. return 0;
  380. }
  381. static int cx23885_start_video_dma(struct cx23885_dev *dev,
  382. struct cx23885_dmaqueue *q,
  383. struct cx23885_buffer *buf)
  384. {
  385. dprintk(1, "%s()\n", __func__);
  386. /* setup fifo + format */
  387. cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
  388. buf->bpl, buf->risc.dma);
  389. cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
  390. /* reset counter */
  391. cx_write(VID_A_GPCNT_CTL, 3);
  392. q->count = 1;
  393. /* enable irq */
  394. cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
  395. cx_set(VID_A_INT_MSK, 0x000011);
  396. /* start dma */
  397. cx_set(DEV_CNTRL2, (1<<5));
  398. cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
  399. return 0;
  400. }
  401. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  402. struct cx23885_dmaqueue *q)
  403. {
  404. struct cx23885_buffer *buf, *prev;
  405. struct list_head *item;
  406. dprintk(1, "%s()\n", __func__);
  407. if (!list_empty(&q->active)) {
  408. buf = list_entry(q->active.next, struct cx23885_buffer,
  409. vb.queue);
  410. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  411. buf, buf->vb.i);
  412. cx23885_start_video_dma(dev, q, buf);
  413. list_for_each(item, &q->active) {
  414. buf = list_entry(item, struct cx23885_buffer,
  415. vb.queue);
  416. buf->count = q->count++;
  417. }
  418. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  419. return 0;
  420. }
  421. prev = NULL;
  422. for (;;) {
  423. if (list_empty(&q->queued))
  424. return 0;
  425. buf = list_entry(q->queued.next, struct cx23885_buffer,
  426. vb.queue);
  427. if (NULL == prev) {
  428. list_move_tail(&buf->vb.queue, &q->active);
  429. cx23885_start_video_dma(dev, q, buf);
  430. buf->vb.state = VIDEOBUF_ACTIVE;
  431. buf->count = q->count++;
  432. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  433. dprintk(2, "[%p/%d] restart_queue - first active\n",
  434. buf, buf->vb.i);
  435. } else if (prev->vb.width == buf->vb.width &&
  436. prev->vb.height == buf->vb.height &&
  437. prev->fmt == buf->fmt) {
  438. list_move_tail(&buf->vb.queue, &q->active);
  439. buf->vb.state = VIDEOBUF_ACTIVE;
  440. buf->count = q->count++;
  441. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  442. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  443. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  444. buf, buf->vb.i);
  445. } else {
  446. return 0;
  447. }
  448. prev = buf;
  449. }
  450. }
  451. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  452. unsigned int *size)
  453. {
  454. struct cx23885_fh *fh = q->priv_data;
  455. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  456. if (0 == *count)
  457. *count = 32;
  458. while (*size * *count > vid_limit * 1024 * 1024)
  459. (*count)--;
  460. return 0;
  461. }
  462. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  463. enum v4l2_field field)
  464. {
  465. struct cx23885_fh *fh = q->priv_data;
  466. struct cx23885_dev *dev = fh->dev;
  467. struct cx23885_buffer *buf =
  468. container_of(vb, struct cx23885_buffer, vb);
  469. int rc, init_buffer = 0;
  470. u32 line0_offset, line1_offset;
  471. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  472. BUG_ON(NULL == fh->fmt);
  473. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  474. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  475. return -EINVAL;
  476. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  477. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  478. return -EINVAL;
  479. if (buf->fmt != fh->fmt ||
  480. buf->vb.width != fh->width ||
  481. buf->vb.height != fh->height ||
  482. buf->vb.field != field) {
  483. buf->fmt = fh->fmt;
  484. buf->vb.width = fh->width;
  485. buf->vb.height = fh->height;
  486. buf->vb.field = field;
  487. init_buffer = 1;
  488. }
  489. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  490. init_buffer = 1;
  491. rc = videobuf_iolock(q, &buf->vb, NULL);
  492. if (0 != rc)
  493. goto fail;
  494. }
  495. if (init_buffer) {
  496. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  497. switch (buf->vb.field) {
  498. case V4L2_FIELD_TOP:
  499. cx23885_risc_buffer(dev->pci, &buf->risc,
  500. dma->sglist, 0, UNSET,
  501. buf->bpl, 0, buf->vb.height);
  502. break;
  503. case V4L2_FIELD_BOTTOM:
  504. cx23885_risc_buffer(dev->pci, &buf->risc,
  505. dma->sglist, UNSET, 0,
  506. buf->bpl, 0, buf->vb.height);
  507. break;
  508. case V4L2_FIELD_INTERLACED:
  509. if (dev->tvnorm & V4L2_STD_NTSC) {
  510. /* cx25840 transmits NTSC bottom field first */
  511. dprintk(1, "%s() Creating NTSC risc\n",
  512. __func__);
  513. line0_offset = buf->bpl;
  514. line1_offset = 0;
  515. } else {
  516. /* All other formats are top field first */
  517. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  518. __func__);
  519. line0_offset = 0;
  520. line1_offset = buf->bpl;
  521. }
  522. cx23885_risc_buffer(dev->pci, &buf->risc,
  523. dma->sglist, line0_offset,
  524. line1_offset,
  525. buf->bpl, buf->bpl,
  526. buf->vb.height >> 1);
  527. break;
  528. case V4L2_FIELD_SEQ_TB:
  529. cx23885_risc_buffer(dev->pci, &buf->risc,
  530. dma->sglist,
  531. 0, buf->bpl * (buf->vb.height >> 1),
  532. buf->bpl, 0,
  533. buf->vb.height >> 1);
  534. break;
  535. case V4L2_FIELD_SEQ_BT:
  536. cx23885_risc_buffer(dev->pci, &buf->risc,
  537. dma->sglist,
  538. buf->bpl * (buf->vb.height >> 1), 0,
  539. buf->bpl, 0,
  540. buf->vb.height >> 1);
  541. break;
  542. default:
  543. BUG();
  544. }
  545. }
  546. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  547. buf, buf->vb.i,
  548. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  549. (unsigned long)buf->risc.dma);
  550. buf->vb.state = VIDEOBUF_PREPARED;
  551. return 0;
  552. fail:
  553. cx23885_free_buffer(q, buf);
  554. return rc;
  555. }
  556. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  557. {
  558. struct cx23885_buffer *buf = container_of(vb,
  559. struct cx23885_buffer, vb);
  560. struct cx23885_buffer *prev;
  561. struct cx23885_fh *fh = vq->priv_data;
  562. struct cx23885_dev *dev = fh->dev;
  563. struct cx23885_dmaqueue *q = &dev->vidq;
  564. /* add jump to stopper */
  565. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  566. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  567. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  568. if (!list_empty(&q->queued)) {
  569. list_add_tail(&buf->vb.queue, &q->queued);
  570. buf->vb.state = VIDEOBUF_QUEUED;
  571. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  572. buf, buf->vb.i);
  573. } else if (list_empty(&q->active)) {
  574. list_add_tail(&buf->vb.queue, &q->active);
  575. cx23885_start_video_dma(dev, q, buf);
  576. buf->vb.state = VIDEOBUF_ACTIVE;
  577. buf->count = q->count++;
  578. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  579. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  580. buf, buf->vb.i);
  581. } else {
  582. prev = list_entry(q->active.prev, struct cx23885_buffer,
  583. vb.queue);
  584. if (prev->vb.width == buf->vb.width &&
  585. prev->vb.height == buf->vb.height &&
  586. prev->fmt == buf->fmt) {
  587. list_add_tail(&buf->vb.queue, &q->active);
  588. buf->vb.state = VIDEOBUF_ACTIVE;
  589. buf->count = q->count++;
  590. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  591. /* 64 bit bits 63-32 */
  592. prev->risc.jmp[2] = cpu_to_le32(0);
  593. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  594. buf, buf->vb.i);
  595. } else {
  596. list_add_tail(&buf->vb.queue, &q->queued);
  597. buf->vb.state = VIDEOBUF_QUEUED;
  598. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  599. buf, buf->vb.i);
  600. }
  601. }
  602. }
  603. static void buffer_release(struct videobuf_queue *q,
  604. struct videobuf_buffer *vb)
  605. {
  606. struct cx23885_buffer *buf = container_of(vb,
  607. struct cx23885_buffer, vb);
  608. cx23885_free_buffer(q, buf);
  609. }
  610. static struct videobuf_queue_ops cx23885_video_qops = {
  611. .buf_setup = buffer_setup,
  612. .buf_prepare = buffer_prepare,
  613. .buf_queue = buffer_queue,
  614. .buf_release = buffer_release,
  615. };
  616. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  617. {
  618. switch (fh->type) {
  619. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  620. return &fh->vidq;
  621. case V4L2_BUF_TYPE_VBI_CAPTURE:
  622. return &fh->vbiq;
  623. default:
  624. BUG();
  625. return NULL;
  626. }
  627. }
  628. static int get_resource(struct cx23885_fh *fh)
  629. {
  630. switch (fh->type) {
  631. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  632. return RESOURCE_VIDEO;
  633. case V4L2_BUF_TYPE_VBI_CAPTURE:
  634. return RESOURCE_VBI;
  635. default:
  636. BUG();
  637. return 0;
  638. }
  639. }
  640. static int video_open(struct file *file)
  641. {
  642. int minor = video_devdata(file)->minor;
  643. struct cx23885_dev *h, *dev = NULL;
  644. struct cx23885_fh *fh;
  645. struct list_head *list;
  646. enum v4l2_buf_type type = 0;
  647. int radio = 0;
  648. lock_kernel();
  649. list_for_each(list, &cx23885_devlist) {
  650. h = list_entry(list, struct cx23885_dev, devlist);
  651. if (h->video_dev->minor == minor) {
  652. dev = h;
  653. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  654. }
  655. if (h->vbi_dev &&
  656. h->vbi_dev->minor == minor) {
  657. dev = h;
  658. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  659. }
  660. if (h->radio_dev &&
  661. h->radio_dev->minor == minor) {
  662. radio = 1;
  663. dev = h;
  664. }
  665. }
  666. if (NULL == dev) {
  667. unlock_kernel();
  668. return -ENODEV;
  669. }
  670. dprintk(1, "open minor=%d radio=%d type=%s\n",
  671. minor, radio, v4l2_type_names[type]);
  672. /* allocate + initialize per filehandle data */
  673. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  674. if (NULL == fh) {
  675. unlock_kernel();
  676. return -ENOMEM;
  677. }
  678. file->private_data = fh;
  679. fh->dev = dev;
  680. fh->radio = radio;
  681. fh->type = type;
  682. fh->width = 320;
  683. fh->height = 240;
  684. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  685. videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
  686. &dev->pci->dev, &dev->slock,
  687. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  688. V4L2_FIELD_INTERLACED,
  689. sizeof(struct cx23885_buffer),
  690. fh);
  691. dprintk(1, "post videobuf_queue_init()\n");
  692. unlock_kernel();
  693. return 0;
  694. }
  695. static ssize_t video_read(struct file *file, char __user *data,
  696. size_t count, loff_t *ppos)
  697. {
  698. struct cx23885_fh *fh = file->private_data;
  699. switch (fh->type) {
  700. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  701. if (res_locked(fh->dev, RESOURCE_VIDEO))
  702. return -EBUSY;
  703. return videobuf_read_one(&fh->vidq, data, count, ppos,
  704. file->f_flags & O_NONBLOCK);
  705. case V4L2_BUF_TYPE_VBI_CAPTURE:
  706. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  707. return -EBUSY;
  708. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  709. file->f_flags & O_NONBLOCK);
  710. default:
  711. BUG();
  712. return 0;
  713. }
  714. }
  715. static unsigned int video_poll(struct file *file,
  716. struct poll_table_struct *wait)
  717. {
  718. struct cx23885_fh *fh = file->private_data;
  719. struct cx23885_buffer *buf;
  720. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  721. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  722. return POLLERR;
  723. return videobuf_poll_stream(file, &fh->vbiq, wait);
  724. }
  725. if (res_check(fh, RESOURCE_VIDEO)) {
  726. /* streaming capture */
  727. if (list_empty(&fh->vidq.stream))
  728. return POLLERR;
  729. buf = list_entry(fh->vidq.stream.next,
  730. struct cx23885_buffer, vb.stream);
  731. } else {
  732. /* read() capture */
  733. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  734. if (NULL == buf)
  735. return POLLERR;
  736. }
  737. poll_wait(file, &buf->vb.done, wait);
  738. if (buf->vb.state == VIDEOBUF_DONE ||
  739. buf->vb.state == VIDEOBUF_ERROR)
  740. return POLLIN|POLLRDNORM;
  741. return 0;
  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. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_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. if (f->fmt.pix.height < 32)
  855. f->fmt.pix.height = 32;
  856. if (f->fmt.pix.height > maxh)
  857. f->fmt.pix.height = maxh;
  858. if (f->fmt.pix.width < 48)
  859. f->fmt.pix.width = 48;
  860. if (f->fmt.pix.width > maxw)
  861. f->fmt.pix.width = maxw;
  862. f->fmt.pix.width &= ~0x03;
  863. f->fmt.pix.bytesperline =
  864. (f->fmt.pix.width * fmt->depth) >> 3;
  865. f->fmt.pix.sizeimage =
  866. f->fmt.pix.height * f->fmt.pix.bytesperline;
  867. return 0;
  868. }
  869. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  870. struct v4l2_format *f)
  871. {
  872. struct cx23885_fh *fh = priv;
  873. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  874. int err;
  875. dprintk(2, "%s()\n", __func__);
  876. err = vidioc_try_fmt_vid_cap(file, priv, f);
  877. if (0 != err)
  878. return err;
  879. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  880. fh->width = f->fmt.pix.width;
  881. fh->height = f->fmt.pix.height;
  882. fh->vidq.field = f->fmt.pix.field;
  883. dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
  884. fh->width, fh->height, fh->vidq.field);
  885. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
  886. return 0;
  887. }
  888. static int vidioc_querycap(struct file *file, void *priv,
  889. struct v4l2_capability *cap)
  890. {
  891. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  892. strcpy(cap->driver, "cx23885");
  893. strlcpy(cap->card, cx23885_boards[dev->board].name,
  894. sizeof(cap->card));
  895. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  896. cap->version = CX23885_VERSION_CODE;
  897. cap->capabilities =
  898. V4L2_CAP_VIDEO_CAPTURE |
  899. V4L2_CAP_READWRITE |
  900. V4L2_CAP_STREAMING |
  901. V4L2_CAP_VBI_CAPTURE;
  902. if (UNSET != dev->tuner_type)
  903. cap->capabilities |= V4L2_CAP_TUNER;
  904. return 0;
  905. }
  906. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  907. struct v4l2_fmtdesc *f)
  908. {
  909. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  910. return -EINVAL;
  911. strlcpy(f->description, formats[f->index].name,
  912. sizeof(f->description));
  913. f->pixelformat = formats[f->index].fourcc;
  914. return 0;
  915. }
  916. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  917. static int vidiocgmbuf(struct file *file, void *priv,
  918. struct video_mbuf *mbuf)
  919. {
  920. struct cx23885_fh *fh = priv;
  921. struct videobuf_queue *q;
  922. struct v4l2_requestbuffers req;
  923. unsigned int i;
  924. int err;
  925. q = get_queue(fh);
  926. memset(&req, 0, sizeof(req));
  927. req.type = q->type;
  928. req.count = 8;
  929. req.memory = V4L2_MEMORY_MMAP;
  930. err = videobuf_reqbufs(q, &req);
  931. if (err < 0)
  932. return err;
  933. mbuf->frames = req.count;
  934. mbuf->size = 0;
  935. for (i = 0; i < mbuf->frames; i++) {
  936. mbuf->offsets[i] = q->bufs[i]->boff;
  937. mbuf->size += q->bufs[i]->bsize;
  938. }
  939. return 0;
  940. }
  941. #endif
  942. static int vidioc_reqbufs(struct file *file, void *priv,
  943. struct v4l2_requestbuffers *p)
  944. {
  945. struct cx23885_fh *fh = priv;
  946. return videobuf_reqbufs(get_queue(fh), p);
  947. }
  948. static int vidioc_querybuf(struct file *file, void *priv,
  949. struct v4l2_buffer *p)
  950. {
  951. struct cx23885_fh *fh = priv;
  952. return videobuf_querybuf(get_queue(fh), p);
  953. }
  954. static int vidioc_qbuf(struct file *file, void *priv,
  955. struct v4l2_buffer *p)
  956. {
  957. struct cx23885_fh *fh = priv;
  958. return videobuf_qbuf(get_queue(fh), p);
  959. }
  960. static int vidioc_dqbuf(struct file *file, void *priv,
  961. struct v4l2_buffer *p)
  962. {
  963. struct cx23885_fh *fh = priv;
  964. return videobuf_dqbuf(get_queue(fh), p,
  965. file->f_flags & O_NONBLOCK);
  966. }
  967. static int vidioc_streamon(struct file *file, void *priv,
  968. enum v4l2_buf_type i)
  969. {
  970. struct cx23885_fh *fh = priv;
  971. struct cx23885_dev *dev = fh->dev;
  972. dprintk(1, "%s()\n", __func__);
  973. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  974. return -EINVAL;
  975. if (unlikely(i != fh->type))
  976. return -EINVAL;
  977. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  978. return -EBUSY;
  979. return videobuf_streamon(get_queue(fh));
  980. }
  981. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  982. {
  983. struct cx23885_fh *fh = priv;
  984. struct cx23885_dev *dev = fh->dev;
  985. int err, res;
  986. dprintk(1, "%s()\n", __func__);
  987. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  988. return -EINVAL;
  989. if (i != fh->type)
  990. return -EINVAL;
  991. res = get_resource(fh);
  992. err = videobuf_streamoff(get_queue(fh));
  993. if (err < 0)
  994. return err;
  995. res_free(dev, fh, res);
  996. return 0;
  997. }
  998. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  999. {
  1000. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1001. dprintk(1, "%s()\n", __func__);
  1002. mutex_lock(&dev->lock);
  1003. cx23885_set_tvnorm(dev, *tvnorms);
  1004. mutex_unlock(&dev->lock);
  1005. return 0;
  1006. }
  1007. static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1008. {
  1009. static const char *iname[] = {
  1010. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1011. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1012. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1013. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1014. [CX23885_VMUX_SVIDEO] = "S-Video",
  1015. [CX23885_VMUX_TELEVISION] = "Television",
  1016. [CX23885_VMUX_CABLE] = "Cable TV",
  1017. [CX23885_VMUX_DVB] = "DVB",
  1018. [CX23885_VMUX_DEBUG] = "for debug only",
  1019. };
  1020. unsigned int n;
  1021. dprintk(1, "%s()\n", __func__);
  1022. n = i->index;
  1023. if (n >= 4)
  1024. return -EINVAL;
  1025. if (0 == INPUT(n)->type)
  1026. return -EINVAL;
  1027. memset(i, 0, sizeof(*i));
  1028. i->index = n;
  1029. i->type = V4L2_INPUT_TYPE_CAMERA;
  1030. strcpy(i->name, iname[INPUT(n)->type]);
  1031. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1032. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1033. i->type = V4L2_INPUT_TYPE_TUNER;
  1034. i->std = CX23885_NORMS;
  1035. return 0;
  1036. }
  1037. static int vidioc_enum_input(struct file *file, void *priv,
  1038. struct v4l2_input *i)
  1039. {
  1040. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1041. dprintk(1, "%s()\n", __func__);
  1042. return cx23885_enum_input(dev, i);
  1043. }
  1044. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1045. {
  1046. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1047. *i = dev->input;
  1048. dprintk(1, "%s() returns %d\n", __func__, *i);
  1049. return 0;
  1050. }
  1051. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1052. {
  1053. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1054. dprintk(1, "%s(%d)\n", __func__, i);
  1055. if (i >= 4) {
  1056. dprintk(1, "%s() -EINVAL\n", __func__);
  1057. return -EINVAL;
  1058. }
  1059. mutex_lock(&dev->lock);
  1060. cx23885_video_mux(dev, i);
  1061. mutex_unlock(&dev->lock);
  1062. return 0;
  1063. }
  1064. static int vidioc_queryctrl(struct file *file, void *priv,
  1065. struct v4l2_queryctrl *qctrl)
  1066. {
  1067. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1068. if (unlikely(qctrl->id == 0))
  1069. return -EINVAL;
  1070. return cx23885_ctrl_query(qctrl);
  1071. }
  1072. static int vidioc_g_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_get_control(dev, ctl);
  1077. }
  1078. static int vidioc_s_ctrl(struct file *file, void *priv,
  1079. struct v4l2_control *ctl)
  1080. {
  1081. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1082. return cx23885_set_control(dev, ctl);
  1083. }
  1084. static int vidioc_g_tuner(struct file *file, void *priv,
  1085. struct v4l2_tuner *t)
  1086. {
  1087. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1088. if (unlikely(UNSET == dev->tuner_type))
  1089. return -EINVAL;
  1090. if (0 != t->index)
  1091. return -EINVAL;
  1092. strcpy(t->name, "Television");
  1093. t->type = V4L2_TUNER_ANALOG_TV;
  1094. t->capability = V4L2_TUNER_CAP_NORM;
  1095. t->rangehigh = 0xffffffffUL;
  1096. t->signal = 0xffff ; /* LOCKED */
  1097. return 0;
  1098. }
  1099. static int vidioc_s_tuner(struct file *file, void *priv,
  1100. struct v4l2_tuner *t)
  1101. {
  1102. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1103. if (UNSET == dev->tuner_type)
  1104. return -EINVAL;
  1105. if (0 != t->index)
  1106. return -EINVAL;
  1107. return 0;
  1108. }
  1109. static int vidioc_g_frequency(struct file *file, void *priv,
  1110. struct v4l2_frequency *f)
  1111. {
  1112. struct cx23885_fh *fh = priv;
  1113. struct cx23885_dev *dev = fh->dev;
  1114. if (unlikely(UNSET == dev->tuner_type))
  1115. return -EINVAL;
  1116. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1117. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1118. f->frequency = dev->freq;
  1119. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
  1120. return 0;
  1121. }
  1122. static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1123. {
  1124. if (unlikely(UNSET == dev->tuner_type))
  1125. return -EINVAL;
  1126. if (unlikely(f->tuner != 0))
  1127. return -EINVAL;
  1128. mutex_lock(&dev->lock);
  1129. dev->freq = f->frequency;
  1130. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
  1131. /* When changing channels it is required to reset TVAUDIO */
  1132. msleep(10);
  1133. mutex_unlock(&dev->lock);
  1134. return 0;
  1135. }
  1136. static int vidioc_s_frequency(struct file *file, void *priv,
  1137. struct v4l2_frequency *f)
  1138. {
  1139. struct cx23885_fh *fh = priv;
  1140. struct cx23885_dev *dev = fh->dev;
  1141. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1142. return -EINVAL;
  1143. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1144. return -EINVAL;
  1145. return
  1146. cx23885_set_freq(dev, f);
  1147. }
  1148. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1149. static int vidioc_g_register(struct file *file, void *fh,
  1150. struct v4l2_dbg_register *reg)
  1151. {
  1152. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1153. if (!v4l2_chip_match_host(&reg->match))
  1154. return -EINVAL;
  1155. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
  1156. return 0;
  1157. }
  1158. static int vidioc_s_register(struct file *file, void *fh,
  1159. struct v4l2_dbg_register *reg)
  1160. {
  1161. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1162. if (!v4l2_chip_match_host(&reg->match))
  1163. return -EINVAL;
  1164. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
  1165. return 0;
  1166. }
  1167. #endif
  1168. /* ----------------------------------------------------------- */
  1169. static void cx23885_vid_timeout(unsigned long data)
  1170. {
  1171. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1172. struct cx23885_dmaqueue *q = &dev->vidq;
  1173. struct cx23885_buffer *buf;
  1174. unsigned long flags;
  1175. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1176. cx_clear(VID_A_DMA_CTL, 0x11);
  1177. spin_lock_irqsave(&dev->slock, flags);
  1178. while (!list_empty(&q->active)) {
  1179. buf = list_entry(q->active.next,
  1180. struct cx23885_buffer, vb.queue);
  1181. list_del(&buf->vb.queue);
  1182. buf->vb.state = VIDEOBUF_ERROR;
  1183. wake_up(&buf->vb.done);
  1184. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1185. dev->name, buf, buf->vb.i,
  1186. (unsigned long)buf->risc.dma);
  1187. }
  1188. cx23885_restart_video_queue(dev, q);
  1189. spin_unlock_irqrestore(&dev->slock, flags);
  1190. }
  1191. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1192. {
  1193. u32 mask, count;
  1194. int handled = 0;
  1195. mask = cx_read(VID_A_INT_MSK);
  1196. if (0 == (status & mask))
  1197. return handled;
  1198. cx_write(VID_A_INT_STAT, status);
  1199. dprintk(2, "%s() status = 0x%08x\n", __func__, status);
  1200. /* risc op code error */
  1201. if (status & (1 << 16)) {
  1202. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1203. dev->name);
  1204. cx_clear(VID_A_DMA_CTL, 0x11);
  1205. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1206. }
  1207. /* risc1 y */
  1208. if (status & 0x01) {
  1209. spin_lock(&dev->slock);
  1210. count = cx_read(VID_A_GPCNT);
  1211. cx23885_video_wakeup(dev, &dev->vidq, count);
  1212. spin_unlock(&dev->slock);
  1213. handled++;
  1214. }
  1215. /* risc2 y */
  1216. if (status & 0x10) {
  1217. dprintk(2, "stopper video\n");
  1218. spin_lock(&dev->slock);
  1219. cx23885_restart_video_queue(dev, &dev->vidq);
  1220. spin_unlock(&dev->slock);
  1221. handled++;
  1222. }
  1223. return handled;
  1224. }
  1225. /* ----------------------------------------------------------- */
  1226. /* exported stuff */
  1227. static const struct v4l2_file_operations video_fops = {
  1228. .owner = THIS_MODULE,
  1229. .open = video_open,
  1230. .release = video_release,
  1231. .read = video_read,
  1232. .poll = video_poll,
  1233. .mmap = video_mmap,
  1234. .ioctl = video_ioctl2,
  1235. };
  1236. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1237. .vidioc_querycap = vidioc_querycap,
  1238. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1239. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1240. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1241. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1242. .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
  1243. .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
  1244. .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
  1245. .vidioc_reqbufs = vidioc_reqbufs,
  1246. .vidioc_querybuf = vidioc_querybuf,
  1247. .vidioc_qbuf = vidioc_qbuf,
  1248. .vidioc_dqbuf = vidioc_dqbuf,
  1249. .vidioc_s_std = vidioc_s_std,
  1250. .vidioc_enum_input = vidioc_enum_input,
  1251. .vidioc_g_input = vidioc_g_input,
  1252. .vidioc_s_input = vidioc_s_input,
  1253. .vidioc_queryctrl = vidioc_queryctrl,
  1254. .vidioc_g_ctrl = vidioc_g_ctrl,
  1255. .vidioc_s_ctrl = vidioc_s_ctrl,
  1256. .vidioc_streamon = vidioc_streamon,
  1257. .vidioc_streamoff = vidioc_streamoff,
  1258. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1259. .vidiocgmbuf = vidiocgmbuf,
  1260. #endif
  1261. .vidioc_g_tuner = vidioc_g_tuner,
  1262. .vidioc_s_tuner = vidioc_s_tuner,
  1263. .vidioc_g_frequency = vidioc_g_frequency,
  1264. .vidioc_s_frequency = vidioc_s_frequency,
  1265. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1266. .vidioc_g_register = vidioc_g_register,
  1267. .vidioc_s_register = vidioc_s_register,
  1268. #endif
  1269. };
  1270. static struct video_device cx23885_vbi_template;
  1271. static struct video_device cx23885_video_template = {
  1272. .name = "cx23885-video",
  1273. .fops = &video_fops,
  1274. .minor = -1,
  1275. .ioctl_ops = &video_ioctl_ops,
  1276. .tvnorms = CX23885_NORMS,
  1277. .current_norm = V4L2_STD_NTSC_M,
  1278. };
  1279. static const struct v4l2_file_operations radio_fops = {
  1280. .owner = THIS_MODULE,
  1281. .open = video_open,
  1282. .release = video_release,
  1283. .ioctl = video_ioctl2,
  1284. };
  1285. void cx23885_video_unregister(struct cx23885_dev *dev)
  1286. {
  1287. dprintk(1, "%s()\n", __func__);
  1288. cx_clear(PCI_INT_MSK, 1);
  1289. if (dev->video_dev) {
  1290. if (-1 != dev->video_dev->minor)
  1291. video_unregister_device(dev->video_dev);
  1292. else
  1293. video_device_release(dev->video_dev);
  1294. dev->video_dev = NULL;
  1295. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1296. }
  1297. }
  1298. int cx23885_video_register(struct cx23885_dev *dev)
  1299. {
  1300. int err;
  1301. dprintk(1, "%s()\n", __func__);
  1302. spin_lock_init(&dev->slock);
  1303. /* Initialize VBI template */
  1304. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1305. sizeof(cx23885_vbi_template));
  1306. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1307. dev->tvnorm = cx23885_video_template.current_norm;
  1308. /* init video dma queues */
  1309. INIT_LIST_HEAD(&dev->vidq.active);
  1310. INIT_LIST_HEAD(&dev->vidq.queued);
  1311. dev->vidq.timeout.function = cx23885_vid_timeout;
  1312. dev->vidq.timeout.data = (unsigned long)dev;
  1313. init_timer(&dev->vidq.timeout);
  1314. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1315. VID_A_DMA_CTL, 0x11, 0x00);
  1316. /* Don't enable VBI yet */
  1317. cx_set(PCI_INT_MSK, 1);
  1318. /* register v4l devices */
  1319. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1320. &cx23885_video_template, "video");
  1321. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1322. video_nr[dev->nr]);
  1323. if (err < 0) {
  1324. printk(KERN_INFO "%s: can't register video device\n",
  1325. dev->name);
  1326. goto fail_unreg;
  1327. }
  1328. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1329. dev->name, dev->video_dev->num);
  1330. /* initial device configuration */
  1331. mutex_lock(&dev->lock);
  1332. cx23885_set_tvnorm(dev, dev->tvnorm);
  1333. init_controls(dev);
  1334. cx23885_video_mux(dev, 0);
  1335. mutex_unlock(&dev->lock);
  1336. return 0;
  1337. fail_unreg:
  1338. cx23885_video_unregister(dev);
  1339. return err;
  1340. }