blob: 53323c338a6d2032bd22d48c3a51f14a1aaa1608 [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 {
Pantelis Koukousoulas848ed3c2007-01-20 01:57:36 -0300324 /* pkt: FIXME: We are returning one "fake" input here
325 which could very well be called "whatever_we_like".
326 This is for apps that want to see an audio input
327 just to feel comfortable, as well as to test if
328 it can do stereo or sth. There is actually no guarantee
329 that the actual audio input cannot change behind the app's
330 back, but most applications should not mind that either.
331
332 Hopefully, mplayer people will work with us on this (this
333 whole mess is to support mplayer pvr://), or Hans will come
334 up with a more standard way to say "we have inputs but we
335 don 't want you to change them independent of video" which
336 will sort this mess.
337 */
338 struct v4l2_audio *vin = arg;
Mike Iselyd8554972006-06-26 20:58:46 -0300339 ret = -EINVAL;
Pantelis Koukousoulas848ed3c2007-01-20 01:57:36 -0300340 if (vin->index > 0) break;
341 strncpy(vin->name, "PVRUSB2 Audio",14);
342 vin->capability = V4L2_AUDCAP_STEREO;
343 ret = 0;
344 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300345 break;
346 }
347
348 case VIDIOC_G_AUDIO:
349 {
Pantelis Koukousoulas848ed3c2007-01-20 01:57:36 -0300350 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
351 struct v4l2_audio *vin = arg;
352 memset(vin,0,sizeof(*vin));
353 vin->index = 0;
354 strncpy(vin->name, "PVRUSB2 Audio",14);
355 vin->capability = V4L2_AUDCAP_STEREO;
356 ret = 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300357 break;
358 }
359
360 case VIDIOC_S_AUDIO:
361 {
362 ret = -EINVAL;
363 break;
364 }
365 case VIDIOC_G_TUNER:
366 {
367 struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
Mike Isely18103c52007-01-20 00:09:47 -0300368 pvr2_hdw_execute_tuner_poll(hdw);
369 ret = pvr2_hdw_get_tuner_status(hdw,vt);
Mike Iselyd8554972006-06-26 20:58:46 -0300370 break;
371 }
372
373 case VIDIOC_S_TUNER:
374 {
375 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
376
377 if (vt->index != 0)
378 break;
379
380 ret = pvr2_ctrl_set_value(
381 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_AUDIOMODE),
382 vt->audmode);
Mike Isely11fc76c2007-01-20 00:24:52 -0300383 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300384 }
385
386 case VIDIOC_S_FREQUENCY:
387 {
388 const struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
Mike Iselyc0e69312006-12-27 23:25:06 -0300389 unsigned long fv;
Mike Isely18103c52007-01-20 00:09:47 -0300390 struct v4l2_tuner vt;
391 int cur_input;
392 struct pvr2_ctrl *ctrlp;
393 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
394 if (ret != 0) break;
395 ctrlp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
396 ret = pvr2_ctrl_get_value(ctrlp,&cur_input);
397 if (ret != 0) break;
Mike Iselyc0e69312006-12-27 23:25:06 -0300398 if (vf->type == V4L2_TUNER_RADIO) {
Mike Isely18103c52007-01-20 00:09:47 -0300399 if (cur_input != PVR2_CVAL_INPUT_RADIO) {
400 pvr2_ctrl_set_value(ctrlp,
401 PVR2_CVAL_INPUT_RADIO);
402 }
403 } else {
404 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
405 pvr2_ctrl_set_value(ctrlp,
406 PVR2_CVAL_INPUT_TV);
407 }
408 }
409 fv = vf->frequency;
410 if (vt.capability & V4L2_TUNER_CAP_LOW) {
Mike Iselyc0e69312006-12-27 23:25:06 -0300411 fv = (fv * 125) / 2;
412 } else {
413 fv = fv * 62500;
414 }
Mike Iselyd8554972006-06-26 20:58:46 -0300415 ret = pvr2_ctrl_set_value(
Mike Iselyc0e69312006-12-27 23:25:06 -0300416 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),fv);
Mike Iselyd8554972006-06-26 20:58:46 -0300417 break;
418 }
419
420 case VIDIOC_G_FREQUENCY:
421 {
422 struct v4l2_frequency *vf = (struct v4l2_frequency *)arg;
423 int val = 0;
Mike Isely18103c52007-01-20 00:09:47 -0300424 int cur_input;
425 struct v4l2_tuner vt;
426 ret = pvr2_hdw_get_tuner_status(hdw,&vt);
427 if (ret != 0) break;
Mike Iselyd8554972006-06-26 20:58:46 -0300428 ret = pvr2_ctrl_get_value(
429 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_FREQUENCY),
430 &val);
Mike Iselyc0e69312006-12-27 23:25:06 -0300431 if (ret != 0) break;
432 pvr2_ctrl_get_value(
433 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT),
434 &cur_input);
435 if (cur_input == PVR2_CVAL_INPUT_RADIO) {
Mike Iselyc0e69312006-12-27 23:25:06 -0300436 vf->type = V4L2_TUNER_RADIO;
437 } else {
Mike Iselyc0e69312006-12-27 23:25:06 -0300438 vf->type = V4L2_TUNER_ANALOG_TV;
439 }
Mike Isely18103c52007-01-20 00:09:47 -0300440 if (vt.capability & V4L2_TUNER_CAP_LOW) {
441 val = (val * 2) / 125;
442 } else {
443 val /= 62500;
444 }
445 vf->frequency = val;
Mike Iselyd8554972006-06-26 20:58:46 -0300446 break;
447 }
448
449 case VIDIOC_ENUM_FMT:
450 {
451 struct v4l2_fmtdesc *fd = (struct v4l2_fmtdesc *)arg;
452
453 /* Only one format is supported : mpeg.*/
454 if (fd->index != 0)
455 break;
456
457 memcpy(fd, pvr_fmtdesc, sizeof(struct v4l2_fmtdesc));
458 ret = 0;
459 break;
460 }
461
462 case VIDIOC_G_FMT:
463 {
464 struct v4l2_format *vf = (struct v4l2_format *)arg;
465 int val;
466 switch(vf->type) {
467 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
468 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
469 sizeof(struct v4l2_format));
470 val = 0;
471 pvr2_ctrl_get_value(
472 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES),
473 &val);
474 vf->fmt.pix.width = val;
475 val = 0;
476 pvr2_ctrl_get_value(
Mike Iselyd8554972006-06-26 20:58:46 -0300477 pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES),
478 &val);
479 vf->fmt.pix.height = val;
480 ret = 0;
481 break;
482 case V4L2_BUF_TYPE_VBI_CAPTURE:
483 // ????? Still need to figure out to do VBI correctly
484 ret = -EINVAL;
485 break;
486 default:
487 ret = -EINVAL;
488 break;
489 }
490 break;
491 }
492
493 case VIDIOC_TRY_FMT:
494 case VIDIOC_S_FMT:
495 {
496 struct v4l2_format *vf = (struct v4l2_format *)arg;
497
498 ret = 0;
499 switch(vf->type) {
500 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
Pantelis Koukousoulasfd694962007-01-20 01:59:54 -0300501 int lmin,lmax,ldef;
Mike Iselye95a1912006-08-08 09:10:07 -0300502 struct pvr2_ctrl *hcp,*vcp;
Mike Iselyd8554972006-06-26 20:58:46 -0300503 int h = vf->fmt.pix.height;
504 int w = vf->fmt.pix.width;
Mike Iselye95a1912006-08-08 09:10:07 -0300505 hcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_HRES);
506 vcp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_VRES);
Mike Iselyd8554972006-06-26 20:58:46 -0300507
Mike Iselye95a1912006-08-08 09:10:07 -0300508 lmin = pvr2_ctrl_get_min(hcp);
509 lmax = pvr2_ctrl_get_max(hcp);
Pantelis Koukousoulasfd694962007-01-20 01:59:54 -0300510 ldef = pvr2_ctrl_get_def(hcp);
511 if (w == -1) {
512 w = ldef;
513 } else if (w < lmin) {
Mike Iselye95a1912006-08-08 09:10:07 -0300514 w = lmin;
515 } else if (w > lmax) {
516 w = lmax;
Mike Isely039c4302006-06-25 20:04:16 -0300517 }
Hans Verkuilb31e3412006-09-01 18:36:10 -0300518 lmin = pvr2_ctrl_get_min(vcp);
519 lmax = pvr2_ctrl_get_max(vcp);
Pantelis Koukousoulasfd694962007-01-20 01:59:54 -0300520 ldef = pvr2_ctrl_get_def(vcp);
521 if (h == -1) {
522 h = ldef;
523 } else if (h < lmin) {
Hans Verkuilb31e3412006-09-01 18:36:10 -0300524 h = lmin;
525 } else if (h > lmax) {
526 h = lmax;
527 }
Mike Iselyd8554972006-06-26 20:58:46 -0300528
529 memcpy(vf, &pvr_format[PVR_FORMAT_PIX],
530 sizeof(struct v4l2_format));
Mike Isely039c4302006-06-25 20:04:16 -0300531 vf->fmt.pix.width = w;
532 vf->fmt.pix.height = h;
Mike Iselyd8554972006-06-26 20:58:46 -0300533
534 if (cmd == VIDIOC_S_FMT) {
Mike Iselye95a1912006-08-08 09:10:07 -0300535 pvr2_ctrl_set_value(hcp,vf->fmt.pix.width);
536 pvr2_ctrl_set_value(vcp,vf->fmt.pix.height);
Mike Iselyd8554972006-06-26 20:58:46 -0300537 }
538 } break;
539 case V4L2_BUF_TYPE_VBI_CAPTURE:
540 // ????? Still need to figure out to do VBI correctly
541 ret = -EINVAL;
542 break;
543 default:
544 ret = -EINVAL;
545 break;
546 }
547 break;
548 }
549
550 case VIDIOC_STREAMON:
551 {
Mike Isely16eb40d2006-12-30 18:27:32 -0300552 if (!fh->dev_info->stream) {
553 /* No stream defined for this node. This means
554 that we're not currently allowed to stream from
555 this node. */
556 ret = -EPERM;
557 break;
558 }
Mike Iselyd8554972006-06-26 20:58:46 -0300559 ret = pvr2_hdw_set_stream_type(hdw,dev_info->config);
560 if (ret < 0) return ret;
561 ret = pvr2_hdw_set_streaming(hdw,!0);
562 break;
563 }
564
565 case VIDIOC_STREAMOFF:
566 {
Mike Isely16eb40d2006-12-30 18:27:32 -0300567 if (!fh->dev_info->stream) {
568 /* No stream defined for this node. This means
569 that we're not currently allowed to stream from
570 this node. */
571 ret = -EPERM;
572 break;
573 }
Mike Iselyd8554972006-06-26 20:58:46 -0300574 ret = pvr2_hdw_set_streaming(hdw,0);
575 break;
576 }
577
578 case VIDIOC_QUERYCTRL:
579 {
580 struct pvr2_ctrl *cptr;
581 struct v4l2_queryctrl *vc = (struct v4l2_queryctrl *)arg;
582 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300583 if (vc->id & V4L2_CTRL_FLAG_NEXT_CTRL) {
584 cptr = pvr2_hdw_get_ctrl_nextv4l(
585 hdw,(vc->id & ~V4L2_CTRL_FLAG_NEXT_CTRL));
586 if (cptr) vc->id = pvr2_ctrl_get_v4lid(cptr);
587 } else {
588 cptr = pvr2_hdw_get_ctrl_v4l(hdw,vc->id);
589 }
Mike Iselyd8554972006-06-26 20:58:46 -0300590 if (!cptr) {
Mike Isely0885ba12006-06-25 21:30:47 -0300591 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300592 "QUERYCTRL id=0x%x not implemented here",
593 vc->id);
Mike Iselyd8554972006-06-26 20:58:46 -0300594 ret = -EINVAL;
595 break;
596 }
597
Mike Iselya761f432006-06-25 20:04:44 -0300598 pvr2_trace(PVR2_TRACE_V4LIOCTL,
599 "QUERYCTRL id=0x%x mapping name=%s (%s)",
600 vc->id,pvr2_ctrl_get_name(cptr),
601 pvr2_ctrl_get_desc(cptr));
602 strlcpy(vc->name,pvr2_ctrl_get_desc(cptr),sizeof(vc->name));
603 vc->flags = pvr2_ctrl_get_v4lflags(cptr);
Mike Iselyd8554972006-06-26 20:58:46 -0300604 vc->default_value = pvr2_ctrl_get_def(cptr);
605 switch (pvr2_ctrl_get_type(cptr)) {
606 case pvr2_ctl_enum:
607 vc->type = V4L2_CTRL_TYPE_MENU;
608 vc->minimum = 0;
609 vc->maximum = pvr2_ctrl_get_cnt(cptr) - 1;
610 vc->step = 1;
611 break;
Mike Isely33213962006-06-25 20:04:40 -0300612 case pvr2_ctl_bool:
Mike Isely1d9f8462006-06-25 20:04:58 -0300613 vc->type = V4L2_CTRL_TYPE_BOOLEAN;
Mike Isely33213962006-06-25 20:04:40 -0300614 vc->minimum = 0;
615 vc->maximum = 1;
616 vc->step = 1;
617 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300618 case pvr2_ctl_int:
619 vc->type = V4L2_CTRL_TYPE_INTEGER;
620 vc->minimum = pvr2_ctrl_get_min(cptr);
621 vc->maximum = pvr2_ctrl_get_max(cptr);
622 vc->step = 1;
623 break;
624 default:
Mike Isely0885ba12006-06-25 21:30:47 -0300625 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselya761f432006-06-25 20:04:44 -0300626 "QUERYCTRL id=0x%x name=%s not mappable",
627 vc->id,pvr2_ctrl_get_name(cptr));
Mike Iselyd8554972006-06-26 20:58:46 -0300628 ret = -EINVAL;
629 break;
630 }
631 break;
632 }
633
634 case VIDIOC_QUERYMENU:
635 {
636 struct v4l2_querymenu *vm = (struct v4l2_querymenu *)arg;
637 unsigned int cnt = 0;
638 ret = pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw,vm->id),
639 vm->index,
640 vm->name,sizeof(vm->name)-1,
641 &cnt);
642 vm->name[cnt] = 0;
643 break;
644 }
645
646 case VIDIOC_G_CTRL:
647 {
648 struct v4l2_control *vc = (struct v4l2_control *)arg;
649 int val = 0;
650 ret = pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
651 &val);
652 vc->value = val;
653 break;
654 }
655
656 case VIDIOC_S_CTRL:
657 {
658 struct v4l2_control *vc = (struct v4l2_control *)arg;
659 ret = pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw,vc->id),
660 vc->value);
661 break;
662 }
663
Mike Isely1d9f8462006-06-25 20:04:58 -0300664 case VIDIOC_G_EXT_CTRLS:
665 {
666 struct v4l2_ext_controls *ctls =
667 (struct v4l2_ext_controls *)arg;
668 struct v4l2_ext_control *ctrl;
669 unsigned int idx;
670 int val;
Mike Iselyc1c26802007-01-20 00:30:23 -0300671 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300672 for (idx = 0; idx < ctls->count; idx++) {
673 ctrl = ctls->controls + idx;
674 ret = pvr2_ctrl_get_value(
675 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),&val);
676 if (ret) {
677 ctls->error_idx = idx;
678 break;
679 }
680 /* Ensure that if read as a 64 bit value, the user
681 will still get a hopefully sane value */
682 ctrl->value64 = 0;
683 ctrl->value = val;
684 }
685 break;
686 }
687
688 case VIDIOC_S_EXT_CTRLS:
689 {
690 struct v4l2_ext_controls *ctls =
691 (struct v4l2_ext_controls *)arg;
692 struct v4l2_ext_control *ctrl;
693 unsigned int idx;
Mike Iselyc1c26802007-01-20 00:30:23 -0300694 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300695 for (idx = 0; idx < ctls->count; idx++) {
696 ctrl = ctls->controls + idx;
697 ret = pvr2_ctrl_set_value(
698 pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id),
699 ctrl->value);
700 if (ret) {
701 ctls->error_idx = idx;
702 break;
703 }
704 }
705 break;
706 }
707
708 case VIDIOC_TRY_EXT_CTRLS:
709 {
710 struct v4l2_ext_controls *ctls =
711 (struct v4l2_ext_controls *)arg;
712 struct v4l2_ext_control *ctrl;
713 struct pvr2_ctrl *pctl;
714 unsigned int idx;
715 /* For the moment just validate that the requested control
716 actually exists. */
Mike Iselyc1c26802007-01-20 00:30:23 -0300717 ret = 0;
Mike Isely1d9f8462006-06-25 20:04:58 -0300718 for (idx = 0; idx < ctls->count; idx++) {
719 ctrl = ctls->controls + idx;
720 pctl = pvr2_hdw_get_ctrl_v4l(hdw,ctrl->id);
721 if (!pctl) {
722 ret = -EINVAL;
723 ctls->error_idx = idx;
724 break;
725 }
726 }
727 break;
728 }
729
Mike Iselyd8554972006-06-26 20:58:46 -0300730 case VIDIOC_LOG_STATUS:
731 {
Mike Iselyd8554972006-06-26 20:58:46 -0300732 pvr2_hdw_trigger_module_log(hdw);
Mike Iselyd8554972006-06-26 20:58:46 -0300733 ret = 0;
734 break;
735 }
Mike Isely32ffa9a2006-09-23 22:26:52 -0300736#ifdef CONFIG_VIDEO_ADV_DEBUG
737 case VIDIOC_INT_G_REGISTER:
738 case VIDIOC_INT_S_REGISTER:
739 {
740 u32 val;
741 struct v4l2_register *req = (struct v4l2_register *)arg;
742 if (cmd == VIDIOC_INT_S_REGISTER) val = req->val;
743 ret = pvr2_hdw_register_access(
744 hdw,req->i2c_id,req->reg,
745 cmd == VIDIOC_INT_S_REGISTER,&val);
Mike Isely6d988162006-09-28 17:53:49 -0300746 if (cmd == VIDIOC_INT_G_REGISTER) req->val = val;
Mike Isely32ffa9a2006-09-23 22:26:52 -0300747 break;
748 }
749#endif
Mike Iselyd8554972006-06-26 20:58:46 -0300750
751 default :
752 ret = v4l_compat_translate_ioctl(inode,file,cmd,
753 arg,pvr2_v4l2_do_ioctl);
754 }
755
756 pvr2_hdw_commit_ctl(hdw);
757
758 if (ret < 0) {
759 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
Mike Isely0885ba12006-06-25 21:30:47 -0300760 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300761 "pvr2_v4l2_do_ioctl failure, ret=%d",ret);
762 } else {
Mike Isely0885ba12006-06-25 21:30:47 -0300763 if (pvrusb2_debug & PVR2_TRACE_V4LIOCTL) {
764 pvr2_trace(PVR2_TRACE_V4LIOCTL,
Mike Iselyd8554972006-06-26 20:58:46 -0300765 "pvr2_v4l2_do_ioctl failure, ret=%d"
766 " command was:",ret);
767 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw),
768 cmd);
769 }
770 }
771 } else {
772 pvr2_trace(PVR2_TRACE_V4LIOCTL,
773 "pvr2_v4l2_do_ioctl complete, ret=%d (0x%x)",
774 ret,ret);
775 }
776 return ret;
777}
778
779
780static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev *dip)
781{
Mike Isely0f0f2572006-12-27 23:19:42 -0300782 int minor_id = dip->devbase.minor;
Mike Isely0f0f2572006-12-27 23:19:42 -0300783 struct pvr2_hdw *hdw = dip->v4lp->channel.mc_head->hdw;
Mike Isely16eb40d2006-12-30 18:27:32 -0300784 enum pvr2_config cfg = dip->config;
785 int v4l_type = dip->v4l_type;
Mike Isely0f0f2572006-12-27 23:19:42 -0300786
Mike Isely16eb40d2006-12-30 18:27:32 -0300787 pvr2_hdw_v4l_store_minor_number(hdw,dip->minor_type,-1);
Mike Isely75910052006-09-23 22:30:50 -0300788
789 /* Paranoia */
Randy Dunlapc2625bf2006-10-29 11:12:27 -0300790 dip->v4lp = NULL;
791 dip->stream = NULL;
Mike Isely75910052006-09-23 22:30:50 -0300792
793 /* Actual deallocation happens later when all internal references
794 are gone. */
795 video_unregister_device(&dip->devbase);
Mike Isely0f0f2572006-12-27 23:19:42 -0300796
Mike Isely16eb40d2006-12-30 18:27:32 -0300797 printk(KERN_INFO "pvrusb2: unregistered device %s%u [%s]\n",
798 get_v4l_name(v4l_type),minor_id & 0x1f,
799 pvr2_config_get_name(cfg));
Mike Isely0f0f2572006-12-27 23:19:42 -0300800
Mike Iselyd8554972006-06-26 20:58:46 -0300801}
802
803
804static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2 *vp)
805{
Mike Isely0f0f2572006-12-27 23:19:42 -0300806 if (vp->dev_video) {
807 pvr2_v4l2_dev_destroy(vp->dev_video);
808 vp->dev_video = 0;
809 }
810 if (vp->dev_radio) {
811 pvr2_v4l2_dev_destroy(vp->dev_radio);
812 vp->dev_radio = 0;
813 }
Mike Iselyd8554972006-06-26 20:58:46 -0300814
815 pvr2_trace(PVR2_TRACE_STRUCT,"Destroying pvr2_v4l2 id=%p",vp);
816 pvr2_channel_done(&vp->channel);
817 kfree(vp);
818}
819
820
Mike Isely75910052006-09-23 22:30:50 -0300821static void pvr2_video_device_release(struct video_device *vdev)
822{
823 struct pvr2_v4l2_dev *dev;
824 dev = container_of(vdev,struct pvr2_v4l2_dev,devbase);
825 kfree(dev);
826}
827
828
Adrian Bunk07e337e2006-06-30 11:30:20 -0300829static void pvr2_v4l2_internal_check(struct pvr2_channel *chp)
Mike Iselyd8554972006-06-26 20:58:46 -0300830{
831 struct pvr2_v4l2 *vp;
832 vp = container_of(chp,struct pvr2_v4l2,channel);
833 if (!vp->channel.mc_head->disconnect_flag) return;
834 if (vp->vfirst) return;
835 pvr2_v4l2_destroy_no_lock(vp);
836}
837
838
Adrian Bunk07e337e2006-06-30 11:30:20 -0300839static int pvr2_v4l2_ioctl(struct inode *inode, struct file *file,
840 unsigned int cmd, unsigned long arg)
Mike Iselyd8554972006-06-26 20:58:46 -0300841{
842
843/* Temporary hack : use ivtv api until a v4l2 one is available. */
844#define IVTV_IOC_G_CODEC 0xFFEE7703
845#define IVTV_IOC_S_CODEC 0xFFEE7704
846 if (cmd == IVTV_IOC_G_CODEC || cmd == IVTV_IOC_S_CODEC) return 0;
847 return video_usercopy(inode, file, cmd, arg, pvr2_v4l2_do_ioctl);
848}
849
850
Adrian Bunk07e337e2006-06-30 11:30:20 -0300851static int pvr2_v4l2_release(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300852{
853 struct pvr2_v4l2_fh *fhp = file->private_data;
854 struct pvr2_v4l2 *vp = fhp->vhead;
855 struct pvr2_context *mp = fhp->vhead->channel.mc_head;
Mike Iselyc74e0062006-12-30 18:31:22 -0300856 struct pvr2_hdw *hdw = fhp->channel.mc_head->hdw;
Mike Iselyd8554972006-06-26 20:58:46 -0300857
858 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_release");
859
860 if (fhp->rhp) {
861 struct pvr2_stream *sp;
Mike Iselyd8554972006-06-26 20:58:46 -0300862 pvr2_hdw_set_streaming(hdw,0);
863 sp = pvr2_ioread_get_stream(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300864 if (sp) pvr2_stream_set_callback(sp,NULL,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300865 pvr2_ioread_destroy(fhp->rhp);
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300866 fhp->rhp = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300867 }
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300868
Mike Iselyd8554972006-06-26 20:58:46 -0300869 v4l2_prio_close(&vp->prio, &fhp->prio);
870 file->private_data = NULL;
871
872 pvr2_context_enter(mp); do {
Mike Iselyc74e0062006-12-30 18:31:22 -0300873 /* Restore the previous input selection, if it makes sense
874 to do so. */
875 if (fhp->dev_info->v4l_type == VFL_TYPE_RADIO) {
876 struct pvr2_ctrl *cp;
877 int pval;
878 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
879 pvr2_ctrl_get_value(cp,&pval);
880 /* Only restore if we're still selecting the radio */
881 if (pval == PVR2_CVAL_INPUT_RADIO) {
882 pvr2_ctrl_set_value(cp,fhp->prev_input_val);
883 pvr2_hdw_commit_ctl(hdw);
884 }
885 }
886
Mike Iselyd8554972006-06-26 20:58:46 -0300887 if (fhp->vnext) {
888 fhp->vnext->vprev = fhp->vprev;
889 } else {
890 vp->vlast = fhp->vprev;
891 }
892 if (fhp->vprev) {
893 fhp->vprev->vnext = fhp->vnext;
894 } else {
895 vp->vfirst = fhp->vnext;
896 }
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300897 fhp->vnext = NULL;
898 fhp->vprev = NULL;
899 fhp->vhead = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300900 pvr2_channel_done(&fhp->channel);
901 pvr2_trace(PVR2_TRACE_STRUCT,
902 "Destroying pvr_v4l2_fh id=%p",fhp);
903 kfree(fhp);
904 if (vp->channel.mc_head->disconnect_flag && !vp->vfirst) {
905 pvr2_v4l2_destroy_no_lock(vp);
906 }
907 } while (0); pvr2_context_exit(mp);
908 return 0;
909}
910
911
Adrian Bunk07e337e2006-06-30 11:30:20 -0300912static int pvr2_v4l2_open(struct inode *inode, struct file *file)
Mike Iselyd8554972006-06-26 20:58:46 -0300913{
Mike Isely75910052006-09-23 22:30:50 -0300914 struct pvr2_v4l2_dev *dip; /* Our own context pointer */
Mike Iselyd8554972006-06-26 20:58:46 -0300915 struct pvr2_v4l2_fh *fhp;
916 struct pvr2_v4l2 *vp;
917 struct pvr2_hdw *hdw;
918
Mike Isely75910052006-09-23 22:30:50 -0300919 dip = container_of(video_devdata(file),struct pvr2_v4l2_dev,devbase);
Mike Iselyd8554972006-06-26 20:58:46 -0300920
921 vp = dip->v4lp;
922 hdw = vp->channel.hdw;
923
924 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,"pvr2_v4l2_open");
925
926 if (!pvr2_hdw_dev_ok(hdw)) {
927 pvr2_trace(PVR2_TRACE_OPEN_CLOSE,
928 "pvr2_v4l2_open: hardware not ready");
929 return -EIO;
930 }
931
Mike Isely4b85dee2007-01-20 00:03:32 -0300932 fhp = kzalloc(sizeof(*fhp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300933 if (!fhp) {
934 return -ENOMEM;
935 }
Mike Iselyd8554972006-06-26 20:58:46 -0300936
937 init_waitqueue_head(&fhp->wait_data);
938 fhp->dev_info = dip;
939
940 pvr2_context_enter(vp->channel.mc_head); do {
941 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr_v4l2_fh id=%p",fhp);
942 pvr2_channel_init(&fhp->channel,vp->channel.mc_head);
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -0300943
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300944 fhp->vnext = NULL;
Mike Iselyd8554972006-06-26 20:58:46 -0300945 fhp->vprev = vp->vlast;
946 if (vp->vlast) {
947 vp->vlast->vnext = fhp;
948 } else {
949 vp->vfirst = fhp;
950 }
951 vp->vlast = fhp;
952 fhp->vhead = vp;
Mike Iselyc74e0062006-12-30 18:31:22 -0300953
954 /* Opening the /dev/radioX device implies a mode switch.
955 So execute that here. Note that you can get the
956 IDENTICAL effect merely by opening the normal video
957 device and setting the input appropriately. */
958 if (dip->v4l_type == VFL_TYPE_RADIO) {
959 struct pvr2_ctrl *cp;
960 cp = pvr2_hdw_get_ctrl_by_id(hdw,PVR2_CID_INPUT);
961 pvr2_ctrl_get_value(cp,&fhp->prev_input_val);
962 pvr2_ctrl_set_value(cp,PVR2_CVAL_INPUT_RADIO);
963 pvr2_hdw_commit_ctl(hdw);
964 }
Mike Iselyd8554972006-06-26 20:58:46 -0300965 } while (0); pvr2_context_exit(vp->channel.mc_head);
966
967 fhp->file = file;
968 file->private_data = fhp;
969 v4l2_prio_open(&vp->prio,&fhp->prio);
970
971 fhp->fw_mode_flag = pvr2_hdw_cpufw_get_enabled(hdw);
972
973 return 0;
974}
975
976
977static void pvr2_v4l2_notify(struct pvr2_v4l2_fh *fhp)
978{
979 wake_up(&fhp->wait_data);
980}
981
982static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh *fh)
983{
984 int ret;
985 struct pvr2_stream *sp;
986 struct pvr2_hdw *hdw;
987 if (fh->rhp) return 0;
988
Mike Isely16eb40d2006-12-30 18:27:32 -0300989 if (!fh->dev_info->stream) {
990 /* No stream defined for this node. This means that we're
991 not currently allowed to stream from this node. */
992 return -EPERM;
993 }
994
Mike Iselyd8554972006-06-26 20:58:46 -0300995 /* First read() attempt. Try to claim the stream and start
996 it... */
997 if ((ret = pvr2_channel_claim_stream(&fh->channel,
998 fh->dev_info->stream)) != 0) {
999 /* Someone else must already have it */
1000 return ret;
1001 }
1002
1003 fh->rhp = pvr2_channel_create_mpeg_stream(fh->dev_info->stream);
1004 if (!fh->rhp) {
Mike Iselya0fd1cb2006-06-30 11:35:28 -03001005 pvr2_channel_claim_stream(&fh->channel,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -03001006 return -ENOMEM;
1007 }
1008
1009 hdw = fh->channel.mc_head->hdw;
1010 sp = fh->dev_info->stream->stream;
1011 pvr2_stream_set_callback(sp,(pvr2_stream_callback)pvr2_v4l2_notify,fh);
1012 pvr2_hdw_set_stream_type(hdw,fh->dev_info->config);
1013 pvr2_hdw_set_streaming(hdw,!0);
1014 ret = pvr2_ioread_set_enabled(fh->rhp,!0);
1015
1016 return ret;
1017}
1018
1019
1020static ssize_t pvr2_v4l2_read(struct file *file,
1021 char __user *buff, size_t count, loff_t *ppos)
1022{
1023 struct pvr2_v4l2_fh *fh = file->private_data;
1024 int ret;
1025
1026 if (fh->fw_mode_flag) {
1027 struct pvr2_hdw *hdw = fh->channel.mc_head->hdw;
1028 char *tbuf;
1029 int c1,c2;
1030 int tcnt = 0;
1031 unsigned int offs = *ppos;
1032
1033 tbuf = kmalloc(PAGE_SIZE,GFP_KERNEL);
1034 if (!tbuf) return -ENOMEM;
1035
1036 while (count) {
1037 c1 = count;
1038 if (c1 > PAGE_SIZE) c1 = PAGE_SIZE;
1039 c2 = pvr2_hdw_cpufw_get(hdw,offs,tbuf,c1);
1040 if (c2 < 0) {
1041 tcnt = c2;
1042 break;
1043 }
1044 if (!c2) break;
1045 if (copy_to_user(buff,tbuf,c2)) {
1046 tcnt = -EFAULT;
1047 break;
1048 }
1049 offs += c2;
1050 tcnt += c2;
1051 buff += c2;
1052 count -= c2;
1053 *ppos += c2;
1054 }
1055 kfree(tbuf);
1056 return tcnt;
1057 }
1058
1059 if (!fh->rhp) {
1060 ret = pvr2_v4l2_iosetup(fh);
1061 if (ret) {
1062 return ret;
1063 }
1064 }
1065
1066 for (;;) {
1067 ret = pvr2_ioread_read(fh->rhp,buff,count);
1068 if (ret >= 0) break;
1069 if (ret != -EAGAIN) break;
1070 if (file->f_flags & O_NONBLOCK) break;
1071 /* Doing blocking I/O. Wait here. */
1072 ret = wait_event_interruptible(
1073 fh->wait_data,
1074 pvr2_ioread_avail(fh->rhp) >= 0);
1075 if (ret < 0) break;
1076 }
1077
1078 return ret;
1079}
1080
1081
1082static unsigned int pvr2_v4l2_poll(struct file *file, poll_table *wait)
1083{
1084 unsigned int mask = 0;
1085 struct pvr2_v4l2_fh *fh = file->private_data;
1086 int ret;
1087
1088 if (fh->fw_mode_flag) {
1089 mask |= POLLIN | POLLRDNORM;
1090 return mask;
1091 }
1092
1093 if (!fh->rhp) {
1094 ret = pvr2_v4l2_iosetup(fh);
1095 if (ret) return POLLERR;
1096 }
1097
1098 poll_wait(file,&fh->wait_data,wait);
1099
1100 if (pvr2_ioread_avail(fh->rhp) >= 0) {
1101 mask |= POLLIN | POLLRDNORM;
1102 }
1103
1104 return mask;
1105}
1106
1107
Arjan van de Venfa027c22007-02-12 00:55:33 -08001108static const struct file_operations vdev_fops = {
Mike Iselyd8554972006-06-26 20:58:46 -03001109 .owner = THIS_MODULE,
1110 .open = pvr2_v4l2_open,
1111 .release = pvr2_v4l2_release,
1112 .read = pvr2_v4l2_read,
1113 .ioctl = pvr2_v4l2_ioctl,
1114 .llseek = no_llseek,
1115 .poll = pvr2_v4l2_poll,
1116};
1117
1118
1119#define VID_HARDWARE_PVRUSB2 38 /* FIXME : need a good value */
1120
1121static struct video_device vdev_template = {
1122 .owner = THIS_MODULE,
1123 .type = VID_TYPE_CAPTURE | VID_TYPE_TUNER,
1124 .type2 = (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VBI_CAPTURE
1125 | V4L2_CAP_TUNER | V4L2_CAP_AUDIO
1126 | V4L2_CAP_READWRITE),
1127 .hardware = VID_HARDWARE_PVRUSB2,
1128 .fops = &vdev_fops,
1129};
1130
1131
1132static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev *dip,
1133 struct pvr2_v4l2 *vp,
Mike Isely16eb40d2006-12-30 18:27:32 -03001134 int v4l_type)
Mike Iselyd8554972006-06-26 20:58:46 -03001135{
1136 int mindevnum;
1137 int unit_number;
Mike Isely16eb40d2006-12-30 18:27:32 -03001138 int *nr_ptr = 0;
Mike Iselyd8554972006-06-26 20:58:46 -03001139 dip->v4lp = vp;
Mike Iselyd8554972006-06-26 20:58:46 -03001140
1141
Mike Isely16eb40d2006-12-30 18:27:32 -03001142 dip->v4l_type = v4l_type;
1143 switch (v4l_type) {
1144 case VFL_TYPE_GRABBER:
Mike Iselyd8554972006-06-26 20:58:46 -03001145 dip->stream = &vp->channel.mc_head->video_stream;
Mike Isely16eb40d2006-12-30 18:27:32 -03001146 dip->config = pvr2_config_mpeg;
1147 dip->minor_type = pvr2_v4l_type_video;
1148 nr_ptr = video_nr;
Mike Iselyc74e0062006-12-30 18:31:22 -03001149 if (!dip->stream) {
1150 err("Failed to set up pvrusb2 v4l video dev"
1151 " due to missing stream instance");
1152 return;
1153 }
Mike Iselyd8554972006-06-26 20:58:46 -03001154 break;
Mike Isely16eb40d2006-12-30 18:27:32 -03001155 case VFL_TYPE_VBI:
1156 dip->config = pvr2_config_vbi;
1157 dip->minor_type = pvr2_v4l_type_vbi;
1158 nr_ptr = vbi_nr;
Mike Iselyd8554972006-06-26 20:58:46 -03001159 break;
Mike Isely16eb40d2006-12-30 18:27:32 -03001160 case VFL_TYPE_RADIO:
Mike Iselyaf78a482007-01-20 00:04:31 -03001161 dip->stream = &vp->channel.mc_head->video_stream;
1162 dip->config = pvr2_config_mpeg;
Mike Isely16eb40d2006-12-30 18:27:32 -03001163 dip->minor_type = pvr2_v4l_type_radio;
1164 nr_ptr = radio_nr;
Mike Iselyd8554972006-06-26 20:58:46 -03001165 break;
1166 default:
1167 /* Bail out (this should be impossible) */
1168 err("Failed to set up pvrusb2 v4l dev"
1169 " due to unrecognized config");
1170 return;
1171 }
1172
Mike Isely75910052006-09-23 22:30:50 -03001173 memcpy(&dip->devbase,&vdev_template,sizeof(vdev_template));
1174 dip->devbase.release = pvr2_video_device_release;
Mike Iselyd8554972006-06-26 20:58:46 -03001175
1176 mindevnum = -1;
1177 unit_number = pvr2_hdw_get_unit_number(vp->channel.mc_head->hdw);
Mike Isely16eb40d2006-12-30 18:27:32 -03001178 if (nr_ptr && (unit_number >= 0) && (unit_number < PVR_NUM)) {
1179 mindevnum = nr_ptr[unit_number];
Mike Iselyd8554972006-06-26 20:58:46 -03001180 }
Mike Isely16eb40d2006-12-30 18:27:32 -03001181 if ((video_register_device(&dip->devbase,
1182 dip->v4l_type, mindevnum) < 0) &&
1183 (video_register_device(&dip->devbase,
1184 dip->v4l_type, -1) < 0)) {
Pantelis Koukousoulasae2b9e22006-12-27 23:09:55 -03001185 err("Failed to register pvrusb2 v4l device");
1186 }
Mike Isely16eb40d2006-12-30 18:27:32 -03001187
1188 printk(KERN_INFO "pvrusb2: registered device %s%u [%s]\n",
1189 get_v4l_name(dip->v4l_type),dip->devbase.minor & 0x1f,
1190 pvr2_config_get_name(dip->config));
Mike Iselyd8554972006-06-26 20:58:46 -03001191
Mike Iselyd8554972006-06-26 20:58:46 -03001192 pvr2_hdw_v4l_store_minor_number(vp->channel.mc_head->hdw,
Mike Isely16eb40d2006-12-30 18:27:32 -03001193 dip->minor_type,dip->devbase.minor);
Mike Iselyd8554972006-06-26 20:58:46 -03001194}
1195
1196
1197struct pvr2_v4l2 *pvr2_v4l2_create(struct pvr2_context *mnp)
1198{
1199 struct pvr2_v4l2 *vp;
1200
Mike Isely4b85dee2007-01-20 00:03:32 -03001201 vp = kzalloc(sizeof(*vp),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -03001202 if (!vp) return vp;
Mike Isely4b85dee2007-01-20 00:03:32 -03001203 vp->dev_video = kzalloc(sizeof(*vp->dev_video),GFP_KERNEL);
1204 vp->dev_radio = kzalloc(sizeof(*vp->dev_radio),GFP_KERNEL);
Mike Isely0f0f2572006-12-27 23:19:42 -03001205 if (!(vp->dev_video && vp->dev_radio)) {
Mike Isely6710fb82007-01-20 00:02:26 -03001206 kfree(vp->dev_video);
1207 kfree(vp->dev_radio);
Mike Isely75910052006-09-23 22:30:50 -03001208 kfree(vp);
Randy Dunlapc2625bf2006-10-29 11:12:27 -03001209 return NULL;
Mike Isely75910052006-09-23 22:30:50 -03001210 }
Mike Iselyd8554972006-06-26 20:58:46 -03001211 pvr2_channel_init(&vp->channel,mnp);
1212 pvr2_trace(PVR2_TRACE_STRUCT,"Creating pvr2_v4l2 id=%p",vp);
1213
1214 vp->channel.check_func = pvr2_v4l2_internal_check;
1215
1216 /* register streams */
Mike Isely16eb40d2006-12-30 18:27:32 -03001217 pvr2_v4l2_dev_init(vp->dev_video,vp,VFL_TYPE_GRABBER);
1218 pvr2_v4l2_dev_init(vp->dev_radio,vp,VFL_TYPE_RADIO);
Mike Iselyd8554972006-06-26 20:58:46 -03001219
1220 return vp;
1221}
1222
1223/*
1224 Stuff for Emacs to see, in order to encourage consistent editing style:
1225 *** Local Variables: ***
1226 *** mode: c ***
1227 *** fill-column: 75 ***
1228 *** tab-width: 8 ***
1229 *** c-basic-offset: 8 ***
1230 *** End: ***
1231 */