blob: c5593526a6e724b467bb1ca2b5b46d44017bbf46 [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
66#define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
67
68/* Limits minimum and default number of buffers */
69#define EM28XX_MIN_BUF 4
70#define EM28XX_DEF_BUF 8
71
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072MODULE_AUTHOR(DRIVER_AUTHOR);
73MODULE_DESCRIPTION(DRIVER_DESC);
74MODULE_LICENSE("GPL");
75
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080076static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080077
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080078static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020079static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030080static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
82
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080083module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020084module_param_array(video_nr, int, NULL, 0444);
85module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030086module_param_array(radio_nr, int, NULL, 0444);
87MODULE_PARM_DESC(card, "card type");
88MODULE_PARM_DESC(video_nr, "video device numbers");
89MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
90MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080091
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030092static unsigned int video_debug;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080093module_param(video_debug,int,0644);
94MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
95
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020096/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
97static unsigned long em28xx_devused;
98
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080099/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200100/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800101static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800102 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200103 .id = V4L2_CID_AUDIO_VOLUME,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Volume",
106 .minimum = 0x0,
107 .maximum = 0x1f,
108 .step = 0x1,
109 .default_value = 0x1f,
110 .flags = 0,
111 },{
112 .id = V4L2_CID_AUDIO_MUTE,
113 .type = V4L2_CTRL_TYPE_BOOLEAN,
114 .name = "Mute",
115 .minimum = 0,
116 .maximum = 1,
117 .step = 1,
118 .default_value = 1,
119 .flags = 0,
120 }
121};
122
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800123static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800124
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300125/* ------------------------------------------------------------------
126 DMA and thread functions
127 ------------------------------------------------------------------*/
128
129/*
130 * Announces that a buffer were filled and request the next
131 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300132static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300133 struct em28xx_dmaqueue *dma_q,
134 struct em28xx_buffer *buf)
135{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300136 mod_timer(&dma_q->timeout, jiffies+BUFFER_TIMEOUT);
137
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300138 /* Nobody is waiting something to be done, just return */
139 if (!waitqueue_active(&buf->vb.done)) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300140 printk(KERN_ERR "em28xx: buffer underrun at %ld\n",
141 jiffies);
142
143 return;
144 }
145
146 /* Advice that buffer was filled */
147 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
148 buf->vb.state = VIDEOBUF_DONE;
149 buf->vb.field_count++;
150 do_gettimeofday(&buf->vb.ts);
151
152 list_del(&buf->vb.queue);
153 wake_up(&buf->vb.done);
154}
155
156/*
157 * Identify the buffer header type and properly handles
158 */
159static void em28xx_copy_video(struct em28xx *dev,
160 struct em28xx_dmaqueue *dma_q,
161 struct em28xx_buffer *buf,
162 unsigned char *p,
163 unsigned char *outp, unsigned long len)
164{
165 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300166 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300167
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300168 if (dev->frame_size != buf->vb.size) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169 em28xx_errdev("size %i and buf.length %lu are different!\n",
170 dev->frame_size, buf->vb.size);
171 return;
172 }
173
174 if (dma_q->pos + len > buf->vb.size)
175 len = buf->vb.size - dma_q->pos;
176
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300177 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178 em28xx_isocdbg("frame is not complete\n");
179 len += 4;
180 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300181 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182
183 startread = p;
184 remain = len;
185
186 /* Interlaces frame */
187 if (buf->top_field)
188 fieldstart = outp;
189 else
190 fieldstart = outp + dev->bytesperline;
191
192 linesdone = dma_q->pos / dev->bytesperline;
193 currlinedone = dma_q->pos % dev->bytesperline;
194 offset = linesdone * dev->bytesperline * 2 + currlinedone;
195 startwrite = fieldstart + offset;
196 lencopy = dev->bytesperline - currlinedone;
197 lencopy = lencopy > remain ? remain : lencopy;
198
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300199 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300200 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300201 ((char *)startwrite + lencopy) -
202 ((char *)outp + buf->vb.size));
203 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300204 }
205 BUG_ON(lencopy <= 0);
206 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300207
208 remain -= lencopy;
209
210 while (remain > 0) {
211 startwrite += lencopy + dev->bytesperline;
212 startread += lencopy;
213 if (dev->bytesperline > remain)
214 lencopy = remain;
215 else
216 lencopy = dev->bytesperline;
217
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300218 BUG_ON(lencopy <= 0);
219
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300220 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300221 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300222 ((char *)startwrite + lencopy) -
223 ((char *)outp + buf->vb.size));
224 lencopy = remain = (char *)outp + buf->vb.size -
225 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300226 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300227 if (lencopy <= 0)
228 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300229
230 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300231
232 remain -= lencopy;
233 }
234
235 dma_q->pos += len;
236}
237
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300238static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300239 int packet, int status)
240{
241 char *errmsg = "Unknown";
242
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300243 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300244 case -ENOENT:
245 errmsg = "unlinked synchronuously";
246 break;
247 case -ECONNRESET:
248 errmsg = "unlinked asynchronuously";
249 break;
250 case -ENOSR:
251 errmsg = "Buffer error (overrun)";
252 break;
253 case -EPIPE:
254 errmsg = "Stalled (device not responding)";
255 break;
256 case -EOVERFLOW:
257 errmsg = "Babble (bad cable?)";
258 break;
259 case -EPROTO:
260 errmsg = "Bit-stuff error (bad cable?)";
261 break;
262 case -EILSEQ:
263 errmsg = "CRC/Timeout (could be anything)";
264 break;
265 case -ETIME:
266 errmsg = "Device does not respond";
267 break;
268 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300269 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
271 } else {
272 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
273 packet, status, errmsg);
274 }
275}
276
277/*
278 * video-buf generic routine to get the next available buffer
279 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300280static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300281 struct em28xx_buffer **buf)
282{
283 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
284
285 if (list_empty(&dma_q->active)) {
286 em28xx_isocdbg("No active queue to serve\n");
287 return 0;
288 }
289
290 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
291
292 return 1;
293}
294
295/*
296 * Controls the isoc copy of each urb packet
297 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300298static inline int em28xx_isoc_copy(struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300300 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301 struct em28xx_dmaqueue *dma_q = urb->context;
302 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300303 unsigned char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300304 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300305 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300306
307 if (!dev)
308 return 0;
309
310 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
311 return 0;
312
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300313 if (urb->status < 0) {
314 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300315 if (urb->status == -ENOENT)
316 return 0;
317 }
318
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300319 buf = dev->isoc_ctl.buf;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300320
321 if (!buf) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300322 rc = get_next_buf(dma_q, &buf);
323 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300324 return rc;
325 }
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300328
329
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330 for (i = 0; i < urb->number_of_packets; i++) {
331 int status = urb->iso_frame_desc[i].status;
332
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300333 if (status < 0) {
334 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300335 if (urb->iso_frame_desc[i].status != -EPROTO)
336 continue;
337 }
338
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300339 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340
341 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300342 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300343 continue;
344 }
345 if (urb->iso_frame_desc[i].actual_length >
346 dev->max_pkt_size) {
347 em28xx_isocdbg("packet bigger than packet size");
348 continue;
349 }
350
351 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300352
353 /* FIXME: incomplete buffer checks where removed to make
354 logic simpler. Impacts of those changes should be evaluated
355 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300356 if (p[0] == 0x22 && p[1] == 0x5a) {
357 /* FIXME - are the fields the right way around? */
358 em28xx_isocdbg("Video frame, length=%i, %s\n", len,
359 (p[2] & 1)? "top" : "bottom");
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300360 em28xx_isocdbg("Current buffer is: outp = 0x%p,"
361 " len = %i\n", outp, (int)buf->vb.size);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300362
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300363 if (p[2] & 1) {
364 if (buf->receiving) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300365 buffer_filled(dev, dma_q, buf);
366 rc = get_next_buf(dma_q, &buf);
367 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300368 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300370 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300371 }
372
373 buf->top_field = 1;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300374 } else
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300375 buf->top_field = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300376 buf->receiving = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377 dma_q->pos = 0;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300378 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300379 em28xx_isocdbg("VBI HEADER!!!\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300380 }
381
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300382 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300383
384 /* FIXME: Should add vbi copy */
385 }
386 return rc;
387}
388
389/* ------------------------------------------------------------------
390 URB control
391 ------------------------------------------------------------------*/
392
393/*
394 * IRQ callback, called by URB callback
395 */
396static void em28xx_irq_callback(struct urb *urb)
397{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300398 struct em28xx_dmaqueue *dma_q = urb->context;
399 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300400 int rc, i;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401 unsigned long flags;
402
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300403 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300404
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405 /* Copy data from URB */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300406 rc = em28xx_isoc_copy(urb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300407
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300408 /* Reset urb buffers */
409 for (i = 0; i < urb->number_of_packets; i++) {
410 urb->iso_frame_desc[i].status = 0;
411 urb->iso_frame_desc[i].actual_length = 0;
412 }
413 urb->status = 0;
414
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300415 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
416 if (urb->status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300417 em28xx_err("urb resubmit failed (error=%i)\n",
418 urb->status);
419 }
420
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300421 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300422}
423
424/*
425 * Stop and Deallocate URBs
426 */
427static void em28xx_uninit_isoc(struct em28xx *dev)
428{
429 struct urb *urb;
430 int i;
431
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300432 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
433
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300434 dev->isoc_ctl.nfields = -1;
435 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300436 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438 if (urb) {
439 usb_kill_urb(urb);
440 usb_unlink_urb(urb);
441 if (dev->isoc_ctl.transfer_buffer[i]) {
442 usb_buffer_free(dev->udev,
443 urb->transfer_buffer_length,
444 dev->isoc_ctl.transfer_buffer[i],
445 urb->transfer_dma);
446 }
447 usb_free_urb(urb);
448 dev->isoc_ctl.urb[i] = NULL;
449 }
450 dev->isoc_ctl.transfer_buffer[i] = NULL;
451 }
452
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300453 kfree(dev->isoc_ctl.urb);
454 kfree(dev->isoc_ctl.transfer_buffer);
455 dev->isoc_ctl.urb = NULL;
456 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300460 del_timer(&dev->vidq.timeout);
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300461 em28xx_capture_start(dev, 0);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300462}
463
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300464
465/*
466 * Allocate URBs and start IRQ
467 */
468static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
469 int num_bufs)
470{
471 struct em28xx_dmaqueue *dma_q = &dev->vidq;
472 int i;
473 int sb_size, pipe;
474 struct urb *urb;
475 int j, k;
476
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300477 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
478
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479 /* De-allocates all pending stuff */
480 em28xx_uninit_isoc(dev);
481
482 dev->isoc_ctl.num_bufs = num_bufs;
483
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300484 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485 if (!dev->isoc_ctl.urb) {
486 em28xx_errdev("cannot alloc memory for usb buffers\n");
487 return -ENOMEM;
488 }
489
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300490 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491 GFP_KERNEL);
492 if (!dev->isoc_ctl.urb) {
493 em28xx_errdev("cannot allocate memory for usbtransfer\n");
494 kfree(dev->isoc_ctl.urb);
495 return -ENOMEM;
496 }
497
498 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
499
500 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
501
502 /* allocate urbs and transfer buffers */
503 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
504 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
505 if (!urb) {
506 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
507 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300508 return -ENOMEM;
509 }
510 dev->isoc_ctl.urb[i] = urb;
511
512 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
513 sb_size, GFP_KERNEL, &urb->transfer_dma);
514 if (!dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300515 em28xx_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300516 " buffer %i%s\n",
517 sb_size, i,
518 in_interrupt()?" while in int":"");
519 em28xx_uninit_isoc(dev);
520 return -ENOMEM;
521 }
522 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
523
524 /* FIXME: this is a hack - should be
525 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
526 should also be using 'desc.bInterval'
527 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300528 pipe = usb_rcvisocpipe(dev->udev, 0x82);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300529 usb_fill_int_urb(urb, dev->udev, pipe,
530 dev->isoc_ctl.transfer_buffer[i], sb_size,
531 em28xx_irq_callback, dma_q, 1);
532
533 urb->number_of_packets = max_packets;
534 urb->transfer_flags = URB_ISO_ASAP;
535
536 k = 0;
537 for (j = 0; j < max_packets; j++) {
538 urb->iso_frame_desc[j].offset = k;
539 urb->iso_frame_desc[j].length =
540 dev->isoc_ctl.max_pkt_size;
541 k += dev->isoc_ctl.max_pkt_size;
542 }
543 }
544
545 return 0;
546}
547
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300548static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300549{
550 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300551 int i, rc = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300552
553 em28xx_videodbg("Called em28xx_start_thread\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300554
555 init_waitqueue_head(&dma_q->wq);
556
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300557 em28xx_capture_start(dev, 1);
558
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300559 /* submit urbs and enables IRQ */
560 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
561 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
562 if (rc) {
563 em28xx_err("submit of urb %i failed (error=%i)\n", i,
564 rc);
565 em28xx_uninit_isoc(dev);
566 return rc;
567 }
568 }
569
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300570 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300571 return rc;
572
573 return 0;
574}
575
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300576static void em28xx_vid_timeout(unsigned long data)
577{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300578 struct em28xx *dev = (struct em28xx *)data;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300579 struct em28xx_dmaqueue *vidq = &dev->vidq;
580 struct em28xx_buffer *buf;
581 unsigned long flags;
582
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300583 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300584
585 list_for_each_entry(buf, vidq->active.next, vb.queue) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300586 list_del(&buf->vb.queue);
587 buf->vb.state = VIDEOBUF_ERROR;
588 wake_up(&buf->vb.done);
589 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
590 buf, buf->vb.i);
591 }
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300592 /* Instead of trying to restart, just sets timeout again */
593 mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300594
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300595 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300596}
597
598/* ------------------------------------------------------------------
599 Videobuf operations
600 ------------------------------------------------------------------*/
601
602static int
603buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
604{
605 struct em28xx_fh *fh = vq->priv_data;
606
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300607 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300608 if (0 == *count)
609 *count = EM28XX_DEF_BUF;
610
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300611 if (*count < EM28XX_MIN_BUF)
612 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300613
614 return 0;
615}
616
617static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
618{
619 if (in_interrupt())
620 BUG();
621
622 videobuf_waiton(&buf->vb, 0, 0);
623 videobuf_vmalloc_free(&buf->vb);
624 buf->vb.state = VIDEOBUF_NEEDS_INIT;
625}
626
627static int
628buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
629 enum v4l2_field field)
630{
631 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300632 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300633 struct em28xx *dev = fh->dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300634 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300635 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300637 /* BUG_ON(NULL == fh->fmt); */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300638
639 /* FIXME: It assumes depth = 16 */
640 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300641 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300642
643 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
644 return -EINVAL;
645
646 if (buf->fmt != fh->fmt ||
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300647 buf->vb.width != dev->width ||
648 buf->vb.height != dev->height ||
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300649 buf->vb.field != field) {
650 buf->fmt = fh->fmt;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300651 buf->vb.width = dev->width;
652 buf->vb.height = dev->height;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300653 buf->vb.field = field;
654 buf->vb.state = VIDEOBUF_NEEDS_INIT;
655 }
656
657 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300658 rc = videobuf_iolock(vq, &buf->vb, NULL);
659 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300660 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661 }
662
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300663 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300664 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300665
666 if (urb_init) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300667 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
668 EM28XX_NUM_BUFS);
669 if (rc < 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300670 goto fail;
671
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300672 rc = em28xx_start_thread(vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300673 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300674 goto fail;
675 }
676
677 buf->vb.state = VIDEOBUF_PREPARED;
678 return 0;
679
680fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300681 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300682 return rc;
683}
684
685static void
686buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
687{
688 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
689 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300690 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300691 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300692
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300693 buf->vb.state = VIDEOBUF_QUEUED;
694 list_add_tail(&buf->vb.queue, &vidq->active);
695
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300696}
697
698static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
699{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300700 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300701 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300702 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300703
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300704 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300705
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300706 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300707}
708
709static struct videobuf_queue_ops em28xx_video_qops = {
710 .buf_setup = buffer_setup,
711 .buf_prepare = buffer_prepare,
712 .buf_queue = buffer_queue,
713 .buf_release = buffer_release,
714};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800715
716/********************* v4l2 interface ******************************************/
717
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800718/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800719 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800720 * inits registers with sane defaults
721 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800722static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800723{
724
725 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300726 if (!dev->is_em2800)
727 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800728
729 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200730
Markus Rechberger9475fb12006-02-27 00:07:34 -0300731/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
732/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200733 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
734
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800735 dev->mute = 1; /* maybe not the right place... */
736 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300737
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800738 em28xx_outfmt_set_yuv422(dev);
739 em28xx_colorlevels_set_default(dev);
740 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800741
742 return 0;
743}
744
745/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800746 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800747 * configure i2c attached devices
748 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800749static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800750{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300751 struct v4l2_routing route;
752
753 route.input = INPUT(dev->ctl_input)->vmux;
754 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100755 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300756 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300757 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800758}
759
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800760static void video_mux(struct em28xx *dev, int index)
761{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300762 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800763
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300764 route.input = INPUT(index)->vmux;
765 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800766 dev->ctl_input = index;
767 dev->ctl_ainput = INPUT(index)->amux;
768
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300769 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800770
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800771 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200772 if (dev->i2s_speed)
773 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300774 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300775 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300776 /* Note: this is msp3400 specific */
777 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800778 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300779
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300780 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800781}
782
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300783/* Usage lock check functions */
784static int res_get(struct em28xx_fh *fh)
785{
786 struct em28xx *dev = fh->dev;
787 int rc = 0;
788
789 /* This instance already has stream_on */
790 if (fh->stream_on)
791 return rc;
792
793 mutex_lock(&dev->lock);
794
795 if (dev->stream_on)
796 rc = -EINVAL;
797 else {
798 dev->stream_on = 1;
799 fh->stream_on = 1;
800 }
801
802 mutex_unlock(&dev->lock);
803 return rc;
804}
805
806static int res_check(struct em28xx_fh *fh)
807{
808 return (fh->stream_on);
809}
810
811static void res_free(struct em28xx_fh *fh)
812{
813 struct em28xx *dev = fh->dev;
814
815 mutex_lock(&dev->lock);
816 fh->stream_on = 0;
817 dev->stream_on = 0;
818 mutex_unlock(&dev->lock);
819}
820
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800821/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822 * em28xx_get_ctrl()
823 * return the current saturation, brightness or contrast, mute state
824 */
825static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
826{
827 switch (ctrl->id) {
828 case V4L2_CID_AUDIO_MUTE:
829 ctrl->value = dev->mute;
830 return 0;
831 case V4L2_CID_AUDIO_VOLUME:
832 ctrl->value = dev->volume;
833 return 0;
834 default:
835 return -EINVAL;
836 }
837}
838
839/*
840 * em28xx_set_ctrl()
841 * mute or set new saturation, brightness or contrast
842 */
843static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
844{
845 switch (ctrl->id) {
846 case V4L2_CID_AUDIO_MUTE:
847 if (ctrl->value != dev->mute) {
848 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300849 return em28xx_audio_analog_set(dev);
850 }
851 return 0;
852 case V4L2_CID_AUDIO_VOLUME:
853 dev->volume = ctrl->value;
854 return em28xx_audio_analog_set(dev);
855 default:
856 return -EINVAL;
857 }
858}
859
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300860static int check_dev(struct em28xx *dev)
861{
862 if (dev->state & DEV_DISCONNECTED) {
863 em28xx_errdev("v4l2 ioctl: device not present\n");
864 return -ENODEV;
865 }
866
867 if (dev->state & DEV_MISCONFIGURED) {
868 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
869 "close and open it again\n");
870 return -EIO;
871 }
872 return 0;
873}
874
875static void get_scale(struct em28xx *dev,
876 unsigned int width, unsigned int height,
877 unsigned int *hscale, unsigned int *vscale)
878{
879 unsigned int maxw = norm_maxw(dev);
880 unsigned int maxh = norm_maxh(dev);
881
882 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
883 if (*hscale >= 0x4000)
884 *hscale = 0x3fff;
885
886 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
887 if (*vscale >= 0x4000)
888 *vscale = 0x3fff;
889}
890
891/* ------------------------------------------------------------------
892 IOCTL vidioc handling
893 ------------------------------------------------------------------*/
894
895static int vidioc_g_fmt_cap(struct file *file, void *priv,
896 struct v4l2_format *f)
897{
898 struct em28xx_fh *fh = priv;
899 struct em28xx *dev = fh->dev;
900
901 mutex_lock(&dev->lock);
902
903 f->fmt.pix.width = dev->width;
904 f->fmt.pix.height = dev->height;
905 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
906 f->fmt.pix.bytesperline = dev->bytesperline;
907 f->fmt.pix.sizeimage = dev->frame_size;
908 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
909
910 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
911 f->fmt.pix.field = dev->interlaced ?
912 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
913
914 mutex_unlock(&dev->lock);
915 return 0;
916}
917
918static int vidioc_try_fmt_cap(struct file *file, void *priv,
919 struct v4l2_format *f)
920{
921 struct em28xx_fh *fh = priv;
922 struct em28xx *dev = fh->dev;
923 int width = f->fmt.pix.width;
924 int height = f->fmt.pix.height;
925 unsigned int maxw = norm_maxw(dev);
926 unsigned int maxh = norm_maxh(dev);
927 unsigned int hscale, vscale;
928
929 /* width must even because of the YUYV format
930 height must be even because of interlacing */
931 height &= 0xfffe;
932 width &= 0xfffe;
933
934 if (height < 32)
935 height = 32;
936 if (height > maxh)
937 height = maxh;
938 if (width < 48)
939 width = 48;
940 if (width > maxw)
941 width = maxw;
942
943 mutex_lock(&dev->lock);
944
945 if (dev->is_em2800) {
946 /* the em2800 can only scale down to 50% */
947 if (height % (maxh / 2))
948 height = maxh;
949 if (width % (maxw / 2))
950 width = maxw;
951 /* according to empiatech support */
952 /* the MaxPacketSize is to small to support */
953 /* framesizes larger than 640x480 @ 30 fps */
954 /* or 640x576 @ 25 fps. As this would cut */
955 /* of a part of the image we prefer */
956 /* 360x576 or 360x480 for now */
957 if (width == maxw && height == maxh)
958 width /= 2;
959 }
960
961 get_scale(dev, width, height, &hscale, &vscale);
962
963 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
964 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
965
966 f->fmt.pix.width = width;
967 f->fmt.pix.height = height;
968 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
969 f->fmt.pix.bytesperline = width * 2;
970 f->fmt.pix.sizeimage = width * 2 * height;
971 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
972 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
973
974 mutex_unlock(&dev->lock);
975 return 0;
976}
977
978static int vidioc_s_fmt_cap(struct file *file, void *priv,
979 struct v4l2_format *f)
980{
981 struct em28xx_fh *fh = priv;
982 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300983 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984
985 rc = check_dev(dev);
986 if (rc < 0)
987 return rc;
988
989 vidioc_try_fmt_cap(file, priv, f);
990
991 mutex_lock(&dev->lock);
992
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300993 /* set new image size */
994 dev->width = f->fmt.pix.width;
995 dev->height = f->fmt.pix.height;
996 dev->frame_size = dev->width * dev->height * 2;
997 dev->field_size = dev->frame_size >> 1;
998 dev->bytesperline = dev->width * 2;
999 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1000
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001001 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001002 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001003
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001005 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001006}
1007
1008static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1009{
1010 struct em28xx_fh *fh = priv;
1011 struct em28xx *dev = fh->dev;
1012 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001013 int rc;
1014
1015 rc = check_dev(dev);
1016 if (rc < 0)
1017 return rc;
1018
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001019 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001020 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001021 mutex_unlock(&dev->lock);
1022
1023 /* Adjusts width/height, if needed */
1024 f.fmt.pix.width = dev->width;
1025 f.fmt.pix.height = dev->height;
1026 vidioc_try_fmt_cap(file, priv, &f);
1027
1028 mutex_lock(&dev->lock);
1029
1030 /* set new image size */
1031 dev->width = f.fmt.pix.width;
1032 dev->height = f.fmt.pix.height;
1033 dev->frame_size = dev->width * dev->height * 2;
1034 dev->field_size = dev->frame_size >> 1;
1035 dev->bytesperline = dev->width * 2;
1036 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1037
1038 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001039 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001040
1041 mutex_unlock(&dev->lock);
1042 return 0;
1043}
1044
1045static const char *iname[] = {
1046 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1047 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1048 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1049 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1050 [EM28XX_VMUX_SVIDEO] = "S-Video",
1051 [EM28XX_VMUX_TELEVISION] = "Television",
1052 [EM28XX_VMUX_CABLE] = "Cable TV",
1053 [EM28XX_VMUX_DVB] = "DVB",
1054 [EM28XX_VMUX_DEBUG] = "for debug only",
1055};
1056
1057static int vidioc_enum_input(struct file *file, void *priv,
1058 struct v4l2_input *i)
1059{
1060 struct em28xx_fh *fh = priv;
1061 struct em28xx *dev = fh->dev;
1062 unsigned int n;
1063
1064 n = i->index;
1065 if (n >= MAX_EM28XX_INPUT)
1066 return -EINVAL;
1067 if (0 == INPUT(n)->type)
1068 return -EINVAL;
1069
1070 i->index = n;
1071 i->type = V4L2_INPUT_TYPE_CAMERA;
1072
1073 strcpy(i->name, iname[INPUT(n)->type]);
1074
1075 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1076 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1077 i->type = V4L2_INPUT_TYPE_TUNER;
1078
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001079 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001080
1081 return 0;
1082}
1083
1084static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1085{
1086 struct em28xx_fh *fh = priv;
1087 struct em28xx *dev = fh->dev;
1088
1089 *i = dev->ctl_input;
1090
1091 return 0;
1092}
1093
1094static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1095{
1096 struct em28xx_fh *fh = priv;
1097 struct em28xx *dev = fh->dev;
1098 int rc;
1099
1100 rc = check_dev(dev);
1101 if (rc < 0)
1102 return rc;
1103
1104 if (i >= MAX_EM28XX_INPUT)
1105 return -EINVAL;
1106 if (0 == INPUT(i)->type)
1107 return -EINVAL;
1108
1109 mutex_lock(&dev->lock);
1110
1111 video_mux(dev, i);
1112
1113 mutex_unlock(&dev->lock);
1114 return 0;
1115}
1116
1117static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1118{
1119 struct em28xx_fh *fh = priv;
1120 struct em28xx *dev = fh->dev;
1121 unsigned int index = a->index;
1122
1123 if (a->index > 1)
1124 return -EINVAL;
1125
1126 index = dev->ctl_ainput;
1127
1128 if (index == 0) {
1129 strcpy(a->name, "Television");
1130 } else {
1131 strcpy(a->name, "Line In");
1132 }
1133 a->capability = V4L2_AUDCAP_STEREO;
1134 a->index = index;
1135
1136 return 0;
1137}
1138
1139static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1140{
1141 struct em28xx_fh *fh = priv;
1142 struct em28xx *dev = fh->dev;
1143
1144 if (a->index != dev->ctl_ainput)
1145 return -EINVAL;
1146
1147 return 0;
1148}
1149
1150static int vidioc_queryctrl(struct file *file, void *priv,
1151 struct v4l2_queryctrl *qc)
1152{
1153 struct em28xx_fh *fh = priv;
1154 struct em28xx *dev = fh->dev;
1155 int id = qc->id;
1156 int i;
1157 int rc;
1158
1159 rc = check_dev(dev);
1160 if (rc < 0)
1161 return rc;
1162
1163 memset(qc, 0, sizeof(*qc));
1164
1165 qc->id = id;
1166
1167 if (!dev->has_msp34xx) {
1168 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1169 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1170 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1171 return 0;
1172 }
1173 }
1174 }
1175 mutex_lock(&dev->lock);
1176 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1177 mutex_unlock(&dev->lock);
1178
1179 if (qc->type)
1180 return 0;
1181 else
1182 return -EINVAL;
1183}
1184
1185static int vidioc_g_ctrl(struct file *file, void *priv,
1186 struct v4l2_control *ctrl)
1187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190 int rc;
1191
1192 rc = check_dev(dev);
1193 if (rc < 0)
1194 return rc;
1195 mutex_lock(&dev->lock);
1196
1197 if (!dev->has_msp34xx)
1198 rc = em28xx_get_ctrl(dev, ctrl);
1199 else
1200 rc = -EINVAL;
1201
1202 if (rc == -EINVAL) {
1203 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1204 rc = 0;
1205 }
1206
1207 mutex_unlock(&dev->lock);
1208 return rc;
1209}
1210
1211static int vidioc_s_ctrl(struct file *file, void *priv,
1212 struct v4l2_control *ctrl)
1213{
1214 struct em28xx_fh *fh = priv;
1215 struct em28xx *dev = fh->dev;
1216 u8 i;
1217 int rc;
1218
1219 rc = check_dev(dev);
1220 if (rc < 0)
1221 return rc;
1222
1223 mutex_lock(&dev->lock);
1224
1225 if (dev->has_msp34xx)
1226 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1227 else {
1228 rc = 1;
1229 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1230 if (ctrl->id == em28xx_qctrl[i].id) {
1231 if (ctrl->value < em28xx_qctrl[i].minimum ||
1232 ctrl->value > em28xx_qctrl[i].maximum) {
1233 rc = -ERANGE;
1234 break;
1235 }
1236
1237 rc = em28xx_set_ctrl(dev, ctrl);
1238 break;
1239 }
1240 }
1241 }
1242
1243 /* Control not found - try to send it to the attached devices */
1244 if (rc == 1) {
1245 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1246 rc = 0;
1247 }
1248
1249 mutex_unlock(&dev->lock);
1250 return rc;
1251}
1252
1253static int vidioc_g_tuner(struct file *file, void *priv,
1254 struct v4l2_tuner *t)
1255{
1256 struct em28xx_fh *fh = priv;
1257 struct em28xx *dev = fh->dev;
1258 int rc;
1259
1260 rc = check_dev(dev);
1261 if (rc < 0)
1262 return rc;
1263
1264 if (0 != t->index)
1265 return -EINVAL;
1266
1267 strcpy(t->name, "Tuner");
1268
1269 mutex_lock(&dev->lock);
1270
1271 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1272
1273 mutex_unlock(&dev->lock);
1274 return 0;
1275}
1276
1277static int vidioc_s_tuner(struct file *file, void *priv,
1278 struct v4l2_tuner *t)
1279{
1280 struct em28xx_fh *fh = priv;
1281 struct em28xx *dev = fh->dev;
1282 int rc;
1283
1284 rc = check_dev(dev);
1285 if (rc < 0)
1286 return rc;
1287
1288 if (0 != t->index)
1289 return -EINVAL;
1290
1291 mutex_lock(&dev->lock);
1292
1293 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1294
1295 mutex_unlock(&dev->lock);
1296 return 0;
1297}
1298
1299static int vidioc_g_frequency(struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1301{
1302 struct em28xx_fh *fh = priv;
1303 struct em28xx *dev = fh->dev;
1304
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001305 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306 f->frequency = dev->ctl_freq;
1307
1308 return 0;
1309}
1310
1311static int vidioc_s_frequency(struct file *file, void *priv,
1312 struct v4l2_frequency *f)
1313{
1314 struct em28xx_fh *fh = priv;
1315 struct em28xx *dev = fh->dev;
1316 int rc;
1317
1318 rc = check_dev(dev);
1319 if (rc < 0)
1320 return rc;
1321
1322 if (0 != f->tuner)
1323 return -EINVAL;
1324
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001325 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1326 return -EINVAL;
1327 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001328 return -EINVAL;
1329
1330 mutex_lock(&dev->lock);
1331
1332 dev->ctl_freq = f->frequency;
1333 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1334
1335 mutex_unlock(&dev->lock);
1336 return 0;
1337}
1338
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001339#ifdef CONFIG_VIDEO_ADV_DEBUG
1340static int em28xx_reg_len(int reg)
1341{
1342 switch (reg) {
1343 case AC97LSB_REG:
1344 case HSCALELOW_REG:
1345 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001346 return 2;
1347 default:
1348 return 1;
1349 }
1350}
1351
1352static int vidioc_g_register(struct file *file, void *priv,
1353 struct v4l2_register *reg)
1354{
1355 struct em28xx_fh *fh = priv;
1356 struct em28xx *dev = fh->dev;
1357 int ret;
1358
1359 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1360 return -EINVAL;
1361
1362 if (em28xx_reg_len(reg->reg) == 1) {
1363 ret = em28xx_read_reg(dev, reg->reg);
1364 if (ret < 0)
1365 return ret;
1366
1367 reg->val = ret;
1368 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001369 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001370 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1371 reg->reg, (char *)&val, 2);
1372 if (ret < 0)
1373 return ret;
1374
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001375 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001376 }
1377
1378 return 0;
1379}
1380
1381static int vidioc_s_register(struct file *file, void *priv,
1382 struct v4l2_register *reg)
1383{
1384 struct em28xx_fh *fh = priv;
1385 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001386 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001387
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001388 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001389
1390 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1391 em28xx_reg_len(reg->reg));
1392}
1393#endif
1394
1395
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001396static int vidioc_cropcap(struct file *file, void *priv,
1397 struct v4l2_cropcap *cc)
1398{
1399 struct em28xx_fh *fh = priv;
1400 struct em28xx *dev = fh->dev;
1401
1402 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1403 return -EINVAL;
1404
1405 cc->bounds.left = 0;
1406 cc->bounds.top = 0;
1407 cc->bounds.width = dev->width;
1408 cc->bounds.height = dev->height;
1409 cc->defrect = cc->bounds;
1410 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1411 cc->pixelaspect.denominator = 59;
1412
1413 return 0;
1414}
1415
1416static int vidioc_streamon(struct file *file, void *priv,
1417 enum v4l2_buf_type type)
1418{
1419 struct em28xx_fh *fh = priv;
1420 struct em28xx *dev = fh->dev;
1421 int rc;
1422
1423 rc = check_dev(dev);
1424 if (rc < 0)
1425 return rc;
1426
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001428 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001429 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001430
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001431 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432}
1433
1434static int vidioc_streamoff(struct file *file, void *priv,
1435 enum v4l2_buf_type type)
1436{
1437 struct em28xx_fh *fh = priv;
1438 struct em28xx *dev = fh->dev;
1439 int rc;
1440
1441 rc = check_dev(dev);
1442 if (rc < 0)
1443 return rc;
1444
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001445 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1446 return -EINVAL;
1447 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001448 return -EINVAL;
1449
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001450 videobuf_streamoff(&fh->vb_vidq);
1451 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001452
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001453 return 0;
1454}
1455
1456static int vidioc_querycap(struct file *file, void *priv,
1457 struct v4l2_capability *cap)
1458{
1459 struct em28xx_fh *fh = priv;
1460 struct em28xx *dev = fh->dev;
1461
1462 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1463 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1464 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1465
1466 cap->version = EM28XX_VERSION_CODE;
1467
1468 cap->capabilities =
1469 V4L2_CAP_SLICED_VBI_CAPTURE |
1470 V4L2_CAP_VIDEO_CAPTURE |
1471 V4L2_CAP_AUDIO |
1472 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1473
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001474 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001475 cap->capabilities |= V4L2_CAP_TUNER;
1476
1477 return 0;
1478}
1479
1480static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1481 struct v4l2_fmtdesc *fmtd)
1482{
1483 if (fmtd->index != 0)
1484 return -EINVAL;
1485
1486 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1487 strcpy(fmtd->description, "Packed YUY2");
1488 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1489 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1490
1491 return 0;
1492}
1493
1494/* Sliced VBI ioctls */
1495static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1496 struct v4l2_format *f)
1497{
1498 struct em28xx_fh *fh = priv;
1499 struct em28xx *dev = fh->dev;
1500 int rc;
1501
1502 rc = check_dev(dev);
1503 if (rc < 0)
1504 return rc;
1505
1506 mutex_lock(&dev->lock);
1507
1508 f->fmt.sliced.service_set = 0;
1509
1510 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1511
1512 if (f->fmt.sliced.service_set == 0)
1513 rc = -EINVAL;
1514
1515 mutex_unlock(&dev->lock);
1516 return rc;
1517}
1518
1519static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1520 struct v4l2_format *f)
1521{
1522 struct em28xx_fh *fh = priv;
1523 struct em28xx *dev = fh->dev;
1524 int rc;
1525
1526 rc = check_dev(dev);
1527 if (rc < 0)
1528 return rc;
1529
1530 mutex_lock(&dev->lock);
1531 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1532 mutex_unlock(&dev->lock);
1533
1534 if (f->fmt.sliced.service_set == 0)
1535 return -EINVAL;
1536
1537 return 0;
1538}
1539
1540
1541static int vidioc_reqbufs(struct file *file, void *priv,
1542 struct v4l2_requestbuffers *rb)
1543{
1544 struct em28xx_fh *fh = priv;
1545 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001546 int rc;
1547
1548 rc = check_dev(dev);
1549 if (rc < 0)
1550 return rc;
1551
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001552 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001553}
1554
1555static int vidioc_querybuf(struct file *file, void *priv,
1556 struct v4l2_buffer *b)
1557{
1558 struct em28xx_fh *fh = priv;
1559 struct em28xx *dev = fh->dev;
1560 int rc;
1561
1562 rc = check_dev(dev);
1563 if (rc < 0)
1564 return rc;
1565
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001566 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001567}
1568
1569static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1570{
1571 struct em28xx_fh *fh = priv;
1572 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001573 int rc;
1574
1575 rc = check_dev(dev);
1576 if (rc < 0)
1577 return rc;
1578
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001579 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001580}
1581
1582static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1583{
1584 struct em28xx_fh *fh = priv;
1585 struct em28xx *dev = fh->dev;
1586 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001587
1588 rc = check_dev(dev);
1589 if (rc < 0)
1590 return rc;
1591
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001592 return (videobuf_dqbuf(&fh->vb_vidq, b,
1593 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001594}
1595
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001596#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001597static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001598{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001599 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001600
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001601 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001602}
1603#endif
1604
1605
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001606/* ----------------------------------------------------------- */
1607/* RADIO ESPECIFIC IOCTLS */
1608/* ----------------------------------------------------------- */
1609
1610static int radio_querycap(struct file *file, void *priv,
1611 struct v4l2_capability *cap)
1612{
1613 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1614
1615 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1616 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1617 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1618
1619 cap->version = EM28XX_VERSION_CODE;
1620 cap->capabilities = V4L2_CAP_TUNER;
1621 return 0;
1622}
1623
1624static int radio_g_tuner(struct file *file, void *priv,
1625 struct v4l2_tuner *t)
1626{
1627 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1628
1629 if (unlikely(t->index > 0))
1630 return -EINVAL;
1631
1632 strcpy(t->name, "Radio");
1633 t->type = V4L2_TUNER_RADIO;
1634
1635 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1636 return 0;
1637}
1638
1639static int radio_enum_input(struct file *file, void *priv,
1640 struct v4l2_input *i)
1641{
1642 if (i->index != 0)
1643 return -EINVAL;
1644 strcpy(i->name, "Radio");
1645 i->type = V4L2_INPUT_TYPE_TUNER;
1646
1647 return 0;
1648}
1649
1650static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1651{
1652 if (unlikely(a->index))
1653 return -EINVAL;
1654
1655 strcpy(a->name, "Radio");
1656 return 0;
1657}
1658
1659static int radio_s_tuner(struct file *file, void *priv,
1660 struct v4l2_tuner *t)
1661{
1662 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1663
1664 if (0 != t->index)
1665 return -EINVAL;
1666
1667 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1668
1669 return 0;
1670}
1671
1672static int radio_s_audio(struct file *file, void *fh,
1673 struct v4l2_audio *a)
1674{
1675 return 0;
1676}
1677
1678static int radio_s_input(struct file *file, void *fh, unsigned int i)
1679{
1680 return 0;
1681}
1682
1683static int radio_queryctrl(struct file *file, void *priv,
1684 struct v4l2_queryctrl *qc)
1685{
1686 int i;
1687
1688 if (qc->id < V4L2_CID_BASE ||
1689 qc->id >= V4L2_CID_LASTP1)
1690 return -EINVAL;
1691
1692 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1693 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1694 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1695 return 0;
1696 }
1697 }
1698
1699 return -EINVAL;
1700}
1701
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001702/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001703 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704 * inits the device and starts isoc transfer
1705 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001706static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001707{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001709 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001710 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001711 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001712 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001713
Trent Piephoa991f442007-10-10 05:37:43 -03001714 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001715 if (h->vdev->minor == minor) {
1716 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001717 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001718 }
1719 if (h->vbi_dev->minor == minor) {
1720 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001721 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001722 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001723 if (h->radio_dev &&
1724 h->radio_dev->minor == minor) {
1725 radio = 1;
1726 dev = h;
1727 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001728 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001729 if (NULL == dev)
1730 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001731
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001732 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001733 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001734
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001735 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001736
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001737 if (!fh) {
1738 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1739 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001740 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001741 mutex_lock(&dev->lock);
1742 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001743 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001744 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001745 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001747 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001748 dev->width = norm_maxw(dev);
1749 dev->height = norm_maxh(dev);
1750 dev->frame_size = dev->width * dev->height * 2;
1751 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1752 dev->bytesperline = dev->width * 2;
1753 dev->hscale = 0;
1754 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001756 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001757 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001758
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001759 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001760 if (fh->radio) {
1761 em28xx_videodbg("video_open: setting radio device\n");
1762 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1763 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001764
1765 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001767 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1768 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1769 sizeof(struct em28xx_buffer), fh);
1770
Ingo Molnar3593cab2006-02-07 06:49:14 -02001771 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772 return errCode;
1773}
1774
1775/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001776 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001777 * unregisters the v4l2,i2c and usb devices
1778 * called when the device gets disconected or at module unload
1779*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001780static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001782
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001783 /*FIXME: I2C IR should be disconnected */
1784
1785 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1786 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1787 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001788 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001789 if (dev->radio_dev) {
1790 if (-1 != dev->radio_dev->minor)
1791 video_unregister_device(dev->radio_dev);
1792 else
1793 video_device_release(dev->radio_dev);
1794 dev->radio_dev = NULL;
1795 }
1796 if (dev->vbi_dev) {
1797 if (-1 != dev->vbi_dev->minor)
1798 video_unregister_device(dev->vbi_dev);
1799 else
1800 video_device_release(dev->vbi_dev);
1801 dev->vbi_dev = NULL;
1802 }
1803 if (dev->vdev) {
1804 if (-1 != dev->vdev->minor)
1805 video_unregister_device(dev->vdev);
1806 else
1807 video_device_release(dev->vdev);
1808 dev->vdev = NULL;
1809 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001810 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001811 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001812
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001813 /* Mark device as unused */
1814 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001815}
1816
1817/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001818 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001819 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1820 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001821static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001823 struct em28xx_fh *fh = filp->private_data;
1824 struct em28xx *dev = fh->dev;
1825 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001826
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001827 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001829
1830 if (res_check(fh))
1831 res_free(fh);
1832
Ingo Molnar3593cab2006-02-07 06:49:14 -02001833 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001834
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001835 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001836 videobuf_stop(&fh->vb_vidq);
1837 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001838
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001839 /* the device is already disconnect,
1840 free the remaining resources */
1841 if (dev->state & DEV_DISCONNECTED) {
1842 em28xx_release_resources(dev);
1843 mutex_unlock(&dev->lock);
1844 kfree(dev);
1845 return 0;
1846 }
1847
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001848 /* do this before setting alternate! */
1849 em28xx_uninit_isoc(dev);
1850
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001851 /* set alternate 0 */
1852 dev->alt = 0;
1853 em28xx_videodbg("setting alternate 0\n");
1854 errCode = usb_set_interface(dev->udev, 0, 0);
1855 if (errCode < 0) {
1856 em28xx_errdev("cannot change alternate number to "
1857 "0 (error=%i)\n", errCode);
1858 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001859 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001860 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001861 dev->users--;
1862 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001863 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001864 return 0;
1865}
1866
1867/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001868 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001869 * will allocate buffers when called for the first time
1870 */
1871static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001872em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001873 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001874{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001875 struct em28xx_fh *fh = filp->private_data;
1876 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001877 int rc;
1878
1879 rc = check_dev(dev);
1880 if (rc < 0)
1881 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001882
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001883 /* FIXME: read() is not prepared to allow changing the video
1884 resolution while streaming. Seems a bug at em28xx_set_fmt
1885 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001886
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001887 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1888 if (unlikely(res_get(fh)))
1889 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001890
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001891 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1892 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001893 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001894 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001895}
1896
1897/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001898 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001899 * will allocate buffers when called for the first time
1900 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001901static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001903 struct em28xx_fh *fh = filp->private_data;
1904 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001905 int rc;
1906
1907 rc = check_dev(dev);
1908 if (rc < 0)
1909 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001911 if (unlikely(res_get(fh) < 0))
1912 return POLLERR;
1913
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001914 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1915 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001916
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001917 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001918}
1919
1920/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001921 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001922 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001923static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001924{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001925 struct em28xx_fh *fh = filp->private_data;
1926 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001927 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001928
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001929 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001930 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001931
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001932 rc = check_dev(dev);
1933 if (rc < 0)
1934 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001935
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001936 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001937
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001938 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1939 (unsigned long)vma->vm_start,
1940 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1941 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001942
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001943 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001944}
1945
Arjan van de Venfa027c22007-02-12 00:55:33 -08001946static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001947 .owner = THIS_MODULE,
1948 .open = em28xx_v4l2_open,
1949 .release = em28xx_v4l2_close,
1950 .read = em28xx_v4l2_read,
1951 .poll = em28xx_v4l2_poll,
1952 .mmap = em28xx_v4l2_mmap,
1953 .ioctl = video_ioctl2,
1954 .llseek = no_llseek,
1955 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001956};
1957
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001958static const struct file_operations radio_fops = {
1959 .owner = THIS_MODULE,
1960 .open = em28xx_v4l2_open,
1961 .release = em28xx_v4l2_close,
1962 .ioctl = video_ioctl2,
1963 .compat_ioctl = v4l_compat_ioctl32,
1964 .llseek = no_llseek,
1965};
1966
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001967static const struct video_device em28xx_video_template = {
1968 .fops = &em28xx_v4l_fops,
1969 .release = video_device_release,
1970
1971 .minor = -1,
1972 .vidioc_querycap = vidioc_querycap,
1973 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1974 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1975 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1976 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1977 .vidioc_g_audio = vidioc_g_audio,
1978 .vidioc_s_audio = vidioc_s_audio,
1979 .vidioc_cropcap = vidioc_cropcap,
1980
1981 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1982 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1983 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1984
1985 .vidioc_reqbufs = vidioc_reqbufs,
1986 .vidioc_querybuf = vidioc_querybuf,
1987 .vidioc_qbuf = vidioc_qbuf,
1988 .vidioc_dqbuf = vidioc_dqbuf,
1989 .vidioc_s_std = vidioc_s_std,
1990 .vidioc_enum_input = vidioc_enum_input,
1991 .vidioc_g_input = vidioc_g_input,
1992 .vidioc_s_input = vidioc_s_input,
1993 .vidioc_queryctrl = vidioc_queryctrl,
1994 .vidioc_g_ctrl = vidioc_g_ctrl,
1995 .vidioc_s_ctrl = vidioc_s_ctrl,
1996 .vidioc_streamon = vidioc_streamon,
1997 .vidioc_streamoff = vidioc_streamoff,
1998 .vidioc_g_tuner = vidioc_g_tuner,
1999 .vidioc_s_tuner = vidioc_s_tuner,
2000 .vidioc_g_frequency = vidioc_g_frequency,
2001 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002002#ifdef CONFIG_VIDEO_ADV_DEBUG
2003 .vidioc_g_register = vidioc_g_register,
2004 .vidioc_s_register = vidioc_s_register,
2005#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002006#ifdef CONFIG_VIDEO_V4L1_COMPAT
2007 .vidiocgmbuf = vidiocgmbuf,
2008#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002009
2010 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002011 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002012};
2013
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002014static struct video_device em28xx_radio_template = {
2015 .name = "em28xx-radio",
2016 .type = VID_TYPE_TUNER,
2017 .fops = &radio_fops,
2018 .minor = -1,
2019 .vidioc_querycap = radio_querycap,
2020 .vidioc_g_tuner = radio_g_tuner,
2021 .vidioc_enum_input = radio_enum_input,
2022 .vidioc_g_audio = radio_g_audio,
2023 .vidioc_s_tuner = radio_s_tuner,
2024 .vidioc_s_audio = radio_s_audio,
2025 .vidioc_s_input = radio_s_input,
2026 .vidioc_queryctrl = radio_queryctrl,
2027 .vidioc_g_ctrl = vidioc_g_ctrl,
2028 .vidioc_s_ctrl = vidioc_s_ctrl,
2029 .vidioc_g_frequency = vidioc_g_frequency,
2030 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002031#ifdef CONFIG_VIDEO_ADV_DEBUG
2032 .vidioc_g_register = vidioc_g_register,
2033 .vidioc_s_register = vidioc_s_register,
2034#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002035};
2036
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002037/******************************** usb interface *****************************************/
2038
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002039
2040static LIST_HEAD(em28xx_extension_devlist);
2041static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2042
2043int em28xx_register_extension(struct em28xx_ops *ops)
2044{
2045 struct em28xx *h, *dev = NULL;
2046
2047 list_for_each_entry(h, &em28xx_devlist, devlist)
2048 dev = h;
2049
2050 mutex_lock(&em28xx_extension_devlist_lock);
2051 list_add_tail(&ops->next, &em28xx_extension_devlist);
2052 if (dev)
2053 ops->init(dev);
2054
2055 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2056 mutex_unlock(&em28xx_extension_devlist_lock);
2057
2058 return 0;
2059}
2060EXPORT_SYMBOL(em28xx_register_extension);
2061
2062void em28xx_unregister_extension(struct em28xx_ops *ops)
2063{
2064 struct em28xx *h, *dev = NULL;
2065
2066 list_for_each_entry(h, &em28xx_devlist, devlist)
2067 dev = h;
2068
2069 if (dev)
2070 ops->fini(dev);
2071
2072 mutex_lock(&em28xx_extension_devlist_lock);
2073 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2074 list_del(&ops->next);
2075 mutex_unlock(&em28xx_extension_devlist_lock);
2076}
2077EXPORT_SYMBOL(em28xx_unregister_extension);
2078
Adrian Bunk532fe652008-01-28 22:10:48 -03002079static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2080 const struct video_device *template,
2081 const int type,
2082 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002083{
2084 struct video_device *vfd;
2085
2086 vfd = video_device_alloc();
2087 if (NULL == vfd)
2088 return NULL;
2089 *vfd = *template;
2090 vfd->minor = -1;
2091 vfd->dev = &dev->udev->dev;
2092 vfd->release = video_device_release;
2093 vfd->type = type;
2094
2095 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2096 dev->name, type_name);
2097
2098 return vfd;
2099}
2100
2101
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002103 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002104 * allocates and inits the device structs, registers i2c bus and v4l device
2105 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002106static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002107 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002108{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002109 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002110 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002111 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002112 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002113 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002114
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002115 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002116 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002117 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002119 init_waitqueue_head(&dev->wait_frame);
2120 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002121
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002122 dev->em28xx_write_regs = em28xx_write_regs;
2123 dev->em28xx_read_reg = em28xx_read_reg;
2124 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2125 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2126 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002127 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002129 errCode = em28xx_read_reg(dev, CHIPID_REG);
2130 if (errCode >= 0)
2131 em28xx_info("em28xx chip ID = %d\n", errCode);
2132
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002133 em28xx_pre_card_setup(dev);
2134
2135 errCode = em28xx_config(dev);
2136 if (errCode) {
2137 em28xx_errdev("error configuring device\n");
2138 em28xx_devused &= ~(1<<dev->devno);
2139 kfree(dev);
2140 return -ENOMEM;
2141 }
2142
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002143 /* register i2c bus */
2144 em28xx_i2c_register(dev);
2145
2146 /* Do board specific init and eeprom reading */
2147 em28xx_card_setup(dev);
2148
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002149 /* Configure audio */
2150 em28xx_audio_analog_set(dev);
2151
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002152 /* configure the device */
2153 em28xx_config_i2c(dev);
2154
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002155 /* set default norm */
2156 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002157
2158 maxw = norm_maxw(dev);
2159 maxh = norm_maxh(dev);
2160
2161 /* set default image size */
2162 dev->width = maxw;
2163 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002164 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002165 dev->field_size = dev->width * dev->height;
2166 dev->frame_size =
2167 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2168 dev->bytesperline = dev->width * 2;
2169 dev->hscale = 0;
2170 dev->vscale = 0;
2171 dev->ctl_input = 2;
2172
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002173 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002175 list_add_tail(&dev->devlist, &em28xx_devlist);
2176
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002177 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002178 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2179 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002180 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002181 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002182 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03002184 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002185 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002186
2187 /* register v4l2 video video_device */
2188 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2189 video_nr[dev->devno]);
2190 if (retval) {
2191 em28xx_errdev("unable to register video device (error=%i).\n",
2192 retval);
2193 goto fail_unreg;
2194 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002195
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002196 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002197 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2198 VFL_TYPE_VBI, "vbi");
2199 /* register v4l2 vbi video_device */
2200 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2201 vbi_nr[dev->devno]) < 0) {
2202 em28xx_errdev("unable to register vbi device\n");
2203 retval = -ENODEV;
2204 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002205 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002206
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002207 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2208 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2209 VFL_TYPE_RADIO, "radio");
2210 if (NULL == dev->radio_dev) {
2211 em28xx_errdev("cannot allocate video_device.\n");
2212 goto fail_unreg;
2213 }
2214 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2215 radio_nr[dev->devno]);
2216 if (retval < 0) {
2217 em28xx_errdev("can't register radio device\n");
2218 goto fail_unreg;
2219 }
2220 em28xx_info("Registered radio device as /dev/radio%d\n",
2221 dev->radio_dev->minor & 0x1f);
2222 }
2223
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002224 /* init video dma queues */
2225 INIT_LIST_HEAD(&dev->vidq.active);
2226 INIT_LIST_HEAD(&dev->vidq.queued);
2227
2228 dev->vidq.timeout.function = em28xx_vid_timeout;
2229 dev->vidq.timeout.data = (unsigned long)dev;
2230 init_timer(&dev->vidq.timeout);
2231
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002232
Sascha Sommer5a804152007-11-03 21:22:38 -03002233 if (dev->has_msp34xx) {
2234 /* Send a reset to other chips via gpio */
2235 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2236 msleep(3);
2237 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2238 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002239 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002240
Sascha Sommer5a804152007-11-03 21:22:38 -03002241 video_mux(dev, 0);
2242
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002243 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2244 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2245 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002246
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002247 mutex_lock(&em28xx_extension_devlist_lock);
2248 if (!list_empty(&em28xx_extension_devlist)) {
2249 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2250 if (ops->id)
2251 ops->init(dev);
2252 }
2253 }
2254 mutex_unlock(&em28xx_extension_devlist_lock);
2255
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002256 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002257
2258fail_unreg:
2259 em28xx_release_resources(dev);
2260 mutex_unlock(&dev->lock);
2261 kfree(dev);
2262 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002263}
2264
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002265#if defined(CONFIG_MODULES) && defined(MODULE)
2266static void request_module_async(struct work_struct *work)
2267{
2268 struct em28xx *dev = container_of(work,
2269 struct em28xx, request_module_wk);
2270
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002271 if (dev->has_audio_class)
2272 request_module("snd-usb-audio");
2273 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002274 request_module("em28xx-alsa");
2275}
2276
2277static void request_modules(struct em28xx *dev)
2278{
2279 INIT_WORK(&dev->request_module_wk, request_module_async);
2280 schedule_work(&dev->request_module_wk);
2281}
2282#else
2283#define request_modules(dev)
2284#endif /* CONFIG_MODULES */
2285
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002286/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288 * checks for supported devices
2289 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002290static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002291 const struct usb_device_id *id)
2292{
2293 const struct usb_endpoint_descriptor *endpoint;
2294 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002295 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002296 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002297 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002298 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002299
2300 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002301 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2302
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002303 /* Check to see next free device and mark as used */
2304 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2305 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002306
2307 /* Don't register audio interfaces */
2308 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002309 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002310 udev->descriptor.idVendor,udev->descriptor.idProduct,
2311 ifnum,
2312 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002313
2314 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002315 return -ENODEV;
2316 }
2317
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002318 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002319 udev->descriptor.idVendor,udev->descriptor.idProduct,
2320 ifnum,
2321 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002322
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002323 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2324
Michael Opdenacker59c51592007-05-09 08:57:56 +02002325 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002326 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2327 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002328 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002329 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002330 return -ENODEV;
2331 }
2332 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002333 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002334 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002335 return -ENODEV;
2336 }
2337
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002338 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002339 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002340 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002341 return -ENOMEM;
2342 }
2343
2344 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002345 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002346 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002347 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002348 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002349 return -ENOMEM;
2350 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002351
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002352 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002353 dev->devno = nr;
2354 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002355 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002356
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002357 /* Checks if audio is provided by some interface */
2358 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2359 uif = udev->config->interface[i];
2360 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2361 dev->has_audio_class = 1;
2362 break;
2363 }
2364 }
2365
2366 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2367 dev->has_audio_class ? "Has" : "Doesn't have");
2368
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002369 /* compute alternate max packet sizes */
2370 uif = udev->actconfig->interface[0];
2371
2372 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002373 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002374// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2375 dev->alt_max_pkt_size = kmalloc(32*
2376 dev->num_alt,GFP_KERNEL);
2377 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002378 em28xx_errdev("out of memory!\n");
2379 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002380 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002381 return -ENOMEM;
2382 }
2383
2384 for (i = 0; i < dev->num_alt ; i++) {
2385 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2386 wMaxPacketSize);
2387 dev->alt_max_pkt_size[i] =
2388 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002389 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002390 dev->alt_max_pkt_size[i]);
2391 }
2392
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002393 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002394 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002395
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002396 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002397 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002398 if (retval)
2399 return retval;
2400
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002401 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002402
2403 /* save our data pointer in this interface device */
2404 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002405
2406 request_modules(dev);
2407
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002408 return 0;
2409}
2410
2411/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002412 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413 * called when the device gets diconencted
2414 * video device will be unregistered on v4l2_close in case it is still open
2415 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002416static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002417{
Sascha Sommer5a804152007-11-03 21:22:38 -03002418 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002419 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002420
2421 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002422 usb_set_intfdata(interface, NULL);
2423
2424 if (!dev)
2425 return;
2426
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002427 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002428
Sascha Sommer5a804152007-11-03 21:22:38 -03002429 /* wait until all current v4l2 io is finished then deallocate resources */
2430 mutex_lock(&dev->lock);
2431
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002432 wake_up_interruptible_all(&dev->open);
2433
2434 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002435 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002436 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002437 "deallocation are deferred on close.\n",
2438 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2439
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002440 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002441 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002442 dev->state |= DEV_DISCONNECTED;
2443 wake_up_interruptible(&dev->wait_frame);
2444 wake_up_interruptible(&dev->wait_stream);
2445 } else {
2446 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002447 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002448 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002449 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002450
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002451 mutex_lock(&em28xx_extension_devlist_lock);
2452 if (!list_empty(&em28xx_extension_devlist)) {
2453 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2454 ops->fini(dev);
2455 }
2456 }
2457 mutex_unlock(&em28xx_extension_devlist_lock);
2458
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002459 if (!dev->users) {
2460 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002461 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002462 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002463}
2464
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002465static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002466 .name = "em28xx",
2467 .probe = em28xx_usb_probe,
2468 .disconnect = em28xx_usb_disconnect,
2469 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002470};
2471
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002472static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002473{
2474 int result;
2475
2476 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002477 (EM28XX_VERSION_CODE >> 16) & 0xff,
2478 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002479#ifdef SNAPSHOT
2480 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2481 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2482#endif
2483
2484 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002485 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002486 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002487 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002488 " usb_register failed. Error number %d.\n", result);
2489
2490 return result;
2491}
2492
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002493static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002494{
2495 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002496 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002497}
2498
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002499module_init(em28xx_module_init);
2500module_exit(em28xx_module_exit);