cx23885-video.c 39 KB

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