blob: fb533fda2198308200975716176166aba62b1809 [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 dev->mode = EM28XX_ANALOG_MODE;
395
396 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300397 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300398 f.frequency = dev->ctl_freq;
399
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402 return 0;
403}
404
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300405/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 if (in_interrupt())
412 BUG();
413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
418
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
422 */
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
430}
431
432static int
433buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
435{
436 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
447
Brandon Philips05612972008-04-13 14:57:01 -0300448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 }
457
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300459 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460
461 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
464 em28xx_isoc_copy, EM28XX_ANALOG_CAPTURE);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 goto fail;
467 }
468
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
471
472fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300473 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300474 return rc;
475}
476
477static void
478buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479{
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300482 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
487
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488}
489
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300490static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300495 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300497 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300499 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500}
501
502static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
507};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300509/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800512 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800513 * inits registers with sane defaults
514 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800515static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516{
517
518 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300519 if (!dev->is_em2800)
520 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800521
522 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200523
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300524/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
525/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
526 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200527
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528 dev->mute = 1; /* maybe not the right place... */
529 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300530
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800531 em28xx_outfmt_set_yuv422(dev);
532 em28xx_colorlevels_set_default(dev);
533 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800534
535 return 0;
536}
537
538/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800539 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800540 * configure i2c attached devices
541 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800542static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800543{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300544 struct v4l2_routing route;
545
546 route.input = INPUT(dev->ctl_input)->vmux;
547 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100548 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300549 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300550 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800551}
552
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800553static void video_mux(struct em28xx *dev, int index)
554{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300555 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800556
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300557 route.input = INPUT(index)->vmux;
558 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800559 dev->ctl_input = index;
560 dev->ctl_ainput = INPUT(index)->amux;
561
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300562 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800563
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800564 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300565 if (dev->i2s_speed) {
566 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
567 &dev->i2s_speed);
568 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300569 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300570 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300571 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300572 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
573 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800574 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300575
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300576 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800577}
578
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300579/* Usage lock check functions */
580static int res_get(struct em28xx_fh *fh)
581{
582 struct em28xx *dev = fh->dev;
583 int rc = 0;
584
585 /* This instance already has stream_on */
586 if (fh->stream_on)
587 return rc;
588
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300589 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300590 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300591
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300592 mutex_lock(&dev->lock);
593 dev->stream_on = 1;
594 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300595 mutex_unlock(&dev->lock);
596 return rc;
597}
598
599static int res_check(struct em28xx_fh *fh)
600{
601 return (fh->stream_on);
602}
603
604static void res_free(struct em28xx_fh *fh)
605{
606 struct em28xx *dev = fh->dev;
607
608 mutex_lock(&dev->lock);
609 fh->stream_on = 0;
610 dev->stream_on = 0;
611 mutex_unlock(&dev->lock);
612}
613
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800614/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300615 * em28xx_get_ctrl()
616 * return the current saturation, brightness or contrast, mute state
617 */
618static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
619{
620 switch (ctrl->id) {
621 case V4L2_CID_AUDIO_MUTE:
622 ctrl->value = dev->mute;
623 return 0;
624 case V4L2_CID_AUDIO_VOLUME:
625 ctrl->value = dev->volume;
626 return 0;
627 default:
628 return -EINVAL;
629 }
630}
631
632/*
633 * em28xx_set_ctrl()
634 * mute or set new saturation, brightness or contrast
635 */
636static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
637{
638 switch (ctrl->id) {
639 case V4L2_CID_AUDIO_MUTE:
640 if (ctrl->value != dev->mute) {
641 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300642 return em28xx_audio_analog_set(dev);
643 }
644 return 0;
645 case V4L2_CID_AUDIO_VOLUME:
646 dev->volume = ctrl->value;
647 return em28xx_audio_analog_set(dev);
648 default:
649 return -EINVAL;
650 }
651}
652
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300653static int check_dev(struct em28xx *dev)
654{
655 if (dev->state & DEV_DISCONNECTED) {
656 em28xx_errdev("v4l2 ioctl: device not present\n");
657 return -ENODEV;
658 }
659
660 if (dev->state & DEV_MISCONFIGURED) {
661 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
662 "close and open it again\n");
663 return -EIO;
664 }
665 return 0;
666}
667
668static void get_scale(struct em28xx *dev,
669 unsigned int width, unsigned int height,
670 unsigned int *hscale, unsigned int *vscale)
671{
672 unsigned int maxw = norm_maxw(dev);
673 unsigned int maxh = norm_maxh(dev);
674
675 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
676 if (*hscale >= 0x4000)
677 *hscale = 0x3fff;
678
679 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
680 if (*vscale >= 0x4000)
681 *vscale = 0x3fff;
682}
683
684/* ------------------------------------------------------------------
685 IOCTL vidioc handling
686 ------------------------------------------------------------------*/
687
688static int vidioc_g_fmt_cap(struct file *file, void *priv,
689 struct v4l2_format *f)
690{
691 struct em28xx_fh *fh = priv;
692 struct em28xx *dev = fh->dev;
693
694 mutex_lock(&dev->lock);
695
696 f->fmt.pix.width = dev->width;
697 f->fmt.pix.height = dev->height;
698 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300699 f->fmt.pix.bytesperline = dev->width * 2;
700 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300701 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
702
703 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
704 f->fmt.pix.field = dev->interlaced ?
705 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
706
707 mutex_unlock(&dev->lock);
708 return 0;
709}
710
711static int vidioc_try_fmt_cap(struct file *file, void *priv,
712 struct v4l2_format *f)
713{
714 struct em28xx_fh *fh = priv;
715 struct em28xx *dev = fh->dev;
716 int width = f->fmt.pix.width;
717 int height = f->fmt.pix.height;
718 unsigned int maxw = norm_maxw(dev);
719 unsigned int maxh = norm_maxh(dev);
720 unsigned int hscale, vscale;
721
722 /* width must even because of the YUYV format
723 height must be even because of interlacing */
724 height &= 0xfffe;
725 width &= 0xfffe;
726
727 if (height < 32)
728 height = 32;
729 if (height > maxh)
730 height = maxh;
731 if (width < 48)
732 width = 48;
733 if (width > maxw)
734 width = maxw;
735
736 mutex_lock(&dev->lock);
737
738 if (dev->is_em2800) {
739 /* the em2800 can only scale down to 50% */
740 if (height % (maxh / 2))
741 height = maxh;
742 if (width % (maxw / 2))
743 width = maxw;
744 /* according to empiatech support */
745 /* the MaxPacketSize is to small to support */
746 /* framesizes larger than 640x480 @ 30 fps */
747 /* or 640x576 @ 25 fps. As this would cut */
748 /* of a part of the image we prefer */
749 /* 360x576 or 360x480 for now */
750 if (width == maxw && height == maxh)
751 width /= 2;
752 }
753
754 get_scale(dev, width, height, &hscale, &vscale);
755
756 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
757 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
758
759 f->fmt.pix.width = width;
760 f->fmt.pix.height = height;
761 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
762 f->fmt.pix.bytesperline = width * 2;
763 f->fmt.pix.sizeimage = width * 2 * height;
764 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
765 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
766
767 mutex_unlock(&dev->lock);
768 return 0;
769}
770
771static int vidioc_s_fmt_cap(struct file *file, void *priv,
772 struct v4l2_format *f)
773{
774 struct em28xx_fh *fh = priv;
775 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300776 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300777
778 rc = check_dev(dev);
779 if (rc < 0)
780 return rc;
781
782 vidioc_try_fmt_cap(file, priv, f);
783
784 mutex_lock(&dev->lock);
785
Brandon Philips05612972008-04-13 14:57:01 -0300786 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
787 em28xx_errdev("%s queue busy\n", __func__);
788 rc = -EBUSY;
789 goto out;
790 }
791
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300792 if (dev->stream_on && !fh->stream_on) {
793 em28xx_errdev("%s device in use by another fh\n", __func__);
794 rc = -EBUSY;
795 goto out;
796 }
797
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300798 /* set new image size */
799 dev->width = f->fmt.pix.width;
800 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300801 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
802
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300805
Brandon Philips05612972008-04-13 14:57:01 -0300806 rc = 0;
807
808out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300810 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811}
812
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300813static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300814{
815 struct em28xx_fh *fh = priv;
816 struct em28xx *dev = fh->dev;
817 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818 int rc;
819
820 rc = check_dev(dev);
821 if (rc < 0)
822 return rc;
823
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300824 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300825 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826 mutex_unlock(&dev->lock);
827
828 /* Adjusts width/height, if needed */
829 f.fmt.pix.width = dev->width;
830 f.fmt.pix.height = dev->height;
831 vidioc_try_fmt_cap(file, priv, &f);
832
833 mutex_lock(&dev->lock);
834
835 /* set new image size */
836 dev->width = f.fmt.pix.width;
837 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300838 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
839
840 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300841 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300842
843 mutex_unlock(&dev->lock);
844 return 0;
845}
846
847static const char *iname[] = {
848 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
849 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
850 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
851 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
852 [EM28XX_VMUX_SVIDEO] = "S-Video",
853 [EM28XX_VMUX_TELEVISION] = "Television",
854 [EM28XX_VMUX_CABLE] = "Cable TV",
855 [EM28XX_VMUX_DVB] = "DVB",
856 [EM28XX_VMUX_DEBUG] = "for debug only",
857};
858
859static int vidioc_enum_input(struct file *file, void *priv,
860 struct v4l2_input *i)
861{
862 struct em28xx_fh *fh = priv;
863 struct em28xx *dev = fh->dev;
864 unsigned int n;
865
866 n = i->index;
867 if (n >= MAX_EM28XX_INPUT)
868 return -EINVAL;
869 if (0 == INPUT(n)->type)
870 return -EINVAL;
871
872 i->index = n;
873 i->type = V4L2_INPUT_TYPE_CAMERA;
874
875 strcpy(i->name, iname[INPUT(n)->type]);
876
877 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
878 (EM28XX_VMUX_CABLE == INPUT(n)->type))
879 i->type = V4L2_INPUT_TYPE_TUNER;
880
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300881 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300882
883 return 0;
884}
885
886static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
887{
888 struct em28xx_fh *fh = priv;
889 struct em28xx *dev = fh->dev;
890
891 *i = dev->ctl_input;
892
893 return 0;
894}
895
896static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
897{
898 struct em28xx_fh *fh = priv;
899 struct em28xx *dev = fh->dev;
900 int rc;
901
902 rc = check_dev(dev);
903 if (rc < 0)
904 return rc;
905
906 if (i >= MAX_EM28XX_INPUT)
907 return -EINVAL;
908 if (0 == INPUT(i)->type)
909 return -EINVAL;
910
911 mutex_lock(&dev->lock);
912
913 video_mux(dev, i);
914
915 mutex_unlock(&dev->lock);
916 return 0;
917}
918
919static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
920{
921 struct em28xx_fh *fh = priv;
922 struct em28xx *dev = fh->dev;
923 unsigned int index = a->index;
924
925 if (a->index > 1)
926 return -EINVAL;
927
928 index = dev->ctl_ainput;
929
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300930 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300931 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300932 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300933 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300934
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300935 a->capability = V4L2_AUDCAP_STEREO;
936 a->index = index;
937
938 return 0;
939}
940
941static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
942{
943 struct em28xx_fh *fh = priv;
944 struct em28xx *dev = fh->dev;
945
946 if (a->index != dev->ctl_ainput)
947 return -EINVAL;
948
949 return 0;
950}
951
952static int vidioc_queryctrl(struct file *file, void *priv,
953 struct v4l2_queryctrl *qc)
954{
955 struct em28xx_fh *fh = priv;
956 struct em28xx *dev = fh->dev;
957 int id = qc->id;
958 int i;
959 int rc;
960
961 rc = check_dev(dev);
962 if (rc < 0)
963 return rc;
964
965 memset(qc, 0, sizeof(*qc));
966
967 qc->id = id;
968
969 if (!dev->has_msp34xx) {
970 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
971 if (qc->id && qc->id == em28xx_qctrl[i].id) {
972 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
973 return 0;
974 }
975 }
976 }
977 mutex_lock(&dev->lock);
978 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
979 mutex_unlock(&dev->lock);
980
981 if (qc->type)
982 return 0;
983 else
984 return -EINVAL;
985}
986
987static int vidioc_g_ctrl(struct file *file, void *priv,
988 struct v4l2_control *ctrl)
989{
990 struct em28xx_fh *fh = priv;
991 struct em28xx *dev = fh->dev;
992 int rc;
993
994 rc = check_dev(dev);
995 if (rc < 0)
996 return rc;
997 mutex_lock(&dev->lock);
998
999 if (!dev->has_msp34xx)
1000 rc = em28xx_get_ctrl(dev, ctrl);
1001 else
1002 rc = -EINVAL;
1003
1004 if (rc == -EINVAL) {
1005 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1006 rc = 0;
1007 }
1008
1009 mutex_unlock(&dev->lock);
1010 return rc;
1011}
1012
1013static int vidioc_s_ctrl(struct file *file, void *priv,
1014 struct v4l2_control *ctrl)
1015{
1016 struct em28xx_fh *fh = priv;
1017 struct em28xx *dev = fh->dev;
1018 u8 i;
1019 int rc;
1020
1021 rc = check_dev(dev);
1022 if (rc < 0)
1023 return rc;
1024
1025 mutex_lock(&dev->lock);
1026
1027 if (dev->has_msp34xx)
1028 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1029 else {
1030 rc = 1;
1031 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1032 if (ctrl->id == em28xx_qctrl[i].id) {
1033 if (ctrl->value < em28xx_qctrl[i].minimum ||
1034 ctrl->value > em28xx_qctrl[i].maximum) {
1035 rc = -ERANGE;
1036 break;
1037 }
1038
1039 rc = em28xx_set_ctrl(dev, ctrl);
1040 break;
1041 }
1042 }
1043 }
1044
1045 /* Control not found - try to send it to the attached devices */
1046 if (rc == 1) {
1047 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1048 rc = 0;
1049 }
1050
1051 mutex_unlock(&dev->lock);
1052 return rc;
1053}
1054
1055static int vidioc_g_tuner(struct file *file, void *priv,
1056 struct v4l2_tuner *t)
1057{
1058 struct em28xx_fh *fh = priv;
1059 struct em28xx *dev = fh->dev;
1060 int rc;
1061
1062 rc = check_dev(dev);
1063 if (rc < 0)
1064 return rc;
1065
1066 if (0 != t->index)
1067 return -EINVAL;
1068
1069 strcpy(t->name, "Tuner");
1070
1071 mutex_lock(&dev->lock);
1072
1073 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1074
1075 mutex_unlock(&dev->lock);
1076 return 0;
1077}
1078
1079static int vidioc_s_tuner(struct file *file, void *priv,
1080 struct v4l2_tuner *t)
1081{
1082 struct em28xx_fh *fh = priv;
1083 struct em28xx *dev = fh->dev;
1084 int rc;
1085
1086 rc = check_dev(dev);
1087 if (rc < 0)
1088 return rc;
1089
1090 if (0 != t->index)
1091 return -EINVAL;
1092
1093 mutex_lock(&dev->lock);
1094
1095 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1096
1097 mutex_unlock(&dev->lock);
1098 return 0;
1099}
1100
1101static int vidioc_g_frequency(struct file *file, void *priv,
1102 struct v4l2_frequency *f)
1103{
1104 struct em28xx_fh *fh = priv;
1105 struct em28xx *dev = fh->dev;
1106
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001107 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001108 f->frequency = dev->ctl_freq;
1109
1110 return 0;
1111}
1112
1113static int vidioc_s_frequency(struct file *file, void *priv,
1114 struct v4l2_frequency *f)
1115{
1116 struct em28xx_fh *fh = priv;
1117 struct em28xx *dev = fh->dev;
1118 int rc;
1119
1120 rc = check_dev(dev);
1121 if (rc < 0)
1122 return rc;
1123
1124 if (0 != f->tuner)
1125 return -EINVAL;
1126
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001127 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1128 return -EINVAL;
1129 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001130 return -EINVAL;
1131
1132 mutex_lock(&dev->lock);
1133
1134 dev->ctl_freq = f->frequency;
1135 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1136
1137 mutex_unlock(&dev->lock);
1138 return 0;
1139}
1140
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001141#ifdef CONFIG_VIDEO_ADV_DEBUG
1142static int em28xx_reg_len(int reg)
1143{
1144 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001145 case EM28XX_R40_AC97LSB:
1146 case EM28XX_R30_HSCALELOW:
1147 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001148 return 2;
1149 default:
1150 return 1;
1151 }
1152}
1153
1154static int vidioc_g_register(struct file *file, void *priv,
1155 struct v4l2_register *reg)
1156{
1157 struct em28xx_fh *fh = priv;
1158 struct em28xx *dev = fh->dev;
1159 int ret;
1160
1161 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1162 return -EINVAL;
1163
1164 if (em28xx_reg_len(reg->reg) == 1) {
1165 ret = em28xx_read_reg(dev, reg->reg);
1166 if (ret < 0)
1167 return ret;
1168
1169 reg->val = ret;
1170 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001171 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001172 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1173 reg->reg, (char *)&val, 2);
1174 if (ret < 0)
1175 return ret;
1176
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001177 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001178 }
1179
1180 return 0;
1181}
1182
1183static int vidioc_s_register(struct file *file, void *priv,
1184 struct v4l2_register *reg)
1185{
1186 struct em28xx_fh *fh = priv;
1187 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001188 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001189
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001190 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001191
1192 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1193 em28xx_reg_len(reg->reg));
1194}
1195#endif
1196
1197
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001198static int vidioc_cropcap(struct file *file, void *priv,
1199 struct v4l2_cropcap *cc)
1200{
1201 struct em28xx_fh *fh = priv;
1202 struct em28xx *dev = fh->dev;
1203
1204 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1205 return -EINVAL;
1206
1207 cc->bounds.left = 0;
1208 cc->bounds.top = 0;
1209 cc->bounds.width = dev->width;
1210 cc->bounds.height = dev->height;
1211 cc->defrect = cc->bounds;
1212 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1213 cc->pixelaspect.denominator = 59;
1214
1215 return 0;
1216}
1217
1218static int vidioc_streamon(struct file *file, void *priv,
1219 enum v4l2_buf_type type)
1220{
1221 struct em28xx_fh *fh = priv;
1222 struct em28xx *dev = fh->dev;
1223 int rc;
1224
1225 rc = check_dev(dev);
1226 if (rc < 0)
1227 return rc;
1228
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001229
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001230 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001232
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001233 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001234}
1235
1236static int vidioc_streamoff(struct file *file, void *priv,
1237 enum v4l2_buf_type type)
1238{
1239 struct em28xx_fh *fh = priv;
1240 struct em28xx *dev = fh->dev;
1241 int rc;
1242
1243 rc = check_dev(dev);
1244 if (rc < 0)
1245 return rc;
1246
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001247 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1248 return -EINVAL;
1249 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001250 return -EINVAL;
1251
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001252 videobuf_streamoff(&fh->vb_vidq);
1253 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001254
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001255 return 0;
1256}
1257
1258static int vidioc_querycap(struct file *file, void *priv,
1259 struct v4l2_capability *cap)
1260{
1261 struct em28xx_fh *fh = priv;
1262 struct em28xx *dev = fh->dev;
1263
1264 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1265 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1266 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1267
1268 cap->version = EM28XX_VERSION_CODE;
1269
1270 cap->capabilities =
1271 V4L2_CAP_SLICED_VBI_CAPTURE |
1272 V4L2_CAP_VIDEO_CAPTURE |
1273 V4L2_CAP_AUDIO |
1274 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1275
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001276 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001277 cap->capabilities |= V4L2_CAP_TUNER;
1278
1279 return 0;
1280}
1281
1282static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1283 struct v4l2_fmtdesc *fmtd)
1284{
1285 if (fmtd->index != 0)
1286 return -EINVAL;
1287
1288 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1289 strcpy(fmtd->description, "Packed YUY2");
1290 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1291 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1292
1293 return 0;
1294}
1295
1296/* Sliced VBI ioctls */
1297static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1298 struct v4l2_format *f)
1299{
1300 struct em28xx_fh *fh = priv;
1301 struct em28xx *dev = fh->dev;
1302 int rc;
1303
1304 rc = check_dev(dev);
1305 if (rc < 0)
1306 return rc;
1307
1308 mutex_lock(&dev->lock);
1309
1310 f->fmt.sliced.service_set = 0;
1311
1312 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1313
1314 if (f->fmt.sliced.service_set == 0)
1315 rc = -EINVAL;
1316
1317 mutex_unlock(&dev->lock);
1318 return rc;
1319}
1320
1321static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1322 struct v4l2_format *f)
1323{
1324 struct em28xx_fh *fh = priv;
1325 struct em28xx *dev = fh->dev;
1326 int rc;
1327
1328 rc = check_dev(dev);
1329 if (rc < 0)
1330 return rc;
1331
1332 mutex_lock(&dev->lock);
1333 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1334 mutex_unlock(&dev->lock);
1335
1336 if (f->fmt.sliced.service_set == 0)
1337 return -EINVAL;
1338
1339 return 0;
1340}
1341
1342
1343static int vidioc_reqbufs(struct file *file, void *priv,
1344 struct v4l2_requestbuffers *rb)
1345{
1346 struct em28xx_fh *fh = priv;
1347 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001348 int rc;
1349
1350 rc = check_dev(dev);
1351 if (rc < 0)
1352 return rc;
1353
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001354 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001355}
1356
1357static int vidioc_querybuf(struct file *file, void *priv,
1358 struct v4l2_buffer *b)
1359{
1360 struct em28xx_fh *fh = priv;
1361 struct em28xx *dev = fh->dev;
1362 int rc;
1363
1364 rc = check_dev(dev);
1365 if (rc < 0)
1366 return rc;
1367
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001368 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001369}
1370
1371static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1372{
1373 struct em28xx_fh *fh = priv;
1374 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001375 int rc;
1376
1377 rc = check_dev(dev);
1378 if (rc < 0)
1379 return rc;
1380
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001381 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001382}
1383
1384static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1385{
1386 struct em28xx_fh *fh = priv;
1387 struct em28xx *dev = fh->dev;
1388 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001389
1390 rc = check_dev(dev);
1391 if (rc < 0)
1392 return rc;
1393
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001394 return (videobuf_dqbuf(&fh->vb_vidq, b,
1395 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396}
1397
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001398#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001399static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001400{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001401 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001402
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001403 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001404}
1405#endif
1406
1407
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001408/* ----------------------------------------------------------- */
1409/* RADIO ESPECIFIC IOCTLS */
1410/* ----------------------------------------------------------- */
1411
1412static int radio_querycap(struct file *file, void *priv,
1413 struct v4l2_capability *cap)
1414{
1415 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1416
1417 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1418 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1419 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1420
1421 cap->version = EM28XX_VERSION_CODE;
1422 cap->capabilities = V4L2_CAP_TUNER;
1423 return 0;
1424}
1425
1426static int radio_g_tuner(struct file *file, void *priv,
1427 struct v4l2_tuner *t)
1428{
1429 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1430
1431 if (unlikely(t->index > 0))
1432 return -EINVAL;
1433
1434 strcpy(t->name, "Radio");
1435 t->type = V4L2_TUNER_RADIO;
1436
1437 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1438 return 0;
1439}
1440
1441static int radio_enum_input(struct file *file, void *priv,
1442 struct v4l2_input *i)
1443{
1444 if (i->index != 0)
1445 return -EINVAL;
1446 strcpy(i->name, "Radio");
1447 i->type = V4L2_INPUT_TYPE_TUNER;
1448
1449 return 0;
1450}
1451
1452static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1453{
1454 if (unlikely(a->index))
1455 return -EINVAL;
1456
1457 strcpy(a->name, "Radio");
1458 return 0;
1459}
1460
1461static int radio_s_tuner(struct file *file, void *priv,
1462 struct v4l2_tuner *t)
1463{
1464 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1465
1466 if (0 != t->index)
1467 return -EINVAL;
1468
1469 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1470
1471 return 0;
1472}
1473
1474static int radio_s_audio(struct file *file, void *fh,
1475 struct v4l2_audio *a)
1476{
1477 return 0;
1478}
1479
1480static int radio_s_input(struct file *file, void *fh, unsigned int i)
1481{
1482 return 0;
1483}
1484
1485static int radio_queryctrl(struct file *file, void *priv,
1486 struct v4l2_queryctrl *qc)
1487{
1488 int i;
1489
1490 if (qc->id < V4L2_CID_BASE ||
1491 qc->id >= V4L2_CID_LASTP1)
1492 return -EINVAL;
1493
1494 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1495 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1496 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1497 return 0;
1498 }
1499 }
1500
1501 return -EINVAL;
1502}
1503
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001504/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001505 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001506 * inits the device and starts isoc transfer
1507 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001508static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001509{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001510 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001511 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001512 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001513 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001514 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001515
Trent Piephoa991f442007-10-10 05:37:43 -03001516 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001517 if (h->vdev->minor == minor) {
1518 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001519 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001520 }
1521 if (h->vbi_dev->minor == minor) {
1522 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001523 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001524 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525 if (h->radio_dev &&
1526 h->radio_dev->minor == minor) {
1527 radio = 1;
1528 dev = h;
1529 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001530 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001531 if (NULL == dev)
1532 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001533
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001534 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001535 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001536
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001537 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001538
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001539 if (!fh) {
1540 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1541 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001542 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001543 mutex_lock(&dev->lock);
1544 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001545 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001546 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001547 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001548
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001549 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001550 dev->width = norm_maxw(dev);
1551 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001552 dev->hscale = 0;
1553 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001554
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001555 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001556 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001558 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001559 if (fh->radio) {
1560 em28xx_videodbg("video_open: setting radio device\n");
1561 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1562 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001563
1564 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001565
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001566 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1567 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1568 sizeof(struct em28xx_buffer), fh);
1569
Ingo Molnar3593cab2006-02-07 06:49:14 -02001570 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001571 return errCode;
1572}
1573
1574/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001575 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001576 * unregisters the v4l2,i2c and usb devices
1577 * called when the device gets disconected or at module unload
1578*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001579static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001580{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001581
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001582 /*FIXME: I2C IR should be disconnected */
1583
1584 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1585 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1586 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001587 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001588 if (dev->radio_dev) {
1589 if (-1 != dev->radio_dev->minor)
1590 video_unregister_device(dev->radio_dev);
1591 else
1592 video_device_release(dev->radio_dev);
1593 dev->radio_dev = NULL;
1594 }
1595 if (dev->vbi_dev) {
1596 if (-1 != dev->vbi_dev->minor)
1597 video_unregister_device(dev->vbi_dev);
1598 else
1599 video_device_release(dev->vbi_dev);
1600 dev->vbi_dev = NULL;
1601 }
1602 if (dev->vdev) {
1603 if (-1 != dev->vdev->minor)
1604 video_unregister_device(dev->vdev);
1605 else
1606 video_device_release(dev->vdev);
1607 dev->vdev = NULL;
1608 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001609 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001611
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001612 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001613 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001614}
1615
1616/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001617 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001618 * stops streaming and deallocates all resources allocated by the v4l2
1619 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001620 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001621static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001623 struct em28xx_fh *fh = filp->private_data;
1624 struct em28xx *dev = fh->dev;
1625 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001626
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001627 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001628
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001629
1630 if (res_check(fh))
1631 res_free(fh);
1632
Ingo Molnar3593cab2006-02-07 06:49:14 -02001633 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001634
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001635 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001636 videobuf_stop(&fh->vb_vidq);
1637 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001638
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001639 /* the device is already disconnect,
1640 free the remaining resources */
1641 if (dev->state & DEV_DISCONNECTED) {
1642 em28xx_release_resources(dev);
1643 mutex_unlock(&dev->lock);
1644 kfree(dev);
1645 return 0;
1646 }
1647
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001648 /* do this before setting alternate! */
1649 em28xx_uninit_isoc(dev);
1650
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001651 /* set alternate 0 */
1652 dev->alt = 0;
1653 em28xx_videodbg("setting alternate 0\n");
1654 errCode = usb_set_interface(dev->udev, 0, 0);
1655 if (errCode < 0) {
1656 em28xx_errdev("cannot change alternate number to "
1657 "0 (error=%i)\n", errCode);
1658 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001659 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001660 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661 dev->users--;
1662 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001663 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664 return 0;
1665}
1666
1667/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001668 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 * will allocate buffers when called for the first time
1670 */
1671static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001672em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001673 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001674{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001675 struct em28xx_fh *fh = filp->private_data;
1676 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001677 int rc;
1678
1679 rc = check_dev(dev);
1680 if (rc < 0)
1681 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001683 /* FIXME: read() is not prepared to allow changing the video
1684 resolution while streaming. Seems a bug at em28xx_set_fmt
1685 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001686
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001687 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1688 if (unlikely(res_get(fh)))
1689 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001690
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001691 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1692 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001693 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001694 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695}
1696
1697/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001698 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001699 * will allocate buffers when called for the first time
1700 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001701static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001703 struct em28xx_fh *fh = filp->private_data;
1704 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001705 int rc;
1706
1707 rc = check_dev(dev);
1708 if (rc < 0)
1709 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001710
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001711 if (unlikely(res_get(fh) < 0))
1712 return POLLERR;
1713
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001714 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1715 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001716
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001717 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718}
1719
1720/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001721 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001723static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001725 struct em28xx_fh *fh = filp->private_data;
1726 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001727 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001728
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001729 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001730 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001731
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001732 rc = check_dev(dev);
1733 if (rc < 0)
1734 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001735
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001736 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001738 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1739 (unsigned long)vma->vm_start,
1740 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1741 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001743 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001744}
1745
Arjan van de Venfa027c22007-02-12 00:55:33 -08001746static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001747 .owner = THIS_MODULE,
1748 .open = em28xx_v4l2_open,
1749 .release = em28xx_v4l2_close,
1750 .read = em28xx_v4l2_read,
1751 .poll = em28xx_v4l2_poll,
1752 .mmap = em28xx_v4l2_mmap,
1753 .ioctl = video_ioctl2,
1754 .llseek = no_llseek,
1755 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001756};
1757
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001758static const struct file_operations radio_fops = {
1759 .owner = THIS_MODULE,
1760 .open = em28xx_v4l2_open,
1761 .release = em28xx_v4l2_close,
1762 .ioctl = video_ioctl2,
1763 .compat_ioctl = v4l_compat_ioctl32,
1764 .llseek = no_llseek,
1765};
1766
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001767static const struct video_device em28xx_video_template = {
1768 .fops = &em28xx_v4l_fops,
1769 .release = video_device_release,
1770
1771 .minor = -1,
1772 .vidioc_querycap = vidioc_querycap,
1773 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1774 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1775 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1776 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1777 .vidioc_g_audio = vidioc_g_audio,
1778 .vidioc_s_audio = vidioc_s_audio,
1779 .vidioc_cropcap = vidioc_cropcap,
1780
1781 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1782 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1783 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1784
1785 .vidioc_reqbufs = vidioc_reqbufs,
1786 .vidioc_querybuf = vidioc_querybuf,
1787 .vidioc_qbuf = vidioc_qbuf,
1788 .vidioc_dqbuf = vidioc_dqbuf,
1789 .vidioc_s_std = vidioc_s_std,
1790 .vidioc_enum_input = vidioc_enum_input,
1791 .vidioc_g_input = vidioc_g_input,
1792 .vidioc_s_input = vidioc_s_input,
1793 .vidioc_queryctrl = vidioc_queryctrl,
1794 .vidioc_g_ctrl = vidioc_g_ctrl,
1795 .vidioc_s_ctrl = vidioc_s_ctrl,
1796 .vidioc_streamon = vidioc_streamon,
1797 .vidioc_streamoff = vidioc_streamoff,
1798 .vidioc_g_tuner = vidioc_g_tuner,
1799 .vidioc_s_tuner = vidioc_s_tuner,
1800 .vidioc_g_frequency = vidioc_g_frequency,
1801 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001802#ifdef CONFIG_VIDEO_ADV_DEBUG
1803 .vidioc_g_register = vidioc_g_register,
1804 .vidioc_s_register = vidioc_s_register,
1805#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001806#ifdef CONFIG_VIDEO_V4L1_COMPAT
1807 .vidiocgmbuf = vidiocgmbuf,
1808#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001809
1810 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001811 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001812};
1813
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001814static struct video_device em28xx_radio_template = {
1815 .name = "em28xx-radio",
1816 .type = VID_TYPE_TUNER,
1817 .fops = &radio_fops,
1818 .minor = -1,
1819 .vidioc_querycap = radio_querycap,
1820 .vidioc_g_tuner = radio_g_tuner,
1821 .vidioc_enum_input = radio_enum_input,
1822 .vidioc_g_audio = radio_g_audio,
1823 .vidioc_s_tuner = radio_s_tuner,
1824 .vidioc_s_audio = radio_s_audio,
1825 .vidioc_s_input = radio_s_input,
1826 .vidioc_queryctrl = radio_queryctrl,
1827 .vidioc_g_ctrl = vidioc_g_ctrl,
1828 .vidioc_s_ctrl = vidioc_s_ctrl,
1829 .vidioc_g_frequency = vidioc_g_frequency,
1830 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001831#ifdef CONFIG_VIDEO_ADV_DEBUG
1832 .vidioc_g_register = vidioc_g_register,
1833 .vidioc_s_register = vidioc_s_register,
1834#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001835};
1836
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001837/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001838
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001839
1840static LIST_HEAD(em28xx_extension_devlist);
1841static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1842
1843int em28xx_register_extension(struct em28xx_ops *ops)
1844{
1845 struct em28xx *h, *dev = NULL;
1846
1847 list_for_each_entry(h, &em28xx_devlist, devlist)
1848 dev = h;
1849
1850 mutex_lock(&em28xx_extension_devlist_lock);
1851 list_add_tail(&ops->next, &em28xx_extension_devlist);
1852 if (dev)
1853 ops->init(dev);
1854
1855 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1856 mutex_unlock(&em28xx_extension_devlist_lock);
1857
1858 return 0;
1859}
1860EXPORT_SYMBOL(em28xx_register_extension);
1861
1862void em28xx_unregister_extension(struct em28xx_ops *ops)
1863{
1864 struct em28xx *h, *dev = NULL;
1865
1866 list_for_each_entry(h, &em28xx_devlist, devlist)
1867 dev = h;
1868
1869 if (dev)
1870 ops->fini(dev);
1871
1872 mutex_lock(&em28xx_extension_devlist_lock);
1873 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1874 list_del(&ops->next);
1875 mutex_unlock(&em28xx_extension_devlist_lock);
1876}
1877EXPORT_SYMBOL(em28xx_unregister_extension);
1878
Adrian Bunk532fe652008-01-28 22:10:48 -03001879static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1880 const struct video_device *template,
1881 const int type,
1882 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001883{
1884 struct video_device *vfd;
1885
1886 vfd = video_device_alloc();
1887 if (NULL == vfd)
1888 return NULL;
1889 *vfd = *template;
1890 vfd->minor = -1;
1891 vfd->dev = &dev->udev->dev;
1892 vfd->release = video_device_release;
1893 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001894 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001895
1896 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1897 dev->name, type_name);
1898
1899 return vfd;
1900}
1901
1902
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001903/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001904 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001905 * allocates and inits the device structs, registers i2c bus and v4l device
1906 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001907static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001908 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001909{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001910 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001911 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001912 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001913 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001914 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001915
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001916 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001917 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001918 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001920 init_waitqueue_head(&dev->wait_frame);
1921 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001922
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001923 dev->em28xx_write_regs = em28xx_write_regs;
1924 dev->em28xx_read_reg = em28xx_read_reg;
1925 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1926 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1927 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001928 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001929
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001930 em28xx_pre_card_setup(dev);
1931
1932 errCode = em28xx_config(dev);
1933 if (errCode) {
1934 em28xx_errdev("error configuring device\n");
1935 em28xx_devused &= ~(1<<dev->devno);
1936 kfree(dev);
1937 return -ENOMEM;
1938 }
1939
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001940 /* register i2c bus */
1941 em28xx_i2c_register(dev);
1942
1943 /* Do board specific init and eeprom reading */
1944 em28xx_card_setup(dev);
1945
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001946 /* Configure audio */
1947 em28xx_audio_analog_set(dev);
1948
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001949 /* configure the device */
1950 em28xx_config_i2c(dev);
1951
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001952 /* set default norm */
1953 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001954
1955 maxw = norm_maxw(dev);
1956 maxh = norm_maxh(dev);
1957
1958 /* set default image size */
1959 dev->width = maxw;
1960 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001961 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001962 dev->hscale = 0;
1963 dev->vscale = 0;
1964 dev->ctl_input = 2;
1965
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001966 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001967
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001968 list_add_tail(&dev->devlist, &em28xx_devlist);
1969
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001970 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001971 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1972 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001973 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001974 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001975 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001976 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001977 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001978 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001979
1980 /* register v4l2 video video_device */
1981 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1982 video_nr[dev->devno]);
1983 if (retval) {
1984 em28xx_errdev("unable to register video device (error=%i).\n",
1985 retval);
1986 goto fail_unreg;
1987 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001988
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001989 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001990 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1991 VFL_TYPE_VBI, "vbi");
1992 /* register v4l2 vbi video_device */
1993 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1994 vbi_nr[dev->devno]) < 0) {
1995 em28xx_errdev("unable to register vbi device\n");
1996 retval = -ENODEV;
1997 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001998 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001999
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002000 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2001 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2002 VFL_TYPE_RADIO, "radio");
2003 if (NULL == dev->radio_dev) {
2004 em28xx_errdev("cannot allocate video_device.\n");
2005 goto fail_unreg;
2006 }
2007 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2008 radio_nr[dev->devno]);
2009 if (retval < 0) {
2010 em28xx_errdev("can't register radio device\n");
2011 goto fail_unreg;
2012 }
2013 em28xx_info("Registered radio device as /dev/radio%d\n",
2014 dev->radio_dev->minor & 0x1f);
2015 }
2016
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002017 /* init video dma queues */
2018 INIT_LIST_HEAD(&dev->vidq.active);
2019 INIT_LIST_HEAD(&dev->vidq.queued);
2020
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002021
Sascha Sommer5a804152007-11-03 21:22:38 -03002022 if (dev->has_msp34xx) {
2023 /* Send a reset to other chips via gpio */
2024 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2025 msleep(3);
2026 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2027 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002028 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002029
Sascha Sommer5a804152007-11-03 21:22:38 -03002030 video_mux(dev, 0);
2031
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002032 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2033 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2034 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002035
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002036 mutex_lock(&em28xx_extension_devlist_lock);
2037 if (!list_empty(&em28xx_extension_devlist)) {
2038 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2039 if (ops->id)
2040 ops->init(dev);
2041 }
2042 }
2043 mutex_unlock(&em28xx_extension_devlist_lock);
2044
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002045 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002046
2047fail_unreg:
2048 em28xx_release_resources(dev);
2049 mutex_unlock(&dev->lock);
2050 kfree(dev);
2051 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002052}
2053
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002054#if defined(CONFIG_MODULES) && defined(MODULE)
2055static void request_module_async(struct work_struct *work)
2056{
2057 struct em28xx *dev = container_of(work,
2058 struct em28xx, request_module_wk);
2059
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002060 if (dev->has_audio_class)
2061 request_module("snd-usb-audio");
2062 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002063 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002064
2065 if (dev->has_dvb)
2066 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002067}
2068
2069static void request_modules(struct em28xx *dev)
2070{
2071 INIT_WORK(&dev->request_module_wk, request_module_async);
2072 schedule_work(&dev->request_module_wk);
2073}
2074#else
2075#define request_modules(dev)
2076#endif /* CONFIG_MODULES */
2077
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002078/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002079 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002080 * checks for supported devices
2081 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002082static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002083 const struct usb_device_id *id)
2084{
2085 const struct usb_endpoint_descriptor *endpoint;
2086 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002087 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002088 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002089 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002090 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002091
2092 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002093 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2094
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002095 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002096 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2097 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002098
2099 /* Don't register audio interfaces */
2100 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002101 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002102 udev->descriptor.idVendor,
2103 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002104 ifnum,
2105 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002106
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002107 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002108 return -ENODEV;
2109 }
2110
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002111 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002112 udev->descriptor.idVendor,
2113 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002114 ifnum,
2115 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002116
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002117 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2118
Michael Opdenacker59c51592007-05-09 08:57:56 +02002119 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002120 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2121 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002122 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002123 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002124 return -ENODEV;
2125 }
2126 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002127 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002128 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002129 return -ENODEV;
2130 }
2131
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002132 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002133 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2134 EM28XX_MAXBOARDS);
2135 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002136 return -ENOMEM;
2137 }
2138
2139 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002140 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002141 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002142 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002143 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002144 return -ENOMEM;
2145 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002146
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002147 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002148 dev->devno = nr;
2149 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002150 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002151
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002152 /* Checks if audio is provided by some interface */
2153 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2154 uif = udev->config->interface[i];
2155 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2156 dev->has_audio_class = 1;
2157 break;
2158 }
2159 }
2160
2161 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2162 dev->has_audio_class ? "Has" : "Doesn't have");
2163
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002164 /* compute alternate max packet sizes */
2165 uif = udev->actconfig->interface[0];
2166
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002167 dev->num_alt = uif->num_altsetting;
2168 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2169/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2170 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2171
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002172 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002173 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002174 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002175 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002176 return -ENOMEM;
2177 }
2178
2179 for (i = 0; i < dev->num_alt ; i++) {
2180 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2181 wMaxPacketSize);
2182 dev->alt_max_pkt_size[i] =
2183 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002184 em28xx_info("Alternate setting %i, max size= %i\n", i,
2185 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002186 }
2187
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002188 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002189 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002190
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002191 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002192 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002193 if (retval)
2194 return retval;
2195
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002196 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002197
2198 /* save our data pointer in this interface device */
2199 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002200
2201 request_modules(dev);
2202
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002203 return 0;
2204}
2205
2206/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002207 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002208 * called when the device gets diconencted
2209 * video device will be unregistered on v4l2_close in case it is still open
2210 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002211static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212{
Sascha Sommer5a804152007-11-03 21:22:38 -03002213 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002214 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002215
2216 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002217 usb_set_intfdata(interface, NULL);
2218
2219 if (!dev)
2220 return;
2221
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002222 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002223
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002224 /* wait until all current v4l2 io is finished then deallocate
2225 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002226 mutex_lock(&dev->lock);
2227
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002228 wake_up_interruptible_all(&dev->open);
2229
2230 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002231 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002233 "deallocation are deferred on close.\n",
2234 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2235
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002236 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002237 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002238 dev->state |= DEV_DISCONNECTED;
2239 wake_up_interruptible(&dev->wait_frame);
2240 wake_up_interruptible(&dev->wait_stream);
2241 } else {
2242 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002243 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002244 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002245 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002247 mutex_lock(&em28xx_extension_devlist_lock);
2248 if (!list_empty(&em28xx_extension_devlist)) {
2249 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2250 ops->fini(dev);
2251 }
2252 }
2253 mutex_unlock(&em28xx_extension_devlist_lock);
2254
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002255 if (!dev->users) {
2256 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002257 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002258 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002259}
2260
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002261static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002262 .name = "em28xx",
2263 .probe = em28xx_usb_probe,
2264 .disconnect = em28xx_usb_disconnect,
2265 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002266};
2267
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002268static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002269{
2270 int result;
2271
2272 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002273 (EM28XX_VERSION_CODE >> 16) & 0xff,
2274 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002275#ifdef SNAPSHOT
2276 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2277 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2278#endif
2279
2280 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002281 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002282 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002283 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002284 " usb_register failed. Error number %d.\n", result);
2285
2286 return result;
2287}
2288
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002289static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002290{
2291 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002292 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293}
2294
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002295module_init(em28xx_module_init);
2296module_exit(em28xx_module_exit);