blob: 2a67638652662b0f2259d6d055c309d04aecb7fe [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
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
23#include <linux/kernel.h>
Mauro Carvalho Chehabce4260c2006-06-26 22:26:08 -030024#include <linux/version.h>
Mike Iselyd8554972006-06-26 20:58:46 -030025#include "pvrusb2-context.h"
26#include "pvrusb2-hdw.h"
27#include "pvrusb2.h"
28#include "pvrusb2-debug.h"
29#include "pvrusb2-v4l2.h"
30#include "pvrusb2-ioread.h"
31#include <linux/videodev2.h>
Mike Isely43e06022006-09-23 23:47:50 -030032#include <media/v4l2-dev.h>
Mike Iselyd8554972006-06-26 20:58:46 -030033#include <media/v4l2-common.h>
34
35struct pvr2_v4l2_dev;
36struct pvr2_v4l2_fh;
37struct pvr2_v4l2;
38
Mike Iselyd8554972006-06-26 20:58:46 -030039struct pvr2_v4l2_dev {
Mike Isely75910052006-09-23 22:30:50 -030040 struct video_device devbase; /* MUST be first! */
Mike Iselyd8554972006-06-26 20:58:46 -030041 struct pvr2_v4l2 *v4lp;
Mike Iselyd8554972006-06-26 20:58:46 -030042 struct pvr2_context_stream *stream;
Mike Isely16eb40d2006-12-30 18:27:32 -030043 /* 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 */
Mike Iselyd8554972006-06-26 20:58:46 -030047};
48
49struct pvr2_v4l2_fh {
50 struct pvr2_channel channel;
51 struct pvr2_v4l2_dev *dev_info;
52 enum v4l2_priority prio;
53 struct pvr2_ioread *rhp;
54 struct file *file;
55 struct pvr2_v4l2 *vhead;
56 struct pvr2_v4l2_fh *vnext;
57 struct pvr2_v4l2_fh *vprev;
58 wait_queue_head_t wait_data;
59 int fw_mode_flag;
Mike Iselyc74e0062006-12-30 18:31:22 -030060 int prev_input_val;
Mike Iselyd8554972006-06-26 20:58:46 -030061};
62
63struct pvr2_v4l2 {
64 struct pvr2_channel channel;
65 struct pvr2_v4l2_fh *vfirst;
66 struct pvr2_v4l2_fh *vlast;
67
68 struct v4l2_prio_state prio;
69
Mike Isely0f0f2572006-12-27 23:19:42 -030070 /* streams - Note that these must be separately, individually,
71 * allocated pointers. This is because the v4l core is going to
72 * manage their deletion - separately, individually... */
73 struct pvr2_v4l2_dev *dev_video;
74 struct pvr2_v4l2_dev *dev_radio;
Mike Iselyd8554972006-06-26 20:58:46 -030075};
76
77static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
78module_param_array(video_nr, int, NULL, 0444);
Mike Isely5e6862c2006-12-27 23:17:26 -030079MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
80static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
81module_param_array(radio_nr, int, NULL, 0444);
82MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
83static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
84module_param_array(vbi_nr, int, NULL, 0444);
85MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
Mike Iselyd8554972006-06-26 20:58:46 -030086
Adrian Bunk07e337e2006-06-30 11:30:20 -030087static struct v4l2_capability pvr_capability ={
Mike Iselyd8554972006-06-26 20:58:46 -030088 .driver = "pvrusb2",
89 .card = "Hauppauge WinTV pvr-usb2",
90 .bus_info = "usb",
91 .version = KERNEL_VERSION(0,8,0),
92 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -030093 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
Mike Iselyd8554972006-06-26 20:58:46 -030094 V4L2_CAP_READWRITE),
95 .reserved = {0,0,0,0}
96};
97
Adrian Bunk07e337e2006-06-30 11:30:20 -030098static struct v4l2_fmtdesc pvr_fmtdesc [] = {
Mike Iselyd8554972006-06-26 20:58:46 -030099 {
100 .index = 0,
101 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
102 .flags = V4L2_FMT_FLAG_COMPRESSED,
103 .description = "MPEG1/2",
104 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
105 // breaks when I do that.
106 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
107 .reserved = { 0, 0, 0, 0 }
108 }
109};
110
111#define PVR_FORMAT_PIX 0
112#define PVR_FORMAT_VBI 1
113
Adrian Bunk07e337e2006-06-30 11:30:20 -0300114static struct v4l2_format pvr_format [] = {
Mike Iselyd8554972006-06-26 20:58:46 -0300115 [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
Mike Isely16eb40d2006-12-30 18:27:32 -0300151
152static const char *get_v4l_name(int v4l_type)
153{
154 switch (v4l_type) {
155 case VFL_TYPE_GRABBER: return "video";
156 case VFL_TYPE_RADIO: return "radio";
157 case VFL_TYPE_VBI: return "vbi";
158 default: return "?";
159 }
160}
161
162
Mike Iselyd8554972006-06-26 20:58:46 -0300163/*
164 * pvr_ioctl()
165 *
166 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
167 *
168 */
169static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
170 unsigned int cmd, void *arg)
171{
172 struct pvr2_v4l2_fh *fh = file->private_data;
173 struct pvr2_v4l2 *vp = fh->vhead;
174 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
175 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
176 int ret = -EINVAL;
177
178 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
179 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
180 }
181
182 if (!pvr2_hdw_dev_ok(hdw)) {
183 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
184 "ioctl failed - bad or no context");
185 return -EFAULT;
186 }
187
188 /* check priority */
189 switch (cmd) {
190 case VIDIOC_S_CTRL:
191 case VIDIOC_S_STD:
192 case VIDIOC_S_INPUT:
193 case VIDIOC_S_TUNER:
194 case VIDIOC_S_FREQUENCY:
195 ret = v4l2_prio_check(&vp->prio, &fh->prio);
196 if (ret)
197 return ret;
198 }
199
200 switch (cmd) {
201 case VIDIOC_QUERYCAP:
202 {
203 struct v4l2_capability *cap = arg;
204
205 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
206
207 ret = 0;
208 break;
209 }
210
211 case VIDIOC_G_PRIORITY:
212 {
213 enum v4l2_priority *p = arg;
214
215 *p = v4l2_prio_max(&vp->prio);
216 ret = 0;
217 break;
218 }
219
220 case VIDIOC_S_PRIORITY:
221 {
222 enum v4l2_priority *prio = arg;
223
224 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
225 break;
226 }
227
228 case VIDIOC_ENUMSTD:
229 {
230 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
231 int idx = vs->index;
232 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
233 break;
234 }
235
236 case VIDIOC_G_STD:
237 {
238 int val = 0;
239 ret = pvr2_ctrl_get_value(
240 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
241 *(v4l2_std_id *)arg = val;
242 break;
243 }
244
245 case VIDIOC_S_STD:
246 {
247 ret = pvr2_ctrl_set_value(
248 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
249 *(v4l2_std_id *)arg);
250 break;
251 }
252
253 case VIDIOC_ENUMINPUT:
254 {
255 struct pvr2_ctrl *cptr;
256 struct v4l2_input *vi = (struct v4l2_input *)arg;
257 struct v4l2_input tmp;
258 unsigned int cnt;
259
260 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
261
262 memset(&tmp,0,sizeof(tmp));
263 tmp.index = vi->index;
264 ret = 0;
265 switch (vi->index) {
266 case PVR2_CVAL_INPUT_TV:
267 case PVR2_CVAL_INPUT_RADIO:
268 tmp.type = V4L2_INPUT_TYPE_TUNER;
269 break;
270 case PVR2_CVAL_INPUT_SVIDEO:
271 case PVR2_CVAL_INPUT_COMPOSITE:
272 tmp.type = V4L2_INPUT_TYPE_CAMERA;
273 break;
274 default:
275 ret = -EINVAL;
276 break;
277 }
278 if (ret < 0) break;
279
280 cnt = 0;
281 pvr2_ctrl_get_valname(cptr,vi->index,
282 tmp.name,sizeof(tmp.name)-1,&cnt);
283 tmp.name[cnt] = 0;
284
285 /* Don't bother with audioset, since this driver currently
286 always switches the audio whenever the video is
287 switched. */
288
289 /* Handling std is a tougher problem. It doesn't make
290 sense in cases where a device might be multi-standard.
291 We could just copy out the current value for the
292 standard, but it can change over time. For now just
293 leave it zero. */
294
295 memcpy(vi, &tmp, sizeof(tmp));
296
297 ret = 0;
298 break;
299 }
300
301 case VIDIOC_G_INPUT:
302 {
303 struct pvr2_ctrl *cptr;
304 struct v4l2_input *vi = (struct v4l2_input *)arg;
305 int val;
306 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
307 val = 0;
308 ret = pvr2_ctrl_get_value(cptr,&val);
309 vi->index = val;
310 break;
311 }
312
313 case VIDIOC_S_INPUT:
314 {
315 struct v4l2_input *vi = (struct v4l2_input *)arg;
316 ret = pvr2_ctrl_set_value(
317 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
318 vi->index);
319 break;
320 }
321
322 case VIDIOC_ENUMAUDIO:
323 {
324 ret = -EINVAL;
325 break;
326 }
327
328 case VIDIOC_G_AUDIO:
329 {
330 ret = -EINVAL;
331 break;
332 }
333
334 case VIDIOC_S_AUDIO:
335 {
336 ret = -EINVAL;
337 break;
338 }
339 case VIDIOC_G_TUNER:
340 {
341 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
Mike Isely18103c52007-01-20 00:09:47 -0300342 pvr2_hdw_execute_tuner_poll(hdw);
343 ret = pvr2_hdw_get_tuner_status(hdw,vt);
Mike Iselyd8554972006-06-26 20:58:46 -0300344 break;
345 }
346
347 case VIDIOC_S_TUNER:
348 {
349 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
350
351 if (vt->index != 0)
352 break;
353
354 ret = pvr2_ctrl_set_value(
355 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
356 vt->audmode);
357 }
358
359 case VIDIOC_S_FREQUENCY:
360 {
361 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
Mike Iselyc0e69312006-12-27 23:25:06 -0300362 unsigned long fv;
Mike Isely18103c52007-01-20 00:09:47 -0300363 struct v4l2_tuner vt;
364 int cur_input;
365 struct pvr2_ctrl *ctrlp;
366 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
367 if (ret != 0) break;
368 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
369 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
370 if (ret != 0) break;
Mike Iselyc0e69312006-12-27 23:25:06 -0300371 if (vf->type == V4L2_TUNER_RADIO) {
Mike Isely18103c52007-01-20 00:09:47 -0300372 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
373 pvr2_ctrl_set_value(ctrlp,
374 PVR2_CVAL_INPUT_RADIO);
375 }
376 } else {
377 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
378 pvr2_ctrl_set_value(ctrlp,
379 PVR2_CVAL_INPUT_TV);
380 }
381 }
382 fv = vf->frequency;
383 if (vt.capability & V4L2_TUNER_CAP_LOW) {
Mike Iselyc0e69312006-12-27 23:25:06 -0300384 fv = (fv * 125) / 2;
385 } else {
386 fv = fv * 62500;
387 }
Mike Iselyd8554972006-06-26 20:58:46 -0300388 ret = pvr2_ctrl_set_value(
Mike Iselyc0e69312006-12-27 23:25:06 -0300389 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
Mike Iselyd8554972006-06-26 20:58:46 -0300390 break;
391 }
392
393 case VIDIOC_G_FREQUENCY:
394 {
395 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
396 int val = 0;
Mike Isely18103c52007-01-20 00:09:47 -0300397 int cur_input;
398 struct v4l2_tuner vt;
399 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
400 if (ret != 0) break;
Mike Iselyd8554972006-06-26 20:58:46 -0300401 ret = pvr2_ctrl_get_value(
402 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
403 &val);
Mike Iselyc0e69312006-12-27 23:25:06 -0300404 if (ret != 0) break;
405 pvr2_ctrl_get_value(
406 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
407 &cur_input);
408 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
Mike Iselyc0e69312006-12-27 23:25:06 -0300409 vf->type = V4L2_TUNER_RADIO;
410 } else {
Mike Iselyc0e69312006-12-27 23:25:06 -0300411 vf->type = V4L2_TUNER_ANALOG_TV;
412 }
Mike Isely18103c52007-01-20 00:09:47 -0300413 if (vt.capability & V4L2_TUNER_CAP_LOW) {
414 val = (val * 2) / 125;
415 } else {
416 val /= 62500;
417 }
418 vf->frequency = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300419 break;
420 }
421
422 case VIDIOC_ENUM_FMT:
423 {
424 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
425
426 /* Only one format is supported : mpeg.*/
427 if (fd->index != 0)
428 break;
429
430 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
431 ret = 0;
432 break;
433 }
434
435 case VIDIOC_G_FMT:
436 {
437 struct v4l2_format *vf = (struct v4l2_format *)arg;
438 int val;
439 switch(vf->type) {
440 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
441 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
442 sizeof(struct v4l2_format));
443 val = 0;
444 pvr2_ctrl_get_value(
445 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
446 &val);
447 vf->fmt.pix.width = val;
448 val = 0;
449 pvr2_ctrl_get_value(
Mike Iselyd8554972006-06-26 20:58:46 -0300450 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
451 &val);
452 vf->fmt.pix.height = val;
453 ret = 0;
454 break;
455 case V4L2_BUF_TYPE_VBI_CAPTURE:
456 // ????? Still need to figure out to do VBI correctly
457 ret = -EINVAL;
458 break;
459 default:
460 ret = -EINVAL;
461 break;
462 }
463 break;
464 }
465
466 case VIDIOC_TRY_FMT:
467 case VIDIOC_S_FMT:
468 {
469 struct v4l2_format *vf = (struct v4l2_format *)arg;
470
471 ret = 0;
472 switch(vf->type) {
473 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
Mike Iselye95a1912006-08-08 09:10:07 -0300474 int lmin,lmax;
475 struct pvr2_ctrl *hcp,*vcp;
Mike Iselyd8554972006-06-26 20:58:46 -0300476 int h = vf->fmt.pix.height;
477 int w = vf->fmt.pix.width;
Mike Iselye95a1912006-08-08 09:10:07 -0300478 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
479 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
Mike Iselyd8554972006-06-26 20:58:46 -0300480
Mike Iselye95a1912006-08-08 09:10:07 -0300481 lmin = pvr2_ctrl_get_min(hcp);
482 lmax = pvr2_ctrl_get_max(hcp);
Mike Iselye95a1912006-08-08 09:10:07 -0300483 if (w < lmin) {
484 w = lmin;
485 } else if (w > lmax) {
486 w = lmax;
Mike Isely039c4302006-06-25 20:04:16 -0300487 }
Hans Verkuilb31e3412006-09-01 18:36:10 -0300488 lmin = pvr2_ctrl_get_min(vcp);
489 lmax = pvr2_ctrl_get_max(vcp);
490 if (h < lmin) {
491 h = lmin;
492 } else if (h > lmax) {
493 h = lmax;
494 }
Mike Iselyd8554972006-06-26 20:58:46 -0300495
496 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
497 sizeof(struct v4l2_format));
Mike Isely039c4302006-06-25 20:04:16 -0300498 vf->fmt.pix.width = w;
499 vf->fmt.pix.height = h;
Mike Iselyd8554972006-06-26 20:58:46 -0300500
501 if (cmd == VIDIOC_S_FMT) {
Mike Iselye95a1912006-08-08 09:10:07 -0300502 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
503 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
Mike Iselyd8554972006-06-26 20:58:46 -0300504 }
505 } break;
506 case V4L2_BUF_TYPE_VBI_CAPTURE:
507 // ????? Still need to figure out to do VBI correctly
508 ret = -EINVAL;
509 break;
510 default:
511 ret = -EINVAL;
512 break;
513 }
514 break;
515 }
516
517 case VIDIOC_STREAMON:
518 {
Mike Isely16eb40d2006-12-30 18:27:32 -0300519 if (!fh->dev_info->stream) {
520 /* No stream defined for this node. This means
521 that we're not currently allowed to stream from
522 this node. */
523 ret = -EPERM;
524 break;
525 }
Mike Iselyd8554972006-06-26 20:58:46 -0300526 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
527 if (ret < 0) return ret;
528 ret = pvr2_hdw_set_streaming(hdw,!0);
529 break;
530 }
531
532 case VIDIOC_STREAMOFF:
533 {
Mike Isely16eb40d2006-12-30 18:27:32 -0300534 if (!fh->dev_info->stream) {
535 /* No stream defined for this node. This means
536 that we're not currently allowed to stream from
537 this node. */
538 ret = -EPERM;
539 break;
540 }
Mike Iselyd8554972006-06-26 20:58:46 -0300541 ret = pvr2_hdw_set_streaming(hdw,0);
542 break;
543 }
544
545 case VIDIOC_QUERYCTRL:
546 {
547 struct pvr2_ctrl *cptr;
548 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
549 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300550 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
551 cptr = pvr2_hdw_get_ctrl_nextv4l(
552 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
553 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
554 } else {
555 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
556 }
Mike Iselyd8554972006-06-26 20:58:46 -0300557 if (!cptr) {
Mike Isely0885ba12006-06-25 21:30:47 -0300558 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300559 "QUERYCTRL id=0x%x not implemented here",
560 vc->id);
Mike Iselyd8554972006-06-26 20:58:46 -0300561 ret = -EINVAL;
562 break;
563 }
564
Mike Iselya761f432006-06-25 20:04:44 -0300565 pvr2_trace(PVR2_TRACE_V4LIOCTL,
566 "QUERYCTRL id=0x%x mapping name=%s (%s)",
567 vc->id,pvr2_ctrl_get_name(cptr),
568 pvr2_ctrl_get_desc(cptr));
569 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
570 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
Mike Iselyd8554972006-06-26 20:58:46 -0300571 vc->default_value = pvr2_ctrl_get_def(cptr);
572 switch (pvr2_ctrl_get_type(cptr)) {
573 case pvr2_ctl_enum:
574 vc->type = V4L2_CTRL_TYPE_MENU;
575 vc->minimum = 0;
576 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
577 vc->step = 1;
578 break;
Mike Isely33213962006-06-25 20:04:40 -0300579 case pvr2_ctl_bool:
Mike Isely1d9f8462006-06-25 20:04:58 -0300580 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
Mike Isely33213962006-06-25 20:04:40 -0300581 vc->minimum = 0;
582 vc->maximum = 1;
583 vc->step = 1;
584 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300585 case pvr2_ctl_int:
586 vc->type = V4L2_CTRL_TYPE_INTEGER;
587 vc->minimum = pvr2_ctrl_get_min(cptr);
588 vc->maximum = pvr2_ctrl_get_max(cptr);
589 vc->step = 1;
590 break;
591 default:
Mike Isely0885ba12006-06-25 21:30:47 -0300592 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300593 "QUERYCTRL id=0x%x name=%s not mappable",
594 vc->id,pvr2_ctrl_get_name(cptr));
Mike Iselyd8554972006-06-26 20:58:46 -0300595 ret = -EINVAL;
596 break;
597 }
598 break;
599 }
600
601 case VIDIOC_QUERYMENU:
602 {
603 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
604 unsigned int cnt = 0;
605 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
606 vm->index,
607 vm->name,sizeof(vm->name)-1,
608 &cnt);
609 vm->name[cnt] = 0;
610 break;
611 }
612
613 case VIDIOC_G_CTRL:
614 {
615 struct v4l2_control *vc = (struct v4l2_control *)arg;
616 int val = 0;
617 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
618 &val);
619 vc->value = val;
620 break;
621 }
622
623 case VIDIOC_S_CTRL:
624 {
625 struct v4l2_control *vc = (struct v4l2_control *)arg;
626 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
627 vc->value);
628 break;
629 }
630
Mike Isely1d9f8462006-06-25 20:04:58 -0300631 case VIDIOC_G_EXT_CTRLS:
632 {
633 struct v4l2_ext_controls *ctls =
634 (struct v4l2_ext_controls *)arg;
635 struct v4l2_ext_control *ctrl;
636 unsigned int idx;
637 int val;
638 for (idx = 0; idx < ctls->count; idx++) {
639 ctrl = ctls->controls + idx;
640 ret = pvr2_ctrl_get_value(
641 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
642 if (ret) {
643 ctls->error_idx = idx;
644 break;
645 }
646 /* Ensure that if read as a 64 bit value, the user
647 will still get a hopefully sane value */
648 ctrl->value64 = 0;
649 ctrl->value = val;
650 }
651 break;
652 }
653
654 case VIDIOC_S_EXT_CTRLS:
655 {
656 struct v4l2_ext_controls *ctls =
657 (struct v4l2_ext_controls *)arg;
658 struct v4l2_ext_control *ctrl;
659 unsigned int idx;
660 for (idx = 0; idx < ctls->count; idx++) {
661 ctrl = ctls->controls + idx;
662 ret = pvr2_ctrl_set_value(
663 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
664 ctrl->value);
665 if (ret) {
666 ctls->error_idx = idx;
667 break;
668 }
669 }
670 break;
671 }
672
673 case VIDIOC_TRY_EXT_CTRLS:
674 {
675 struct v4l2_ext_controls *ctls =
676 (struct v4l2_ext_controls *)arg;
677 struct v4l2_ext_control *ctrl;
678 struct pvr2_ctrl *pctl;
679 unsigned int idx;
680 /* For the moment just validate that the requested control
681 actually exists. */
682 for (idx = 0; idx < ctls->count; idx++) {
683 ctrl = ctls->controls + idx;
684 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
685 if (!pctl) {
686 ret = -EINVAL;
687 ctls->error_idx = idx;
688 break;
689 }
690 }
691 break;
692 }
693
Mike Iselyd8554972006-06-26 20:58:46 -0300694 case VIDIOC_LOG_STATUS:
695 {
Mike Iselyd8554972006-06-26 20:58:46 -0300696 pvr2_hdw_trigger_module_log(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300697 ret = 0;
698 break;
699 }
Mike Isely32ffa9a2006-09-23 22:26:52 -0300700#ifdef CONFIG_VIDEO_ADV_DEBUG
701 case VIDIOC_INT_G_REGISTER:
702 case VIDIOC_INT_S_REGISTER:
703 {
704 u32 val;
705 struct v4l2_register *req = (struct v4l2_register *)arg;
706 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
707 ret = pvr2_hdw_register_access(
708 hdw,req->i2c_id,req->reg,
709 cmd == VIDIOC_INT_S_REGISTER,&val);
Mike Isely6d988162006-09-28 17:53:49 -0300710 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
Mike Isely32ffa9a2006-09-23 22:26:52 -0300711 break;
712 }
713#endif
Mike Iselyd8554972006-06-26 20:58:46 -0300714
715 default :
716 ret = v4l_compat_translate_ioctl(inode,file,cmd,
717 arg,pvr2_v4l2_do_ioctl);
718 }
719
720 pvr2_hdw_commit_ctl(hdw);
721
722 if (ret < 0) {
723 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
Mike Isely0885ba12006-06-25 21:30:47 -0300724 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300725 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
726 } else {
Mike Isely0885ba12006-06-25 21:30:47 -0300727 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
728 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300729 "pvr2_v4l2_do_ioctl failure, ret=%d"
730 " command was:",ret);
731 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
732 cmd);
733 }
734 }
735 } else {
736 pvr2_trace(PVR2_TRACE_V4LIOCTL,
737 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
738 ret,ret);
739 }
740 return ret;
741}
742
743
744static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
745{
Mike Isely0f0f2572006-12-27 23:19:42 -0300746 int minor_id = dip->devbase.minor;
Mike Isely0f0f2572006-12-27 23:19:42 -0300747 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
Mike Isely16eb40d2006-12-30 18:27:32 -0300748 enum pvr2_config cfg = dip->config;
749 int v4l_type = dip->v4l_type;
Mike Isely0f0f2572006-12-27 23:19:42 -0300750
Mike Isely16eb40d2006-12-30 18:27:32 -0300751 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
Mike Isely75910052006-09-23 22:30:50 -0300752
753 /* Paranoia */
Randy Dunlapc2625bf2006-10-29 11:12:27 -0300754 dip->v4lp = NULL;
755 dip->stream = NULL;
Mike Isely75910052006-09-23 22:30:50 -0300756
757 /* Actual deallocation happens later when all internal references
758 are gone. */
759 video_unregister_device(&dip->devbase);
Mike Isely0f0f2572006-12-27 23:19:42 -0300760
Mike Isely16eb40d2006-12-30 18:27:32 -0300761 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
762 get_v4l_name(v4l_type),minor_id & 0x1f,
763 pvr2_config_get_name(cfg));
Mike Isely0f0f2572006-12-27 23:19:42 -0300764
Mike Iselyd8554972006-06-26 20:58:46 -0300765}
766
767
768static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
769{
Mike Isely0f0f2572006-12-27 23:19:42 -0300770 if (vp->dev_video) {
771 pvr2_v4l2_dev_destroy(vp->dev_video);
772 vp->dev_video = 0;
773 }
774 if (vp->dev_radio) {
775 pvr2_v4l2_dev_destroy(vp->dev_radio);
776 vp->dev_radio = 0;
777 }
Mike Iselyd8554972006-06-26 20:58:46 -0300778
779 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
780 pvr2_channel_done(&vp->channel);
781 kfree(vp);
782}
783
784
Mike Isely75910052006-09-23 22:30:50 -0300785static void pvr2_video_device_release(struct video_device *vdev)
786{
787 struct pvr2_v4l2_dev *dev;
788 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
789 kfree(dev);
790}
791
792
Adrian Bunk07e337e2006-06-30 11:30:20 -0300793static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
Mike Iselyd8554972006-06-26 20:58:46 -0300794{
795 struct pvr2_v4l2 *vp;
796 vp = container_of(chp,struct pvr2_v4l2,channel);
797 if (!vp->channel.mc_head->disconnect_flag) return;
798 if (vp->vfirst) return;
799 pvr2_v4l2_destroy_no_lock(vp);
800}
801
802
Adrian Bunk07e337e2006-06-30 11:30:20 -0300803static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
804 unsigned int cmd, unsigned long arg)
Mike Iselyd8554972006-06-26 20:58:46 -0300805{
806
807/* Temporary hack : use ivtv api until a v4l2 one is available. */
808#define IVTV_IOC_G_CODEC 0xFFEE7703
809#define IVTV_IOC_S_CODEC 0xFFEE7704
810 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
811 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
812}
813
814
Adrian Bunk07e337e2006-06-30 11:30:20 -0300815static int pvr2_v4l2_release(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300816{
817 struct pvr2_v4l2_fh *fhp = file->private_data;
818 struct pvr2_v4l2 *vp = fhp->vhead;
819 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
Mike Iselyc74e0062006-12-30 18:31:22 -0300820 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
Mike Iselyd8554972006-06-26 20:58:46 -0300821
822 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
823
824 if (fhp->rhp) {
825 struct pvr2_stream *sp;
Mike Iselyd8554972006-06-26 20:58:46 -0300826 pvr2_hdw_set_streaming(hdw,0);
827 sp = pvr2_ioread_get_stream(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300828 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300829 pvr2_ioread_destroy(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300830 fhp->rhp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300831 }
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300832
Mike Iselyd8554972006-06-26 20:58:46 -0300833 v4l2_prio_close(&vp->prio, &fhp->prio);
834 file->private_data = NULL;
835
836 pvr2_context_enter(mp); do {
Mike Iselyc74e0062006-12-30 18:31:22 -0300837 /* Restore the previous input selection, if it makes sense
838 to do so. */
839 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
840 struct pvr2_ctrl *cp;
841 int pval;
842 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
843 pvr2_ctrl_get_value(cp,&pval);
844 /* Only restore if we're still selecting the radio */
845 if (pval == PVR2_CVAL_INPUT_RADIO) {
846 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
847 pvr2_hdw_commit_ctl(hdw);
848 }
849 }
850
Mike Iselyd8554972006-06-26 20:58:46 -0300851 if (fhp->vnext) {
852 fhp->vnext->vprev = fhp->vprev;
853 } else {
854 vp->vlast = fhp->vprev;
855 }
856 if (fhp->vprev) {
857 fhp->vprev->vnext = fhp->vnext;
858 } else {
859 vp->vfirst = fhp->vnext;
860 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300861 fhp->vnext = NULL;
862 fhp->vprev = NULL;
863 fhp->vhead = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300864 pvr2_channel_done(&fhp->channel);
865 pvr2_trace(PVR2_TRACE_STRUCT,
866 "Destroying pvr_v4l2_fh id=%p",fhp);
867 kfree(fhp);
868 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
869 pvr2_v4l2_destroy_no_lock(vp);
870 }
871 } while (0); pvr2_context_exit(mp);
872 return 0;
873}
874
875
Adrian Bunk07e337e2006-06-30 11:30:20 -0300876static int pvr2_v4l2_open(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300877{
Mike Isely75910052006-09-23 22:30:50 -0300878 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
Mike Iselyd8554972006-06-26 20:58:46 -0300879 struct pvr2_v4l2_fh *fhp;
880 struct pvr2_v4l2 *vp;
881 struct pvr2_hdw *hdw;
882
Mike Isely75910052006-09-23 22:30:50 -0300883 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300884
885 vp = dip->v4lp;
886 hdw = vp->channel.hdw;
887
888 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
889
890 if (!pvr2_hdw_dev_ok(hdw)) {
891 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
892 "pvr2_v4l2_open: hardware not ready");
893 return -EIO;
894 }
895
Mike Isely4b85dee2007-01-20 00:03:32 -0300896 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300897 if (!fhp) {
898 return -ENOMEM;
899 }
Mike Iselyd8554972006-06-26 20:58:46 -0300900
901 init_waitqueue_head(&fhp->wait_data);
902 fhp->dev_info = dip;
903
904 pvr2_context_enter(vp->channel.mc_head); do {
905 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
906 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300907
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300908 fhp->vnext = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300909 fhp->vprev = vp->vlast;
910 if (vp->vlast) {
911 vp->vlast->vnext = fhp;
912 } else {
913 vp->vfirst = fhp;
914 }
915 vp->vlast = fhp;
916 fhp->vhead = vp;
Mike Iselyc74e0062006-12-30 18:31:22 -0300917
918 /* Opening the /dev/radioX device implies a mode switch.
919 So execute that here. Note that you can get the
920 IDENTICAL effect merely by opening the normal video
921 device and setting the input appropriately. */
922 if (dip->v4l_type == VFL_TYPE_RADIO) {
923 struct pvr2_ctrl *cp;
924 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
925 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
926 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
927 pvr2_hdw_commit_ctl(hdw);
928 }
Mike Iselyd8554972006-06-26 20:58:46 -0300929 } while (0); pvr2_context_exit(vp->channel.mc_head);
930
931 fhp->file = file;
932 file->private_data = fhp;
933 v4l2_prio_open(&vp->prio,&fhp->prio);
934
935 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
936
937 return 0;
938}
939
940
941static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
942{
943 wake_up(&fhp->wait_data);
944}
945
946static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
947{
948 int ret;
949 struct pvr2_stream *sp;
950 struct pvr2_hdw *hdw;
951 if (fh->rhp) return 0;
952
Mike Isely16eb40d2006-12-30 18:27:32 -0300953 if (!fh->dev_info->stream) {
954 /* No stream defined for this node. This means that we're
955 not currently allowed to stream from this node. */
956 return -EPERM;
957 }
958
Mike Iselyd8554972006-06-26 20:58:46 -0300959 /* First read() attempt. Try to claim the stream and start
960 it... */
961 if ((ret = pvr2_channel_claim_stream(&fh->channel,
962 fh->dev_info->stream)) != 0) {
963 /* Someone else must already have it */
964 return ret;
965 }
966
967 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
968 if (!fh->rhp) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300969 pvr2_channel_claim_stream(&fh->channel,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300970 return -ENOMEM;
971 }
972
973 hdw = fh->channel.mc_head->hdw;
974 sp = fh->dev_info->stream->stream;
975 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
976 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
977 pvr2_hdw_set_streaming(hdw,!0);
978 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
979
980 return ret;
981}
982
983
984static ssize_t pvr2_v4l2_read(struct file *file,
985 char __user *buff, size_t count, loff_t *ppos)
986{
987 struct pvr2_v4l2_fh *fh = file->private_data;
988 int ret;
989
990 if (fh->fw_mode_flag) {
991 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
992 char *tbuf;
993 int c1,c2;
994 int tcnt = 0;
995 unsigned int offs = *ppos;
996
997 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
998 if (!tbuf) return -ENOMEM;
999
1000 while (count) {
1001 c1 = count;
1002 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1003 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1004 if (c2 < 0) {
1005 tcnt = c2;
1006 break;
1007 }
1008 if (!c2) break;
1009 if (copy_to_user(buff,tbuf,c2)) {
1010 tcnt = -EFAULT;
1011 break;
1012 }
1013 offs += c2;
1014 tcnt += c2;
1015 buff += c2;
1016 count -= c2;
1017 *ppos += c2;
1018 }
1019 kfree(tbuf);
1020 return tcnt;
1021 }
1022
1023 if (!fh->rhp) {
1024 ret = pvr2_v4l2_iosetup(fh);
1025 if (ret) {
1026 return ret;
1027 }
1028 }
1029
1030 for (;;) {
1031 ret = pvr2_ioread_read(fh->rhp,buff,count);
1032 if (ret >= 0) break;
1033 if (ret != -EAGAIN) break;
1034 if (file->f_flags & O_NONBLOCK) break;
1035 /* Doing blocking I/O. Wait here. */
1036 ret = wait_event_interruptible(
1037 fh->wait_data,
1038 pvr2_ioread_avail(fh->rhp) >= 0);
1039 if (ret < 0) break;
1040 }
1041
1042 return ret;
1043}
1044
1045
1046static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1047{
1048 unsigned int mask = 0;
1049 struct pvr2_v4l2_fh *fh = file->private_data;
1050 int ret;
1051
1052 if (fh->fw_mode_flag) {
1053 mask |= POLLIN | POLLRDNORM;
1054 return mask;
1055 }
1056
1057 if (!fh->rhp) {
1058 ret = pvr2_v4l2_iosetup(fh);
1059 if (ret) return POLLERR;
1060 }
1061
1062 poll_wait(file,&fh->wait_data,wait);
1063
1064 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1065 mask |= POLLIN | POLLRDNORM;
1066 }
1067
1068 return mask;
1069}
1070
1071
Arjan van de Venfa027c22007-02-12 00:55:33 -08001072static const struct file_operations vdev_fops = {
Mike Iselyd8554972006-06-26 20:58:46 -03001073 .owner = THIS_MODULE,
1074 .open = pvr2_v4l2_open,
1075 .release = pvr2_v4l2_release,
1076 .read = pvr2_v4l2_read,
1077 .ioctl = pvr2_v4l2_ioctl,
1078 .llseek = no_llseek,
1079 .poll = pvr2_v4l2_poll,
1080};
1081
1082
1083#define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1084
1085static struct video_device vdev_template = {
1086 .owner = THIS_MODULE,
1087 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1088 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1089 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1090 | V4L2_CAP_READWRITE),
1091 .hardware = VID_HARDWARE_PVRUSB2,
1092 .fops = &vdev_fops,
1093};
1094
1095
1096static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1097 struct pvr2_v4l2 *vp,
Mike Isely16eb40d2006-12-30 18:27:32 -03001098 int v4l_type)
Mike Iselyd8554972006-06-26 20:58:46 -03001099{
1100 int mindevnum;
1101 int unit_number;
Mike Isely16eb40d2006-12-30 18:27:32 -03001102 int *nr_ptr = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001103 dip->v4lp = vp;
Mike Iselyd8554972006-06-26 20:58:46 -03001104
1105
Mike Isely16eb40d2006-12-30 18:27:32 -03001106 dip->v4l_type = v4l_type;
1107 switch (v4l_type) {
1108 case VFL_TYPE_GRABBER:
Mike Iselyd8554972006-06-26 20:58:46 -03001109 dip->stream = &vp->channel.mc_head->video_stream;
Mike Isely16eb40d2006-12-30 18:27:32 -03001110 dip->config = pvr2_config_mpeg;
1111 dip->minor_type = pvr2_v4l_type_video;
1112 nr_ptr = video_nr;
Mike Iselyc74e0062006-12-30 18:31:22 -03001113 if (!dip->stream) {
1114 err("Failed to set up pvrusb2 v4l video dev"
1115 " due to missing stream instance");
1116 return;
1117 }
Mike Iselyd8554972006-06-26 20:58:46 -03001118 break;
Mike Isely16eb40d2006-12-30 18:27:32 -03001119 case VFL_TYPE_VBI:
1120 dip->config = pvr2_config_vbi;
1121 dip->minor_type = pvr2_v4l_type_vbi;
1122 nr_ptr = vbi_nr;
Mike Iselyd8554972006-06-26 20:58:46 -03001123 break;
Mike Isely16eb40d2006-12-30 18:27:32 -03001124 case VFL_TYPE_RADIO:
Mike Iselyaf78a482007-01-20 00:04:31 -03001125 dip->stream = &vp->channel.mc_head->video_stream;
1126 dip->config = pvr2_config_mpeg;
Mike Isely16eb40d2006-12-30 18:27:32 -03001127 dip->minor_type = pvr2_v4l_type_radio;
1128 nr_ptr = radio_nr;
Mike Iselyd8554972006-06-26 20:58:46 -03001129 break;
1130 default:
1131 /* Bail out (this should be impossible) */
1132 err("Failed to set up pvrusb2 v4l dev"
1133 " due to unrecognized config");
1134 return;
1135 }
1136
Mike Isely75910052006-09-23 22:30:50 -03001137 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1138 dip->devbase.release = pvr2_video_device_release;
Mike Iselyd8554972006-06-26 20:58:46 -03001139
1140 mindevnum = -1;
1141 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
Mike Isely16eb40d2006-12-30 18:27:32 -03001142 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1143 mindevnum = nr_ptr[unit_number];
Mike Iselyd8554972006-06-26 20:58:46 -03001144 }
Mike Isely16eb40d2006-12-30 18:27:32 -03001145 if ((video_register_device(&dip->devbase,
1146 dip->v4l_type, mindevnum) < 0) &&
1147 (video_register_device(&dip->devbase,
1148 dip->v4l_type, -1) < 0)) {
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001149 err("Failed to register pvrusb2 v4l device");
1150 }
Mike Isely16eb40d2006-12-30 18:27:32 -03001151
1152 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1153 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1154 pvr2_config_get_name(dip->config));
Mike Iselyd8554972006-06-26 20:58:46 -03001155
Mike Iselyd8554972006-06-26 20:58:46 -03001156 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
Mike Isely16eb40d2006-12-30 18:27:32 -03001157 dip->minor_type,dip->devbase.minor);
Mike Iselyd8554972006-06-26 20:58:46 -03001158}
1159
1160
1161struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1162{
1163 struct pvr2_v4l2 *vp;
1164
Mike Isely4b85dee2007-01-20 00:03:32 -03001165 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001166 if (!vp) return vp;
Mike Isely4b85dee2007-01-20 00:03:32 -03001167 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1168 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
Mike Isely0f0f2572006-12-27 23:19:42 -03001169 if (!(vp->dev_video && vp->dev_radio)) {
Mike Isely6710fb82007-01-20 00:02:26 -03001170 kfree(vp->dev_video);
1171 kfree(vp->dev_radio);
Mike Isely75910052006-09-23 22:30:50 -03001172 kfree(vp);
Randy Dunlapc2625bf2006-10-29 11:12:27 -03001173 return NULL;
Mike Isely75910052006-09-23 22:30:50 -03001174 }
Mike Iselyd8554972006-06-26 20:58:46 -03001175 pvr2_channel_init(&vp->channel,mnp);
1176 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1177
1178 vp->channel.check_func = pvr2_v4l2_internal_check;
1179
1180 /* register streams */
Mike Isely16eb40d2006-12-30 18:27:32 -03001181 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1182 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
Mike Iselyd8554972006-06-26 20:58:46 -03001183
1184 return vp;
1185}
1186
1187/*
1188 Stuff for Emacs to see, in order to encourage consistent editing style:
1189 *** Local Variables: ***
1190 *** mode: c ***
1191 *** fill-column: 75 ***
1192 *** tab-width: 8 ***
1193 *** c-basic-offset: 8 ***
1194 *** End: ***
1195 */