blob: e2d04cf4ba9d8e6b5be7c2a1b84a8b380808c46d [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 /* Advice that buffer was filled */
139 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
140 buf->vb.state = VIDEOBUF_DONE;
141 buf->vb.field_count++;
142 do_gettimeofday(&buf->vb.ts);
143
144 list_del(&buf->vb.queue);
145 wake_up(&buf->vb.done);
146}
147
148/*
149 * Identify the buffer header type and properly handles
150 */
151static void em28xx_copy_video(struct em28xx *dev,
152 struct em28xx_dmaqueue *dma_q,
153 struct em28xx_buffer *buf,
154 unsigned char *p,
155 unsigned char *outp, unsigned long len)
156{
157 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300158 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300160 if (dev->frame_size != buf->vb.size) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300161 em28xx_errdev("size %i and buf.length %lu are different!\n",
162 dev->frame_size, buf->vb.size);
163 return;
164 }
165
166 if (dma_q->pos + len > buf->vb.size)
167 len = buf->vb.size - dma_q->pos;
168
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300169 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300170 em28xx_isocdbg("frame is not complete\n");
171 len += 4;
172 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300173 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300174
175 startread = p;
176 remain = len;
177
178 /* Interlaces frame */
179 if (buf->top_field)
180 fieldstart = outp;
181 else
182 fieldstart = outp + dev->bytesperline;
183
184 linesdone = dma_q->pos / dev->bytesperline;
185 currlinedone = dma_q->pos % dev->bytesperline;
186 offset = linesdone * dev->bytesperline * 2 + currlinedone;
187 startwrite = fieldstart + offset;
188 lencopy = dev->bytesperline - currlinedone;
189 lencopy = lencopy > remain ? remain : lencopy;
190
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300191 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300192 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300193 ((char *)startwrite + lencopy) -
194 ((char *)outp + buf->vb.size));
195 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300196 }
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300197 if (lencopy <= 0)
198 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300199 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200
201 remain -= lencopy;
202
203 while (remain > 0) {
204 startwrite += lencopy + dev->bytesperline;
205 startread += lencopy;
206 if (dev->bytesperline > remain)
207 lencopy = remain;
208 else
209 lencopy = dev->bytesperline;
210
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300211 BUG_ON(lencopy <= 0);
212
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300214 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300215 ((char *)startwrite + lencopy) -
216 ((char *)outp + buf->vb.size));
217 lencopy = remain = (char *)outp + buf->vb.size -
218 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300219 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300220 if (lencopy <= 0)
221 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300222
223 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224
225 remain -= lencopy;
226 }
227
228 dma_q->pos += len;
229}
230
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300231static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300232 int packet, int status)
233{
234 char *errmsg = "Unknown";
235
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300236 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300237 case -ENOENT:
238 errmsg = "unlinked synchronuously";
239 break;
240 case -ECONNRESET:
241 errmsg = "unlinked asynchronuously";
242 break;
243 case -ENOSR:
244 errmsg = "Buffer error (overrun)";
245 break;
246 case -EPIPE:
247 errmsg = "Stalled (device not responding)";
248 break;
249 case -EOVERFLOW:
250 errmsg = "Babble (bad cable?)";
251 break;
252 case -EPROTO:
253 errmsg = "Bit-stuff error (bad cable?)";
254 break;
255 case -EILSEQ:
256 errmsg = "CRC/Timeout (could be anything)";
257 break;
258 case -ETIME:
259 errmsg = "Device does not respond";
260 break;
261 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300262 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300263 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
264 } else {
265 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
266 packet, status, errmsg);
267 }
268}
269
270/*
271 * video-buf generic routine to get the next available buffer
272 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300273static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300274 struct em28xx_buffer **buf)
275{
276 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
277
278 if (list_empty(&dma_q->active)) {
279 em28xx_isocdbg("No active queue to serve\n");
280 return 0;
281 }
282
283 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
284
285 return 1;
286}
287
288/*
289 * Controls the isoc copy of each urb packet
290 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300291static inline int em28xx_isoc_copy(struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300292{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300293 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300294 struct em28xx_dmaqueue *dma_q = urb->context;
295 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300296 unsigned char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300298 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299
300 if (!dev)
301 return 0;
302
303 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
304 return 0;
305
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300306 if (urb->status < 0) {
307 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300308 if (urb->status == -ENOENT)
309 return 0;
310 }
311
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300312 buf = dev->isoc_ctl.buf;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300313
314 if (!buf) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300315 rc = get_next_buf(dma_q, &buf);
316 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300317 return rc;
318 }
319
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300320 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300321
322
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 for (i = 0; i < urb->number_of_packets; i++) {
324 int status = urb->iso_frame_desc[i].status;
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 if (status < 0) {
327 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328 if (urb->iso_frame_desc[i].status != -EPROTO)
329 continue;
330 }
331
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300332 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300333
334 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300335 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300336 continue;
337 }
338 if (urb->iso_frame_desc[i].actual_length >
339 dev->max_pkt_size) {
340 em28xx_isocdbg("packet bigger than packet size");
341 continue;
342 }
343
344 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300345
346 /* FIXME: incomplete buffer checks where removed to make
347 logic simpler. Impacts of those changes should be evaluated
348 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300349 if (p[0] == 0x22 && p[1] == 0x5a) {
350 /* FIXME - are the fields the right way around? */
351 em28xx_isocdbg("Video frame, length=%i, %s\n", len,
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300352 (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300353 em28xx_isocdbg("Current buffer is: outp = 0x%p,"
354 " len = %i\n", outp, (int)buf->vb.size);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300355
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300356 if (p[2] & 1)
357 buf->top_field = 0;
358 else {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300359 if (buf->receiving) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300360 buffer_filled(dev, dma_q, buf);
361 rc = get_next_buf(dma_q, &buf);
362 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300363 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300364
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300365 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300366 }
367
368 buf->top_field = 1;
Aidan Thorntone0fadfd2008-04-13 14:56:02 -0300369 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300370 buf->receiving = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371 dma_q->pos = 0;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300372 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300373 em28xx_isocdbg("VBI HEADER!!!\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300374 }
375
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300376 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300377
378 /* FIXME: Should add vbi copy */
379 }
380 return rc;
381}
382
383/* ------------------------------------------------------------------
384 URB control
385 ------------------------------------------------------------------*/
386
387/*
388 * IRQ callback, called by URB callback
389 */
390static void em28xx_irq_callback(struct urb *urb)
391{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300392 struct em28xx_dmaqueue *dma_q = urb->context;
393 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300394 int rc, i;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300395 unsigned long flags;
396
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300397 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300398
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300399 /* Copy data from URB */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300400 rc = em28xx_isoc_copy(urb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402 /* Reset urb buffers */
403 for (i = 0; i < urb->number_of_packets; i++) {
404 urb->iso_frame_desc[i].status = 0;
405 urb->iso_frame_desc[i].actual_length = 0;
406 }
407 urb->status = 0;
408
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300409 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
410 if (urb->status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 em28xx_err("urb resubmit failed (error=%i)\n",
412 urb->status);
413 }
414
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300415 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300416}
417
418/*
419 * Stop and Deallocate URBs
420 */
421static void em28xx_uninit_isoc(struct em28xx *dev)
422{
423 struct urb *urb;
424 int i;
425
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300426 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
427
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300428 dev->isoc_ctl.nfields = -1;
429 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300430 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300431 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300432 if (urb) {
433 usb_kill_urb(urb);
434 usb_unlink_urb(urb);
435 if (dev->isoc_ctl.transfer_buffer[i]) {
436 usb_buffer_free(dev->udev,
437 urb->transfer_buffer_length,
438 dev->isoc_ctl.transfer_buffer[i],
439 urb->transfer_dma);
440 }
441 usb_free_urb(urb);
442 dev->isoc_ctl.urb[i] = NULL;
443 }
444 dev->isoc_ctl.transfer_buffer[i] = NULL;
445 }
446
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300447 kfree(dev->isoc_ctl.urb);
448 kfree(dev->isoc_ctl.transfer_buffer);
449 dev->isoc_ctl.urb = NULL;
450 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300452 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300453
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300454 del_timer(&dev->vidq.timeout);
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300455 em28xx_capture_start(dev, 0);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456}
457
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458
459/*
460 * Allocate URBs and start IRQ
461 */
462static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
463 int num_bufs)
464{
465 struct em28xx_dmaqueue *dma_q = &dev->vidq;
466 int i;
467 int sb_size, pipe;
468 struct urb *urb;
469 int j, k;
470
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300471 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
472
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473 /* De-allocates all pending stuff */
474 em28xx_uninit_isoc(dev);
475
476 dev->isoc_ctl.num_bufs = num_bufs;
477
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300478 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300479 if (!dev->isoc_ctl.urb) {
480 em28xx_errdev("cannot alloc memory for usb buffers\n");
481 return -ENOMEM;
482 }
483
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300484 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485 GFP_KERNEL);
486 if (!dev->isoc_ctl.urb) {
487 em28xx_errdev("cannot allocate memory for usbtransfer\n");
488 kfree(dev->isoc_ctl.urb);
489 return -ENOMEM;
490 }
491
492 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
493
494 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
495
496 /* allocate urbs and transfer buffers */
497 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
498 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
499 if (!urb) {
500 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
501 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300502 return -ENOMEM;
503 }
504 dev->isoc_ctl.urb[i] = urb;
505
506 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
507 sb_size, GFP_KERNEL, &urb->transfer_dma);
508 if (!dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300509 em28xx_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300510 " buffer %i%s\n",
511 sb_size, i,
512 in_interrupt()?" while in int":"");
513 em28xx_uninit_isoc(dev);
514 return -ENOMEM;
515 }
516 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
517
518 /* FIXME: this is a hack - should be
519 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
520 should also be using 'desc.bInterval'
521 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300522 pipe = usb_rcvisocpipe(dev->udev, 0x82);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300523 usb_fill_int_urb(urb, dev->udev, pipe,
524 dev->isoc_ctl.transfer_buffer[i], sb_size,
525 em28xx_irq_callback, dma_q, 1);
526
527 urb->number_of_packets = max_packets;
528 urb->transfer_flags = URB_ISO_ASAP;
529
530 k = 0;
531 for (j = 0; j < max_packets; j++) {
532 urb->iso_frame_desc[j].offset = k;
533 urb->iso_frame_desc[j].length =
534 dev->isoc_ctl.max_pkt_size;
535 k += dev->isoc_ctl.max_pkt_size;
536 }
537 }
538
539 return 0;
540}
541
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300542static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300543{
544 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300545 int i, rc = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300546
547 em28xx_videodbg("Called em28xx_start_thread\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300548
549 init_waitqueue_head(&dma_q->wq);
550
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300551 em28xx_capture_start(dev, 1);
552
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300553 /* submit urbs and enables IRQ */
554 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
555 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
556 if (rc) {
557 em28xx_err("submit of urb %i failed (error=%i)\n", i,
558 rc);
559 em28xx_uninit_isoc(dev);
560 return rc;
561 }
562 }
563
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300564 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300565 return rc;
566
567 return 0;
568}
569
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300570static void em28xx_vid_timeout(unsigned long data)
571{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300572 struct em28xx *dev = (struct em28xx *)data;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300573 struct em28xx_dmaqueue *vidq = &dev->vidq;
574 struct em28xx_buffer *buf;
575 unsigned long flags;
576
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300577 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300578
579 list_for_each_entry(buf, vidq->active.next, vb.queue) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300580 list_del(&buf->vb.queue);
581 buf->vb.state = VIDEOBUF_ERROR;
582 wake_up(&buf->vb.done);
583 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
584 buf, buf->vb.i);
585 }
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300586 /* Instead of trying to restart, just sets timeout again */
587 mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300588
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300589 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300590}
591
592/* ------------------------------------------------------------------
593 Videobuf operations
594 ------------------------------------------------------------------*/
595
596static int
597buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
598{
599 struct em28xx_fh *fh = vq->priv_data;
600
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300601 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300602 if (0 == *count)
603 *count = EM28XX_DEF_BUF;
604
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300605 if (*count < EM28XX_MIN_BUF)
606 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300607
608 return 0;
609}
610
611static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
612{
613 if (in_interrupt())
614 BUG();
615
616 videobuf_waiton(&buf->vb, 0, 0);
617 videobuf_vmalloc_free(&buf->vb);
618 buf->vb.state = VIDEOBUF_NEEDS_INIT;
619}
620
621static int
622buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
623 enum v4l2_field field)
624{
625 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300626 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300627 struct em28xx *dev = fh->dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300628 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300629 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300630
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300631 /* BUG_ON(NULL == fh->fmt); */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300632
633 /* FIXME: It assumes depth = 16 */
634 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300635 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636
637 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
638 return -EINVAL;
639
640 if (buf->fmt != fh->fmt ||
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300641 buf->vb.width != dev->width ||
642 buf->vb.height != dev->height ||
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300643 buf->vb.field != field) {
644 buf->fmt = fh->fmt;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300645 buf->vb.width = dev->width;
646 buf->vb.height = dev->height;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300647 buf->vb.field = field;
648 buf->vb.state = VIDEOBUF_NEEDS_INIT;
649 }
650
651 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300652 rc = videobuf_iolock(vq, &buf->vb, NULL);
653 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300654 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300655 }
656
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300657 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300658 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300659
660 if (urb_init) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300661 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
662 EM28XX_NUM_BUFS);
663 if (rc < 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300664 goto fail;
665
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300666 rc = em28xx_start_thread(vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300667 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300668 goto fail;
669 }
670
671 buf->vb.state = VIDEOBUF_PREPARED;
672 return 0;
673
674fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300675 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300676 return rc;
677}
678
679static void
680buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
681{
682 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
683 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300684 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300685 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300686
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300687 buf->vb.state = VIDEOBUF_QUEUED;
688 list_add_tail(&buf->vb.queue, &vidq->active);
689
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300690}
691
692static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
693{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300694 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300695 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300696 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300697
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300698 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300699
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300700 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300701}
702
703static struct videobuf_queue_ops em28xx_video_qops = {
704 .buf_setup = buffer_setup,
705 .buf_prepare = buffer_prepare,
706 .buf_queue = buffer_queue,
707 .buf_release = buffer_release,
708};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800709
710/********************* v4l2 interface ******************************************/
711
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800712/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800713 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800714 * inits registers with sane defaults
715 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800716static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800717{
718
719 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300720 if (!dev->is_em2800)
721 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800722
723 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200724
Markus Rechberger9475fb12006-02-27 00:07:34 -0300725/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
726/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200727 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
728
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800729 dev->mute = 1; /* maybe not the right place... */
730 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300731
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800732 em28xx_outfmt_set_yuv422(dev);
733 em28xx_colorlevels_set_default(dev);
734 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800735
736 return 0;
737}
738
739/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800740 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800741 * configure i2c attached devices
742 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800743static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800744{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300745 struct v4l2_routing route;
746
747 route.input = INPUT(dev->ctl_input)->vmux;
748 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100749 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300750 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300751 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800752}
753
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800754static void video_mux(struct em28xx *dev, int index)
755{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300756 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800757
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300758 route.input = INPUT(index)->vmux;
759 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800760 dev->ctl_input = index;
761 dev->ctl_ainput = INPUT(index)->amux;
762
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300763 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800764
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800765 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200766 if (dev->i2s_speed)
767 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300768 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300769 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300770 /* Note: this is msp3400 specific */
771 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800772 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300773
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300774 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800775}
776
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300777/* Usage lock check functions */
778static int res_get(struct em28xx_fh *fh)
779{
780 struct em28xx *dev = fh->dev;
781 int rc = 0;
782
783 /* This instance already has stream_on */
784 if (fh->stream_on)
785 return rc;
786
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300787 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300788 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300789
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300790 mutex_lock(&dev->lock);
791 dev->stream_on = 1;
792 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300793 mutex_unlock(&dev->lock);
794 return rc;
795}
796
797static int res_check(struct em28xx_fh *fh)
798{
799 return (fh->stream_on);
800}
801
802static void res_free(struct em28xx_fh *fh)
803{
804 struct em28xx *dev = fh->dev;
805
806 mutex_lock(&dev->lock);
807 fh->stream_on = 0;
808 dev->stream_on = 0;
809 mutex_unlock(&dev->lock);
810}
811
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800812/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813 * em28xx_get_ctrl()
814 * return the current saturation, brightness or contrast, mute state
815 */
816static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
817{
818 switch (ctrl->id) {
819 case V4L2_CID_AUDIO_MUTE:
820 ctrl->value = dev->mute;
821 return 0;
822 case V4L2_CID_AUDIO_VOLUME:
823 ctrl->value = dev->volume;
824 return 0;
825 default:
826 return -EINVAL;
827 }
828}
829
830/*
831 * em28xx_set_ctrl()
832 * mute or set new saturation, brightness or contrast
833 */
834static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
835{
836 switch (ctrl->id) {
837 case V4L2_CID_AUDIO_MUTE:
838 if (ctrl->value != dev->mute) {
839 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300840 return em28xx_audio_analog_set(dev);
841 }
842 return 0;
843 case V4L2_CID_AUDIO_VOLUME:
844 dev->volume = ctrl->value;
845 return em28xx_audio_analog_set(dev);
846 default:
847 return -EINVAL;
848 }
849}
850
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300851static int check_dev(struct em28xx *dev)
852{
853 if (dev->state & DEV_DISCONNECTED) {
854 em28xx_errdev("v4l2 ioctl: device not present\n");
855 return -ENODEV;
856 }
857
858 if (dev->state & DEV_MISCONFIGURED) {
859 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
860 "close and open it again\n");
861 return -EIO;
862 }
863 return 0;
864}
865
866static void get_scale(struct em28xx *dev,
867 unsigned int width, unsigned int height,
868 unsigned int *hscale, unsigned int *vscale)
869{
870 unsigned int maxw = norm_maxw(dev);
871 unsigned int maxh = norm_maxh(dev);
872
873 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
874 if (*hscale >= 0x4000)
875 *hscale = 0x3fff;
876
877 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
878 if (*vscale >= 0x4000)
879 *vscale = 0x3fff;
880}
881
882/* ------------------------------------------------------------------
883 IOCTL vidioc handling
884 ------------------------------------------------------------------*/
885
886static int vidioc_g_fmt_cap(struct file *file, void *priv,
887 struct v4l2_format *f)
888{
889 struct em28xx_fh *fh = priv;
890 struct em28xx *dev = fh->dev;
891
892 mutex_lock(&dev->lock);
893
894 f->fmt.pix.width = dev->width;
895 f->fmt.pix.height = dev->height;
896 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
897 f->fmt.pix.bytesperline = dev->bytesperline;
898 f->fmt.pix.sizeimage = dev->frame_size;
899 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
900
901 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
902 f->fmt.pix.field = dev->interlaced ?
903 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
904
905 mutex_unlock(&dev->lock);
906 return 0;
907}
908
909static int vidioc_try_fmt_cap(struct file *file, void *priv,
910 struct v4l2_format *f)
911{
912 struct em28xx_fh *fh = priv;
913 struct em28xx *dev = fh->dev;
914 int width = f->fmt.pix.width;
915 int height = f->fmt.pix.height;
916 unsigned int maxw = norm_maxw(dev);
917 unsigned int maxh = norm_maxh(dev);
918 unsigned int hscale, vscale;
919
920 /* width must even because of the YUYV format
921 height must be even because of interlacing */
922 height &= 0xfffe;
923 width &= 0xfffe;
924
925 if (height < 32)
926 height = 32;
927 if (height > maxh)
928 height = maxh;
929 if (width < 48)
930 width = 48;
931 if (width > maxw)
932 width = maxw;
933
934 mutex_lock(&dev->lock);
935
936 if (dev->is_em2800) {
937 /* the em2800 can only scale down to 50% */
938 if (height % (maxh / 2))
939 height = maxh;
940 if (width % (maxw / 2))
941 width = maxw;
942 /* according to empiatech support */
943 /* the MaxPacketSize is to small to support */
944 /* framesizes larger than 640x480 @ 30 fps */
945 /* or 640x576 @ 25 fps. As this would cut */
946 /* of a part of the image we prefer */
947 /* 360x576 or 360x480 for now */
948 if (width == maxw && height == maxh)
949 width /= 2;
950 }
951
952 get_scale(dev, width, height, &hscale, &vscale);
953
954 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
955 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
956
957 f->fmt.pix.width = width;
958 f->fmt.pix.height = height;
959 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
960 f->fmt.pix.bytesperline = width * 2;
961 f->fmt.pix.sizeimage = width * 2 * height;
962 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
963 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
964
965 mutex_unlock(&dev->lock);
966 return 0;
967}
968
969static int vidioc_s_fmt_cap(struct file *file, void *priv,
970 struct v4l2_format *f)
971{
972 struct em28xx_fh *fh = priv;
973 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300974 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300975
976 rc = check_dev(dev);
977 if (rc < 0)
978 return rc;
979
980 vidioc_try_fmt_cap(file, priv, f);
981
982 mutex_lock(&dev->lock);
983
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300984 /* set new image size */
985 dev->width = f->fmt.pix.width;
986 dev->height = f->fmt.pix.height;
987 dev->frame_size = dev->width * dev->height * 2;
988 dev->field_size = dev->frame_size >> 1;
989 dev->bytesperline = dev->width * 2;
990 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
991
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300992 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300993 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300994
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300995 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300996 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997}
998
999static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
1000{
1001 struct em28xx_fh *fh = priv;
1002 struct em28xx *dev = fh->dev;
1003 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001004 int rc;
1005
1006 rc = check_dev(dev);
1007 if (rc < 0)
1008 return rc;
1009
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001010 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001011 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001012 mutex_unlock(&dev->lock);
1013
1014 /* Adjusts width/height, if needed */
1015 f.fmt.pix.width = dev->width;
1016 f.fmt.pix.height = dev->height;
1017 vidioc_try_fmt_cap(file, priv, &f);
1018
1019 mutex_lock(&dev->lock);
1020
1021 /* set new image size */
1022 dev->width = f.fmt.pix.width;
1023 dev->height = f.fmt.pix.height;
1024 dev->frame_size = dev->width * dev->height * 2;
1025 dev->field_size = dev->frame_size >> 1;
1026 dev->bytesperline = dev->width * 2;
1027 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1028
1029 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001030 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001031
1032 mutex_unlock(&dev->lock);
1033 return 0;
1034}
1035
1036static const char *iname[] = {
1037 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1038 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1039 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1040 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1041 [EM28XX_VMUX_SVIDEO] = "S-Video",
1042 [EM28XX_VMUX_TELEVISION] = "Television",
1043 [EM28XX_VMUX_CABLE] = "Cable TV",
1044 [EM28XX_VMUX_DVB] = "DVB",
1045 [EM28XX_VMUX_DEBUG] = "for debug only",
1046};
1047
1048static int vidioc_enum_input(struct file *file, void *priv,
1049 struct v4l2_input *i)
1050{
1051 struct em28xx_fh *fh = priv;
1052 struct em28xx *dev = fh->dev;
1053 unsigned int n;
1054
1055 n = i->index;
1056 if (n >= MAX_EM28XX_INPUT)
1057 return -EINVAL;
1058 if (0 == INPUT(n)->type)
1059 return -EINVAL;
1060
1061 i->index = n;
1062 i->type = V4L2_INPUT_TYPE_CAMERA;
1063
1064 strcpy(i->name, iname[INPUT(n)->type]);
1065
1066 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1067 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1068 i->type = V4L2_INPUT_TYPE_TUNER;
1069
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001070 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001071
1072 return 0;
1073}
1074
1075static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1076{
1077 struct em28xx_fh *fh = priv;
1078 struct em28xx *dev = fh->dev;
1079
1080 *i = dev->ctl_input;
1081
1082 return 0;
1083}
1084
1085static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1086{
1087 struct em28xx_fh *fh = priv;
1088 struct em28xx *dev = fh->dev;
1089 int rc;
1090
1091 rc = check_dev(dev);
1092 if (rc < 0)
1093 return rc;
1094
1095 if (i >= MAX_EM28XX_INPUT)
1096 return -EINVAL;
1097 if (0 == INPUT(i)->type)
1098 return -EINVAL;
1099
1100 mutex_lock(&dev->lock);
1101
1102 video_mux(dev, i);
1103
1104 mutex_unlock(&dev->lock);
1105 return 0;
1106}
1107
1108static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1109{
1110 struct em28xx_fh *fh = priv;
1111 struct em28xx *dev = fh->dev;
1112 unsigned int index = a->index;
1113
1114 if (a->index > 1)
1115 return -EINVAL;
1116
1117 index = dev->ctl_ainput;
1118
1119 if (index == 0) {
1120 strcpy(a->name, "Television");
1121 } else {
1122 strcpy(a->name, "Line In");
1123 }
1124 a->capability = V4L2_AUDCAP_STEREO;
1125 a->index = index;
1126
1127 return 0;
1128}
1129
1130static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1131{
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
1134
1135 if (a->index != dev->ctl_ainput)
1136 return -EINVAL;
1137
1138 return 0;
1139}
1140
1141static int vidioc_queryctrl(struct file *file, void *priv,
1142 struct v4l2_queryctrl *qc)
1143{
1144 struct em28xx_fh *fh = priv;
1145 struct em28xx *dev = fh->dev;
1146 int id = qc->id;
1147 int i;
1148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 memset(qc, 0, sizeof(*qc));
1155
1156 qc->id = id;
1157
1158 if (!dev->has_msp34xx) {
1159 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1160 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1161 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1162 return 0;
1163 }
1164 }
1165 }
1166 mutex_lock(&dev->lock);
1167 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1168 mutex_unlock(&dev->lock);
1169
1170 if (qc->type)
1171 return 0;
1172 else
1173 return -EINVAL;
1174}
1175
1176static int vidioc_g_ctrl(struct file *file, void *priv,
1177 struct v4l2_control *ctrl)
1178{
1179 struct em28xx_fh *fh = priv;
1180 struct em28xx *dev = fh->dev;
1181 int rc;
1182
1183 rc = check_dev(dev);
1184 if (rc < 0)
1185 return rc;
1186 mutex_lock(&dev->lock);
1187
1188 if (!dev->has_msp34xx)
1189 rc = em28xx_get_ctrl(dev, ctrl);
1190 else
1191 rc = -EINVAL;
1192
1193 if (rc == -EINVAL) {
1194 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1195 rc = 0;
1196 }
1197
1198 mutex_unlock(&dev->lock);
1199 return rc;
1200}
1201
1202static int vidioc_s_ctrl(struct file *file, void *priv,
1203 struct v4l2_control *ctrl)
1204{
1205 struct em28xx_fh *fh = priv;
1206 struct em28xx *dev = fh->dev;
1207 u8 i;
1208 int rc;
1209
1210 rc = check_dev(dev);
1211 if (rc < 0)
1212 return rc;
1213
1214 mutex_lock(&dev->lock);
1215
1216 if (dev->has_msp34xx)
1217 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1218 else {
1219 rc = 1;
1220 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1221 if (ctrl->id == em28xx_qctrl[i].id) {
1222 if (ctrl->value < em28xx_qctrl[i].minimum ||
1223 ctrl->value > em28xx_qctrl[i].maximum) {
1224 rc = -ERANGE;
1225 break;
1226 }
1227
1228 rc = em28xx_set_ctrl(dev, ctrl);
1229 break;
1230 }
1231 }
1232 }
1233
1234 /* Control not found - try to send it to the attached devices */
1235 if (rc == 1) {
1236 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1237 rc = 0;
1238 }
1239
1240 mutex_unlock(&dev->lock);
1241 return rc;
1242}
1243
1244static int vidioc_g_tuner(struct file *file, void *priv,
1245 struct v4l2_tuner *t)
1246{
1247 struct em28xx_fh *fh = priv;
1248 struct em28xx *dev = fh->dev;
1249 int rc;
1250
1251 rc = check_dev(dev);
1252 if (rc < 0)
1253 return rc;
1254
1255 if (0 != t->index)
1256 return -EINVAL;
1257
1258 strcpy(t->name, "Tuner");
1259
1260 mutex_lock(&dev->lock);
1261
1262 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1263
1264 mutex_unlock(&dev->lock);
1265 return 0;
1266}
1267
1268static int vidioc_s_tuner(struct file *file, void *priv,
1269 struct v4l2_tuner *t)
1270{
1271 struct em28xx_fh *fh = priv;
1272 struct em28xx *dev = fh->dev;
1273 int rc;
1274
1275 rc = check_dev(dev);
1276 if (rc < 0)
1277 return rc;
1278
1279 if (0 != t->index)
1280 return -EINVAL;
1281
1282 mutex_lock(&dev->lock);
1283
1284 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1285
1286 mutex_unlock(&dev->lock);
1287 return 0;
1288}
1289
1290static int vidioc_g_frequency(struct file *file, void *priv,
1291 struct v4l2_frequency *f)
1292{
1293 struct em28xx_fh *fh = priv;
1294 struct em28xx *dev = fh->dev;
1295
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001296 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297 f->frequency = dev->ctl_freq;
1298
1299 return 0;
1300}
1301
1302static int vidioc_s_frequency(struct file *file, void *priv,
1303 struct v4l2_frequency *f)
1304{
1305 struct em28xx_fh *fh = priv;
1306 struct em28xx *dev = fh->dev;
1307 int rc;
1308
1309 rc = check_dev(dev);
1310 if (rc < 0)
1311 return rc;
1312
1313 if (0 != f->tuner)
1314 return -EINVAL;
1315
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001316 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1317 return -EINVAL;
1318 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001319 return -EINVAL;
1320
1321 mutex_lock(&dev->lock);
1322
1323 dev->ctl_freq = f->frequency;
1324 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1325
1326 mutex_unlock(&dev->lock);
1327 return 0;
1328}
1329
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001330#ifdef CONFIG_VIDEO_ADV_DEBUG
1331static int em28xx_reg_len(int reg)
1332{
1333 switch (reg) {
1334 case AC97LSB_REG:
1335 case HSCALELOW_REG:
1336 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001337 return 2;
1338 default:
1339 return 1;
1340 }
1341}
1342
1343static int vidioc_g_register(struct file *file, void *priv,
1344 struct v4l2_register *reg)
1345{
1346 struct em28xx_fh *fh = priv;
1347 struct em28xx *dev = fh->dev;
1348 int ret;
1349
1350 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1351 return -EINVAL;
1352
1353 if (em28xx_reg_len(reg->reg) == 1) {
1354 ret = em28xx_read_reg(dev, reg->reg);
1355 if (ret < 0)
1356 return ret;
1357
1358 reg->val = ret;
1359 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001360 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001361 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1362 reg->reg, (char *)&val, 2);
1363 if (ret < 0)
1364 return ret;
1365
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001366 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001367 }
1368
1369 return 0;
1370}
1371
1372static int vidioc_s_register(struct file *file, void *priv,
1373 struct v4l2_register *reg)
1374{
1375 struct em28xx_fh *fh = priv;
1376 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001377 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001378
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001379 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001380
1381 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1382 em28xx_reg_len(reg->reg));
1383}
1384#endif
1385
1386
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001387static int vidioc_cropcap(struct file *file, void *priv,
1388 struct v4l2_cropcap *cc)
1389{
1390 struct em28xx_fh *fh = priv;
1391 struct em28xx *dev = fh->dev;
1392
1393 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 return -EINVAL;
1395
1396 cc->bounds.left = 0;
1397 cc->bounds.top = 0;
1398 cc->bounds.width = dev->width;
1399 cc->bounds.height = dev->height;
1400 cc->defrect = cc->bounds;
1401 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1402 cc->pixelaspect.denominator = 59;
1403
1404 return 0;
1405}
1406
1407static int vidioc_streamon(struct file *file, void *priv,
1408 enum v4l2_buf_type type)
1409{
1410 struct em28xx_fh *fh = priv;
1411 struct em28xx *dev = fh->dev;
1412 int rc;
1413
1414 rc = check_dev(dev);
1415 if (rc < 0)
1416 return rc;
1417
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001418
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001419 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001420 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001422 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001423}
1424
1425static int vidioc_streamoff(struct file *file, void *priv,
1426 enum v4l2_buf_type type)
1427{
1428 struct em28xx_fh *fh = priv;
1429 struct em28xx *dev = fh->dev;
1430 int rc;
1431
1432 rc = check_dev(dev);
1433 if (rc < 0)
1434 return rc;
1435
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001436 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1437 return -EINVAL;
1438 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001439 return -EINVAL;
1440
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001441 videobuf_streamoff(&fh->vb_vidq);
1442 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001443
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001444 return 0;
1445}
1446
1447static int vidioc_querycap(struct file *file, void *priv,
1448 struct v4l2_capability *cap)
1449{
1450 struct em28xx_fh *fh = priv;
1451 struct em28xx *dev = fh->dev;
1452
1453 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1454 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1455 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1456
1457 cap->version = EM28XX_VERSION_CODE;
1458
1459 cap->capabilities =
1460 V4L2_CAP_SLICED_VBI_CAPTURE |
1461 V4L2_CAP_VIDEO_CAPTURE |
1462 V4L2_CAP_AUDIO |
1463 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1464
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001465 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001466 cap->capabilities |= V4L2_CAP_TUNER;
1467
1468 return 0;
1469}
1470
1471static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1472 struct v4l2_fmtdesc *fmtd)
1473{
1474 if (fmtd->index != 0)
1475 return -EINVAL;
1476
1477 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1478 strcpy(fmtd->description, "Packed YUY2");
1479 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1480 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1481
1482 return 0;
1483}
1484
1485/* Sliced VBI ioctls */
1486static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1487 struct v4l2_format *f)
1488{
1489 struct em28xx_fh *fh = priv;
1490 struct em28xx *dev = fh->dev;
1491 int rc;
1492
1493 rc = check_dev(dev);
1494 if (rc < 0)
1495 return rc;
1496
1497 mutex_lock(&dev->lock);
1498
1499 f->fmt.sliced.service_set = 0;
1500
1501 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1502
1503 if (f->fmt.sliced.service_set == 0)
1504 rc = -EINVAL;
1505
1506 mutex_unlock(&dev->lock);
1507 return rc;
1508}
1509
1510static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1511 struct v4l2_format *f)
1512{
1513 struct em28xx_fh *fh = priv;
1514 struct em28xx *dev = fh->dev;
1515 int rc;
1516
1517 rc = check_dev(dev);
1518 if (rc < 0)
1519 return rc;
1520
1521 mutex_lock(&dev->lock);
1522 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1523 mutex_unlock(&dev->lock);
1524
1525 if (f->fmt.sliced.service_set == 0)
1526 return -EINVAL;
1527
1528 return 0;
1529}
1530
1531
1532static int vidioc_reqbufs(struct file *file, void *priv,
1533 struct v4l2_requestbuffers *rb)
1534{
1535 struct em28xx_fh *fh = priv;
1536 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001537 int rc;
1538
1539 rc = check_dev(dev);
1540 if (rc < 0)
1541 return rc;
1542
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001543 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001544}
1545
1546static int vidioc_querybuf(struct file *file, void *priv,
1547 struct v4l2_buffer *b)
1548{
1549 struct em28xx_fh *fh = priv;
1550 struct em28xx *dev = fh->dev;
1551 int rc;
1552
1553 rc = check_dev(dev);
1554 if (rc < 0)
1555 return rc;
1556
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001557 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001558}
1559
1560static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1561{
1562 struct em28xx_fh *fh = priv;
1563 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001564 int rc;
1565
1566 rc = check_dev(dev);
1567 if (rc < 0)
1568 return rc;
1569
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001570 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001571}
1572
1573static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1574{
1575 struct em28xx_fh *fh = priv;
1576 struct em28xx *dev = fh->dev;
1577 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001578
1579 rc = check_dev(dev);
1580 if (rc < 0)
1581 return rc;
1582
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001583 return (videobuf_dqbuf(&fh->vb_vidq, b,
1584 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001585}
1586
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001587#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001588static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001589{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001590 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001591
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001592 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001593}
1594#endif
1595
1596
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001597/* ----------------------------------------------------------- */
1598/* RADIO ESPECIFIC IOCTLS */
1599/* ----------------------------------------------------------- */
1600
1601static int radio_querycap(struct file *file, void *priv,
1602 struct v4l2_capability *cap)
1603{
1604 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1605
1606 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1607 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1608 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1609
1610 cap->version = EM28XX_VERSION_CODE;
1611 cap->capabilities = V4L2_CAP_TUNER;
1612 return 0;
1613}
1614
1615static int radio_g_tuner(struct file *file, void *priv,
1616 struct v4l2_tuner *t)
1617{
1618 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1619
1620 if (unlikely(t->index > 0))
1621 return -EINVAL;
1622
1623 strcpy(t->name, "Radio");
1624 t->type = V4L2_TUNER_RADIO;
1625
1626 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1627 return 0;
1628}
1629
1630static int radio_enum_input(struct file *file, void *priv,
1631 struct v4l2_input *i)
1632{
1633 if (i->index != 0)
1634 return -EINVAL;
1635 strcpy(i->name, "Radio");
1636 i->type = V4L2_INPUT_TYPE_TUNER;
1637
1638 return 0;
1639}
1640
1641static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1642{
1643 if (unlikely(a->index))
1644 return -EINVAL;
1645
1646 strcpy(a->name, "Radio");
1647 return 0;
1648}
1649
1650static int radio_s_tuner(struct file *file, void *priv,
1651 struct v4l2_tuner *t)
1652{
1653 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1654
1655 if (0 != t->index)
1656 return -EINVAL;
1657
1658 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1659
1660 return 0;
1661}
1662
1663static int radio_s_audio(struct file *file, void *fh,
1664 struct v4l2_audio *a)
1665{
1666 return 0;
1667}
1668
1669static int radio_s_input(struct file *file, void *fh, unsigned int i)
1670{
1671 return 0;
1672}
1673
1674static int radio_queryctrl(struct file *file, void *priv,
1675 struct v4l2_queryctrl *qc)
1676{
1677 int i;
1678
1679 if (qc->id < V4L2_CID_BASE ||
1680 qc->id >= V4L2_CID_LASTP1)
1681 return -EINVAL;
1682
1683 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1684 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1685 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1686 return 0;
1687 }
1688 }
1689
1690 return -EINVAL;
1691}
1692
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001693/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001694 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001695 * inits the device and starts isoc transfer
1696 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001697static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001698{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001699 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001700 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001701 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001702 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001703 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001704
Trent Piephoa991f442007-10-10 05:37:43 -03001705 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001706 if (h->vdev->minor == minor) {
1707 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001708 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001709 }
1710 if (h->vbi_dev->minor == minor) {
1711 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001712 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001713 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001714 if (h->radio_dev &&
1715 h->radio_dev->minor == minor) {
1716 radio = 1;
1717 dev = h;
1718 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001719 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001720 if (NULL == dev)
1721 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001722
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001723 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001724 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001726 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001727
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001728 if (!fh) {
1729 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1730 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001732 mutex_lock(&dev->lock);
1733 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001734 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001735 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001736 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001737
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001738 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001739 dev->width = norm_maxw(dev);
1740 dev->height = norm_maxh(dev);
1741 dev->frame_size = dev->width * dev->height * 2;
1742 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1743 dev->bytesperline = dev->width * 2;
1744 dev->hscale = 0;
1745 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001747 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001748 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001749
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001750 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001751 if (fh->radio) {
1752 em28xx_videodbg("video_open: setting radio device\n");
1753 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1754 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001755
1756 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001758 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1759 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1760 sizeof(struct em28xx_buffer), fh);
1761
Ingo Molnar3593cab2006-02-07 06:49:14 -02001762 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001763 return errCode;
1764}
1765
1766/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001767 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001768 * unregisters the v4l2,i2c and usb devices
1769 * called when the device gets disconected or at module unload
1770*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001771static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001772{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001773
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001774 /*FIXME: I2C IR should be disconnected */
1775
1776 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1777 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1778 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001779 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001780 if (dev->radio_dev) {
1781 if (-1 != dev->radio_dev->minor)
1782 video_unregister_device(dev->radio_dev);
1783 else
1784 video_device_release(dev->radio_dev);
1785 dev->radio_dev = NULL;
1786 }
1787 if (dev->vbi_dev) {
1788 if (-1 != dev->vbi_dev->minor)
1789 video_unregister_device(dev->vbi_dev);
1790 else
1791 video_device_release(dev->vbi_dev);
1792 dev->vbi_dev = NULL;
1793 }
1794 if (dev->vdev) {
1795 if (-1 != dev->vdev->minor)
1796 video_unregister_device(dev->vdev);
1797 else
1798 video_device_release(dev->vdev);
1799 dev->vdev = NULL;
1800 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001801 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001802 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001803
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001804 /* Mark device as unused */
1805 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806}
1807
1808/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001809 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1811 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001812static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001813{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001814 struct em28xx_fh *fh = filp->private_data;
1815 struct em28xx *dev = fh->dev;
1816 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001817
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001818 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001819
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001820
1821 if (res_check(fh))
1822 res_free(fh);
1823
Ingo Molnar3593cab2006-02-07 06:49:14 -02001824 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001825
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001826 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001827 videobuf_stop(&fh->vb_vidq);
1828 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001829
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001830 /* the device is already disconnect,
1831 free the remaining resources */
1832 if (dev->state & DEV_DISCONNECTED) {
1833 em28xx_release_resources(dev);
1834 mutex_unlock(&dev->lock);
1835 kfree(dev);
1836 return 0;
1837 }
1838
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001839 /* do this before setting alternate! */
1840 em28xx_uninit_isoc(dev);
1841
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001842 /* set alternate 0 */
1843 dev->alt = 0;
1844 em28xx_videodbg("setting alternate 0\n");
1845 errCode = usb_set_interface(dev->udev, 0, 0);
1846 if (errCode < 0) {
1847 em28xx_errdev("cannot change alternate number to "
1848 "0 (error=%i)\n", errCode);
1849 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001850 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001851 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001852 dev->users--;
1853 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001854 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001855 return 0;
1856}
1857
1858/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001859 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001860 * will allocate buffers when called for the first time
1861 */
1862static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001863em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001864 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001865{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001866 struct em28xx_fh *fh = filp->private_data;
1867 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001868 int rc;
1869
1870 rc = check_dev(dev);
1871 if (rc < 0)
1872 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001873
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001874 /* FIXME: read() is not prepared to allow changing the video
1875 resolution while streaming. Seems a bug at em28xx_set_fmt
1876 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001877
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001878 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1879 if (unlikely(res_get(fh)))
1880 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001881
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001882 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1883 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001884 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001885 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886}
1887
1888/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001889 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001890 * will allocate buffers when called for the first time
1891 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001892static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001893{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001894 struct em28xx_fh *fh = filp->private_data;
1895 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001896 int rc;
1897
1898 rc = check_dev(dev);
1899 if (rc < 0)
1900 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001901
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001902 if (unlikely(res_get(fh) < 0))
1903 return POLLERR;
1904
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001905 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1906 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001907
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001908 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001909}
1910
1911/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001912 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001913 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001914static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001915{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001916 struct em28xx_fh *fh = filp->private_data;
1917 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001918 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001919
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001920 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001921 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001922
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001923 rc = check_dev(dev);
1924 if (rc < 0)
1925 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001926
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001927 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001928
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001929 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1930 (unsigned long)vma->vm_start,
1931 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1932 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001933
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001934 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001935}
1936
Arjan van de Venfa027c22007-02-12 00:55:33 -08001937static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001938 .owner = THIS_MODULE,
1939 .open = em28xx_v4l2_open,
1940 .release = em28xx_v4l2_close,
1941 .read = em28xx_v4l2_read,
1942 .poll = em28xx_v4l2_poll,
1943 .mmap = em28xx_v4l2_mmap,
1944 .ioctl = video_ioctl2,
1945 .llseek = no_llseek,
1946 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001947};
1948
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001949static const struct file_operations radio_fops = {
1950 .owner = THIS_MODULE,
1951 .open = em28xx_v4l2_open,
1952 .release = em28xx_v4l2_close,
1953 .ioctl = video_ioctl2,
1954 .compat_ioctl = v4l_compat_ioctl32,
1955 .llseek = no_llseek,
1956};
1957
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001958static const struct video_device em28xx_video_template = {
1959 .fops = &em28xx_v4l_fops,
1960 .release = video_device_release,
1961
1962 .minor = -1,
1963 .vidioc_querycap = vidioc_querycap,
1964 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1965 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1966 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1967 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1968 .vidioc_g_audio = vidioc_g_audio,
1969 .vidioc_s_audio = vidioc_s_audio,
1970 .vidioc_cropcap = vidioc_cropcap,
1971
1972 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1973 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1974 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1975
1976 .vidioc_reqbufs = vidioc_reqbufs,
1977 .vidioc_querybuf = vidioc_querybuf,
1978 .vidioc_qbuf = vidioc_qbuf,
1979 .vidioc_dqbuf = vidioc_dqbuf,
1980 .vidioc_s_std = vidioc_s_std,
1981 .vidioc_enum_input = vidioc_enum_input,
1982 .vidioc_g_input = vidioc_g_input,
1983 .vidioc_s_input = vidioc_s_input,
1984 .vidioc_queryctrl = vidioc_queryctrl,
1985 .vidioc_g_ctrl = vidioc_g_ctrl,
1986 .vidioc_s_ctrl = vidioc_s_ctrl,
1987 .vidioc_streamon = vidioc_streamon,
1988 .vidioc_streamoff = vidioc_streamoff,
1989 .vidioc_g_tuner = vidioc_g_tuner,
1990 .vidioc_s_tuner = vidioc_s_tuner,
1991 .vidioc_g_frequency = vidioc_g_frequency,
1992 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001993#ifdef CONFIG_VIDEO_ADV_DEBUG
1994 .vidioc_g_register = vidioc_g_register,
1995 .vidioc_s_register = vidioc_s_register,
1996#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001997#ifdef CONFIG_VIDEO_V4L1_COMPAT
1998 .vidiocgmbuf = vidiocgmbuf,
1999#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002000
2001 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002002 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002003};
2004
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002005static struct video_device em28xx_radio_template = {
2006 .name = "em28xx-radio",
2007 .type = VID_TYPE_TUNER,
2008 .fops = &radio_fops,
2009 .minor = -1,
2010 .vidioc_querycap = radio_querycap,
2011 .vidioc_g_tuner = radio_g_tuner,
2012 .vidioc_enum_input = radio_enum_input,
2013 .vidioc_g_audio = radio_g_audio,
2014 .vidioc_s_tuner = radio_s_tuner,
2015 .vidioc_s_audio = radio_s_audio,
2016 .vidioc_s_input = radio_s_input,
2017 .vidioc_queryctrl = radio_queryctrl,
2018 .vidioc_g_ctrl = vidioc_g_ctrl,
2019 .vidioc_s_ctrl = vidioc_s_ctrl,
2020 .vidioc_g_frequency = vidioc_g_frequency,
2021 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002022#ifdef CONFIG_VIDEO_ADV_DEBUG
2023 .vidioc_g_register = vidioc_g_register,
2024 .vidioc_s_register = vidioc_s_register,
2025#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002026};
2027
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002028/******************************** usb interface *****************************************/
2029
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002030
2031static LIST_HEAD(em28xx_extension_devlist);
2032static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2033
2034int em28xx_register_extension(struct em28xx_ops *ops)
2035{
2036 struct em28xx *h, *dev = NULL;
2037
2038 list_for_each_entry(h, &em28xx_devlist, devlist)
2039 dev = h;
2040
2041 mutex_lock(&em28xx_extension_devlist_lock);
2042 list_add_tail(&ops->next, &em28xx_extension_devlist);
2043 if (dev)
2044 ops->init(dev);
2045
2046 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2047 mutex_unlock(&em28xx_extension_devlist_lock);
2048
2049 return 0;
2050}
2051EXPORT_SYMBOL(em28xx_register_extension);
2052
2053void em28xx_unregister_extension(struct em28xx_ops *ops)
2054{
2055 struct em28xx *h, *dev = NULL;
2056
2057 list_for_each_entry(h, &em28xx_devlist, devlist)
2058 dev = h;
2059
2060 if (dev)
2061 ops->fini(dev);
2062
2063 mutex_lock(&em28xx_extension_devlist_lock);
2064 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2065 list_del(&ops->next);
2066 mutex_unlock(&em28xx_extension_devlist_lock);
2067}
2068EXPORT_SYMBOL(em28xx_unregister_extension);
2069
Adrian Bunk532fe652008-01-28 22:10:48 -03002070static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2071 const struct video_device *template,
2072 const int type,
2073 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002074{
2075 struct video_device *vfd;
2076
2077 vfd = video_device_alloc();
2078 if (NULL == vfd)
2079 return NULL;
2080 *vfd = *template;
2081 vfd->minor = -1;
2082 vfd->dev = &dev->udev->dev;
2083 vfd->release = video_device_release;
2084 vfd->type = type;
2085
2086 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2087 dev->name, type_name);
2088
2089 return vfd;
2090}
2091
2092
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002093/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002094 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095 * allocates and inits the device structs, registers i2c bus and v4l device
2096 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002097static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002098 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002100 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002101 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002103 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002104 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002106 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002107 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002108 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002109 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002110 init_waitqueue_head(&dev->wait_frame);
2111 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002112
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002113 dev->em28xx_write_regs = em28xx_write_regs;
2114 dev->em28xx_read_reg = em28xx_read_reg;
2115 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2116 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2117 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002118 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002119
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002120 errCode = em28xx_read_reg(dev, CHIPID_REG);
2121 if (errCode >= 0)
2122 em28xx_info("em28xx chip ID = %d\n", errCode);
2123
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002124 em28xx_pre_card_setup(dev);
2125
2126 errCode = em28xx_config(dev);
2127 if (errCode) {
2128 em28xx_errdev("error configuring device\n");
2129 em28xx_devused &= ~(1<<dev->devno);
2130 kfree(dev);
2131 return -ENOMEM;
2132 }
2133
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002134 /* register i2c bus */
2135 em28xx_i2c_register(dev);
2136
2137 /* Do board specific init and eeprom reading */
2138 em28xx_card_setup(dev);
2139
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002140 /* Configure audio */
2141 em28xx_audio_analog_set(dev);
2142
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002143 /* configure the device */
2144 em28xx_config_i2c(dev);
2145
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002146 /* set default norm */
2147 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002148
2149 maxw = norm_maxw(dev);
2150 maxh = norm_maxh(dev);
2151
2152 /* set default image size */
2153 dev->width = maxw;
2154 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002155 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002156 dev->field_size = dev->width * dev->height;
2157 dev->frame_size =
2158 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2159 dev->bytesperline = dev->width * 2;
2160 dev->hscale = 0;
2161 dev->vscale = 0;
2162 dev->ctl_input = 2;
2163
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002164 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002165
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002166 list_add_tail(&dev->devlist, &em28xx_devlist);
2167
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002168 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002169 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2170 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002172 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002173 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002174 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03002175 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002176 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002177
2178 /* register v4l2 video video_device */
2179 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2180 video_nr[dev->devno]);
2181 if (retval) {
2182 em28xx_errdev("unable to register video device (error=%i).\n",
2183 retval);
2184 goto fail_unreg;
2185 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002186
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002187 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002188 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2189 VFL_TYPE_VBI, "vbi");
2190 /* register v4l2 vbi video_device */
2191 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2192 vbi_nr[dev->devno]) < 0) {
2193 em28xx_errdev("unable to register vbi device\n");
2194 retval = -ENODEV;
2195 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002196 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002197
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002198 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2199 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2200 VFL_TYPE_RADIO, "radio");
2201 if (NULL == dev->radio_dev) {
2202 em28xx_errdev("cannot allocate video_device.\n");
2203 goto fail_unreg;
2204 }
2205 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2206 radio_nr[dev->devno]);
2207 if (retval < 0) {
2208 em28xx_errdev("can't register radio device\n");
2209 goto fail_unreg;
2210 }
2211 em28xx_info("Registered radio device as /dev/radio%d\n",
2212 dev->radio_dev->minor & 0x1f);
2213 }
2214
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002215 /* init video dma queues */
2216 INIT_LIST_HEAD(&dev->vidq.active);
2217 INIT_LIST_HEAD(&dev->vidq.queued);
2218
2219 dev->vidq.timeout.function = em28xx_vid_timeout;
2220 dev->vidq.timeout.data = (unsigned long)dev;
2221 init_timer(&dev->vidq.timeout);
2222
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002223
Sascha Sommer5a804152007-11-03 21:22:38 -03002224 if (dev->has_msp34xx) {
2225 /* Send a reset to other chips via gpio */
2226 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2227 msleep(3);
2228 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2229 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002230 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002231
Sascha Sommer5a804152007-11-03 21:22:38 -03002232 video_mux(dev, 0);
2233
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002234 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2235 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2236 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002238 mutex_lock(&em28xx_extension_devlist_lock);
2239 if (!list_empty(&em28xx_extension_devlist)) {
2240 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2241 if (ops->id)
2242 ops->init(dev);
2243 }
2244 }
2245 mutex_unlock(&em28xx_extension_devlist_lock);
2246
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002248
2249fail_unreg:
2250 em28xx_release_resources(dev);
2251 mutex_unlock(&dev->lock);
2252 kfree(dev);
2253 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002254}
2255
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002256#if defined(CONFIG_MODULES) && defined(MODULE)
2257static void request_module_async(struct work_struct *work)
2258{
2259 struct em28xx *dev = container_of(work,
2260 struct em28xx, request_module_wk);
2261
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002262 if (dev->has_audio_class)
2263 request_module("snd-usb-audio");
2264 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002265 request_module("em28xx-alsa");
2266}
2267
2268static void request_modules(struct em28xx *dev)
2269{
2270 INIT_WORK(&dev->request_module_wk, request_module_async);
2271 schedule_work(&dev->request_module_wk);
2272}
2273#else
2274#define request_modules(dev)
2275#endif /* CONFIG_MODULES */
2276
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002277/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002278 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002279 * checks for supported devices
2280 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002281static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002282 const struct usb_device_id *id)
2283{
2284 const struct usb_endpoint_descriptor *endpoint;
2285 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002286 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002289 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002290
2291 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002292 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2293
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002294 /* Check to see next free device and mark as used */
2295 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2296 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002297
2298 /* Don't register audio interfaces */
2299 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002300 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002301 udev->descriptor.idVendor,udev->descriptor.idProduct,
2302 ifnum,
2303 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002304
2305 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002306 return -ENODEV;
2307 }
2308
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002309 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002310 udev->descriptor.idVendor,udev->descriptor.idProduct,
2311 ifnum,
2312 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002313
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002314 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2315
Michael Opdenacker59c51592007-05-09 08:57:56 +02002316 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002317 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2318 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002319 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002320 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002321 return -ENODEV;
2322 }
2323 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002324 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002325 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002326 return -ENODEV;
2327 }
2328
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002329 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002330 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002331 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002332 return -ENOMEM;
2333 }
2334
2335 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002336 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002337 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002338 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002339 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002340 return -ENOMEM;
2341 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002342
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002343 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002344 dev->devno = nr;
2345 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002346 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002347
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002348 /* Checks if audio is provided by some interface */
2349 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2350 uif = udev->config->interface[i];
2351 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2352 dev->has_audio_class = 1;
2353 break;
2354 }
2355 }
2356
2357 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2358 dev->has_audio_class ? "Has" : "Doesn't have");
2359
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002360 /* compute alternate max packet sizes */
2361 uif = udev->actconfig->interface[0];
2362
2363 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002364 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002365// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2366 dev->alt_max_pkt_size = kmalloc(32*
2367 dev->num_alt,GFP_KERNEL);
2368 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002369 em28xx_errdev("out of memory!\n");
2370 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002371 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002372 return -ENOMEM;
2373 }
2374
2375 for (i = 0; i < dev->num_alt ; i++) {
2376 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2377 wMaxPacketSize);
2378 dev->alt_max_pkt_size[i] =
2379 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002380 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002381 dev->alt_max_pkt_size[i]);
2382 }
2383
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002384 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002385 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002386
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002387 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002388 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002389 if (retval)
2390 return retval;
2391
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002392 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002393
2394 /* save our data pointer in this interface device */
2395 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002396
2397 request_modules(dev);
2398
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002399 return 0;
2400}
2401
2402/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002403 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002404 * called when the device gets diconencted
2405 * video device will be unregistered on v4l2_close in case it is still open
2406 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002407static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002408{
Sascha Sommer5a804152007-11-03 21:22:38 -03002409 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002410 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002411
2412 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002413 usb_set_intfdata(interface, NULL);
2414
2415 if (!dev)
2416 return;
2417
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002418 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002419
Sascha Sommer5a804152007-11-03 21:22:38 -03002420 /* wait until all current v4l2 io is finished then deallocate resources */
2421 mutex_lock(&dev->lock);
2422
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002423 wake_up_interruptible_all(&dev->open);
2424
2425 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002426 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002427 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002428 "deallocation are deferred on close.\n",
2429 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2430
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002431 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002432 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002433 dev->state |= DEV_DISCONNECTED;
2434 wake_up_interruptible(&dev->wait_frame);
2435 wake_up_interruptible(&dev->wait_stream);
2436 } else {
2437 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002438 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002439 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002440 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002441
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002442 mutex_lock(&em28xx_extension_devlist_lock);
2443 if (!list_empty(&em28xx_extension_devlist)) {
2444 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2445 ops->fini(dev);
2446 }
2447 }
2448 mutex_unlock(&em28xx_extension_devlist_lock);
2449
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002450 if (!dev->users) {
2451 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002452 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002453 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002454}
2455
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002456static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002457 .name = "em28xx",
2458 .probe = em28xx_usb_probe,
2459 .disconnect = em28xx_usb_disconnect,
2460 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002461};
2462
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002463static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002464{
2465 int result;
2466
2467 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002468 (EM28XX_VERSION_CODE >> 16) & 0xff,
2469 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002470#ifdef SNAPSHOT
2471 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2472 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2473#endif
2474
2475 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002476 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002477 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002478 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002479 " usb_register failed. Error number %d.\n", result);
2480
2481 return result;
2482}
2483
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002484static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002485{
2486 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002487 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002488}
2489
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002490module_init(em28xx_module_init);
2491module_exit(em28xx_module_exit);