blob: cc3260f37e846a979ae7b333db38814ae96d7e4e [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 Iselyd8554972006-06-26 20:58:46 -030043 enum pvr2_config config;
44};
45
46struct pvr2_v4l2_fh {
47 struct pvr2_channel channel;
48 struct pvr2_v4l2_dev *dev_info;
49 enum v4l2_priority prio;
50 struct pvr2_ioread *rhp;
51 struct file *file;
52 struct pvr2_v4l2 *vhead;
53 struct pvr2_v4l2_fh *vnext;
54 struct pvr2_v4l2_fh *vprev;
55 wait_queue_head_t wait_data;
56 int fw_mode_flag;
57};
58
59struct pvr2_v4l2 {
60 struct pvr2_channel channel;
61 struct pvr2_v4l2_fh *vfirst;
62 struct pvr2_v4l2_fh *vlast;
63
64 struct v4l2_prio_state prio;
65
Mike Isely0f0f2572006-12-27 23:19:42 -030066 /* streams - Note that these must be separately, individually,
67 * allocated pointers. This is because the v4l core is going to
68 * manage their deletion - separately, individually... */
69 struct pvr2_v4l2_dev *dev_video;
70 struct pvr2_v4l2_dev *dev_radio;
Mike Iselyd8554972006-06-26 20:58:46 -030071};
72
73static int video_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
74module_param_array(video_nr, int, NULL, 0444);
Mike Isely5e6862c2006-12-27 23:17:26 -030075MODULE_PARM_DESC(video_nr, "Offset for device's video dev minor");
76static int radio_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
77module_param_array(radio_nr, int, NULL, 0444);
78MODULE_PARM_DESC(radio_nr, "Offset for device's radio dev minor");
79static int vbi_nr[PVR_NUM] = {[0 ... PVR_NUM-1] = -1};
80module_param_array(vbi_nr, int, NULL, 0444);
81MODULE_PARM_DESC(vbi_nr, "Offset for device's vbi dev minor");
Mike Iselyd8554972006-06-26 20:58:46 -030082
Adrian Bunk07e337e2006-06-30 11:30:20 -030083static struct v4l2_capability pvr_capability ={
Mike Iselyd8554972006-06-26 20:58:46 -030084 .driver = "pvrusb2",
85 .card = "Hauppauge WinTV pvr-usb2",
86 .bus_info = "usb",
87 .version = KERNEL_VERSION(0,8,0),
88 .capabilities = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE |
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -030089 V4L2_CAP_TUNER | V4L2_CAP_AUDIO | V4L2_CAP_RADIO |
Mike Iselyd8554972006-06-26 20:58:46 -030090 V4L2_CAP_READWRITE),
91 .reserved = {0,0,0,0}
92};
93
94static struct v4l2_tuner pvr_v4l2_tuners[]= {
95 {
96 .index = 0,
97 .name = "TV Tuner",
98 .type = V4L2_TUNER_ANALOG_TV,
99 .capability = (V4L2_TUNER_CAP_NORM |
100 V4L2_TUNER_CAP_STEREO |
101 V4L2_TUNER_CAP_LANG1 |
102 V4L2_TUNER_CAP_LANG2),
103 .rangelow = 0,
104 .rangehigh = 0,
105 .rxsubchans = V4L2_TUNER_SUB_STEREO,
106 .audmode = V4L2_TUNER_MODE_STEREO,
107 .signal = 0,
108 .afc = 0,
109 .reserved = {0,0,0,0}
110 }
111};
112
Adrian Bunk07e337e2006-06-30 11:30:20 -0300113static struct v4l2_fmtdesc pvr_fmtdesc [] = {
Mike Iselyd8554972006-06-26 20:58:46 -0300114 {
115 .index = 0,
116 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
117 .flags = V4L2_FMT_FLAG_COMPRESSED,
118 .description = "MPEG1/2",
119 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
120 // breaks when I do that.
121 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
122 .reserved = { 0, 0, 0, 0 }
123 }
124};
125
126#define PVR_FORMAT_PIX 0
127#define PVR_FORMAT_VBI 1
128
Adrian Bunk07e337e2006-06-30 11:30:20 -0300129static struct v4l2_format pvr_format [] = {
Mike Iselyd8554972006-06-26 20:58:46 -0300130 [PVR_FORMAT_PIX] = {
131 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
132 .fmt = {
133 .pix = {
134 .width = 720,
135 .height = 576,
136 // This should really be V4L2_PIX_FMT_MPEG,
137 // but xawtv breaks when I do that.
138 .pixelformat = 0, // V4L2_PIX_FMT_MPEG,
139 .field = V4L2_FIELD_INTERLACED,
140 .bytesperline = 0, // doesn't make sense
141 // here
142 //FIXME : Don't know what to put here...
143 .sizeimage = (32*1024),
144 .colorspace = 0, // doesn't make sense here
145 .priv = 0
146 }
147 }
148 },
149 [PVR_FORMAT_VBI] = {
150 .type = V4L2_BUF_TYPE_VBI_CAPTURE,
151 .fmt = {
152 .vbi = {
153 .sampling_rate = 27000000,
154 .offset = 248,
155 .samples_per_line = 1443,
156 .sample_format = V4L2_PIX_FMT_GREY,
157 .start = { 0, 0 },
158 .count = { 0, 0 },
159 .flags = 0,
160 .reserved = { 0, 0 }
161 }
162 }
163 }
164};
165
166/*
167 * pvr_ioctl()
168 *
169 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
170 *
171 */
172static int pvr2_v4l2_do_ioctl(struct inode *inode, struct file *file,
173 unsigned int cmd, void *arg)
174{
175 struct pvr2_v4l2_fh *fh = file->private_data;
176 struct pvr2_v4l2 *vp = fh->vhead;
177 struct pvr2_v4l2_dev *dev_info = fh->dev_info;
178 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
179 int ret = -EINVAL;
180
181 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
182 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),cmd);
183 }
184
185 if (!pvr2_hdw_dev_ok(hdw)) {
186 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
187 "ioctl failed - bad or no context");
188 return -EFAULT;
189 }
190
191 /* check priority */
192 switch (cmd) {
193 case VIDIOC_S_CTRL:
194 case VIDIOC_S_STD:
195 case VIDIOC_S_INPUT:
196 case VIDIOC_S_TUNER:
197 case VIDIOC_S_FREQUENCY:
198 ret = v4l2_prio_check(&vp->prio, &fh->prio);
199 if (ret)
200 return ret;
201 }
202
203 switch (cmd) {
204 case VIDIOC_QUERYCAP:
205 {
206 struct v4l2_capability *cap = arg;
207
208 memcpy(cap, &pvr_capability, sizeof(struct v4l2_capability));
209
210 ret = 0;
211 break;
212 }
213
214 case VIDIOC_G_PRIORITY:
215 {
216 enum v4l2_priority *p = arg;
217
218 *p = v4l2_prio_max(&vp->prio);
219 ret = 0;
220 break;
221 }
222
223 case VIDIOC_S_PRIORITY:
224 {
225 enum v4l2_priority *prio = arg;
226
227 ret = v4l2_prio_change(&vp->prio, &fh->prio, *prio);
228 break;
229 }
230
231 case VIDIOC_ENUMSTD:
232 {
233 struct v4l2_standard *vs = (struct v4l2_standard *)arg;
234 int idx = vs->index;
235 ret = pvr2_hdw_get_stdenum_value(hdw,vs,idx+1);
236 break;
237 }
238
239 case VIDIOC_G_STD:
240 {
241 int val = 0;
242 ret = pvr2_ctrl_get_value(
243 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),&val);
244 *(v4l2_std_id *)arg = val;
245 break;
246 }
247
248 case VIDIOC_S_STD:
249 {
250 ret = pvr2_ctrl_set_value(
251 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_STDCUR),
252 *(v4l2_std_id *)arg);
253 break;
254 }
255
256 case VIDIOC_ENUMINPUT:
257 {
258 struct pvr2_ctrl *cptr;
259 struct v4l2_input *vi = (struct v4l2_input *)arg;
260 struct v4l2_input tmp;
261 unsigned int cnt;
262
263 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
264
265 memset(&tmp,0,sizeof(tmp));
266 tmp.index = vi->index;
267 ret = 0;
268 switch (vi->index) {
269 case PVR2_CVAL_INPUT_TV:
270 case PVR2_CVAL_INPUT_RADIO:
271 tmp.type = V4L2_INPUT_TYPE_TUNER;
272 break;
273 case PVR2_CVAL_INPUT_SVIDEO:
274 case PVR2_CVAL_INPUT_COMPOSITE:
275 tmp.type = V4L2_INPUT_TYPE_CAMERA;
276 break;
277 default:
278 ret = -EINVAL;
279 break;
280 }
281 if (ret < 0) break;
282
283 cnt = 0;
284 pvr2_ctrl_get_valname(cptr,vi->index,
285 tmp.name,sizeof(tmp.name)-1,&cnt);
286 tmp.name[cnt] = 0;
287
288 /* Don't bother with audioset, since this driver currently
289 always switches the audio whenever the video is
290 switched. */
291
292 /* Handling std is a tougher problem. It doesn't make
293 sense in cases where a device might be multi-standard.
294 We could just copy out the current value for the
295 standard, but it can change over time. For now just
296 leave it zero. */
297
298 memcpy(vi, &tmp, sizeof(tmp));
299
300 ret = 0;
301 break;
302 }
303
304 case VIDIOC_G_INPUT:
305 {
306 struct pvr2_ctrl *cptr;
307 struct v4l2_input *vi = (struct v4l2_input *)arg;
308 int val;
309 cptr = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
310 val = 0;
311 ret = pvr2_ctrl_get_value(cptr,&val);
312 vi->index = val;
313 break;
314 }
315
316 case VIDIOC_S_INPUT:
317 {
318 struct v4l2_input *vi = (struct v4l2_input *)arg;
319 ret = pvr2_ctrl_set_value(
320 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
321 vi->index);
322 break;
323 }
324
325 case VIDIOC_ENUMAUDIO:
326 {
327 ret = -EINVAL;
328 break;
329 }
330
331 case VIDIOC_G_AUDIO:
332 {
333 ret = -EINVAL;
334 break;
335 }
336
337 case VIDIOC_S_AUDIO:
338 {
339 ret = -EINVAL;
340 break;
341 }
342 case VIDIOC_G_TUNER:
343 {
344 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
345 unsigned int status_mask;
346 int val;
347 if (vt->index !=0) break;
348
349 status_mask = pvr2_hdw_get_signal_status(hdw);
350
351 memcpy(vt, &pvr_v4l2_tuners[vt->index],
352 sizeof(struct v4l2_tuner));
353
354 vt->signal = 0;
355 if (status_mask & PVR2_SIGNAL_OK) {
356 if (status_mask & PVR2_SIGNAL_STEREO) {
357 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
358 } else {
359 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
360 }
361 if (status_mask & PVR2_SIGNAL_SAP) {
362 vt->rxsubchans |= (V4L2_TUNER_SUB_LANG1 |
363 V4L2_TUNER_SUB_LANG2);
364 }
365 vt->signal = 65535;
366 }
367
368 val = 0;
369 ret = pvr2_ctrl_get_value(
370 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
371 &val);
372 vt->audmode = val;
373 break;
374 }
375
376 case VIDIOC_S_TUNER:
377 {
378 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
379
380 if (vt->index != 0)
381 break;
382
383 ret = pvr2_ctrl_set_value(
384 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
385 vt->audmode);
386 }
387
388 case VIDIOC_S_FREQUENCY:
389 {
390 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
Mike Iselyc0e69312006-12-27 23:25:06 -0300391 unsigned long fv;
392 fv = vf->frequency;
393 if (vf->type == V4L2_TUNER_RADIO) {
394 fv = (fv * 125) / 2;
395 } else {
396 fv = fv * 62500;
397 }
Mike Iselyd8554972006-06-26 20:58:46 -0300398 ret = pvr2_ctrl_set_value(
Mike Iselyc0e69312006-12-27 23:25:06 -0300399 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
Mike Iselyd8554972006-06-26 20:58:46 -0300400 break;
401 }
402
403 case VIDIOC_G_FREQUENCY:
404 {
405 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
406 int val = 0;
Mike Iselyc0e69312006-12-27 23:25:06 -0300407 int cur_input = PVR2_CVAL_INPUT_TV;
Mike Iselyd8554972006-06-26 20:58:46 -0300408 ret = pvr2_ctrl_get_value(
409 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
410 &val);
Mike Iselyc0e69312006-12-27 23:25:06 -0300411 if (ret != 0) break;
412 pvr2_ctrl_get_value(
413 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
414 &cur_input);
415 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
416 val = (val * 2) / 125;
417 vf->frequency = val;
418 vf->type = V4L2_TUNER_RADIO;
419 } else {
420 val /= 62500;
421 vf->frequency = val;
422 vf->type = V4L2_TUNER_ANALOG_TV;
423 }
Mike Iselyd8554972006-06-26 20:58:46 -0300424 break;
425 }
426
427 case VIDIOC_ENUM_FMT:
428 {
429 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
430
431 /* Only one format is supported : mpeg.*/
432 if (fd->index != 0)
433 break;
434
435 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
436 ret = 0;
437 break;
438 }
439
440 case VIDIOC_G_FMT:
441 {
442 struct v4l2_format *vf = (struct v4l2_format *)arg;
443 int val;
444 switch(vf->type) {
445 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
446 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
447 sizeof(struct v4l2_format));
448 val = 0;
449 pvr2_ctrl_get_value(
450 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
451 &val);
452 vf->fmt.pix.width = val;
453 val = 0;
454 pvr2_ctrl_get_value(
Mike Iselyd8554972006-06-26 20:58:46 -0300455 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
456 &val);
457 vf->fmt.pix.height = val;
458 ret = 0;
459 break;
460 case V4L2_BUF_TYPE_VBI_CAPTURE:
461 // ????? Still need to figure out to do VBI correctly
462 ret = -EINVAL;
463 break;
464 default:
465 ret = -EINVAL;
466 break;
467 }
468 break;
469 }
470
471 case VIDIOC_TRY_FMT:
472 case VIDIOC_S_FMT:
473 {
474 struct v4l2_format *vf = (struct v4l2_format *)arg;
475
476 ret = 0;
477 switch(vf->type) {
478 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
Mike Iselye95a1912006-08-08 09:10:07 -0300479 int lmin,lmax;
480 struct pvr2_ctrl *hcp,*vcp;
Mike Iselyd8554972006-06-26 20:58:46 -0300481 int h = vf->fmt.pix.height;
482 int w = vf->fmt.pix.width;
Mike Iselye95a1912006-08-08 09:10:07 -0300483 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
484 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
Mike Iselyd8554972006-06-26 20:58:46 -0300485
Mike Iselye95a1912006-08-08 09:10:07 -0300486 lmin = pvr2_ctrl_get_min(hcp);
487 lmax = pvr2_ctrl_get_max(hcp);
Mike Iselye95a1912006-08-08 09:10:07 -0300488 if (w < lmin) {
489 w = lmin;
490 } else if (w > lmax) {
491 w = lmax;
Mike Isely039c4302006-06-25 20:04:16 -0300492 }
Hans Verkuilb31e3412006-09-01 18:36:10 -0300493 lmin = pvr2_ctrl_get_min(vcp);
494 lmax = pvr2_ctrl_get_max(vcp);
495 if (h < lmin) {
496 h = lmin;
497 } else if (h > lmax) {
498 h = lmax;
499 }
Mike Iselyd8554972006-06-26 20:58:46 -0300500
501 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
502 sizeof(struct v4l2_format));
Mike Isely039c4302006-06-25 20:04:16 -0300503 vf->fmt.pix.width = w;
504 vf->fmt.pix.height = h;
Mike Iselyd8554972006-06-26 20:58:46 -0300505
506 if (cmd == VIDIOC_S_FMT) {
Mike Iselye95a1912006-08-08 09:10:07 -0300507 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
508 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
Mike Iselyd8554972006-06-26 20:58:46 -0300509 }
510 } break;
511 case V4L2_BUF_TYPE_VBI_CAPTURE:
512 // ????? Still need to figure out to do VBI correctly
513 ret = -EINVAL;
514 break;
515 default:
516 ret = -EINVAL;
517 break;
518 }
519 break;
520 }
521
522 case VIDIOC_STREAMON:
523 {
524 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
525 if (ret < 0) return ret;
526 ret = pvr2_hdw_set_streaming(hdw,!0);
527 break;
528 }
529
530 case VIDIOC_STREAMOFF:
531 {
532 ret = pvr2_hdw_set_streaming(hdw,0);
533 break;
534 }
535
536 case VIDIOC_QUERYCTRL:
537 {
538 struct pvr2_ctrl *cptr;
539 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
540 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300541 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
542 cptr = pvr2_hdw_get_ctrl_nextv4l(
543 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
544 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
545 } else {
546 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
547 }
Mike Iselyd8554972006-06-26 20:58:46 -0300548 if (!cptr) {
Mike Isely0885ba12006-06-25 21:30:47 -0300549 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300550 "QUERYCTRL id=0x%x not implemented here",
551 vc->id);
Mike Iselyd8554972006-06-26 20:58:46 -0300552 ret = -EINVAL;
553 break;
554 }
555
Mike Iselya761f432006-06-25 20:04:44 -0300556 pvr2_trace(PVR2_TRACE_V4LIOCTL,
557 "QUERYCTRL id=0x%x mapping name=%s (%s)",
558 vc->id,pvr2_ctrl_get_name(cptr),
559 pvr2_ctrl_get_desc(cptr));
560 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
561 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
Mike Iselyd8554972006-06-26 20:58:46 -0300562 vc->default_value = pvr2_ctrl_get_def(cptr);
563 switch (pvr2_ctrl_get_type(cptr)) {
564 case pvr2_ctl_enum:
565 vc->type = V4L2_CTRL_TYPE_MENU;
566 vc->minimum = 0;
567 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
568 vc->step = 1;
569 break;
Mike Isely33213962006-06-25 20:04:40 -0300570 case pvr2_ctl_bool:
Mike Isely1d9f8462006-06-25 20:04:58 -0300571 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
Mike Isely33213962006-06-25 20:04:40 -0300572 vc->minimum = 0;
573 vc->maximum = 1;
574 vc->step = 1;
575 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300576 case pvr2_ctl_int:
577 vc->type = V4L2_CTRL_TYPE_INTEGER;
578 vc->minimum = pvr2_ctrl_get_min(cptr);
579 vc->maximum = pvr2_ctrl_get_max(cptr);
580 vc->step = 1;
581 break;
582 default:
Mike Isely0885ba12006-06-25 21:30:47 -0300583 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300584 "QUERYCTRL id=0x%x name=%s not mappable",
585 vc->id,pvr2_ctrl_get_name(cptr));
Mike Iselyd8554972006-06-26 20:58:46 -0300586 ret = -EINVAL;
587 break;
588 }
589 break;
590 }
591
592 case VIDIOC_QUERYMENU:
593 {
594 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
595 unsigned int cnt = 0;
596 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
597 vm->index,
598 vm->name,sizeof(vm->name)-1,
599 &cnt);
600 vm->name[cnt] = 0;
601 break;
602 }
603
604 case VIDIOC_G_CTRL:
605 {
606 struct v4l2_control *vc = (struct v4l2_control *)arg;
607 int val = 0;
608 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
609 &val);
610 vc->value = val;
611 break;
612 }
613
614 case VIDIOC_S_CTRL:
615 {
616 struct v4l2_control *vc = (struct v4l2_control *)arg;
617 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
618 vc->value);
619 break;
620 }
621
Mike Isely1d9f8462006-06-25 20:04:58 -0300622 case VIDIOC_G_EXT_CTRLS:
623 {
624 struct v4l2_ext_controls *ctls =
625 (struct v4l2_ext_controls *)arg;
626 struct v4l2_ext_control *ctrl;
627 unsigned int idx;
628 int val;
629 for (idx = 0; idx < ctls->count; idx++) {
630 ctrl = ctls->controls + idx;
631 ret = pvr2_ctrl_get_value(
632 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
633 if (ret) {
634 ctls->error_idx = idx;
635 break;
636 }
637 /* Ensure that if read as a 64 bit value, the user
638 will still get a hopefully sane value */
639 ctrl->value64 = 0;
640 ctrl->value = val;
641 }
642 break;
643 }
644
645 case VIDIOC_S_EXT_CTRLS:
646 {
647 struct v4l2_ext_controls *ctls =
648 (struct v4l2_ext_controls *)arg;
649 struct v4l2_ext_control *ctrl;
650 unsigned int idx;
651 for (idx = 0; idx < ctls->count; idx++) {
652 ctrl = ctls->controls + idx;
653 ret = pvr2_ctrl_set_value(
654 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
655 ctrl->value);
656 if (ret) {
657 ctls->error_idx = idx;
658 break;
659 }
660 }
661 break;
662 }
663
664 case VIDIOC_TRY_EXT_CTRLS:
665 {
666 struct v4l2_ext_controls *ctls =
667 (struct v4l2_ext_controls *)arg;
668 struct v4l2_ext_control *ctrl;
669 struct pvr2_ctrl *pctl;
670 unsigned int idx;
671 /* For the moment just validate that the requested control
672 actually exists. */
673 for (idx = 0; idx < ctls->count; idx++) {
674 ctrl = ctls->controls + idx;
675 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
676 if (!pctl) {
677 ret = -EINVAL;
678 ctls->error_idx = idx;
679 break;
680 }
681 }
682 break;
683 }
684
Mike Iselyd8554972006-06-26 20:58:46 -0300685 case VIDIOC_LOG_STATUS:
686 {
Mike Iselyd8554972006-06-26 20:58:46 -0300687 pvr2_hdw_trigger_module_log(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300688 ret = 0;
689 break;
690 }
Mike Isely32ffa9a2006-09-23 22:26:52 -0300691#ifdef CONFIG_VIDEO_ADV_DEBUG
692 case VIDIOC_INT_G_REGISTER:
693 case VIDIOC_INT_S_REGISTER:
694 {
695 u32 val;
696 struct v4l2_register *req = (struct v4l2_register *)arg;
697 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
698 ret = pvr2_hdw_register_access(
699 hdw,req->i2c_id,req->reg,
700 cmd == VIDIOC_INT_S_REGISTER,&val);
Mike Isely6d988162006-09-28 17:53:49 -0300701 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
Mike Isely32ffa9a2006-09-23 22:26:52 -0300702 break;
703 }
704#endif
Mike Iselyd8554972006-06-26 20:58:46 -0300705
706 default :
707 ret = v4l_compat_translate_ioctl(inode,file,cmd,
708 arg,pvr2_v4l2_do_ioctl);
709 }
710
711 pvr2_hdw_commit_ctl(hdw);
712
713 if (ret < 0) {
714 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
Mike Isely0885ba12006-06-25 21:30:47 -0300715 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300716 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
717 } else {
Mike Isely0885ba12006-06-25 21:30:47 -0300718 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
719 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300720 "pvr2_v4l2_do_ioctl failure, ret=%d"
721 " command was:",ret);
722 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
723 cmd);
724 }
725 }
726 } else {
727 pvr2_trace(PVR2_TRACE_V4LIOCTL,
728 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
729 ret,ret);
730 }
731 return ret;
732}
733
734
735static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
736{
Mike Isely0f0f2572006-12-27 23:19:42 -0300737 enum pvr2_config cfg = dip->config;
738 int minor_id = dip->devbase.minor;
739 enum pvr2_v4l_type pvt;
740 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
741
742 switch (cfg) {
743 case pvr2_config_mpeg:
744 pvt = pvr2_v4l_type_video;
745 break;
746 case pvr2_config_vbi:
747 pvt = pvr2_v4l_type_vbi;
748 break;
749 case pvr2_config_radio:
750 pvt = pvr2_v4l_type_radio;
751 break;
752 default: /* paranoia */
753 pvt = pvr2_v4l_type_video;
754 break;
755 }
756 pvr2_hdw_v4l_store_minor_number(hdw,pvt,-1);
Mike Isely75910052006-09-23 22:30:50 -0300757
758 /* Paranoia */
Randy Dunlapc2625bf2006-10-29 11:12:27 -0300759 dip->v4lp = NULL;
760 dip->stream = NULL;
Mike Isely75910052006-09-23 22:30:50 -0300761
762 /* Actual deallocation happens later when all internal references
763 are gone. */
764 video_unregister_device(&dip->devbase);
Mike Isely0f0f2572006-12-27 23:19:42 -0300765
766 switch (cfg) {
767 case pvr2_config_mpeg:
768 printk(KERN_INFO "pvrusb2: unregistered device video%d [%s]\n",
769 minor_id & 0x1f,
770 pvr2_config_get_name(cfg));
771 break;
772 case pvr2_config_radio:
773 printk(KERN_INFO "pvrusb2: unregistered device radio%d [%s]\n",
774 minor_id & 0x1f,
775 pvr2_config_get_name(cfg));
776 break;
777 case pvr2_config_vbi:
778 printk(KERN_INFO "pvrusb2: unregistered device vbi%d [%s]\n",
779 minor_id & 0x1f,
780 pvr2_config_get_name(cfg));
781 break;
782 default:
783 break;
784 }
785
Mike Iselyd8554972006-06-26 20:58:46 -0300786}
787
788
789static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
790{
Mike Isely0f0f2572006-12-27 23:19:42 -0300791 if (vp->dev_video) {
792 pvr2_v4l2_dev_destroy(vp->dev_video);
793 vp->dev_video = 0;
794 }
795 if (vp->dev_radio) {
796 pvr2_v4l2_dev_destroy(vp->dev_radio);
797 vp->dev_radio = 0;
798 }
Mike Iselyd8554972006-06-26 20:58:46 -0300799
800 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
801 pvr2_channel_done(&vp->channel);
802 kfree(vp);
803}
804
805
Mike Isely75910052006-09-23 22:30:50 -0300806static void pvr2_video_device_release(struct video_device *vdev)
807{
808 struct pvr2_v4l2_dev *dev;
809 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
810 kfree(dev);
811}
812
813
Adrian Bunk07e337e2006-06-30 11:30:20 -0300814static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
Mike Iselyd8554972006-06-26 20:58:46 -0300815{
816 struct pvr2_v4l2 *vp;
817 vp = container_of(chp,struct pvr2_v4l2,channel);
818 if (!vp->channel.mc_head->disconnect_flag) return;
819 if (vp->vfirst) return;
820 pvr2_v4l2_destroy_no_lock(vp);
821}
822
823
Adrian Bunk07e337e2006-06-30 11:30:20 -0300824static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
825 unsigned int cmd, unsigned long arg)
Mike Iselyd8554972006-06-26 20:58:46 -0300826{
827
828/* Temporary hack : use ivtv api until a v4l2 one is available. */
829#define IVTV_IOC_G_CODEC 0xFFEE7703
830#define IVTV_IOC_S_CODEC 0xFFEE7704
831 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
832 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
833}
834
835
Adrian Bunk07e337e2006-06-30 11:30:20 -0300836static int pvr2_v4l2_release(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300837{
838 struct pvr2_v4l2_fh *fhp = file->private_data;
839 struct pvr2_v4l2 *vp = fhp->vhead;
840 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
841
842 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
843
844 if (fhp->rhp) {
845 struct pvr2_stream *sp;
846 struct pvr2_hdw *hdw;
847 hdw = fhp->channel.mc_head->hdw;
848 pvr2_hdw_set_streaming(hdw,0);
849 sp = pvr2_ioread_get_stream(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300850 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300851 pvr2_ioread_destroy(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300852 fhp->rhp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300853 }
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300854
855 if (fhp->dev_info->config == pvr2_config_radio) {
856 int ret;
857 struct pvr2_hdw *hdw;
858 hdw = fhp->channel.mc_head->hdw;
859 if ((ret = pvr2_ctrl_set_value(
860 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
861 PVR2_CVAL_INPUT_TV))) {
862 return ret;
863 }
864 }
865
Mike Iselyd8554972006-06-26 20:58:46 -0300866 v4l2_prio_close(&vp->prio, &fhp->prio);
867 file->private_data = NULL;
868
869 pvr2_context_enter(mp); do {
870 if (fhp->vnext) {
871 fhp->vnext->vprev = fhp->vprev;
872 } else {
873 vp->vlast = fhp->vprev;
874 }
875 if (fhp->vprev) {
876 fhp->vprev->vnext = fhp->vnext;
877 } else {
878 vp->vfirst = fhp->vnext;
879 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300880 fhp->vnext = NULL;
881 fhp->vprev = NULL;
882 fhp->vhead = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300883 pvr2_channel_done(&fhp->channel);
884 pvr2_trace(PVR2_TRACE_STRUCT,
885 "Destroying pvr_v4l2_fh id=%p",fhp);
886 kfree(fhp);
887 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
888 pvr2_v4l2_destroy_no_lock(vp);
889 }
890 } while (0); pvr2_context_exit(mp);
891 return 0;
892}
893
894
Adrian Bunk07e337e2006-06-30 11:30:20 -0300895static int pvr2_v4l2_open(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300896{
Mike Isely75910052006-09-23 22:30:50 -0300897 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
Mike Iselyd8554972006-06-26 20:58:46 -0300898 struct pvr2_v4l2_fh *fhp;
899 struct pvr2_v4l2 *vp;
900 struct pvr2_hdw *hdw;
901
Mike Isely75910052006-09-23 22:30:50 -0300902 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300903
904 vp = dip->v4lp;
905 hdw = vp->channel.hdw;
906
907 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
908
909 if (!pvr2_hdw_dev_ok(hdw)) {
910 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
911 "pvr2_v4l2_open: hardware not ready");
912 return -EIO;
913 }
914
915 fhp = kmalloc(sizeof(*fhp),GFP_KERNEL);
916 if (!fhp) {
917 return -ENOMEM;
918 }
919 memset(fhp,0,sizeof(*fhp));
920
921 init_waitqueue_head(&fhp->wait_data);
922 fhp->dev_info = dip;
923
924 pvr2_context_enter(vp->channel.mc_head); do {
925 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
926 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300927
Mike Isely62f5fda2006-12-27 23:33:00 -0300928 /* Opening the /dev/radioX device implies a mode switch.
929 So execute that here. Note that you can get the
930 IDENTICAL effect merely by opening the normal video
931 device and setting the input appropriately. */
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300932 if (dip->config == pvr2_config_radio) {
Mike Isely62f5fda2006-12-27 23:33:00 -0300933 pvr2_ctrl_set_value(
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300934 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
Mike Isely62f5fda2006-12-27 23:33:00 -0300935 PVR2_CVAL_INPUT_RADIO);
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300936 }
937
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300938 fhp->vnext = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300939 fhp->vprev = vp->vlast;
940 if (vp->vlast) {
941 vp->vlast->vnext = fhp;
942 } else {
943 vp->vfirst = fhp;
944 }
945 vp->vlast = fhp;
946 fhp->vhead = vp;
947 } while (0); pvr2_context_exit(vp->channel.mc_head);
948
949 fhp->file = file;
950 file->private_data = fhp;
951 v4l2_prio_open(&vp->prio,&fhp->prio);
952
953 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
954
955 return 0;
956}
957
958
959static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
960{
961 wake_up(&fhp->wait_data);
962}
963
964static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
965{
966 int ret;
967 struct pvr2_stream *sp;
968 struct pvr2_hdw *hdw;
969 if (fh->rhp) return 0;
970
971 /* First read() attempt. Try to claim the stream and start
972 it... */
973 if ((ret = pvr2_channel_claim_stream(&fh->channel,
974 fh->dev_info->stream)) != 0) {
975 /* Someone else must already have it */
976 return ret;
977 }
978
979 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
980 if (!fh->rhp) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300981 pvr2_channel_claim_stream(&fh->channel,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300982 return -ENOMEM;
983 }
984
985 hdw = fh->channel.mc_head->hdw;
986 sp = fh->dev_info->stream->stream;
987 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
988 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
989 pvr2_hdw_set_streaming(hdw,!0);
990 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
991
992 return ret;
993}
994
995
996static ssize_t pvr2_v4l2_read(struct file *file,
997 char __user *buff, size_t count, loff_t *ppos)
998{
999 struct pvr2_v4l2_fh *fh = file->private_data;
1000 int ret;
1001
1002 if (fh->fw_mode_flag) {
1003 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1004 char *tbuf;
1005 int c1,c2;
1006 int tcnt = 0;
1007 unsigned int offs = *ppos;
1008
1009 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1010 if (!tbuf) return -ENOMEM;
1011
1012 while (count) {
1013 c1 = count;
1014 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1015 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1016 if (c2 < 0) {
1017 tcnt = c2;
1018 break;
1019 }
1020 if (!c2) break;
1021 if (copy_to_user(buff,tbuf,c2)) {
1022 tcnt = -EFAULT;
1023 break;
1024 }
1025 offs += c2;
1026 tcnt += c2;
1027 buff += c2;
1028 count -= c2;
1029 *ppos += c2;
1030 }
1031 kfree(tbuf);
1032 return tcnt;
1033 }
1034
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001035 if (fh->dev_info->config == pvr2_config_radio) {
1036 /* Radio device nodes on this device
1037 cannot be read or written. */
1038 return -EPERM;
1039 }
1040
Mike Iselyd8554972006-06-26 20:58:46 -03001041 if (!fh->rhp) {
1042 ret = pvr2_v4l2_iosetup(fh);
1043 if (ret) {
1044 return ret;
1045 }
1046 }
1047
1048 for (;;) {
1049 ret = pvr2_ioread_read(fh->rhp,buff,count);
1050 if (ret >= 0) break;
1051 if (ret != -EAGAIN) break;
1052 if (file->f_flags & O_NONBLOCK) break;
1053 /* Doing blocking I/O. Wait here. */
1054 ret = wait_event_interruptible(
1055 fh->wait_data,
1056 pvr2_ioread_avail(fh->rhp) >= 0);
1057 if (ret < 0) break;
1058 }
1059
1060 return ret;
1061}
1062
1063
1064static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1065{
1066 unsigned int mask = 0;
1067 struct pvr2_v4l2_fh *fh = file->private_data;
1068 int ret;
1069
1070 if (fh->fw_mode_flag) {
1071 mask |= POLLIN | POLLRDNORM;
1072 return mask;
1073 }
1074
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001075 if (fh->dev_info->config == pvr2_config_radio) {
1076 /* Radio device nodes on this device
1077 cannot be read or written. */
1078 return -EPERM;
1079 }
1080
Mike Iselyd8554972006-06-26 20:58:46 -03001081 if (!fh->rhp) {
1082 ret = pvr2_v4l2_iosetup(fh);
1083 if (ret) return POLLERR;
1084 }
1085
1086 poll_wait(file,&fh->wait_data,wait);
1087
1088 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1089 mask |= POLLIN | POLLRDNORM;
1090 }
1091
1092 return mask;
1093}
1094
1095
Arjan van de Venfa027c22007-02-12 00:55:33 -08001096static const struct file_operations vdev_fops = {
Mike Iselyd8554972006-06-26 20:58:46 -03001097 .owner = THIS_MODULE,
1098 .open = pvr2_v4l2_open,
1099 .release = pvr2_v4l2_release,
1100 .read = pvr2_v4l2_read,
1101 .ioctl = pvr2_v4l2_ioctl,
1102 .llseek = no_llseek,
1103 .poll = pvr2_v4l2_poll,
1104};
1105
1106
1107#define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1108
1109static struct video_device vdev_template = {
1110 .owner = THIS_MODULE,
1111 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1112 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1113 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1114 | V4L2_CAP_READWRITE),
1115 .hardware = VID_HARDWARE_PVRUSB2,
1116 .fops = &vdev_fops,
1117};
1118
1119
1120static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1121 struct pvr2_v4l2 *vp,
1122 enum pvr2_config cfg)
1123{
1124 int mindevnum;
1125 int unit_number;
1126 int v4l_type;
Mike Isely80793842006-12-27 23:12:28 -03001127 enum pvr2_v4l_type pvt;
Mike Iselyd8554972006-06-26 20:58:46 -03001128 dip->v4lp = vp;
1129 dip->config = cfg;
1130
1131
Mike Isely0f0f2572006-12-27 23:19:42 -03001132 switch (dip->config) {
Mike Iselyd8554972006-06-26 20:58:46 -03001133 case pvr2_config_mpeg:
1134 v4l_type = VFL_TYPE_GRABBER;
Mike Isely80793842006-12-27 23:12:28 -03001135 pvt = pvr2_v4l_type_video;
Mike Iselyd8554972006-06-26 20:58:46 -03001136 dip->stream = &vp->channel.mc_head->video_stream;
1137 break;
1138 case pvr2_config_vbi:
1139 v4l_type = VFL_TYPE_VBI;
Mike Isely80793842006-12-27 23:12:28 -03001140 pvt = pvr2_v4l_type_vbi;
Mike Iselyd8554972006-06-26 20:58:46 -03001141 break;
1142 case pvr2_config_radio:
1143 v4l_type = VFL_TYPE_RADIO;
Mike Isely80793842006-12-27 23:12:28 -03001144 pvt = pvr2_v4l_type_radio;
Mike Iselyd8554972006-06-26 20:58:46 -03001145 break;
1146 default:
1147 /* Bail out (this should be impossible) */
1148 err("Failed to set up pvrusb2 v4l dev"
1149 " due to unrecognized config");
1150 return;
1151 }
1152
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001153 /* radio device doesn 't need its own stream */
Mike Isely0f0f2572006-12-27 23:19:42 -03001154 if (!dip->stream && dip->config != pvr2_config_radio) {
Mike Iselyd8554972006-06-26 20:58:46 -03001155 err("Failed to set up pvrusb2 v4l dev"
1156 " due to missing stream instance");
1157 return;
1158 }
1159
Mike Isely75910052006-09-23 22:30:50 -03001160 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1161 dip->devbase.release = pvr2_video_device_release;
Mike Iselyd8554972006-06-26 20:58:46 -03001162
1163 mindevnum = -1;
1164 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
1165 if ((unit_number >= 0) && (unit_number < PVR_NUM)) {
Mike Isely5e6862c2006-12-27 23:17:26 -03001166 switch (v4l_type) {
1167 case VFL_TYPE_VBI:
1168 mindevnum = vbi_nr[unit_number];
1169 break;
1170 case VFL_TYPE_RADIO:
1171 mindevnum = radio_nr[unit_number];
1172 break;
1173 case VFL_TYPE_GRABBER:
1174 default:
1175 mindevnum = video_nr[unit_number];
1176 break;
1177 }
Mike Iselyd8554972006-06-26 20:58:46 -03001178 }
Mike Isely75910052006-09-23 22:30:50 -03001179 if ((video_register_device(&dip->devbase, v4l_type, mindevnum) < 0) &&
1180 (video_register_device(&dip->devbase, v4l_type, -1) < 0)) {
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001181 err("Failed to register pvrusb2 v4l device");
1182 }
Mike Isely0f0f2572006-12-27 23:19:42 -03001183 switch (dip->config) {
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001184 case pvr2_config_mpeg:
Mike Isely47ed3bc2006-06-29 09:26:43 -03001185 printk(KERN_INFO "pvrusb2: registered device video%d [%s]\n",
Mike Isely5e6862c2006-12-27 23:17:26 -03001186 dip->devbase.minor & 0x1f,
1187 pvr2_config_get_name(dip->config));
Mike Isely0f0f2572006-12-27 23:19:42 -03001188 break;
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001189 case pvr2_config_radio:
1190 printk(KERN_INFO "pvrusb2: registered device radio%d [%s]\n",
Mike Isely5e6862c2006-12-27 23:17:26 -03001191 dip->devbase.minor & 0x1f,
1192 pvr2_config_get_name(dip->config));
Mike Isely0f0f2572006-12-27 23:19:42 -03001193 break;
1194 case pvr2_config_vbi:
1195 printk(KERN_INFO "pvrusb2: registered device vbi%d [%s]\n",
1196 dip->devbase.minor & 0x1f,
1197 pvr2_config_get_name(dip->config));
1198 break;
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001199 default:
Mike Isely0f0f2572006-12-27 23:19:42 -03001200 break;
Mike Iselyd8554972006-06-26 20:58:46 -03001201 }
1202
Mike Iselyd8554972006-06-26 20:58:46 -03001203 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
Mike Isely80793842006-12-27 23:12:28 -03001204 pvt,dip->devbase.minor);
Mike Iselyd8554972006-06-26 20:58:46 -03001205}
1206
1207
1208struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1209{
1210 struct pvr2_v4l2 *vp;
1211
1212 vp = kmalloc(sizeof(*vp),GFP_KERNEL);
1213 if (!vp) return vp;
1214 memset(vp,0,sizeof(*vp));
Mike Isely0f0f2572006-12-27 23:19:42 -03001215 vp->dev_video = kmalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1216 vp->dev_radio = kmalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
1217 if (!(vp->dev_video && vp->dev_radio)) {
1218 if (vp->dev_video) kfree(vp->dev_video);
1219 if (vp->dev_radio) kfree(vp->dev_radio);
Mike Isely75910052006-09-23 22:30:50 -03001220 kfree(vp);
Randy Dunlapc2625bf2006-10-29 11:12:27 -03001221 return NULL;
Mike Isely75910052006-09-23 22:30:50 -03001222 }
Mike Isely0f0f2572006-12-27 23:19:42 -03001223 memset(vp->dev_video,0,sizeof(*vp->dev_video));
1224 memset(vp->dev_radio,0,sizeof(*vp->dev_radio));
Mike Iselyd8554972006-06-26 20:58:46 -03001225 pvr2_channel_init(&vp->channel,mnp);
1226 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1227
1228 vp->channel.check_func = pvr2_v4l2_internal_check;
1229
1230 /* register streams */
Mike Isely0f0f2572006-12-27 23:19:42 -03001231 pvr2_v4l2_dev_init(vp->dev_video,vp,pvr2_config_mpeg);
1232 pvr2_v4l2_dev_init(vp->dev_radio,vp,pvr2_config_radio);
Mike Iselyd8554972006-06-26 20:58:46 -03001233
1234 return vp;
1235}
1236
1237/*
1238 Stuff for Emacs to see, in order to encourage consistent editing style:
1239 *** Local Variables: ***
1240 *** mode: c ***
1241 *** fill-column: 75 ***
1242 *** tab-width: 8 ***
1243 *** c-basic-offset: 8 ***
1244 *** End: ***
1245 */