pvrusb2-v4l2.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*
  2. *
  3. * $Id$
  4. *
  5. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  6. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/version.h>
  24. #include "pvrusb2-context.h"
  25. #include "pvrusb2-hdw.h"
  26. #include "pvrusb2.h"
  27. #include "pvrusb2-debug.h"
  28. #include "pvrusb2-v4l2.h"
  29. #include "pvrusb2-ioread.h"
  30. #include <linux/videodev2.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/v4l2-common.h>
  33. struct pvr2_v4l2_dev;
  34. struct pvr2_v4l2_fh;
  35. struct pvr2_v4l2;
  36. struct pvr2_v4l2_dev {
  37. struct video_device devbase; /* MUST be first! */
  38. struct pvr2_v4l2 *v4lp;
  39. struct pvr2_context_stream *stream;
  40. /* Information about this device: */
  41. enum pvr2_config config; /* Expected stream format */
  42. int v4l_type; /* V4L defined type for this device node */
  43. enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
  44. };
  45. struct pvr2_v4l2_fh {
  46. struct pvr2_channel channel;
  47. struct pvr2_v4l2_dev *dev_info;
  48. enum v4l2_priority prio;
  49. struct pvr2_ioread *rhp;
  50. struct file *file;
  51. struct pvr2_v4l2 *vhead;
  52. struct pvr2_v4l2_fh *vnext;
  53. struct pvr2_v4l2_fh *vprev;
  54. wait_queue_head_t wait_data;
  55. int fw_mode_flag;
  56. int prev_input_val;
  57. };
  58. struct pvr2_v4l2 {
  59. struct pvr2_channel channel;
  60. struct pvr2_v4l2_fh *vfirst;
  61. struct pvr2_v4l2_fh *vlast;
  62. struct v4l2_prio_state prio;
  63. /* streams - Note that these must be separately, individually,
  64. * allocated pointers. This is because the v4l core is going to
  65. * manage their deletion - separately, individually... */
  66. struct pvr2_v4l2_dev *dev_video;
  67. struct pvr2_v4l2_dev *dev_radio;
  68. };
  69. static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  70. module_param_array(video_nr, int, NULL, 0444);
  71. MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
  72. static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  73. module_param_array(radio_nr, int, NULL, 0444);
  74. MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
  75. static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  76. module_param_array(vbi_nr, int, NULL, 0444);
  77. MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
  78. static struct v4l2_capability pvr_capability ={
  79. .driver = "pvrusb2",
  80. .card = "Hauppauge WinTV pvr-usb2",
  81. .bus_info = "usb",
  82. .version = KERNEL_VERSION(0,8,0),
  83. .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
  84. V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
  85. V4L2_CAP_READWRITE),
  86. .reserved = {0,0,0,0}
  87. };
  88. static struct v4l2_fmtdesc pvr_fmtdesc [] = {
  89. {
  90. .index = 0,
  91. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  92. .flags = V4L2_FMT_FLAG_COMPRESSED,
  93. .description = "MPEG1/2",
  94. // This should really be V4L2_PIX_FMT_MPEG, but xawtv
  95. // breaks when I do that.
  96. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  97. .reserved = { 0, 0, 0, 0 }
  98. }
  99. };
  100. #define PVR_FORMAT_PIX 0
  101. #define PVR_FORMAT_VBI 1
  102. static struct v4l2_format pvr_format [] = {
  103. [PVR_FORMAT_PIX] = {
  104. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  105. .fmt = {
  106. .pix = {
  107. .width = 720,
  108. .height = 576,
  109. // This should really be V4L2_PIX_FMT_MPEG,
  110. // but xawtv breaks when I do that.
  111. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  112. .field = V4L2_FIELD_INTERLACED,
  113. .bytesperline = 0, // doesn't make sense
  114. // here
  115. //FIXME : Don't know what to put here...
  116. .sizeimage = (32*1024),
  117. .colorspace = 0, // doesn't make sense here
  118. .priv = 0
  119. }
  120. }
  121. },
  122. [PVR_FORMAT_VBI] = {
  123. .type = V4L2_BUF_TYPE_VBI_CAPTURE,
  124. .fmt = {
  125. .vbi = {
  126. .sampling_rate = 27000000,
  127. .offset = 248,
  128. .samples_per_line = 1443,
  129. .sample_format = V4L2_PIX_FMT_GREY,
  130. .start = { 0, 0 },
  131. .count = { 0, 0 },
  132. .flags = 0,
  133. .reserved = { 0, 0 }
  134. }
  135. }
  136. }
  137. };
  138. static const char *get_v4l_name(int v4l_type)
  139. {
  140. switch (v4l_type) {
  141. case VFL_TYPE_GRABBER: return "video";
  142. case VFL_TYPE_RADIO: return "radio";
  143. case VFL_TYPE_VBI: return "vbi";
  144. default: return "?";
  145. }
  146. }
  147. /*
  148. * pvr_ioctl()
  149. *
  150. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  151. *
  152. */
  153. static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
  154. unsigned int cmd, void *arg)
  155. {
  156. struct pvr2_v4l2_fh *fh = file->private_data;
  157. struct pvr2_v4l2 *vp = fh->vhead;
  158. struct pvr2_v4l2_dev *dev_info = fh->dev_info;
  159. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  160. int ret = -EINVAL;
  161. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  162. v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
  163. }
  164. if (!pvr2_hdw_dev_ok(hdw)) {
  165. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  166. "ioctl failed - bad or no context");
  167. return -EFAULT;
  168. }
  169. /* check priority */
  170. switch (cmd) {
  171. case VIDIOC_S_CTRL:
  172. case VIDIOC_S_STD:
  173. case VIDIOC_S_INPUT:
  174. case VIDIOC_S_TUNER:
  175. case VIDIOC_S_FREQUENCY:
  176. ret = v4l2_prio_check(&vp->prio, &fh->prio);
  177. if (ret)
  178. return ret;
  179. }
  180. switch (cmd) {
  181. case VIDIOC_QUERYCAP:
  182. {
  183. struct v4l2_capability *cap = arg;
  184. memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
  185. ret = 0;
  186. break;
  187. }
  188. case VIDIOC_G_PRIORITY:
  189. {
  190. enum v4l2_priority *p = arg;
  191. *p = v4l2_prio_max(&vp->prio);
  192. ret = 0;
  193. break;
  194. }
  195. case VIDIOC_S_PRIORITY:
  196. {
  197. enum v4l2_priority *prio = arg;
  198. ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
  199. break;
  200. }
  201. case VIDIOC_ENUMSTD:
  202. {
  203. struct v4l2_standard *vs = (struct v4l2_standard *)arg;
  204. int idx = vs->index;
  205. ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
  206. break;
  207. }
  208. case VIDIOC_G_STD:
  209. {
  210. int val = 0;
  211. ret = pvr2_ctrl_get_value(
  212. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
  213. *(v4l2_std_id *)arg = val;
  214. break;
  215. }
  216. case VIDIOC_S_STD:
  217. {
  218. ret = pvr2_ctrl_set_value(
  219. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
  220. *(v4l2_std_id *)arg);
  221. break;
  222. }
  223. case VIDIOC_ENUMINPUT:
  224. {
  225. struct pvr2_ctrl *cptr;
  226. struct v4l2_input *vi = (struct v4l2_input *)arg;
  227. struct v4l2_input tmp;
  228. unsigned int cnt;
  229. cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  230. memset(&tmp,0,sizeof(tmp));
  231. tmp.index = vi->index;
  232. ret = 0;
  233. switch (vi->index) {
  234. case PVR2_CVAL_INPUT_TV:
  235. case PVR2_CVAL_INPUT_RADIO:
  236. tmp.type = V4L2_INPUT_TYPE_TUNER;
  237. break;
  238. case PVR2_CVAL_INPUT_SVIDEO:
  239. case PVR2_CVAL_INPUT_COMPOSITE:
  240. tmp.type = V4L2_INPUT_TYPE_CAMERA;
  241. break;
  242. default:
  243. ret = -EINVAL;
  244. break;
  245. }
  246. if (ret < 0) break;
  247. cnt = 0;
  248. pvr2_ctrl_get_valname(cptr,vi->index,
  249. tmp.name,sizeof(tmp.name)-1,&cnt);
  250. tmp.name[cnt] = 0;
  251. /* Don't bother with audioset, since this driver currently
  252. always switches the audio whenever the video is
  253. switched. */
  254. /* Handling std is a tougher problem. It doesn't make
  255. sense in cases where a device might be multi-standard.
  256. We could just copy out the current value for the
  257. standard, but it can change over time. For now just
  258. leave it zero. */
  259. memcpy(vi, &tmp, sizeof(tmp));
  260. ret = 0;
  261. break;
  262. }
  263. case VIDIOC_G_INPUT:
  264. {
  265. struct pvr2_ctrl *cptr;
  266. struct v4l2_input *vi = (struct v4l2_input *)arg;
  267. int val;
  268. cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  269. val = 0;
  270. ret = pvr2_ctrl_get_value(cptr,&val);
  271. vi->index = val;
  272. break;
  273. }
  274. case VIDIOC_S_INPUT:
  275. {
  276. struct v4l2_input *vi = (struct v4l2_input *)arg;
  277. ret = pvr2_ctrl_set_value(
  278. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
  279. vi->index);
  280. break;
  281. }
  282. case VIDIOC_ENUMAUDIO:
  283. {
  284. ret = -EINVAL;
  285. break;
  286. }
  287. case VIDIOC_G_AUDIO:
  288. {
  289. ret = -EINVAL;
  290. break;
  291. }
  292. case VIDIOC_S_AUDIO:
  293. {
  294. ret = -EINVAL;
  295. break;
  296. }
  297. case VIDIOC_G_TUNER:
  298. {
  299. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  300. pvr2_hdw_execute_tuner_poll(hdw);
  301. ret = pvr2_hdw_get_tuner_status(hdw,vt);
  302. break;
  303. }
  304. case VIDIOC_S_TUNER:
  305. {
  306. struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
  307. if (vt->index != 0)
  308. break;
  309. ret = pvr2_ctrl_set_value(
  310. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
  311. vt->audmode);
  312. }
  313. case VIDIOC_S_FREQUENCY:
  314. {
  315. const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
  316. unsigned long fv;
  317. struct v4l2_tuner vt;
  318. int cur_input;
  319. struct pvr2_ctrl *ctrlp;
  320. ret = pvr2_hdw_get_tuner_status(hdw,&vt);
  321. if (ret != 0) break;
  322. ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  323. ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
  324. if (ret != 0) break;
  325. if (vf->type == V4L2_TUNER_RADIO) {
  326. if (cur_input != PVR2_CVAL_INPUT_RADIO) {
  327. pvr2_ctrl_set_value(ctrlp,
  328. PVR2_CVAL_INPUT_RADIO);
  329. }
  330. } else {
  331. if (cur_input == PVR2_CVAL_INPUT_RADIO) {
  332. pvr2_ctrl_set_value(ctrlp,
  333. PVR2_CVAL_INPUT_TV);
  334. }
  335. }
  336. fv = vf->frequency;
  337. if (vt.capability & V4L2_TUNER_CAP_LOW) {
  338. fv = (fv * 125) / 2;
  339. } else {
  340. fv = fv * 62500;
  341. }
  342. ret = pvr2_ctrl_set_value(
  343. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
  344. break;
  345. }
  346. case VIDIOC_G_FREQUENCY:
  347. {
  348. struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
  349. int val = 0;
  350. int cur_input;
  351. struct v4l2_tuner vt;
  352. ret = pvr2_hdw_get_tuner_status(hdw,&vt);
  353. if (ret != 0) break;
  354. ret = pvr2_ctrl_get_value(
  355. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
  356. &val);
  357. if (ret != 0) break;
  358. pvr2_ctrl_get_value(
  359. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
  360. &cur_input);
  361. if (cur_input == PVR2_CVAL_INPUT_RADIO) {
  362. vf->type = V4L2_TUNER_RADIO;
  363. } else {
  364. vf->type = V4L2_TUNER_ANALOG_TV;
  365. }
  366. if (vt.capability & V4L2_TUNER_CAP_LOW) {
  367. val = (val * 2) / 125;
  368. } else {
  369. val /= 62500;
  370. }
  371. vf->frequency = val;
  372. break;
  373. }
  374. case VIDIOC_ENUM_FMT:
  375. {
  376. struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
  377. /* Only one format is supported : mpeg.*/
  378. if (fd->index != 0)
  379. break;
  380. memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
  381. ret = 0;
  382. break;
  383. }
  384. case VIDIOC_G_FMT:
  385. {
  386. struct v4l2_format *vf = (struct v4l2_format *)arg;
  387. int val;
  388. switch(vf->type) {
  389. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  390. memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
  391. sizeof(struct v4l2_format));
  392. val = 0;
  393. pvr2_ctrl_get_value(
  394. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
  395. &val);
  396. vf->fmt.pix.width = val;
  397. val = 0;
  398. pvr2_ctrl_get_value(
  399. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
  400. &val);
  401. vf->fmt.pix.height = val;
  402. ret = 0;
  403. break;
  404. case V4L2_BUF_TYPE_VBI_CAPTURE:
  405. // ????? Still need to figure out to do VBI correctly
  406. ret = -EINVAL;
  407. break;
  408. default:
  409. ret = -EINVAL;
  410. break;
  411. }
  412. break;
  413. }
  414. case VIDIOC_TRY_FMT:
  415. case VIDIOC_S_FMT:
  416. {
  417. struct v4l2_format *vf = (struct v4l2_format *)arg;
  418. ret = 0;
  419. switch(vf->type) {
  420. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  421. int lmin,lmax;
  422. struct pvr2_ctrl *hcp,*vcp;
  423. int h = vf->fmt.pix.height;
  424. int w = vf->fmt.pix.width;
  425. hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
  426. vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
  427. lmin = pvr2_ctrl_get_min(hcp);
  428. lmax = pvr2_ctrl_get_max(hcp);
  429. if (w < lmin) {
  430. w = lmin;
  431. } else if (w > lmax) {
  432. w = lmax;
  433. }
  434. lmin = pvr2_ctrl_get_min(vcp);
  435. lmax = pvr2_ctrl_get_max(vcp);
  436. if (h < lmin) {
  437. h = lmin;
  438. } else if (h > lmax) {
  439. h = lmax;
  440. }
  441. memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
  442. sizeof(struct v4l2_format));
  443. vf->fmt.pix.width = w;
  444. vf->fmt.pix.height = h;
  445. if (cmd == VIDIOC_S_FMT) {
  446. pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
  447. pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
  448. }
  449. } break;
  450. case V4L2_BUF_TYPE_VBI_CAPTURE:
  451. // ????? Still need to figure out to do VBI correctly
  452. ret = -EINVAL;
  453. break;
  454. default:
  455. ret = -EINVAL;
  456. break;
  457. }
  458. break;
  459. }
  460. case VIDIOC_STREAMON:
  461. {
  462. if (!fh->dev_info->stream) {
  463. /* No stream defined for this node. This means
  464. that we're not currently allowed to stream from
  465. this node. */
  466. ret = -EPERM;
  467. break;
  468. }
  469. ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
  470. if (ret < 0) return ret;
  471. ret = pvr2_hdw_set_streaming(hdw,!0);
  472. break;
  473. }
  474. case VIDIOC_STREAMOFF:
  475. {
  476. if (!fh->dev_info->stream) {
  477. /* No stream defined for this node. This means
  478. that we're not currently allowed to stream from
  479. this node. */
  480. ret = -EPERM;
  481. break;
  482. }
  483. ret = pvr2_hdw_set_streaming(hdw,0);
  484. break;
  485. }
  486. case VIDIOC_QUERYCTRL:
  487. {
  488. struct pvr2_ctrl *cptr;
  489. struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
  490. ret = 0;
  491. if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  492. cptr = pvr2_hdw_get_ctrl_nextv4l(
  493. hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
  494. if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
  495. } else {
  496. cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
  497. }
  498. if (!cptr) {
  499. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  500. "QUERYCTRL id=0x%x not implemented here",
  501. vc->id);
  502. ret = -EINVAL;
  503. break;
  504. }
  505. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  506. "QUERYCTRL id=0x%x mapping name=%s (%s)",
  507. vc->id,pvr2_ctrl_get_name(cptr),
  508. pvr2_ctrl_get_desc(cptr));
  509. strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
  510. vc->flags = pvr2_ctrl_get_v4lflags(cptr);
  511. vc->default_value = pvr2_ctrl_get_def(cptr);
  512. switch (pvr2_ctrl_get_type(cptr)) {
  513. case pvr2_ctl_enum:
  514. vc->type = V4L2_CTRL_TYPE_MENU;
  515. vc->minimum = 0;
  516. vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
  517. vc->step = 1;
  518. break;
  519. case pvr2_ctl_bool:
  520. vc->type = V4L2_CTRL_TYPE_BOOLEAN;
  521. vc->minimum = 0;
  522. vc->maximum = 1;
  523. vc->step = 1;
  524. break;
  525. case pvr2_ctl_int:
  526. vc->type = V4L2_CTRL_TYPE_INTEGER;
  527. vc->minimum = pvr2_ctrl_get_min(cptr);
  528. vc->maximum = pvr2_ctrl_get_max(cptr);
  529. vc->step = 1;
  530. break;
  531. default:
  532. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  533. "QUERYCTRL id=0x%x name=%s not mappable",
  534. vc->id,pvr2_ctrl_get_name(cptr));
  535. ret = -EINVAL;
  536. break;
  537. }
  538. break;
  539. }
  540. case VIDIOC_QUERYMENU:
  541. {
  542. struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
  543. unsigned int cnt = 0;
  544. ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
  545. vm->index,
  546. vm->name,sizeof(vm->name)-1,
  547. &cnt);
  548. vm->name[cnt] = 0;
  549. break;
  550. }
  551. case VIDIOC_G_CTRL:
  552. {
  553. struct v4l2_control *vc = (struct v4l2_control *)arg;
  554. int val = 0;
  555. ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
  556. &val);
  557. vc->value = val;
  558. break;
  559. }
  560. case VIDIOC_S_CTRL:
  561. {
  562. struct v4l2_control *vc = (struct v4l2_control *)arg;
  563. ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
  564. vc->value);
  565. break;
  566. }
  567. case VIDIOC_G_EXT_CTRLS:
  568. {
  569. struct v4l2_ext_controls *ctls =
  570. (struct v4l2_ext_controls *)arg;
  571. struct v4l2_ext_control *ctrl;
  572. unsigned int idx;
  573. int val;
  574. for (idx = 0; idx < ctls->count; idx++) {
  575. ctrl = ctls->controls + idx;
  576. ret = pvr2_ctrl_get_value(
  577. pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
  578. if (ret) {
  579. ctls->error_idx = idx;
  580. break;
  581. }
  582. /* Ensure that if read as a 64 bit value, the user
  583. will still get a hopefully sane value */
  584. ctrl->value64 = 0;
  585. ctrl->value = val;
  586. }
  587. break;
  588. }
  589. case VIDIOC_S_EXT_CTRLS:
  590. {
  591. struct v4l2_ext_controls *ctls =
  592. (struct v4l2_ext_controls *)arg;
  593. struct v4l2_ext_control *ctrl;
  594. unsigned int idx;
  595. for (idx = 0; idx < ctls->count; idx++) {
  596. ctrl = ctls->controls + idx;
  597. ret = pvr2_ctrl_set_value(
  598. pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
  599. ctrl->value);
  600. if (ret) {
  601. ctls->error_idx = idx;
  602. break;
  603. }
  604. }
  605. break;
  606. }
  607. case VIDIOC_TRY_EXT_CTRLS:
  608. {
  609. struct v4l2_ext_controls *ctls =
  610. (struct v4l2_ext_controls *)arg;
  611. struct v4l2_ext_control *ctrl;
  612. struct pvr2_ctrl *pctl;
  613. unsigned int idx;
  614. /* For the moment just validate that the requested control
  615. actually exists. */
  616. for (idx = 0; idx < ctls->count; idx++) {
  617. ctrl = ctls->controls + idx;
  618. pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
  619. if (!pctl) {
  620. ret = -EINVAL;
  621. ctls->error_idx = idx;
  622. break;
  623. }
  624. }
  625. break;
  626. }
  627. case VIDIOC_LOG_STATUS:
  628. {
  629. pvr2_hdw_trigger_module_log(hdw);
  630. ret = 0;
  631. break;
  632. }
  633. #ifdef CONFIG_VIDEO_ADV_DEBUG
  634. case VIDIOC_INT_G_REGISTER:
  635. case VIDIOC_INT_S_REGISTER:
  636. {
  637. u32 val;
  638. struct v4l2_register *req = (struct v4l2_register *)arg;
  639. if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
  640. ret = pvr2_hdw_register_access(
  641. hdw,req->i2c_id,req->reg,
  642. cmd == VIDIOC_INT_S_REGISTER,&val);
  643. if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
  644. break;
  645. }
  646. #endif
  647. default :
  648. ret = v4l_compat_translate_ioctl(inode,file,cmd,
  649. arg,pvr2_v4l2_do_ioctl);
  650. }
  651. pvr2_hdw_commit_ctl(hdw);
  652. if (ret < 0) {
  653. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  654. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  655. "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
  656. } else {
  657. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  658. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  659. "pvr2_v4l2_do_ioctl failure, ret=%d"
  660. " command was:",ret);
  661. v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
  662. cmd);
  663. }
  664. }
  665. } else {
  666. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  667. "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
  668. ret,ret);
  669. }
  670. return ret;
  671. }
  672. static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
  673. {
  674. int minor_id = dip->devbase.minor;
  675. struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
  676. enum pvr2_config cfg = dip->config;
  677. int v4l_type = dip->v4l_type;
  678. pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
  679. /* Paranoia */
  680. dip->v4lp = NULL;
  681. dip->stream = NULL;
  682. /* Actual deallocation happens later when all internal references
  683. are gone. */
  684. video_unregister_device(&dip->devbase);
  685. printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
  686. get_v4l_name(v4l_type),minor_id & 0x1f,
  687. pvr2_config_get_name(cfg));
  688. }
  689. static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
  690. {
  691. if (vp->dev_video) {
  692. pvr2_v4l2_dev_destroy(vp->dev_video);
  693. vp->dev_video = 0;
  694. }
  695. if (vp->dev_radio) {
  696. pvr2_v4l2_dev_destroy(vp->dev_radio);
  697. vp->dev_radio = 0;
  698. }
  699. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
  700. pvr2_channel_done(&vp->channel);
  701. kfree(vp);
  702. }
  703. static void pvr2_video_device_release(struct video_device *vdev)
  704. {
  705. struct pvr2_v4l2_dev *dev;
  706. dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
  707. kfree(dev);
  708. }
  709. static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
  710. {
  711. struct pvr2_v4l2 *vp;
  712. vp = container_of(chp,struct pvr2_v4l2,channel);
  713. if (!vp->channel.mc_head->disconnect_flag) return;
  714. if (vp->vfirst) return;
  715. pvr2_v4l2_destroy_no_lock(vp);
  716. }
  717. static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
  718. unsigned int cmd, unsigned long arg)
  719. {
  720. /* Temporary hack : use ivtv api until a v4l2 one is available. */
  721. #define IVTV_IOC_G_CODEC 0xFFEE7703
  722. #define IVTV_IOC_S_CODEC 0xFFEE7704
  723. if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
  724. return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
  725. }
  726. static int pvr2_v4l2_release(struct inode *inode, struct file *file)
  727. {
  728. struct pvr2_v4l2_fh *fhp = file->private_data;
  729. struct pvr2_v4l2 *vp = fhp->vhead;
  730. struct pvr2_context *mp = fhp->vhead->channel.mc_head;
  731. struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
  732. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
  733. if (fhp->rhp) {
  734. struct pvr2_stream *sp;
  735. pvr2_hdw_set_streaming(hdw,0);
  736. sp = pvr2_ioread_get_stream(fhp->rhp);
  737. if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
  738. pvr2_ioread_destroy(fhp->rhp);
  739. fhp->rhp = NULL;
  740. }
  741. v4l2_prio_close(&vp->prio, &fhp->prio);
  742. file->private_data = NULL;
  743. pvr2_context_enter(mp); do {
  744. /* Restore the previous input selection, if it makes sense
  745. to do so. */
  746. if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
  747. struct pvr2_ctrl *cp;
  748. int pval;
  749. cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  750. pvr2_ctrl_get_value(cp,&pval);
  751. /* Only restore if we're still selecting the radio */
  752. if (pval == PVR2_CVAL_INPUT_RADIO) {
  753. pvr2_ctrl_set_value(cp,fhp->prev_input_val);
  754. pvr2_hdw_commit_ctl(hdw);
  755. }
  756. }
  757. if (fhp->vnext) {
  758. fhp->vnext->vprev = fhp->vprev;
  759. } else {
  760. vp->vlast = fhp->vprev;
  761. }
  762. if (fhp->vprev) {
  763. fhp->vprev->vnext = fhp->vnext;
  764. } else {
  765. vp->vfirst = fhp->vnext;
  766. }
  767. fhp->vnext = NULL;
  768. fhp->vprev = NULL;
  769. fhp->vhead = NULL;
  770. pvr2_channel_done(&fhp->channel);
  771. pvr2_trace(PVR2_TRACE_STRUCT,
  772. "Destroying pvr_v4l2_fh id=%p",fhp);
  773. kfree(fhp);
  774. if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
  775. pvr2_v4l2_destroy_no_lock(vp);
  776. }
  777. } while (0); pvr2_context_exit(mp);
  778. return 0;
  779. }
  780. static int pvr2_v4l2_open(struct inode *inode, struct file *file)
  781. {
  782. struct pvr2_v4l2_dev *dip; /* Our own context pointer */
  783. struct pvr2_v4l2_fh *fhp;
  784. struct pvr2_v4l2 *vp;
  785. struct pvr2_hdw *hdw;
  786. dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
  787. vp = dip->v4lp;
  788. hdw = vp->channel.hdw;
  789. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
  790. if (!pvr2_hdw_dev_ok(hdw)) {
  791. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
  792. "pvr2_v4l2_open: hardware not ready");
  793. return -EIO;
  794. }
  795. fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
  796. if (!fhp) {
  797. return -ENOMEM;
  798. }
  799. init_waitqueue_head(&fhp->wait_data);
  800. fhp->dev_info = dip;
  801. pvr2_context_enter(vp->channel.mc_head); do {
  802. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
  803. pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
  804. fhp->vnext = NULL;
  805. fhp->vprev = vp->vlast;
  806. if (vp->vlast) {
  807. vp->vlast->vnext = fhp;
  808. } else {
  809. vp->vfirst = fhp;
  810. }
  811. vp->vlast = fhp;
  812. fhp->vhead = vp;
  813. /* Opening the /dev/radioX device implies a mode switch.
  814. So execute that here. Note that you can get the
  815. IDENTICAL effect merely by opening the normal video
  816. device and setting the input appropriately. */
  817. if (dip->v4l_type == VFL_TYPE_RADIO) {
  818. struct pvr2_ctrl *cp;
  819. cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
  820. pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
  821. pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
  822. pvr2_hdw_commit_ctl(hdw);
  823. }
  824. } while (0); pvr2_context_exit(vp->channel.mc_head);
  825. fhp->file = file;
  826. file->private_data = fhp;
  827. v4l2_prio_open(&vp->prio,&fhp->prio);
  828. fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
  829. return 0;
  830. }
  831. static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
  832. {
  833. wake_up(&fhp->wait_data);
  834. }
  835. static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
  836. {
  837. int ret;
  838. struct pvr2_stream *sp;
  839. struct pvr2_hdw *hdw;
  840. if (fh->rhp) return 0;
  841. if (!fh->dev_info->stream) {
  842. /* No stream defined for this node. This means that we're
  843. not currently allowed to stream from this node. */
  844. return -EPERM;
  845. }
  846. /* First read() attempt. Try to claim the stream and start
  847. it... */
  848. if ((ret = pvr2_channel_claim_stream(&fh->channel,
  849. fh->dev_info->stream)) != 0) {
  850. /* Someone else must already have it */
  851. return ret;
  852. }
  853. fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
  854. if (!fh->rhp) {
  855. pvr2_channel_claim_stream(&fh->channel,NULL);
  856. return -ENOMEM;
  857. }
  858. hdw = fh->channel.mc_head->hdw;
  859. sp = fh->dev_info->stream->stream;
  860. pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
  861. pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
  862. pvr2_hdw_set_streaming(hdw,!0);
  863. ret = pvr2_ioread_set_enabled(fh->rhp,!0);
  864. return ret;
  865. }
  866. static ssize_t pvr2_v4l2_read(struct file *file,
  867. char __user *buff, size_t count, loff_t *ppos)
  868. {
  869. struct pvr2_v4l2_fh *fh = file->private_data;
  870. int ret;
  871. if (fh->fw_mode_flag) {
  872. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  873. char *tbuf;
  874. int c1,c2;
  875. int tcnt = 0;
  876. unsigned int offs = *ppos;
  877. tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
  878. if (!tbuf) return -ENOMEM;
  879. while (count) {
  880. c1 = count;
  881. if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
  882. c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
  883. if (c2 < 0) {
  884. tcnt = c2;
  885. break;
  886. }
  887. if (!c2) break;
  888. if (copy_to_user(buff,tbuf,c2)) {
  889. tcnt = -EFAULT;
  890. break;
  891. }
  892. offs += c2;
  893. tcnt += c2;
  894. buff += c2;
  895. count -= c2;
  896. *ppos += c2;
  897. }
  898. kfree(tbuf);
  899. return tcnt;
  900. }
  901. if (!fh->rhp) {
  902. ret = pvr2_v4l2_iosetup(fh);
  903. if (ret) {
  904. return ret;
  905. }
  906. }
  907. for (;;) {
  908. ret = pvr2_ioread_read(fh->rhp,buff,count);
  909. if (ret >= 0) break;
  910. if (ret != -EAGAIN) break;
  911. if (file->f_flags & O_NONBLOCK) break;
  912. /* Doing blocking I/O. Wait here. */
  913. ret = wait_event_interruptible(
  914. fh->wait_data,
  915. pvr2_ioread_avail(fh->rhp) >= 0);
  916. if (ret < 0) break;
  917. }
  918. return ret;
  919. }
  920. static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
  921. {
  922. unsigned int mask = 0;
  923. struct pvr2_v4l2_fh *fh = file->private_data;
  924. int ret;
  925. if (fh->fw_mode_flag) {
  926. mask |= POLLIN | POLLRDNORM;
  927. return mask;
  928. }
  929. if (!fh->rhp) {
  930. ret = pvr2_v4l2_iosetup(fh);
  931. if (ret) return POLLERR;
  932. }
  933. poll_wait(file,&fh->wait_data,wait);
  934. if (pvr2_ioread_avail(fh->rhp) >= 0) {
  935. mask |= POLLIN | POLLRDNORM;
  936. }
  937. return mask;
  938. }
  939. static const struct file_operations vdev_fops = {
  940. .owner = THIS_MODULE,
  941. .open = pvr2_v4l2_open,
  942. .release = pvr2_v4l2_release,
  943. .read = pvr2_v4l2_read,
  944. .ioctl = pvr2_v4l2_ioctl,
  945. .llseek = no_llseek,
  946. .poll = pvr2_v4l2_poll,
  947. };
  948. #define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
  949. static struct video_device vdev_template = {
  950. .owner = THIS_MODULE,
  951. .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
  952. .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
  953. | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
  954. | V4L2_CAP_READWRITE),
  955. .hardware = VID_HARDWARE_PVRUSB2,
  956. .fops = &vdev_fops,
  957. };
  958. static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
  959. struct pvr2_v4l2 *vp,
  960. int v4l_type)
  961. {
  962. int mindevnum;
  963. int unit_number;
  964. int *nr_ptr = 0;
  965. dip->v4lp = vp;
  966. dip->v4l_type = v4l_type;
  967. switch (v4l_type) {
  968. case VFL_TYPE_GRABBER:
  969. dip->stream = &vp->channel.mc_head->video_stream;
  970. dip->config = pvr2_config_mpeg;
  971. dip->minor_type = pvr2_v4l_type_video;
  972. nr_ptr = video_nr;
  973. if (!dip->stream) {
  974. err("Failed to set up pvrusb2 v4l video dev"
  975. " due to missing stream instance");
  976. return;
  977. }
  978. break;
  979. case VFL_TYPE_VBI:
  980. dip->config = pvr2_config_vbi;
  981. dip->minor_type = pvr2_v4l_type_vbi;
  982. nr_ptr = vbi_nr;
  983. break;
  984. case VFL_TYPE_RADIO:
  985. dip->stream = &vp->channel.mc_head->video_stream;
  986. dip->config = pvr2_config_mpeg;
  987. dip->minor_type = pvr2_v4l_type_radio;
  988. nr_ptr = radio_nr;
  989. break;
  990. default:
  991. /* Bail out (this should be impossible) */
  992. err("Failed to set up pvrusb2 v4l dev"
  993. " due to unrecognized config");
  994. return;
  995. }
  996. memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
  997. dip->devbase.release = pvr2_video_device_release;
  998. mindevnum = -1;
  999. unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
  1000. if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
  1001. mindevnum = nr_ptr[unit_number];
  1002. }
  1003. if ((video_register_device(&dip->devbase,
  1004. dip->v4l_type, mindevnum) < 0) &&
  1005. (video_register_device(&dip->devbase,
  1006. dip->v4l_type, -1) < 0)) {
  1007. err("Failed to register pvrusb2 v4l device");
  1008. }
  1009. printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
  1010. get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
  1011. pvr2_config_get_name(dip->config));
  1012. pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
  1013. dip->minor_type,dip->devbase.minor);
  1014. }
  1015. struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
  1016. {
  1017. struct pvr2_v4l2 *vp;
  1018. vp = kzalloc(sizeof(*vp),GFP_KERNEL);
  1019. if (!vp) return vp;
  1020. vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
  1021. vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
  1022. if (!(vp->dev_video && vp->dev_radio)) {
  1023. kfree(vp->dev_video);
  1024. kfree(vp->dev_radio);
  1025. kfree(vp);
  1026. return NULL;
  1027. }
  1028. pvr2_channel_init(&vp->channel,mnp);
  1029. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
  1030. vp->channel.check_func = pvr2_v4l2_internal_check;
  1031. /* register streams */
  1032. pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
  1033. pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
  1034. return vp;
  1035. }
  1036. /*
  1037. Stuff for Emacs to see, in order to encourage consistent editing style:
  1038. *** Local Variables: ***
  1039. *** mode: c ***
  1040. *** fill-column: 75 ***
  1041. *** tab-width: 8 ***
  1042. *** c-basic-offset: 8 ***
  1043. *** End: ***
  1044. */