blob: 0a2ff4afe416e7b97b5879188fb4fa0553534f42 [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{
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300136 mod_timer(&dma_q->timeout, jiffies + BUFFER_TIMEOUT);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300137
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",
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300162 dev->frame_size, buf->vb.size);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300163 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 Thorntone0fadfd342008-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
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300211 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300212 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 ((char *)startwrite + lencopy) -
214 ((char *)outp + buf->vb.size));
215 lencopy = remain = (char *)outp + buf->vb.size -
216 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300217 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300218 if (lencopy <= 0)
219 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300220
221 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300222
223 remain -= lencopy;
224 }
225
226 dma_q->pos += len;
227}
228
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 int packet, int status)
231{
232 char *errmsg = "Unknown";
233
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300234 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300235 case -ENOENT:
236 errmsg = "unlinked synchronuously";
237 break;
238 case -ECONNRESET:
239 errmsg = "unlinked asynchronuously";
240 break;
241 case -ENOSR:
242 errmsg = "Buffer error (overrun)";
243 break;
244 case -EPIPE:
245 errmsg = "Stalled (device not responding)";
246 break;
247 case -EOVERFLOW:
248 errmsg = "Babble (bad cable?)";
249 break;
250 case -EPROTO:
251 errmsg = "Bit-stuff error (bad cable?)";
252 break;
253 case -EILSEQ:
254 errmsg = "CRC/Timeout (could be anything)";
255 break;
256 case -ETIME:
257 errmsg = "Device does not respond";
258 break;
259 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300260 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300261 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
262 } else {
263 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
264 packet, status, errmsg);
265 }
266}
267
268/*
269 * video-buf generic routine to get the next available buffer
270 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300271static inline int get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300272 struct em28xx_buffer **buf)
273{
274 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
275
276 if (list_empty(&dma_q->active)) {
277 em28xx_isocdbg("No active queue to serve\n");
278 return 0;
279 }
280
281 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
282
283 return 1;
284}
285
286/*
287 * Controls the isoc copy of each urb packet
288 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300289static inline int em28xx_isoc_copy(struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300290{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300291 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300292 struct em28xx_dmaqueue *dma_q = urb->context;
293 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300294 unsigned char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300296 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297
298 if (!dev)
299 return 0;
300
301 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
302 return 0;
303
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300304 if (urb->status < 0) {
305 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300306 if (urb->status == -ENOENT)
307 return 0;
308 }
309
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300310 buf = dev->isoc_ctl.buf;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300311
312 if (!buf) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300313 rc = get_next_buf(dma_q, &buf);
314 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300315 return rc;
316 }
317
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300318 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300319
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300320 for (i = 0; i < urb->number_of_packets; i++) {
321 int status = urb->iso_frame_desc[i].status;
322
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300323 if (status < 0) {
324 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300325 if (urb->iso_frame_desc[i].status != -EPROTO)
326 continue;
327 }
328
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300329 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330
331 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300332 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300333 continue;
334 }
335 if (urb->iso_frame_desc[i].actual_length >
336 dev->max_pkt_size) {
337 em28xx_isocdbg("packet bigger than packet size");
338 continue;
339 }
340
341 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300342
343 /* FIXME: incomplete buffer checks where removed to make
344 logic simpler. Impacts of those changes should be evaluated
345 */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300346 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300347 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
348 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300349
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300350 if (p[2] & 1)
351 buf->top_field = 0;
352 else {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300353 if (buf->receiving) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300354 buffer_filled(dev, dma_q, buf);
355 rc = get_next_buf(dma_q, &buf);
356 if (rc <= 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300357 return rc;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300358
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300359 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300360 }
361
362 buf->top_field = 1;
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300363 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300364 buf->receiving = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300365 dma_q->pos = 0;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300366 } else if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300367 em28xx_isocdbg("VBI HEADER!!!\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300368 }
369
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300370 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371
372 /* FIXME: Should add vbi copy */
373 }
374 return rc;
375}
376
377/* ------------------------------------------------------------------
378 URB control
379 ------------------------------------------------------------------*/
380
381/*
382 * IRQ callback, called by URB callback
383 */
384static void em28xx_irq_callback(struct urb *urb)
385{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300386 struct em28xx_dmaqueue *dma_q = urb->context;
387 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300388 int rc, i;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 unsigned long flags;
390
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300391 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300392
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300393 /* Copy data from URB */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300394 rc = em28xx_isoc_copy(urb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300395
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300396 /* Reset urb buffers */
397 for (i = 0; i < urb->number_of_packets; i++) {
398 urb->iso_frame_desc[i].status = 0;
399 urb->iso_frame_desc[i].actual_length = 0;
400 }
401 urb->status = 0;
402
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300403 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
404 if (urb->status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405 em28xx_err("urb resubmit failed (error=%i)\n",
406 urb->status);
407 }
408
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300409 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300410}
411
412/*
413 * Stop and Deallocate URBs
414 */
415static void em28xx_uninit_isoc(struct em28xx *dev)
416{
417 struct urb *urb;
418 int i;
419
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300420 em28xx_isocdbg("em28xx: called em28xx_uninit_isoc\n");
421
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300422 dev->isoc_ctl.nfields = -1;
423 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300424 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300425 urb = dev->isoc_ctl.urb[i];
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300426 if (urb) {
427 usb_kill_urb(urb);
428 usb_unlink_urb(urb);
429 if (dev->isoc_ctl.transfer_buffer[i]) {
430 usb_buffer_free(dev->udev,
431 urb->transfer_buffer_length,
432 dev->isoc_ctl.transfer_buffer[i],
433 urb->transfer_dma);
434 }
435 usb_free_urb(urb);
436 dev->isoc_ctl.urb[i] = NULL;
437 }
438 dev->isoc_ctl.transfer_buffer[i] = NULL;
439 }
440
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300441 kfree(dev->isoc_ctl.urb);
442 kfree(dev->isoc_ctl.transfer_buffer);
443 dev->isoc_ctl.urb = NULL;
444 dev->isoc_ctl.transfer_buffer = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300445
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300446 dev->isoc_ctl.num_bufs = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300447
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300448 del_timer(&dev->vidq.timeout);
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300449 em28xx_capture_start(dev, 0);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450}
451
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452/*
453 * Allocate URBs and start IRQ
454 */
455static int em28xx_prepare_isoc(struct em28xx *dev, int max_packets,
456 int num_bufs)
457{
458 struct em28xx_dmaqueue *dma_q = &dev->vidq;
459 int i;
460 int sb_size, pipe;
461 struct urb *urb;
462 int j, k;
463
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300464 em28xx_isocdbg("em28xx: called em28xx_prepare_isoc\n");
465
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 /* De-allocates all pending stuff */
467 em28xx_uninit_isoc(dev);
468
469 dev->isoc_ctl.num_bufs = num_bufs;
470
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300471 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300472 if (!dev->isoc_ctl.urb) {
473 em28xx_errdev("cannot alloc memory for usb buffers\n");
474 return -ENOMEM;
475 }
476
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300477 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300478 GFP_KERNEL);
479 if (!dev->isoc_ctl.urb) {
480 em28xx_errdev("cannot allocate memory for usbtransfer\n");
481 kfree(dev->isoc_ctl.urb);
482 return -ENOMEM;
483 }
484
485 dev->isoc_ctl.max_pkt_size = dev->max_pkt_size;
486
487 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
488
489 /* allocate urbs and transfer buffers */
490 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
491 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
492 if (!urb) {
493 em28xx_err("cannot alloc isoc_ctl.urb %i\n", i);
494 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495 return -ENOMEM;
496 }
497 dev->isoc_ctl.urb[i] = urb;
498
499 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->udev,
500 sb_size, GFP_KERNEL, &urb->transfer_dma);
501 if (!dev->isoc_ctl.transfer_buffer[i]) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300502 em28xx_err("unable to allocate %i bytes for transfer"
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300503 " buffer %i%s\n",
504 sb_size, i,
505 in_interrupt()?" while in int":"");
506 em28xx_uninit_isoc(dev);
507 return -ENOMEM;
508 }
509 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
510
511 /* FIXME: this is a hack - should be
512 'desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK'
513 should also be using 'desc.bInterval'
514 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300515 pipe = usb_rcvisocpipe(dev->udev, 0x82);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300516 usb_fill_int_urb(urb, dev->udev, pipe,
517 dev->isoc_ctl.transfer_buffer[i], sb_size,
518 em28xx_irq_callback, dma_q, 1);
519
520 urb->number_of_packets = max_packets;
521 urb->transfer_flags = URB_ISO_ASAP;
522
523 k = 0;
524 for (j = 0; j < max_packets; j++) {
525 urb->iso_frame_desc[j].offset = k;
526 urb->iso_frame_desc[j].length =
527 dev->isoc_ctl.max_pkt_size;
528 k += dev->isoc_ctl.max_pkt_size;
529 }
530 }
531
532 return 0;
533}
534
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300535static int em28xx_start_thread(struct em28xx_dmaqueue *dma_q)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300536{
537 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300538 int i, rc = 0;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300539
540 em28xx_videodbg("Called em28xx_start_thread\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300541
542 init_waitqueue_head(&dma_q->wq);
543
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300544 em28xx_capture_start(dev, 1);
545
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300546 /* submit urbs and enables IRQ */
547 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
548 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
549 if (rc) {
550 em28xx_err("submit of urb %i failed (error=%i)\n", i,
551 rc);
552 em28xx_uninit_isoc(dev);
553 return rc;
554 }
555 }
556
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300557 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300558 return rc;
559
560 return 0;
561}
562
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300563static void em28xx_vid_timeout(unsigned long data)
564{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300565 struct em28xx *dev = (struct em28xx *)data;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300566 struct em28xx_dmaqueue *vidq = &dev->vidq;
567 struct em28xx_buffer *buf;
568 unsigned long flags;
569
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300570 spin_lock_irqsave(&dev->slock, flags);
Mauro Carvalho Chehabca21d2d2008-04-13 14:40:36 -0300571
572 list_for_each_entry(buf, vidq->active.next, vb.queue) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300573 list_del(&buf->vb.queue);
574 buf->vb.state = VIDEOBUF_ERROR;
575 wake_up(&buf->vb.done);
576 em28xx_videodbg("em28xx/0: [%p/%d] timeout\n",
577 buf, buf->vb.i);
578 }
Mauro Carvalho Chehab47625da2008-04-13 14:40:10 -0300579 /* Instead of trying to restart, just sets timeout again */
580 mod_timer(&vidq->timeout, jiffies + BUFFER_TIMEOUT);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300581
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300582 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300583}
584
585/* ------------------------------------------------------------------
586 Videobuf operations
587 ------------------------------------------------------------------*/
588
589static int
590buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
591{
592 struct em28xx_fh *fh = vq->priv_data;
593
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300594 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300595 if (0 == *count)
596 *count = EM28XX_DEF_BUF;
597
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300598 if (*count < EM28XX_MIN_BUF)
599 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300600
601 return 0;
602}
603
604static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
605{
606 if (in_interrupt())
607 BUG();
608
609 videobuf_waiton(&buf->vb, 0, 0);
610 videobuf_vmalloc_free(&buf->vb);
611 buf->vb.state = VIDEOBUF_NEEDS_INIT;
612}
613
614static int
615buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
616 enum v4l2_field field)
617{
618 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300619 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300620 struct em28xx *dev = fh->dev;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300621 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300622 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300623
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300624 /* BUG_ON(NULL == fh->fmt); */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300625
626 /* FIXME: It assumes depth = 16 */
627 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300628 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300629
630 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
631 return -EINVAL;
632
633 if (buf->fmt != fh->fmt ||
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300634 buf->vb.width != dev->width ||
635 buf->vb.height != dev->height ||
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300636 buf->vb.field != field) {
637 buf->fmt = fh->fmt;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300638 buf->vb.width = dev->width;
639 buf->vb.height = dev->height;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300640 buf->vb.field = field;
641 buf->vb.state = VIDEOBUF_NEEDS_INIT;
642 }
643
644 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300645 rc = videobuf_iolock(vq, &buf->vb, NULL);
646 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300647 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300648 }
649
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300650 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300651 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300652
653 if (urb_init) {
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300654 rc = em28xx_prepare_isoc(dev, EM28XX_NUM_PACKETS,
655 EM28XX_NUM_BUFS);
656 if (rc < 0)
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300657 goto fail;
658
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300659 rc = em28xx_start_thread(vidq);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300660 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300661 goto fail;
662 }
663
664 buf->vb.state = VIDEOBUF_PREPARED;
665 return 0;
666
667fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300668 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300669 return rc;
670}
671
672static void
673buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
674{
675 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
676 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300677 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300678 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300679
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300680 buf->vb.state = VIDEOBUF_QUEUED;
681 list_add_tail(&buf->vb.queue, &vidq->active);
682
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300683}
684
685static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb)
686{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300687 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300688 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300689 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300690
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300691 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300692
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300693 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300694}
695
696static struct videobuf_queue_ops em28xx_video_qops = {
697 .buf_setup = buffer_setup,
698 .buf_prepare = buffer_prepare,
699 .buf_queue = buffer_queue,
700 .buf_release = buffer_release,
701};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800702
703/********************* v4l2 interface ******************************************/
704
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800705/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800706 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800707 * inits registers with sane defaults
708 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800709static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800710{
711
712 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300713 if (!dev->is_em2800)
714 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800715
716 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200717
Markus Rechberger9475fb12006-02-27 00:07:34 -0300718/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
719/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200720 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
721
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800722 dev->mute = 1; /* maybe not the right place... */
723 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300724
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800725 em28xx_outfmt_set_yuv422(dev);
726 em28xx_colorlevels_set_default(dev);
727 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800728
729 return 0;
730}
731
732/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800733 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800734 * configure i2c attached devices
735 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800736static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800737{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300738 struct v4l2_routing route;
739
740 route.input = INPUT(dev->ctl_input)->vmux;
741 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100742 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300743 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300744 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800745}
746
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800747static void video_mux(struct em28xx *dev, int index)
748{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300749 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800750
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300751 route.input = INPUT(index)->vmux;
752 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800753 dev->ctl_input = index;
754 dev->ctl_ainput = INPUT(index)->amux;
755
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300756 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800757
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800758 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200759 if (dev->i2s_speed)
760 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300761 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300762 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300763 /* Note: this is msp3400 specific */
764 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800765 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300766
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300767 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800768}
769
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300770/* Usage lock check functions */
771static int res_get(struct em28xx_fh *fh)
772{
773 struct em28xx *dev = fh->dev;
774 int rc = 0;
775
776 /* This instance already has stream_on */
777 if (fh->stream_on)
778 return rc;
779
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300780 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300781 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300782
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300783 mutex_lock(&dev->lock);
784 dev->stream_on = 1;
785 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300786 mutex_unlock(&dev->lock);
787 return rc;
788}
789
790static int res_check(struct em28xx_fh *fh)
791{
792 return (fh->stream_on);
793}
794
795static void res_free(struct em28xx_fh *fh)
796{
797 struct em28xx *dev = fh->dev;
798
799 mutex_lock(&dev->lock);
800 fh->stream_on = 0;
801 dev->stream_on = 0;
802 mutex_unlock(&dev->lock);
803}
804
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800805/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300806 * em28xx_get_ctrl()
807 * return the current saturation, brightness or contrast, mute state
808 */
809static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
810{
811 switch (ctrl->id) {
812 case V4L2_CID_AUDIO_MUTE:
813 ctrl->value = dev->mute;
814 return 0;
815 case V4L2_CID_AUDIO_VOLUME:
816 ctrl->value = dev->volume;
817 return 0;
818 default:
819 return -EINVAL;
820 }
821}
822
823/*
824 * em28xx_set_ctrl()
825 * mute or set new saturation, brightness or contrast
826 */
827static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
828{
829 switch (ctrl->id) {
830 case V4L2_CID_AUDIO_MUTE:
831 if (ctrl->value != dev->mute) {
832 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300833 return em28xx_audio_analog_set(dev);
834 }
835 return 0;
836 case V4L2_CID_AUDIO_VOLUME:
837 dev->volume = ctrl->value;
838 return em28xx_audio_analog_set(dev);
839 default:
840 return -EINVAL;
841 }
842}
843
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300844static int check_dev(struct em28xx *dev)
845{
846 if (dev->state & DEV_DISCONNECTED) {
847 em28xx_errdev("v4l2 ioctl: device not present\n");
848 return -ENODEV;
849 }
850
851 if (dev->state & DEV_MISCONFIGURED) {
852 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
853 "close and open it again\n");
854 return -EIO;
855 }
856 return 0;
857}
858
859static void get_scale(struct em28xx *dev,
860 unsigned int width, unsigned int height,
861 unsigned int *hscale, unsigned int *vscale)
862{
863 unsigned int maxw = norm_maxw(dev);
864 unsigned int maxh = norm_maxh(dev);
865
866 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
867 if (*hscale >= 0x4000)
868 *hscale = 0x3fff;
869
870 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
871 if (*vscale >= 0x4000)
872 *vscale = 0x3fff;
873}
874
875/* ------------------------------------------------------------------
876 IOCTL vidioc handling
877 ------------------------------------------------------------------*/
878
879static int vidioc_g_fmt_cap(struct file *file, void *priv,
880 struct v4l2_format *f)
881{
882 struct em28xx_fh *fh = priv;
883 struct em28xx *dev = fh->dev;
884
885 mutex_lock(&dev->lock);
886
887 f->fmt.pix.width = dev->width;
888 f->fmt.pix.height = dev->height;
889 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
890 f->fmt.pix.bytesperline = dev->bytesperline;
891 f->fmt.pix.sizeimage = dev->frame_size;
892 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
893
894 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
895 f->fmt.pix.field = dev->interlaced ?
896 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
897
898 mutex_unlock(&dev->lock);
899 return 0;
900}
901
902static int vidioc_try_fmt_cap(struct file *file, void *priv,
903 struct v4l2_format *f)
904{
905 struct em28xx_fh *fh = priv;
906 struct em28xx *dev = fh->dev;
907 int width = f->fmt.pix.width;
908 int height = f->fmt.pix.height;
909 unsigned int maxw = norm_maxw(dev);
910 unsigned int maxh = norm_maxh(dev);
911 unsigned int hscale, vscale;
912
913 /* width must even because of the YUYV format
914 height must be even because of interlacing */
915 height &= 0xfffe;
916 width &= 0xfffe;
917
918 if (height < 32)
919 height = 32;
920 if (height > maxh)
921 height = maxh;
922 if (width < 48)
923 width = 48;
924 if (width > maxw)
925 width = maxw;
926
927 mutex_lock(&dev->lock);
928
929 if (dev->is_em2800) {
930 /* the em2800 can only scale down to 50% */
931 if (height % (maxh / 2))
932 height = maxh;
933 if (width % (maxw / 2))
934 width = maxw;
935 /* according to empiatech support */
936 /* the MaxPacketSize is to small to support */
937 /* framesizes larger than 640x480 @ 30 fps */
938 /* or 640x576 @ 25 fps. As this would cut */
939 /* of a part of the image we prefer */
940 /* 360x576 or 360x480 for now */
941 if (width == maxw && height == maxh)
942 width /= 2;
943 }
944
945 get_scale(dev, width, height, &hscale, &vscale);
946
947 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
948 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
949
950 f->fmt.pix.width = width;
951 f->fmt.pix.height = height;
952 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
953 f->fmt.pix.bytesperline = width * 2;
954 f->fmt.pix.sizeimage = width * 2 * height;
955 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
956 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
957
958 mutex_unlock(&dev->lock);
959 return 0;
960}
961
962static int vidioc_s_fmt_cap(struct file *file, void *priv,
963 struct v4l2_format *f)
964{
965 struct em28xx_fh *fh = priv;
966 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300967 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300968
969 rc = check_dev(dev);
970 if (rc < 0)
971 return rc;
972
973 vidioc_try_fmt_cap(file, priv, f);
974
975 mutex_lock(&dev->lock);
976
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300977 /* set new image size */
978 dev->width = f->fmt.pix.width;
979 dev->height = f->fmt.pix.height;
980 dev->frame_size = dev->width * dev->height * 2;
981 dev->field_size = dev->frame_size >> 1;
982 dev->bytesperline = dev->width * 2;
983 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
984
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300985 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300986 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300987
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300988 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300989 return 0;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300990}
991
992static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
993{
994 struct em28xx_fh *fh = priv;
995 struct em28xx *dev = fh->dev;
996 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 int rc;
998
999 rc = check_dev(dev);
1000 if (rc < 0)
1001 return rc;
1002
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001003 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001004 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001005 mutex_unlock(&dev->lock);
1006
1007 /* Adjusts width/height, if needed */
1008 f.fmt.pix.width = dev->width;
1009 f.fmt.pix.height = dev->height;
1010 vidioc_try_fmt_cap(file, priv, &f);
1011
1012 mutex_lock(&dev->lock);
1013
1014 /* set new image size */
1015 dev->width = f.fmt.pix.width;
1016 dev->height = f.fmt.pix.height;
1017 dev->frame_size = dev->width * dev->height * 2;
1018 dev->field_size = dev->frame_size >> 1;
1019 dev->bytesperline = dev->width * 2;
1020 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
1021
1022 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001023 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001024
1025 mutex_unlock(&dev->lock);
1026 return 0;
1027}
1028
1029static const char *iname[] = {
1030 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
1031 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
1032 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
1033 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
1034 [EM28XX_VMUX_SVIDEO] = "S-Video",
1035 [EM28XX_VMUX_TELEVISION] = "Television",
1036 [EM28XX_VMUX_CABLE] = "Cable TV",
1037 [EM28XX_VMUX_DVB] = "DVB",
1038 [EM28XX_VMUX_DEBUG] = "for debug only",
1039};
1040
1041static int vidioc_enum_input(struct file *file, void *priv,
1042 struct v4l2_input *i)
1043{
1044 struct em28xx_fh *fh = priv;
1045 struct em28xx *dev = fh->dev;
1046 unsigned int n;
1047
1048 n = i->index;
1049 if (n >= MAX_EM28XX_INPUT)
1050 return -EINVAL;
1051 if (0 == INPUT(n)->type)
1052 return -EINVAL;
1053
1054 i->index = n;
1055 i->type = V4L2_INPUT_TYPE_CAMERA;
1056
1057 strcpy(i->name, iname[INPUT(n)->type]);
1058
1059 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
1060 (EM28XX_VMUX_CABLE == INPUT(n)->type))
1061 i->type = V4L2_INPUT_TYPE_TUNER;
1062
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001063 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001064
1065 return 0;
1066}
1067
1068static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1069{
1070 struct em28xx_fh *fh = priv;
1071 struct em28xx *dev = fh->dev;
1072
1073 *i = dev->ctl_input;
1074
1075 return 0;
1076}
1077
1078static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1079{
1080 struct em28xx_fh *fh = priv;
1081 struct em28xx *dev = fh->dev;
1082 int rc;
1083
1084 rc = check_dev(dev);
1085 if (rc < 0)
1086 return rc;
1087
1088 if (i >= MAX_EM28XX_INPUT)
1089 return -EINVAL;
1090 if (0 == INPUT(i)->type)
1091 return -EINVAL;
1092
1093 mutex_lock(&dev->lock);
1094
1095 video_mux(dev, i);
1096
1097 mutex_unlock(&dev->lock);
1098 return 0;
1099}
1100
1101static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1102{
1103 struct em28xx_fh *fh = priv;
1104 struct em28xx *dev = fh->dev;
1105 unsigned int index = a->index;
1106
1107 if (a->index > 1)
1108 return -EINVAL;
1109
1110 index = dev->ctl_ainput;
1111
1112 if (index == 0) {
1113 strcpy(a->name, "Television");
1114 } else {
1115 strcpy(a->name, "Line In");
1116 }
1117 a->capability = V4L2_AUDCAP_STEREO;
1118 a->index = index;
1119
1120 return 0;
1121}
1122
1123static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1124{
1125 struct em28xx_fh *fh = priv;
1126 struct em28xx *dev = fh->dev;
1127
1128 if (a->index != dev->ctl_ainput)
1129 return -EINVAL;
1130
1131 return 0;
1132}
1133
1134static int vidioc_queryctrl(struct file *file, void *priv,
1135 struct v4l2_queryctrl *qc)
1136{
1137 struct em28xx_fh *fh = priv;
1138 struct em28xx *dev = fh->dev;
1139 int id = qc->id;
1140 int i;
1141 int rc;
1142
1143 rc = check_dev(dev);
1144 if (rc < 0)
1145 return rc;
1146
1147 memset(qc, 0, sizeof(*qc));
1148
1149 qc->id = id;
1150
1151 if (!dev->has_msp34xx) {
1152 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1153 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1154 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1155 return 0;
1156 }
1157 }
1158 }
1159 mutex_lock(&dev->lock);
1160 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1161 mutex_unlock(&dev->lock);
1162
1163 if (qc->type)
1164 return 0;
1165 else
1166 return -EINVAL;
1167}
1168
1169static int vidioc_g_ctrl(struct file *file, void *priv,
1170 struct v4l2_control *ctrl)
1171{
1172 struct em28xx_fh *fh = priv;
1173 struct em28xx *dev = fh->dev;
1174 int rc;
1175
1176 rc = check_dev(dev);
1177 if (rc < 0)
1178 return rc;
1179 mutex_lock(&dev->lock);
1180
1181 if (!dev->has_msp34xx)
1182 rc = em28xx_get_ctrl(dev, ctrl);
1183 else
1184 rc = -EINVAL;
1185
1186 if (rc == -EINVAL) {
1187 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1188 rc = 0;
1189 }
1190
1191 mutex_unlock(&dev->lock);
1192 return rc;
1193}
1194
1195static int vidioc_s_ctrl(struct file *file, void *priv,
1196 struct v4l2_control *ctrl)
1197{
1198 struct em28xx_fh *fh = priv;
1199 struct em28xx *dev = fh->dev;
1200 u8 i;
1201 int rc;
1202
1203 rc = check_dev(dev);
1204 if (rc < 0)
1205 return rc;
1206
1207 mutex_lock(&dev->lock);
1208
1209 if (dev->has_msp34xx)
1210 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1211 else {
1212 rc = 1;
1213 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1214 if (ctrl->id == em28xx_qctrl[i].id) {
1215 if (ctrl->value < em28xx_qctrl[i].minimum ||
1216 ctrl->value > em28xx_qctrl[i].maximum) {
1217 rc = -ERANGE;
1218 break;
1219 }
1220
1221 rc = em28xx_set_ctrl(dev, ctrl);
1222 break;
1223 }
1224 }
1225 }
1226
1227 /* Control not found - try to send it to the attached devices */
1228 if (rc == 1) {
1229 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1230 rc = 0;
1231 }
1232
1233 mutex_unlock(&dev->lock);
1234 return rc;
1235}
1236
1237static int vidioc_g_tuner(struct file *file, void *priv,
1238 struct v4l2_tuner *t)
1239{
1240 struct em28xx_fh *fh = priv;
1241 struct em28xx *dev = fh->dev;
1242 int rc;
1243
1244 rc = check_dev(dev);
1245 if (rc < 0)
1246 return rc;
1247
1248 if (0 != t->index)
1249 return -EINVAL;
1250
1251 strcpy(t->name, "Tuner");
1252
1253 mutex_lock(&dev->lock);
1254
1255 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1256
1257 mutex_unlock(&dev->lock);
1258 return 0;
1259}
1260
1261static int vidioc_s_tuner(struct file *file, void *priv,
1262 struct v4l2_tuner *t)
1263{
1264 struct em28xx_fh *fh = priv;
1265 struct em28xx *dev = fh->dev;
1266 int rc;
1267
1268 rc = check_dev(dev);
1269 if (rc < 0)
1270 return rc;
1271
1272 if (0 != t->index)
1273 return -EINVAL;
1274
1275 mutex_lock(&dev->lock);
1276
1277 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1278
1279 mutex_unlock(&dev->lock);
1280 return 0;
1281}
1282
1283static int vidioc_g_frequency(struct file *file, void *priv,
1284 struct v4l2_frequency *f)
1285{
1286 struct em28xx_fh *fh = priv;
1287 struct em28xx *dev = fh->dev;
1288
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001289 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001290 f->frequency = dev->ctl_freq;
1291
1292 return 0;
1293}
1294
1295static int vidioc_s_frequency(struct file *file, void *priv,
1296 struct v4l2_frequency *f)
1297{
1298 struct em28xx_fh *fh = priv;
1299 struct em28xx *dev = fh->dev;
1300 int rc;
1301
1302 rc = check_dev(dev);
1303 if (rc < 0)
1304 return rc;
1305
1306 if (0 != f->tuner)
1307 return -EINVAL;
1308
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001309 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1310 return -EINVAL;
1311 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001312 return -EINVAL;
1313
1314 mutex_lock(&dev->lock);
1315
1316 dev->ctl_freq = f->frequency;
1317 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1318
1319 mutex_unlock(&dev->lock);
1320 return 0;
1321}
1322
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001323#ifdef CONFIG_VIDEO_ADV_DEBUG
1324static int em28xx_reg_len(int reg)
1325{
1326 switch (reg) {
1327 case AC97LSB_REG:
1328 case HSCALELOW_REG:
1329 case VSCALELOW_REG:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001330 return 2;
1331 default:
1332 return 1;
1333 }
1334}
1335
1336static int vidioc_g_register(struct file *file, void *priv,
1337 struct v4l2_register *reg)
1338{
1339 struct em28xx_fh *fh = priv;
1340 struct em28xx *dev = fh->dev;
1341 int ret;
1342
1343 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1344 return -EINVAL;
1345
1346 if (em28xx_reg_len(reg->reg) == 1) {
1347 ret = em28xx_read_reg(dev, reg->reg);
1348 if (ret < 0)
1349 return ret;
1350
1351 reg->val = ret;
1352 } else {
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001353 u64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001354 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1355 reg->reg, (char *)&val, 2);
1356 if (ret < 0)
1357 return ret;
1358
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001359 reg->val = cpu_to_le64((__u64)val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001360 }
1361
1362 return 0;
1363}
1364
1365static int vidioc_s_register(struct file *file, void *priv,
1366 struct v4l2_register *reg)
1367{
1368 struct em28xx_fh *fh = priv;
1369 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001370 u64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001371
Mauro Carvalho Chehab0df81302008-02-06 15:56:16 -03001372 buf = le64_to_cpu((__u64)reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001373
1374 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1375 em28xx_reg_len(reg->reg));
1376}
1377#endif
1378
1379
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001380static int vidioc_cropcap(struct file *file, void *priv,
1381 struct v4l2_cropcap *cc)
1382{
1383 struct em28xx_fh *fh = priv;
1384 struct em28xx *dev = fh->dev;
1385
1386 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1387 return -EINVAL;
1388
1389 cc->bounds.left = 0;
1390 cc->bounds.top = 0;
1391 cc->bounds.width = dev->width;
1392 cc->bounds.height = dev->height;
1393 cc->defrect = cc->bounds;
1394 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1395 cc->pixelaspect.denominator = 59;
1396
1397 return 0;
1398}
1399
1400static int vidioc_streamon(struct file *file, void *priv,
1401 enum v4l2_buf_type type)
1402{
1403 struct em28xx_fh *fh = priv;
1404 struct em28xx *dev = fh->dev;
1405 int rc;
1406
1407 rc = check_dev(dev);
1408 if (rc < 0)
1409 return rc;
1410
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001411
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001412 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001413 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001414
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001415 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001416}
1417
1418static int vidioc_streamoff(struct file *file, void *priv,
1419 enum v4l2_buf_type type)
1420{
1421 struct em28xx_fh *fh = priv;
1422 struct em28xx *dev = fh->dev;
1423 int rc;
1424
1425 rc = check_dev(dev);
1426 if (rc < 0)
1427 return rc;
1428
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001429 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1430 return -EINVAL;
1431 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001432 return -EINVAL;
1433
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001434 videobuf_streamoff(&fh->vb_vidq);
1435 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001436
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001437 return 0;
1438}
1439
1440static int vidioc_querycap(struct file *file, void *priv,
1441 struct v4l2_capability *cap)
1442{
1443 struct em28xx_fh *fh = priv;
1444 struct em28xx *dev = fh->dev;
1445
1446 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1447 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1448 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1449
1450 cap->version = EM28XX_VERSION_CODE;
1451
1452 cap->capabilities =
1453 V4L2_CAP_SLICED_VBI_CAPTURE |
1454 V4L2_CAP_VIDEO_CAPTURE |
1455 V4L2_CAP_AUDIO |
1456 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1457
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001458 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001459 cap->capabilities |= V4L2_CAP_TUNER;
1460
1461 return 0;
1462}
1463
1464static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1465 struct v4l2_fmtdesc *fmtd)
1466{
1467 if (fmtd->index != 0)
1468 return -EINVAL;
1469
1470 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1471 strcpy(fmtd->description, "Packed YUY2");
1472 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1473 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1474
1475 return 0;
1476}
1477
1478/* Sliced VBI ioctls */
1479static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
1480 struct v4l2_format *f)
1481{
1482 struct em28xx_fh *fh = priv;
1483 struct em28xx *dev = fh->dev;
1484 int rc;
1485
1486 rc = check_dev(dev);
1487 if (rc < 0)
1488 return rc;
1489
1490 mutex_lock(&dev->lock);
1491
1492 f->fmt.sliced.service_set = 0;
1493
1494 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1495
1496 if (f->fmt.sliced.service_set == 0)
1497 rc = -EINVAL;
1498
1499 mutex_unlock(&dev->lock);
1500 return rc;
1501}
1502
1503static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1504 struct v4l2_format *f)
1505{
1506 struct em28xx_fh *fh = priv;
1507 struct em28xx *dev = fh->dev;
1508 int rc;
1509
1510 rc = check_dev(dev);
1511 if (rc < 0)
1512 return rc;
1513
1514 mutex_lock(&dev->lock);
1515 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1516 mutex_unlock(&dev->lock);
1517
1518 if (f->fmt.sliced.service_set == 0)
1519 return -EINVAL;
1520
1521 return 0;
1522}
1523
1524
1525static int vidioc_reqbufs(struct file *file, void *priv,
1526 struct v4l2_requestbuffers *rb)
1527{
1528 struct em28xx_fh *fh = priv;
1529 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001530 int rc;
1531
1532 rc = check_dev(dev);
1533 if (rc < 0)
1534 return rc;
1535
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001536 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001537}
1538
1539static int vidioc_querybuf(struct file *file, void *priv,
1540 struct v4l2_buffer *b)
1541{
1542 struct em28xx_fh *fh = priv;
1543 struct em28xx *dev = fh->dev;
1544 int rc;
1545
1546 rc = check_dev(dev);
1547 if (rc < 0)
1548 return rc;
1549
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001550 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001551}
1552
1553static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1554{
1555 struct em28xx_fh *fh = priv;
1556 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001557 int rc;
1558
1559 rc = check_dev(dev);
1560 if (rc < 0)
1561 return rc;
1562
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001563 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001564}
1565
1566static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1567{
1568 struct em28xx_fh *fh = priv;
1569 struct em28xx *dev = fh->dev;
1570 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001571
1572 rc = check_dev(dev);
1573 if (rc < 0)
1574 return rc;
1575
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001576 return (videobuf_dqbuf(&fh->vb_vidq, b,
1577 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001578}
1579
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001580#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001581static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001582{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001583 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001584
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001585 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001586}
1587#endif
1588
1589
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001590/* ----------------------------------------------------------- */
1591/* RADIO ESPECIFIC IOCTLS */
1592/* ----------------------------------------------------------- */
1593
1594static int radio_querycap(struct file *file, void *priv,
1595 struct v4l2_capability *cap)
1596{
1597 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1598
1599 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1600 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1601 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1602
1603 cap->version = EM28XX_VERSION_CODE;
1604 cap->capabilities = V4L2_CAP_TUNER;
1605 return 0;
1606}
1607
1608static int radio_g_tuner(struct file *file, void *priv,
1609 struct v4l2_tuner *t)
1610{
1611 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1612
1613 if (unlikely(t->index > 0))
1614 return -EINVAL;
1615
1616 strcpy(t->name, "Radio");
1617 t->type = V4L2_TUNER_RADIO;
1618
1619 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1620 return 0;
1621}
1622
1623static int radio_enum_input(struct file *file, void *priv,
1624 struct v4l2_input *i)
1625{
1626 if (i->index != 0)
1627 return -EINVAL;
1628 strcpy(i->name, "Radio");
1629 i->type = V4L2_INPUT_TYPE_TUNER;
1630
1631 return 0;
1632}
1633
1634static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1635{
1636 if (unlikely(a->index))
1637 return -EINVAL;
1638
1639 strcpy(a->name, "Radio");
1640 return 0;
1641}
1642
1643static int radio_s_tuner(struct file *file, void *priv,
1644 struct v4l2_tuner *t)
1645{
1646 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1647
1648 if (0 != t->index)
1649 return -EINVAL;
1650
1651 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1652
1653 return 0;
1654}
1655
1656static int radio_s_audio(struct file *file, void *fh,
1657 struct v4l2_audio *a)
1658{
1659 return 0;
1660}
1661
1662static int radio_s_input(struct file *file, void *fh, unsigned int i)
1663{
1664 return 0;
1665}
1666
1667static int radio_queryctrl(struct file *file, void *priv,
1668 struct v4l2_queryctrl *qc)
1669{
1670 int i;
1671
1672 if (qc->id < V4L2_CID_BASE ||
1673 qc->id >= V4L2_CID_LASTP1)
1674 return -EINVAL;
1675
1676 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1677 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1678 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1679 return 0;
1680 }
1681 }
1682
1683 return -EINVAL;
1684}
1685
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001686/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001687 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001688 * inits the device and starts isoc transfer
1689 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001690static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001691{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001692 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001693 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001694 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001695 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001696 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001697
Trent Piephoa991f442007-10-10 05:37:43 -03001698 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001699 if (h->vdev->minor == minor) {
1700 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001701 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001702 }
1703 if (h->vbi_dev->minor == minor) {
1704 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001705 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001706 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001707 if (h->radio_dev &&
1708 h->radio_dev->minor == minor) {
1709 radio = 1;
1710 dev = h;
1711 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001712 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001713 if (NULL == dev)
1714 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001715
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001716 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001717 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001719 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001720
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001721 if (!fh) {
1722 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1723 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001724 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001725 mutex_lock(&dev->lock);
1726 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001727 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001728 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001729 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001731 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001732 dev->width = norm_maxw(dev);
1733 dev->height = norm_maxh(dev);
1734 dev->frame_size = dev->width * dev->height * 2;
1735 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1736 dev->bytesperline = dev->width * 2;
1737 dev->hscale = 0;
1738 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001739
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001740 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001741 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001742
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001743 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001744 if (fh->radio) {
1745 em28xx_videodbg("video_open: setting radio device\n");
1746 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1747 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748
1749 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001750
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001751 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1752 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1753 sizeof(struct em28xx_buffer), fh);
1754
Ingo Molnar3593cab2006-02-07 06:49:14 -02001755 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001756 return errCode;
1757}
1758
1759/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001760 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001761 * unregisters the v4l2,i2c and usb devices
1762 * called when the device gets disconected or at module unload
1763*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001764static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001767 /*FIXME: I2C IR should be disconnected */
1768
1769 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1770 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1771 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001772 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001773 if (dev->radio_dev) {
1774 if (-1 != dev->radio_dev->minor)
1775 video_unregister_device(dev->radio_dev);
1776 else
1777 video_device_release(dev->radio_dev);
1778 dev->radio_dev = NULL;
1779 }
1780 if (dev->vbi_dev) {
1781 if (-1 != dev->vbi_dev->minor)
1782 video_unregister_device(dev->vbi_dev);
1783 else
1784 video_device_release(dev->vbi_dev);
1785 dev->vbi_dev = NULL;
1786 }
1787 if (dev->vdev) {
1788 if (-1 != dev->vdev->minor)
1789 video_unregister_device(dev->vdev);
1790 else
1791 video_device_release(dev->vdev);
1792 dev->vdev = NULL;
1793 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001794 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001795 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001796
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001797 /* Mark device as unused */
1798 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001799}
1800
1801/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001802 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1804 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001805static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001807 struct em28xx_fh *fh = filp->private_data;
1808 struct em28xx *dev = fh->dev;
1809 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001810
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001811 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001812
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001813
1814 if (res_check(fh))
1815 res_free(fh);
1816
Ingo Molnar3593cab2006-02-07 06:49:14 -02001817 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001818
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001819 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001820 videobuf_stop(&fh->vb_vidq);
1821 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001823 /* the device is already disconnect,
1824 free the remaining resources */
1825 if (dev->state & DEV_DISCONNECTED) {
1826 em28xx_release_resources(dev);
1827 mutex_unlock(&dev->lock);
1828 kfree(dev);
1829 return 0;
1830 }
1831
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001832 /* do this before setting alternate! */
1833 em28xx_uninit_isoc(dev);
1834
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001835 /* set alternate 0 */
1836 dev->alt = 0;
1837 em28xx_videodbg("setting alternate 0\n");
1838 errCode = usb_set_interface(dev->udev, 0, 0);
1839 if (errCode < 0) {
1840 em28xx_errdev("cannot change alternate number to "
1841 "0 (error=%i)\n", errCode);
1842 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001843 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001844 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845 dev->users--;
1846 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001847 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001848 return 0;
1849}
1850
1851/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001852 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001853 * will allocate buffers when called for the first time
1854 */
1855static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001856em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001857 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001858{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001859 struct em28xx_fh *fh = filp->private_data;
1860 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001861 int rc;
1862
1863 rc = check_dev(dev);
1864 if (rc < 0)
1865 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001866
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001867 /* FIXME: read() is not prepared to allow changing the video
1868 resolution while streaming. Seems a bug at em28xx_set_fmt
1869 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001870
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001871 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1872 if (unlikely(res_get(fh)))
1873 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001874
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001875 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1876 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001877 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001878 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001879}
1880
1881/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001882 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001883 * will allocate buffers when called for the first time
1884 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001885static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001886{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001887 struct em28xx_fh *fh = filp->private_data;
1888 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001889 int rc;
1890
1891 rc = check_dev(dev);
1892 if (rc < 0)
1893 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001894
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001895 if (unlikely(res_get(fh) < 0))
1896 return POLLERR;
1897
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001898 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1899 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001900
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001901 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902}
1903
1904/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001905 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001906 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001907static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001908{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001909 struct em28xx_fh *fh = filp->private_data;
1910 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001911 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001912
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001913 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001914 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001915
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001916 rc = check_dev(dev);
1917 if (rc < 0)
1918 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001919
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001920 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001921
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001922 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1923 (unsigned long)vma->vm_start,
1924 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1925 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001926
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001927 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001928}
1929
Arjan van de Venfa027c22007-02-12 00:55:33 -08001930static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001931 .owner = THIS_MODULE,
1932 .open = em28xx_v4l2_open,
1933 .release = em28xx_v4l2_close,
1934 .read = em28xx_v4l2_read,
1935 .poll = em28xx_v4l2_poll,
1936 .mmap = em28xx_v4l2_mmap,
1937 .ioctl = video_ioctl2,
1938 .llseek = no_llseek,
1939 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001940};
1941
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001942static const struct file_operations radio_fops = {
1943 .owner = THIS_MODULE,
1944 .open = em28xx_v4l2_open,
1945 .release = em28xx_v4l2_close,
1946 .ioctl = video_ioctl2,
1947 .compat_ioctl = v4l_compat_ioctl32,
1948 .llseek = no_llseek,
1949};
1950
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001951static const struct video_device em28xx_video_template = {
1952 .fops = &em28xx_v4l_fops,
1953 .release = video_device_release,
1954
1955 .minor = -1,
1956 .vidioc_querycap = vidioc_querycap,
1957 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1958 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1959 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1960 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1961 .vidioc_g_audio = vidioc_g_audio,
1962 .vidioc_s_audio = vidioc_s_audio,
1963 .vidioc_cropcap = vidioc_cropcap,
1964
1965 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1966 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1967 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1968
1969 .vidioc_reqbufs = vidioc_reqbufs,
1970 .vidioc_querybuf = vidioc_querybuf,
1971 .vidioc_qbuf = vidioc_qbuf,
1972 .vidioc_dqbuf = vidioc_dqbuf,
1973 .vidioc_s_std = vidioc_s_std,
1974 .vidioc_enum_input = vidioc_enum_input,
1975 .vidioc_g_input = vidioc_g_input,
1976 .vidioc_s_input = vidioc_s_input,
1977 .vidioc_queryctrl = vidioc_queryctrl,
1978 .vidioc_g_ctrl = vidioc_g_ctrl,
1979 .vidioc_s_ctrl = vidioc_s_ctrl,
1980 .vidioc_streamon = vidioc_streamon,
1981 .vidioc_streamoff = vidioc_streamoff,
1982 .vidioc_g_tuner = vidioc_g_tuner,
1983 .vidioc_s_tuner = vidioc_s_tuner,
1984 .vidioc_g_frequency = vidioc_g_frequency,
1985 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001986#ifdef CONFIG_VIDEO_ADV_DEBUG
1987 .vidioc_g_register = vidioc_g_register,
1988 .vidioc_s_register = vidioc_s_register,
1989#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001990#ifdef CONFIG_VIDEO_V4L1_COMPAT
1991 .vidiocgmbuf = vidiocgmbuf,
1992#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001993
1994 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001995 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001996};
1997
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001998static struct video_device em28xx_radio_template = {
1999 .name = "em28xx-radio",
2000 .type = VID_TYPE_TUNER,
2001 .fops = &radio_fops,
2002 .minor = -1,
2003 .vidioc_querycap = radio_querycap,
2004 .vidioc_g_tuner = radio_g_tuner,
2005 .vidioc_enum_input = radio_enum_input,
2006 .vidioc_g_audio = radio_g_audio,
2007 .vidioc_s_tuner = radio_s_tuner,
2008 .vidioc_s_audio = radio_s_audio,
2009 .vidioc_s_input = radio_s_input,
2010 .vidioc_queryctrl = radio_queryctrl,
2011 .vidioc_g_ctrl = vidioc_g_ctrl,
2012 .vidioc_s_ctrl = vidioc_s_ctrl,
2013 .vidioc_g_frequency = vidioc_g_frequency,
2014 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03002015#ifdef CONFIG_VIDEO_ADV_DEBUG
2016 .vidioc_g_register = vidioc_g_register,
2017 .vidioc_s_register = vidioc_s_register,
2018#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002019};
2020
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002021/******************************** usb interface *****************************************/
2022
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002023
2024static LIST_HEAD(em28xx_extension_devlist);
2025static DEFINE_MUTEX(em28xx_extension_devlist_lock);
2026
2027int em28xx_register_extension(struct em28xx_ops *ops)
2028{
2029 struct em28xx *h, *dev = NULL;
2030
2031 list_for_each_entry(h, &em28xx_devlist, devlist)
2032 dev = h;
2033
2034 mutex_lock(&em28xx_extension_devlist_lock);
2035 list_add_tail(&ops->next, &em28xx_extension_devlist);
2036 if (dev)
2037 ops->init(dev);
2038
2039 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
2040 mutex_unlock(&em28xx_extension_devlist_lock);
2041
2042 return 0;
2043}
2044EXPORT_SYMBOL(em28xx_register_extension);
2045
2046void em28xx_unregister_extension(struct em28xx_ops *ops)
2047{
2048 struct em28xx *h, *dev = NULL;
2049
2050 list_for_each_entry(h, &em28xx_devlist, devlist)
2051 dev = h;
2052
2053 if (dev)
2054 ops->fini(dev);
2055
2056 mutex_lock(&em28xx_extension_devlist_lock);
2057 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
2058 list_del(&ops->next);
2059 mutex_unlock(&em28xx_extension_devlist_lock);
2060}
2061EXPORT_SYMBOL(em28xx_unregister_extension);
2062
Adrian Bunk532fe652008-01-28 22:10:48 -03002063static struct video_device *em28xx_vdev_init(struct em28xx *dev,
2064 const struct video_device *template,
2065 const int type,
2066 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002067{
2068 struct video_device *vfd;
2069
2070 vfd = video_device_alloc();
2071 if (NULL == vfd)
2072 return NULL;
2073 *vfd = *template;
2074 vfd->minor = -1;
2075 vfd->dev = &dev->udev->dev;
2076 vfd->release = video_device_release;
2077 vfd->type = type;
2078
2079 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
2080 dev->name, type_name);
2081
2082 return vfd;
2083}
2084
2085
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002086/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002087 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088 * allocates and inits the device structs, registers i2c bus and v4l device
2089 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002090static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002091 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002092{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002093 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002094 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002095 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002096 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002097 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002098
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002099 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02002100 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002101 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002102 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002103 init_waitqueue_head(&dev->wait_frame);
2104 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002105
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002106 dev->em28xx_write_regs = em28xx_write_regs;
2107 dev->em28xx_read_reg = em28xx_read_reg;
2108 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2109 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2110 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002111 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002112
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002113 errCode = em28xx_read_reg(dev, CHIPID_REG);
2114 if (errCode >= 0)
2115 em28xx_info("em28xx chip ID = %d\n", errCode);
2116
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002117 em28xx_pre_card_setup(dev);
2118
2119 errCode = em28xx_config(dev);
2120 if (errCode) {
2121 em28xx_errdev("error configuring device\n");
2122 em28xx_devused &= ~(1<<dev->devno);
2123 kfree(dev);
2124 return -ENOMEM;
2125 }
2126
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002127 /* register i2c bus */
2128 em28xx_i2c_register(dev);
2129
2130 /* Do board specific init and eeprom reading */
2131 em28xx_card_setup(dev);
2132
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002133 /* Configure audio */
2134 em28xx_audio_analog_set(dev);
2135
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002136 /* configure the device */
2137 em28xx_config_i2c(dev);
2138
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002139 /* set default norm */
2140 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002141
2142 maxw = norm_maxw(dev);
2143 maxh = norm_maxh(dev);
2144
2145 /* set default image size */
2146 dev->width = maxw;
2147 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002148 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002149 dev->field_size = dev->width * dev->height;
2150 dev->frame_size =
2151 dev->interlaced ? dev->field_size << 1 : dev->field_size;
2152 dev->bytesperline = dev->width * 2;
2153 dev->hscale = 0;
2154 dev->vscale = 0;
2155 dev->ctl_input = 2;
2156
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002157 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002159 list_add_tail(&dev->devlist, &em28xx_devlist);
2160
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002161 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002162 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
2163 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002164 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002165 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002166 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002167 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03002168 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002169 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002170
2171 /* register v4l2 video video_device */
2172 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2173 video_nr[dev->devno]);
2174 if (retval) {
2175 em28xx_errdev("unable to register video device (error=%i).\n",
2176 retval);
2177 goto fail_unreg;
2178 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002179
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002180 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002181 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
2182 VFL_TYPE_VBI, "vbi");
2183 /* register v4l2 vbi video_device */
2184 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2185 vbi_nr[dev->devno]) < 0) {
2186 em28xx_errdev("unable to register vbi device\n");
2187 retval = -ENODEV;
2188 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002189 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002190
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002191 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2192 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2193 VFL_TYPE_RADIO, "radio");
2194 if (NULL == dev->radio_dev) {
2195 em28xx_errdev("cannot allocate video_device.\n");
2196 goto fail_unreg;
2197 }
2198 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2199 radio_nr[dev->devno]);
2200 if (retval < 0) {
2201 em28xx_errdev("can't register radio device\n");
2202 goto fail_unreg;
2203 }
2204 em28xx_info("Registered radio device as /dev/radio%d\n",
2205 dev->radio_dev->minor & 0x1f);
2206 }
2207
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002208 /* init video dma queues */
2209 INIT_LIST_HEAD(&dev->vidq.active);
2210 INIT_LIST_HEAD(&dev->vidq.queued);
2211
2212 dev->vidq.timeout.function = em28xx_vid_timeout;
2213 dev->vidq.timeout.data = (unsigned long)dev;
2214 init_timer(&dev->vidq.timeout);
2215
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002216
Sascha Sommer5a804152007-11-03 21:22:38 -03002217 if (dev->has_msp34xx) {
2218 /* Send a reset to other chips via gpio */
2219 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2220 msleep(3);
2221 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2222 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002223 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002224
Sascha Sommer5a804152007-11-03 21:22:38 -03002225 video_mux(dev, 0);
2226
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002227 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2228 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2229 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002230
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002231 mutex_lock(&em28xx_extension_devlist_lock);
2232 if (!list_empty(&em28xx_extension_devlist)) {
2233 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2234 if (ops->id)
2235 ops->init(dev);
2236 }
2237 }
2238 mutex_unlock(&em28xx_extension_devlist_lock);
2239
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002240 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002241
2242fail_unreg:
2243 em28xx_release_resources(dev);
2244 mutex_unlock(&dev->lock);
2245 kfree(dev);
2246 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002247}
2248
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002249#if defined(CONFIG_MODULES) && defined(MODULE)
2250static void request_module_async(struct work_struct *work)
2251{
2252 struct em28xx *dev = container_of(work,
2253 struct em28xx, request_module_wk);
2254
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002255 if (dev->has_audio_class)
2256 request_module("snd-usb-audio");
2257 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002258 request_module("em28xx-alsa");
2259}
2260
2261static void request_modules(struct em28xx *dev)
2262{
2263 INIT_WORK(&dev->request_module_wk, request_module_async);
2264 schedule_work(&dev->request_module_wk);
2265}
2266#else
2267#define request_modules(dev)
2268#endif /* CONFIG_MODULES */
2269
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002270/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002271 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002272 * checks for supported devices
2273 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002274static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002275 const struct usb_device_id *id)
2276{
2277 const struct usb_endpoint_descriptor *endpoint;
2278 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002279 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002280 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002281 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002282 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002283
2284 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002285 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2286
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002287 /* Check to see next free device and mark as used */
2288 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2289 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002290
2291 /* Don't register audio interfaces */
2292 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002293 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002294 udev->descriptor.idVendor,udev->descriptor.idProduct,
2295 ifnum,
2296 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002297
2298 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002299 return -ENODEV;
2300 }
2301
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002302 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002303 udev->descriptor.idVendor,udev->descriptor.idProduct,
2304 ifnum,
2305 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002306
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002307 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2308
Michael Opdenacker59c51592007-05-09 08:57:56 +02002309 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002310 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2311 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002312 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002313 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002314 return -ENODEV;
2315 }
2316 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002317 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002318 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002319 return -ENODEV;
2320 }
2321
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002322 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002323 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002324 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002325 return -ENOMEM;
2326 }
2327
2328 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002329 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002330 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002331 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002332 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002333 return -ENOMEM;
2334 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002335
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002336 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002337 dev->devno = nr;
2338 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002339 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002340
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002341 /* Checks if audio is provided by some interface */
2342 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2343 uif = udev->config->interface[i];
2344 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2345 dev->has_audio_class = 1;
2346 break;
2347 }
2348 }
2349
2350 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2351 dev->has_audio_class ? "Has" : "Doesn't have");
2352
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002353 /* compute alternate max packet sizes */
2354 uif = udev->actconfig->interface[0];
2355
2356 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002357 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002358// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2359 dev->alt_max_pkt_size = kmalloc(32*
2360 dev->num_alt,GFP_KERNEL);
2361 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002362 em28xx_errdev("out of memory!\n");
2363 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002364 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002365 return -ENOMEM;
2366 }
2367
2368 for (i = 0; i < dev->num_alt ; i++) {
2369 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2370 wMaxPacketSize);
2371 dev->alt_max_pkt_size[i] =
2372 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002373 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002374 dev->alt_max_pkt_size[i]);
2375 }
2376
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002377 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002378 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002379
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002380 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002381 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002382 if (retval)
2383 return retval;
2384
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002385 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002386
2387 /* save our data pointer in this interface device */
2388 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002389
2390 request_modules(dev);
2391
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002392 return 0;
2393}
2394
2395/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002396 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002397 * called when the device gets diconencted
2398 * video device will be unregistered on v4l2_close in case it is still open
2399 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002400static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002401{
Sascha Sommer5a804152007-11-03 21:22:38 -03002402 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002403 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002404
2405 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002406 usb_set_intfdata(interface, NULL);
2407
2408 if (!dev)
2409 return;
2410
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002411 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002412
Sascha Sommer5a804152007-11-03 21:22:38 -03002413 /* wait until all current v4l2 io is finished then deallocate resources */
2414 mutex_lock(&dev->lock);
2415
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002416 wake_up_interruptible_all(&dev->open);
2417
2418 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002419 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002421 "deallocation are deferred on close.\n",
2422 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2423
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002424 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002425 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002426 dev->state |= DEV_DISCONNECTED;
2427 wake_up_interruptible(&dev->wait_frame);
2428 wake_up_interruptible(&dev->wait_stream);
2429 } else {
2430 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002431 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002432 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002433 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002434
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002435 mutex_lock(&em28xx_extension_devlist_lock);
2436 if (!list_empty(&em28xx_extension_devlist)) {
2437 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2438 ops->fini(dev);
2439 }
2440 }
2441 mutex_unlock(&em28xx_extension_devlist_lock);
2442
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002443 if (!dev->users) {
2444 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002445 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002446 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002447}
2448
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002449static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002450 .name = "em28xx",
2451 .probe = em28xx_usb_probe,
2452 .disconnect = em28xx_usb_disconnect,
2453 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002454};
2455
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002456static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002457{
2458 int result;
2459
2460 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002461 (EM28XX_VERSION_CODE >> 16) & 0xff,
2462 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002463#ifdef SNAPSHOT
2464 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2465 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2466#endif
2467
2468 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002469 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002470 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002471 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002472 " usb_register failed. Error number %d.\n", result);
2473
2474 return result;
2475}
2476
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002477static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002478{
2479 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002480 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002481}
2482
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002483module_init(em28xx_module_init);
2484module_exit(em28xx_module_exit);