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