pvrusb2-v4l2.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include "pvrusb2-context.h"
  24. #include "pvrusb2-hdw.h"
  25. #include "pvrusb2.h"
  26. #include "pvrusb2-debug.h"
  27. #include "pvrusb2-v4l2.h"
  28. #include "pvrusb2-ioread.h"
  29. #include <linux/videodev2.h>
  30. #include <linux/module.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-fh.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. struct pvr2_v4l2_dev;
  37. struct pvr2_v4l2_fh;
  38. struct pvr2_v4l2;
  39. struct pvr2_v4l2_dev {
  40. struct video_device devbase; /* MUST be first! */
  41. struct pvr2_v4l2 *v4lp;
  42. struct pvr2_context_stream *stream;
  43. /* Information about this device: */
  44. enum pvr2_config config; /* Expected stream format */
  45. int v4l_type; /* V4L defined type for this device node */
  46. enum pvr2_v4l_type minor_type; /* pvr2-understood minor device type */
  47. };
  48. struct pvr2_v4l2_fh {
  49. struct v4l2_fh fh;
  50. struct pvr2_channel channel;
  51. struct pvr2_v4l2_dev *pdi;
  52. struct pvr2_ioread *rhp;
  53. struct file *file;
  54. wait_queue_head_t wait_data;
  55. int fw_mode_flag;
  56. /* Map contiguous ordinal value to input id */
  57. unsigned char *input_map;
  58. unsigned int input_cnt;
  59. };
  60. struct pvr2_v4l2 {
  61. struct pvr2_channel channel;
  62. /* streams - Note that these must be separately, individually,
  63. * allocated pointers. This is because the v4l core is going to
  64. * manage their deletion - separately, individually... */
  65. struct pvr2_v4l2_dev *dev_video;
  66. struct pvr2_v4l2_dev *dev_radio;
  67. };
  68. static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  69. module_param_array(video_nr, int, NULL, 0444);
  70. MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
  71. static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  72. module_param_array(radio_nr, int, NULL, 0444);
  73. MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
  74. static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
  75. module_param_array(vbi_nr, int, NULL, 0444);
  76. MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
  77. static struct v4l2_fmtdesc pvr_fmtdesc [] = {
  78. {
  79. .index = 0,
  80. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  81. .flags = V4L2_FMT_FLAG_COMPRESSED,
  82. .description = "MPEG1/2",
  83. // This should really be V4L2_PIX_FMT_MPEG, but xawtv
  84. // breaks when I do that.
  85. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  86. }
  87. };
  88. #define PVR_FORMAT_PIX 0
  89. #define PVR_FORMAT_VBI 1
  90. static struct v4l2_format pvr_format [] = {
  91. [PVR_FORMAT_PIX] = {
  92. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  93. .fmt = {
  94. .pix = {
  95. .width = 720,
  96. .height = 576,
  97. // This should really be V4L2_PIX_FMT_MPEG,
  98. // but xawtv breaks when I do that.
  99. .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
  100. .field = V4L2_FIELD_INTERLACED,
  101. .bytesperline = 0, // doesn't make sense
  102. // here
  103. //FIXME : Don't know what to put here...
  104. .sizeimage = (32*1024),
  105. .colorspace = 0, // doesn't make sense here
  106. .priv = 0
  107. }
  108. }
  109. },
  110. [PVR_FORMAT_VBI] = {
  111. .type = V4L2_BUF_TYPE_VBI_CAPTURE,
  112. .fmt = {
  113. .vbi = {
  114. .sampling_rate = 27000000,
  115. .offset = 248,
  116. .samples_per_line = 1443,
  117. .sample_format = V4L2_PIX_FMT_GREY,
  118. .start = { 0, 0 },
  119. .count = { 0, 0 },
  120. .flags = 0,
  121. }
  122. }
  123. }
  124. };
  125. /*
  126. * This is part of Video 4 Linux API. These procedures handle ioctl() calls.
  127. */
  128. static int pvr2_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
  129. {
  130. struct pvr2_v4l2_fh *fh = file->private_data;
  131. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  132. strlcpy(cap->driver, "pvrusb2", sizeof(cap->driver));
  133. strlcpy(cap->bus_info, pvr2_hdw_get_bus_info(hdw),
  134. sizeof(cap->bus_info));
  135. strlcpy(cap->card, pvr2_hdw_get_desc(hdw), sizeof(cap->card));
  136. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
  137. V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
  138. V4L2_CAP_READWRITE | V4L2_CAP_DEVICE_CAPS;
  139. switch (fh->pdi->devbase.vfl_type) {
  140. case VFL_TYPE_GRABBER:
  141. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO;
  142. break;
  143. case VFL_TYPE_RADIO:
  144. cap->device_caps = V4L2_CAP_RADIO;
  145. break;
  146. }
  147. cap->device_caps |= V4L2_CAP_TUNER | V4L2_CAP_READWRITE;
  148. return 0;
  149. }
  150. static int pvr2_g_std(struct file *file, void *priv, v4l2_std_id *std)
  151. {
  152. struct pvr2_v4l2_fh *fh = file->private_data;
  153. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  154. int val = 0;
  155. int ret;
  156. ret = pvr2_ctrl_get_value(
  157. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), &val);
  158. *std = val;
  159. return ret;
  160. }
  161. static int pvr2_s_std(struct file *file, void *priv, v4l2_std_id std)
  162. {
  163. struct pvr2_v4l2_fh *fh = file->private_data;
  164. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  165. return pvr2_ctrl_set_value(
  166. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDCUR), std);
  167. }
  168. static int pvr2_querystd(struct file *file, void *priv, v4l2_std_id *std)
  169. {
  170. struct pvr2_v4l2_fh *fh = file->private_data;
  171. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  172. int val = 0;
  173. int ret;
  174. ret = pvr2_ctrl_get_value(
  175. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_STDDETECT), &val);
  176. *std = val;
  177. return ret;
  178. }
  179. static int pvr2_enum_input(struct file *file, void *priv, struct v4l2_input *vi)
  180. {
  181. struct pvr2_v4l2_fh *fh = file->private_data;
  182. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  183. struct pvr2_ctrl *cptr;
  184. struct v4l2_input tmp;
  185. unsigned int cnt;
  186. int val;
  187. cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
  188. memset(&tmp, 0, sizeof(tmp));
  189. tmp.index = vi->index;
  190. if (vi->index >= fh->input_cnt)
  191. return -EINVAL;
  192. val = fh->input_map[vi->index];
  193. switch (val) {
  194. case PVR2_CVAL_INPUT_TV:
  195. case PVR2_CVAL_INPUT_DTV:
  196. case PVR2_CVAL_INPUT_RADIO:
  197. tmp.type = V4L2_INPUT_TYPE_TUNER;
  198. break;
  199. case PVR2_CVAL_INPUT_SVIDEO:
  200. case PVR2_CVAL_INPUT_COMPOSITE:
  201. tmp.type = V4L2_INPUT_TYPE_CAMERA;
  202. break;
  203. default:
  204. return -EINVAL;
  205. }
  206. cnt = 0;
  207. pvr2_ctrl_get_valname(cptr, val,
  208. tmp.name, sizeof(tmp.name) - 1, &cnt);
  209. tmp.name[cnt] = 0;
  210. /* Don't bother with audioset, since this driver currently
  211. always switches the audio whenever the video is
  212. switched. */
  213. /* Handling std is a tougher problem. It doesn't make
  214. sense in cases where a device might be multi-standard.
  215. We could just copy out the current value for the
  216. standard, but it can change over time. For now just
  217. leave it zero. */
  218. *vi = tmp;
  219. return 0;
  220. }
  221. static int pvr2_g_input(struct file *file, void *priv, unsigned int *i)
  222. {
  223. struct pvr2_v4l2_fh *fh = file->private_data;
  224. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  225. unsigned int idx;
  226. struct pvr2_ctrl *cptr;
  227. int val;
  228. int ret;
  229. cptr = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
  230. val = 0;
  231. ret = pvr2_ctrl_get_value(cptr, &val);
  232. *i = 0;
  233. for (idx = 0; idx < fh->input_cnt; idx++) {
  234. if (fh->input_map[idx] == val) {
  235. *i = idx;
  236. break;
  237. }
  238. }
  239. return ret;
  240. }
  241. static int pvr2_s_input(struct file *file, void *priv, unsigned int inp)
  242. {
  243. struct pvr2_v4l2_fh *fh = file->private_data;
  244. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  245. if (inp >= fh->input_cnt)
  246. return -EINVAL;
  247. return pvr2_ctrl_set_value(
  248. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
  249. fh->input_map[inp]);
  250. }
  251. static int pvr2_enumaudio(struct file *file, void *priv, struct v4l2_audio *vin)
  252. {
  253. /* pkt: FIXME: We are returning one "fake" input here
  254. which could very well be called "whatever_we_like".
  255. This is for apps that want to see an audio input
  256. just to feel comfortable, as well as to test if
  257. it can do stereo or sth. There is actually no guarantee
  258. that the actual audio input cannot change behind the app's
  259. back, but most applications should not mind that either.
  260. Hopefully, mplayer people will work with us on this (this
  261. whole mess is to support mplayer pvr://), or Hans will come
  262. up with a more standard way to say "we have inputs but we
  263. don 't want you to change them independent of video" which
  264. will sort this mess.
  265. */
  266. if (vin->index > 0)
  267. return -EINVAL;
  268. strncpy(vin->name, "PVRUSB2 Audio", 14);
  269. vin->capability = V4L2_AUDCAP_STEREO;
  270. return 0;
  271. }
  272. static int pvr2_g_audio(struct file *file, void *priv, struct v4l2_audio *vin)
  273. {
  274. /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
  275. vin->index = 0;
  276. strncpy(vin->name, "PVRUSB2 Audio", 14);
  277. vin->capability = V4L2_AUDCAP_STEREO;
  278. return 0;
  279. }
  280. static int pvr2_s_audio(struct file *file, void *priv, const struct v4l2_audio *vout)
  281. {
  282. if (vout->index)
  283. return -EINVAL;
  284. return 0;
  285. }
  286. static int pvr2_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
  287. {
  288. struct pvr2_v4l2_fh *fh = file->private_data;
  289. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  290. if (vt->index != 0)
  291. return -EINVAL; /* Only answer for the 1st tuner */
  292. pvr2_hdw_execute_tuner_poll(hdw);
  293. return pvr2_hdw_get_tuner_status(hdw, vt);
  294. }
  295. static int pvr2_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
  296. {
  297. struct pvr2_v4l2_fh *fh = file->private_data;
  298. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  299. if (vt->index != 0)
  300. return -EINVAL;
  301. return pvr2_ctrl_set_value(
  302. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_AUDIOMODE),
  303. vt->audmode);
  304. }
  305. static int pvr2_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *vf)
  306. {
  307. struct pvr2_v4l2_fh *fh = file->private_data;
  308. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  309. unsigned long fv;
  310. struct v4l2_tuner vt;
  311. int cur_input;
  312. struct pvr2_ctrl *ctrlp;
  313. int ret;
  314. ret = pvr2_hdw_get_tuner_status(hdw, &vt);
  315. if (ret != 0)
  316. return ret;
  317. ctrlp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT);
  318. ret = pvr2_ctrl_get_value(ctrlp, &cur_input);
  319. if (ret != 0)
  320. return ret;
  321. if (vf->type == V4L2_TUNER_RADIO) {
  322. if (cur_input != PVR2_CVAL_INPUT_RADIO)
  323. pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_RADIO);
  324. } else {
  325. if (cur_input == PVR2_CVAL_INPUT_RADIO)
  326. pvr2_ctrl_set_value(ctrlp, PVR2_CVAL_INPUT_TV);
  327. }
  328. fv = vf->frequency;
  329. if (vt.capability & V4L2_TUNER_CAP_LOW)
  330. fv = (fv * 125) / 2;
  331. else
  332. fv = fv * 62500;
  333. return pvr2_ctrl_set_value(
  334. pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
  335. }
  336. static int pvr2_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
  337. {
  338. struct pvr2_v4l2_fh *fh = file->private_data;
  339. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  340. int val = 0;
  341. int cur_input;
  342. struct v4l2_tuner vt;
  343. int ret;
  344. ret = pvr2_hdw_get_tuner_status(hdw, &vt);
  345. if (ret != 0)
  346. return ret;
  347. ret = pvr2_ctrl_get_value(
  348. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_FREQUENCY),
  349. &val);
  350. if (ret != 0)
  351. return ret;
  352. pvr2_ctrl_get_value(
  353. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_INPUT),
  354. &cur_input);
  355. if (cur_input == PVR2_CVAL_INPUT_RADIO)
  356. vf->type = V4L2_TUNER_RADIO;
  357. else
  358. vf->type = V4L2_TUNER_ANALOG_TV;
  359. if (vt.capability & V4L2_TUNER_CAP_LOW)
  360. val = (val * 2) / 125;
  361. else
  362. val /= 62500;
  363. vf->frequency = val;
  364. return 0;
  365. }
  366. static int pvr2_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *fd)
  367. {
  368. /* Only one format is supported : mpeg.*/
  369. if (fd->index != 0)
  370. return -EINVAL;
  371. memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
  372. return 0;
  373. }
  374. static int pvr2_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
  375. {
  376. struct pvr2_v4l2_fh *fh = file->private_data;
  377. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  378. int val;
  379. memcpy(vf, &pvr_format[PVR_FORMAT_PIX], sizeof(struct v4l2_format));
  380. val = 0;
  381. pvr2_ctrl_get_value(
  382. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES),
  383. &val);
  384. vf->fmt.pix.width = val;
  385. val = 0;
  386. pvr2_ctrl_get_value(
  387. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES),
  388. &val);
  389. vf->fmt.pix.height = val;
  390. return 0;
  391. }
  392. static int pvr2_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
  393. {
  394. struct pvr2_v4l2_fh *fh = file->private_data;
  395. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  396. int lmin, lmax, ldef;
  397. struct pvr2_ctrl *hcp, *vcp;
  398. int h = vf->fmt.pix.height;
  399. int w = vf->fmt.pix.width;
  400. hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
  401. vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
  402. lmin = pvr2_ctrl_get_min(hcp);
  403. lmax = pvr2_ctrl_get_max(hcp);
  404. pvr2_ctrl_get_def(hcp, &ldef);
  405. if (w == -1)
  406. w = ldef;
  407. else if (w < lmin)
  408. w = lmin;
  409. else if (w > lmax)
  410. w = lmax;
  411. lmin = pvr2_ctrl_get_min(vcp);
  412. lmax = pvr2_ctrl_get_max(vcp);
  413. pvr2_ctrl_get_def(vcp, &ldef);
  414. if (h == -1)
  415. h = ldef;
  416. else if (h < lmin)
  417. h = lmin;
  418. else if (h > lmax)
  419. h = lmax;
  420. memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
  421. sizeof(struct v4l2_format));
  422. vf->fmt.pix.width = w;
  423. vf->fmt.pix.height = h;
  424. return 0;
  425. }
  426. static int pvr2_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *vf)
  427. {
  428. struct pvr2_v4l2_fh *fh = file->private_data;
  429. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  430. struct pvr2_ctrl *hcp, *vcp;
  431. int ret = pvr2_try_fmt_vid_cap(file, fh, vf);
  432. if (ret)
  433. return ret;
  434. hcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_HRES);
  435. vcp = pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_VRES);
  436. pvr2_ctrl_set_value(hcp, vf->fmt.pix.width);
  437. pvr2_ctrl_set_value(vcp, vf->fmt.pix.height);
  438. return 0;
  439. }
  440. static int pvr2_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  441. {
  442. struct pvr2_v4l2_fh *fh = file->private_data;
  443. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  444. struct pvr2_v4l2_dev *pdi = fh->pdi;
  445. int ret;
  446. if (!fh->pdi->stream) {
  447. /* No stream defined for this node. This means
  448. that we're not currently allowed to stream from
  449. this node. */
  450. return -EPERM;
  451. }
  452. ret = pvr2_hdw_set_stream_type(hdw, pdi->config);
  453. if (ret < 0)
  454. return ret;
  455. return pvr2_hdw_set_streaming(hdw, !0);
  456. }
  457. static int pvr2_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  458. {
  459. struct pvr2_v4l2_fh *fh = file->private_data;
  460. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  461. if (!fh->pdi->stream) {
  462. /* No stream defined for this node. This means
  463. that we're not currently allowed to stream from
  464. this node. */
  465. return -EPERM;
  466. }
  467. return pvr2_hdw_set_streaming(hdw, 0);
  468. }
  469. static int pvr2_queryctrl(struct file *file, void *priv,
  470. struct v4l2_queryctrl *vc)
  471. {
  472. struct pvr2_v4l2_fh *fh = file->private_data;
  473. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  474. struct pvr2_ctrl *cptr;
  475. int val;
  476. if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
  477. cptr = pvr2_hdw_get_ctrl_nextv4l(
  478. hdw, (vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
  479. if (cptr)
  480. vc->id = pvr2_ctrl_get_v4lid(cptr);
  481. } else {
  482. cptr = pvr2_hdw_get_ctrl_v4l(hdw, vc->id);
  483. }
  484. if (!cptr) {
  485. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  486. "QUERYCTRL id=0x%x not implemented here",
  487. vc->id);
  488. return -EINVAL;
  489. }
  490. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  491. "QUERYCTRL id=0x%x mapping name=%s (%s)",
  492. vc->id, pvr2_ctrl_get_name(cptr),
  493. pvr2_ctrl_get_desc(cptr));
  494. strlcpy(vc->name, pvr2_ctrl_get_desc(cptr), sizeof(vc->name));
  495. vc->flags = pvr2_ctrl_get_v4lflags(cptr);
  496. pvr2_ctrl_get_def(cptr, &val);
  497. vc->default_value = val;
  498. switch (pvr2_ctrl_get_type(cptr)) {
  499. case pvr2_ctl_enum:
  500. vc->type = V4L2_CTRL_TYPE_MENU;
  501. vc->minimum = 0;
  502. vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
  503. vc->step = 1;
  504. break;
  505. case pvr2_ctl_bool:
  506. vc->type = V4L2_CTRL_TYPE_BOOLEAN;
  507. vc->minimum = 0;
  508. vc->maximum = 1;
  509. vc->step = 1;
  510. break;
  511. case pvr2_ctl_int:
  512. vc->type = V4L2_CTRL_TYPE_INTEGER;
  513. vc->minimum = pvr2_ctrl_get_min(cptr);
  514. vc->maximum = pvr2_ctrl_get_max(cptr);
  515. vc->step = 1;
  516. break;
  517. default:
  518. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  519. "QUERYCTRL id=0x%x name=%s not mappable",
  520. vc->id, pvr2_ctrl_get_name(cptr));
  521. return -EINVAL;
  522. }
  523. return 0;
  524. }
  525. static int pvr2_querymenu(struct file *file, void *priv, struct v4l2_querymenu *vm)
  526. {
  527. struct pvr2_v4l2_fh *fh = file->private_data;
  528. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  529. unsigned int cnt = 0;
  530. int ret;
  531. ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw, vm->id),
  532. vm->index,
  533. vm->name, sizeof(vm->name) - 1,
  534. &cnt);
  535. vm->name[cnt] = 0;
  536. return ret;
  537. }
  538. static int pvr2_g_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
  539. {
  540. struct pvr2_v4l2_fh *fh = file->private_data;
  541. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  542. int val = 0;
  543. int ret;
  544. ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
  545. &val);
  546. vc->value = val;
  547. return ret;
  548. }
  549. static int pvr2_s_ctrl(struct file *file, void *priv, struct v4l2_control *vc)
  550. {
  551. struct pvr2_v4l2_fh *fh = file->private_data;
  552. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  553. return pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw, vc->id),
  554. vc->value);
  555. }
  556. static int pvr2_g_ext_ctrls(struct file *file, void *priv,
  557. struct v4l2_ext_controls *ctls)
  558. {
  559. struct pvr2_v4l2_fh *fh = file->private_data;
  560. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  561. struct v4l2_ext_control *ctrl;
  562. unsigned int idx;
  563. int val;
  564. int ret;
  565. ret = 0;
  566. for (idx = 0; idx < ctls->count; idx++) {
  567. ctrl = ctls->controls + idx;
  568. ret = pvr2_ctrl_get_value(
  569. pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id), &val);
  570. if (ret) {
  571. ctls->error_idx = idx;
  572. return ret;
  573. }
  574. /* Ensure that if read as a 64 bit value, the user
  575. will still get a hopefully sane value */
  576. ctrl->value64 = 0;
  577. ctrl->value = val;
  578. }
  579. return 0;
  580. }
  581. static int pvr2_s_ext_ctrls(struct file *file, void *priv,
  582. struct v4l2_ext_controls *ctls)
  583. {
  584. struct pvr2_v4l2_fh *fh = file->private_data;
  585. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  586. struct v4l2_ext_control *ctrl;
  587. unsigned int idx;
  588. int ret;
  589. ret = 0;
  590. for (idx = 0; idx < ctls->count; idx++) {
  591. ctrl = ctls->controls + idx;
  592. ret = pvr2_ctrl_set_value(
  593. pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id),
  594. ctrl->value);
  595. if (ret) {
  596. ctls->error_idx = idx;
  597. return ret;
  598. }
  599. }
  600. return 0;
  601. }
  602. static int pvr2_try_ext_ctrls(struct file *file, void *priv,
  603. struct v4l2_ext_controls *ctls)
  604. {
  605. struct pvr2_v4l2_fh *fh = file->private_data;
  606. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  607. struct v4l2_ext_control *ctrl;
  608. struct pvr2_ctrl *pctl;
  609. unsigned int idx;
  610. /* For the moment just validate that the requested control
  611. actually exists. */
  612. for (idx = 0; idx < ctls->count; idx++) {
  613. ctrl = ctls->controls + idx;
  614. pctl = pvr2_hdw_get_ctrl_v4l(hdw, ctrl->id);
  615. if (!pctl) {
  616. ctls->error_idx = idx;
  617. return -EINVAL;
  618. }
  619. }
  620. return 0;
  621. }
  622. static int pvr2_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cap)
  623. {
  624. struct pvr2_v4l2_fh *fh = file->private_data;
  625. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  626. int ret;
  627. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  628. return -EINVAL;
  629. ret = pvr2_hdw_get_cropcap(hdw, cap);
  630. cap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* paranoia */
  631. return ret;
  632. }
  633. static int pvr2_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  634. {
  635. struct pvr2_v4l2_fh *fh = file->private_data;
  636. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  637. int val = 0;
  638. int ret;
  639. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  640. return -EINVAL;
  641. ret = pvr2_ctrl_get_value(
  642. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL), &val);
  643. if (ret != 0)
  644. return -EINVAL;
  645. crop->c.left = val;
  646. ret = pvr2_ctrl_get_value(
  647. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT), &val);
  648. if (ret != 0)
  649. return -EINVAL;
  650. crop->c.top = val;
  651. ret = pvr2_ctrl_get_value(
  652. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW), &val);
  653. if (ret != 0)
  654. return -EINVAL;
  655. crop->c.width = val;
  656. ret = pvr2_ctrl_get_value(
  657. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH), &val);
  658. if (ret != 0)
  659. return -EINVAL;
  660. crop->c.height = val;
  661. return 0;
  662. }
  663. static int pvr2_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
  664. {
  665. struct pvr2_v4l2_fh *fh = file->private_data;
  666. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  667. int ret;
  668. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  669. return -EINVAL;
  670. ret = pvr2_ctrl_set_value(
  671. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPL),
  672. crop->c.left);
  673. if (ret != 0)
  674. return -EINVAL;
  675. ret = pvr2_ctrl_set_value(
  676. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPT),
  677. crop->c.top);
  678. if (ret != 0)
  679. return -EINVAL;
  680. ret = pvr2_ctrl_set_value(
  681. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPW),
  682. crop->c.width);
  683. if (ret != 0)
  684. return -EINVAL;
  685. ret = pvr2_ctrl_set_value(
  686. pvr2_hdw_get_ctrl_by_id(hdw, PVR2_CID_CROPH),
  687. crop->c.height);
  688. if (ret != 0)
  689. return -EINVAL;
  690. return 0;
  691. }
  692. static int pvr2_log_status(struct file *file, void *priv)
  693. {
  694. struct pvr2_v4l2_fh *fh = file->private_data;
  695. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  696. pvr2_hdw_trigger_module_log(hdw);
  697. return 0;
  698. }
  699. static const struct v4l2_ioctl_ops pvr2_ioctl_ops = {
  700. .vidioc_querycap = pvr2_querycap,
  701. .vidioc_s_audio = pvr2_s_audio,
  702. .vidioc_g_audio = pvr2_g_audio,
  703. .vidioc_enumaudio = pvr2_enumaudio,
  704. .vidioc_enum_input = pvr2_enum_input,
  705. .vidioc_cropcap = pvr2_cropcap,
  706. .vidioc_s_crop = pvr2_s_crop,
  707. .vidioc_g_crop = pvr2_g_crop,
  708. .vidioc_g_input = pvr2_g_input,
  709. .vidioc_s_input = pvr2_s_input,
  710. .vidioc_g_frequency = pvr2_g_frequency,
  711. .vidioc_s_frequency = pvr2_s_frequency,
  712. .vidioc_s_tuner = pvr2_s_tuner,
  713. .vidioc_g_tuner = pvr2_g_tuner,
  714. .vidioc_g_std = pvr2_g_std,
  715. .vidioc_s_std = pvr2_s_std,
  716. .vidioc_querystd = pvr2_querystd,
  717. .vidioc_log_status = pvr2_log_status,
  718. .vidioc_enum_fmt_vid_cap = pvr2_enum_fmt_vid_cap,
  719. .vidioc_g_fmt_vid_cap = pvr2_g_fmt_vid_cap,
  720. .vidioc_s_fmt_vid_cap = pvr2_s_fmt_vid_cap,
  721. .vidioc_try_fmt_vid_cap = pvr2_try_fmt_vid_cap,
  722. .vidioc_streamon = pvr2_streamon,
  723. .vidioc_streamoff = pvr2_streamoff,
  724. .vidioc_queryctrl = pvr2_queryctrl,
  725. .vidioc_querymenu = pvr2_querymenu,
  726. .vidioc_g_ctrl = pvr2_g_ctrl,
  727. .vidioc_s_ctrl = pvr2_s_ctrl,
  728. .vidioc_g_ext_ctrls = pvr2_g_ext_ctrls,
  729. .vidioc_s_ext_ctrls = pvr2_s_ext_ctrls,
  730. .vidioc_try_ext_ctrls = pvr2_try_ext_ctrls,
  731. };
  732. static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
  733. {
  734. struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
  735. enum pvr2_config cfg = dip->config;
  736. char msg[80];
  737. unsigned int mcnt;
  738. /* Construct the unregistration message *before* we actually
  739. perform the unregistration step. By doing it this way we don't
  740. have to worry about potentially touching deleted resources. */
  741. mcnt = scnprintf(msg, sizeof(msg) - 1,
  742. "pvrusb2: unregistered device %s [%s]",
  743. video_device_node_name(&dip->devbase),
  744. pvr2_config_get_name(cfg));
  745. msg[mcnt] = 0;
  746. pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
  747. /* Paranoia */
  748. dip->v4lp = NULL;
  749. dip->stream = NULL;
  750. /* Actual deallocation happens later when all internal references
  751. are gone. */
  752. video_unregister_device(&dip->devbase);
  753. printk(KERN_INFO "%s\n", msg);
  754. }
  755. static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev *dip)
  756. {
  757. if (!dip) return;
  758. if (!dip->devbase.v4l2_dev->dev) return;
  759. dip->devbase.v4l2_dev->dev = NULL;
  760. device_move(&dip->devbase.dev, NULL, DPM_ORDER_NONE);
  761. }
  762. static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
  763. {
  764. if (vp->dev_video) {
  765. pvr2_v4l2_dev_destroy(vp->dev_video);
  766. vp->dev_video = NULL;
  767. }
  768. if (vp->dev_radio) {
  769. pvr2_v4l2_dev_destroy(vp->dev_radio);
  770. vp->dev_radio = NULL;
  771. }
  772. pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
  773. pvr2_channel_done(&vp->channel);
  774. kfree(vp);
  775. }
  776. static void pvr2_video_device_release(struct video_device *vdev)
  777. {
  778. struct pvr2_v4l2_dev *dev;
  779. dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
  780. kfree(dev);
  781. }
  782. static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
  783. {
  784. struct pvr2_v4l2 *vp;
  785. vp = container_of(chp,struct pvr2_v4l2,channel);
  786. if (!vp->channel.mc_head->disconnect_flag) return;
  787. pvr2_v4l2_dev_disassociate_parent(vp->dev_video);
  788. pvr2_v4l2_dev_disassociate_parent(vp->dev_radio);
  789. if (!list_empty(&vp->dev_video->devbase.fh_list) ||
  790. !list_empty(&vp->dev_radio->devbase.fh_list))
  791. return;
  792. pvr2_v4l2_destroy_no_lock(vp);
  793. }
  794. static long pvr2_v4l2_ioctl(struct file *file,
  795. unsigned int cmd, unsigned long arg)
  796. {
  797. struct pvr2_v4l2_fh *fh = file->private_data;
  798. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  799. long ret = -EINVAL;
  800. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL)
  801. v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw), cmd);
  802. if (!pvr2_hdw_dev_ok(hdw)) {
  803. pvr2_trace(PVR2_TRACE_ERROR_LEGS,
  804. "ioctl failed - bad or no context");
  805. return -EFAULT;
  806. }
  807. ret = video_ioctl2(file, cmd, arg);
  808. pvr2_hdw_commit_ctl(hdw);
  809. if (ret < 0) {
  810. if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
  811. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  812. "pvr2_v4l2_do_ioctl failure, ret=%ld"
  813. " command was:", ret);
  814. v4l_printk_ioctl(pvr2_hdw_get_driver_name(hdw), cmd);
  815. }
  816. } else {
  817. pvr2_trace(PVR2_TRACE_V4LIOCTL,
  818. "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
  819. ret, ret);
  820. }
  821. return ret;
  822. }
  823. static int pvr2_v4l2_release(struct file *file)
  824. {
  825. struct pvr2_v4l2_fh *fhp = file->private_data;
  826. struct pvr2_v4l2 *vp = fhp->pdi->v4lp;
  827. struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
  828. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
  829. if (fhp->rhp) {
  830. struct pvr2_stream *sp;
  831. pvr2_hdw_set_streaming(hdw,0);
  832. sp = pvr2_ioread_get_stream(fhp->rhp);
  833. if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
  834. pvr2_ioread_destroy(fhp->rhp);
  835. fhp->rhp = NULL;
  836. }
  837. v4l2_fh_del(&fhp->fh);
  838. v4l2_fh_exit(&fhp->fh);
  839. file->private_data = NULL;
  840. pvr2_channel_done(&fhp->channel);
  841. pvr2_trace(PVR2_TRACE_STRUCT,
  842. "Destroying pvr_v4l2_fh id=%p",fhp);
  843. if (fhp->input_map) {
  844. kfree(fhp->input_map);
  845. fhp->input_map = NULL;
  846. }
  847. kfree(fhp);
  848. if (vp->channel.mc_head->disconnect_flag &&
  849. list_empty(&vp->dev_video->devbase.fh_list) &&
  850. list_empty(&vp->dev_radio->devbase.fh_list)) {
  851. pvr2_v4l2_destroy_no_lock(vp);
  852. }
  853. return 0;
  854. }
  855. static int pvr2_v4l2_open(struct file *file)
  856. {
  857. struct pvr2_v4l2_dev *dip; /* Our own context pointer */
  858. struct pvr2_v4l2_fh *fhp;
  859. struct pvr2_v4l2 *vp;
  860. struct pvr2_hdw *hdw;
  861. unsigned int input_mask = 0;
  862. unsigned int input_cnt,idx;
  863. int ret = 0;
  864. dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
  865. vp = dip->v4lp;
  866. hdw = vp->channel.hdw;
  867. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
  868. if (!pvr2_hdw_dev_ok(hdw)) {
  869. pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
  870. "pvr2_v4l2_open: hardware not ready");
  871. return -EIO;
  872. }
  873. fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
  874. if (!fhp) {
  875. return -ENOMEM;
  876. }
  877. v4l2_fh_init(&fhp->fh, &dip->devbase);
  878. init_waitqueue_head(&fhp->wait_data);
  879. fhp->pdi = dip;
  880. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
  881. pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
  882. if (dip->v4l_type == VFL_TYPE_RADIO) {
  883. /* Opening device as a radio, legal input selection subset
  884. is just the radio. */
  885. input_mask = (1 << PVR2_CVAL_INPUT_RADIO);
  886. } else {
  887. /* Opening the main V4L device, legal input selection
  888. subset includes all analog inputs. */
  889. input_mask = ((1 << PVR2_CVAL_INPUT_RADIO) |
  890. (1 << PVR2_CVAL_INPUT_TV) |
  891. (1 << PVR2_CVAL_INPUT_COMPOSITE) |
  892. (1 << PVR2_CVAL_INPUT_SVIDEO));
  893. }
  894. ret = pvr2_channel_limit_inputs(&fhp->channel,input_mask);
  895. if (ret) {
  896. pvr2_channel_done(&fhp->channel);
  897. pvr2_trace(PVR2_TRACE_STRUCT,
  898. "Destroying pvr_v4l2_fh id=%p (input mask error)",
  899. fhp);
  900. kfree(fhp);
  901. return ret;
  902. }
  903. input_mask &= pvr2_hdw_get_input_available(hdw);
  904. input_cnt = 0;
  905. for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
  906. if (input_mask & (1 << idx)) input_cnt++;
  907. }
  908. fhp->input_cnt = input_cnt;
  909. fhp->input_map = kzalloc(input_cnt,GFP_KERNEL);
  910. if (!fhp->input_map) {
  911. pvr2_channel_done(&fhp->channel);
  912. pvr2_trace(PVR2_TRACE_STRUCT,
  913. "Destroying pvr_v4l2_fh id=%p (input map failure)",
  914. fhp);
  915. kfree(fhp);
  916. return -ENOMEM;
  917. }
  918. input_cnt = 0;
  919. for (idx = 0; idx < (sizeof(input_mask) << 3); idx++) {
  920. if (!(input_mask & (1 << idx))) continue;
  921. fhp->input_map[input_cnt++] = idx;
  922. }
  923. fhp->file = file;
  924. file->private_data = fhp;
  925. fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
  926. v4l2_fh_add(&fhp->fh);
  927. return 0;
  928. }
  929. static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
  930. {
  931. wake_up(&fhp->wait_data);
  932. }
  933. static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
  934. {
  935. int ret;
  936. struct pvr2_stream *sp;
  937. struct pvr2_hdw *hdw;
  938. if (fh->rhp) return 0;
  939. if (!fh->pdi->stream) {
  940. /* No stream defined for this node. This means that we're
  941. not currently allowed to stream from this node. */
  942. return -EPERM;
  943. }
  944. /* First read() attempt. Try to claim the stream and start
  945. it... */
  946. if ((ret = pvr2_channel_claim_stream(&fh->channel,
  947. fh->pdi->stream)) != 0) {
  948. /* Someone else must already have it */
  949. return ret;
  950. }
  951. fh->rhp = pvr2_channel_create_mpeg_stream(fh->pdi->stream);
  952. if (!fh->rhp) {
  953. pvr2_channel_claim_stream(&fh->channel,NULL);
  954. return -ENOMEM;
  955. }
  956. hdw = fh->channel.mc_head->hdw;
  957. sp = fh->pdi->stream->stream;
  958. pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
  959. pvr2_hdw_set_stream_type(hdw,fh->pdi->config);
  960. if ((ret = pvr2_hdw_set_streaming(hdw,!0)) < 0) return ret;
  961. return pvr2_ioread_set_enabled(fh->rhp,!0);
  962. }
  963. static ssize_t pvr2_v4l2_read(struct file *file,
  964. char __user *buff, size_t count, loff_t *ppos)
  965. {
  966. struct pvr2_v4l2_fh *fh = file->private_data;
  967. int ret;
  968. if (fh->fw_mode_flag) {
  969. struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
  970. char *tbuf;
  971. int c1,c2;
  972. int tcnt = 0;
  973. unsigned int offs = *ppos;
  974. tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
  975. if (!tbuf) return -ENOMEM;
  976. while (count) {
  977. c1 = count;
  978. if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
  979. c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
  980. if (c2 < 0) {
  981. tcnt = c2;
  982. break;
  983. }
  984. if (!c2) break;
  985. if (copy_to_user(buff,tbuf,c2)) {
  986. tcnt = -EFAULT;
  987. break;
  988. }
  989. offs += c2;
  990. tcnt += c2;
  991. buff += c2;
  992. count -= c2;
  993. *ppos += c2;
  994. }
  995. kfree(tbuf);
  996. return tcnt;
  997. }
  998. if (!fh->rhp) {
  999. ret = pvr2_v4l2_iosetup(fh);
  1000. if (ret) {
  1001. return ret;
  1002. }
  1003. }
  1004. for (;;) {
  1005. ret = pvr2_ioread_read(fh->rhp,buff,count);
  1006. if (ret >= 0) break;
  1007. if (ret != -EAGAIN) break;
  1008. if (file->f_flags & O_NONBLOCK) break;
  1009. /* Doing blocking I/O. Wait here. */
  1010. ret = wait_event_interruptible(
  1011. fh->wait_data,
  1012. pvr2_ioread_avail(fh->rhp) >= 0);
  1013. if (ret < 0) break;
  1014. }
  1015. return ret;
  1016. }
  1017. static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
  1018. {
  1019. unsigned int mask = 0;
  1020. struct pvr2_v4l2_fh *fh = file->private_data;
  1021. int ret;
  1022. if (fh->fw_mode_flag) {
  1023. mask |= POLLIN | POLLRDNORM;
  1024. return mask;
  1025. }
  1026. if (!fh->rhp) {
  1027. ret = pvr2_v4l2_iosetup(fh);
  1028. if (ret) return POLLERR;
  1029. }
  1030. poll_wait(file,&fh->wait_data,wait);
  1031. if (pvr2_ioread_avail(fh->rhp) >= 0) {
  1032. mask |= POLLIN | POLLRDNORM;
  1033. }
  1034. return mask;
  1035. }
  1036. static const struct v4l2_file_operations vdev_fops = {
  1037. .owner = THIS_MODULE,
  1038. .open = pvr2_v4l2_open,
  1039. .release = pvr2_v4l2_release,
  1040. .read = pvr2_v4l2_read,
  1041. .unlocked_ioctl = pvr2_v4l2_ioctl,
  1042. .poll = pvr2_v4l2_poll,
  1043. };
  1044. static struct video_device vdev_template = {
  1045. .fops = &vdev_fops,
  1046. };
  1047. static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
  1048. struct pvr2_v4l2 *vp,
  1049. int v4l_type)
  1050. {
  1051. int mindevnum;
  1052. int unit_number;
  1053. struct pvr2_hdw *hdw;
  1054. int *nr_ptr = NULL;
  1055. dip->v4lp = vp;
  1056. hdw = vp->channel.mc_head->hdw;
  1057. dip->v4l_type = v4l_type;
  1058. switch (v4l_type) {
  1059. case VFL_TYPE_GRABBER:
  1060. dip->stream = &vp->channel.mc_head->video_stream;
  1061. dip->config = pvr2_config_mpeg;
  1062. dip->minor_type = pvr2_v4l_type_video;
  1063. nr_ptr = video_nr;
  1064. if (!dip->stream) {
  1065. pr_err(KBUILD_MODNAME
  1066. ": Failed to set up pvrusb2 v4l video dev"
  1067. " due to missing stream instance\n");
  1068. return;
  1069. }
  1070. break;
  1071. case VFL_TYPE_VBI:
  1072. dip->config = pvr2_config_vbi;
  1073. dip->minor_type = pvr2_v4l_type_vbi;
  1074. nr_ptr = vbi_nr;
  1075. break;
  1076. case VFL_TYPE_RADIO:
  1077. dip->stream = &vp->channel.mc_head->video_stream;
  1078. dip->config = pvr2_config_mpeg;
  1079. dip->minor_type = pvr2_v4l_type_radio;
  1080. nr_ptr = radio_nr;
  1081. break;
  1082. default:
  1083. /* Bail out (this should be impossible) */
  1084. pr_err(KBUILD_MODNAME ": Failed to set up pvrusb2 v4l dev"
  1085. " due to unrecognized config\n");
  1086. return;
  1087. }
  1088. dip->devbase = vdev_template;
  1089. dip->devbase.release = pvr2_video_device_release;
  1090. dip->devbase.ioctl_ops = &pvr2_ioctl_ops;
  1091. {
  1092. int val;
  1093. pvr2_ctrl_get_value(
  1094. pvr2_hdw_get_ctrl_by_id(hdw,
  1095. PVR2_CID_STDAVAIL), &val);
  1096. dip->devbase.tvnorms = (v4l2_std_id)val;
  1097. }
  1098. mindevnum = -1;
  1099. unit_number = pvr2_hdw_get_unit_number(hdw);
  1100. if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
  1101. mindevnum = nr_ptr[unit_number];
  1102. }
  1103. pvr2_hdw_set_v4l2_dev(hdw, &dip->devbase);
  1104. if ((video_register_device(&dip->devbase,
  1105. dip->v4l_type, mindevnum) < 0) &&
  1106. (video_register_device(&dip->devbase,
  1107. dip->v4l_type, -1) < 0)) {
  1108. pr_err(KBUILD_MODNAME
  1109. ": Failed to register pvrusb2 v4l device\n");
  1110. }
  1111. printk(KERN_INFO "pvrusb2: registered device %s [%s]\n",
  1112. video_device_node_name(&dip->devbase),
  1113. pvr2_config_get_name(dip->config));
  1114. pvr2_hdw_v4l_store_minor_number(hdw,
  1115. dip->minor_type,dip->devbase.minor);
  1116. }
  1117. struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
  1118. {
  1119. struct pvr2_v4l2 *vp;
  1120. vp = kzalloc(sizeof(*vp),GFP_KERNEL);
  1121. if (!vp) return vp;
  1122. pvr2_channel_init(&vp->channel,mnp);
  1123. pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
  1124. vp->channel.check_func = pvr2_v4l2_internal_check;
  1125. /* register streams */
  1126. vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
  1127. if (!vp->dev_video) goto fail;
  1128. pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
  1129. if (pvr2_hdw_get_input_available(vp->channel.mc_head->hdw) &
  1130. (1 << PVR2_CVAL_INPUT_RADIO)) {
  1131. vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
  1132. if (!vp->dev_radio) goto fail;
  1133. pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
  1134. }
  1135. return vp;
  1136. fail:
  1137. pvr2_trace(PVR2_TRACE_STRUCT,"Failure creating pvr2_v4l2 id=%p",vp);
  1138. pvr2_v4l2_destroy_no_lock(vp);
  1139. return NULL;
  1140. }