cx23885-video.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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_WARN "%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. #ifdef CONFIG_PM
  404. static int cx23885_stop_video_dma(struct cx23885_dev *dev)
  405. {
  406. dprintk(1, "%s()\n", __FUNCTION__);
  407. /* stop dma */
  408. cx_clear(VID_A_DMA_CTL, 0x11);
  409. /* disable irqs */
  410. cx_clear(PCI_INT_MSK, 0x000001);
  411. cx_clear(VID_A_INT_MSK, 0x000011);
  412. return 0;
  413. }
  414. #endif
  415. static int cx23885_restart_video_queue(struct cx23885_dev *dev,
  416. struct cx23885_dmaqueue *q)
  417. {
  418. struct cx23885_buffer *buf, *prev;
  419. struct list_head *item;
  420. dprintk(1, "%s()\n", __FUNCTION__);
  421. if (!list_empty(&q->active)) {
  422. buf = list_entry(q->active.next, struct cx23885_buffer,
  423. vb.queue);
  424. dprintk(2, "restart_queue [%p/%d]: restart dma\n",
  425. buf, buf->vb.i);
  426. cx23885_start_video_dma(dev, q, buf);
  427. list_for_each(item, &q->active) {
  428. buf = list_entry(item, struct cx23885_buffer,
  429. vb.queue);
  430. buf->count = q->count++;
  431. }
  432. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  433. return 0;
  434. }
  435. prev = NULL;
  436. for (;;) {
  437. if (list_empty(&q->queued))
  438. return 0;
  439. buf = list_entry(q->queued.next, struct cx23885_buffer,
  440. vb.queue);
  441. if (NULL == prev) {
  442. list_move_tail(&buf->vb.queue, &q->active);
  443. cx23885_start_video_dma(dev, q, buf);
  444. buf->vb.state = VIDEOBUF_ACTIVE;
  445. buf->count = q->count++;
  446. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  447. dprintk(2, "[%p/%d] restart_queue - first active\n",
  448. buf, buf->vb.i);
  449. } else if (prev->vb.width == buf->vb.width &&
  450. prev->vb.height == buf->vb.height &&
  451. prev->fmt == buf->fmt) {
  452. list_move_tail(&buf->vb.queue, &q->active);
  453. buf->vb.state = VIDEOBUF_ACTIVE;
  454. buf->count = q->count++;
  455. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  456. prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
  457. dprintk(2, "[%p/%d] restart_queue - move to active\n",
  458. buf, buf->vb.i);
  459. } else {
  460. return 0;
  461. }
  462. prev = buf;
  463. }
  464. }
  465. static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
  466. unsigned int *size)
  467. {
  468. struct cx23885_fh *fh = q->priv_data;
  469. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  470. if (0 == *count)
  471. *count = 32;
  472. while (*size * *count > vid_limit * 1024 * 1024)
  473. (*count)--;
  474. return 0;
  475. }
  476. static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  477. enum v4l2_field field)
  478. {
  479. struct cx23885_fh *fh = q->priv_data;
  480. struct cx23885_dev *dev = fh->dev;
  481. struct cx23885_buffer *buf =
  482. container_of(vb, struct cx23885_buffer, vb);
  483. int rc, init_buffer = 0;
  484. u32 line0_offset, line1_offset;
  485. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  486. BUG_ON(NULL == fh->fmt);
  487. if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
  488. fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
  489. return -EINVAL;
  490. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  491. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  492. return -EINVAL;
  493. if (buf->fmt != fh->fmt ||
  494. buf->vb.width != fh->width ||
  495. buf->vb.height != fh->height ||
  496. buf->vb.field != field) {
  497. buf->fmt = fh->fmt;
  498. buf->vb.width = fh->width;
  499. buf->vb.height = fh->height;
  500. buf->vb.field = field;
  501. init_buffer = 1;
  502. }
  503. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  504. init_buffer = 1;
  505. rc = videobuf_iolock(q, &buf->vb, NULL);
  506. if (0 != rc)
  507. goto fail;
  508. }
  509. if (init_buffer) {
  510. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  511. switch (buf->vb.field) {
  512. case V4L2_FIELD_TOP:
  513. cx23885_risc_buffer(dev->pci, &buf->risc,
  514. dma->sglist, 0, UNSET,
  515. buf->bpl, 0, buf->vb.height);
  516. break;
  517. case V4L2_FIELD_BOTTOM:
  518. cx23885_risc_buffer(dev->pci, &buf->risc,
  519. dma->sglist, UNSET, 0,
  520. buf->bpl, 0, buf->vb.height);
  521. break;
  522. case V4L2_FIELD_INTERLACED:
  523. if (dev->tvnorm & V4L2_STD_NTSC) {
  524. /* cx25840 transmits NTSC bottom field first */
  525. dprintk(1, "%s() Creating NTSC risc\n",
  526. __FUNCTION__);
  527. line0_offset = buf->bpl;
  528. line1_offset = 0;
  529. } else {
  530. /* All other formats are top field first */
  531. dprintk(1, "%s() Creating PAL/SECAM risc\n",
  532. __FUNCTION__);
  533. line0_offset = 0;
  534. line1_offset = buf->bpl;
  535. }
  536. cx23885_risc_buffer(dev->pci, &buf->risc,
  537. dma->sglist, line0_offset,
  538. line1_offset,
  539. buf->bpl, buf->bpl,
  540. buf->vb.height >> 1);
  541. break;
  542. case V4L2_FIELD_SEQ_TB:
  543. cx23885_risc_buffer(dev->pci, &buf->risc,
  544. dma->sglist,
  545. 0, buf->bpl * (buf->vb.height >> 1),
  546. buf->bpl, 0,
  547. buf->vb.height >> 1);
  548. break;
  549. case V4L2_FIELD_SEQ_BT:
  550. cx23885_risc_buffer(dev->pci, &buf->risc,
  551. dma->sglist,
  552. buf->bpl * (buf->vb.height >> 1), 0,
  553. buf->bpl, 0,
  554. buf->vb.height >> 1);
  555. break;
  556. default:
  557. BUG();
  558. }
  559. }
  560. dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  561. buf, buf->vb.i,
  562. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  563. (unsigned long)buf->risc.dma);
  564. buf->vb.state = VIDEOBUF_PREPARED;
  565. return 0;
  566. fail:
  567. cx23885_free_buffer(q, buf);
  568. return rc;
  569. }
  570. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  571. {
  572. struct cx23885_buffer *buf = container_of(vb,
  573. struct cx23885_buffer, vb);
  574. struct cx23885_buffer *prev;
  575. struct cx23885_fh *fh = vq->priv_data;
  576. struct cx23885_dev *dev = fh->dev;
  577. struct cx23885_dmaqueue *q = &dev->vidq;
  578. /* add jump to stopper */
  579. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  580. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  581. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  582. if (!list_empty(&q->queued)) {
  583. list_add_tail(&buf->vb.queue, &q->queued);
  584. buf->vb.state = VIDEOBUF_QUEUED;
  585. dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
  586. buf, buf->vb.i);
  587. } else if (list_empty(&q->active)) {
  588. list_add_tail(&buf->vb.queue, &q->active);
  589. cx23885_start_video_dma(dev, q, buf);
  590. buf->vb.state = VIDEOBUF_ACTIVE;
  591. buf->count = q->count++;
  592. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  593. dprintk(2, "[%p/%d] buffer_queue - first active\n",
  594. buf, buf->vb.i);
  595. } else {
  596. prev = list_entry(q->active.prev, struct cx23885_buffer,
  597. vb.queue);
  598. if (prev->vb.width == buf->vb.width &&
  599. prev->vb.height == buf->vb.height &&
  600. prev->fmt == buf->fmt) {
  601. list_add_tail(&buf->vb.queue, &q->active);
  602. buf->vb.state = VIDEOBUF_ACTIVE;
  603. buf->count = q->count++;
  604. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  605. /* 64 bit bits 63-32 */
  606. prev->risc.jmp[2] = cpu_to_le32(0);
  607. dprintk(2, "[%p/%d] buffer_queue - append to active\n",
  608. buf, buf->vb.i);
  609. } else {
  610. list_add_tail(&buf->vb.queue, &q->queued);
  611. buf->vb.state = VIDEOBUF_QUEUED;
  612. dprintk(2, "[%p/%d] buffer_queue - first queued\n",
  613. buf, buf->vb.i);
  614. }
  615. }
  616. }
  617. static void buffer_release(struct videobuf_queue *q,
  618. struct videobuf_buffer *vb)
  619. {
  620. struct cx23885_buffer *buf = container_of(vb,
  621. struct cx23885_buffer, vb);
  622. cx23885_free_buffer(q, buf);
  623. }
  624. static struct videobuf_queue_ops cx23885_video_qops = {
  625. .buf_setup = buffer_setup,
  626. .buf_prepare = buffer_prepare,
  627. .buf_queue = buffer_queue,
  628. .buf_release = buffer_release,
  629. };
  630. static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
  631. {
  632. switch (fh->type) {
  633. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  634. return &fh->vidq;
  635. case V4L2_BUF_TYPE_VBI_CAPTURE:
  636. return &fh->vbiq;
  637. default:
  638. BUG();
  639. return NULL;
  640. }
  641. }
  642. static int get_resource(struct cx23885_fh *fh)
  643. {
  644. switch (fh->type) {
  645. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  646. return RESOURCE_VIDEO;
  647. case V4L2_BUF_TYPE_VBI_CAPTURE:
  648. return RESOURCE_VBI;
  649. default:
  650. BUG();
  651. return 0;
  652. }
  653. }
  654. static int video_open(struct inode *inode, struct file *file)
  655. {
  656. int minor = iminor(inode);
  657. struct cx23885_dev *h, *dev = NULL;
  658. struct cx23885_fh *fh;
  659. struct list_head *list;
  660. enum v4l2_buf_type type = 0;
  661. int radio = 0;
  662. list_for_each(list, &cx23885_devlist) {
  663. h = list_entry(list, struct cx23885_dev, devlist);
  664. if (h->video_dev->minor == minor) {
  665. dev = h;
  666. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  667. }
  668. if (h->vbi_dev &&
  669. h->vbi_dev->minor == minor) {
  670. dev = h;
  671. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  672. }
  673. if (h->radio_dev &&
  674. h->radio_dev->minor == minor) {
  675. radio = 1;
  676. dev = h;
  677. }
  678. }
  679. if (NULL == dev)
  680. return -ENODEV;
  681. dprintk(1, "open minor=%d radio=%d type=%s\n",
  682. minor, radio, v4l2_type_names[type]);
  683. /* allocate + initialize per filehandle data */
  684. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  685. if (NULL == fh)
  686. return -ENOMEM;
  687. file->private_data = fh;
  688. fh->dev = dev;
  689. fh->radio = radio;
  690. fh->type = type;
  691. fh->width = 320;
  692. fh->height = 240;
  693. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  694. videobuf_queue_pci_init(&fh->vidq, &cx23885_video_qops,
  695. dev->pci, &dev->slock,
  696. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  697. V4L2_FIELD_INTERLACED,
  698. sizeof(struct cx23885_buffer),
  699. fh);
  700. dprintk(1, "post videobuf_queue_init()\n");
  701. return 0;
  702. }
  703. static ssize_t video_read(struct file *file, char __user *data,
  704. size_t count, loff_t *ppos)
  705. {
  706. struct cx23885_fh *fh = file->private_data;
  707. switch (fh->type) {
  708. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  709. if (res_locked(fh->dev, RESOURCE_VIDEO))
  710. return -EBUSY;
  711. return videobuf_read_one(&fh->vidq, data, count, ppos,
  712. file->f_flags & O_NONBLOCK);
  713. case V4L2_BUF_TYPE_VBI_CAPTURE:
  714. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  715. return -EBUSY;
  716. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  717. file->f_flags & O_NONBLOCK);
  718. default:
  719. BUG();
  720. return 0;
  721. }
  722. }
  723. static unsigned int video_poll(struct file *file,
  724. struct poll_table_struct *wait)
  725. {
  726. struct cx23885_fh *fh = file->private_data;
  727. struct cx23885_buffer *buf;
  728. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  729. if (!res_get(fh->dev, fh, RESOURCE_VBI))
  730. return POLLERR;
  731. return videobuf_poll_stream(file, &fh->vbiq, wait);
  732. }
  733. if (res_check(fh, RESOURCE_VIDEO)) {
  734. /* streaming capture */
  735. if (list_empty(&fh->vidq.stream))
  736. return POLLERR;
  737. buf = list_entry(fh->vidq.stream.next,
  738. struct cx23885_buffer, vb.stream);
  739. } else {
  740. /* read() capture */
  741. buf = (struct cx23885_buffer *)fh->vidq.read_buf;
  742. if (NULL == buf)
  743. return POLLERR;
  744. }
  745. poll_wait(file, &buf->vb.done, wait);
  746. if (buf->vb.state == VIDEOBUF_DONE ||
  747. buf->vb.state == VIDEOBUF_ERROR)
  748. return POLLIN|POLLRDNORM;
  749. return 0;
  750. }
  751. static int video_release(struct inode *inode, struct file *file)
  752. {
  753. struct cx23885_fh *fh = file->private_data;
  754. struct cx23885_dev *dev = fh->dev;
  755. /* turn off overlay */
  756. if (res_check(fh, RESOURCE_OVERLAY)) {
  757. /* FIXME */
  758. res_free(dev, fh, RESOURCE_OVERLAY);
  759. }
  760. /* stop video capture */
  761. if (res_check(fh, RESOURCE_VIDEO)) {
  762. videobuf_queue_cancel(&fh->vidq);
  763. res_free(dev, fh, RESOURCE_VIDEO);
  764. }
  765. if (fh->vidq.read_buf) {
  766. buffer_release(&fh->vidq, fh->vidq.read_buf);
  767. kfree(fh->vidq.read_buf);
  768. }
  769. /* stop vbi capture */
  770. if (res_check(fh, RESOURCE_VBI)) {
  771. if (fh->vbiq.streaming)
  772. videobuf_streamoff(&fh->vbiq);
  773. if (fh->vbiq.reading)
  774. videobuf_read_stop(&fh->vbiq);
  775. res_free(dev, fh, RESOURCE_VBI);
  776. }
  777. videobuf_mmap_free(&fh->vidq);
  778. file->private_data = NULL;
  779. kfree(fh);
  780. return 0;
  781. }
  782. static int video_mmap(struct file *file, struct vm_area_struct *vma)
  783. {
  784. struct cx23885_fh *fh = file->private_data;
  785. return videobuf_mmap_mapper(get_queue(fh), vma);
  786. }
  787. /* ------------------------------------------------------------------ */
  788. /* VIDEO CTRL IOCTLS */
  789. int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
  790. {
  791. dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __FUNCTION__);
  792. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(cx23885_get_control);
  796. int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
  797. {
  798. dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
  799. " (disabled - no action)\n", __FUNCTION__);
  800. return 0;
  801. }
  802. EXPORT_SYMBOL(cx23885_set_control);
  803. static void init_controls(struct cx23885_dev *dev)
  804. {
  805. struct v4l2_control ctrl;
  806. int i;
  807. for (i = 0; i < CX23885_CTLS; i++) {
  808. ctrl.id = cx23885_ctls[i].v.id;
  809. ctrl.value = cx23885_ctls[i].v.default_value;
  810. cx23885_set_control(dev, &ctrl);
  811. }
  812. }
  813. /* ------------------------------------------------------------------ */
  814. /* VIDEO IOCTLS */
  815. static int vidioc_g_fmt_cap(struct file *file, void *priv,
  816. struct v4l2_format *f)
  817. {
  818. struct cx23885_fh *fh = priv;
  819. f->fmt.pix.width = fh->width;
  820. f->fmt.pix.height = fh->height;
  821. f->fmt.pix.field = fh->vidq.field;
  822. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  823. f->fmt.pix.bytesperline =
  824. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  825. f->fmt.pix.sizeimage =
  826. f->fmt.pix.height * f->fmt.pix.bytesperline;
  827. return 0;
  828. }
  829. static int vidioc_try_fmt_cap(struct file *file, void *priv,
  830. struct v4l2_format *f)
  831. {
  832. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  833. struct cx23885_fmt *fmt;
  834. enum v4l2_field field;
  835. unsigned int maxw, maxh;
  836. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  837. if (NULL == fmt)
  838. return -EINVAL;
  839. field = f->fmt.pix.field;
  840. maxw = norm_maxw(dev->tvnorm);
  841. maxh = norm_maxh(dev->tvnorm);
  842. if (V4L2_FIELD_ANY == field) {
  843. field = (f->fmt.pix.height > maxh/2)
  844. ? V4L2_FIELD_INTERLACED
  845. : V4L2_FIELD_BOTTOM;
  846. }
  847. switch (field) {
  848. case V4L2_FIELD_TOP:
  849. case V4L2_FIELD_BOTTOM:
  850. maxh = maxh / 2;
  851. break;
  852. case V4L2_FIELD_INTERLACED:
  853. break;
  854. default:
  855. return -EINVAL;
  856. }
  857. f->fmt.pix.field = field;
  858. if (f->fmt.pix.height < 32)
  859. f->fmt.pix.height = 32;
  860. if (f->fmt.pix.height > maxh)
  861. f->fmt.pix.height = maxh;
  862. if (f->fmt.pix.width < 48)
  863. f->fmt.pix.width = 48;
  864. if (f->fmt.pix.width > maxw)
  865. f->fmt.pix.width = maxw;
  866. f->fmt.pix.width &= ~0x03;
  867. f->fmt.pix.bytesperline =
  868. (f->fmt.pix.width * fmt->depth) >> 3;
  869. f->fmt.pix.sizeimage =
  870. f->fmt.pix.height * f->fmt.pix.bytesperline;
  871. return 0;
  872. }
  873. static int vidioc_s_fmt_cap(struct file *file, void *priv,
  874. struct v4l2_format *f)
  875. {
  876. struct cx23885_fh *fh = priv;
  877. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  878. int err;
  879. dprintk(2, "%s()\n", __FUNCTION__);
  880. err = vidioc_try_fmt_cap(file, priv, f);
  881. if (0 != err)
  882. return err;
  883. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  884. fh->width = f->fmt.pix.width;
  885. fh->height = f->fmt.pix.height;
  886. fh->vidq.field = f->fmt.pix.field;
  887. dprintk(2, "%s() width=%d height=%d field=%d\n", __FUNCTION__,
  888. fh->width, fh->height, fh->vidq.field);
  889. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
  890. return 0;
  891. }
  892. static int vidioc_querycap(struct file *file, void *priv,
  893. struct v4l2_capability *cap)
  894. {
  895. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  896. strcpy(cap->driver, "cx23885");
  897. strlcpy(cap->card, cx23885_boards[dev->board].name,
  898. sizeof(cap->card));
  899. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  900. cap->version = CX23885_VERSION_CODE;
  901. cap->capabilities =
  902. V4L2_CAP_VIDEO_CAPTURE |
  903. V4L2_CAP_READWRITE |
  904. V4L2_CAP_STREAMING |
  905. V4L2_CAP_VBI_CAPTURE;
  906. if (UNSET != dev->tuner_type)
  907. cap->capabilities |= V4L2_CAP_TUNER;
  908. return 0;
  909. }
  910. static int vidioc_enum_fmt_cap(struct file *file, void *priv,
  911. struct v4l2_fmtdesc *f)
  912. {
  913. if (unlikely(f->index >= ARRAY_SIZE(formats)))
  914. return -EINVAL;
  915. strlcpy(f->description, formats[f->index].name,
  916. sizeof(f->description));
  917. f->pixelformat = formats[f->index].fourcc;
  918. return 0;
  919. }
  920. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  921. static int vidiocgmbuf(struct file *file, void *priv,
  922. struct video_mbuf *mbuf)
  923. {
  924. struct cx23885_fh *fh = priv;
  925. struct videobuf_queue *q;
  926. struct v4l2_requestbuffers req;
  927. unsigned int i;
  928. int err;
  929. q = get_queue(fh);
  930. memset(&req, 0, sizeof(req));
  931. req.type = q->type;
  932. req.count = 8;
  933. req.memory = V4L2_MEMORY_MMAP;
  934. err = videobuf_reqbufs(q, &req);
  935. if (err < 0)
  936. return err;
  937. mbuf->frames = req.count;
  938. mbuf->size = 0;
  939. for (i = 0; i < mbuf->frames; i++) {
  940. mbuf->offsets[i] = q->bufs[i]->boff;
  941. mbuf->size += q->bufs[i]->bsize;
  942. }
  943. return 0;
  944. }
  945. #endif
  946. static int vidioc_reqbufs(struct file *file, void *priv,
  947. struct v4l2_requestbuffers *p)
  948. {
  949. struct cx23885_fh *fh = priv;
  950. return (videobuf_reqbufs(get_queue(fh), p));
  951. }
  952. static int vidioc_querybuf(struct file *file, void *priv,
  953. struct v4l2_buffer *p)
  954. {
  955. struct cx23885_fh *fh = priv;
  956. return (videobuf_querybuf(get_queue(fh), p));
  957. }
  958. static int vidioc_qbuf(struct file *file, void *priv,
  959. struct v4l2_buffer *p)
  960. {
  961. struct cx23885_fh *fh = priv;
  962. return (videobuf_qbuf(get_queue(fh), p));
  963. }
  964. static int vidioc_dqbuf(struct file *file, void *priv,
  965. struct v4l2_buffer *p)
  966. {
  967. struct cx23885_fh *fh = priv;
  968. return (videobuf_dqbuf(get_queue(fh), p,
  969. file->f_flags & O_NONBLOCK));
  970. }
  971. static int vidioc_streamon(struct file *file, void *priv,
  972. enum v4l2_buf_type i)
  973. {
  974. struct cx23885_fh *fh = priv;
  975. struct cx23885_dev *dev = fh->dev;
  976. dprintk(1, "%s()\n", __FUNCTION__);
  977. if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
  978. return -EINVAL;
  979. if (unlikely(i != fh->type))
  980. return -EINVAL;
  981. if (unlikely(!res_get(dev, fh, get_resource(fh))))
  982. return -EBUSY;
  983. return videobuf_streamon(get_queue(fh));
  984. }
  985. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  986. {
  987. struct cx23885_fh *fh = priv;
  988. struct cx23885_dev *dev = fh->dev;
  989. int err, res;
  990. dprintk(1, "%s()\n", __FUNCTION__);
  991. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  992. return -EINVAL;
  993. if (i != fh->type)
  994. return -EINVAL;
  995. res = get_resource(fh);
  996. err = videobuf_streamoff(get_queue(fh));
  997. if (err < 0)
  998. return err;
  999. res_free(dev, fh, res);
  1000. return 0;
  1001. }
  1002. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
  1003. {
  1004. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1005. dprintk(1, "%s()\n", __FUNCTION__);
  1006. mutex_lock(&dev->lock);
  1007. cx23885_set_tvnorm(dev, *tvnorms);
  1008. mutex_unlock(&dev->lock);
  1009. return 0;
  1010. }
  1011. int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
  1012. {
  1013. static const char *iname[] = {
  1014. [CX23885_VMUX_COMPOSITE1] = "Composite1",
  1015. [CX23885_VMUX_COMPOSITE2] = "Composite2",
  1016. [CX23885_VMUX_COMPOSITE3] = "Composite3",
  1017. [CX23885_VMUX_COMPOSITE4] = "Composite4",
  1018. [CX23885_VMUX_SVIDEO] = "S-Video",
  1019. [CX23885_VMUX_TELEVISION] = "Television",
  1020. [CX23885_VMUX_CABLE] = "Cable TV",
  1021. [CX23885_VMUX_DVB] = "DVB",
  1022. [CX23885_VMUX_DEBUG] = "for debug only",
  1023. };
  1024. unsigned int n;
  1025. dprintk(1, "%s()\n", __FUNCTION__);
  1026. n = i->index;
  1027. if (n >= 4)
  1028. return -EINVAL;
  1029. if (0 == INPUT(n)->type)
  1030. return -EINVAL;
  1031. memset(i, 0, sizeof(*i));
  1032. i->index = n;
  1033. i->type = V4L2_INPUT_TYPE_CAMERA;
  1034. strcpy(i->name, iname[INPUT(n)->type]);
  1035. if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
  1036. (CX23885_VMUX_CABLE == INPUT(n)->type))
  1037. i->type = V4L2_INPUT_TYPE_TUNER;
  1038. i->std = CX23885_NORMS;
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL(cx23885_enum_input);
  1042. static int vidioc_enum_input(struct file *file, void *priv,
  1043. struct v4l2_input *i)
  1044. {
  1045. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1046. dprintk(1, "%s()\n", __FUNCTION__);
  1047. return cx23885_enum_input(dev, i);
  1048. }
  1049. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1050. {
  1051. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1052. *i = dev->input;
  1053. dprintk(1, "%s() returns %d\n", __FUNCTION__, *i);
  1054. return 0;
  1055. }
  1056. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1057. {
  1058. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1059. dprintk(1, "%s(%d)\n", __FUNCTION__, i);
  1060. if (i >= 4) {
  1061. dprintk(1, "%s() -EINVAL\n", __FUNCTION__);
  1062. return -EINVAL;
  1063. }
  1064. mutex_lock(&dev->lock);
  1065. cx23885_video_mux(dev, i);
  1066. mutex_unlock(&dev->lock);
  1067. return 0;
  1068. }
  1069. static int vidioc_queryctrl(struct file *file, void *priv,
  1070. struct v4l2_queryctrl *qctrl)
  1071. {
  1072. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  1073. if (unlikely(qctrl->id == 0))
  1074. return -EINVAL;
  1075. return cx23885_ctrl_query(qctrl);
  1076. }
  1077. static int vidioc_g_ctrl(struct file *file, void *priv,
  1078. struct v4l2_control *ctl)
  1079. {
  1080. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1081. return cx23885_get_control(dev, ctl);
  1082. }
  1083. static int vidioc_s_ctrl(struct file *file, void *priv,
  1084. struct v4l2_control *ctl)
  1085. {
  1086. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1087. return cx23885_set_control(dev, ctl);
  1088. }
  1089. static int vidioc_g_tuner(struct file *file, void *priv,
  1090. struct v4l2_tuner *t)
  1091. {
  1092. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1093. if (unlikely(UNSET == dev->tuner_type))
  1094. return -EINVAL;
  1095. if (0 != t->index)
  1096. return -EINVAL;
  1097. strcpy(t->name, "Television");
  1098. t->type = V4L2_TUNER_ANALOG_TV;
  1099. t->capability = V4L2_TUNER_CAP_NORM;
  1100. t->rangehigh = 0xffffffffUL;
  1101. t->signal = 0xffff ; /* LOCKED */
  1102. return 0;
  1103. }
  1104. static int vidioc_s_tuner(struct file *file, void *priv,
  1105. struct v4l2_tuner *t)
  1106. {
  1107. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1108. if (UNSET == dev->tuner_type)
  1109. return -EINVAL;
  1110. if (0 != t->index)
  1111. return -EINVAL;
  1112. return 0;
  1113. }
  1114. static int vidioc_g_frequency(struct file *file, void *priv,
  1115. struct v4l2_frequency *f)
  1116. {
  1117. struct cx23885_fh *fh = priv;
  1118. struct cx23885_dev *dev = fh->dev;
  1119. if (unlikely(UNSET == dev->tuner_type))
  1120. return -EINVAL;
  1121. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1122. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1123. f->frequency = dev->freq;
  1124. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
  1125. return 0;
  1126. }
  1127. int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
  1128. {
  1129. if (unlikely(UNSET == dev->tuner_type))
  1130. return -EINVAL;
  1131. if (unlikely(f->tuner != 0))
  1132. return -EINVAL;
  1133. mutex_lock(&dev->lock);
  1134. dev->freq = f->frequency;
  1135. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
  1136. /* When changing channels it is required to reset TVAUDIO */
  1137. msleep(10);
  1138. mutex_unlock(&dev->lock);
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL(cx23885_set_freq);
  1142. static int vidioc_s_frequency(struct file *file, void *priv,
  1143. struct v4l2_frequency *f)
  1144. {
  1145. struct cx23885_fh *fh = priv;
  1146. struct cx23885_dev *dev = fh->dev;
  1147. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1148. return -EINVAL;
  1149. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1150. return -EINVAL;
  1151. return
  1152. cx23885_set_freq(dev, f);
  1153. }
  1154. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1155. static int vidioc_g_register(struct file *file, void *fh,
  1156. struct v4l2_register *reg)
  1157. {
  1158. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1159. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
  1160. return 0;
  1161. }
  1162. static int vidioc_s_register(struct file *file, void *fh,
  1163. struct v4l2_register *reg)
  1164. {
  1165. struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
  1166. cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
  1167. return 0;
  1168. }
  1169. #endif
  1170. /* ----------------------------------------------------------- */
  1171. /* RADIO ESPECIFIC IOCTLS */
  1172. /* ----------------------------------------------------------- */
  1173. static int radio_querycap(struct file *file, void *priv,
  1174. struct v4l2_capability *cap)
  1175. {
  1176. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1177. strcpy(cap->driver, "cx23885");
  1178. strlcpy(cap->card, cx23885_boards[dev->board].name,
  1179. sizeof(cap->card));
  1180. sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
  1181. cap->version = CX23885_VERSION_CODE;
  1182. cap->capabilities = V4L2_CAP_TUNER;
  1183. return 0;
  1184. }
  1185. static int radio_g_tuner(struct file *file, void *priv,
  1186. struct v4l2_tuner *t)
  1187. {
  1188. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1189. if (unlikely(t->index > 0))
  1190. return -EINVAL;
  1191. strcpy(t->name, "Radio");
  1192. t->type = V4L2_TUNER_RADIO;
  1193. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
  1194. return 0;
  1195. }
  1196. static int radio_enum_input(struct file *file, void *priv,
  1197. struct v4l2_input *i)
  1198. {
  1199. if (i->index != 0)
  1200. return -EINVAL;
  1201. strcpy(i->name, "Radio");
  1202. i->type = V4L2_INPUT_TYPE_TUNER;
  1203. return 0;
  1204. }
  1205. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1206. {
  1207. if (unlikely(a->index))
  1208. return -EINVAL;
  1209. memset(a, 0, sizeof(*a));
  1210. strcpy(a->name, "Radio");
  1211. return 0;
  1212. }
  1213. /* FIXME: Should add a standard for radio */
  1214. static int radio_s_tuner(struct file *file, void *priv,
  1215. struct v4l2_tuner *t)
  1216. {
  1217. struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
  1218. if (0 != t->index)
  1219. return -EINVAL;
  1220. cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
  1221. return 0;
  1222. }
  1223. static int radio_s_audio(struct file *file, void *fh,
  1224. struct v4l2_audio *a)
  1225. {
  1226. return 0;
  1227. }
  1228. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1229. {
  1230. return 0;
  1231. }
  1232. static int radio_queryctrl(struct file *file, void *priv,
  1233. struct v4l2_queryctrl *c)
  1234. {
  1235. int i;
  1236. if (c->id < V4L2_CID_BASE ||
  1237. c->id >= V4L2_CID_LASTP1)
  1238. return -EINVAL;
  1239. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1240. for (i = 0; i < CX23885_CTLS; i++)
  1241. if (cx23885_ctls[i].v.id == c->id)
  1242. break;
  1243. *c = cx23885_ctls[i].v;
  1244. } else
  1245. *c = no_ctl;
  1246. return 0;
  1247. }
  1248. /* ----------------------------------------------------------- */
  1249. static void cx23885_vid_timeout(unsigned long data)
  1250. {
  1251. struct cx23885_dev *dev = (struct cx23885_dev *)data;
  1252. struct cx23885_dmaqueue *q = &dev->vidq;
  1253. struct cx23885_buffer *buf;
  1254. unsigned long flags;
  1255. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1256. cx_clear(VID_A_DMA_CTL, 0x11);
  1257. spin_lock_irqsave(&dev->slock, flags);
  1258. while (!list_empty(&q->active)) {
  1259. buf = list_entry(q->active.next,
  1260. struct cx23885_buffer, vb.queue);
  1261. list_del(&buf->vb.queue);
  1262. buf->vb.state = VIDEOBUF_ERROR;
  1263. wake_up(&buf->vb.done);
  1264. printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
  1265. dev->name, buf, buf->vb.i,
  1266. (unsigned long)buf->risc.dma);
  1267. }
  1268. cx23885_restart_video_queue(dev, q);
  1269. spin_unlock_irqrestore(&dev->slock, flags);
  1270. }
  1271. int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
  1272. {
  1273. u32 mask, count;
  1274. int handled = 0;
  1275. mask = cx_read(VID_A_INT_MSK);
  1276. if (0 == (status & mask))
  1277. return handled;
  1278. cx_write(VID_A_INT_STAT, status);
  1279. dprintk(2, "%s() status = 0x%08x\n", __FUNCTION__, status);
  1280. /* risc op code error */
  1281. if (status & (1 << 16)) {
  1282. printk(KERN_WARNING "%s/0: video risc op code error\n",
  1283. dev->name);
  1284. cx_clear(VID_A_DMA_CTL, 0x11);
  1285. cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
  1286. }
  1287. /* risc1 y */
  1288. if (status & 0x01) {
  1289. spin_lock(&dev->slock);
  1290. count = cx_read(VID_A_GPCNT);
  1291. cx23885_video_wakeup(dev, &dev->vidq, count);
  1292. spin_unlock(&dev->slock);
  1293. handled++;
  1294. }
  1295. /* risc2 y */
  1296. if (status & 0x10) {
  1297. dprintk(2, "stopper video\n");
  1298. spin_lock(&dev->slock);
  1299. cx23885_restart_video_queue(dev, &dev->vidq);
  1300. spin_unlock(&dev->slock);
  1301. handled++;
  1302. }
  1303. return handled;
  1304. }
  1305. /* ----------------------------------------------------------- */
  1306. /* exported stuff */
  1307. static const struct file_operations video_fops = {
  1308. .owner = THIS_MODULE,
  1309. .open = video_open,
  1310. .release = video_release,
  1311. .read = video_read,
  1312. .poll = video_poll,
  1313. .mmap = video_mmap,
  1314. .ioctl = video_ioctl2,
  1315. .compat_ioctl = v4l_compat_ioctl32,
  1316. .llseek = no_llseek,
  1317. };
  1318. static struct video_device cx23885_vbi_template;
  1319. static struct video_device cx23885_video_template = {
  1320. .name = "cx23885-video",
  1321. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1322. .fops = &video_fops,
  1323. .minor = -1,
  1324. .vidioc_querycap = vidioc_querycap,
  1325. .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
  1326. .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
  1327. .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
  1328. .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
  1329. .vidioc_g_fmt_vbi = cx23885_vbi_fmt,
  1330. .vidioc_try_fmt_vbi = cx23885_vbi_fmt,
  1331. .vidioc_s_fmt_vbi = cx23885_vbi_fmt,
  1332. .vidioc_reqbufs = vidioc_reqbufs,
  1333. .vidioc_querybuf = vidioc_querybuf,
  1334. .vidioc_qbuf = vidioc_qbuf,
  1335. .vidioc_dqbuf = vidioc_dqbuf,
  1336. .vidioc_s_std = vidioc_s_std,
  1337. .vidioc_enum_input = vidioc_enum_input,
  1338. .vidioc_g_input = vidioc_g_input,
  1339. .vidioc_s_input = vidioc_s_input,
  1340. .vidioc_queryctrl = vidioc_queryctrl,
  1341. .vidioc_g_ctrl = vidioc_g_ctrl,
  1342. .vidioc_s_ctrl = vidioc_s_ctrl,
  1343. .vidioc_streamon = vidioc_streamon,
  1344. .vidioc_streamoff = vidioc_streamoff,
  1345. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1346. .vidiocgmbuf = vidiocgmbuf,
  1347. #endif
  1348. .vidioc_g_tuner = vidioc_g_tuner,
  1349. .vidioc_s_tuner = vidioc_s_tuner,
  1350. .vidioc_g_frequency = vidioc_g_frequency,
  1351. .vidioc_s_frequency = vidioc_s_frequency,
  1352. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1353. .vidioc_g_register = vidioc_g_register,
  1354. .vidioc_s_register = vidioc_s_register,
  1355. #endif
  1356. .tvnorms = CX23885_NORMS,
  1357. .current_norm = V4L2_STD_NTSC_M,
  1358. };
  1359. static const struct file_operations radio_fops = {
  1360. .owner = THIS_MODULE,
  1361. .open = video_open,
  1362. .release = video_release,
  1363. .ioctl = video_ioctl2,
  1364. .compat_ioctl = v4l_compat_ioctl32,
  1365. .llseek = no_llseek,
  1366. };
  1367. void cx23885_video_unregister(struct cx23885_dev *dev)
  1368. {
  1369. dprintk(1, "%s()\n", __FUNCTION__);
  1370. cx_clear(PCI_INT_MSK, 1);
  1371. if (dev->video_dev) {
  1372. if (-1 != dev->video_dev->minor)
  1373. video_unregister_device(dev->video_dev);
  1374. else
  1375. video_device_release(dev->video_dev);
  1376. dev->video_dev = NULL;
  1377. btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
  1378. }
  1379. }
  1380. int cx23885_video_register(struct cx23885_dev *dev)
  1381. {
  1382. int err;
  1383. dprintk(1, "%s()\n", __FUNCTION__);
  1384. spin_lock_init(&dev->slock);
  1385. /* Initialize VBI template */
  1386. memcpy(&cx23885_vbi_template, &cx23885_video_template,
  1387. sizeof(cx23885_vbi_template));
  1388. strcpy(cx23885_vbi_template.name, "cx23885-vbi");
  1389. cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
  1390. dev->tvnorm = cx23885_video_template.current_norm;
  1391. /* init video dma queues */
  1392. INIT_LIST_HEAD(&dev->vidq.active);
  1393. INIT_LIST_HEAD(&dev->vidq.queued);
  1394. dev->vidq.timeout.function = cx23885_vid_timeout;
  1395. dev->vidq.timeout.data = (unsigned long)dev;
  1396. init_timer(&dev->vidq.timeout);
  1397. cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
  1398. VID_A_DMA_CTL, 0x11, 0x00);
  1399. cx_set(PCI_INT_MSK, 1);
  1400. /* register v4l devices */
  1401. dev->video_dev = cx23885_vdev_init(dev, dev->pci,
  1402. &cx23885_video_template, "video");
  1403. err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
  1404. video_nr[dev->nr]);
  1405. if (err < 0) {
  1406. printk(KERN_INFO "%s: can't register video device\n",
  1407. dev->name);
  1408. goto fail_unreg;
  1409. }
  1410. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1411. dev->name, dev->video_dev->minor & 0x1f);
  1412. /* initial device configuration */
  1413. mutex_lock(&dev->lock);
  1414. cx23885_set_tvnorm(dev, dev->tvnorm);
  1415. init_controls(dev);
  1416. cx23885_video_mux(dev, 0);
  1417. mutex_unlock(&dev->lock);
  1418. /* FIXME start tvaudio thread */
  1419. return 0;
  1420. fail_unreg:
  1421. cx23885_video_unregister(dev);
  1422. return err;
  1423. }