pvrusb2-v4l2.c 28 KB

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