cx23885-video.c 40 KB

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