blob: 486db84092e3eb39360720c120dd7f3f5d4b1907 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -030042#include <media/v4l2-chip-ident.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030043#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030044#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080045
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
47 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030048 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080051#define DRIVER_DESC "Empia em28xx based USB video device driver"
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080055 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030057 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030059static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030060module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030063#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030067 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030070
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020075static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030076static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
77static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
78
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030081module_param_array(radio_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030082MODULE_PARM_DESC(video_nr, "video device numbers");
83MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080085
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030086static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030087module_param(video_debug, int, 0644);
88MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080089
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030090/* supported video standards */
91static struct em28xx_fmt format[] = {
92 {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030093 .name = "16 bpp YUY2, 4:2:2, packed",
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -030094 .fourcc = V4L2_PIX_FMT_YUYV,
95 .depth = 16,
Devin Heitmueller3fbf9302008-12-29 23:34:37 -030096 .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030097 }, {
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -030098 .name = "16 bpp RGB 565, LE",
Mauro Carvalho Chehab43cb9fe2009-06-30 08:36:17 -030099 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
Mauro Carvalho Chehab58fc1ce2009-07-03 02:54:18 -0300101 .reg = EM28XX_OUTFMT_RGB_16_656,
102 }, {
103 .name = "8 bpp Bayer BGBG..GRGR",
104 .fourcc = V4L2_PIX_FMT_SBGGR8,
105 .depth = 8,
106 .reg = EM28XX_OUTFMT_RGB_8_BGBG,
107 }, {
108 .name = "8 bpp Bayer GRGR..BGBG",
109 .fourcc = V4L2_PIX_FMT_SGRBG8,
110 .depth = 8,
111 .reg = EM28XX_OUTFMT_RGB_8_GRGR,
112 }, {
113 .name = "8 bpp Bayer GBGB..RGRG",
114 .fourcc = V4L2_PIX_FMT_SGBRG8,
115 .depth = 8,
116 .reg = EM28XX_OUTFMT_RGB_8_GBGB,
117 }, {
118 .name = "12 bpp YUV411",
119 .fourcc = V4L2_PIX_FMT_YUV411P,
120 .depth = 12,
121 .reg = EM28XX_OUTFMT_YUV411,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300122 },
123};
124
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800125/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200126/* Common to all boards */
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -0300127static struct v4l2_queryctrl ac97_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200129 .id = V4L2_CID_AUDIO_VOLUME,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "Volume",
132 .minimum = 0x0,
133 .maximum = 0x1f,
134 .step = 0x1,
135 .default_value = 0x1f,
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300136 .flags = V4L2_CTRL_FLAG_SLIDER,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300137 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200138 .id = V4L2_CID_AUDIO_MUTE,
139 .type = V4L2_CTRL_TYPE_BOOLEAN,
140 .name = "Mute",
141 .minimum = 0,
142 .maximum = 1,
143 .step = 1,
144 .default_value = 1,
145 .flags = 0,
146 }
147};
148
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300149/* ------------------------------------------------------------------
150 DMA and thread functions
151 ------------------------------------------------------------------*/
152
153/*
154 * Announces that a buffer were filled and request the next
155 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300157 struct em28xx_dmaqueue *dma_q,
158 struct em28xx_buffer *buf)
159{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160 /* Advice that buffer was filled */
161 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
162 buf->vb.state = VIDEOBUF_DONE;
163 buf->vb.field_count++;
164 do_gettimeofday(&buf->vb.ts);
165
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300166 dev->isoc_ctl.vid_buf = NULL;
167
168 list_del(&buf->vb.queue);
169 wake_up(&buf->vb.done);
170}
171
172static inline void vbi_buffer_filled(struct em28xx *dev,
173 struct em28xx_dmaqueue *dma_q,
174 struct em28xx_buffer *buf)
175{
176 /* Advice that buffer was filled */
177 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
178
179 buf->vb.state = VIDEOBUF_DONE;
180 buf->vb.field_count++;
181 do_gettimeofday(&buf->vb.ts);
182
183 dev->isoc_ctl.vbi_buf = NULL;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300184
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300185 list_del(&buf->vb.queue);
186 wake_up(&buf->vb.done);
187}
188
189/*
190 * Identify the buffer header type and properly handles
191 */
192static void em28xx_copy_video(struct em28xx *dev,
193 struct em28xx_dmaqueue *dma_q,
194 struct em28xx_buffer *buf,
195 unsigned char *p,
196 unsigned char *outp, unsigned long len)
197{
198 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300199 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300200 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201
202 if (dma_q->pos + len > buf->vb.size)
203 len = buf->vb.size - dma_q->pos;
204
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300205 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300206 em28xx_isocdbg("frame is not complete\n");
207 len += 4;
208 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300209 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300210
211 startread = p;
212 remain = len;
213
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300214 if (dev->progressive)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215 fieldstart = outp;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300216 else {
217 /* Interlaces two half frames */
218 if (buf->top_field)
219 fieldstart = outp;
220 else
221 fieldstart = outp + bytesperline;
222 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300224 linesdone = dma_q->pos / bytesperline;
225 currlinedone = dma_q->pos % bytesperline;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300226
227 if (dev->progressive)
228 offset = linesdone * bytesperline + currlinedone;
229 else
230 offset = linesdone * bytesperline * 2 + currlinedone;
231
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300232 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300233 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300234 lencopy = lencopy > remain ? remain : lencopy;
235
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300236 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300237 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300238 ((char *)startwrite + lencopy) -
239 ((char *)outp + buf->vb.size));
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300240 remain = (char *)outp + buf->vb.size - (char *)startwrite;
241 lencopy = remain;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300242 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300243 if (lencopy <= 0)
244 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300245 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300246
247 remain -= lencopy;
248
249 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300250 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300251 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300252 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300253 lencopy = remain;
254 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300255 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300256
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300257 if ((char *)startwrite + lencopy > (char *)outp +
258 buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300259 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300260 ((char *)startwrite + lencopy) -
261 ((char *)outp + buf->vb.size));
262 lencopy = remain = (char *)outp + buf->vb.size -
263 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300264 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300265 if (lencopy <= 0)
266 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300267
268 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300269
270 remain -= lencopy;
271 }
272
273 dma_q->pos += len;
274}
275
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300276static void em28xx_copy_vbi(struct em28xx *dev,
277 struct em28xx_dmaqueue *dma_q,
278 struct em28xx_buffer *buf,
279 unsigned char *p,
280 unsigned char *outp, unsigned long len)
281{
282 void *startwrite, *startread;
283 int offset;
284 int bytesperline = 720;
285
286 if (dev == NULL) {
287 printk("dev is null\n");
288 return;
289 }
290
291 if (dma_q == NULL) {
292 printk("dma_q is null\n");
293 return;
294 }
295 if (buf == NULL) {
296 return;
297 }
298 if (p == NULL) {
299 printk("p is null\n");
300 return;
301 }
302 if (outp == NULL) {
303 printk("outp is null\n");
304 return;
305 }
306
307 if (dma_q->pos + len > buf->vb.size)
308 len = buf->vb.size - dma_q->pos;
309
310 if ((p[0] == 0x33 && p[1] == 0x95) ||
311 (p[0] == 0x88 && p[1] == 0x88)) {
312 /* Header field, advance past it */
313 p += 4;
314 } else {
315 len += 4;
316 }
317
318 startread = p;
319
320 startwrite = outp + dma_q->pos;
321 offset = dma_q->pos;
322
323 /* Make sure the bottom field populates the second half of the frame */
324 if (buf->top_field == 0) {
325 startwrite += bytesperline * 0x0c;
326 offset += bytesperline * 0x0c;
327 }
328
329 memcpy(startwrite, startread, len);
330 dma_q->pos += len;
331}
332
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300333static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300334 int packet, int status)
335{
336 char *errmsg = "Unknown";
337
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300338 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300339 case -ENOENT:
340 errmsg = "unlinked synchronuously";
341 break;
342 case -ECONNRESET:
343 errmsg = "unlinked asynchronuously";
344 break;
345 case -ENOSR:
346 errmsg = "Buffer error (overrun)";
347 break;
348 case -EPIPE:
349 errmsg = "Stalled (device not responding)";
350 break;
351 case -EOVERFLOW:
352 errmsg = "Babble (bad cable?)";
353 break;
354 case -EPROTO:
355 errmsg = "Bit-stuff error (bad cable?)";
356 break;
357 case -EILSEQ:
358 errmsg = "CRC/Timeout (could be anything)";
359 break;
360 case -ETIME:
361 errmsg = "Device does not respond";
362 break;
363 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300364 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
366 } else {
367 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
368 packet, status, errmsg);
369 }
370}
371
372/*
373 * video-buf generic routine to get the next available buffer
374 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300375static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300376 struct em28xx_buffer **buf)
377{
378 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300379 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300380
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300381 if (list_empty(&dma_q->active)) {
382 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300383 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300384 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300385 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300386 }
387
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300388 /* Get the next buffer */
389 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
390
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300391 /* Cleans up buffer - Usefull for testing for frame/URB loss */
392 outp = videobuf_to_vmalloc(&(*buf)->vb);
393 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300394
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300395 dev->isoc_ctl.vid_buf = *buf;
396
397 return;
398}
399
400/*
401 * video-buf generic routine to get the next available VBI buffer
402 */
403static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
404 struct em28xx_buffer **buf)
405{
406 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
407 char *outp;
408
409 if (list_empty(&dma_q->active)) {
410 em28xx_isocdbg("No active queue to serve\n");
411 dev->isoc_ctl.vbi_buf = NULL;
412 *buf = NULL;
413 return;
414 }
415
416 /* Get the next buffer */
417 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
418 /* Cleans up buffer - Usefull for testing for frame/URB loss */
419 outp = videobuf_to_vmalloc(&(*buf)->vb);
420 memset(outp, 0x00, (*buf)->vb.size);
421
422 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300423
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300424 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300425}
426
427/*
428 * Controls the isoc copy of each urb packet
429 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300430static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300431{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300432 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300433 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300434 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300435 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300436 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437
438 if (!dev)
439 return 0;
440
441 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
442 return 0;
443
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300444 if (urb->status < 0) {
445 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300446 if (urb->status == -ENOENT)
447 return 0;
448 }
449
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300450 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300451 if (buf != NULL)
452 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300453
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 for (i = 0; i < urb->number_of_packets; i++) {
455 int status = urb->iso_frame_desc[i].status;
456
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300457 if (status < 0) {
458 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459 if (urb->iso_frame_desc[i].status != -EPROTO)
460 continue;
461 }
462
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300463 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464
465 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300466 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300467 continue;
468 }
469 if (urb->iso_frame_desc[i].actual_length >
470 dev->max_pkt_size) {
471 em28xx_isocdbg("packet bigger than packet size");
472 continue;
473 }
474
475 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300476
477 /* FIXME: incomplete buffer checks where removed to make
478 logic simpler. Impacts of those changes should be evaluated
479 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300480 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
481 em28xx_isocdbg("VBI HEADER!!!\n");
482 /* FIXME: Should add vbi copy */
483 continue;
484 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300485 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300486 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300487 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300489 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300490 if (buf != NULL)
491 buffer_filled(dev, dma_q, buf);
492 get_next_buf(dma_q, &buf);
493 if (buf == NULL)
494 outp = NULL;
495 else
496 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300497 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300498
499 if (buf != NULL) {
500 if (p[2] & 1)
501 buf->top_field = 0;
502 else
503 buf->top_field = 1;
504 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300505
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300506 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300508 if (buf != NULL)
509 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300510 }
511 return rc;
512}
513
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300514/* Version of isoc handler that takes into account a mixture of video and
515 VBI data */
516static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
517{
518 struct em28xx_buffer *buf, *vbi_buf;
519 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300520 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300521 unsigned char *outp = NULL;
522 unsigned char *vbioutp = NULL;
523 int i, len = 0, rc = 1;
524 unsigned char *p;
525 int vbi_size;
526
527 if (!dev)
528 return 0;
529
530 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
531 return 0;
532
533 if (urb->status < 0) {
534 print_err_status(dev, -1, urb->status);
535 if (urb->status == -ENOENT)
536 return 0;
537 }
538
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300539 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300540 if (buf != NULL)
541 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300542
543 vbi_buf = dev->isoc_ctl.vbi_buf;
544 if (vbi_buf != NULL)
545 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
546
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300547 for (i = 0; i < urb->number_of_packets; i++) {
548 int status = urb->iso_frame_desc[i].status;
549
550 if (status < 0) {
551 print_err_status(dev, i, status);
552 if (urb->iso_frame_desc[i].status != -EPROTO)
553 continue;
554 }
555
556 len = urb->iso_frame_desc[i].actual_length - 4;
557
558 if (urb->iso_frame_desc[i].actual_length <= 0) {
559 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
560 continue;
561 }
562 if (urb->iso_frame_desc[i].actual_length >
563 dev->max_pkt_size) {
564 em28xx_isocdbg("packet bigger than packet size");
565 continue;
566 }
567
568 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
569
570 /* capture type 0 = vbi start
571 capture type 1 = video start
572 capture type 2 = video in progress */
573 if (p[0] == 0x33 && p[1] == 0x95) {
574 dev->capture_type = 0;
575 dev->vbi_read = 0;
576 em28xx_isocdbg("VBI START HEADER!!!\n");
577 dev->cur_field = p[2];
578 }
579
580 /* FIXME: get rid of hard-coded value */
581 vbi_size = 720 * 0x0c;
582
583 if (dev->capture_type == 0) {
584 if (dev->vbi_read >= vbi_size) {
585 /* We've already read all the VBI data, so
586 treat the rest as video */
587 printk("djh c should never happen\n");
588 } else if ((dev->vbi_read + len) < vbi_size) {
589 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300590 if (dev->vbi_read == 0 &&
591 (!(dev->cur_field & 1))) {
592 /* Brand new frame */
593 if (vbi_buf != NULL)
594 vbi_buffer_filled(dev,
595 vbi_dma_q,
596 vbi_buf);
597 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
598 if (vbi_buf == NULL)
599 vbioutp = NULL;
600 else {
601 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
602 }
603 }
604
605 if (dev->vbi_read == 0) {
606 vbi_dma_q->pos = 0;
607 if (vbi_buf != NULL) {
608 if (dev->cur_field & 1)
609 vbi_buf->top_field = 0;
610 else
611 vbi_buf->top_field = 1;
612 }
613 }
614
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300615 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300616 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
617 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300618 } else {
619 /* Some of this frame is VBI data and some is
620 video data */
621 int vbi_data_len = vbi_size - dev->vbi_read;
622 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300623 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
624 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300625 dev->capture_type = 1;
626 p += vbi_data_len;
627 len -= vbi_data_len;
628 }
629 }
630
631 if (dev->capture_type == 1) {
632 dev->capture_type = 2;
633 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
634 len, (p[2] & 1) ? "odd" : "even");
635
636 if (dev->progressive || !(dev->cur_field & 1)) {
637 if (buf != NULL)
638 buffer_filled(dev, dma_q, buf);
639 get_next_buf(dma_q, &buf);
640 if (buf == NULL)
641 outp = NULL;
642 else
643 outp = videobuf_to_vmalloc(&buf->vb);
644 }
645 if (buf != NULL) {
646 if (dev->cur_field & 1)
647 buf->top_field = 0;
648 else
649 buf->top_field = 1;
650 }
651
652 dma_q->pos = 0;
653 }
654 if (buf != NULL && dev->capture_type == 2)
655 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
656 }
657 return rc;
658}
659
660
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300662 Videobuf operations
663 ------------------------------------------------------------------*/
664
665static int
666buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
667{
668 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300669 struct em28xx *dev = fh->dev;
670 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300671
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300672 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
673
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300674 if (0 == *count)
675 *count = EM28XX_DEF_BUF;
676
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300677 if (*count < EM28XX_MIN_BUF)
678 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300680 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300681 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300682 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300683 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300684
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300685 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300686
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300687 return 0;
688}
689
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300690/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300691static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
692{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300693 struct em28xx_fh *fh = vq->priv_data;
694 struct em28xx *dev = fh->dev;
695 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300696 if (in_interrupt())
697 BUG();
698
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300699 /* We used to wait for the buffer to finish here, but this didn't work
700 because, as we were keeping the state as VIDEOBUF_QUEUED,
701 videobuf_queue_cancel marked it as finished for us.
702 (Also, it could wedge forever if the hardware was misconfigured.)
703
704 This should be safe; by the time we get here, the buffer isn't
705 queued anymore. If we ever start marking the buffers as
706 VIDEOBUF_ACTIVE, it won't be, though.
707 */
708 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300709 if (dev->isoc_ctl.vid_buf == buf)
710 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300711 spin_unlock_irqrestore(&dev->slock, flags);
712
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300713 videobuf_vmalloc_free(&buf->vb);
714 buf->vb.state = VIDEOBUF_NEEDS_INIT;
715}
716
717static int
718buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
719 enum v4l2_field field)
720{
721 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300722 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300723 struct em28xx *dev = fh->dev;
724 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300725
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300726 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300727
728 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
729 return -EINVAL;
730
Brandon Philips05612972008-04-13 14:57:01 -0300731 buf->vb.width = dev->width;
732 buf->vb.height = dev->height;
733 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300734
735 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300736 rc = videobuf_iolock(vq, &buf->vb, NULL);
737 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300738 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300739 }
740
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300742 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300743
744 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300745 if (em28xx_vbi_supported(dev) == 1)
746 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
747 EM28XX_NUM_BUFS,
748 dev->max_pkt_size,
749 em28xx_isoc_copy_vbi);
750 else
751 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
752 EM28XX_NUM_BUFS,
753 dev->max_pkt_size,
754 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300755 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300756 goto fail;
757 }
758
759 buf->vb.state = VIDEOBUF_PREPARED;
760 return 0;
761
762fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300763 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300764 return rc;
765}
766
767static void
768buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
769{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300770 struct em28xx_buffer *buf = container_of(vb,
771 struct em28xx_buffer,
772 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300773 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300774 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300775 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300776
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300777 buf->vb.state = VIDEOBUF_QUEUED;
778 list_add_tail(&buf->vb.queue, &vidq->active);
779
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300780}
781
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300782static void buffer_release(struct videobuf_queue *vq,
783 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300784{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300785 struct em28xx_buffer *buf = container_of(vb,
786 struct em28xx_buffer,
787 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300788 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300789 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300790
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300791 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300792
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300793 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300794}
795
796static struct videobuf_queue_ops em28xx_video_qops = {
797 .buf_setup = buffer_setup,
798 .buf_prepare = buffer_prepare,
799 .buf_queue = buffer_queue,
800 .buf_release = buffer_release,
801};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800802
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300803/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800804
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800805static void video_mux(struct em28xx *dev, int index)
806{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800807 dev->ctl_input = index;
808 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300809 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800810
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300811 if (!dev->ctl_aoutput)
812 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
813
Hans Verkuil5325b422009-04-02 11:26:22 -0300814 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
815 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800816
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300817 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300818 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300819 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
820 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300821 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300822 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300823 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
824 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800825 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300826
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300827 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300828 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
829 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300830 }
831
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300832 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800833}
834
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300835/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300836static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300837{
838 struct em28xx *dev = fh->dev;
839
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300840 if (fh->resources & bit)
841 /* have it already allocated */
842 return 1;
843
844 /* is it free? */
845 mutex_lock(&dev->lock);
846 if (dev->resources & bit) {
847 /* no, someone else uses it */
848 mutex_unlock(&dev->lock);
849 return 0;
850 }
851 /* it's free, grab it */
852 fh->resources |= bit;
853 dev->resources |= bit;
854 em28xx_videodbg("res: get %d\n", bit);
855 mutex_unlock(&dev->lock);
856 return 1;
857}
858
859static int res_check(struct em28xx_fh *fh, unsigned int bit)
860{
861 return (fh->resources & bit);
862}
863
864static int res_locked(struct em28xx *dev, unsigned int bit)
865{
866 return (dev->resources & bit);
867}
868
869static void res_free(struct em28xx_fh *fh, unsigned int bits)
870{
871 struct em28xx *dev = fh->dev;
872
873 BUG_ON((fh->resources & bits) != bits);
874
875 mutex_lock(&dev->lock);
876 fh->resources &= ~bits;
877 dev->resources &= ~bits;
878 em28xx_videodbg("res: put %d\n", bits);
879 mutex_unlock(&dev->lock);
880}
881
882static int get_ressource(struct em28xx_fh *fh)
883{
884 switch (fh->type) {
885 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
886 return EM28XX_RESOURCE_VIDEO;
887 case V4L2_BUF_TYPE_VBI_CAPTURE:
888 return EM28XX_RESOURCE_VBI;
889 default:
890 BUG();
891 return 0;
892 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300893}
894
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800895/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300896 * ac97_queryctrl()
897 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300898 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300899static int ac97_queryctrl(struct v4l2_queryctrl *qc)
900{
901 int i;
902
903 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
904 if (qc->id && qc->id == ac97_qctrl[i].id) {
905 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
906 return 0;
907 }
908 }
909
910 /* Control is not ac97 related */
911 return 1;
912}
913
914/*
915 * ac97_get_ctrl()
916 * return the current values for ac97 mute and volume
917 */
918static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300919{
920 switch (ctrl->id) {
921 case V4L2_CID_AUDIO_MUTE:
922 ctrl->value = dev->mute;
923 return 0;
924 case V4L2_CID_AUDIO_VOLUME:
925 ctrl->value = dev->volume;
926 return 0;
927 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300928 /* Control is not ac97 related */
929 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300930 }
931}
932
933/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300934 * ac97_set_ctrl()
935 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300936 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300937static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300938{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300939 int i;
940
941 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
942 if (ctrl->id == ac97_qctrl[i].id)
943 goto handle;
944
945 /* Announce that hasn't handle it */
946 return 1;
947
948handle:
949 if (ctrl->value < ac97_qctrl[i].minimum ||
950 ctrl->value > ac97_qctrl[i].maximum)
951 return -ERANGE;
952
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300953 switch (ctrl->id) {
954 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300955 dev->mute = ctrl->value;
956 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300957 case V4L2_CID_AUDIO_VOLUME:
958 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300959 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300961
962 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963}
964
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300965static int check_dev(struct em28xx *dev)
966{
967 if (dev->state & DEV_DISCONNECTED) {
968 em28xx_errdev("v4l2 ioctl: device not present\n");
969 return -ENODEV;
970 }
971
972 if (dev->state & DEV_MISCONFIGURED) {
973 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
974 "close and open it again\n");
975 return -EIO;
976 }
977 return 0;
978}
979
980static void get_scale(struct em28xx *dev,
981 unsigned int width, unsigned int height,
982 unsigned int *hscale, unsigned int *vscale)
983{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300984 unsigned int maxw = norm_maxw(dev);
985 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986
987 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
988 if (*hscale >= 0x4000)
989 *hscale = 0x3fff;
990
991 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
992 if (*vscale >= 0x4000)
993 *vscale = 0x3fff;
994}
995
996/* ------------------------------------------------------------------
997 IOCTL vidioc handling
998 ------------------------------------------------------------------*/
999
Hans Verkuil78b526a2008-05-28 12:16:41 -03001000static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001001 struct v4l2_format *f)
1002{
1003 struct em28xx_fh *fh = priv;
1004 struct em28xx *dev = fh->dev;
1005
1006 mutex_lock(&dev->lock);
1007
1008 f->fmt.pix.width = dev->width;
1009 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001010 f->fmt.pix.pixelformat = dev->format->fourcc;
1011 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001012 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1014
1015 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001016 if (dev->progressive)
1017 f->fmt.pix.field = V4L2_FIELD_NONE;
1018 else
1019 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001020 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1021
1022 mutex_unlock(&dev->lock);
1023 return 0;
1024}
1025
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001026static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1027{
1028 unsigned int i;
1029
1030 for (i = 0; i < ARRAY_SIZE(format); i++)
1031 if (format[i].fourcc == fourcc)
1032 return &format[i];
1033
1034 return NULL;
1035}
1036
Hans Verkuil78b526a2008-05-28 12:16:41 -03001037static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001038 struct v4l2_format *f)
1039{
1040 struct em28xx_fh *fh = priv;
1041 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001042 unsigned int width = f->fmt.pix.width;
1043 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001044 unsigned int maxw = norm_maxw(dev);
1045 unsigned int maxh = norm_maxh(dev);
1046 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001047 struct em28xx_fmt *fmt;
1048
1049 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1050 if (!fmt) {
1051 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1052 f->fmt.pix.pixelformat);
1053 return -EINVAL;
1054 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001055
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001056 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001057 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001058 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1059 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1060 /* According to empiatech support the MaxPacketSize is too small
1061 * to support framesizes larger than 640x480 @ 30 fps or 640x576
1062 * @ 25 fps. As this would cut of a part of the image we prefer
1063 * 360x576 or 360x480 for now */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064 if (width == maxw && height == maxh)
1065 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001066 } else {
1067 /* width must even because of the YUYV format
1068 height must be even because of interlacing */
1069 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001070 }
1071
1072 get_scale(dev, width, height, &hscale, &vscale);
1073
1074 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1075 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1076
1077 f->fmt.pix.width = width;
1078 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001079 f->fmt.pix.pixelformat = fmt->fourcc;
1080 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1081 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001082 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001083 if (dev->progressive)
1084 f->fmt.pix.field = V4L2_FIELD_NONE;
1085 else
1086 f->fmt.pix.field = dev->interlaced ?
1087 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001088
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001089 return 0;
1090}
1091
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001092static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1093 unsigned width, unsigned height)
1094{
1095 struct em28xx_fmt *fmt;
1096
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001097 fmt = format_by_fourcc(fourcc);
1098 if (!fmt)
1099 return -EINVAL;
1100
1101 dev->format = fmt;
1102 dev->width = width;
1103 dev->height = height;
1104
1105 /* set new image size */
1106 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1107
1108 em28xx_set_alternate(dev);
1109 em28xx_resolution_set(dev);
1110
1111 return 0;
1112}
1113
Hans Verkuil78b526a2008-05-28 12:16:41 -03001114static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001115 struct v4l2_format *f)
1116{
1117 struct em28xx_fh *fh = priv;
1118 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001119 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001120
1121 rc = check_dev(dev);
1122 if (rc < 0)
1123 return rc;
1124
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001125 mutex_lock(&dev->lock);
1126
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001127 vidioc_try_fmt_vid_cap(file, priv, f);
1128
Brandon Philips05612972008-04-13 14:57:01 -03001129 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1130 em28xx_errdev("%s queue busy\n", __func__);
1131 rc = -EBUSY;
1132 goto out;
1133 }
1134
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001135 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1136 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -03001137
1138out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001139 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -03001140 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001141}
1142
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001143static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1144{
1145 struct em28xx_fh *fh = priv;
1146 struct em28xx *dev = fh->dev;
1147 struct v4l2_format f;
1148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 *norm = dev->norm;
1155
1156 return 0;
1157}
1158
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001159static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160{
1161 struct em28xx_fh *fh = priv;
1162 struct em28xx *dev = fh->dev;
1163 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001164 int rc;
1165
1166 rc = check_dev(dev);
1167 if (rc < 0)
1168 return rc;
1169
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001170 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001171 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001172
1173 /* Adjusts width/height, if needed */
1174 f.fmt.pix.width = dev->width;
1175 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001176 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001177
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001178 /* set new image size */
1179 dev->width = f.fmt.pix.width;
1180 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1182
1183 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001184 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001185
1186 mutex_unlock(&dev->lock);
1187 return 0;
1188}
1189
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001190static int vidioc_g_parm(struct file *file, void *priv,
1191 struct v4l2_streamparm *p)
1192{
1193 struct em28xx_fh *fh = priv;
1194 struct em28xx *dev = fh->dev;
1195 int rc = 0;
1196
1197 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1198 return -EINVAL;
1199
1200 if (dev->board.is_webcam)
1201 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1202 video, g_parm, p);
1203 else
1204 v4l2_video_std_frame_period(dev->norm,
1205 &p->parm.capture.timeperframe);
1206
1207 return rc;
1208}
1209
1210static int vidioc_s_parm(struct file *file, void *priv,
1211 struct v4l2_streamparm *p)
1212{
1213 struct em28xx_fh *fh = priv;
1214 struct em28xx *dev = fh->dev;
1215
1216 if (!dev->board.is_webcam)
1217 return -EINVAL;
1218
1219 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1220 return -EINVAL;
1221
1222 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1223}
1224
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001225static const char *iname[] = {
1226 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1227 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1228 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1229 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1230 [EM28XX_VMUX_SVIDEO] = "S-Video",
1231 [EM28XX_VMUX_TELEVISION] = "Television",
1232 [EM28XX_VMUX_CABLE] = "Cable TV",
1233 [EM28XX_VMUX_DVB] = "DVB",
1234 [EM28XX_VMUX_DEBUG] = "for debug only",
1235};
1236
1237static int vidioc_enum_input(struct file *file, void *priv,
1238 struct v4l2_input *i)
1239{
1240 struct em28xx_fh *fh = priv;
1241 struct em28xx *dev = fh->dev;
1242 unsigned int n;
1243
1244 n = i->index;
1245 if (n >= MAX_EM28XX_INPUT)
1246 return -EINVAL;
1247 if (0 == INPUT(n)->type)
1248 return -EINVAL;
1249
1250 i->index = n;
1251 i->type = V4L2_INPUT_TYPE_CAMERA;
1252
1253 strcpy(i->name, iname[INPUT(n)->type]);
1254
1255 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1256 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1257 i->type = V4L2_INPUT_TYPE_TUNER;
1258
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001259 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001260
1261 return 0;
1262}
1263
1264static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1265{
1266 struct em28xx_fh *fh = priv;
1267 struct em28xx *dev = fh->dev;
1268
1269 *i = dev->ctl_input;
1270
1271 return 0;
1272}
1273
1274static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1275{
1276 struct em28xx_fh *fh = priv;
1277 struct em28xx *dev = fh->dev;
1278 int rc;
1279
1280 rc = check_dev(dev);
1281 if (rc < 0)
1282 return rc;
1283
1284 if (i >= MAX_EM28XX_INPUT)
1285 return -EINVAL;
1286 if (0 == INPUT(i)->type)
1287 return -EINVAL;
1288
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001289 dev->ctl_input = i;
1290
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001292 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001293 mutex_unlock(&dev->lock);
1294 return 0;
1295}
1296
1297static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1298{
1299 struct em28xx_fh *fh = priv;
1300 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001301
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001302 if (!dev->audio_mode.has_audio)
1303 return -EINVAL;
1304
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001305 switch (a->index) {
1306 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001308 break;
1309 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001310 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001311 break;
1312 case EM28XX_AMUX_VIDEO2:
1313 strcpy(a->name, "Television alt");
1314 break;
1315 case EM28XX_AMUX_PHONE:
1316 strcpy(a->name, "Phone");
1317 break;
1318 case EM28XX_AMUX_MIC:
1319 strcpy(a->name, "Mic");
1320 break;
1321 case EM28XX_AMUX_CD:
1322 strcpy(a->name, "CD");
1323 break;
1324 case EM28XX_AMUX_AUX:
1325 strcpy(a->name, "Aux");
1326 break;
1327 case EM28XX_AMUX_PCM_OUT:
1328 strcpy(a->name, "PCM");
1329 break;
1330 default:
1331 return -EINVAL;
1332 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001333
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001334 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001335 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001336
1337 return 0;
1338}
1339
1340static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1341{
1342 struct em28xx_fh *fh = priv;
1343 struct em28xx *dev = fh->dev;
1344
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001345
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001346 if (!dev->audio_mode.has_audio)
1347 return -EINVAL;
1348
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001349 if (a->index >= MAX_EM28XX_INPUT)
1350 return -EINVAL;
1351 if (0 == INPUT(a->index)->type)
1352 return -EINVAL;
1353
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001354 mutex_lock(&dev->lock);
1355
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001356 dev->ctl_ainput = INPUT(a->index)->amux;
1357 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001358
1359 if (!dev->ctl_aoutput)
1360 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001361
1362 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363 return 0;
1364}
1365
1366static int vidioc_queryctrl(struct file *file, void *priv,
1367 struct v4l2_queryctrl *qc)
1368{
1369 struct em28xx_fh *fh = priv;
1370 struct em28xx *dev = fh->dev;
1371 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001372 int rc;
1373
1374 rc = check_dev(dev);
1375 if (rc < 0)
1376 return rc;
1377
1378 memset(qc, 0, sizeof(*qc));
1379
1380 qc->id = id;
1381
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001382 /* enumberate AC97 controls */
1383 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1384 rc = ac97_queryctrl(qc);
1385 if (!rc)
1386 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001388
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001389 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001390 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001391 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001392 mutex_unlock(&dev->lock);
1393
1394 if (qc->type)
1395 return 0;
1396 else
1397 return -EINVAL;
1398}
1399
1400static int vidioc_g_ctrl(struct file *file, void *priv,
1401 struct v4l2_control *ctrl)
1402{
1403 struct em28xx_fh *fh = priv;
1404 struct em28xx *dev = fh->dev;
1405 int rc;
1406
1407 rc = check_dev(dev);
1408 if (rc < 0)
1409 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001410 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001411
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001412 mutex_lock(&dev->lock);
1413
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001414 /* Set an AC97 control */
1415 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1416 rc = ac97_get_ctrl(dev, ctrl);
1417 else
1418 rc = 1;
1419
1420 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1421 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001422 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001423 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001424 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001425
1426 mutex_unlock(&dev->lock);
1427 return rc;
1428}
1429
1430static int vidioc_s_ctrl(struct file *file, void *priv,
1431 struct v4l2_control *ctrl)
1432{
1433 struct em28xx_fh *fh = priv;
1434 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001435 int rc;
1436
1437 rc = check_dev(dev);
1438 if (rc < 0)
1439 return rc;
1440
1441 mutex_lock(&dev->lock);
1442
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001443 /* Set an AC97 control */
1444 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1445 rc = ac97_set_ctrl(dev, ctrl);
1446 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001447 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001448
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001449 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001450 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001451 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001452
1453 /*
1454 * In the case of non-AC97 volume controls, we still need
1455 * to do some setups at em28xx, in order to mute/unmute
1456 * and to adjust audio volume. However, the value ranges
1457 * should be checked by the corresponding V4L subdriver.
1458 */
1459 switch (ctrl->id) {
1460 case V4L2_CID_AUDIO_MUTE:
1461 dev->mute = ctrl->value;
1462 rc = em28xx_audio_analog_set(dev);
1463 break;
1464 case V4L2_CID_AUDIO_VOLUME:
1465 dev->volume = ctrl->value;
1466 rc = em28xx_audio_analog_set(dev);
1467 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001468 }
1469
1470 mutex_unlock(&dev->lock);
1471 return rc;
1472}
1473
1474static int vidioc_g_tuner(struct file *file, void *priv,
1475 struct v4l2_tuner *t)
1476{
1477 struct em28xx_fh *fh = priv;
1478 struct em28xx *dev = fh->dev;
1479 int rc;
1480
1481 rc = check_dev(dev);
1482 if (rc < 0)
1483 return rc;
1484
1485 if (0 != t->index)
1486 return -EINVAL;
1487
1488 strcpy(t->name, "Tuner");
1489
1490 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001491 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001492 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001493
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001494 return 0;
1495}
1496
1497static int vidioc_s_tuner(struct file *file, void *priv,
1498 struct v4l2_tuner *t)
1499{
1500 struct em28xx_fh *fh = priv;
1501 struct em28xx *dev = fh->dev;
1502 int rc;
1503
1504 rc = check_dev(dev);
1505 if (rc < 0)
1506 return rc;
1507
1508 if (0 != t->index)
1509 return -EINVAL;
1510
1511 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001512 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001513 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001514
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001515 return 0;
1516}
1517
1518static int vidioc_g_frequency(struct file *file, void *priv,
1519 struct v4l2_frequency *f)
1520{
1521 struct em28xx_fh *fh = priv;
1522 struct em28xx *dev = fh->dev;
1523
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001524 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001526 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001527 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001528
1529 return 0;
1530}
1531
1532static int vidioc_s_frequency(struct file *file, void *priv,
1533 struct v4l2_frequency *f)
1534{
1535 struct em28xx_fh *fh = priv;
1536 struct em28xx *dev = fh->dev;
1537 int rc;
1538
1539 rc = check_dev(dev);
1540 if (rc < 0)
1541 return rc;
1542
1543 if (0 != f->tuner)
1544 return -EINVAL;
1545
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001546 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1547 return -EINVAL;
1548 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001549 return -EINVAL;
1550
1551 mutex_lock(&dev->lock);
1552
1553 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001554 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555
1556 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001557
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001558 return 0;
1559}
1560
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001561#ifdef CONFIG_VIDEO_ADV_DEBUG
1562static int em28xx_reg_len(int reg)
1563{
1564 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001565 case EM28XX_R40_AC97LSB:
1566 case EM28XX_R30_HSCALELOW:
1567 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001568 return 2;
1569 default:
1570 return 1;
1571 }
1572}
1573
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001574static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001575 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001576{
1577 struct em28xx_fh *fh = priv;
1578 struct em28xx *dev = fh->dev;
1579
1580 chip->ident = V4L2_IDENT_NONE;
1581 chip->revision = 0;
1582
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001583 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001584
1585 return 0;
1586}
1587
1588
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001589static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001590 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001591{
1592 struct em28xx_fh *fh = priv;
1593 struct em28xx *dev = fh->dev;
1594 int ret;
1595
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001596 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001597 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001598 mutex_lock(&dev->lock);
1599 ret = em28xx_read_ac97(dev, reg->reg);
1600 mutex_unlock(&dev->lock);
1601 if (ret < 0)
1602 return ret;
1603
1604 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001605 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001606 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001607 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001608 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001609 return 0;
1610 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001611 /* TODO: is this correct? */
1612 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1613 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001614 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001615 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001616 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001617 }
1618
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001619 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001620 reg->size = em28xx_reg_len(reg->reg);
1621 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001622 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001623 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001624 mutex_unlock(&dev->lock);
1625
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001626 if (ret < 0)
1627 return ret;
1628
1629 reg->val = ret;
1630 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001631 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001632 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001633 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1634 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001635 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001636 if (ret < 0)
1637 return ret;
1638
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001639 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001640 }
1641
1642 return 0;
1643}
1644
1645static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001646 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001647{
1648 struct em28xx_fh *fh = priv;
1649 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001650 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001651 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001652
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001653 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001654 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001655 mutex_lock(&dev->lock);
1656 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1657 mutex_unlock(&dev->lock);
1658
1659 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001660 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001661 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001662 return 0;
1663 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001664 /* TODO: is this correct? */
1665 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1666 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001667 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001668 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001669 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001670 }
1671
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001672 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001673 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001674
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001675 mutex_lock(&dev->lock);
1676 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1677 em28xx_reg_len(reg->reg));
1678 mutex_unlock(&dev->lock);
1679
1680 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001681}
1682#endif
1683
1684
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001685static int vidioc_cropcap(struct file *file, void *priv,
1686 struct v4l2_cropcap *cc)
1687{
1688 struct em28xx_fh *fh = priv;
1689 struct em28xx *dev = fh->dev;
1690
1691 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1692 return -EINVAL;
1693
1694 cc->bounds.left = 0;
1695 cc->bounds.top = 0;
1696 cc->bounds.width = dev->width;
1697 cc->bounds.height = dev->height;
1698 cc->defrect = cc->bounds;
1699 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1700 cc->pixelaspect.denominator = 59;
1701
1702 return 0;
1703}
1704
1705static int vidioc_streamon(struct file *file, void *priv,
1706 enum v4l2_buf_type type)
1707{
1708 struct em28xx_fh *fh = priv;
1709 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001710 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001711
1712 rc = check_dev(dev);
1713 if (rc < 0)
1714 return rc;
1715
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001716 if (unlikely(type != fh->type))
1717 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001718
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1720 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001721
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001722 if (unlikely(!res_get(fh,get_ressource(fh))))
1723 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001724
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001725 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1726 rc = videobuf_streamon(&fh->vb_vidq);
1727 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1728 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001729
1730 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001731}
1732
1733static int vidioc_streamoff(struct file *file, void *priv,
1734 enum v4l2_buf_type type)
1735{
1736 struct em28xx_fh *fh = priv;
1737 struct em28xx *dev = fh->dev;
1738 int rc;
1739
1740 rc = check_dev(dev);
1741 if (rc < 0)
1742 return rc;
1743
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001744 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1745 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001746 return -EINVAL;
1747 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001748 return -EINVAL;
1749
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001750 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1751 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001752
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001753 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001754 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001755 res_free(fh, EM28XX_RESOURCE_VIDEO);
1756 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001757 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001758 res_free(fh, EM28XX_RESOURCE_VBI);
1759 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001760
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001761 return 0;
1762}
1763
1764static int vidioc_querycap(struct file *file, void *priv,
1765 struct v4l2_capability *cap)
1766{
1767 struct em28xx_fh *fh = priv;
1768 struct em28xx *dev = fh->dev;
1769
1770 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1771 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001772 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001773
1774 cap->version = EM28XX_VERSION_CODE;
1775
1776 cap->capabilities =
1777 V4L2_CAP_SLICED_VBI_CAPTURE |
1778 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001779 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1780
Devin Heitmueller04146142009-09-11 00:08:44 -03001781 if (dev->vbi_dev)
1782 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1783
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001784 if (dev->audio_mode.has_audio)
1785 cap->capabilities |= V4L2_CAP_AUDIO;
1786
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001787 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001788 cap->capabilities |= V4L2_CAP_TUNER;
1789
1790 return 0;
1791}
1792
Hans Verkuil78b526a2008-05-28 12:16:41 -03001793static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001794 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001795{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001796 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001797 return -EINVAL;
1798
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001799 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1800 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001801
1802 return 0;
1803}
1804
1805/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001806static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001807 struct v4l2_format *f)
1808{
1809 struct em28xx_fh *fh = priv;
1810 struct em28xx *dev = fh->dev;
1811 int rc;
1812
1813 rc = check_dev(dev);
1814 if (rc < 0)
1815 return rc;
1816
1817 mutex_lock(&dev->lock);
1818
1819 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001820 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001821
1822 if (f->fmt.sliced.service_set == 0)
1823 rc = -EINVAL;
1824
1825 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001826
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001827 return rc;
1828}
1829
Hans Verkuil78b526a2008-05-28 12:16:41 -03001830static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001831 struct v4l2_format *f)
1832{
1833 struct em28xx_fh *fh = priv;
1834 struct em28xx *dev = fh->dev;
1835 int rc;
1836
1837 rc = check_dev(dev);
1838 if (rc < 0)
1839 return rc;
1840
1841 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001842 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001843 mutex_unlock(&dev->lock);
1844
1845 if (f->fmt.sliced.service_set == 0)
1846 return -EINVAL;
1847
1848 return 0;
1849}
1850
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001851/* RAW VBI ioctls */
1852
1853static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1854 struct v4l2_format *format)
1855{
1856 format->fmt.vbi.samples_per_line = 720;
1857 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1858 format->fmt.vbi.offset = 0;
1859 format->fmt.vbi.flags = 0;
1860
1861 /* Varies by video standard (NTSC, PAL, etc.) */
1862 /* FIXME: hard-coded for NTSC support */
1863 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1864 format->fmt.vbi.count[0] = 12;
1865 format->fmt.vbi.count[1] = 12;
1866 format->fmt.vbi.start[0] = 10;
1867 format->fmt.vbi.start[1] = 273;
1868
1869 return 0;
1870}
1871
1872static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1873 struct v4l2_format *format)
1874{
1875 format->fmt.vbi.samples_per_line = 720;
1876 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1877 format->fmt.vbi.offset = 0;
1878 format->fmt.vbi.flags = 0;
1879
1880 /* Varies by video standard (NTSC, PAL, etc.) */
1881 /* FIXME: hard-coded for NTSC support */
1882 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1883 format->fmt.vbi.count[0] = 12;
1884 format->fmt.vbi.count[1] = 12;
1885 format->fmt.vbi.start[0] = 10;
1886 format->fmt.vbi.start[1] = 273;
1887
1888 return 0;
1889}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001890
1891static int vidioc_reqbufs(struct file *file, void *priv,
1892 struct v4l2_requestbuffers *rb)
1893{
1894 struct em28xx_fh *fh = priv;
1895 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001896 int rc;
1897
1898 rc = check_dev(dev);
1899 if (rc < 0)
1900 return rc;
1901
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001902 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1903 return videobuf_reqbufs(&fh->vb_vidq, rb);
1904 else
1905 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001906}
1907
1908static int vidioc_querybuf(struct file *file, void *priv,
1909 struct v4l2_buffer *b)
1910{
1911 struct em28xx_fh *fh = priv;
1912 struct em28xx *dev = fh->dev;
1913 int rc;
1914
1915 rc = check_dev(dev);
1916 if (rc < 0)
1917 return rc;
1918
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001919 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1920 return videobuf_querybuf(&fh->vb_vidq, b);
1921 else {
1922 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1923 the videobuf framework, but we probably shouldn't be
1924 returning a buffer larger than that which was asked for.
1925 At a minimum, it causes a crash in zvbi since it does
1926 a memcpy based on the source buffer length */
1927 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1928 b->length = 17280;
1929 return result;
1930 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001931}
1932
1933static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1934{
1935 struct em28xx_fh *fh = priv;
1936 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001937 int rc;
1938
1939 rc = check_dev(dev);
1940 if (rc < 0)
1941 return rc;
1942
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001943 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1944 return videobuf_qbuf(&fh->vb_vidq, b);
1945 else {
1946 return videobuf_qbuf(&fh->vb_vbiq, b);
1947 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001948}
1949
1950static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1951{
1952 struct em28xx_fh *fh = priv;
1953 struct em28xx *dev = fh->dev;
1954 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001955
1956 rc = check_dev(dev);
1957 if (rc < 0)
1958 return rc;
1959
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001960 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1961 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1962 O_NONBLOCK);
1963 else
1964 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1965 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001966}
1967
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001968#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001969static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001970{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001971 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001972
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001973 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1974 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1975 else
1976 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001977}
1978#endif
1979
1980
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001981/* ----------------------------------------------------------- */
1982/* RADIO ESPECIFIC IOCTLS */
1983/* ----------------------------------------------------------- */
1984
1985static int radio_querycap(struct file *file, void *priv,
1986 struct v4l2_capability *cap)
1987{
1988 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1989
1990 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1991 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001992 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001993
1994 cap->version = EM28XX_VERSION_CODE;
1995 cap->capabilities = V4L2_CAP_TUNER;
1996 return 0;
1997}
1998
1999static int radio_g_tuner(struct file *file, void *priv,
2000 struct v4l2_tuner *t)
2001{
2002 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2003
2004 if (unlikely(t->index > 0))
2005 return -EINVAL;
2006
2007 strcpy(t->name, "Radio");
2008 t->type = V4L2_TUNER_RADIO;
2009
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002010 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002011 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002012 mutex_unlock(&dev->lock);
2013
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002014 return 0;
2015}
2016
2017static int radio_enum_input(struct file *file, void *priv,
2018 struct v4l2_input *i)
2019{
2020 if (i->index != 0)
2021 return -EINVAL;
2022 strcpy(i->name, "Radio");
2023 i->type = V4L2_INPUT_TYPE_TUNER;
2024
2025 return 0;
2026}
2027
2028static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2029{
2030 if (unlikely(a->index))
2031 return -EINVAL;
2032
2033 strcpy(a->name, "Radio");
2034 return 0;
2035}
2036
2037static int radio_s_tuner(struct file *file, void *priv,
2038 struct v4l2_tuner *t)
2039{
2040 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2041
2042 if (0 != t->index)
2043 return -EINVAL;
2044
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002045 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002046 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002047 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002048
2049 return 0;
2050}
2051
2052static int radio_s_audio(struct file *file, void *fh,
2053 struct v4l2_audio *a)
2054{
2055 return 0;
2056}
2057
2058static int radio_s_input(struct file *file, void *fh, unsigned int i)
2059{
2060 return 0;
2061}
2062
2063static int radio_queryctrl(struct file *file, void *priv,
2064 struct v4l2_queryctrl *qc)
2065{
2066 int i;
2067
2068 if (qc->id < V4L2_CID_BASE ||
2069 qc->id >= V4L2_CID_LASTP1)
2070 return -EINVAL;
2071
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002072 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2073 if (qc->id && qc->id == ac97_qctrl[i].id) {
2074 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002075 return 0;
2076 }
2077 }
2078
2079 return -EINVAL;
2080}
2081
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002082/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002083 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002084 * inits the device and starts isoc transfer
2085 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002086static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087{
Hans Verkuilbec43662008-12-30 06:58:20 -03002088 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002089 int errCode = 0, radio;
2090 struct em28xx *dev;
2091 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002092 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002093 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002094
Hans Verkuilbec43662008-12-30 06:58:20 -03002095 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002096
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002097 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002098 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002099
2100 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08002101
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002102 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002103 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002104
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002106 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002107 if (!fh) {
2108 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002109 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002110 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002112 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002113 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002114 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002115 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002117 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002118 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002119 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002120 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002121
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002122 /* Needed, since GPIO might have disabled power of
2123 some i2c device
2124 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002125 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002126
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002127 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002128 if (fh->radio) {
2129 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002130 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002131 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002132
2133 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002135 if (dev->progressive)
2136 field = V4L2_FIELD_NONE;
2137 else
2138 field = V4L2_FIELD_INTERLACED;
2139
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002140 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2141 NULL, &dev->slock,
2142 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2143 sizeof(struct em28xx_buffer), fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002144
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002145 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2146 NULL, &dev->slock,
2147 V4L2_BUF_TYPE_VBI_CAPTURE,
2148 V4L2_FIELD_SEQ_TB,
2149 sizeof(struct em28xx_buffer), fh);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002150
Ingo Molnar3593cab2006-02-07 06:49:14 -02002151 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002152
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153 return errCode;
2154}
2155
2156/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002157 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158 * unregisters the v4l2,i2c and usb devices
2159 * called when the device gets disconected or at module unload
2160*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002161void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002162{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002163
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002164 /*FIXME: I2C IR should be disconnected */
2165
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002166 if (dev->radio_dev) {
2167 if (-1 != dev->radio_dev->minor)
2168 video_unregister_device(dev->radio_dev);
2169 else
2170 video_device_release(dev->radio_dev);
2171 dev->radio_dev = NULL;
2172 }
2173 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03002174 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
2175 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002176 if (-1 != dev->vbi_dev->minor)
2177 video_unregister_device(dev->vbi_dev);
2178 else
2179 video_device_release(dev->vbi_dev);
2180 dev->vbi_dev = NULL;
2181 }
2182 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03002183 em28xx_info("V4L2 device /dev/video%d deregistered\n",
2184 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002185 if (-1 != dev->vdev->minor)
2186 video_unregister_device(dev->vdev);
2187 else
2188 video_device_release(dev->vdev);
2189 dev->vdev = NULL;
2190 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002191}
2192
2193/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002194 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002195 * stops streaming and deallocates all resources allocated by the v4l2
2196 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002197 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002198static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002199{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002200 struct em28xx_fh *fh = filp->private_data;
2201 struct em28xx *dev = fh->dev;
2202 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002204 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002206 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002207 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002208 res_free(fh, EM28XX_RESOURCE_VIDEO);
2209 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002210
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002211 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2212 videobuf_stop(&fh->vb_vbiq);
2213 res_free(fh, EM28XX_RESOURCE_VBI);
2214 }
2215
2216 if(dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002217 /* the device is already disconnect,
2218 free the remaining resources */
2219 if (dev->state & DEV_DISCONNECTED) {
2220 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002221 kfree(dev);
2222 return 0;
2223 }
2224
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002225 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03002226 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002227
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002228 /* do this before setting alternate! */
2229 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002230 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002231
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002232 /* set alternate 0 */
2233 dev->alt = 0;
2234 em28xx_videodbg("setting alternate 0\n");
2235 errCode = usb_set_interface(dev->udev, 0, 0);
2236 if (errCode < 0) {
2237 em28xx_errdev("cannot change alternate number to "
2238 "0 (error=%i)\n", errCode);
2239 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002241
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002242 videobuf_mmap_free(&fh->vb_vidq);
2243 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002244 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002245 dev->users--;
2246 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247 return 0;
2248}
2249
2250/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002251 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002252 * will allocate buffers when called for the first time
2253 */
2254static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002255em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002256 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002257{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002258 struct em28xx_fh *fh = filp->private_data;
2259 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002260 int rc;
2261
2262 rc = check_dev(dev);
2263 if (rc < 0)
2264 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002265
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002266 /* FIXME: read() is not prepared to allow changing the video
2267 resolution while streaming. Seems a bug at em28xx_set_fmt
2268 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002269
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002270 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002271 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2272 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002273
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002274 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2275 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002276 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002277
2278
2279 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002280 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2281 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002282
2283 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2284 filp->f_flags & O_NONBLOCK);
2285 }
2286
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002287 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288}
2289
2290/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002292 * will allocate buffers when called for the first time
2293 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002294static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002295{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002296 struct em28xx_fh *fh = filp->private_data;
2297 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002298 int rc;
2299
2300 rc = check_dev(dev);
2301 if (rc < 0)
2302 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002303
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002304 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2305 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2306 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002307 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002308 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2309 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2310 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002311 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002312 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002313 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002314 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002315}
2316
2317/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002318 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002319 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002320static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002321{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002322 struct em28xx_fh *fh = filp->private_data;
2323 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002324 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002325
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002326 rc = check_dev(dev);
2327 if (rc < 0)
2328 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002329
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002330 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2331 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2332 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2333 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002334
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002335 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2336 (unsigned long)vma->vm_start,
2337 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2338 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002339
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002340 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002341}
2342
Hans Verkuilbec43662008-12-30 06:58:20 -03002343static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002344 .owner = THIS_MODULE,
2345 .open = em28xx_v4l2_open,
2346 .release = em28xx_v4l2_close,
2347 .read = em28xx_v4l2_read,
2348 .poll = em28xx_v4l2_poll,
2349 .mmap = em28xx_v4l2_mmap,
2350 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002351};
2352
Hans Verkuila3998102008-07-21 02:57:38 -03002353static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002354 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002355 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2356 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2357 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2358 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002359 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2360 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002361 .vidioc_g_audio = vidioc_g_audio,
2362 .vidioc_s_audio = vidioc_s_audio,
2363 .vidioc_cropcap = vidioc_cropcap,
2364
Hans Verkuil78b526a2008-05-28 12:16:41 -03002365 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2366 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2367 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002368
2369 .vidioc_reqbufs = vidioc_reqbufs,
2370 .vidioc_querybuf = vidioc_querybuf,
2371 .vidioc_qbuf = vidioc_qbuf,
2372 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002373 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002374 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002375 .vidioc_g_parm = vidioc_g_parm,
2376 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002377 .vidioc_enum_input = vidioc_enum_input,
2378 .vidioc_g_input = vidioc_g_input,
2379 .vidioc_s_input = vidioc_s_input,
2380 .vidioc_queryctrl = vidioc_queryctrl,
2381 .vidioc_g_ctrl = vidioc_g_ctrl,
2382 .vidioc_s_ctrl = vidioc_s_ctrl,
2383 .vidioc_streamon = vidioc_streamon,
2384 .vidioc_streamoff = vidioc_streamoff,
2385 .vidioc_g_tuner = vidioc_g_tuner,
2386 .vidioc_s_tuner = vidioc_s_tuner,
2387 .vidioc_g_frequency = vidioc_g_frequency,
2388 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002389#ifdef CONFIG_VIDEO_ADV_DEBUG
2390 .vidioc_g_register = vidioc_g_register,
2391 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002392 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002393#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002394#ifdef CONFIG_VIDEO_V4L1_COMPAT
2395 .vidiocgmbuf = vidiocgmbuf,
2396#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002397};
2398
2399static const struct video_device em28xx_video_template = {
2400 .fops = &em28xx_v4l_fops,
2401 .release = video_device_release,
2402 .ioctl_ops = &video_ioctl_ops,
2403
2404 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002405
2406 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002407 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002408};
2409
Hans Verkuilbec43662008-12-30 06:58:20 -03002410static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002411 .owner = THIS_MODULE,
2412 .open = em28xx_v4l2_open,
2413 .release = em28xx_v4l2_close,
2414 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002415};
2416
2417static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002418 .vidioc_querycap = radio_querycap,
2419 .vidioc_g_tuner = radio_g_tuner,
2420 .vidioc_enum_input = radio_enum_input,
2421 .vidioc_g_audio = radio_g_audio,
2422 .vidioc_s_tuner = radio_s_tuner,
2423 .vidioc_s_audio = radio_s_audio,
2424 .vidioc_s_input = radio_s_input,
2425 .vidioc_queryctrl = radio_queryctrl,
2426 .vidioc_g_ctrl = vidioc_g_ctrl,
2427 .vidioc_s_ctrl = vidioc_s_ctrl,
2428 .vidioc_g_frequency = vidioc_g_frequency,
2429 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002430#ifdef CONFIG_VIDEO_ADV_DEBUG
2431 .vidioc_g_register = vidioc_g_register,
2432 .vidioc_s_register = vidioc_s_register,
2433#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002434};
2435
Hans Verkuila3998102008-07-21 02:57:38 -03002436static struct video_device em28xx_radio_template = {
2437 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002438 .fops = &radio_fops,
2439 .ioctl_ops = &radio_ioctl_ops,
2440 .minor = -1,
2441};
2442
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002443/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002444
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002445
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002446
Adrian Bunk532fe652008-01-28 22:10:48 -03002447static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002448 const struct video_device *template,
2449 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002450{
2451 struct video_device *vfd;
2452
2453 vfd = video_device_alloc();
2454 if (NULL == vfd)
2455 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002456
2457 *vfd = *template;
2458 vfd->minor = -1;
2459 vfd->v4l2_dev = &dev->v4l2_dev;
2460 vfd->release = video_device_release;
2461 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002462
2463 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2464 dev->name, type_name);
2465
2466 return vfd;
2467}
2468
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002469int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002470{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002471 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002472 int ret;
2473
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002474 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2475 dev->name,
2476 (EM28XX_VERSION_CODE >> 16) & 0xff,
2477 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2478
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002479 /* set default norm */
2480 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002481 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002482 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002483
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002484 /* Analog specific initialization */
2485 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002486 em28xx_set_video_format(dev, format[0].fourcc,
2487 norm_maxw(dev), norm_maxh(dev));
2488
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002489 video_mux(dev, dev->ctl_input);
2490
2491 /* Audio defaults */
2492 dev->mute = 1;
2493 dev->volume = 0x1f;
2494
2495 /* enable vbi capturing */
2496
2497/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002498 val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
2499 em28xx_write_reg(dev, EM28XX_R0F_XCLK,
2500 (EM28XX_XCLK_AUDIO_UNMUTE | val));
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002501 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
2502
2503 em28xx_set_outfmt(dev);
2504 em28xx_colorlevels_set_default(dev);
2505 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002506
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002507 /* allocate and fill video video_device struct */
2508 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2509 if (!dev->vdev) {
2510 em28xx_errdev("cannot allocate video_device.\n");
2511 return -ENODEV;
2512 }
2513
2514 /* register v4l2 video video_device */
2515 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2516 video_nr[dev->devno]);
2517 if (ret) {
2518 em28xx_errdev("unable to register video device (error=%i).\n",
2519 ret);
2520 return ret;
2521 }
2522
2523 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002524 if (em28xx_vbi_supported(dev) == 1) {
2525 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2526 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002527
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002528 /* register v4l2 vbi video_device */
2529 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2530 vbi_nr[dev->devno]);
2531 if (ret < 0) {
2532 em28xx_errdev("unable to register vbi device\n");
2533 return ret;
2534 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002535 }
2536
2537 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002538 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2539 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002540 if (!dev->radio_dev) {
2541 em28xx_errdev("cannot allocate video_device.\n");
2542 return -ENODEV;
2543 }
2544 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2545 radio_nr[dev->devno]);
2546 if (ret < 0) {
2547 em28xx_errdev("can't register radio device\n");
2548 return ret;
2549 }
2550 em28xx_info("Registered radio device as /dev/radio%d\n",
2551 dev->radio_dev->num);
2552 }
2553
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002554 em28xx_info("V4L2 video device registered as /dev/video%d\n",
2555 dev->vdev->num);
2556
2557 if (dev->vbi_dev)
2558 em28xx_info("V4L2 VBI device registered as /dev/vbi%d\n",
2559 dev->vbi_dev->num);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002560
2561 return 0;
2562}