blob: 838e7ecfd8652a4051ae5ccd880ca1dbd1776c25 [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 Verkuil2474ed42006-03-19 12:35:57 -030041#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030042#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080043
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080044#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
45 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030046 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080048
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080049#define DRIVER_NAME "em28xx"
50#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030051#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080052
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080053#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080054 if (video_debug) \
55 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030056 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080057
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030058static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030059module_param(isoc_debug, int, 0644);
60MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030061
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030062#define em28xx_isocdbg(fmt, arg...) \
63do {\
64 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030065 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030066 dev->name, __func__ , ##arg); \
67 } \
68 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030069
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080070MODULE_AUTHOR(DRIVER_AUTHOR);
71MODULE_DESCRIPTION(DRIVER_DESC);
72MODULE_LICENSE("GPL");
73
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080074static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080075
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080076static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030078static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
79static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080081module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020082module_param_array(video_nr, int, NULL, 0444);
83module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030084module_param_array(radio_nr, int, NULL, 0444);
85MODULE_PARM_DESC(card, "card type");
86MODULE_PARM_DESC(video_nr, "video device numbers");
87MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
88MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080089
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030090static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030091module_param(video_debug, int, 0644);
92MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080093
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020094/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
95static unsigned long em28xx_devused;
96
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080097/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020098/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080099static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800100 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200101 .id = V4L2_CID_AUDIO_VOLUME,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Volume",
104 .minimum = 0x0,
105 .maximum = 0x1f,
106 .step = 0x1,
107 .default_value = 0x1f,
108 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300109 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200110 .id = V4L2_CID_AUDIO_MUTE,
111 .type = V4L2_CTRL_TYPE_BOOLEAN,
112 .name = "Mute",
113 .minimum = 0,
114 .maximum = 1,
115 .step = 1,
116 .default_value = 1,
117 .flags = 0,
118 }
119};
120
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800121static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800122
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300123/* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
126
127/*
128 * Announces that a buffer were filled and request the next
129 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300130static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
133{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
139
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300140 dev->isoc_ctl.buf = NULL;
141
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
144}
145
146/*
147 * Identify the buffer header type and properly handles
148 */
149static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
152 unsigned char *p,
153 unsigned char *outp, unsigned long len)
154{
155 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300156 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300157 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300158
159 if (dma_q->pos + len > buf->vb.size)
160 len = buf->vb.size - dma_q->pos;
161
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300162 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 em28xx_isocdbg("frame is not complete\n");
164 len += 4;
165 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300166 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300167
168 startread = p;
169 remain = len;
170
171 /* Interlaces frame */
172 if (buf->top_field)
173 fieldstart = outp;
174 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300175 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300176
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300177 linesdone = dma_q->pos / bytesperline;
178 currlinedone = dma_q->pos % bytesperline;
179 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300180 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300181 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182 lencopy = lencopy > remain ? remain : lencopy;
183
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300184 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300185 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300186 ((char *)startwrite + lencopy) -
187 ((char *)outp + buf->vb.size));
188 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300189 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300190 if (lencopy <= 0)
191 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300192 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300193
194 remain -= lencopy;
195
196 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300197 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300198 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300199 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200 lencopy = remain;
201 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300202 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300203
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300204 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300205 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 ((char *)startwrite + lencopy) -
207 ((char *)outp + buf->vb.size));
208 lencopy = remain = (char *)outp + buf->vb.size -
209 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300210 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300211 if (lencopy <= 0)
212 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300213
214 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300215
216 remain -= lencopy;
217 }
218
219 dma_q->pos += len;
220}
221
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300222static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300223 int packet, int status)
224{
225 char *errmsg = "Unknown";
226
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300228 case -ENOENT:
229 errmsg = "unlinked synchronuously";
230 break;
231 case -ECONNRESET:
232 errmsg = "unlinked asynchronuously";
233 break;
234 case -ENOSR:
235 errmsg = "Buffer error (overrun)";
236 break;
237 case -EPIPE:
238 errmsg = "Stalled (device not responding)";
239 break;
240 case -EOVERFLOW:
241 errmsg = "Babble (bad cable?)";
242 break;
243 case -EPROTO:
244 errmsg = "Bit-stuff error (bad cable?)";
245 break;
246 case -EILSEQ:
247 errmsg = "CRC/Timeout (could be anything)";
248 break;
249 case -ETIME:
250 errmsg = "Device does not respond";
251 break;
252 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300253 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300254 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
255 } else {
256 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
257 packet, status, errmsg);
258 }
259}
260
261/*
262 * video-buf generic routine to get the next available buffer
263 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300264static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265 struct em28xx_buffer **buf)
266{
267 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300268 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300269
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300270 if (list_empty(&dma_q->active)) {
271 em28xx_isocdbg("No active queue to serve\n");
272 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300273 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300274 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300275 }
276
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300277 /* Get the next buffer */
278 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
279
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300280 /* Cleans up buffer - Usefull for testing for frame/URB loss */
281 outp = videobuf_to_vmalloc(&(*buf)->vb);
282 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300283
284 dev->isoc_ctl.buf = *buf;
285
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300286 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300287}
288
289/*
290 * Controls the isoc copy of each urb packet
291 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300292static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300293{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300294 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300296 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300298 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299
300 if (!dev)
301 return 0;
302
303 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
304 return 0;
305
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300306 if (urb->status < 0) {
307 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300308 if (urb->status == -ENOENT)
309 return 0;
310 }
311
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300312 buf = dev->isoc_ctl.buf;
313 if (buf != NULL)
314 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300315
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300316 for (i = 0; i < urb->number_of_packets; i++) {
317 int status = urb->iso_frame_desc[i].status;
318
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300319 if (status < 0) {
320 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300321 if (urb->iso_frame_desc[i].status != -EPROTO)
322 continue;
323 }
324
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300325 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300326
327 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300328 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300329 continue;
330 }
331 if (urb->iso_frame_desc[i].actual_length >
332 dev->max_pkt_size) {
333 em28xx_isocdbg("packet bigger than packet size");
334 continue;
335 }
336
337 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300338
339 /* FIXME: incomplete buffer checks where removed to make
340 logic simpler. Impacts of those changes should be evaluated
341 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300342 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
343 em28xx_isocdbg("VBI HEADER!!!\n");
344 /* FIXME: Should add vbi copy */
345 continue;
346 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300347 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300348 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
349 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300350
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300351 if (!(p[2] & 1)) {
352 if (buf != NULL)
353 buffer_filled(dev, dma_q, buf);
354 get_next_buf(dma_q, &buf);
355 if (buf == NULL)
356 outp = NULL;
357 else
358 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300359 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300360
361 if (buf != NULL) {
362 if (p[2] & 1)
363 buf->top_field = 0;
364 else
365 buf->top_field = 1;
366 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300367
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300370 if (buf != NULL)
371 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300372 }
373 return rc;
374}
375
376/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377 Videobuf operations
378 ------------------------------------------------------------------*/
379
380static int
381buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
382{
383 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300384 struct em28xx *dev = fh->dev;
385 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300386
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300387 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300388 if (0 == *count)
389 *count = EM28XX_DEF_BUF;
390
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300391 if (*count < EM28XX_MIN_BUF)
392 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300393
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300394 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300395 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300396 f.frequency = dev->ctl_freq;
397
398 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
399
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300400 return 0;
401}
402
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300403/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300404static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
405{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300406 struct em28xx_fh *fh = vq->priv_data;
407 struct em28xx *dev = fh->dev;
408 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300409 if (in_interrupt())
410 BUG();
411
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300412 /* We used to wait for the buffer to finish here, but this didn't work
413 because, as we were keeping the state as VIDEOBUF_QUEUED,
414 videobuf_queue_cancel marked it as finished for us.
415 (Also, it could wedge forever if the hardware was misconfigured.)
416
417 This should be safe; by the time we get here, the buffer isn't
418 queued anymore. If we ever start marking the buffers as
419 VIDEOBUF_ACTIVE, it won't be, though.
420 */
421 spin_lock_irqsave(&dev->slock, flags);
422 if (dev->isoc_ctl.buf == buf)
423 dev->isoc_ctl.buf = NULL;
424 spin_unlock_irqrestore(&dev->slock, flags);
425
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426 videobuf_vmalloc_free(&buf->vb);
427 buf->vb.state = VIDEOBUF_NEEDS_INIT;
428}
429
430static int
431buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
432 enum v4l2_field field)
433{
434 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300435 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 struct em28xx *dev = fh->dev;
437 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439 /* FIXME: It assumes depth = 16 */
440 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300441 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300442
443 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
444 return -EINVAL;
445
Brandon Philips05612972008-04-13 14:57:01 -0300446 buf->vb.width = dev->width;
447 buf->vb.height = dev->height;
448 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300449
450 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300451 rc = videobuf_iolock(vq, &buf->vb, NULL);
452 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300453 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 }
455
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300457 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458
459 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300460 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
461 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300462 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300463 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464 goto fail;
465 }
466
467 buf->vb.state = VIDEOBUF_PREPARED;
468 return 0;
469
470fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300471 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300472 return rc;
473}
474
475static void
476buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
477{
478 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
479 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300480 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300481 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300482
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300483 buf->vb.state = VIDEOBUF_QUEUED;
484 list_add_tail(&buf->vb.queue, &vidq->active);
485
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300486}
487
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300488static void buffer_release(struct videobuf_queue *vq,
489 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300490{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300491 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300493 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300495 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300497 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498}
499
500static struct videobuf_queue_ops em28xx_video_qops = {
501 .buf_setup = buffer_setup,
502 .buf_prepare = buffer_prepare,
503 .buf_queue = buffer_queue,
504 .buf_release = buffer_release,
505};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800506
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300507/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800510 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511 * inits registers with sane defaults
512 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800513static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800514{
515
516 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300517 if (!dev->is_em2800)
518 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800519
520 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200521
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300522/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
523/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
524 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200525
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800526 dev->mute = 1; /* maybe not the right place... */
527 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300528
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800529 em28xx_outfmt_set_yuv422(dev);
530 em28xx_colorlevels_set_default(dev);
531 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800532
533 return 0;
534}
535
536/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800537 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800538 * configure i2c attached devices
539 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800540static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800541{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300542 struct v4l2_routing route;
543
544 route.input = INPUT(dev->ctl_input)->vmux;
545 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100546 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300547 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300548 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800549}
550
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800551static void video_mux(struct em28xx *dev, int index)
552{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300553 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800554
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300555 route.input = INPUT(index)->vmux;
556 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800557 dev->ctl_input = index;
558 dev->ctl_ainput = INPUT(index)->amux;
559
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300560 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800561
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800562 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300563 if (dev->i2s_speed) {
564 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
565 &dev->i2s_speed);
566 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300567 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300568 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300569 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300570 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
571 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800572 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300573
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300574 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800575}
576
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300577/* Usage lock check functions */
578static int res_get(struct em28xx_fh *fh)
579{
580 struct em28xx *dev = fh->dev;
581 int rc = 0;
582
583 /* This instance already has stream_on */
584 if (fh->stream_on)
585 return rc;
586
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300587 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300588 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300589
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300590 mutex_lock(&dev->lock);
591 dev->stream_on = 1;
592 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300593 mutex_unlock(&dev->lock);
594 return rc;
595}
596
597static int res_check(struct em28xx_fh *fh)
598{
599 return (fh->stream_on);
600}
601
602static void res_free(struct em28xx_fh *fh)
603{
604 struct em28xx *dev = fh->dev;
605
606 mutex_lock(&dev->lock);
607 fh->stream_on = 0;
608 dev->stream_on = 0;
609 mutex_unlock(&dev->lock);
610}
611
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800612/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300613 * em28xx_get_ctrl()
614 * return the current saturation, brightness or contrast, mute state
615 */
616static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
617{
618 switch (ctrl->id) {
619 case V4L2_CID_AUDIO_MUTE:
620 ctrl->value = dev->mute;
621 return 0;
622 case V4L2_CID_AUDIO_VOLUME:
623 ctrl->value = dev->volume;
624 return 0;
625 default:
626 return -EINVAL;
627 }
628}
629
630/*
631 * em28xx_set_ctrl()
632 * mute or set new saturation, brightness or contrast
633 */
634static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
635{
636 switch (ctrl->id) {
637 case V4L2_CID_AUDIO_MUTE:
638 if (ctrl->value != dev->mute) {
639 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300640 return em28xx_audio_analog_set(dev);
641 }
642 return 0;
643 case V4L2_CID_AUDIO_VOLUME:
644 dev->volume = ctrl->value;
645 return em28xx_audio_analog_set(dev);
646 default:
647 return -EINVAL;
648 }
649}
650
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300651static int check_dev(struct em28xx *dev)
652{
653 if (dev->state & DEV_DISCONNECTED) {
654 em28xx_errdev("v4l2 ioctl: device not present\n");
655 return -ENODEV;
656 }
657
658 if (dev->state & DEV_MISCONFIGURED) {
659 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
660 "close and open it again\n");
661 return -EIO;
662 }
663 return 0;
664}
665
666static void get_scale(struct em28xx *dev,
667 unsigned int width, unsigned int height,
668 unsigned int *hscale, unsigned int *vscale)
669{
670 unsigned int maxw = norm_maxw(dev);
671 unsigned int maxh = norm_maxh(dev);
672
673 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
674 if (*hscale >= 0x4000)
675 *hscale = 0x3fff;
676
677 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
678 if (*vscale >= 0x4000)
679 *vscale = 0x3fff;
680}
681
682/* ------------------------------------------------------------------
683 IOCTL vidioc handling
684 ------------------------------------------------------------------*/
685
Hans Verkuil78b526a2008-05-28 12:16:41 -0300686static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300687 struct v4l2_format *f)
688{
689 struct em28xx_fh *fh = priv;
690 struct em28xx *dev = fh->dev;
691
692 mutex_lock(&dev->lock);
693
694 f->fmt.pix.width = dev->width;
695 f->fmt.pix.height = dev->height;
696 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300697 f->fmt.pix.bytesperline = dev->width * 2;
698 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300699 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
700
701 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
702 f->fmt.pix.field = dev->interlaced ?
703 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
704
705 mutex_unlock(&dev->lock);
706 return 0;
707}
708
Hans Verkuil78b526a2008-05-28 12:16:41 -0300709static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710 struct v4l2_format *f)
711{
712 struct em28xx_fh *fh = priv;
713 struct em28xx *dev = fh->dev;
714 int width = f->fmt.pix.width;
715 int height = f->fmt.pix.height;
716 unsigned int maxw = norm_maxw(dev);
717 unsigned int maxh = norm_maxh(dev);
718 unsigned int hscale, vscale;
719
720 /* width must even because of the YUYV format
721 height must be even because of interlacing */
722 height &= 0xfffe;
723 width &= 0xfffe;
724
725 if (height < 32)
726 height = 32;
727 if (height > maxh)
728 height = maxh;
729 if (width < 48)
730 width = 48;
731 if (width > maxw)
732 width = maxw;
733
734 mutex_lock(&dev->lock);
735
736 if (dev->is_em2800) {
737 /* the em2800 can only scale down to 50% */
738 if (height % (maxh / 2))
739 height = maxh;
740 if (width % (maxw / 2))
741 width = maxw;
742 /* according to empiatech support */
743 /* the MaxPacketSize is to small to support */
744 /* framesizes larger than 640x480 @ 30 fps */
745 /* or 640x576 @ 25 fps. As this would cut */
746 /* of a part of the image we prefer */
747 /* 360x576 or 360x480 for now */
748 if (width == maxw && height == maxh)
749 width /= 2;
750 }
751
752 get_scale(dev, width, height, &hscale, &vscale);
753
754 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
755 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
756
757 f->fmt.pix.width = width;
758 f->fmt.pix.height = height;
759 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
760 f->fmt.pix.bytesperline = width * 2;
761 f->fmt.pix.sizeimage = width * 2 * height;
762 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
763 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
764
765 mutex_unlock(&dev->lock);
766 return 0;
767}
768
Hans Verkuil78b526a2008-05-28 12:16:41 -0300769static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300770 struct v4l2_format *f)
771{
772 struct em28xx_fh *fh = priv;
773 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300774 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300775
776 rc = check_dev(dev);
777 if (rc < 0)
778 return rc;
779
Hans Verkuil78b526a2008-05-28 12:16:41 -0300780 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300781
782 mutex_lock(&dev->lock);
783
Brandon Philips05612972008-04-13 14:57:01 -0300784 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
785 em28xx_errdev("%s queue busy\n", __func__);
786 rc = -EBUSY;
787 goto out;
788 }
789
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300790 if (dev->stream_on && !fh->stream_on) {
791 em28xx_errdev("%s device in use by another fh\n", __func__);
792 rc = -EBUSY;
793 goto out;
794 }
795
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300796 /* set new image size */
797 dev->width = f->fmt.pix.width;
798 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
800
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300801 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300802 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803
Brandon Philips05612972008-04-13 14:57:01 -0300804 rc = 0;
805
806out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300807 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300808 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809}
810
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300811static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812{
813 struct em28xx_fh *fh = priv;
814 struct em28xx *dev = fh->dev;
815 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816 int rc;
817
818 rc = check_dev(dev);
819 if (rc < 0)
820 return rc;
821
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300823 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300824 mutex_unlock(&dev->lock);
825
826 /* Adjusts width/height, if needed */
827 f.fmt.pix.width = dev->width;
828 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300829 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300830
831 mutex_lock(&dev->lock);
832
833 /* set new image size */
834 dev->width = f.fmt.pix.width;
835 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300836 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
837
838 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300839 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300840
841 mutex_unlock(&dev->lock);
842 return 0;
843}
844
845static const char *iname[] = {
846 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
847 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
848 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
849 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
850 [EM28XX_VMUX_SVIDEO] = "S-Video",
851 [EM28XX_VMUX_TELEVISION] = "Television",
852 [EM28XX_VMUX_CABLE] = "Cable TV",
853 [EM28XX_VMUX_DVB] = "DVB",
854 [EM28XX_VMUX_DEBUG] = "for debug only",
855};
856
857static int vidioc_enum_input(struct file *file, void *priv,
858 struct v4l2_input *i)
859{
860 struct em28xx_fh *fh = priv;
861 struct em28xx *dev = fh->dev;
862 unsigned int n;
863
864 n = i->index;
865 if (n >= MAX_EM28XX_INPUT)
866 return -EINVAL;
867 if (0 == INPUT(n)->type)
868 return -EINVAL;
869
870 i->index = n;
871 i->type = V4L2_INPUT_TYPE_CAMERA;
872
873 strcpy(i->name, iname[INPUT(n)->type]);
874
875 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
876 (EM28XX_VMUX_CABLE == INPUT(n)->type))
877 i->type = V4L2_INPUT_TYPE_TUNER;
878
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300879 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300880
881 return 0;
882}
883
884static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
885{
886 struct em28xx_fh *fh = priv;
887 struct em28xx *dev = fh->dev;
888
889 *i = dev->ctl_input;
890
891 return 0;
892}
893
894static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
895{
896 struct em28xx_fh *fh = priv;
897 struct em28xx *dev = fh->dev;
898 int rc;
899
900 rc = check_dev(dev);
901 if (rc < 0)
902 return rc;
903
904 if (i >= MAX_EM28XX_INPUT)
905 return -EINVAL;
906 if (0 == INPUT(i)->type)
907 return -EINVAL;
908
909 mutex_lock(&dev->lock);
910
911 video_mux(dev, i);
912
913 mutex_unlock(&dev->lock);
914 return 0;
915}
916
917static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
918{
919 struct em28xx_fh *fh = priv;
920 struct em28xx *dev = fh->dev;
921 unsigned int index = a->index;
922
923 if (a->index > 1)
924 return -EINVAL;
925
926 index = dev->ctl_ainput;
927
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300928 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300929 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300930 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300931 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300932
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933 a->capability = V4L2_AUDCAP_STEREO;
934 a->index = index;
935
936 return 0;
937}
938
939static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
940{
941 struct em28xx_fh *fh = priv;
942 struct em28xx *dev = fh->dev;
943
944 if (a->index != dev->ctl_ainput)
945 return -EINVAL;
946
947 return 0;
948}
949
950static int vidioc_queryctrl(struct file *file, void *priv,
951 struct v4l2_queryctrl *qc)
952{
953 struct em28xx_fh *fh = priv;
954 struct em28xx *dev = fh->dev;
955 int id = qc->id;
956 int i;
957 int rc;
958
959 rc = check_dev(dev);
960 if (rc < 0)
961 return rc;
962
963 memset(qc, 0, sizeof(*qc));
964
965 qc->id = id;
966
967 if (!dev->has_msp34xx) {
968 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
969 if (qc->id && qc->id == em28xx_qctrl[i].id) {
970 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
971 return 0;
972 }
973 }
974 }
975 mutex_lock(&dev->lock);
976 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
977 mutex_unlock(&dev->lock);
978
979 if (qc->type)
980 return 0;
981 else
982 return -EINVAL;
983}
984
985static int vidioc_g_ctrl(struct file *file, void *priv,
986 struct v4l2_control *ctrl)
987{
988 struct em28xx_fh *fh = priv;
989 struct em28xx *dev = fh->dev;
990 int rc;
991
992 rc = check_dev(dev);
993 if (rc < 0)
994 return rc;
995 mutex_lock(&dev->lock);
996
997 if (!dev->has_msp34xx)
998 rc = em28xx_get_ctrl(dev, ctrl);
999 else
1000 rc = -EINVAL;
1001
1002 if (rc == -EINVAL) {
1003 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1004 rc = 0;
1005 }
1006
1007 mutex_unlock(&dev->lock);
1008 return rc;
1009}
1010
1011static int vidioc_s_ctrl(struct file *file, void *priv,
1012 struct v4l2_control *ctrl)
1013{
1014 struct em28xx_fh *fh = priv;
1015 struct em28xx *dev = fh->dev;
1016 u8 i;
1017 int rc;
1018
1019 rc = check_dev(dev);
1020 if (rc < 0)
1021 return rc;
1022
1023 mutex_lock(&dev->lock);
1024
1025 if (dev->has_msp34xx)
1026 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1027 else {
1028 rc = 1;
1029 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1030 if (ctrl->id == em28xx_qctrl[i].id) {
1031 if (ctrl->value < em28xx_qctrl[i].minimum ||
1032 ctrl->value > em28xx_qctrl[i].maximum) {
1033 rc = -ERANGE;
1034 break;
1035 }
1036
1037 rc = em28xx_set_ctrl(dev, ctrl);
1038 break;
1039 }
1040 }
1041 }
1042
1043 /* Control not found - try to send it to the attached devices */
1044 if (rc == 1) {
1045 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1046 rc = 0;
1047 }
1048
1049 mutex_unlock(&dev->lock);
1050 return rc;
1051}
1052
1053static int vidioc_g_tuner(struct file *file, void *priv,
1054 struct v4l2_tuner *t)
1055{
1056 struct em28xx_fh *fh = priv;
1057 struct em28xx *dev = fh->dev;
1058 int rc;
1059
1060 rc = check_dev(dev);
1061 if (rc < 0)
1062 return rc;
1063
1064 if (0 != t->index)
1065 return -EINVAL;
1066
1067 strcpy(t->name, "Tuner");
1068
1069 mutex_lock(&dev->lock);
1070
1071 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1072
1073 mutex_unlock(&dev->lock);
1074 return 0;
1075}
1076
1077static int vidioc_s_tuner(struct file *file, void *priv,
1078 struct v4l2_tuner *t)
1079{
1080 struct em28xx_fh *fh = priv;
1081 struct em28xx *dev = fh->dev;
1082 int rc;
1083
1084 rc = check_dev(dev);
1085 if (rc < 0)
1086 return rc;
1087
1088 if (0 != t->index)
1089 return -EINVAL;
1090
1091 mutex_lock(&dev->lock);
1092
1093 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1094
1095 mutex_unlock(&dev->lock);
1096 return 0;
1097}
1098
1099static int vidioc_g_frequency(struct file *file, void *priv,
1100 struct v4l2_frequency *f)
1101{
1102 struct em28xx_fh *fh = priv;
1103 struct em28xx *dev = fh->dev;
1104
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001105 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001106 f->frequency = dev->ctl_freq;
1107
1108 return 0;
1109}
1110
1111static int vidioc_s_frequency(struct file *file, void *priv,
1112 struct v4l2_frequency *f)
1113{
1114 struct em28xx_fh *fh = priv;
1115 struct em28xx *dev = fh->dev;
1116 int rc;
1117
1118 rc = check_dev(dev);
1119 if (rc < 0)
1120 return rc;
1121
1122 if (0 != f->tuner)
1123 return -EINVAL;
1124
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001125 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1126 return -EINVAL;
1127 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001128 return -EINVAL;
1129
1130 mutex_lock(&dev->lock);
1131
1132 dev->ctl_freq = f->frequency;
1133 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1134
1135 mutex_unlock(&dev->lock);
1136 return 0;
1137}
1138
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001139#ifdef CONFIG_VIDEO_ADV_DEBUG
1140static int em28xx_reg_len(int reg)
1141{
1142 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001143 case EM28XX_R40_AC97LSB:
1144 case EM28XX_R30_HSCALELOW:
1145 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001146 return 2;
1147 default:
1148 return 1;
1149 }
1150}
1151
1152static int vidioc_g_register(struct file *file, void *priv,
1153 struct v4l2_register *reg)
1154{
1155 struct em28xx_fh *fh = priv;
1156 struct em28xx *dev = fh->dev;
1157 int ret;
1158
1159 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1160 return -EINVAL;
1161
1162 if (em28xx_reg_len(reg->reg) == 1) {
1163 ret = em28xx_read_reg(dev, reg->reg);
1164 if (ret < 0)
1165 return ret;
1166
1167 reg->val = ret;
1168 } else {
Al Viroa954b662008-05-21 00:32:51 -03001169 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001170 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1171 reg->reg, (char *)&val, 2);
1172 if (ret < 0)
1173 return ret;
1174
Al Viroa954b662008-05-21 00:32:51 -03001175 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001176 }
1177
1178 return 0;
1179}
1180
1181static int vidioc_s_register(struct file *file, void *priv,
1182 struct v4l2_register *reg)
1183{
1184 struct em28xx_fh *fh = priv;
1185 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001186 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001187
Al Viroa954b662008-05-21 00:32:51 -03001188 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001189
1190 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1191 em28xx_reg_len(reg->reg));
1192}
1193#endif
1194
1195
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001196static int vidioc_cropcap(struct file *file, void *priv,
1197 struct v4l2_cropcap *cc)
1198{
1199 struct em28xx_fh *fh = priv;
1200 struct em28xx *dev = fh->dev;
1201
1202 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1203 return -EINVAL;
1204
1205 cc->bounds.left = 0;
1206 cc->bounds.top = 0;
1207 cc->bounds.width = dev->width;
1208 cc->bounds.height = dev->height;
1209 cc->defrect = cc->bounds;
1210 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1211 cc->pixelaspect.denominator = 59;
1212
1213 return 0;
1214}
1215
1216static int vidioc_streamon(struct file *file, void *priv,
1217 enum v4l2_buf_type type)
1218{
1219 struct em28xx_fh *fh = priv;
1220 struct em28xx *dev = fh->dev;
1221 int rc;
1222
1223 rc = check_dev(dev);
1224 if (rc < 0)
1225 return rc;
1226
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001227
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001228 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001229 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001231 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001232}
1233
1234static int vidioc_streamoff(struct file *file, void *priv,
1235 enum v4l2_buf_type type)
1236{
1237 struct em28xx_fh *fh = priv;
1238 struct em28xx *dev = fh->dev;
1239 int rc;
1240
1241 rc = check_dev(dev);
1242 if (rc < 0)
1243 return rc;
1244
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001245 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1246 return -EINVAL;
1247 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001248 return -EINVAL;
1249
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001250 videobuf_streamoff(&fh->vb_vidq);
1251 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001252
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253 return 0;
1254}
1255
1256static int vidioc_querycap(struct file *file, void *priv,
1257 struct v4l2_capability *cap)
1258{
1259 struct em28xx_fh *fh = priv;
1260 struct em28xx *dev = fh->dev;
1261
1262 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1263 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1264 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1265
1266 cap->version = EM28XX_VERSION_CODE;
1267
1268 cap->capabilities =
1269 V4L2_CAP_SLICED_VBI_CAPTURE |
1270 V4L2_CAP_VIDEO_CAPTURE |
1271 V4L2_CAP_AUDIO |
1272 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1273
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001274 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001275 cap->capabilities |= V4L2_CAP_TUNER;
1276
1277 return 0;
1278}
1279
Hans Verkuil78b526a2008-05-28 12:16:41 -03001280static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001281 struct v4l2_fmtdesc *fmtd)
1282{
1283 if (fmtd->index != 0)
1284 return -EINVAL;
1285
1286 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1287 strcpy(fmtd->description, "Packed YUY2");
1288 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1289 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1290
1291 return 0;
1292}
1293
1294/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001295static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001296 struct v4l2_format *f)
1297{
1298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
1300 int rc;
1301
1302 rc = check_dev(dev);
1303 if (rc < 0)
1304 return rc;
1305
1306 mutex_lock(&dev->lock);
1307
1308 f->fmt.sliced.service_set = 0;
1309
1310 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1311
1312 if (f->fmt.sliced.service_set == 0)
1313 rc = -EINVAL;
1314
1315 mutex_unlock(&dev->lock);
1316 return rc;
1317}
1318
Hans Verkuil78b526a2008-05-28 12:16:41 -03001319static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001320 struct v4l2_format *f)
1321{
1322 struct em28xx_fh *fh = priv;
1323 struct em28xx *dev = fh->dev;
1324 int rc;
1325
1326 rc = check_dev(dev);
1327 if (rc < 0)
1328 return rc;
1329
1330 mutex_lock(&dev->lock);
1331 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1332 mutex_unlock(&dev->lock);
1333
1334 if (f->fmt.sliced.service_set == 0)
1335 return -EINVAL;
1336
1337 return 0;
1338}
1339
1340
1341static int vidioc_reqbufs(struct file *file, void *priv,
1342 struct v4l2_requestbuffers *rb)
1343{
1344 struct em28xx_fh *fh = priv;
1345 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001346 int rc;
1347
1348 rc = check_dev(dev);
1349 if (rc < 0)
1350 return rc;
1351
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001352 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001353}
1354
1355static int vidioc_querybuf(struct file *file, void *priv,
1356 struct v4l2_buffer *b)
1357{
1358 struct em28xx_fh *fh = priv;
1359 struct em28xx *dev = fh->dev;
1360 int rc;
1361
1362 rc = check_dev(dev);
1363 if (rc < 0)
1364 return rc;
1365
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001366 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367}
1368
1369static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1370{
1371 struct em28xx_fh *fh = priv;
1372 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001373 int rc;
1374
1375 rc = check_dev(dev);
1376 if (rc < 0)
1377 return rc;
1378
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001379 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001380}
1381
1382static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1383{
1384 struct em28xx_fh *fh = priv;
1385 struct em28xx *dev = fh->dev;
1386 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387
1388 rc = check_dev(dev);
1389 if (rc < 0)
1390 return rc;
1391
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001392 return (videobuf_dqbuf(&fh->vb_vidq, b,
1393 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001394}
1395
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001396#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001397static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001398{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001399 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001400
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001401 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001402}
1403#endif
1404
1405
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001406/* ----------------------------------------------------------- */
1407/* RADIO ESPECIFIC IOCTLS */
1408/* ----------------------------------------------------------- */
1409
1410static int radio_querycap(struct file *file, void *priv,
1411 struct v4l2_capability *cap)
1412{
1413 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1414
1415 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1416 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1417 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1418
1419 cap->version = EM28XX_VERSION_CODE;
1420 cap->capabilities = V4L2_CAP_TUNER;
1421 return 0;
1422}
1423
1424static int radio_g_tuner(struct file *file, void *priv,
1425 struct v4l2_tuner *t)
1426{
1427 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1428
1429 if (unlikely(t->index > 0))
1430 return -EINVAL;
1431
1432 strcpy(t->name, "Radio");
1433 t->type = V4L2_TUNER_RADIO;
1434
1435 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1436 return 0;
1437}
1438
1439static int radio_enum_input(struct file *file, void *priv,
1440 struct v4l2_input *i)
1441{
1442 if (i->index != 0)
1443 return -EINVAL;
1444 strcpy(i->name, "Radio");
1445 i->type = V4L2_INPUT_TYPE_TUNER;
1446
1447 return 0;
1448}
1449
1450static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1451{
1452 if (unlikely(a->index))
1453 return -EINVAL;
1454
1455 strcpy(a->name, "Radio");
1456 return 0;
1457}
1458
1459static int radio_s_tuner(struct file *file, void *priv,
1460 struct v4l2_tuner *t)
1461{
1462 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1463
1464 if (0 != t->index)
1465 return -EINVAL;
1466
1467 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1468
1469 return 0;
1470}
1471
1472static int radio_s_audio(struct file *file, void *fh,
1473 struct v4l2_audio *a)
1474{
1475 return 0;
1476}
1477
1478static int radio_s_input(struct file *file, void *fh, unsigned int i)
1479{
1480 return 0;
1481}
1482
1483static int radio_queryctrl(struct file *file, void *priv,
1484 struct v4l2_queryctrl *qc)
1485{
1486 int i;
1487
1488 if (qc->id < V4L2_CID_BASE ||
1489 qc->id >= V4L2_CID_LASTP1)
1490 return -EINVAL;
1491
1492 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1493 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1494 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1495 return 0;
1496 }
1497 }
1498
1499 return -EINVAL;
1500}
1501
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001502/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001503 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001504 * inits the device and starts isoc transfer
1505 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001506static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001507{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001508 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001509 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001510 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001511 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001512 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001513
Trent Piephoa991f442007-10-10 05:37:43 -03001514 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001515 if (h->vdev->minor == minor) {
1516 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001517 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001518 }
1519 if (h->vbi_dev->minor == minor) {
1520 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001521 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001522 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001523 if (h->radio_dev &&
1524 h->radio_dev->minor == minor) {
1525 radio = 1;
1526 dev = h;
1527 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001528 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001529 if (NULL == dev)
1530 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001531
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001532 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001533 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001535
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001536 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001537 if (!fh) {
1538 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1539 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001540 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001541 mutex_lock(&dev->lock);
1542 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001543 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001544 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001545 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001546
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001547 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001548 dev->width = norm_maxw(dev);
1549 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001550 dev->hscale = 0;
1551 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001553 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001554 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001555 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001556
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001557 /* Needed, since GPIO might have disabled power of
1558 some i2c device
1559 */
1560 em28xx_config_i2c(dev);
1561
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001562 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001563 if (fh->radio) {
1564 em28xx_videodbg("video_open: setting radio device\n");
1565 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1566 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001567
1568 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001569
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001570 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1571 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1572 sizeof(struct em28xx_buffer), fh);
1573
Ingo Molnar3593cab2006-02-07 06:49:14 -02001574 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001575
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001576 return errCode;
1577}
1578
1579/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001580 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001581 * unregisters the v4l2,i2c and usb devices
1582 * called when the device gets disconected or at module unload
1583*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001584static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001585{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001586
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001587 /*FIXME: I2C IR should be disconnected */
1588
1589 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1590 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1591 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001592 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001593 if (dev->sbutton_input_dev)
1594 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001595 if (dev->radio_dev) {
1596 if (-1 != dev->radio_dev->minor)
1597 video_unregister_device(dev->radio_dev);
1598 else
1599 video_device_release(dev->radio_dev);
1600 dev->radio_dev = NULL;
1601 }
1602 if (dev->vbi_dev) {
1603 if (-1 != dev->vbi_dev->minor)
1604 video_unregister_device(dev->vbi_dev);
1605 else
1606 video_device_release(dev->vbi_dev);
1607 dev->vbi_dev = NULL;
1608 }
1609 if (dev->vdev) {
1610 if (-1 != dev->vdev->minor)
1611 video_unregister_device(dev->vdev);
1612 else
1613 video_device_release(dev->vdev);
1614 dev->vdev = NULL;
1615 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001616 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001617 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001618
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001619 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001620 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001621}
1622
1623/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001624 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001625 * stops streaming and deallocates all resources allocated by the v4l2
1626 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001627 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001628static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001630 struct em28xx_fh *fh = filp->private_data;
1631 struct em28xx *dev = fh->dev;
1632 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001634 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001635
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001636
1637 if (res_check(fh))
1638 res_free(fh);
1639
Ingo Molnar3593cab2006-02-07 06:49:14 -02001640 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001641
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001642 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001643 videobuf_stop(&fh->vb_vidq);
1644 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001646 /* the device is already disconnect,
1647 free the remaining resources */
1648 if (dev->state & DEV_DISCONNECTED) {
1649 em28xx_release_resources(dev);
1650 mutex_unlock(&dev->lock);
1651 kfree(dev);
1652 return 0;
1653 }
1654
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001655 /* do this before setting alternate! */
1656 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001657 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001658
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001659 /* set alternate 0 */
1660 dev->alt = 0;
1661 em28xx_videodbg("setting alternate 0\n");
1662 errCode = usb_set_interface(dev->udev, 0, 0);
1663 if (errCode < 0) {
1664 em28xx_errdev("cannot change alternate number to "
1665 "0 (error=%i)\n", errCode);
1666 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001667 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001668 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 dev->users--;
1670 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001671 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001672 return 0;
1673}
1674
1675/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001676 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677 * will allocate buffers when called for the first time
1678 */
1679static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001680em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001681 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001683 struct em28xx_fh *fh = filp->private_data;
1684 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001685 int rc;
1686
1687 rc = check_dev(dev);
1688 if (rc < 0)
1689 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001691 /* FIXME: read() is not prepared to allow changing the video
1692 resolution while streaming. Seems a bug at em28xx_set_fmt
1693 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001694
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001695 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1696 if (unlikely(res_get(fh)))
1697 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001698
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001699 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1700 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001701 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001702 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001703}
1704
1705/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001706 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001707 * will allocate buffers when called for the first time
1708 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001709static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 struct em28xx_fh *fh = filp->private_data;
1712 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001713 int rc;
1714
1715 rc = check_dev(dev);
1716 if (rc < 0)
1717 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001719 if (unlikely(res_get(fh) < 0))
1720 return POLLERR;
1721
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001722 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1723 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001725 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726}
1727
1728/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001729 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001731static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001732{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001733 struct em28xx_fh *fh = filp->private_data;
1734 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001735 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001736
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001737 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001738 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001739
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001740 rc = check_dev(dev);
1741 if (rc < 0)
1742 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001743
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001744 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001746 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1747 (unsigned long)vma->vm_start,
1748 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1749 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001750
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001751 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001752}
1753
Arjan van de Venfa027c22007-02-12 00:55:33 -08001754static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001755 .owner = THIS_MODULE,
1756 .open = em28xx_v4l2_open,
1757 .release = em28xx_v4l2_close,
1758 .read = em28xx_v4l2_read,
1759 .poll = em28xx_v4l2_poll,
1760 .mmap = em28xx_v4l2_mmap,
1761 .ioctl = video_ioctl2,
1762 .llseek = no_llseek,
1763 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001764};
1765
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001766static const struct file_operations radio_fops = {
1767 .owner = THIS_MODULE,
1768 .open = em28xx_v4l2_open,
1769 .release = em28xx_v4l2_close,
1770 .ioctl = video_ioctl2,
1771 .compat_ioctl = v4l_compat_ioctl32,
1772 .llseek = no_llseek,
1773};
1774
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001775static const struct video_device em28xx_video_template = {
1776 .fops = &em28xx_v4l_fops,
1777 .release = video_device_release,
1778
1779 .minor = -1,
1780 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001781 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1782 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1783 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1784 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001785 .vidioc_g_audio = vidioc_g_audio,
1786 .vidioc_s_audio = vidioc_s_audio,
1787 .vidioc_cropcap = vidioc_cropcap,
1788
Hans Verkuil78b526a2008-05-28 12:16:41 -03001789 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1790 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1791 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001792
1793 .vidioc_reqbufs = vidioc_reqbufs,
1794 .vidioc_querybuf = vidioc_querybuf,
1795 .vidioc_qbuf = vidioc_qbuf,
1796 .vidioc_dqbuf = vidioc_dqbuf,
1797 .vidioc_s_std = vidioc_s_std,
1798 .vidioc_enum_input = vidioc_enum_input,
1799 .vidioc_g_input = vidioc_g_input,
1800 .vidioc_s_input = vidioc_s_input,
1801 .vidioc_queryctrl = vidioc_queryctrl,
1802 .vidioc_g_ctrl = vidioc_g_ctrl,
1803 .vidioc_s_ctrl = vidioc_s_ctrl,
1804 .vidioc_streamon = vidioc_streamon,
1805 .vidioc_streamoff = vidioc_streamoff,
1806 .vidioc_g_tuner = vidioc_g_tuner,
1807 .vidioc_s_tuner = vidioc_s_tuner,
1808 .vidioc_g_frequency = vidioc_g_frequency,
1809 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001810#ifdef CONFIG_VIDEO_ADV_DEBUG
1811 .vidioc_g_register = vidioc_g_register,
1812 .vidioc_s_register = vidioc_s_register,
1813#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001814#ifdef CONFIG_VIDEO_V4L1_COMPAT
1815 .vidiocgmbuf = vidiocgmbuf,
1816#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001817
1818 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001819 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001820};
1821
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001822static struct video_device em28xx_radio_template = {
1823 .name = "em28xx-radio",
1824 .type = VID_TYPE_TUNER,
1825 .fops = &radio_fops,
1826 .minor = -1,
1827 .vidioc_querycap = radio_querycap,
1828 .vidioc_g_tuner = radio_g_tuner,
1829 .vidioc_enum_input = radio_enum_input,
1830 .vidioc_g_audio = radio_g_audio,
1831 .vidioc_s_tuner = radio_s_tuner,
1832 .vidioc_s_audio = radio_s_audio,
1833 .vidioc_s_input = radio_s_input,
1834 .vidioc_queryctrl = radio_queryctrl,
1835 .vidioc_g_ctrl = vidioc_g_ctrl,
1836 .vidioc_s_ctrl = vidioc_s_ctrl,
1837 .vidioc_g_frequency = vidioc_g_frequency,
1838 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001839#ifdef CONFIG_VIDEO_ADV_DEBUG
1840 .vidioc_g_register = vidioc_g_register,
1841 .vidioc_s_register = vidioc_s_register,
1842#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001843};
1844
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001845/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001846
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001847
1848static LIST_HEAD(em28xx_extension_devlist);
1849static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1850
1851int em28xx_register_extension(struct em28xx_ops *ops)
1852{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001853 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001854
1855 mutex_lock(&em28xx_extension_devlist_lock);
1856 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001857 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1858 if (dev)
1859 ops->init(dev);
1860 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001861 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1862 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001863 return 0;
1864}
1865EXPORT_SYMBOL(em28xx_register_extension);
1866
1867void em28xx_unregister_extension(struct em28xx_ops *ops)
1868{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001869 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001870
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001871 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1872 if (dev)
1873 ops->fini(dev);
1874 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001875
1876 mutex_lock(&em28xx_extension_devlist_lock);
1877 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1878 list_del(&ops->next);
1879 mutex_unlock(&em28xx_extension_devlist_lock);
1880}
1881EXPORT_SYMBOL(em28xx_unregister_extension);
1882
Adrian Bunk532fe652008-01-28 22:10:48 -03001883static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1884 const struct video_device *template,
1885 const int type,
1886 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001887{
1888 struct video_device *vfd;
1889
1890 vfd = video_device_alloc();
1891 if (NULL == vfd)
1892 return NULL;
1893 *vfd = *template;
1894 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001895 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001896 vfd->release = video_device_release;
1897 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001898 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001899
1900 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1901 dev->name, type_name);
1902
1903 return vfd;
1904}
1905
1906
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001907/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001908 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001909 * allocates and inits the device structs, registers i2c bus and v4l device
1910 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001911static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001912 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001913{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001914 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001915 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001916 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001917 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001918 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001920 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001921 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001922 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001923 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001924 init_waitqueue_head(&dev->wait_frame);
1925 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001926
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001927 dev->em28xx_write_regs = em28xx_write_regs;
1928 dev->em28xx_read_reg = em28xx_read_reg;
1929 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1930 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1931 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001932 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001933
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001934 em28xx_pre_card_setup(dev);
1935
1936 errCode = em28xx_config(dev);
1937 if (errCode) {
1938 em28xx_errdev("error configuring device\n");
1939 em28xx_devused &= ~(1<<dev->devno);
1940 kfree(dev);
1941 return -ENOMEM;
1942 }
1943
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001944 /* register i2c bus */
1945 em28xx_i2c_register(dev);
1946
1947 /* Do board specific init and eeprom reading */
1948 em28xx_card_setup(dev);
1949
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001950 /* Configure audio */
1951 em28xx_audio_analog_set(dev);
1952
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001953 /* configure the device */
1954 em28xx_config_i2c(dev);
1955
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001956 /* set default norm */
1957 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001958
1959 maxw = norm_maxw(dev);
1960 maxh = norm_maxh(dev);
1961
1962 /* set default image size */
1963 dev->width = maxw;
1964 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001965 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001966 dev->hscale = 0;
1967 dev->vscale = 0;
1968 dev->ctl_input = 2;
1969
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001970 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001971
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001972 list_add_tail(&dev->devlist, &em28xx_devlist);
1973
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001974 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001975 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1976 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001977 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001978 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001979 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001980 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001981 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001982 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001983
1984 /* register v4l2 video video_device */
1985 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1986 video_nr[dev->devno]);
1987 if (retval) {
1988 em28xx_errdev("unable to register video device (error=%i).\n",
1989 retval);
1990 goto fail_unreg;
1991 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001992
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001993 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001994 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1995 VFL_TYPE_VBI, "vbi");
1996 /* register v4l2 vbi video_device */
1997 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1998 vbi_nr[dev->devno]) < 0) {
1999 em28xx_errdev("unable to register vbi device\n");
2000 retval = -ENODEV;
2001 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002002 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002003
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002004 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2005 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2006 VFL_TYPE_RADIO, "radio");
2007 if (NULL == dev->radio_dev) {
2008 em28xx_errdev("cannot allocate video_device.\n");
2009 goto fail_unreg;
2010 }
2011 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2012 radio_nr[dev->devno]);
2013 if (retval < 0) {
2014 em28xx_errdev("can't register radio device\n");
2015 goto fail_unreg;
2016 }
2017 em28xx_info("Registered radio device as /dev/radio%d\n",
2018 dev->radio_dev->minor & 0x1f);
2019 }
2020
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002021 /* init video dma queues */
2022 INIT_LIST_HEAD(&dev->vidq.active);
2023 INIT_LIST_HEAD(&dev->vidq.queued);
2024
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002025
Sascha Sommer5a804152007-11-03 21:22:38 -03002026 if (dev->has_msp34xx) {
2027 /* Send a reset to other chips via gpio */
2028 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2029 msleep(3);
2030 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2031 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002032 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002033
Sascha Sommer5a804152007-11-03 21:22:38 -03002034 video_mux(dev, 0);
2035
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002036 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2037 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2038 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002039
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002040 mutex_lock(&em28xx_extension_devlist_lock);
2041 if (!list_empty(&em28xx_extension_devlist)) {
2042 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2043 if (ops->id)
2044 ops->init(dev);
2045 }
2046 }
2047 mutex_unlock(&em28xx_extension_devlist_lock);
2048
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002049 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002050
2051fail_unreg:
2052 em28xx_release_resources(dev);
2053 mutex_unlock(&dev->lock);
2054 kfree(dev);
2055 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002056}
2057
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002058#if defined(CONFIG_MODULES) && defined(MODULE)
2059static void request_module_async(struct work_struct *work)
2060{
2061 struct em28xx *dev = container_of(work,
2062 struct em28xx, request_module_wk);
2063
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002064 if (dev->has_audio_class)
2065 request_module("snd-usb-audio");
2066 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002067 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002068
2069 if (dev->has_dvb)
2070 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002071}
2072
2073static void request_modules(struct em28xx *dev)
2074{
2075 INIT_WORK(&dev->request_module_wk, request_module_async);
2076 schedule_work(&dev->request_module_wk);
2077}
2078#else
2079#define request_modules(dev)
2080#endif /* CONFIG_MODULES */
2081
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002082/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002083 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002084 * checks for supported devices
2085 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002086static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002087 const struct usb_device_id *id)
2088{
2089 const struct usb_endpoint_descriptor *endpoint;
2090 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002091 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002092 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002094 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095
2096 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002097 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2098
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002099 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002100 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2101 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002102
2103 /* Don't register audio interfaces */
2104 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002105 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002106 udev->descriptor.idVendor,
2107 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002108 ifnum,
2109 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002110
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002111 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002112 return -ENODEV;
2113 }
2114
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002115 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002116 udev->descriptor.idVendor,
2117 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002118 ifnum,
2119 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002120
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002121 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2122
Michael Opdenacker59c51592007-05-09 08:57:56 +02002123 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002124 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2125 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002126 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002127 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128 return -ENODEV;
2129 }
2130 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002131 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002132 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133 return -ENODEV;
2134 }
2135
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002136 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002137 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2138 EM28XX_MAXBOARDS);
2139 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002140 return -ENOMEM;
2141 }
2142
2143 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002144 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002145 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002146 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002147 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002148 return -ENOMEM;
2149 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002150
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002151 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002152 dev->devno = nr;
2153 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002154 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002155
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002156 /* Checks if audio is provided by some interface */
2157 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2158 uif = udev->config->interface[i];
2159 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2160 dev->has_audio_class = 1;
2161 break;
2162 }
2163 }
2164
2165 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2166 dev->has_audio_class ? "Has" : "Doesn't have");
2167
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002168 /* compute alternate max packet sizes */
2169 uif = udev->actconfig->interface[0];
2170
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002171 dev->num_alt = uif->num_altsetting;
2172 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2173/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2174 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2175
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002176 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002177 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002178 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002179 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002180 return -ENOMEM;
2181 }
2182
2183 for (i = 0; i < dev->num_alt ; i++) {
2184 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2185 wMaxPacketSize);
2186 dev->alt_max_pkt_size[i] =
2187 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002188 em28xx_info("Alternate setting %i, max size= %i\n", i,
2189 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002190 }
2191
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002192 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002193 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002194
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002196 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002197 if (retval)
2198 return retval;
2199
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002200 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002201
2202 /* save our data pointer in this interface device */
2203 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002204
2205 request_modules(dev);
2206
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207 return 0;
2208}
2209
2210/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002211 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212 * called when the device gets diconencted
2213 * video device will be unregistered on v4l2_close in case it is still open
2214 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002215static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002216{
Sascha Sommer5a804152007-11-03 21:22:38 -03002217 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002218 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002219
2220 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002221 usb_set_intfdata(interface, NULL);
2222
2223 if (!dev)
2224 return;
2225
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002226 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002227
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002228 /* wait until all current v4l2 io is finished then deallocate
2229 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002230 mutex_lock(&dev->lock);
2231
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232 wake_up_interruptible_all(&dev->open);
2233
2234 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002235 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002236 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002237 "deallocation are deferred on close.\n",
2238 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2239
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002241 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242 dev->state |= DEV_DISCONNECTED;
2243 wake_up_interruptible(&dev->wait_frame);
2244 wake_up_interruptible(&dev->wait_stream);
2245 } else {
2246 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002247 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002248 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002249 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002250
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002251 mutex_lock(&em28xx_extension_devlist_lock);
2252 if (!list_empty(&em28xx_extension_devlist)) {
2253 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2254 ops->fini(dev);
2255 }
2256 }
2257 mutex_unlock(&em28xx_extension_devlist_lock);
2258
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002259 if (!dev->users) {
2260 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002261 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002262 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002263}
2264
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002265static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002266 .name = "em28xx",
2267 .probe = em28xx_usb_probe,
2268 .disconnect = em28xx_usb_disconnect,
2269 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270};
2271
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002272static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002273{
2274 int result;
2275
2276 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002277 (EM28XX_VERSION_CODE >> 16) & 0xff,
2278 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002279#ifdef SNAPSHOT
2280 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2281 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2282#endif
2283
2284 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002285 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002286 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288 " usb_register failed. Error number %d.\n", result);
2289
2290 return result;
2291}
2292
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002293static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002294{
2295 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002296 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297}
2298
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002299module_init(em28xx_module_init);
2300module_exit(em28xx_module_exit);