blob: 3a1dfb7726f8d8919dd1ff9a08505d582de6f2b8 [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) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300259 em28xx_isocdbg("Overflow of %zi bytes past buffer end"
260 "(2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300261 ((char *)startwrite + lencopy) -
262 ((char *)outp + buf->vb.size));
263 lencopy = remain = (char *)outp + buf->vb.size -
264 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300265 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300266 if (lencopy <= 0)
267 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300268
269 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270
271 remain -= lencopy;
272 }
273
274 dma_q->pos += len;
275}
276
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300277static void em28xx_copy_vbi(struct em28xx *dev,
278 struct em28xx_dmaqueue *dma_q,
279 struct em28xx_buffer *buf,
280 unsigned char *p,
281 unsigned char *outp, unsigned long len)
282{
283 void *startwrite, *startread;
284 int offset;
285 int bytesperline = 720;
286
287 if (dev == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300288 em28xx_isocdbg("dev is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300289 return;
290 }
291
292 if (dma_q == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300293 em28xx_isocdbg("dma_q is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300294 return;
295 }
296 if (buf == NULL) {
297 return;
298 }
299 if (p == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300300 em28xx_isocdbg("p is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300301 return;
302 }
303 if (outp == NULL) {
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300304 em28xx_isocdbg("outp is null\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300305 return;
306 }
307
308 if (dma_q->pos + len > buf->vb.size)
309 len = buf->vb.size - dma_q->pos;
310
311 if ((p[0] == 0x33 && p[1] == 0x95) ||
312 (p[0] == 0x88 && p[1] == 0x88)) {
313 /* Header field, advance past it */
314 p += 4;
315 } else {
316 len += 4;
317 }
318
319 startread = p;
320
321 startwrite = outp + dma_q->pos;
322 offset = dma_q->pos;
323
324 /* Make sure the bottom field populates the second half of the frame */
325 if (buf->top_field == 0) {
326 startwrite += bytesperline * 0x0c;
327 offset += bytesperline * 0x0c;
328 }
329
330 memcpy(startwrite, startread, len);
331 dma_q->pos += len;
332}
333
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300334static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300335 int packet, int status)
336{
337 char *errmsg = "Unknown";
338
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300339 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340 case -ENOENT:
341 errmsg = "unlinked synchronuously";
342 break;
343 case -ECONNRESET:
344 errmsg = "unlinked asynchronuously";
345 break;
346 case -ENOSR:
347 errmsg = "Buffer error (overrun)";
348 break;
349 case -EPIPE:
350 errmsg = "Stalled (device not responding)";
351 break;
352 case -EOVERFLOW:
353 errmsg = "Babble (bad cable?)";
354 break;
355 case -EPROTO:
356 errmsg = "Bit-stuff error (bad cable?)";
357 break;
358 case -EILSEQ:
359 errmsg = "CRC/Timeout (could be anything)";
360 break;
361 case -ETIME:
362 errmsg = "Device does not respond";
363 break;
364 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300365 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300366 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
367 } else {
368 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
369 packet, status, errmsg);
370 }
371}
372
373/*
374 * video-buf generic routine to get the next available buffer
375 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300376static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377 struct em28xx_buffer **buf)
378{
379 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300380 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300381
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300382 if (list_empty(&dma_q->active)) {
383 em28xx_isocdbg("No active queue to serve\n");
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300384 dev->isoc_ctl.vid_buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300385 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300386 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300387 }
388
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300389 /* Get the next buffer */
390 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
391
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300392 /* Cleans up buffer - Usefull for testing for frame/URB loss */
393 outp = videobuf_to_vmalloc(&(*buf)->vb);
394 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300395
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300396 dev->isoc_ctl.vid_buf = *buf;
397
398 return;
399}
400
401/*
402 * video-buf generic routine to get the next available VBI buffer
403 */
404static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
405 struct em28xx_buffer **buf)
406{
407 struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
408 char *outp;
409
410 if (list_empty(&dma_q->active)) {
411 em28xx_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.vbi_buf = NULL;
413 *buf = NULL;
414 return;
415 }
416
417 /* Get the next buffer */
418 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
419 /* Cleans up buffer - Usefull for testing for frame/URB loss */
420 outp = videobuf_to_vmalloc(&(*buf)->vb);
421 memset(outp, 0x00, (*buf)->vb.size);
422
423 dev->isoc_ctl.vbi_buf = *buf;
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300424
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300425 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426}
427
428/*
429 * Controls the isoc copy of each urb packet
430 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300431static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300432{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300433 struct em28xx_buffer *buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300434 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300435 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300437 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438
439 if (!dev)
440 return 0;
441
442 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
443 return 0;
444
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300445 if (urb->status < 0) {
446 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447 if (urb->status == -ENOENT)
448 return 0;
449 }
450
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300451 buf = dev->isoc_ctl.vid_buf;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300452 if (buf != NULL)
453 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 for (i = 0; i < urb->number_of_packets; i++) {
456 int status = urb->iso_frame_desc[i].status;
457
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 if (status < 0) {
459 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460 if (urb->iso_frame_desc[i].status != -EPROTO)
461 continue;
462 }
463
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300464 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465
466 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300467 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300468 continue;
469 }
470 if (urb->iso_frame_desc[i].actual_length >
471 dev->max_pkt_size) {
472 em28xx_isocdbg("packet bigger than packet size");
473 continue;
474 }
475
476 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300477
478 /* FIXME: incomplete buffer checks where removed to make
479 logic simpler. Impacts of those changes should be evaluated
480 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300481 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
482 em28xx_isocdbg("VBI HEADER!!!\n");
483 /* FIXME: Should add vbi copy */
484 continue;
485 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300486 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300487 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300488 len, (p[2] & 1) ? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -0300490 if (dev->progressive || !(p[2] & 1)) {
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300491 if (buf != NULL)
492 buffer_filled(dev, dma_q, buf);
493 get_next_buf(dma_q, &buf);
494 if (buf == NULL)
495 outp = NULL;
496 else
497 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300498 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300499
500 if (buf != NULL) {
501 if (p[2] & 1)
502 buf->top_field = 0;
503 else
504 buf->top_field = 1;
505 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300506
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300507 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300509 if (buf != NULL)
510 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300511 }
512 return rc;
513}
514
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300515/* Version of isoc handler that takes into account a mixture of video and
516 VBI data */
517static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
518{
519 struct em28xx_buffer *buf, *vbi_buf;
520 struct em28xx_dmaqueue *dma_q = &dev->vidq;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300521 struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300522 unsigned char *outp = NULL;
523 unsigned char *vbioutp = NULL;
524 int i, len = 0, rc = 1;
525 unsigned char *p;
526 int vbi_size;
527
528 if (!dev)
529 return 0;
530
531 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
532 return 0;
533
534 if (urb->status < 0) {
535 print_err_status(dev, -1, urb->status);
536 if (urb->status == -ENOENT)
537 return 0;
538 }
539
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300540 buf = dev->isoc_ctl.vid_buf;
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300541 if (buf != NULL)
542 outp = videobuf_to_vmalloc(&buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300543
544 vbi_buf = dev->isoc_ctl.vbi_buf;
545 if (vbi_buf != NULL)
546 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
547
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300548 for (i = 0; i < urb->number_of_packets; i++) {
549 int status = urb->iso_frame_desc[i].status;
550
551 if (status < 0) {
552 print_err_status(dev, i, status);
553 if (urb->iso_frame_desc[i].status != -EPROTO)
554 continue;
555 }
556
557 len = urb->iso_frame_desc[i].actual_length - 4;
558
559 if (urb->iso_frame_desc[i].actual_length <= 0) {
560 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
561 continue;
562 }
563 if (urb->iso_frame_desc[i].actual_length >
564 dev->max_pkt_size) {
565 em28xx_isocdbg("packet bigger than packet size");
566 continue;
567 }
568
569 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
570
571 /* capture type 0 = vbi start
572 capture type 1 = video start
573 capture type 2 = video in progress */
574 if (p[0] == 0x33 && p[1] == 0x95) {
575 dev->capture_type = 0;
576 dev->vbi_read = 0;
577 em28xx_isocdbg("VBI START HEADER!!!\n");
578 dev->cur_field = p[2];
579 }
580
581 /* FIXME: get rid of hard-coded value */
582 vbi_size = 720 * 0x0c;
583
584 if (dev->capture_type == 0) {
585 if (dev->vbi_read >= vbi_size) {
586 /* We've already read all the VBI data, so
587 treat the rest as video */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300588 em28xx_isocdbg("dev->vbi_read > vbi_size\n");
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300589 } else if ((dev->vbi_read + len) < vbi_size) {
590 /* This entire frame is VBI data */
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300591 if (dev->vbi_read == 0 &&
592 (!(dev->cur_field & 1))) {
593 /* Brand new frame */
594 if (vbi_buf != NULL)
595 vbi_buffer_filled(dev,
596 vbi_dma_q,
597 vbi_buf);
598 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
599 if (vbi_buf == NULL)
600 vbioutp = NULL;
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300601 else
602 vbioutp = videobuf_to_vmalloc(
603 &vbi_buf->vb);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300604 }
605
606 if (dev->vbi_read == 0) {
607 vbi_dma_q->pos = 0;
608 if (vbi_buf != NULL) {
609 if (dev->cur_field & 1)
610 vbi_buf->top_field = 0;
611 else
612 vbi_buf->top_field = 1;
613 }
614 }
615
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300616 dev->vbi_read += len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300617 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
618 vbioutp, len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300619 } else {
620 /* Some of this frame is VBI data and some is
621 video data */
622 int vbi_data_len = vbi_size - dev->vbi_read;
623 dev->vbi_read += vbi_data_len;
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300624 em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
625 vbioutp, vbi_data_len);
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300626 dev->capture_type = 1;
627 p += vbi_data_len;
628 len -= vbi_data_len;
629 }
630 }
631
632 if (dev->capture_type == 1) {
633 dev->capture_type = 2;
634 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
635 len, (p[2] & 1) ? "odd" : "even");
636
637 if (dev->progressive || !(dev->cur_field & 1)) {
638 if (buf != NULL)
639 buffer_filled(dev, dma_q, buf);
640 get_next_buf(dma_q, &buf);
641 if (buf == NULL)
642 outp = NULL;
643 else
644 outp = videobuf_to_vmalloc(&buf->vb);
645 }
646 if (buf != NULL) {
647 if (dev->cur_field & 1)
648 buf->top_field = 0;
649 else
650 buf->top_field = 1;
651 }
652
653 dma_q->pos = 0;
654 }
655 if (buf != NULL && dev->capture_type == 2)
656 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
657 }
658 return rc;
659}
660
661
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300662/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300663 Videobuf operations
664 ------------------------------------------------------------------*/
665
666static int
667buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
668{
669 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300670 struct em28xx *dev = fh->dev;
671 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300672
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300673 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
674 >> 3;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -0300675
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300676 if (0 == *count)
677 *count = EM28XX_DEF_BUF;
678
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300679 if (*count < EM28XX_MIN_BUF)
680 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300681
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300682 /* Ask tuner to go to analog or radio mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300683 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300684 f.frequency = dev->ctl_freq;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300685 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300686
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300687 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300688
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300689 return 0;
690}
691
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300692/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300693static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
694{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300695 struct em28xx_fh *fh = vq->priv_data;
696 struct em28xx *dev = fh->dev;
697 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300698 if (in_interrupt())
699 BUG();
700
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300701 /* We used to wait for the buffer to finish here, but this didn't work
702 because, as we were keeping the state as VIDEOBUF_QUEUED,
703 videobuf_queue_cancel marked it as finished for us.
704 (Also, it could wedge forever if the hardware was misconfigured.)
705
706 This should be safe; by the time we get here, the buffer isn't
707 queued anymore. If we ever start marking the buffers as
708 VIDEOBUF_ACTIVE, it won't be, though.
709 */
710 spin_lock_irqsave(&dev->slock, flags);
Devin Heitmueller28abf0832009-09-01 01:54:54 -0300711 if (dev->isoc_ctl.vid_buf == buf)
712 dev->isoc_ctl.vid_buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300713 spin_unlock_irqrestore(&dev->slock, flags);
714
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300715 videobuf_vmalloc_free(&buf->vb);
716 buf->vb.state = VIDEOBUF_NEEDS_INIT;
717}
718
719static int
720buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
721 enum v4l2_field field)
722{
723 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300724 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300725 struct em28xx *dev = fh->dev;
726 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300727
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300728 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
729 + 7) >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300730
731 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
732 return -EINVAL;
733
Brandon Philips05612972008-04-13 14:57:01 -0300734 buf->vb.width = dev->width;
735 buf->vb.height = dev->height;
736 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300737
738 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300739 rc = videobuf_iolock(vq, &buf->vb, NULL);
740 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300741 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300742 }
743
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300744 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300745 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300746
747 if (urb_init) {
Devin Heitmuellerda52a552009-09-01 01:19:46 -0300748 if (em28xx_vbi_supported(dev) == 1)
749 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
750 EM28XX_NUM_BUFS,
751 dev->max_pkt_size,
752 em28xx_isoc_copy_vbi);
753 else
754 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
755 EM28XX_NUM_BUFS,
756 dev->max_pkt_size,
757 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300758 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300759 goto fail;
760 }
761
762 buf->vb.state = VIDEOBUF_PREPARED;
763 return 0;
764
765fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300766 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300767 return rc;
768}
769
770static void
771buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
772{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300773 struct em28xx_buffer *buf = container_of(vb,
774 struct em28xx_buffer,
775 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300776 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300777 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300778 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300779
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300780 buf->vb.state = VIDEOBUF_QUEUED;
781 list_add_tail(&buf->vb.queue, &vidq->active);
782
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300783}
784
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300785static void buffer_release(struct videobuf_queue *vq,
786 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300787{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300788 struct em28xx_buffer *buf = container_of(vb,
789 struct em28xx_buffer,
790 vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300791 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300792 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300793
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300794 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300795
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300796 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300797}
798
799static struct videobuf_queue_ops em28xx_video_qops = {
800 .buf_setup = buffer_setup,
801 .buf_prepare = buffer_prepare,
802 .buf_queue = buffer_queue,
803 .buf_release = buffer_release,
804};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800805
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300806/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800807
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800808static void video_mux(struct em28xx *dev, int index)
809{
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800810 dev->ctl_input = index;
811 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300812 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800813
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300814 if (!dev->ctl_aoutput)
815 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
816
Hans Verkuil5325b422009-04-02 11:26:22 -0300817 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
818 INPUT(index)->vmux, 0, 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800819
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300820 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300821 if (dev->i2s_speed) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -0300822 v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
823 s_i2s_clock_freq, dev->i2s_speed);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300824 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300825 /* Note: this is msp3400 specific */
Hans Verkuil5325b422009-04-02 11:26:22 -0300826 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
827 dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800828 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300829
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300830 if (dev->board.adecoder != EM28XX_NOADECODER) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300831 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
832 dev->ctl_ainput, dev->ctl_aoutput, 0);
Vitaly Wool2bd1d9e2009-03-04 08:27:52 -0300833 }
834
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300835 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800836}
837
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300838/* Usage lock check functions */
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300839static int res_get(struct em28xx_fh *fh, unsigned int bit)
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300840{
841 struct em28xx *dev = fh->dev;
842
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300843 if (fh->resources & bit)
844 /* have it already allocated */
845 return 1;
846
847 /* is it free? */
848 mutex_lock(&dev->lock);
849 if (dev->resources & bit) {
850 /* no, someone else uses it */
851 mutex_unlock(&dev->lock);
852 return 0;
853 }
854 /* it's free, grab it */
855 fh->resources |= bit;
856 dev->resources |= bit;
857 em28xx_videodbg("res: get %d\n", bit);
858 mutex_unlock(&dev->lock);
859 return 1;
860}
861
862static int res_check(struct em28xx_fh *fh, unsigned int bit)
863{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300864 return fh->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300865}
866
867static int res_locked(struct em28xx *dev, unsigned int bit)
868{
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -0300869 return dev->resources & bit;
Devin Heitmueller8c873d32009-09-03 00:23:27 -0300870}
871
872static void res_free(struct em28xx_fh *fh, unsigned int bits)
873{
874 struct em28xx *dev = fh->dev;
875
876 BUG_ON((fh->resources & bits) != bits);
877
878 mutex_lock(&dev->lock);
879 fh->resources &= ~bits;
880 dev->resources &= ~bits;
881 em28xx_videodbg("res: put %d\n", bits);
882 mutex_unlock(&dev->lock);
883}
884
885static int get_ressource(struct em28xx_fh *fh)
886{
887 switch (fh->type) {
888 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
889 return EM28XX_RESOURCE_VIDEO;
890 case V4L2_BUF_TYPE_VBI_CAPTURE:
891 return EM28XX_RESOURCE_VBI;
892 default:
893 BUG();
894 return 0;
895 }
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300896}
897
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800898/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300899 * ac97_queryctrl()
900 * return the ac97 supported controls
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300901 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300902static int ac97_queryctrl(struct v4l2_queryctrl *qc)
903{
904 int i;
905
906 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
907 if (qc->id && qc->id == ac97_qctrl[i].id) {
908 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
909 return 0;
910 }
911 }
912
913 /* Control is not ac97 related */
914 return 1;
915}
916
917/*
918 * ac97_get_ctrl()
919 * return the current values for ac97 mute and volume
920 */
921static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300922{
923 switch (ctrl->id) {
924 case V4L2_CID_AUDIO_MUTE:
925 ctrl->value = dev->mute;
926 return 0;
927 case V4L2_CID_AUDIO_VOLUME:
928 ctrl->value = dev->volume;
929 return 0;
930 default:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300931 /* Control is not ac97 related */
932 return 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933 }
934}
935
936/*
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300937 * ac97_set_ctrl()
938 * set values for ac97 mute and volume
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939 */
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300940static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941{
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300942 int i;
943
944 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
945 if (ctrl->id == ac97_qctrl[i].id)
946 goto handle;
947
948 /* Announce that hasn't handle it */
949 return 1;
950
951handle:
952 if (ctrl->value < ac97_qctrl[i].minimum ||
953 ctrl->value > ac97_qctrl[i].maximum)
954 return -ERANGE;
955
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300956 switch (ctrl->id) {
957 case V4L2_CID_AUDIO_MUTE:
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300958 dev->mute = ctrl->value;
959 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300960 case V4L2_CID_AUDIO_VOLUME:
961 dev->volume = ctrl->value;
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300962 break;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300963 }
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -0300964
965 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300966}
967
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968static int check_dev(struct em28xx *dev)
969{
970 if (dev->state & DEV_DISCONNECTED) {
971 em28xx_errdev("v4l2 ioctl: device not present\n");
972 return -ENODEV;
973 }
974
975 if (dev->state & DEV_MISCONFIGURED) {
976 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
977 "close and open it again\n");
978 return -EIO;
979 }
980 return 0;
981}
982
983static void get_scale(struct em28xx *dev,
984 unsigned int width, unsigned int height,
985 unsigned int *hscale, unsigned int *vscale)
986{
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -0300987 unsigned int maxw = norm_maxw(dev);
988 unsigned int maxh = norm_maxh(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300989
990 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
991 if (*hscale >= 0x4000)
992 *hscale = 0x3fff;
993
994 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
995 if (*vscale >= 0x4000)
996 *vscale = 0x3fff;
997}
998
999/* ------------------------------------------------------------------
1000 IOCTL vidioc handling
1001 ------------------------------------------------------------------*/
1002
Hans Verkuil78b526a2008-05-28 12:16:41 -03001003static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 struct v4l2_format *f)
1005{
1006 struct em28xx_fh *fh = priv;
1007 struct em28xx *dev = fh->dev;
1008
1009 mutex_lock(&dev->lock);
1010
1011 f->fmt.pix.width = dev->width;
1012 f->fmt.pix.height = dev->height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001013 f->fmt.pix.pixelformat = dev->format->fourcc;
1014 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -03001015 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001016 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1017
1018 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001019 if (dev->progressive)
1020 f->fmt.pix.field = V4L2_FIELD_NONE;
1021 else
1022 f->fmt.pix.field = dev->interlaced ?
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001023 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1024
1025 mutex_unlock(&dev->lock);
1026 return 0;
1027}
1028
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001029static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
1030{
1031 unsigned int i;
1032
1033 for (i = 0; i < ARRAY_SIZE(format); i++)
1034 if (format[i].fourcc == fourcc)
1035 return &format[i];
1036
1037 return NULL;
1038}
1039
Hans Verkuil78b526a2008-05-28 12:16:41 -03001040static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001041 struct v4l2_format *f)
1042{
1043 struct em28xx_fh *fh = priv;
1044 struct em28xx *dev = fh->dev;
Trent Piephoccb83402009-05-30 21:45:46 -03001045 unsigned int width = f->fmt.pix.width;
1046 unsigned int height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001047 unsigned int maxw = norm_maxw(dev);
1048 unsigned int maxh = norm_maxh(dev);
1049 unsigned int hscale, vscale;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001050 struct em28xx_fmt *fmt;
1051
1052 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1053 if (!fmt) {
1054 em28xx_videodbg("Fourcc format (%08x) invalid.\n",
1055 f->fmt.pix.pixelformat);
1056 return -EINVAL;
1057 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001058
Mauro Carvalho Chehab55699962009-07-13 20:15:02 -03001059 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001060 /* the em2800 can only scale down to 50% */
Trent Piephoccb83402009-05-30 21:45:46 -03001061 height = height > (3 * maxh / 4) ? maxh : maxh / 2;
1062 width = width > (3 * maxw / 4) ? maxw : maxw / 2;
1063 /* According to empiatech support the MaxPacketSize is too small
1064 * to support framesizes larger than 640x480 @ 30 fps or 640x576
1065 * @ 25 fps. As this would cut of a part of the image we prefer
1066 * 360x576 or 360x480 for now */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001067 if (width == maxw && height == maxh)
1068 width /= 2;
Trent Piephoccb83402009-05-30 21:45:46 -03001069 } else {
1070 /* width must even because of the YUYV format
1071 height must be even because of interlacing */
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001072 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
1073 1, 0);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001074 }
1075
1076 get_scale(dev, width, height, &hscale, &vscale);
1077
1078 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
1079 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
1080
1081 f->fmt.pix.width = width;
1082 f->fmt.pix.height = height;
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001083 f->fmt.pix.pixelformat = fmt->fourcc;
1084 f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
1085 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001086 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03001087 if (dev->progressive)
1088 f->fmt.pix.field = V4L2_FIELD_NONE;
1089 else
1090 f->fmt.pix.field = dev->interlaced ?
1091 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001092
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001093 return 0;
1094}
1095
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001096static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
1097 unsigned width, unsigned height)
1098{
1099 struct em28xx_fmt *fmt;
1100
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001101 fmt = format_by_fourcc(fourcc);
1102 if (!fmt)
1103 return -EINVAL;
1104
1105 dev->format = fmt;
1106 dev->width = width;
1107 dev->height = height;
1108
1109 /* set new image size */
1110 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1111
1112 em28xx_set_alternate(dev);
1113 em28xx_resolution_set(dev);
1114
1115 return 0;
1116}
1117
Hans Verkuil78b526a2008-05-28 12:16:41 -03001118static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001119 struct v4l2_format *f)
1120{
1121 struct em28xx_fh *fh = priv;
1122 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001123 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001124
1125 rc = check_dev(dev);
1126 if (rc < 0)
1127 return rc;
1128
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129 mutex_lock(&dev->lock);
1130
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001131 vidioc_try_fmt_vid_cap(file, priv, f);
1132
Brandon Philips05612972008-04-13 14:57:01 -03001133 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1134 em28xx_errdev("%s queue busy\n", __func__);
1135 rc = -EBUSY;
1136 goto out;
1137 }
1138
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03001139 rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
1140 f->fmt.pix.width, f->fmt.pix.height);
Brandon Philips05612972008-04-13 14:57:01 -03001141
1142out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001143 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -03001144 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001145}
1146
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001147static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1148{
1149 struct em28xx_fh *fh = priv;
1150 struct em28xx *dev = fh->dev;
Devin Heitmueller19bf0032009-09-11 00:40:18 -03001151 int rc;
1152
1153 rc = check_dev(dev);
1154 if (rc < 0)
1155 return rc;
1156
1157 *norm = dev->norm;
1158
1159 return 0;
1160}
1161
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03001162static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001163{
1164 struct em28xx_fh *fh = priv;
1165 struct em28xx *dev = fh->dev;
1166 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001167 int rc;
1168
1169 rc = check_dev(dev);
1170 if (rc < 0)
1171 return rc;
1172
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001173 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001174 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001175
1176 /* Adjusts width/height, if needed */
1177 f.fmt.pix.width = dev->width;
1178 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -03001179 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001180
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001181 /* set new image size */
1182 dev->width = f.fmt.pix.width;
1183 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001184 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1185
1186 em28xx_resolution_set(dev);
Hans Verkuilf41737e2009-04-01 03:52:39 -03001187 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001188
1189 mutex_unlock(&dev->lock);
1190 return 0;
1191}
1192
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03001193static int vidioc_g_parm(struct file *file, void *priv,
1194 struct v4l2_streamparm *p)
1195{
1196 struct em28xx_fh *fh = priv;
1197 struct em28xx *dev = fh->dev;
1198 int rc = 0;
1199
1200 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1201 return -EINVAL;
1202
1203 if (dev->board.is_webcam)
1204 rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
1205 video, g_parm, p);
1206 else
1207 v4l2_video_std_frame_period(dev->norm,
1208 &p->parm.capture.timeperframe);
1209
1210 return rc;
1211}
1212
1213static int vidioc_s_parm(struct file *file, void *priv,
1214 struct v4l2_streamparm *p)
1215{
1216 struct em28xx_fh *fh = priv;
1217 struct em28xx *dev = fh->dev;
1218
1219 if (!dev->board.is_webcam)
1220 return -EINVAL;
1221
1222 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223 return -EINVAL;
1224
1225 return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
1226}
1227
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001228static const char *iname[] = {
1229 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1230 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1231 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1232 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1233 [EM28XX_VMUX_SVIDEO] = "S-Video",
1234 [EM28XX_VMUX_TELEVISION] = "Television",
1235 [EM28XX_VMUX_CABLE] = "Cable TV",
1236 [EM28XX_VMUX_DVB] = "DVB",
1237 [EM28XX_VMUX_DEBUG] = "for debug only",
1238};
1239
1240static int vidioc_enum_input(struct file *file, void *priv,
1241 struct v4l2_input *i)
1242{
1243 struct em28xx_fh *fh = priv;
1244 struct em28xx *dev = fh->dev;
1245 unsigned int n;
1246
1247 n = i->index;
1248 if (n >= MAX_EM28XX_INPUT)
1249 return -EINVAL;
1250 if (0 == INPUT(n)->type)
1251 return -EINVAL;
1252
1253 i->index = n;
1254 i->type = V4L2_INPUT_TYPE_CAMERA;
1255
1256 strcpy(i->name, iname[INPUT(n)->type]);
1257
1258 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1259 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1260 i->type = V4L2_INPUT_TYPE_TUNER;
1261
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001262 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001263
1264 return 0;
1265}
1266
1267static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1268{
1269 struct em28xx_fh *fh = priv;
1270 struct em28xx *dev = fh->dev;
1271
1272 *i = dev->ctl_input;
1273
1274 return 0;
1275}
1276
1277static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1278{
1279 struct em28xx_fh *fh = priv;
1280 struct em28xx *dev = fh->dev;
1281 int rc;
1282
1283 rc = check_dev(dev);
1284 if (rc < 0)
1285 return rc;
1286
1287 if (i >= MAX_EM28XX_INPUT)
1288 return -EINVAL;
1289 if (0 == INPUT(i)->type)
1290 return -EINVAL;
1291
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001292 dev->ctl_input = i;
1293
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001294 mutex_lock(&dev->lock);
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001295 video_mux(dev, dev->ctl_input);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296 mutex_unlock(&dev->lock);
1297 return 0;
1298}
1299
1300static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1301{
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001304
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001305 if (!dev->audio_mode.has_audio)
1306 return -EINVAL;
1307
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001308 switch (a->index) {
1309 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001310 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001311 break;
1312 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001313 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001314 break;
1315 case EM28XX_AMUX_VIDEO2:
1316 strcpy(a->name, "Television alt");
1317 break;
1318 case EM28XX_AMUX_PHONE:
1319 strcpy(a->name, "Phone");
1320 break;
1321 case EM28XX_AMUX_MIC:
1322 strcpy(a->name, "Mic");
1323 break;
1324 case EM28XX_AMUX_CD:
1325 strcpy(a->name, "CD");
1326 break;
1327 case EM28XX_AMUX_AUX:
1328 strcpy(a->name, "Aux");
1329 break;
1330 case EM28XX_AMUX_PCM_OUT:
1331 strcpy(a->name, "PCM");
1332 break;
1333 default:
1334 return -EINVAL;
1335 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001336
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001337 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001338 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001339
1340 return 0;
1341}
1342
1343static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1344{
1345 struct em28xx_fh *fh = priv;
1346 struct em28xx *dev = fh->dev;
1347
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001348
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001349 if (!dev->audio_mode.has_audio)
1350 return -EINVAL;
1351
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03001352 if (a->index >= MAX_EM28XX_INPUT)
1353 return -EINVAL;
1354 if (0 == INPUT(a->index)->type)
1355 return -EINVAL;
1356
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001357 mutex_lock(&dev->lock);
1358
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -03001359 dev->ctl_ainput = INPUT(a->index)->amux;
1360 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -03001361
1362 if (!dev->ctl_aoutput)
1363 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001364
1365 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001366 return 0;
1367}
1368
1369static int vidioc_queryctrl(struct file *file, void *priv,
1370 struct v4l2_queryctrl *qc)
1371{
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
1374 int id = qc->id;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001375 int rc;
1376
1377 rc = check_dev(dev);
1378 if (rc < 0)
1379 return rc;
1380
1381 memset(qc, 0, sizeof(*qc));
1382
1383 qc->id = id;
1384
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001385 /* enumberate AC97 controls */
1386 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
1387 rc = ac97_queryctrl(qc);
1388 if (!rc)
1389 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001390 }
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001391
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001392 /* enumberate V4L2 device controls */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001393 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001394 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395 mutex_unlock(&dev->lock);
1396
1397 if (qc->type)
1398 return 0;
1399 else
1400 return -EINVAL;
1401}
1402
1403static int vidioc_g_ctrl(struct file *file, void *priv,
1404 struct v4l2_control *ctrl)
1405{
1406 struct em28xx_fh *fh = priv;
1407 struct em28xx *dev = fh->dev;
1408 int rc;
1409
1410 rc = check_dev(dev);
1411 if (rc < 0)
1412 return rc;
Mauro Carvalho Chehabb6070f02008-12-22 06:20:32 -03001413 rc = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001414
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001415 mutex_lock(&dev->lock);
1416
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001417 /* Set an AC97 control */
1418 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1419 rc = ac97_get_ctrl(dev, ctrl);
1420 else
1421 rc = 1;
1422
1423 /* It were not an AC97 control. Sends it to the v4l2 dev interface */
1424 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001425 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001426 rc = 0;
Hans Verkuil07f7db42009-01-21 17:06:42 -03001427 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001428
1429 mutex_unlock(&dev->lock);
1430 return rc;
1431}
1432
1433static int vidioc_s_ctrl(struct file *file, void *priv,
1434 struct v4l2_control *ctrl)
1435{
1436 struct em28xx_fh *fh = priv;
1437 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001438 int rc;
1439
1440 rc = check_dev(dev);
1441 if (rc < 0)
1442 return rc;
1443
1444 mutex_lock(&dev->lock);
1445
Mauro Carvalho Chehaba98f6af2009-07-19 10:45:49 -03001446 /* Set an AC97 control */
1447 if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
1448 rc = ac97_set_ctrl(dev, ctrl);
1449 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001450 rc = 1;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001451
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001452 /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001453 if (rc == 1) {
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001454 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
Mauro Carvalho Chehab73c6f462009-07-29 01:42:02 -03001455
1456 /*
1457 * In the case of non-AC97 volume controls, we still need
1458 * to do some setups at em28xx, in order to mute/unmute
1459 * and to adjust audio volume. However, the value ranges
1460 * should be checked by the corresponding V4L subdriver.
1461 */
1462 switch (ctrl->id) {
1463 case V4L2_CID_AUDIO_MUTE:
1464 dev->mute = ctrl->value;
1465 rc = em28xx_audio_analog_set(dev);
1466 break;
1467 case V4L2_CID_AUDIO_VOLUME:
1468 dev->volume = ctrl->value;
1469 rc = em28xx_audio_analog_set(dev);
1470 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001471 }
1472
1473 mutex_unlock(&dev->lock);
1474 return rc;
1475}
1476
1477static int vidioc_g_tuner(struct file *file, void *priv,
1478 struct v4l2_tuner *t)
1479{
1480 struct em28xx_fh *fh = priv;
1481 struct em28xx *dev = fh->dev;
1482 int rc;
1483
1484 rc = check_dev(dev);
1485 if (rc < 0)
1486 return rc;
1487
1488 if (0 != t->index)
1489 return -EINVAL;
1490
1491 strcpy(t->name, "Tuner");
1492
1493 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001494 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001495 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001496
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001497 return 0;
1498}
1499
1500static int vidioc_s_tuner(struct file *file, void *priv,
1501 struct v4l2_tuner *t)
1502{
1503 struct em28xx_fh *fh = priv;
1504 struct em28xx *dev = fh->dev;
1505 int rc;
1506
1507 rc = check_dev(dev);
1508 if (rc < 0)
1509 return rc;
1510
1511 if (0 != t->index)
1512 return -EINVAL;
1513
1514 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001515 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001516 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001517
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001518 return 0;
1519}
1520
1521static int vidioc_g_frequency(struct file *file, void *priv,
1522 struct v4l2_frequency *f)
1523{
1524 struct em28xx_fh *fh = priv;
1525 struct em28xx *dev = fh->dev;
1526
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001527 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001528 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001529 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001530 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001531
1532 return 0;
1533}
1534
1535static int vidioc_s_frequency(struct file *file, void *priv,
1536 struct v4l2_frequency *f)
1537{
1538 struct em28xx_fh *fh = priv;
1539 struct em28xx *dev = fh->dev;
1540 int rc;
1541
1542 rc = check_dev(dev);
1543 if (rc < 0)
1544 return rc;
1545
1546 if (0 != f->tuner)
1547 return -EINVAL;
1548
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001549 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1550 return -EINVAL;
1551 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001552 return -EINVAL;
1553
1554 mutex_lock(&dev->lock);
1555
1556 dev->ctl_freq = f->frequency;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001557 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001558
1559 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001560
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001561 return 0;
1562}
1563
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001564#ifdef CONFIG_VIDEO_ADV_DEBUG
1565static int em28xx_reg_len(int reg)
1566{
1567 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001568 case EM28XX_R40_AC97LSB:
1569 case EM28XX_R30_HSCALELOW:
1570 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001571 return 2;
1572 default:
1573 return 1;
1574 }
1575}
1576
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001577static int vidioc_g_chip_ident(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001578 struct v4l2_dbg_chip_ident *chip)
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001579{
1580 struct em28xx_fh *fh = priv;
1581 struct em28xx *dev = fh->dev;
1582
1583 chip->ident = V4L2_IDENT_NONE;
1584 chip->revision = 0;
1585
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001586 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001587
1588 return 0;
1589}
1590
1591
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001592static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001593 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001594{
1595 struct em28xx_fh *fh = priv;
1596 struct em28xx *dev = fh->dev;
1597 int ret;
1598
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001599 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001600 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001601 mutex_lock(&dev->lock);
1602 ret = em28xx_read_ac97(dev, reg->reg);
1603 mutex_unlock(&dev->lock);
1604 if (ret < 0)
1605 return ret;
1606
1607 reg->val = ret;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001608 reg->size = 1;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001609 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001610 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001611 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001612 return 0;
1613 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001614 /* TODO: is this correct? */
1615 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1616 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001617 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001618 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001619 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001620 }
1621
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001622 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001623 reg->size = em28xx_reg_len(reg->reg);
1624 if (reg->size == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001625 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001626 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001627 mutex_unlock(&dev->lock);
1628
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001629 if (ret < 0)
1630 return ret;
1631
1632 reg->val = ret;
1633 } else {
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001634 __le16 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001635 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001636 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1637 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001638 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001639 if (ret < 0)
1640 return ret;
1641
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001642 reg->val = le16_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001643 }
1644
1645 return 0;
1646}
1647
1648static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001649 struct v4l2_dbg_register *reg)
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001650{
1651 struct em28xx_fh *fh = priv;
1652 struct em28xx *dev = fh->dev;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001653 __le16 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001654 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001655
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001656 switch (reg->match.type) {
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001657 case V4L2_CHIP_MATCH_AC97:
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001658 mutex_lock(&dev->lock);
1659 rc = em28xx_write_ac97(dev, reg->reg, reg->val);
1660 mutex_unlock(&dev->lock);
1661
1662 return rc;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001663 case V4L2_CHIP_MATCH_I2C_DRIVER:
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001664 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001665 return 0;
1666 case V4L2_CHIP_MATCH_I2C_ADDR:
Mauro Carvalho Chehab4efa2d72009-08-09 19:39:23 -03001667 /* TODO: is this correct? */
1668 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1669 return 0;
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001670 default:
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001671 if (!v4l2_chip_match_host(&reg->match))
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001672 return -EINVAL;
Mauro Carvalho Chehab531c98e2008-12-22 13:18:27 -03001673 }
1674
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03001675 /* Match host */
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001676 buf = cpu_to_le16(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001677
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001678 mutex_lock(&dev->lock);
1679 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1680 em28xx_reg_len(reg->reg));
1681 mutex_unlock(&dev->lock);
1682
1683 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001684}
1685#endif
1686
1687
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001688static int vidioc_cropcap(struct file *file, void *priv,
1689 struct v4l2_cropcap *cc)
1690{
1691 struct em28xx_fh *fh = priv;
1692 struct em28xx *dev = fh->dev;
1693
1694 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1695 return -EINVAL;
1696
1697 cc->bounds.left = 0;
1698 cc->bounds.top = 0;
1699 cc->bounds.width = dev->width;
1700 cc->bounds.height = dev->height;
1701 cc->defrect = cc->bounds;
1702 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1703 cc->pixelaspect.denominator = 59;
1704
1705 return 0;
1706}
1707
1708static int vidioc_streamon(struct file *file, void *priv,
1709 enum v4l2_buf_type type)
1710{
1711 struct em28xx_fh *fh = priv;
1712 struct em28xx *dev = fh->dev;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001713 int rc = -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001714
1715 rc = check_dev(dev);
1716 if (rc < 0)
1717 return rc;
1718
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001719 if (unlikely(type != fh->type))
1720 return -EINVAL;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001721
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001722 em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1723 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001724
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001725 if (unlikely(!res_get(fh, get_ressource(fh))))
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001726 return -EBUSY;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001727
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001728 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1729 rc = videobuf_streamon(&fh->vb_vidq);
1730 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1731 rc = videobuf_streamon(&fh->vb_vbiq);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001732
1733 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001734}
1735
1736static int vidioc_streamoff(struct file *file, void *priv,
1737 enum v4l2_buf_type type)
1738{
1739 struct em28xx_fh *fh = priv;
1740 struct em28xx *dev = fh->dev;
1741 int rc;
1742
1743 rc = check_dev(dev);
1744 if (rc < 0)
1745 return rc;
1746
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001747 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1748 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001749 return -EINVAL;
1750 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001751 return -EINVAL;
1752
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001753 em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1754 fh, type, fh->resources, dev->resources);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001755
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001756 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001757 videobuf_streamoff(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001758 res_free(fh, EM28XX_RESOURCE_VIDEO);
1759 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001760 videobuf_streamoff(&fh->vb_vbiq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03001761 res_free(fh, EM28XX_RESOURCE_VBI);
1762 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001763
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001764 return 0;
1765}
1766
1767static int vidioc_querycap(struct file *file, void *priv,
1768 struct v4l2_capability *cap)
1769{
1770 struct em28xx_fh *fh = priv;
1771 struct em28xx *dev = fh->dev;
1772
1773 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1774 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001775 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001776
1777 cap->version = EM28XX_VERSION_CODE;
1778
1779 cap->capabilities =
1780 V4L2_CAP_SLICED_VBI_CAPTURE |
1781 V4L2_CAP_VIDEO_CAPTURE |
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001782 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1783
Devin Heitmueller04146142009-09-11 00:08:44 -03001784 if (dev->vbi_dev)
1785 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
1786
Mauro Carvalho Chehab6c428b52009-08-04 19:52:37 -03001787 if (dev->audio_mode.has_audio)
1788 cap->capabilities |= V4L2_CAP_AUDIO;
1789
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001790 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001791 cap->capabilities |= V4L2_CAP_TUNER;
1792
1793 return 0;
1794}
1795
Hans Verkuil78b526a2008-05-28 12:16:41 -03001796static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001797 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001798{
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001799 if (unlikely(f->index >= ARRAY_SIZE(format)))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001800 return -EINVAL;
1801
Mauro Carvalho Chehabbddcf632008-12-20 09:06:37 -03001802 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1803 f->pixelformat = format[f->index].fourcc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001804
1805 return 0;
1806}
1807
1808/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001809static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001810 struct v4l2_format *f)
1811{
1812 struct em28xx_fh *fh = priv;
1813 struct em28xx *dev = fh->dev;
1814 int rc;
1815
1816 rc = check_dev(dev);
1817 if (rc < 0)
1818 return rc;
1819
1820 mutex_lock(&dev->lock);
1821
1822 f->fmt.sliced.service_set = 0;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001823 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001824
1825 if (f->fmt.sliced.service_set == 0)
1826 rc = -EINVAL;
1827
1828 mutex_unlock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001829
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001830 return rc;
1831}
1832
Hans Verkuil78b526a2008-05-28 12:16:41 -03001833static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001834 struct v4l2_format *f)
1835{
1836 struct em28xx_fh *fh = priv;
1837 struct em28xx *dev = fh->dev;
1838 int rc;
1839
1840 rc = check_dev(dev);
1841 if (rc < 0)
1842 return rc;
1843
1844 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03001845 v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001846 mutex_unlock(&dev->lock);
1847
1848 if (f->fmt.sliced.service_set == 0)
1849 return -EINVAL;
1850
1851 return 0;
1852}
1853
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001854/* RAW VBI ioctls */
1855
1856static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1857 struct v4l2_format *format)
1858{
1859 format->fmt.vbi.samples_per_line = 720;
1860 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1861 format->fmt.vbi.offset = 0;
1862 format->fmt.vbi.flags = 0;
1863
1864 /* Varies by video standard (NTSC, PAL, etc.) */
1865 /* FIXME: hard-coded for NTSC support */
1866 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1867 format->fmt.vbi.count[0] = 12;
1868 format->fmt.vbi.count[1] = 12;
1869 format->fmt.vbi.start[0] = 10;
1870 format->fmt.vbi.start[1] = 273;
1871
1872 return 0;
1873}
1874
1875static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1876 struct v4l2_format *format)
1877{
1878 format->fmt.vbi.samples_per_line = 720;
1879 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1880 format->fmt.vbi.offset = 0;
1881 format->fmt.vbi.flags = 0;
1882
1883 /* Varies by video standard (NTSC, PAL, etc.) */
1884 /* FIXME: hard-coded for NTSC support */
1885 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
1886 format->fmt.vbi.count[0] = 12;
1887 format->fmt.vbi.count[1] = 12;
1888 format->fmt.vbi.start[0] = 10;
1889 format->fmt.vbi.start[1] = 273;
1890
1891 return 0;
1892}
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001893
1894static int vidioc_reqbufs(struct file *file, void *priv,
1895 struct v4l2_requestbuffers *rb)
1896{
1897 struct em28xx_fh *fh = priv;
1898 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001899 int rc;
1900
1901 rc = check_dev(dev);
1902 if (rc < 0)
1903 return rc;
1904
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001905 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1906 return videobuf_reqbufs(&fh->vb_vidq, rb);
1907 else
1908 return videobuf_reqbufs(&fh->vb_vbiq, rb);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001909}
1910
1911static int vidioc_querybuf(struct file *file, void *priv,
1912 struct v4l2_buffer *b)
1913{
1914 struct em28xx_fh *fh = priv;
1915 struct em28xx *dev = fh->dev;
1916 int rc;
1917
1918 rc = check_dev(dev);
1919 if (rc < 0)
1920 return rc;
1921
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001922 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1923 return videobuf_querybuf(&fh->vb_vidq, b);
1924 else {
1925 /* FIXME: I'm not sure yet whether this is a bug in zvbi or
1926 the videobuf framework, but we probably shouldn't be
1927 returning a buffer larger than that which was asked for.
1928 At a minimum, it causes a crash in zvbi since it does
1929 a memcpy based on the source buffer length */
1930 int result = videobuf_querybuf(&fh->vb_vbiq, b);
1931 b->length = 17280;
1932 return result;
1933 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001934}
1935
1936static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1937{
1938 struct em28xx_fh *fh = priv;
1939 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001940 int rc;
1941
1942 rc = check_dev(dev);
1943 if (rc < 0)
1944 return rc;
1945
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001946 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1947 return videobuf_qbuf(&fh->vb_vidq, b);
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03001948 else
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001949 return videobuf_qbuf(&fh->vb_vbiq, b);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001950}
1951
1952static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1953{
1954 struct em28xx_fh *fh = priv;
1955 struct em28xx *dev = fh->dev;
1956 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001957
1958 rc = check_dev(dev);
1959 if (rc < 0)
1960 return rc;
1961
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001962 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1963 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
1964 O_NONBLOCK);
1965 else
1966 return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
1967 O_NONBLOCK);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001968}
1969
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001970#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001971static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001972{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001973 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001974
Devin Heitmueller28abf0832009-09-01 01:54:54 -03001975 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1976 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1977 else
1978 return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001979}
1980#endif
1981
1982
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001983/* ----------------------------------------------------------- */
1984/* RADIO ESPECIFIC IOCTLS */
1985/* ----------------------------------------------------------- */
1986
1987static int radio_querycap(struct file *file, void *priv,
1988 struct v4l2_capability *cap)
1989{
1990 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1991
1992 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1993 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Thierry MERLEcb977162009-01-20 18:01:33 -03001994 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001995
1996 cap->version = EM28XX_VERSION_CODE;
1997 cap->capabilities = V4L2_CAP_TUNER;
1998 return 0;
1999}
2000
2001static int radio_g_tuner(struct file *file, void *priv,
2002 struct v4l2_tuner *t)
2003{
2004 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2005
2006 if (unlikely(t->index > 0))
2007 return -EINVAL;
2008
2009 strcpy(t->name, "Radio");
2010 t->type = V4L2_TUNER_RADIO;
2011
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002012 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002013 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002014 mutex_unlock(&dev->lock);
2015
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002016 return 0;
2017}
2018
2019static int radio_enum_input(struct file *file, void *priv,
2020 struct v4l2_input *i)
2021{
2022 if (i->index != 0)
2023 return -EINVAL;
2024 strcpy(i->name, "Radio");
2025 i->type = V4L2_INPUT_TYPE_TUNER;
2026
2027 return 0;
2028}
2029
2030static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
2031{
2032 if (unlikely(a->index))
2033 return -EINVAL;
2034
2035 strcpy(a->name, "Radio");
2036 return 0;
2037}
2038
2039static int radio_s_tuner(struct file *file, void *priv,
2040 struct v4l2_tuner *t)
2041{
2042 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
2043
2044 if (0 != t->index)
2045 return -EINVAL;
2046
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002047 mutex_lock(&dev->lock);
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002048 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002049 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002050
2051 return 0;
2052}
2053
2054static int radio_s_audio(struct file *file, void *fh,
2055 struct v4l2_audio *a)
2056{
2057 return 0;
2058}
2059
2060static int radio_s_input(struct file *file, void *fh, unsigned int i)
2061{
2062 return 0;
2063}
2064
2065static int radio_queryctrl(struct file *file, void *priv,
2066 struct v4l2_queryctrl *qc)
2067{
2068 int i;
2069
2070 if (qc->id < V4L2_CID_BASE ||
2071 qc->id >= V4L2_CID_LASTP1)
2072 return -EINVAL;
2073
Mauro Carvalho Chehabed10daa2009-07-19 09:10:06 -03002074 for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
2075 if (qc->id && qc->id == ac97_qctrl[i].id) {
2076 memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002077 return 0;
2078 }
2079 }
2080
2081 return -EINVAL;
2082}
2083
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002084/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002085 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002086 * inits the device and starts isoc transfer
2087 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002088static int em28xx_v4l2_open(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002089{
Hans Verkuilbec43662008-12-30 06:58:20 -03002090 int minor = video_devdata(filp)->minor;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002091 int errCode = 0, radio;
2092 struct em28xx *dev;
2093 enum v4l2_buf_type fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002094 struct em28xx_fh *fh;
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002095 enum v4l2_field field;
Markus Rechberger9c755412005-11-08 21:37:52 -08002096
Hans Verkuilbec43662008-12-30 06:58:20 -03002097 dev = em28xx_get_device(minor, &fh_type, &radio);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002098
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002099 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002100 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002101
2102 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08002103
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002104 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002105 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002106
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002107
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002108 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002109 if (!fh) {
2110 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002111 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002112 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002113 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002114 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002115 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002116 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002117 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002119 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002120 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002121 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002122 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002123
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002124 /* Needed, since GPIO might have disabled power of
2125 some i2c device
2126 */
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002127 em28xx_wake_i2c(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002128
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002129 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002130 if (fh->radio) {
2131 em28xx_videodbg("video_open: setting radio device\n");
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002132 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002133 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134
2135 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002136
Mauro Carvalho Chehabc2a6b542009-08-08 03:14:55 -03002137 if (dev->progressive)
2138 field = V4L2_FIELD_NONE;
2139 else
2140 field = V4L2_FIELD_INTERLACED;
2141
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002142 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
2143 NULL, &dev->slock,
2144 V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
2145 sizeof(struct em28xx_buffer), fh);
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002146
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002147 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
2148 NULL, &dev->slock,
2149 V4L2_BUF_TYPE_VBI_CAPTURE,
2150 V4L2_FIELD_SEQ_TB,
2151 sizeof(struct em28xx_buffer), fh);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002152
Ingo Molnar3593cab2006-02-07 06:49:14 -02002153 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03002154
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002155 return errCode;
2156}
2157
2158/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002159 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002160 * unregisters the v4l2,i2c and usb devices
2161 * called when the device gets disconected or at module unload
2162*/
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002163void em28xx_release_analog_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002164{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002165
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002166 /*FIXME: I2C IR should be disconnected */
2167
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002168 if (dev->radio_dev) {
2169 if (-1 != dev->radio_dev->minor)
2170 video_unregister_device(dev->radio_dev);
2171 else
2172 video_device_release(dev->radio_dev);
2173 dev->radio_dev = NULL;
2174 }
2175 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03002176 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
2177 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002178 if (-1 != dev->vbi_dev->minor)
2179 video_unregister_device(dev->vbi_dev);
2180 else
2181 video_device_release(dev->vbi_dev);
2182 dev->vbi_dev = NULL;
2183 }
2184 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03002185 em28xx_info("V4L2 device /dev/video%d deregistered\n",
2186 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002187 if (-1 != dev->vdev->minor)
2188 video_unregister_device(dev->vdev);
2189 else
2190 video_device_release(dev->vdev);
2191 dev->vdev = NULL;
2192 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002193}
2194
2195/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002196 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002197 * stops streaming and deallocates all resources allocated by the v4l2
2198 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002199 */
Hans Verkuilbec43662008-12-30 06:58:20 -03002200static int em28xx_v4l2_close(struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002201{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002202 struct em28xx_fh *fh = filp->private_data;
2203 struct em28xx *dev = fh->dev;
2204 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08002206 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002208 if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002209 videobuf_stop(&fh->vb_vidq);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002210 res_free(fh, EM28XX_RESOURCE_VIDEO);
2211 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002213 if (res_check(fh, EM28XX_RESOURCE_VBI)) {
2214 videobuf_stop(&fh->vb_vbiq);
2215 res_free(fh, EM28XX_RESOURCE_VBI);
2216 }
2217
Devin Heitmuellere3ba4d32009-09-15 00:18:06 -03002218 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002219 /* the device is already disconnect,
2220 free the remaining resources */
2221 if (dev->state & DEV_DISCONNECTED) {
2222 em28xx_release_resources(dev);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002223 kfree(dev);
2224 return 0;
2225 }
2226
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002227 /* Save some power by putting tuner to sleep */
Hans Verkuil7c9fc9d2009-04-01 03:49:59 -03002228 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002229
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002230 /* do this before setting alternate! */
2231 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03002232 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002233
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002234 /* set alternate 0 */
2235 dev->alt = 0;
2236 em28xx_videodbg("setting alternate 0\n");
2237 errCode = usb_set_interface(dev->udev, 0, 0);
2238 if (errCode < 0) {
2239 em28xx_errdev("cannot change alternate number to "
2240 "0 (error=%i)\n", errCode);
2241 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002243
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002244 videobuf_mmap_free(&fh->vb_vidq);
2245 videobuf_mmap_free(&fh->vb_vbiq);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002246 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247 dev->users--;
2248 wake_up_interruptible_nr(&dev->open, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002249 return 0;
2250}
2251
2252/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002253 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254 * will allocate buffers when called for the first time
2255 */
2256static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002257em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03002258 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002259{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002260 struct em28xx_fh *fh = filp->private_data;
2261 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002262 int rc;
2263
2264 rc = check_dev(dev);
2265 if (rc < 0)
2266 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002267
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03002268 /* FIXME: read() is not prepared to allow changing the video
2269 resolution while streaming. Seems a bug at em28xx_set_fmt
2270 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002271
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002272 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002273 if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
2274 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03002275
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002276 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
2277 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002278 }
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002279
2280
2281 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002282 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2283 return -EBUSY;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002284
2285 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
2286 filp->f_flags & O_NONBLOCK);
2287 }
2288
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002289 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002290}
2291
2292/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002293 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002294 * will allocate buffers when called for the first time
2295 */
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002296static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002298 struct em28xx_fh *fh = filp->private_data;
2299 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002300 int rc;
2301
2302 rc = check_dev(dev);
2303 if (rc < 0)
2304 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002305
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002306 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
2307 if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
2308 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002309 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002310 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
2311 if (!res_get(fh, EM28XX_RESOURCE_VBI))
2312 return POLLERR;
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002313 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002314 } else {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002315 return POLLERR;
Devin Heitmueller8c873d32009-09-03 00:23:27 -03002316 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002317}
2318
2319/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002320 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002321 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002322static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002323{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002324 struct em28xx_fh *fh = filp->private_data;
2325 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002326 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08002327
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002328 rc = check_dev(dev);
2329 if (rc < 0)
2330 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002331
Devin Heitmueller91f6dce2009-09-02 22:23:23 -03002332 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
2333 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
2334 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
2335 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002336
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002337 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
2338 (unsigned long)vma->vm_start,
2339 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
2340 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002341
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002342 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002343}
2344
Hans Verkuilbec43662008-12-30 06:58:20 -03002345static const struct v4l2_file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002346 .owner = THIS_MODULE,
2347 .open = em28xx_v4l2_open,
2348 .release = em28xx_v4l2_close,
2349 .read = em28xx_v4l2_read,
2350 .poll = em28xx_v4l2_poll,
2351 .mmap = em28xx_v4l2_mmap,
2352 .ioctl = video_ioctl2,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002353};
2354
Hans Verkuila3998102008-07-21 02:57:38 -03002355static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002356 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03002357 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2358 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2359 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2360 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Devin Heitmueller28abf0832009-09-01 01:54:54 -03002361 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2362 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002363 .vidioc_g_audio = vidioc_g_audio,
2364 .vidioc_s_audio = vidioc_s_audio,
2365 .vidioc_cropcap = vidioc_cropcap,
2366
Hans Verkuil78b526a2008-05-28 12:16:41 -03002367 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
2368 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
2369 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002370
2371 .vidioc_reqbufs = vidioc_reqbufs,
2372 .vidioc_querybuf = vidioc_querybuf,
2373 .vidioc_qbuf = vidioc_qbuf,
2374 .vidioc_dqbuf = vidioc_dqbuf,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002375 .vidioc_g_std = vidioc_g_std,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002376 .vidioc_s_std = vidioc_s_std,
Mauro Carvalho Chehabd96ecda2009-08-06 21:53:59 -03002377 .vidioc_g_parm = vidioc_g_parm,
2378 .vidioc_s_parm = vidioc_s_parm,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002379 .vidioc_enum_input = vidioc_enum_input,
2380 .vidioc_g_input = vidioc_g_input,
2381 .vidioc_s_input = vidioc_s_input,
2382 .vidioc_queryctrl = vidioc_queryctrl,
2383 .vidioc_g_ctrl = vidioc_g_ctrl,
2384 .vidioc_s_ctrl = vidioc_s_ctrl,
2385 .vidioc_streamon = vidioc_streamon,
2386 .vidioc_streamoff = vidioc_streamoff,
2387 .vidioc_g_tuner = vidioc_g_tuner,
2388 .vidioc_s_tuner = vidioc_s_tuner,
2389 .vidioc_g_frequency = vidioc_g_frequency,
2390 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002391#ifdef CONFIG_VIDEO_ADV_DEBUG
2392 .vidioc_g_register = vidioc_g_register,
2393 .vidioc_s_register = vidioc_s_register,
Mauro Carvalho Chehab14983d82008-12-22 20:58:41 -03002394 .vidioc_g_chip_ident = vidioc_g_chip_ident,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002395#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002396#ifdef CONFIG_VIDEO_V4L1_COMPAT
2397 .vidiocgmbuf = vidiocgmbuf,
2398#endif
Hans Verkuila3998102008-07-21 02:57:38 -03002399};
2400
2401static const struct video_device em28xx_video_template = {
2402 .fops = &em28xx_v4l_fops,
2403 .release = video_device_release,
2404 .ioctl_ops = &video_ioctl_ops,
2405
2406 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002407
2408 .tvnorms = V4L2_STD_ALL,
Devin Heitmueller19bf0032009-09-11 00:40:18 -03002409 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002410};
2411
Hans Verkuilbec43662008-12-30 06:58:20 -03002412static const struct v4l2_file_operations radio_fops = {
Hans Verkuila3998102008-07-21 02:57:38 -03002413 .owner = THIS_MODULE,
2414 .open = em28xx_v4l2_open,
2415 .release = em28xx_v4l2_close,
2416 .ioctl = video_ioctl2,
Hans Verkuila3998102008-07-21 02:57:38 -03002417};
2418
2419static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002420 .vidioc_querycap = radio_querycap,
2421 .vidioc_g_tuner = radio_g_tuner,
2422 .vidioc_enum_input = radio_enum_input,
2423 .vidioc_g_audio = radio_g_audio,
2424 .vidioc_s_tuner = radio_s_tuner,
2425 .vidioc_s_audio = radio_s_audio,
2426 .vidioc_s_input = radio_s_input,
2427 .vidioc_queryctrl = radio_queryctrl,
2428 .vidioc_g_ctrl = vidioc_g_ctrl,
2429 .vidioc_s_ctrl = vidioc_s_ctrl,
2430 .vidioc_g_frequency = vidioc_g_frequency,
2431 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002432#ifdef CONFIG_VIDEO_ADV_DEBUG
2433 .vidioc_g_register = vidioc_g_register,
2434 .vidioc_s_register = vidioc_s_register,
2435#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002436};
2437
Hans Verkuila3998102008-07-21 02:57:38 -03002438static struct video_device em28xx_radio_template = {
2439 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03002440 .fops = &radio_fops,
2441 .ioctl_ops = &radio_ioctl_ops,
2442 .minor = -1,
2443};
2444
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002445/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002446
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002447
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002448
Adrian Bunk532fe652008-01-28 22:10:48 -03002449static struct video_device *em28xx_vdev_init(struct em28xx *dev,
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002450 const struct video_device *template,
2451 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002452{
2453 struct video_device *vfd;
2454
2455 vfd = video_device_alloc();
2456 if (NULL == vfd)
2457 return NULL;
Douglas Schilling Landgraff2cf2502009-03-31 17:10:58 -03002458
2459 *vfd = *template;
2460 vfd->minor = -1;
2461 vfd->v4l2_dev = &dev->v4l2_dev;
2462 vfd->release = video_device_release;
2463 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002464
2465 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2466 dev->name, type_name);
2467
2468 return vfd;
2469}
2470
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002471int em28xx_register_analog_devices(struct em28xx *dev)
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002472{
Robert Krakora6e7b9ea2009-01-18 21:59:34 -03002473 u8 val;
Mauro Carvalho Chehab2e5ef2d2008-12-28 22:26:36 -03002474 int ret;
2475
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002476 printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
2477 dev->name,
2478 (EM28XX_VERSION_CODE >> 16) & 0xff,
2479 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
2480
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002481 /* set default norm */
2482 dev->norm = em28xx_video_template.current_norm;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002483 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002484 dev->ctl_input = 0;
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002485
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002486 /* Analog specific initialization */
2487 dev->format = &format[0];
Mauro Carvalho Chehabed5f1432009-07-02 17:34:04 -03002488 em28xx_set_video_format(dev, format[0].fourcc,
2489 norm_maxw(dev), norm_maxh(dev));
2490
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002491 video_mux(dev, dev->ctl_input);
2492
2493 /* Audio defaults */
2494 dev->mute = 1;
2495 dev->volume = 0x1f;
2496
Mauro Carvalho Chehab24c3c412009-01-07 22:49:25 -03002497/* 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
2502 em28xx_set_outfmt(dev);
2503 em28xx_colorlevels_set_default(dev);
2504 em28xx_compression_disable(dev);
Mauro Carvalho Chehab1a23f812008-12-28 22:18:14 -03002505
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002506 /* allocate and fill video video_device struct */
2507 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2508 if (!dev->vdev) {
2509 em28xx_errdev("cannot allocate video_device.\n");
2510 return -ENODEV;
2511 }
2512
2513 /* register v4l2 video video_device */
2514 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2515 video_nr[dev->devno]);
2516 if (ret) {
2517 em28xx_errdev("unable to register video device (error=%i).\n",
2518 ret);
2519 return ret;
2520 }
2521
2522 /* Allocate and fill vbi video_device struct */
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002523 if (em28xx_vbi_supported(dev) == 1) {
2524 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2525 "vbi");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002526
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002527 /* register v4l2 vbi video_device */
2528 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2529 vbi_nr[dev->devno]);
2530 if (ret < 0) {
2531 em28xx_errdev("unable to register vbi device\n");
2532 return ret;
2533 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002534 }
2535
2536 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -03002537 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2538 "radio");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002539 if (!dev->radio_dev) {
2540 em28xx_errdev("cannot allocate video_device.\n");
2541 return -ENODEV;
2542 }
2543 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2544 radio_nr[dev->devno]);
2545 if (ret < 0) {
2546 em28xx_errdev("can't register radio device\n");
2547 return ret;
2548 }
2549 em28xx_info("Registered radio device as /dev/radio%d\n",
2550 dev->radio_dev->num);
2551 }
2552
Devin Heitmueller290c0cf2009-09-11 00:01:06 -03002553 em28xx_info("V4L2 video device registered as /dev/video%d\n",
2554 dev->vdev->num);
2555
2556 if (dev->vbi_dev)
2557 em28xx_info("V4L2 VBI device registered as /dev/vbi%d\n",
2558 dev->vbi_dev->num);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002559
2560 return 0;
2561}