blob: 1a0b09714af5659473933770bf9d5139718d3030 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020039#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030040#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030041#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030055 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030057static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030058module_param(isoc_debug, int, 0644);
59MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030060
61#define em28xx_isocdbg(fmt, arg...) do {\
62 if (isoc_debug) \
63 printk(KERN_INFO "%s %s :"fmt, \
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030064 dev->name, __func__ , ##arg); } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030065
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080066MODULE_AUTHOR(DRIVER_AUTHOR);
67MODULE_DESCRIPTION(DRIVER_DESC);
68MODULE_LICENSE("GPL");
69
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080070static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080071
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080072static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020073static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030074static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
75static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
76
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080077module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078module_param_array(video_nr, int, NULL, 0444);
79module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030080module_param_array(radio_nr, int, NULL, 0444);
81MODULE_PARM_DESC(card, "card type");
82MODULE_PARM_DESC(video_nr, "video device numbers");
83MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
84MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080085
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030086static unsigned int video_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080087module_param(video_debug,int,0644);
88MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
89
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020090/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
91static unsigned long em28xx_devused;
92
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080093/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020094/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080095static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080096 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020097 .id = V4L2_CID_AUDIO_VOLUME,
98 .type = V4L2_CTRL_TYPE_INTEGER,
99 .name = "Volume",
100 .minimum = 0x0,
101 .maximum = 0x1f,
102 .step = 0x1,
103 .default_value = 0x1f,
104 .flags = 0,
105 },{
106 .id = V4L2_CID_AUDIO_MUTE,
107 .type = V4L2_CTRL_TYPE_BOOLEAN,
108 .name = "Mute",
109 .minimum = 0,
110 .maximum = 1,
111 .step = 1,
112 .default_value = 1,
113 .flags = 0,
114 }
115};
116
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800117static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800118
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300119/* ------------------------------------------------------------------
120 DMA and thread functions
121 ------------------------------------------------------------------*/
122
123/*
124 * Announces that a buffer were filled and request the next
125 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300126static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300127 struct em28xx_dmaqueue *dma_q,
128 struct em28xx_buffer *buf)
129{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300130 /* Advice that buffer was filled */
131 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
132 buf->vb.state = VIDEOBUF_DONE;
133 buf->vb.field_count++;
134 do_gettimeofday(&buf->vb.ts);
135
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300136 dev->isoc_ctl.buf = NULL;
137
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300138 list_del(&buf->vb.queue);
139 wake_up(&buf->vb.done);
140}
141
142/*
143 * Identify the buffer header type and properly handles
144 */
145static void em28xx_copy_video(struct em28xx *dev,
146 struct em28xx_dmaqueue *dma_q,
147 struct em28xx_buffer *buf,
148 unsigned char *p,
149 unsigned char *outp, unsigned long len)
150{
151 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300152 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300153 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300154
155 if (dma_q->pos + len > buf->vb.size)
156 len = buf->vb.size - dma_q->pos;
157
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300158 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159 em28xx_isocdbg("frame is not complete\n");
160 len += 4;
161 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300162 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163
164 startread = p;
165 remain = len;
166
167 /* Interlaces frame */
168 if (buf->top_field)
169 fieldstart = outp;
170 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300171 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300172
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300173 linesdone = dma_q->pos / bytesperline;
174 currlinedone = dma_q->pos % bytesperline;
175 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300176 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300177 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178 lencopy = lencopy > remain ? remain : lencopy;
179
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300180 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300181 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300182 ((char *)startwrite + lencopy) -
183 ((char *)outp + buf->vb.size));
184 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300185 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300186 if (lencopy <= 0)
187 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300188 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300189
190 remain -= lencopy;
191
192 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300193 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300194 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300195 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300196 lencopy = remain;
197 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300198 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300199
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300200 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300201 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300202 ((char *)startwrite + lencopy) -
203 ((char *)outp + buf->vb.size));
204 lencopy = remain = (char *)outp + buf->vb.size -
205 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300206 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 if (lencopy <= 0)
208 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300209
210 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300211
212 remain -= lencopy;
213 }
214
215 dma_q->pos += len;
216}
217
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300218static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300219 int packet, int status)
220{
221 char *errmsg = "Unknown";
222
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300223 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 case -ENOENT:
225 errmsg = "unlinked synchronuously";
226 break;
227 case -ECONNRESET:
228 errmsg = "unlinked asynchronuously";
229 break;
230 case -ENOSR:
231 errmsg = "Buffer error (overrun)";
232 break;
233 case -EPIPE:
234 errmsg = "Stalled (device not responding)";
235 break;
236 case -EOVERFLOW:
237 errmsg = "Babble (bad cable?)";
238 break;
239 case -EPROTO:
240 errmsg = "Bit-stuff error (bad cable?)";
241 break;
242 case -EILSEQ:
243 errmsg = "CRC/Timeout (could be anything)";
244 break;
245 case -ETIME:
246 errmsg = "Device does not respond";
247 break;
248 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300249 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300250 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
251 } else {
252 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
253 packet, status, errmsg);
254 }
255}
256
257/*
258 * video-buf generic routine to get the next available buffer
259 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300260static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300261 struct em28xx_buffer **buf)
262{
263 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300264 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300265
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300266 if (list_empty(&dma_q->active)) {
267 em28xx_isocdbg("No active queue to serve\n");
268 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300269 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300270 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300271 }
272
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300273 /* Get the next buffer */
274 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
275
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 /* Cleans up buffer - Usefull for testing for frame/URB loss */
277 outp = videobuf_to_vmalloc(&(*buf)->vb);
278 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300279
280 dev->isoc_ctl.buf = *buf;
281
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300282 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300283}
284
285/*
286 * Controls the isoc copy of each urb packet
287 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300288static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300289{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300290 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300291 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300292 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300293 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300294 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295
296 if (!dev)
297 return 0;
298
299 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
300 return 0;
301
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300302 if (urb->status < 0) {
303 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300304 if (urb->status == -ENOENT)
305 return 0;
306 }
307
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300308 buf = dev->isoc_ctl.buf;
309 if (buf != NULL)
310 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300311
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300312 for (i = 0; i < urb->number_of_packets; i++) {
313 int status = urb->iso_frame_desc[i].status;
314
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300315 if (status < 0) {
316 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300317 if (urb->iso_frame_desc[i].status != -EPROTO)
318 continue;
319 }
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322
323 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300324 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300325 continue;
326 }
327 if (urb->iso_frame_desc[i].actual_length >
328 dev->max_pkt_size) {
329 em28xx_isocdbg("packet bigger than packet size");
330 continue;
331 }
332
333 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300334
335 /* FIXME: incomplete buffer checks where removed to make
336 logic simpler. Impacts of those changes should be evaluated
337 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300338 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
339 em28xx_isocdbg("VBI HEADER!!!\n");
340 /* FIXME: Should add vbi copy */
341 continue;
342 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300343 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300344 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
345 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300346
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300347 if (!(p[2] & 1)) {
348 if (buf != NULL)
349 buffer_filled(dev, dma_q, buf);
350 get_next_buf(dma_q, &buf);
351 if (buf == NULL)
352 outp = NULL;
353 else
354 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300355 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300356
357 if (buf != NULL) {
358 if (p[2] & 1)
359 buf->top_field = 0;
360 else
361 buf->top_field = 1;
362 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300363
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300366 if (buf != NULL)
367 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368 }
369 return rc;
370}
371
372/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300373 Videobuf operations
374 ------------------------------------------------------------------*/
375
376static int
377buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
378{
379 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300380 struct em28xx *dev = fh->dev;
381 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300382
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300383 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300384 if (0 == *count)
385 *count = EM28XX_DEF_BUF;
386
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300387 if (*count < EM28XX_MIN_BUF)
388 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300390 dev->mode = EM28XX_ANALOG_MODE;
391
392 /* Ask tuner to go to analog mode */
393 memset (&f, 0, sizeof(f));
394 f.frequency = dev->ctl_freq;
395
396 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
397
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300398 return 0;
399}
400
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300401/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
403{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300404 struct em28xx_fh *fh = vq->priv_data;
405 struct em28xx *dev = fh->dev;
406 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300407 if (in_interrupt())
408 BUG();
409
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300410 /* We used to wait for the buffer to finish here, but this didn't work
411 because, as we were keeping the state as VIDEOBUF_QUEUED,
412 videobuf_queue_cancel marked it as finished for us.
413 (Also, it could wedge forever if the hardware was misconfigured.)
414
415 This should be safe; by the time we get here, the buffer isn't
416 queued anymore. If we ever start marking the buffers as
417 VIDEOBUF_ACTIVE, it won't be, though.
418 */
419 spin_lock_irqsave(&dev->slock, flags);
420 if (dev->isoc_ctl.buf == buf)
421 dev->isoc_ctl.buf = NULL;
422 spin_unlock_irqrestore(&dev->slock, flags);
423
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424 videobuf_vmalloc_free(&buf->vb);
425 buf->vb.state = VIDEOBUF_NEEDS_INIT;
426}
427
428static int
429buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
430 enum v4l2_field field)
431{
432 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300433 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300434 struct em28xx *dev = fh->dev;
435 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437 /* FIXME: It assumes depth = 16 */
438 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300439 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440
441 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
442 return -EINVAL;
443
Brandon Philips05612972008-04-13 14:57:01 -0300444 buf->vb.width = dev->width;
445 buf->vb.height = dev->height;
446 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447
448 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300449 rc = videobuf_iolock(vq, &buf->vb, NULL);
450 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452 }
453
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300455 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456
457 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300458 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
459 EM28XX_NUM_BUFS, dev->max_pkt_size,
460 em28xx_isoc_copy, EM28XX_ANALOG_CAPTURE);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300461 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300462 goto fail;
463 }
464
465 buf->vb.state = VIDEOBUF_PREPARED;
466 return 0;
467
468fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300469 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300470 return rc;
471}
472
473static void
474buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
475{
476 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
477 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300478 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300480
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300481 buf->vb.state = VIDEOBUF_QUEUED;
482 list_add_tail(&buf->vb.queue, &vidq->active);
483
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484}
485
486static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
487{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300488 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300490 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300492 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495}
496
497static struct videobuf_queue_ops em28xx_video_qops = {
498 .buf_setup = buffer_setup,
499 .buf_prepare = buffer_prepare,
500 .buf_queue = buffer_queue,
501 .buf_release = buffer_release,
502};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800503
504/********************* v4l2 interface ******************************************/
505
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800506/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800507 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508 * inits registers with sane defaults
509 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800510static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511{
512
513 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300514 if (!dev->is_em2800)
515 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516
517 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200518
Markus Rechberger9475fb12006-02-27 00:07:34 -0300519/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
520/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200521 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
522
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800523 dev->mute = 1; /* maybe not the right place... */
524 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300525
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800526 em28xx_outfmt_set_yuv422(dev);
527 em28xx_colorlevels_set_default(dev);
528 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800529
530 return 0;
531}
532
533/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800534 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800535 * configure i2c attached devices
536 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800537static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800538{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300539 struct v4l2_routing route;
540
541 route.input = INPUT(dev->ctl_input)->vmux;
542 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100543 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300544 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300545 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800546}
547
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800548static void video_mux(struct em28xx *dev, int index)
549{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300550 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800551
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300552 route.input = INPUT(index)->vmux;
553 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800554 dev->ctl_input = index;
555 dev->ctl_ainput = INPUT(index)->amux;
556
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800558
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800559 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200560 if (dev->i2s_speed)
561 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300562 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300563 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300564 /* Note: this is msp3400 specific */
565 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800566 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300567
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300568 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800569}
570
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300571/* Usage lock check functions */
572static int res_get(struct em28xx_fh *fh)
573{
574 struct em28xx *dev = fh->dev;
575 int rc = 0;
576
577 /* This instance already has stream_on */
578 if (fh->stream_on)
579 return rc;
580
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300581 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300582 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300583
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300584 mutex_lock(&dev->lock);
585 dev->stream_on = 1;
586 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300587 mutex_unlock(&dev->lock);
588 return rc;
589}
590
591static int res_check(struct em28xx_fh *fh)
592{
593 return (fh->stream_on);
594}
595
596static void res_free(struct em28xx_fh *fh)
597{
598 struct em28xx *dev = fh->dev;
599
600 mutex_lock(&dev->lock);
601 fh->stream_on = 0;
602 dev->stream_on = 0;
603 mutex_unlock(&dev->lock);
604}
605
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800606/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300607 * em28xx_get_ctrl()
608 * return the current saturation, brightness or contrast, mute state
609 */
610static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
611{
612 switch (ctrl->id) {
613 case V4L2_CID_AUDIO_MUTE:
614 ctrl->value = dev->mute;
615 return 0;
616 case V4L2_CID_AUDIO_VOLUME:
617 ctrl->value = dev->volume;
618 return 0;
619 default:
620 return -EINVAL;
621 }
622}
623
624/*
625 * em28xx_set_ctrl()
626 * mute or set new saturation, brightness or contrast
627 */
628static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
629{
630 switch (ctrl->id) {
631 case V4L2_CID_AUDIO_MUTE:
632 if (ctrl->value != dev->mute) {
633 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300634 return em28xx_audio_analog_set(dev);
635 }
636 return 0;
637 case V4L2_CID_AUDIO_VOLUME:
638 dev->volume = ctrl->value;
639 return em28xx_audio_analog_set(dev);
640 default:
641 return -EINVAL;
642 }
643}
644
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300645static int check_dev(struct em28xx *dev)
646{
647 if (dev->state & DEV_DISCONNECTED) {
648 em28xx_errdev("v4l2 ioctl: device not present\n");
649 return -ENODEV;
650 }
651
652 if (dev->state & DEV_MISCONFIGURED) {
653 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
654 "close and open it again\n");
655 return -EIO;
656 }
657 return 0;
658}
659
660static void get_scale(struct em28xx *dev,
661 unsigned int width, unsigned int height,
662 unsigned int *hscale, unsigned int *vscale)
663{
664 unsigned int maxw = norm_maxw(dev);
665 unsigned int maxh = norm_maxh(dev);
666
667 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
668 if (*hscale >= 0x4000)
669 *hscale = 0x3fff;
670
671 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
672 if (*vscale >= 0x4000)
673 *vscale = 0x3fff;
674}
675
676/* ------------------------------------------------------------------
677 IOCTL vidioc handling
678 ------------------------------------------------------------------*/
679
680static int vidioc_g_fmt_cap(struct file *file, void *priv,
681 struct v4l2_format *f)
682{
683 struct em28xx_fh *fh = priv;
684 struct em28xx *dev = fh->dev;
685
686 mutex_lock(&dev->lock);
687
688 f->fmt.pix.width = dev->width;
689 f->fmt.pix.height = dev->height;
690 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300691 f->fmt.pix.bytesperline = dev->width * 2;
692 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300693 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
694
695 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
696 f->fmt.pix.field = dev->interlaced ?
697 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
698
699 mutex_unlock(&dev->lock);
700 return 0;
701}
702
703static int vidioc_try_fmt_cap(struct file *file, void *priv,
704 struct v4l2_format *f)
705{
706 struct em28xx_fh *fh = priv;
707 struct em28xx *dev = fh->dev;
708 int width = f->fmt.pix.width;
709 int height = f->fmt.pix.height;
710 unsigned int maxw = norm_maxw(dev);
711 unsigned int maxh = norm_maxh(dev);
712 unsigned int hscale, vscale;
713
714 /* width must even because of the YUYV format
715 height must be even because of interlacing */
716 height &= 0xfffe;
717 width &= 0xfffe;
718
719 if (height < 32)
720 height = 32;
721 if (height > maxh)
722 height = maxh;
723 if (width < 48)
724 width = 48;
725 if (width > maxw)
726 width = maxw;
727
728 mutex_lock(&dev->lock);
729
730 if (dev->is_em2800) {
731 /* the em2800 can only scale down to 50% */
732 if (height % (maxh / 2))
733 height = maxh;
734 if (width % (maxw / 2))
735 width = maxw;
736 /* according to empiatech support */
737 /* the MaxPacketSize is to small to support */
738 /* framesizes larger than 640x480 @ 30 fps */
739 /* or 640x576 @ 25 fps. As this would cut */
740 /* of a part of the image we prefer */
741 /* 360x576 or 360x480 for now */
742 if (width == maxw && height == maxh)
743 width /= 2;
744 }
745
746 get_scale(dev, width, height, &hscale, &vscale);
747
748 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
749 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
750
751 f->fmt.pix.width = width;
752 f->fmt.pix.height = height;
753 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
754 f->fmt.pix.bytesperline = width * 2;
755 f->fmt.pix.sizeimage = width * 2 * height;
756 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
757 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
758
759 mutex_unlock(&dev->lock);
760 return 0;
761}
762
763static int vidioc_s_fmt_cap(struct file *file, void *priv,
764 struct v4l2_format *f)
765{
766 struct em28xx_fh *fh = priv;
767 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300768 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300769
770 rc = check_dev(dev);
771 if (rc < 0)
772 return rc;
773
774 vidioc_try_fmt_cap(file, priv, f);
775
776 mutex_lock(&dev->lock);
777
Brandon Philips05612972008-04-13 14:57:01 -0300778 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
779 em28xx_errdev("%s queue busy\n", __func__);
780 rc = -EBUSY;
781 goto out;
782 }
783
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300784 if (dev->stream_on && !fh->stream_on) {
785 em28xx_errdev("%s device in use by another fh\n", __func__);
786 rc = -EBUSY;
787 goto out;
788 }
789
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300790 /* set new image size */
791 dev->width = f->fmt.pix.width;
792 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300793 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
794
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300795 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300796 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300797
Brandon Philips05612972008-04-13 14:57:01 -0300798 rc = 0;
799
800out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300801 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300802 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803}
804
805static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
806{
807 struct em28xx_fh *fh = priv;
808 struct em28xx *dev = fh->dev;
809 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810 int rc;
811
812 rc = check_dev(dev);
813 if (rc < 0)
814 return rc;
815
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300816 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300817 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300818 mutex_unlock(&dev->lock);
819
820 /* Adjusts width/height, if needed */
821 f.fmt.pix.width = dev->width;
822 f.fmt.pix.height = dev->height;
823 vidioc_try_fmt_cap(file, priv, &f);
824
825 mutex_lock(&dev->lock);
826
827 /* set new image size */
828 dev->width = f.fmt.pix.width;
829 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300830 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
831
832 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300833 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300834
835 mutex_unlock(&dev->lock);
836 return 0;
837}
838
839static const char *iname[] = {
840 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
841 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
842 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
843 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
844 [EM28XX_VMUX_SVIDEO] = "S-Video",
845 [EM28XX_VMUX_TELEVISION] = "Television",
846 [EM28XX_VMUX_CABLE] = "Cable TV",
847 [EM28XX_VMUX_DVB] = "DVB",
848 [EM28XX_VMUX_DEBUG] = "for debug only",
849};
850
851static int vidioc_enum_input(struct file *file, void *priv,
852 struct v4l2_input *i)
853{
854 struct em28xx_fh *fh = priv;
855 struct em28xx *dev = fh->dev;
856 unsigned int n;
857
858 n = i->index;
859 if (n >= MAX_EM28XX_INPUT)
860 return -EINVAL;
861 if (0 == INPUT(n)->type)
862 return -EINVAL;
863
864 i->index = n;
865 i->type = V4L2_INPUT_TYPE_CAMERA;
866
867 strcpy(i->name, iname[INPUT(n)->type]);
868
869 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
870 (EM28XX_VMUX_CABLE == INPUT(n)->type))
871 i->type = V4L2_INPUT_TYPE_TUNER;
872
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300873 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300874
875 return 0;
876}
877
878static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
879{
880 struct em28xx_fh *fh = priv;
881 struct em28xx *dev = fh->dev;
882
883 *i = dev->ctl_input;
884
885 return 0;
886}
887
888static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
889{
890 struct em28xx_fh *fh = priv;
891 struct em28xx *dev = fh->dev;
892 int rc;
893
894 rc = check_dev(dev);
895 if (rc < 0)
896 return rc;
897
898 if (i >= MAX_EM28XX_INPUT)
899 return -EINVAL;
900 if (0 == INPUT(i)->type)
901 return -EINVAL;
902
903 mutex_lock(&dev->lock);
904
905 video_mux(dev, i);
906
907 mutex_unlock(&dev->lock);
908 return 0;
909}
910
911static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
912{
913 struct em28xx_fh *fh = priv;
914 struct em28xx *dev = fh->dev;
915 unsigned int index = a->index;
916
917 if (a->index > 1)
918 return -EINVAL;
919
920 index = dev->ctl_ainput;
921
922 if (index == 0) {
923 strcpy(a->name, "Television");
924 } else {
925 strcpy(a->name, "Line In");
926 }
927 a->capability = V4L2_AUDCAP_STEREO;
928 a->index = index;
929
930 return 0;
931}
932
933static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
934{
935 struct em28xx_fh *fh = priv;
936 struct em28xx *dev = fh->dev;
937
938 if (a->index != dev->ctl_ainput)
939 return -EINVAL;
940
941 return 0;
942}
943
944static int vidioc_queryctrl(struct file *file, void *priv,
945 struct v4l2_queryctrl *qc)
946{
947 struct em28xx_fh *fh = priv;
948 struct em28xx *dev = fh->dev;
949 int id = qc->id;
950 int i;
951 int rc;
952
953 rc = check_dev(dev);
954 if (rc < 0)
955 return rc;
956
957 memset(qc, 0, sizeof(*qc));
958
959 qc->id = id;
960
961 if (!dev->has_msp34xx) {
962 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
963 if (qc->id && qc->id == em28xx_qctrl[i].id) {
964 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
965 return 0;
966 }
967 }
968 }
969 mutex_lock(&dev->lock);
970 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
971 mutex_unlock(&dev->lock);
972
973 if (qc->type)
974 return 0;
975 else
976 return -EINVAL;
977}
978
979static int vidioc_g_ctrl(struct file *file, void *priv,
980 struct v4l2_control *ctrl)
981{
982 struct em28xx_fh *fh = priv;
983 struct em28xx *dev = fh->dev;
984 int rc;
985
986 rc = check_dev(dev);
987 if (rc < 0)
988 return rc;
989 mutex_lock(&dev->lock);
990
991 if (!dev->has_msp34xx)
992 rc = em28xx_get_ctrl(dev, ctrl);
993 else
994 rc = -EINVAL;
995
996 if (rc == -EINVAL) {
997 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
998 rc = 0;
999 }
1000
1001 mutex_unlock(&dev->lock);
1002 return rc;
1003}
1004
1005static int vidioc_s_ctrl(struct file *file, void *priv,
1006 struct v4l2_control *ctrl)
1007{
1008 struct em28xx_fh *fh = priv;
1009 struct em28xx *dev = fh->dev;
1010 u8 i;
1011 int rc;
1012
1013 rc = check_dev(dev);
1014 if (rc < 0)
1015 return rc;
1016
1017 mutex_lock(&dev->lock);
1018
1019 if (dev->has_msp34xx)
1020 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1021 else {
1022 rc = 1;
1023 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1024 if (ctrl->id == em28xx_qctrl[i].id) {
1025 if (ctrl->value < em28xx_qctrl[i].minimum ||
1026 ctrl->value > em28xx_qctrl[i].maximum) {
1027 rc = -ERANGE;
1028 break;
1029 }
1030
1031 rc = em28xx_set_ctrl(dev, ctrl);
1032 break;
1033 }
1034 }
1035 }
1036
1037 /* Control not found - try to send it to the attached devices */
1038 if (rc == 1) {
1039 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1040 rc = 0;
1041 }
1042
1043 mutex_unlock(&dev->lock);
1044 return rc;
1045}
1046
1047static int vidioc_g_tuner(struct file *file, void *priv,
1048 struct v4l2_tuner *t)
1049{
1050 struct em28xx_fh *fh = priv;
1051 struct em28xx *dev = fh->dev;
1052 int rc;
1053
1054 rc = check_dev(dev);
1055 if (rc < 0)
1056 return rc;
1057
1058 if (0 != t->index)
1059 return -EINVAL;
1060
1061 strcpy(t->name, "Tuner");
1062
1063 mutex_lock(&dev->lock);
1064
1065 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1066
1067 mutex_unlock(&dev->lock);
1068 return 0;
1069}
1070
1071static int vidioc_s_tuner(struct file *file, void *priv,
1072 struct v4l2_tuner *t)
1073{
1074 struct em28xx_fh *fh = priv;
1075 struct em28xx *dev = fh->dev;
1076 int rc;
1077
1078 rc = check_dev(dev);
1079 if (rc < 0)
1080 return rc;
1081
1082 if (0 != t->index)
1083 return -EINVAL;
1084
1085 mutex_lock(&dev->lock);
1086
1087 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1088
1089 mutex_unlock(&dev->lock);
1090 return 0;
1091}
1092
1093static int vidioc_g_frequency(struct file *file, void *priv,
1094 struct v4l2_frequency *f)
1095{
1096 struct em28xx_fh *fh = priv;
1097 struct em28xx *dev = fh->dev;
1098
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001099 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001100 f->frequency = dev->ctl_freq;
1101
1102 return 0;
1103}
1104
1105static int vidioc_s_frequency(struct file *file, void *priv,
1106 struct v4l2_frequency *f)
1107{
1108 struct em28xx_fh *fh = priv;
1109 struct em28xx *dev = fh->dev;
1110 int rc;
1111
1112 rc = check_dev(dev);
1113 if (rc < 0)
1114 return rc;
1115
1116 if (0 != f->tuner)
1117 return -EINVAL;
1118
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001119 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1120 return -EINVAL;
1121 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001122 return -EINVAL;
1123
1124 mutex_lock(&dev->lock);
1125
1126 dev->ctl_freq = f->frequency;
1127 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1128
1129 mutex_unlock(&dev->lock);
1130 return 0;
1131}
1132
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001133#ifdef CONFIG_VIDEO_ADV_DEBUG
1134static int em28xx_reg_len(int reg)
1135{
1136 switch (reg) {
1137 case AC97LSB_REG:
1138 case HSCALELOW_REG:
1139 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001140 return 2;
1141 default:
1142 return 1;
1143 }
1144}
1145
1146static int vidioc_g_register(struct file *file, void *priv,
1147 struct v4l2_register *reg)
1148{
1149 struct em28xx_fh *fh = priv;
1150 struct em28xx *dev = fh->dev;
1151 int ret;
1152
1153 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1154 return -EINVAL;
1155
1156 if (em28xx_reg_len(reg->reg) == 1) {
1157 ret = em28xx_read_reg(dev, reg->reg);
1158 if (ret < 0)
1159 return ret;
1160
1161 reg->val = ret;
1162 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001163 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001164 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1165 reg->reg, (char *)&val, 2);
1166 if (ret < 0)
1167 return ret;
1168
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001169 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001170 }
1171
1172 return 0;
1173}
1174
1175static int vidioc_s_register(struct file *file, void *priv,
1176 struct v4l2_register *reg)
1177{
1178 struct em28xx_fh *fh = priv;
1179 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001180 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001181
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001182 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001183
1184 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1185 em28xx_reg_len(reg->reg));
1186}
1187#endif
1188
1189
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001190static int vidioc_cropcap(struct file *file, void *priv,
1191 struct v4l2_cropcap *cc)
1192{
1193 struct em28xx_fh *fh = priv;
1194 struct em28xx *dev = fh->dev;
1195
1196 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1197 return -EINVAL;
1198
1199 cc->bounds.left = 0;
1200 cc->bounds.top = 0;
1201 cc->bounds.width = dev->width;
1202 cc->bounds.height = dev->height;
1203 cc->defrect = cc->bounds;
1204 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1205 cc->pixelaspect.denominator = 59;
1206
1207 return 0;
1208}
1209
1210static int vidioc_streamon(struct file *file, void *priv,
1211 enum v4l2_buf_type type)
1212{
1213 struct em28xx_fh *fh = priv;
1214 struct em28xx *dev = fh->dev;
1215 int rc;
1216
1217 rc = check_dev(dev);
1218 if (rc < 0)
1219 return rc;
1220
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001221
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001222 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001223 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001224
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001225 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001226}
1227
1228static int vidioc_streamoff(struct file *file, void *priv,
1229 enum v4l2_buf_type type)
1230{
1231 struct em28xx_fh *fh = priv;
1232 struct em28xx *dev = fh->dev;
1233 int rc;
1234
1235 rc = check_dev(dev);
1236 if (rc < 0)
1237 return rc;
1238
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001239 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1240 return -EINVAL;
1241 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001242 return -EINVAL;
1243
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001244 videobuf_streamoff(&fh->vb_vidq);
1245 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001246
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001247 return 0;
1248}
1249
1250static int vidioc_querycap(struct file *file, void *priv,
1251 struct v4l2_capability *cap)
1252{
1253 struct em28xx_fh *fh = priv;
1254 struct em28xx *dev = fh->dev;
1255
1256 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1257 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1258 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1259
1260 cap->version = EM28XX_VERSION_CODE;
1261
1262 cap->capabilities =
1263 V4L2_CAP_SLICED_VBI_CAPTURE |
1264 V4L2_CAP_VIDEO_CAPTURE |
1265 V4L2_CAP_AUDIO |
1266 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1267
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001268 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001269 cap->capabilities |= V4L2_CAP_TUNER;
1270
1271 return 0;
1272}
1273
1274static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1275 struct v4l2_fmtdesc *fmtd)
1276{
1277 if (fmtd->index != 0)
1278 return -EINVAL;
1279
1280 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1281 strcpy(fmtd->description, "Packed YUY2");
1282 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1283 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1284
1285 return 0;
1286}
1287
1288/* Sliced VBI ioctls */
1289static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1290 struct v4l2_format *f)
1291{
1292 struct em28xx_fh *fh = priv;
1293 struct em28xx *dev = fh->dev;
1294 int rc;
1295
1296 rc = check_dev(dev);
1297 if (rc < 0)
1298 return rc;
1299
1300 mutex_lock(&dev->lock);
1301
1302 f->fmt.sliced.service_set = 0;
1303
1304 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1305
1306 if (f->fmt.sliced.service_set == 0)
1307 rc = -EINVAL;
1308
1309 mutex_unlock(&dev->lock);
1310 return rc;
1311}
1312
1313static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1314 struct v4l2_format *f)
1315{
1316 struct em28xx_fh *fh = priv;
1317 struct em28xx *dev = fh->dev;
1318 int rc;
1319
1320 rc = check_dev(dev);
1321 if (rc < 0)
1322 return rc;
1323
1324 mutex_lock(&dev->lock);
1325 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1326 mutex_unlock(&dev->lock);
1327
1328 if (f->fmt.sliced.service_set == 0)
1329 return -EINVAL;
1330
1331 return 0;
1332}
1333
1334
1335static int vidioc_reqbufs(struct file *file, void *priv,
1336 struct v4l2_requestbuffers *rb)
1337{
1338 struct em28xx_fh *fh = priv;
1339 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001340 int rc;
1341
1342 rc = check_dev(dev);
1343 if (rc < 0)
1344 return rc;
1345
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001346 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347}
1348
1349static int vidioc_querybuf(struct file *file, void *priv,
1350 struct v4l2_buffer *b)
1351{
1352 struct em28xx_fh *fh = priv;
1353 struct em28xx *dev = fh->dev;
1354 int rc;
1355
1356 rc = check_dev(dev);
1357 if (rc < 0)
1358 return rc;
1359
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001360 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001361}
1362
1363static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1364{
1365 struct em28xx_fh *fh = priv;
1366 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001367 int rc;
1368
1369 rc = check_dev(dev);
1370 if (rc < 0)
1371 return rc;
1372
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001373 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374}
1375
1376static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1377{
1378 struct em28xx_fh *fh = priv;
1379 struct em28xx *dev = fh->dev;
1380 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381
1382 rc = check_dev(dev);
1383 if (rc < 0)
1384 return rc;
1385
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001386 return (videobuf_dqbuf(&fh->vb_vidq, b,
1387 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001388}
1389
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001390#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001391static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001392{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001393 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001394
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001395 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001396}
1397#endif
1398
1399
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001400/* ----------------------------------------------------------- */
1401/* RADIO ESPECIFIC IOCTLS */
1402/* ----------------------------------------------------------- */
1403
1404static int radio_querycap(struct file *file, void *priv,
1405 struct v4l2_capability *cap)
1406{
1407 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1408
1409 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1410 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1411 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1412
1413 cap->version = EM28XX_VERSION_CODE;
1414 cap->capabilities = V4L2_CAP_TUNER;
1415 return 0;
1416}
1417
1418static int radio_g_tuner(struct file *file, void *priv,
1419 struct v4l2_tuner *t)
1420{
1421 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1422
1423 if (unlikely(t->index > 0))
1424 return -EINVAL;
1425
1426 strcpy(t->name, "Radio");
1427 t->type = V4L2_TUNER_RADIO;
1428
1429 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1430 return 0;
1431}
1432
1433static int radio_enum_input(struct file *file, void *priv,
1434 struct v4l2_input *i)
1435{
1436 if (i->index != 0)
1437 return -EINVAL;
1438 strcpy(i->name, "Radio");
1439 i->type = V4L2_INPUT_TYPE_TUNER;
1440
1441 return 0;
1442}
1443
1444static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1445{
1446 if (unlikely(a->index))
1447 return -EINVAL;
1448
1449 strcpy(a->name, "Radio");
1450 return 0;
1451}
1452
1453static int radio_s_tuner(struct file *file, void *priv,
1454 struct v4l2_tuner *t)
1455{
1456 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1457
1458 if (0 != t->index)
1459 return -EINVAL;
1460
1461 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1462
1463 return 0;
1464}
1465
1466static int radio_s_audio(struct file *file, void *fh,
1467 struct v4l2_audio *a)
1468{
1469 return 0;
1470}
1471
1472static int radio_s_input(struct file *file, void *fh, unsigned int i)
1473{
1474 return 0;
1475}
1476
1477static int radio_queryctrl(struct file *file, void *priv,
1478 struct v4l2_queryctrl *qc)
1479{
1480 int i;
1481
1482 if (qc->id < V4L2_CID_BASE ||
1483 qc->id >= V4L2_CID_LASTP1)
1484 return -EINVAL;
1485
1486 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1487 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1488 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1489 return 0;
1490 }
1491 }
1492
1493 return -EINVAL;
1494}
1495
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001496/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001497 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001498 * inits the device and starts isoc transfer
1499 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001500static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001501{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001502 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001503 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001504 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001505 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001506 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001507
Trent Piephoa991f442007-10-10 05:37:43 -03001508 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001509 if (h->vdev->minor == minor) {
1510 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001511 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001512 }
1513 if (h->vbi_dev->minor == minor) {
1514 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001515 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001516 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001517 if (h->radio_dev &&
1518 h->radio_dev->minor == minor) {
1519 radio = 1;
1520 dev = h;
1521 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001522 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001523 if (NULL == dev)
1524 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001525
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001526 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001527 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001528
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001529 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001530
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001531 if (!fh) {
1532 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1533 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001535 mutex_lock(&dev->lock);
1536 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001537 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001538 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001539 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001540
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001541 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001542 dev->width = norm_maxw(dev);
1543 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001544 dev->hscale = 0;
1545 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001546
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001547 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001548 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001549
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001550 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001551 if (fh->radio) {
1552 em28xx_videodbg("video_open: setting radio device\n");
1553 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1554 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001555
1556 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001558 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1559 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1560 sizeof(struct em28xx_buffer), fh);
1561
Ingo Molnar3593cab2006-02-07 06:49:14 -02001562 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001563 return errCode;
1564}
1565
1566/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001567 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001568 * unregisters the v4l2,i2c and usb devices
1569 * called when the device gets disconected or at module unload
1570*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001571static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001572{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001573
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001574 /*FIXME: I2C IR should be disconnected */
1575
1576 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1577 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1578 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001579 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001580 if (dev->radio_dev) {
1581 if (-1 != dev->radio_dev->minor)
1582 video_unregister_device(dev->radio_dev);
1583 else
1584 video_device_release(dev->radio_dev);
1585 dev->radio_dev = NULL;
1586 }
1587 if (dev->vbi_dev) {
1588 if (-1 != dev->vbi_dev->minor)
1589 video_unregister_device(dev->vbi_dev);
1590 else
1591 video_device_release(dev->vbi_dev);
1592 dev->vbi_dev = NULL;
1593 }
1594 if (dev->vdev) {
1595 if (-1 != dev->vdev->minor)
1596 video_unregister_device(dev->vdev);
1597 else
1598 video_device_release(dev->vdev);
1599 dev->vdev = NULL;
1600 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001601 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001602 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001603
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001604 /* Mark device as unused */
1605 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001606}
1607
1608/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001609 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1611 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001612static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001614 struct em28xx_fh *fh = filp->private_data;
1615 struct em28xx *dev = fh->dev;
1616 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001617
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001618 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001619
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001620
1621 if (res_check(fh))
1622 res_free(fh);
1623
Ingo Molnar3593cab2006-02-07 06:49:14 -02001624 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001625
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001626 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001627 videobuf_stop(&fh->vb_vidq);
1628 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001630 /* the device is already disconnect,
1631 free the remaining resources */
1632 if (dev->state & DEV_DISCONNECTED) {
1633 em28xx_release_resources(dev);
1634 mutex_unlock(&dev->lock);
1635 kfree(dev);
1636 return 0;
1637 }
1638
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001639 /* do this before setting alternate! */
1640 em28xx_uninit_isoc(dev);
1641
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001642 /* set alternate 0 */
1643 dev->alt = 0;
1644 em28xx_videodbg("setting alternate 0\n");
1645 errCode = usb_set_interface(dev->udev, 0, 0);
1646 if (errCode < 0) {
1647 em28xx_errdev("cannot change alternate number to "
1648 "0 (error=%i)\n", errCode);
1649 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001650 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001651 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652 dev->users--;
1653 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001654 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001655 return 0;
1656}
1657
1658/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001659 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660 * will allocate buffers when called for the first time
1661 */
1662static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001663em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001664 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001665{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001666 struct em28xx_fh *fh = filp->private_data;
1667 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001668 int rc;
1669
1670 rc = check_dev(dev);
1671 if (rc < 0)
1672 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001674 /* FIXME: read() is not prepared to allow changing the video
1675 resolution while streaming. Seems a bug at em28xx_set_fmt
1676 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001677
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001678 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1679 if (unlikely(res_get(fh)))
1680 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001681
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001682 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1683 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001684 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001685 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001686}
1687
1688/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001689 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690 * will allocate buffers when called for the first time
1691 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001692static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001694 struct em28xx_fh *fh = filp->private_data;
1695 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001696 int rc;
1697
1698 rc = check_dev(dev);
1699 if (rc < 0)
1700 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001701
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001702 if (unlikely(res_get(fh) < 0))
1703 return POLLERR;
1704
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001705 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1706 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001707
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001708 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001709}
1710
1711/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001712 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001714static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001715{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001716 struct em28xx_fh *fh = filp->private_data;
1717 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001718 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001719
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001720 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001721 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001722
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001723 rc = check_dev(dev);
1724 if (rc < 0)
1725 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001726
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001727 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001728
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001729 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1730 (unsigned long)vma->vm_start,
1731 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1732 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001733
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001734 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001735}
1736
Arjan van de Venfa027c22007-02-12 00:55:33 -08001737static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001738 .owner = THIS_MODULE,
1739 .open = em28xx_v4l2_open,
1740 .release = em28xx_v4l2_close,
1741 .read = em28xx_v4l2_read,
1742 .poll = em28xx_v4l2_poll,
1743 .mmap = em28xx_v4l2_mmap,
1744 .ioctl = video_ioctl2,
1745 .llseek = no_llseek,
1746 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747};
1748
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001749static const struct file_operations radio_fops = {
1750 .owner = THIS_MODULE,
1751 .open = em28xx_v4l2_open,
1752 .release = em28xx_v4l2_close,
1753 .ioctl = video_ioctl2,
1754 .compat_ioctl = v4l_compat_ioctl32,
1755 .llseek = no_llseek,
1756};
1757
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001758static const struct video_device em28xx_video_template = {
1759 .fops = &em28xx_v4l_fops,
1760 .release = video_device_release,
1761
1762 .minor = -1,
1763 .vidioc_querycap = vidioc_querycap,
1764 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1765 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1766 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1767 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1768 .vidioc_g_audio = vidioc_g_audio,
1769 .vidioc_s_audio = vidioc_s_audio,
1770 .vidioc_cropcap = vidioc_cropcap,
1771
1772 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1773 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1774 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1775
1776 .vidioc_reqbufs = vidioc_reqbufs,
1777 .vidioc_querybuf = vidioc_querybuf,
1778 .vidioc_qbuf = vidioc_qbuf,
1779 .vidioc_dqbuf = vidioc_dqbuf,
1780 .vidioc_s_std = vidioc_s_std,
1781 .vidioc_enum_input = vidioc_enum_input,
1782 .vidioc_g_input = vidioc_g_input,
1783 .vidioc_s_input = vidioc_s_input,
1784 .vidioc_queryctrl = vidioc_queryctrl,
1785 .vidioc_g_ctrl = vidioc_g_ctrl,
1786 .vidioc_s_ctrl = vidioc_s_ctrl,
1787 .vidioc_streamon = vidioc_streamon,
1788 .vidioc_streamoff = vidioc_streamoff,
1789 .vidioc_g_tuner = vidioc_g_tuner,
1790 .vidioc_s_tuner = vidioc_s_tuner,
1791 .vidioc_g_frequency = vidioc_g_frequency,
1792 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001793#ifdef CONFIG_VIDEO_ADV_DEBUG
1794 .vidioc_g_register = vidioc_g_register,
1795 .vidioc_s_register = vidioc_s_register,
1796#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001797#ifdef CONFIG_VIDEO_V4L1_COMPAT
1798 .vidiocgmbuf = vidiocgmbuf,
1799#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001800
1801 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001802 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001803};
1804
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001805static struct video_device em28xx_radio_template = {
1806 .name = "em28xx-radio",
1807 .type = VID_TYPE_TUNER,
1808 .fops = &radio_fops,
1809 .minor = -1,
1810 .vidioc_querycap = radio_querycap,
1811 .vidioc_g_tuner = radio_g_tuner,
1812 .vidioc_enum_input = radio_enum_input,
1813 .vidioc_g_audio = radio_g_audio,
1814 .vidioc_s_tuner = radio_s_tuner,
1815 .vidioc_s_audio = radio_s_audio,
1816 .vidioc_s_input = radio_s_input,
1817 .vidioc_queryctrl = radio_queryctrl,
1818 .vidioc_g_ctrl = vidioc_g_ctrl,
1819 .vidioc_s_ctrl = vidioc_s_ctrl,
1820 .vidioc_g_frequency = vidioc_g_frequency,
1821 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001822#ifdef CONFIG_VIDEO_ADV_DEBUG
1823 .vidioc_g_register = vidioc_g_register,
1824 .vidioc_s_register = vidioc_s_register,
1825#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001826};
1827
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828/******************************** usb interface *****************************************/
1829
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001830
1831static LIST_HEAD(em28xx_extension_devlist);
1832static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1833
1834int em28xx_register_extension(struct em28xx_ops *ops)
1835{
1836 struct em28xx *h, *dev = NULL;
1837
1838 list_for_each_entry(h, &em28xx_devlist, devlist)
1839 dev = h;
1840
1841 mutex_lock(&em28xx_extension_devlist_lock);
1842 list_add_tail(&ops->next, &em28xx_extension_devlist);
1843 if (dev)
1844 ops->init(dev);
1845
1846 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1847 mutex_unlock(&em28xx_extension_devlist_lock);
1848
1849 return 0;
1850}
1851EXPORT_SYMBOL(em28xx_register_extension);
1852
1853void em28xx_unregister_extension(struct em28xx_ops *ops)
1854{
1855 struct em28xx *h, *dev = NULL;
1856
1857 list_for_each_entry(h, &em28xx_devlist, devlist)
1858 dev = h;
1859
1860 if (dev)
1861 ops->fini(dev);
1862
1863 mutex_lock(&em28xx_extension_devlist_lock);
1864 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1865 list_del(&ops->next);
1866 mutex_unlock(&em28xx_extension_devlist_lock);
1867}
1868EXPORT_SYMBOL(em28xx_unregister_extension);
1869
Adrian Bunk532fe652008-01-28 22:10:48 -03001870static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1871 const struct video_device *template,
1872 const int type,
1873 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001874{
1875 struct video_device *vfd;
1876
1877 vfd = video_device_alloc();
1878 if (NULL == vfd)
1879 return NULL;
1880 *vfd = *template;
1881 vfd->minor = -1;
1882 vfd->dev = &dev->udev->dev;
1883 vfd->release = video_device_release;
1884 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001885 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001886
1887 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1888 dev->name, type_name);
1889
1890 return vfd;
1891}
1892
1893
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001895 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001896 * allocates and inits the device structs, registers i2c bus and v4l device
1897 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001898static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001899 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001900{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001901 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001902 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001903 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001904 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001905 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001906
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001907 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001908 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001909 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001911 init_waitqueue_head(&dev->wait_frame);
1912 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001913
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001914 dev->em28xx_write_regs = em28xx_write_regs;
1915 dev->em28xx_read_reg = em28xx_read_reg;
1916 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1917 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1918 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001919 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001920
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03001921 errCode = em28xx_read_reg(dev, CHIPID_REG);
1922 if (errCode >= 0)
1923 em28xx_info("em28xx chip ID = %d\n", errCode);
1924
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001925 em28xx_pre_card_setup(dev);
1926
1927 errCode = em28xx_config(dev);
1928 if (errCode) {
1929 em28xx_errdev("error configuring device\n");
1930 em28xx_devused &= ~(1<<dev->devno);
1931 kfree(dev);
1932 return -ENOMEM;
1933 }
1934
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001935 /* register i2c bus */
1936 em28xx_i2c_register(dev);
1937
1938 /* Do board specific init and eeprom reading */
1939 em28xx_card_setup(dev);
1940
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001941 /* Configure audio */
1942 em28xx_audio_analog_set(dev);
1943
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001944 /* configure the device */
1945 em28xx_config_i2c(dev);
1946
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001947 /* set default norm */
1948 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001949
1950 maxw = norm_maxw(dev);
1951 maxh = norm_maxh(dev);
1952
1953 /* set default image size */
1954 dev->width = maxw;
1955 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001956 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001957 dev->hscale = 0;
1958 dev->vscale = 0;
1959 dev->ctl_input = 2;
1960
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001961 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001962
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001963 list_add_tail(&dev->devlist, &em28xx_devlist);
1964
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001965 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001966 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1967 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001968 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001969 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001970 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001971 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001972 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001973 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001974
1975 /* register v4l2 video video_device */
1976 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1977 video_nr[dev->devno]);
1978 if (retval) {
1979 em28xx_errdev("unable to register video device (error=%i).\n",
1980 retval);
1981 goto fail_unreg;
1982 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001983
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001984 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001985 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1986 VFL_TYPE_VBI, "vbi");
1987 /* register v4l2 vbi video_device */
1988 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1989 vbi_nr[dev->devno]) < 0) {
1990 em28xx_errdev("unable to register vbi device\n");
1991 retval = -ENODEV;
1992 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001993 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001994
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001995 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1996 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1997 VFL_TYPE_RADIO, "radio");
1998 if (NULL == dev->radio_dev) {
1999 em28xx_errdev("cannot allocate video_device.\n");
2000 goto fail_unreg;
2001 }
2002 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2003 radio_nr[dev->devno]);
2004 if (retval < 0) {
2005 em28xx_errdev("can't register radio device\n");
2006 goto fail_unreg;
2007 }
2008 em28xx_info("Registered radio device as /dev/radio%d\n",
2009 dev->radio_dev->minor & 0x1f);
2010 }
2011
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002012 /* init video dma queues */
2013 INIT_LIST_HEAD(&dev->vidq.active);
2014 INIT_LIST_HEAD(&dev->vidq.queued);
2015
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002016
Sascha Sommer5a804152007-11-03 21:22:38 -03002017 if (dev->has_msp34xx) {
2018 /* Send a reset to other chips via gpio */
2019 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2020 msleep(3);
2021 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2022 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002023 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002024
Sascha Sommer5a804152007-11-03 21:22:38 -03002025 video_mux(dev, 0);
2026
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002027 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2028 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2029 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002030
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002031 mutex_lock(&em28xx_extension_devlist_lock);
2032 if (!list_empty(&em28xx_extension_devlist)) {
2033 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2034 if (ops->id)
2035 ops->init(dev);
2036 }
2037 }
2038 mutex_unlock(&em28xx_extension_devlist_lock);
2039
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002040 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002041
2042fail_unreg:
2043 em28xx_release_resources(dev);
2044 mutex_unlock(&dev->lock);
2045 kfree(dev);
2046 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002047}
2048
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002049#if defined(CONFIG_MODULES) && defined(MODULE)
2050static void request_module_async(struct work_struct *work)
2051{
2052 struct em28xx *dev = container_of(work,
2053 struct em28xx, request_module_wk);
2054
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002055 if (dev->has_audio_class)
2056 request_module("snd-usb-audio");
2057 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002058 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002059
2060 if (dev->has_dvb)
2061 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002062}
2063
2064static void request_modules(struct em28xx *dev)
2065{
2066 INIT_WORK(&dev->request_module_wk, request_module_async);
2067 schedule_work(&dev->request_module_wk);
2068}
2069#else
2070#define request_modules(dev)
2071#endif /* CONFIG_MODULES */
2072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002073/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002074 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002075 * checks for supported devices
2076 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002077static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002078 const struct usb_device_id *id)
2079{
2080 const struct usb_endpoint_descriptor *endpoint;
2081 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002082 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002083 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002084 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002085 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002086
2087 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002088 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2089
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002090 /* Check to see next free device and mark as used */
2091 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2092 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002093
2094 /* Don't register audio interfaces */
2095 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002096 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002097 udev->descriptor.idVendor,udev->descriptor.idProduct,
2098 ifnum,
2099 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002100
2101 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002102 return -ENODEV;
2103 }
2104
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002105 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002106 udev->descriptor.idVendor,udev->descriptor.idProduct,
2107 ifnum,
2108 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002109
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002110 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2111
Michael Opdenacker59c51592007-05-09 08:57:56 +02002112 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002113 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2114 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002115 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002116 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002117 return -ENODEV;
2118 }
2119 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002120 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002121 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002122 return -ENODEV;
2123 }
2124
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002125 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002126 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002127 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002128 return -ENOMEM;
2129 }
2130
2131 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002132 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002133 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002134 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002135 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002136 return -ENOMEM;
2137 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002138
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002139 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002140 dev->devno = nr;
2141 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002142 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002143
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002144 /* Checks if audio is provided by some interface */
2145 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2146 uif = udev->config->interface[i];
2147 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2148 dev->has_audio_class = 1;
2149 break;
2150 }
2151 }
2152
2153 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2154 dev->has_audio_class ? "Has" : "Doesn't have");
2155
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002156 /* compute alternate max packet sizes */
2157 uif = udev->actconfig->interface[0];
2158
2159 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002160 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002161// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2162 dev->alt_max_pkt_size = kmalloc(32*
2163 dev->num_alt,GFP_KERNEL);
2164 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002165 em28xx_errdev("out of memory!\n");
2166 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002167 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002168 return -ENOMEM;
2169 }
2170
2171 for (i = 0; i < dev->num_alt ; i++) {
2172 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2173 wMaxPacketSize);
2174 dev->alt_max_pkt_size[i] =
2175 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002176 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002177 dev->alt_max_pkt_size[i]);
2178 }
2179
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002180 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002181 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002182
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002184 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002185 if (retval)
2186 return retval;
2187
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002188 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189
2190 /* save our data pointer in this interface device */
2191 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002192
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002193#if defined(CONFIG_VIDEO_EM28XX_DVB) || defined(CONFIG_VIDEO_EM28XX_DVB_MODULE)
2194 dev->qops = kmalloc(sizeof(em28xx_video_qops), GFP_KERNEL);
2195 memcpy(dev->qops, &em28xx_video_qops, sizeof(em28xx_video_qops));
2196#endif
2197
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002198 request_modules(dev);
2199
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002200 return 0;
2201}
2202
2203/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002204 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002205 * called when the device gets diconencted
2206 * video device will be unregistered on v4l2_close in case it is still open
2207 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002208static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209{
Sascha Sommer5a804152007-11-03 21:22:38 -03002210 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002211 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002212
2213 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002214 usb_set_intfdata(interface, NULL);
2215
2216 if (!dev)
2217 return;
2218
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002219 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002220
Sascha Sommer5a804152007-11-03 21:22:38 -03002221 /* wait until all current v4l2 io is finished then deallocate resources */
2222 mutex_lock(&dev->lock);
2223
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002224 wake_up_interruptible_all(&dev->open);
2225
2226 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002227 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002228 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002229 "deallocation are deferred on close.\n",
2230 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2231
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002233 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002234 dev->state |= DEV_DISCONNECTED;
2235 wake_up_interruptible(&dev->wait_frame);
2236 wake_up_interruptible(&dev->wait_stream);
2237 } else {
2238 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002239 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002241 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002242
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002243 mutex_lock(&em28xx_extension_devlist_lock);
2244 if (!list_empty(&em28xx_extension_devlist)) {
2245 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2246 ops->fini(dev);
2247 }
2248 }
2249 mutex_unlock(&em28xx_extension_devlist_lock);
2250
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002251 if (!dev->users) {
2252 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002253 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002254 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002255}
2256
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002257static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002258 .name = "em28xx",
2259 .probe = em28xx_usb_probe,
2260 .disconnect = em28xx_usb_disconnect,
2261 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262};
2263
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002264static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002265{
2266 int result;
2267
2268 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002269 (EM28XX_VERSION_CODE >> 16) & 0xff,
2270 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002271#ifdef SNAPSHOT
2272 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2273 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2274#endif
2275
2276 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002277 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002278 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002279 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280 " usb_register failed. Error number %d.\n", result);
2281
2282 return result;
2283}
2284
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002285static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002286{
2287 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002288 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002289}
2290
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002291module_init(em28xx_module_init);
2292module_exit(em28xx_module_exit);