pvrusb2-v4l2.c 27 KB

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