pvrusb2-v4l2.c 25 KB

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