blob: e478ccba03781070f54c4eec4451c8724ab9afca [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08004
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08005 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
6 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03007 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08008 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08009
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -020010 Some parts based on SN9C10x PC Camera Controllers GPL driver made
11 by Luca Risolia <luca.risolia@studio.unibo.it>
12
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080013 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/init.h>
29#include <linux/list.h>
30#include <linux/module.h>
31#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020032#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080034#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080035#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030036#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020037#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080038
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080039#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020040#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030041#include <media/v4l2-ioctl.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030042#include <media/msp3400.h>
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -030043#include <media/tuner.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080044
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080045#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
46 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030047 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080049
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080050#define DRIVER_NAME "em28xx"
51#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030052#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080053
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080054#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080055 if (video_debug) \
56 printk(KERN_INFO "%s %s :"fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030057 dev->name, __func__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080058
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030059static unsigned int isoc_debug;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -030060module_param(isoc_debug, int, 0644);
61MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030062
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030063#define em28xx_isocdbg(fmt, arg...) \
64do {\
65 if (isoc_debug) { \
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030066 printk(KERN_INFO "%s %s :"fmt, \
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030067 dev->name, __func__ , ##arg); \
68 } \
69 } while (0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -030070
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080071MODULE_AUTHOR(DRIVER_AUTHOR);
72MODULE_DESCRIPTION(DRIVER_DESC);
73MODULE_LICENSE("GPL");
74
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080075static LIST_HEAD(em28xx_devlist);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -030076static DEFINE_MUTEX(em28xx_devlist_mutex);
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;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030093module_param(video_debug, int, 0644);
94MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080095
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,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300111 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200112 .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 Chehabad0ebb92008-04-13 14:37:52 -0300136 /* Advice that buffer was filled */
137 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
138 buf->vb.state = VIDEOBUF_DONE;
139 buf->vb.field_count++;
140 do_gettimeofday(&buf->vb.ts);
141
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300142 dev->isoc_ctl.buf = NULL;
143
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300144 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 Chehab44dc7332008-04-13 15:11:08 -0300159 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300160
161 if (dma_q->pos + len > buf->vb.size)
162 len = buf->vb.size - dma_q->pos;
163
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300164 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300165 em28xx_isocdbg("frame is not complete\n");
166 len += 4;
167 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300168 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300169
170 startread = p;
171 remain = len;
172
173 /* Interlaces frame */
174 if (buf->top_field)
175 fieldstart = outp;
176 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300177 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300178
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300179 linesdone = dma_q->pos / bytesperline;
180 currlinedone = dma_q->pos % bytesperline;
181 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300182 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300183 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300184 lencopy = lencopy > remain ? remain : lencopy;
185
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300186 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300187 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300188 ((char *)startwrite + lencopy) -
189 ((char *)outp + buf->vb.size));
190 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300191 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300192 if (lencopy <= 0)
193 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300194 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300195
196 remain -= lencopy;
197
198 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300199 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300200 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300201 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300202 lencopy = remain;
203 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300204 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300205
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300206 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300207 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300208 ((char *)startwrite + lencopy) -
209 ((char *)outp + buf->vb.size));
210 lencopy = remain = (char *)outp + buf->vb.size -
211 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300212 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300213 if (lencopy <= 0)
214 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300215
216 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300217
218 remain -= lencopy;
219 }
220
221 dma_q->pos += len;
222}
223
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300224static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300225 int packet, int status)
226{
227 char *errmsg = "Unknown";
228
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300229 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300230 case -ENOENT:
231 errmsg = "unlinked synchronuously";
232 break;
233 case -ECONNRESET:
234 errmsg = "unlinked asynchronuously";
235 break;
236 case -ENOSR:
237 errmsg = "Buffer error (overrun)";
238 break;
239 case -EPIPE:
240 errmsg = "Stalled (device not responding)";
241 break;
242 case -EOVERFLOW:
243 errmsg = "Babble (bad cable?)";
244 break;
245 case -EPROTO:
246 errmsg = "Bit-stuff error (bad cable?)";
247 break;
248 case -EILSEQ:
249 errmsg = "CRC/Timeout (could be anything)";
250 break;
251 case -ETIME:
252 errmsg = "Device does not respond";
253 break;
254 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300255 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300256 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
257 } else {
258 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
259 packet, status, errmsg);
260 }
261}
262
263/*
264 * video-buf generic routine to get the next available buffer
265 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300266static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300267 struct em28xx_buffer **buf)
268{
269 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300270 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300271
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300272 if (list_empty(&dma_q->active)) {
273 em28xx_isocdbg("No active queue to serve\n");
274 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300275 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300276 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300277 }
278
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300279 /* Get the next buffer */
280 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
281
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300282 /* Cleans up buffer - Usefull for testing for frame/URB loss */
283 outp = videobuf_to_vmalloc(&(*buf)->vb);
284 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300285
286 dev->isoc_ctl.buf = *buf;
287
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300288 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300289}
290
291/*
292 * Controls the isoc copy of each urb packet
293 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300294static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300295{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300296 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300297 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300298 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300299 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300300 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300301
302 if (!dev)
303 return 0;
304
305 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
306 return 0;
307
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300308 if (urb->status < 0) {
309 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300310 if (urb->status == -ENOENT)
311 return 0;
312 }
313
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300314 buf = dev->isoc_ctl.buf;
315 if (buf != NULL)
316 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300317
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300318 for (i = 0; i < urb->number_of_packets; i++) {
319 int status = urb->iso_frame_desc[i].status;
320
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300321 if (status < 0) {
322 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300323 if (urb->iso_frame_desc[i].status != -EPROTO)
324 continue;
325 }
326
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300327 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300328
329 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300330 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300331 continue;
332 }
333 if (urb->iso_frame_desc[i].actual_length >
334 dev->max_pkt_size) {
335 em28xx_isocdbg("packet bigger than packet size");
336 continue;
337 }
338
339 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300340
341 /* FIXME: incomplete buffer checks where removed to make
342 logic simpler. Impacts of those changes should be evaluated
343 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300344 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
345 em28xx_isocdbg("VBI HEADER!!!\n");
346 /* FIXME: Should add vbi copy */
347 continue;
348 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300349 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300350 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
351 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300352
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300353 if (!(p[2] & 1)) {
354 if (buf != NULL)
355 buffer_filled(dev, dma_q, buf);
356 get_next_buf(dma_q, &buf);
357 if (buf == NULL)
358 outp = NULL;
359 else
360 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300361 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300362
363 if (buf != NULL) {
364 if (p[2] & 1)
365 buf->top_field = 0;
366 else
367 buf->top_field = 1;
368 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300369
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300371 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300372 if (buf != NULL)
373 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300374 }
375 return rc;
376}
377
378/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300379 Videobuf operations
380 ------------------------------------------------------------------*/
381
382static int
383buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
384{
385 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300386 struct em28xx *dev = fh->dev;
387 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300388
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300389 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300390 if (0 == *count)
391 *count = EM28XX_DEF_BUF;
392
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300393 if (*count < EM28XX_MIN_BUF)
394 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300395
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300396 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300397 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300398 f.frequency = dev->ctl_freq;
399
400 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300402 return 0;
403}
404
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300405/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300406static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
407{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300408 struct em28xx_fh *fh = vq->priv_data;
409 struct em28xx *dev = fh->dev;
410 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300411 if (in_interrupt())
412 BUG();
413
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300414 /* We used to wait for the buffer to finish here, but this didn't work
415 because, as we were keeping the state as VIDEOBUF_QUEUED,
416 videobuf_queue_cancel marked it as finished for us.
417 (Also, it could wedge forever if the hardware was misconfigured.)
418
419 This should be safe; by the time we get here, the buffer isn't
420 queued anymore. If we ever start marking the buffers as
421 VIDEOBUF_ACTIVE, it won't be, though.
422 */
423 spin_lock_irqsave(&dev->slock, flags);
424 if (dev->isoc_ctl.buf == buf)
425 dev->isoc_ctl.buf = NULL;
426 spin_unlock_irqrestore(&dev->slock, flags);
427
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300428 videobuf_vmalloc_free(&buf->vb);
429 buf->vb.state = VIDEOBUF_NEEDS_INIT;
430}
431
432static int
433buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
434 enum v4l2_field field)
435{
436 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300437 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300438 struct em28xx *dev = fh->dev;
439 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441 /* FIXME: It assumes depth = 16 */
442 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300443 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300444
445 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
446 return -EINVAL;
447
Brandon Philips05612972008-04-13 14:57:01 -0300448 buf->vb.width = dev->width;
449 buf->vb.height = dev->height;
450 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300451
452 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300453 rc = videobuf_iolock(vq, &buf->vb, NULL);
454 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 }
457
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300458 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300459 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300460
461 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300462 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
463 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300464 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300465 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300466 goto fail;
467 }
468
469 buf->vb.state = VIDEOBUF_PREPARED;
470 return 0;
471
472fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300473 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300474 return rc;
475}
476
477static void
478buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
479{
480 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
481 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300482 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300485 buf->vb.state = VIDEOBUF_QUEUED;
486 list_add_tail(&buf->vb.queue, &vidq->active);
487
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300488}
489
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300490static void buffer_release(struct videobuf_queue *vq,
491 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300492{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300493 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300494 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300495 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300496
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300497 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300498
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300499 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300500}
501
502static struct videobuf_queue_ops em28xx_video_qops = {
503 .buf_setup = buffer_setup,
504 .buf_prepare = buffer_prepare,
505 .buf_queue = buffer_queue,
506 .buf_release = buffer_release,
507};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800508
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300509/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800512 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800513 * inits registers with sane defaults
514 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800515static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800516{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300517 int retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800518
519 /* Sets I2C speed to 100 KHz */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300520 if (!dev->is_em2800) {
521 retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
522 if (retval < 0) {
523 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
524 __func__, retval);
525 return retval;
526 }
527 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800528
529 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200530
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300531/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
532/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
533 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200534
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800535 dev->mute = 1; /* maybe not the right place... */
536 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300537
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538 em28xx_outfmt_set_yuv422(dev);
539 em28xx_colorlevels_set_default(dev);
540 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800541
542 return 0;
543}
544
545/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800546 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800547 * configure i2c attached devices
548 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800549static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300551 struct v4l2_routing route;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300552 int zero = 0;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300553
554 route.input = INPUT(dev->ctl_input)->vmux;
555 route.output = 0;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300556 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300557 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300558 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800559}
560
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800561static void video_mux(struct em28xx *dev, int index)
562{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300563 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800564
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300565 route.input = INPUT(index)->vmux;
566 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800567 dev->ctl_input = index;
568 dev->ctl_ainput = INPUT(index)->amux;
569
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300570 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800571
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800572 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300573 if (dev->i2s_speed) {
574 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
575 &dev->i2s_speed);
576 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300577 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300578 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300579 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300580 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
581 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800582 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300583
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300584 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800585}
586
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300587/* Usage lock check functions */
588static int res_get(struct em28xx_fh *fh)
589{
590 struct em28xx *dev = fh->dev;
591 int rc = 0;
592
593 /* This instance already has stream_on */
594 if (fh->stream_on)
595 return rc;
596
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300597 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300598 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300599
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300600 mutex_lock(&dev->lock);
601 dev->stream_on = 1;
602 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300603 mutex_unlock(&dev->lock);
604 return rc;
605}
606
607static int res_check(struct em28xx_fh *fh)
608{
609 return (fh->stream_on);
610}
611
612static void res_free(struct em28xx_fh *fh)
613{
614 struct em28xx *dev = fh->dev;
615
616 mutex_lock(&dev->lock);
617 fh->stream_on = 0;
618 dev->stream_on = 0;
619 mutex_unlock(&dev->lock);
620}
621
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800622/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300623 * em28xx_get_ctrl()
624 * return the current saturation, brightness or contrast, mute state
625 */
626static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
627{
628 switch (ctrl->id) {
629 case V4L2_CID_AUDIO_MUTE:
630 ctrl->value = dev->mute;
631 return 0;
632 case V4L2_CID_AUDIO_VOLUME:
633 ctrl->value = dev->volume;
634 return 0;
635 default:
636 return -EINVAL;
637 }
638}
639
640/*
641 * em28xx_set_ctrl()
642 * mute or set new saturation, brightness or contrast
643 */
644static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
645{
646 switch (ctrl->id) {
647 case V4L2_CID_AUDIO_MUTE:
648 if (ctrl->value != dev->mute) {
649 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300650 return em28xx_audio_analog_set(dev);
651 }
652 return 0;
653 case V4L2_CID_AUDIO_VOLUME:
654 dev->volume = ctrl->value;
655 return em28xx_audio_analog_set(dev);
656 default:
657 return -EINVAL;
658 }
659}
660
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300661static int check_dev(struct em28xx *dev)
662{
663 if (dev->state & DEV_DISCONNECTED) {
664 em28xx_errdev("v4l2 ioctl: device not present\n");
665 return -ENODEV;
666 }
667
668 if (dev->state & DEV_MISCONFIGURED) {
669 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
670 "close and open it again\n");
671 return -EIO;
672 }
673 return 0;
674}
675
676static void get_scale(struct em28xx *dev,
677 unsigned int width, unsigned int height,
678 unsigned int *hscale, unsigned int *vscale)
679{
680 unsigned int maxw = norm_maxw(dev);
681 unsigned int maxh = norm_maxh(dev);
682
683 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
684 if (*hscale >= 0x4000)
685 *hscale = 0x3fff;
686
687 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
688 if (*vscale >= 0x4000)
689 *vscale = 0x3fff;
690}
691
692/* ------------------------------------------------------------------
693 IOCTL vidioc handling
694 ------------------------------------------------------------------*/
695
Hans Verkuil78b526a2008-05-28 12:16:41 -0300696static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300697 struct v4l2_format *f)
698{
699 struct em28xx_fh *fh = priv;
700 struct em28xx *dev = fh->dev;
701
702 mutex_lock(&dev->lock);
703
704 f->fmt.pix.width = dev->width;
705 f->fmt.pix.height = dev->height;
706 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300707 f->fmt.pix.bytesperline = dev->width * 2;
708 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300709 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
710
711 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
712 f->fmt.pix.field = dev->interlaced ?
713 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
714
715 mutex_unlock(&dev->lock);
716 return 0;
717}
718
Hans Verkuil78b526a2008-05-28 12:16:41 -0300719static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300720 struct v4l2_format *f)
721{
722 struct em28xx_fh *fh = priv;
723 struct em28xx *dev = fh->dev;
724 int width = f->fmt.pix.width;
725 int height = f->fmt.pix.height;
726 unsigned int maxw = norm_maxw(dev);
727 unsigned int maxh = norm_maxh(dev);
728 unsigned int hscale, vscale;
729
730 /* width must even because of the YUYV format
731 height must be even because of interlacing */
732 height &= 0xfffe;
733 width &= 0xfffe;
734
735 if (height < 32)
736 height = 32;
737 if (height > maxh)
738 height = maxh;
739 if (width < 48)
740 width = 48;
741 if (width > maxw)
742 width = maxw;
743
744 mutex_lock(&dev->lock);
745
746 if (dev->is_em2800) {
747 /* the em2800 can only scale down to 50% */
748 if (height % (maxh / 2))
749 height = maxh;
750 if (width % (maxw / 2))
751 width = maxw;
752 /* according to empiatech support */
753 /* the MaxPacketSize is to small to support */
754 /* framesizes larger than 640x480 @ 30 fps */
755 /* or 640x576 @ 25 fps. As this would cut */
756 /* of a part of the image we prefer */
757 /* 360x576 or 360x480 for now */
758 if (width == maxw && height == maxh)
759 width /= 2;
760 }
761
762 get_scale(dev, width, height, &hscale, &vscale);
763
764 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
765 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
766
767 f->fmt.pix.width = width;
768 f->fmt.pix.height = height;
769 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
770 f->fmt.pix.bytesperline = width * 2;
771 f->fmt.pix.sizeimage = width * 2 * height;
772 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
773 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
774
775 mutex_unlock(&dev->lock);
776 return 0;
777}
778
Hans Verkuil78b526a2008-05-28 12:16:41 -0300779static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300780 struct v4l2_format *f)
781{
782 struct em28xx_fh *fh = priv;
783 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300784 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300785
786 rc = check_dev(dev);
787 if (rc < 0)
788 return rc;
789
Hans Verkuil78b526a2008-05-28 12:16:41 -0300790 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300791
792 mutex_lock(&dev->lock);
793
Brandon Philips05612972008-04-13 14:57:01 -0300794 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
795 em28xx_errdev("%s queue busy\n", __func__);
796 rc = -EBUSY;
797 goto out;
798 }
799
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300800 if (dev->stream_on && !fh->stream_on) {
801 em28xx_errdev("%s device in use by another fh\n", __func__);
802 rc = -EBUSY;
803 goto out;
804 }
805
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300806 /* set new image size */
807 dev->width = f->fmt.pix.width;
808 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
810
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300812 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813
Brandon Philips05612972008-04-13 14:57:01 -0300814 rc = 0;
815
816out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300818 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300819}
820
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300821static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300822{
823 struct em28xx_fh *fh = priv;
824 struct em28xx *dev = fh->dev;
825 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300826 int rc;
827
828 rc = check_dev(dev);
829 if (rc < 0)
830 return rc;
831
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300832 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300833 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300834 mutex_unlock(&dev->lock);
835
836 /* Adjusts width/height, if needed */
837 f.fmt.pix.width = dev->width;
838 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300839 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300840
841 mutex_lock(&dev->lock);
842
843 /* set new image size */
844 dev->width = f.fmt.pix.width;
845 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300846 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
847
848 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300849 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300850
851 mutex_unlock(&dev->lock);
852 return 0;
853}
854
855static const char *iname[] = {
856 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
857 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
858 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
859 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
860 [EM28XX_VMUX_SVIDEO] = "S-Video",
861 [EM28XX_VMUX_TELEVISION] = "Television",
862 [EM28XX_VMUX_CABLE] = "Cable TV",
863 [EM28XX_VMUX_DVB] = "DVB",
864 [EM28XX_VMUX_DEBUG] = "for debug only",
865};
866
867static int vidioc_enum_input(struct file *file, void *priv,
868 struct v4l2_input *i)
869{
870 struct em28xx_fh *fh = priv;
871 struct em28xx *dev = fh->dev;
872 unsigned int n;
873
874 n = i->index;
875 if (n >= MAX_EM28XX_INPUT)
876 return -EINVAL;
877 if (0 == INPUT(n)->type)
878 return -EINVAL;
879
880 i->index = n;
881 i->type = V4L2_INPUT_TYPE_CAMERA;
882
883 strcpy(i->name, iname[INPUT(n)->type]);
884
885 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
886 (EM28XX_VMUX_CABLE == INPUT(n)->type))
887 i->type = V4L2_INPUT_TYPE_TUNER;
888
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300889 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300890
891 return 0;
892}
893
894static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
895{
896 struct em28xx_fh *fh = priv;
897 struct em28xx *dev = fh->dev;
898
899 *i = dev->ctl_input;
900
901 return 0;
902}
903
904static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
905{
906 struct em28xx_fh *fh = priv;
907 struct em28xx *dev = fh->dev;
908 int rc;
909
910 rc = check_dev(dev);
911 if (rc < 0)
912 return rc;
913
914 if (i >= MAX_EM28XX_INPUT)
915 return -EINVAL;
916 if (0 == INPUT(i)->type)
917 return -EINVAL;
918
919 mutex_lock(&dev->lock);
920
921 video_mux(dev, i);
922
923 mutex_unlock(&dev->lock);
924 return 0;
925}
926
927static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
928{
929 struct em28xx_fh *fh = priv;
930 struct em28xx *dev = fh->dev;
931 unsigned int index = a->index;
932
933 if (a->index > 1)
934 return -EINVAL;
935
936 index = dev->ctl_ainput;
937
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300938 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300939 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300940 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300941 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300942
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300943 a->capability = V4L2_AUDCAP_STEREO;
944 a->index = index;
945
946 return 0;
947}
948
949static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
950{
951 struct em28xx_fh *fh = priv;
952 struct em28xx *dev = fh->dev;
953
954 if (a->index != dev->ctl_ainput)
955 return -EINVAL;
956
957 return 0;
958}
959
960static int vidioc_queryctrl(struct file *file, void *priv,
961 struct v4l2_queryctrl *qc)
962{
963 struct em28xx_fh *fh = priv;
964 struct em28xx *dev = fh->dev;
965 int id = qc->id;
966 int i;
967 int rc;
968
969 rc = check_dev(dev);
970 if (rc < 0)
971 return rc;
972
973 memset(qc, 0, sizeof(*qc));
974
975 qc->id = id;
976
977 if (!dev->has_msp34xx) {
978 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
979 if (qc->id && qc->id == em28xx_qctrl[i].id) {
980 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
981 return 0;
982 }
983 }
984 }
985 mutex_lock(&dev->lock);
986 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
987 mutex_unlock(&dev->lock);
988
989 if (qc->type)
990 return 0;
991 else
992 return -EINVAL;
993}
994
995static int vidioc_g_ctrl(struct file *file, void *priv,
996 struct v4l2_control *ctrl)
997{
998 struct em28xx_fh *fh = priv;
999 struct em28xx *dev = fh->dev;
1000 int rc;
1001
1002 rc = check_dev(dev);
1003 if (rc < 0)
1004 return rc;
1005 mutex_lock(&dev->lock);
1006
1007 if (!dev->has_msp34xx)
1008 rc = em28xx_get_ctrl(dev, ctrl);
1009 else
1010 rc = -EINVAL;
1011
1012 if (rc == -EINVAL) {
1013 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1014 rc = 0;
1015 }
1016
1017 mutex_unlock(&dev->lock);
1018 return rc;
1019}
1020
1021static int vidioc_s_ctrl(struct file *file, void *priv,
1022 struct v4l2_control *ctrl)
1023{
1024 struct em28xx_fh *fh = priv;
1025 struct em28xx *dev = fh->dev;
1026 u8 i;
1027 int rc;
1028
1029 rc = check_dev(dev);
1030 if (rc < 0)
1031 return rc;
1032
1033 mutex_lock(&dev->lock);
1034
1035 if (dev->has_msp34xx)
1036 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1037 else {
1038 rc = 1;
1039 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1040 if (ctrl->id == em28xx_qctrl[i].id) {
1041 if (ctrl->value < em28xx_qctrl[i].minimum ||
1042 ctrl->value > em28xx_qctrl[i].maximum) {
1043 rc = -ERANGE;
1044 break;
1045 }
1046
1047 rc = em28xx_set_ctrl(dev, ctrl);
1048 break;
1049 }
1050 }
1051 }
1052
1053 /* Control not found - try to send it to the attached devices */
1054 if (rc == 1) {
1055 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1056 rc = 0;
1057 }
1058
1059 mutex_unlock(&dev->lock);
1060 return rc;
1061}
1062
1063static int vidioc_g_tuner(struct file *file, void *priv,
1064 struct v4l2_tuner *t)
1065{
1066 struct em28xx_fh *fh = priv;
1067 struct em28xx *dev = fh->dev;
1068 int rc;
1069
1070 rc = check_dev(dev);
1071 if (rc < 0)
1072 return rc;
1073
1074 if (0 != t->index)
1075 return -EINVAL;
1076
1077 strcpy(t->name, "Tuner");
1078
1079 mutex_lock(&dev->lock);
1080
1081 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1082
1083 mutex_unlock(&dev->lock);
1084 return 0;
1085}
1086
1087static int vidioc_s_tuner(struct file *file, void *priv,
1088 struct v4l2_tuner *t)
1089{
1090 struct em28xx_fh *fh = priv;
1091 struct em28xx *dev = fh->dev;
1092 int rc;
1093
1094 rc = check_dev(dev);
1095 if (rc < 0)
1096 return rc;
1097
1098 if (0 != t->index)
1099 return -EINVAL;
1100
1101 mutex_lock(&dev->lock);
1102
1103 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1104
1105 mutex_unlock(&dev->lock);
1106 return 0;
1107}
1108
1109static int vidioc_g_frequency(struct file *file, void *priv,
1110 struct v4l2_frequency *f)
1111{
1112 struct em28xx_fh *fh = priv;
1113 struct em28xx *dev = fh->dev;
1114
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001115 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001116 f->frequency = dev->ctl_freq;
1117
1118 return 0;
1119}
1120
1121static int vidioc_s_frequency(struct file *file, void *priv,
1122 struct v4l2_frequency *f)
1123{
1124 struct em28xx_fh *fh = priv;
1125 struct em28xx *dev = fh->dev;
1126 int rc;
1127
1128 rc = check_dev(dev);
1129 if (rc < 0)
1130 return rc;
1131
1132 if (0 != f->tuner)
1133 return -EINVAL;
1134
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001135 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1136 return -EINVAL;
1137 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001138 return -EINVAL;
1139
1140 mutex_lock(&dev->lock);
1141
1142 dev->ctl_freq = f->frequency;
1143 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1144
1145 mutex_unlock(&dev->lock);
1146 return 0;
1147}
1148
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001149#ifdef CONFIG_VIDEO_ADV_DEBUG
1150static int em28xx_reg_len(int reg)
1151{
1152 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001153 case EM28XX_R40_AC97LSB:
1154 case EM28XX_R30_HSCALELOW:
1155 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001156 return 2;
1157 default:
1158 return 1;
1159 }
1160}
1161
1162static int vidioc_g_register(struct file *file, void *priv,
1163 struct v4l2_register *reg)
1164{
1165 struct em28xx_fh *fh = priv;
1166 struct em28xx *dev = fh->dev;
1167 int ret;
1168
1169 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1170 return -EINVAL;
1171
1172 if (em28xx_reg_len(reg->reg) == 1) {
1173 ret = em28xx_read_reg(dev, reg->reg);
1174 if (ret < 0)
1175 return ret;
1176
1177 reg->val = ret;
1178 } else {
Al Viroa954b662008-05-21 00:32:51 -03001179 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001180 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1181 reg->reg, (char *)&val, 2);
1182 if (ret < 0)
1183 return ret;
1184
Al Viroa954b662008-05-21 00:32:51 -03001185 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001186 }
1187
1188 return 0;
1189}
1190
1191static int vidioc_s_register(struct file *file, void *priv,
1192 struct v4l2_register *reg)
1193{
1194 struct em28xx_fh *fh = priv;
1195 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001196 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001197
Al Viroa954b662008-05-21 00:32:51 -03001198 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001199
1200 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1201 em28xx_reg_len(reg->reg));
1202}
1203#endif
1204
1205
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001206static int vidioc_cropcap(struct file *file, void *priv,
1207 struct v4l2_cropcap *cc)
1208{
1209 struct em28xx_fh *fh = priv;
1210 struct em28xx *dev = fh->dev;
1211
1212 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1213 return -EINVAL;
1214
1215 cc->bounds.left = 0;
1216 cc->bounds.top = 0;
1217 cc->bounds.width = dev->width;
1218 cc->bounds.height = dev->height;
1219 cc->defrect = cc->bounds;
1220 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1221 cc->pixelaspect.denominator = 59;
1222
1223 return 0;
1224}
1225
1226static int vidioc_streamon(struct file *file, void *priv,
1227 enum v4l2_buf_type type)
1228{
1229 struct em28xx_fh *fh = priv;
1230 struct em28xx *dev = fh->dev;
1231 int rc;
1232
1233 rc = check_dev(dev);
1234 if (rc < 0)
1235 return rc;
1236
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001237
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001238 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001239 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001240
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001241 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001242}
1243
1244static int vidioc_streamoff(struct file *file, void *priv,
1245 enum v4l2_buf_type type)
1246{
1247 struct em28xx_fh *fh = priv;
1248 struct em28xx *dev = fh->dev;
1249 int rc;
1250
1251 rc = check_dev(dev);
1252 if (rc < 0)
1253 return rc;
1254
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001255 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1256 return -EINVAL;
1257 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001258 return -EINVAL;
1259
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001260 videobuf_streamoff(&fh->vb_vidq);
1261 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001262
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001263 return 0;
1264}
1265
1266static int vidioc_querycap(struct file *file, void *priv,
1267 struct v4l2_capability *cap)
1268{
1269 struct em28xx_fh *fh = priv;
1270 struct em28xx *dev = fh->dev;
1271
1272 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1273 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001274 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001275
1276 cap->version = EM28XX_VERSION_CODE;
1277
1278 cap->capabilities =
1279 V4L2_CAP_SLICED_VBI_CAPTURE |
1280 V4L2_CAP_VIDEO_CAPTURE |
1281 V4L2_CAP_AUDIO |
1282 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1283
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001284 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001285 cap->capabilities |= V4L2_CAP_TUNER;
1286
1287 return 0;
1288}
1289
Hans Verkuil78b526a2008-05-28 12:16:41 -03001290static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001291 struct v4l2_fmtdesc *fmtd)
1292{
1293 if (fmtd->index != 0)
1294 return -EINVAL;
1295
1296 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1297 strcpy(fmtd->description, "Packed YUY2");
1298 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1299 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1300
1301 return 0;
1302}
1303
1304/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001305static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306 struct v4l2_format *f)
1307{
1308 struct em28xx_fh *fh = priv;
1309 struct em28xx *dev = fh->dev;
1310 int rc;
1311
1312 rc = check_dev(dev);
1313 if (rc < 0)
1314 return rc;
1315
1316 mutex_lock(&dev->lock);
1317
1318 f->fmt.sliced.service_set = 0;
1319
1320 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1321
1322 if (f->fmt.sliced.service_set == 0)
1323 rc = -EINVAL;
1324
1325 mutex_unlock(&dev->lock);
1326 return rc;
1327}
1328
Hans Verkuil78b526a2008-05-28 12:16:41 -03001329static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001330 struct v4l2_format *f)
1331{
1332 struct em28xx_fh *fh = priv;
1333 struct em28xx *dev = fh->dev;
1334 int rc;
1335
1336 rc = check_dev(dev);
1337 if (rc < 0)
1338 return rc;
1339
1340 mutex_lock(&dev->lock);
1341 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1342 mutex_unlock(&dev->lock);
1343
1344 if (f->fmt.sliced.service_set == 0)
1345 return -EINVAL;
1346
1347 return 0;
1348}
1349
1350
1351static int vidioc_reqbufs(struct file *file, void *priv,
1352 struct v4l2_requestbuffers *rb)
1353{
1354 struct em28xx_fh *fh = priv;
1355 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001356 int rc;
1357
1358 rc = check_dev(dev);
1359 if (rc < 0)
1360 return rc;
1361
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001362 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001363}
1364
1365static int vidioc_querybuf(struct file *file, void *priv,
1366 struct v4l2_buffer *b)
1367{
1368 struct em28xx_fh *fh = priv;
1369 struct em28xx *dev = fh->dev;
1370 int rc;
1371
1372 rc = check_dev(dev);
1373 if (rc < 0)
1374 return rc;
1375
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001376 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001377}
1378
1379static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1380{
1381 struct em28xx_fh *fh = priv;
1382 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001383 int rc;
1384
1385 rc = check_dev(dev);
1386 if (rc < 0)
1387 return rc;
1388
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001389 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001390}
1391
1392static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1393{
1394 struct em28xx_fh *fh = priv;
1395 struct em28xx *dev = fh->dev;
1396 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001397
1398 rc = check_dev(dev);
1399 if (rc < 0)
1400 return rc;
1401
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001402 return (videobuf_dqbuf(&fh->vb_vidq, b,
1403 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001404}
1405
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001406#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001407static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001408{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001409 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001410
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001411 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001412}
1413#endif
1414
1415
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001416/* ----------------------------------------------------------- */
1417/* RADIO ESPECIFIC IOCTLS */
1418/* ----------------------------------------------------------- */
1419
1420static int radio_querycap(struct file *file, void *priv,
1421 struct v4l2_capability *cap)
1422{
1423 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1424
1425 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1426 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001427 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001428
1429 cap->version = EM28XX_VERSION_CODE;
1430 cap->capabilities = V4L2_CAP_TUNER;
1431 return 0;
1432}
1433
1434static int radio_g_tuner(struct file *file, void *priv,
1435 struct v4l2_tuner *t)
1436{
1437 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1438
1439 if (unlikely(t->index > 0))
1440 return -EINVAL;
1441
1442 strcpy(t->name, "Radio");
1443 t->type = V4L2_TUNER_RADIO;
1444
1445 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1446 return 0;
1447}
1448
1449static int radio_enum_input(struct file *file, void *priv,
1450 struct v4l2_input *i)
1451{
1452 if (i->index != 0)
1453 return -EINVAL;
1454 strcpy(i->name, "Radio");
1455 i->type = V4L2_INPUT_TYPE_TUNER;
1456
1457 return 0;
1458}
1459
1460static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1461{
1462 if (unlikely(a->index))
1463 return -EINVAL;
1464
1465 strcpy(a->name, "Radio");
1466 return 0;
1467}
1468
1469static int radio_s_tuner(struct file *file, void *priv,
1470 struct v4l2_tuner *t)
1471{
1472 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1473
1474 if (0 != t->index)
1475 return -EINVAL;
1476
1477 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1478
1479 return 0;
1480}
1481
1482static int radio_s_audio(struct file *file, void *fh,
1483 struct v4l2_audio *a)
1484{
1485 return 0;
1486}
1487
1488static int radio_s_input(struct file *file, void *fh, unsigned int i)
1489{
1490 return 0;
1491}
1492
1493static int radio_queryctrl(struct file *file, void *priv,
1494 struct v4l2_queryctrl *qc)
1495{
1496 int i;
1497
1498 if (qc->id < V4L2_CID_BASE ||
1499 qc->id >= V4L2_CID_LASTP1)
1500 return -EINVAL;
1501
1502 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1503 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1504 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1505 return 0;
1506 }
1507 }
1508
1509 return -EINVAL;
1510}
1511
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001512/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001513 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001514 * inits the device and starts isoc transfer
1515 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001516static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001517{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001518 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001519 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001520 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001521 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001522 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001523
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001524 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001525 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001526 if (h->vdev->minor == minor) {
1527 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001528 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001529 }
1530 if (h->vbi_dev->minor == minor) {
1531 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001532 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001533 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001534 if (h->radio_dev &&
1535 h->radio_dev->minor == minor) {
1536 radio = 1;
1537 dev = h;
1538 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001539 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001540 mutex_unlock(&em28xx_devlist_mutex);
1541 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001542 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001543
1544 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001545
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001546 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001547 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001548
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001549
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001550 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001551 if (!fh) {
1552 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001553 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001554 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001555 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001556 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001557 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001558 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001559 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001560
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001561 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001562 dev->width = norm_maxw(dev);
1563 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001564 dev->hscale = 0;
1565 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001566
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001567 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001568 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001569 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001571 /* Needed, since GPIO might have disabled power of
1572 some i2c device
1573 */
1574 em28xx_config_i2c(dev);
1575
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001576 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001577 if (fh->radio) {
1578 em28xx_videodbg("video_open: setting radio device\n");
1579 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1580 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001581
1582 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001583
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001584 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1585 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1586 sizeof(struct em28xx_buffer), fh);
1587
Ingo Molnar3593cab2006-02-07 06:49:14 -02001588 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001589
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590 return errCode;
1591}
1592
1593/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001594 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001595 * unregisters the v4l2,i2c and usb devices
1596 * called when the device gets disconected or at module unload
1597*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001598static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001600
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001601 /*FIXME: I2C IR should be disconnected */
1602
Markus Rechberger9c755412005-11-08 21:37:52 -08001603 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001604 if (dev->sbutton_input_dev)
1605 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001606 if (dev->radio_dev) {
1607 if (-1 != dev->radio_dev->minor)
1608 video_unregister_device(dev->radio_dev);
1609 else
1610 video_device_release(dev->radio_dev);
1611 dev->radio_dev = NULL;
1612 }
1613 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001614 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1615 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001616 if (-1 != dev->vbi_dev->minor)
1617 video_unregister_device(dev->vbi_dev);
1618 else
1619 video_device_release(dev->vbi_dev);
1620 dev->vbi_dev = NULL;
1621 }
1622 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001623 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1624 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001625 if (-1 != dev->vdev->minor)
1626 video_unregister_device(dev->vdev);
1627 else
1628 video_device_release(dev->vdev);
1629 dev->vdev = NULL;
1630 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001631 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001632 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001633
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001634 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001635 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001636}
1637
1638/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001639 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001640 * stops streaming and deallocates all resources allocated by the v4l2
1641 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001643static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001644{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001645 struct em28xx_fh *fh = filp->private_data;
1646 struct em28xx *dev = fh->dev;
1647 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001648
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001649 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001650
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001651
1652 if (res_check(fh))
1653 res_free(fh);
1654
Ingo Molnar3593cab2006-02-07 06:49:14 -02001655 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001656
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001657 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001658 videobuf_stop(&fh->vb_vidq);
1659 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001661 /* the device is already disconnect,
1662 free the remaining resources */
1663 if (dev->state & DEV_DISCONNECTED) {
1664 em28xx_release_resources(dev);
1665 mutex_unlock(&dev->lock);
1666 kfree(dev);
1667 return 0;
1668 }
1669
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001670 /* do this before setting alternate! */
1671 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001672 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001673
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001674 /* set alternate 0 */
1675 dev->alt = 0;
1676 em28xx_videodbg("setting alternate 0\n");
1677 errCode = usb_set_interface(dev->udev, 0, 0);
1678 if (errCode < 0) {
1679 em28xx_errdev("cannot change alternate number to "
1680 "0 (error=%i)\n", errCode);
1681 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001682 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001683 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001684 dev->users--;
1685 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001686 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001687 return 0;
1688}
1689
1690/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001691 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001692 * will allocate buffers when called for the first time
1693 */
1694static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001695em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001696 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001697{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001698 struct em28xx_fh *fh = filp->private_data;
1699 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001700 int rc;
1701
1702 rc = check_dev(dev);
1703 if (rc < 0)
1704 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001705
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001706 /* FIXME: read() is not prepared to allow changing the video
1707 resolution while streaming. Seems a bug at em28xx_set_fmt
1708 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001709
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001710 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1711 if (unlikely(res_get(fh)))
1712 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001713
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001714 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1715 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001716 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001717 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001718}
1719
1720/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001721 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001722 * will allocate buffers when called for the first time
1723 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001724static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001726 struct em28xx_fh *fh = filp->private_data;
1727 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001728 int rc;
1729
1730 rc = check_dev(dev);
1731 if (rc < 0)
1732 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001733
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001734 if (unlikely(res_get(fh) < 0))
1735 return POLLERR;
1736
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001737 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1738 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001739
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001740 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001741}
1742
1743/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001744 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001745 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001746static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001748 struct em28xx_fh *fh = filp->private_data;
1749 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001750 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001751
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001752 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001753 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001754
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001755 rc = check_dev(dev);
1756 if (rc < 0)
1757 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001758
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001759 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001761 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1762 (unsigned long)vma->vm_start,
1763 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1764 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001766 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001767}
1768
Arjan van de Venfa027c22007-02-12 00:55:33 -08001769static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001770 .owner = THIS_MODULE,
1771 .open = em28xx_v4l2_open,
1772 .release = em28xx_v4l2_close,
1773 .read = em28xx_v4l2_read,
1774 .poll = em28xx_v4l2_poll,
1775 .mmap = em28xx_v4l2_mmap,
1776 .ioctl = video_ioctl2,
1777 .llseek = no_llseek,
1778 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001779};
1780
Hans Verkuila3998102008-07-21 02:57:38 -03001781static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001782 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001783 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1784 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1785 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1786 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001787 .vidioc_g_audio = vidioc_g_audio,
1788 .vidioc_s_audio = vidioc_s_audio,
1789 .vidioc_cropcap = vidioc_cropcap,
1790
Hans Verkuil78b526a2008-05-28 12:16:41 -03001791 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1792 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1793 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001794
1795 .vidioc_reqbufs = vidioc_reqbufs,
1796 .vidioc_querybuf = vidioc_querybuf,
1797 .vidioc_qbuf = vidioc_qbuf,
1798 .vidioc_dqbuf = vidioc_dqbuf,
1799 .vidioc_s_std = vidioc_s_std,
1800 .vidioc_enum_input = vidioc_enum_input,
1801 .vidioc_g_input = vidioc_g_input,
1802 .vidioc_s_input = vidioc_s_input,
1803 .vidioc_queryctrl = vidioc_queryctrl,
1804 .vidioc_g_ctrl = vidioc_g_ctrl,
1805 .vidioc_s_ctrl = vidioc_s_ctrl,
1806 .vidioc_streamon = vidioc_streamon,
1807 .vidioc_streamoff = vidioc_streamoff,
1808 .vidioc_g_tuner = vidioc_g_tuner,
1809 .vidioc_s_tuner = vidioc_s_tuner,
1810 .vidioc_g_frequency = vidioc_g_frequency,
1811 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001812#ifdef CONFIG_VIDEO_ADV_DEBUG
1813 .vidioc_g_register = vidioc_g_register,
1814 .vidioc_s_register = vidioc_s_register,
1815#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001816#ifdef CONFIG_VIDEO_V4L1_COMPAT
1817 .vidiocgmbuf = vidiocgmbuf,
1818#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001819};
1820
1821static const struct video_device em28xx_video_template = {
1822 .fops = &em28xx_v4l_fops,
1823 .release = video_device_release,
1824 .ioctl_ops = &video_ioctl_ops,
1825
1826 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001827
1828 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001829 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001830};
1831
Hans Verkuila3998102008-07-21 02:57:38 -03001832static const struct file_operations radio_fops = {
1833 .owner = THIS_MODULE,
1834 .open = em28xx_v4l2_open,
1835 .release = em28xx_v4l2_close,
1836 .ioctl = video_ioctl2,
1837 .compat_ioctl = v4l_compat_ioctl32,
1838 .llseek = no_llseek,
1839};
1840
1841static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001842 .vidioc_querycap = radio_querycap,
1843 .vidioc_g_tuner = radio_g_tuner,
1844 .vidioc_enum_input = radio_enum_input,
1845 .vidioc_g_audio = radio_g_audio,
1846 .vidioc_s_tuner = radio_s_tuner,
1847 .vidioc_s_audio = radio_s_audio,
1848 .vidioc_s_input = radio_s_input,
1849 .vidioc_queryctrl = radio_queryctrl,
1850 .vidioc_g_ctrl = vidioc_g_ctrl,
1851 .vidioc_s_ctrl = vidioc_s_ctrl,
1852 .vidioc_g_frequency = vidioc_g_frequency,
1853 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001854#ifdef CONFIG_VIDEO_ADV_DEBUG
1855 .vidioc_g_register = vidioc_g_register,
1856 .vidioc_s_register = vidioc_s_register,
1857#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001858};
1859
Hans Verkuila3998102008-07-21 02:57:38 -03001860static struct video_device em28xx_radio_template = {
1861 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001862 .fops = &radio_fops,
1863 .ioctl_ops = &radio_ioctl_ops,
1864 .minor = -1,
1865};
1866
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001867/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001868
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001869
1870static LIST_HEAD(em28xx_extension_devlist);
1871static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1872
1873int em28xx_register_extension(struct em28xx_ops *ops)
1874{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001875 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001876
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001877 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001878 mutex_lock(&em28xx_extension_devlist_lock);
1879 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001880 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1881 if (dev)
1882 ops->init(dev);
1883 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001884 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1885 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001886 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001887 return 0;
1888}
1889EXPORT_SYMBOL(em28xx_register_extension);
1890
1891void em28xx_unregister_extension(struct em28xx_ops *ops)
1892{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001893 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001894
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001895 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001896 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1897 if (dev)
1898 ops->fini(dev);
1899 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001900
1901 mutex_lock(&em28xx_extension_devlist_lock);
1902 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1903 list_del(&ops->next);
1904 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001905 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001906}
1907EXPORT_SYMBOL(em28xx_unregister_extension);
1908
Adrian Bunk532fe652008-01-28 22:10:48 -03001909static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1910 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001911 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001912{
1913 struct video_device *vfd;
1914
1915 vfd = video_device_alloc();
1916 if (NULL == vfd)
1917 return NULL;
1918 *vfd = *template;
1919 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001920 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001921 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001922 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001923
1924 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1925 dev->name, type_name);
1926
1927 return vfd;
1928}
1929
1930
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001931static int register_analog_devices(struct em28xx *dev)
1932{
1933 int ret;
1934
1935 /* allocate and fill video video_device struct */
1936 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
1937 if (!dev->vdev) {
1938 em28xx_errdev("cannot allocate video_device.\n");
1939 return -ENODEV;
1940 }
1941
1942 /* register v4l2 video video_device */
1943 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1944 video_nr[dev->devno]);
1945 if (ret) {
1946 em28xx_errdev("unable to register video device (error=%i).\n",
1947 ret);
1948 return ret;
1949 }
1950
1951 /* Allocate and fill vbi video_device struct */
1952 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
1953
1954 /* register v4l2 vbi video_device */
1955 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1956 vbi_nr[dev->devno]);
1957 if (ret < 0) {
1958 em28xx_errdev("unable to register vbi device\n");
1959 return ret;
1960 }
1961
1962 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1963 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
1964 if (!dev->radio_dev) {
1965 em28xx_errdev("cannot allocate video_device.\n");
1966 return -ENODEV;
1967 }
1968 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1969 radio_nr[dev->devno]);
1970 if (ret < 0) {
1971 em28xx_errdev("can't register radio device\n");
1972 return ret;
1973 }
1974 em28xx_info("Registered radio device as /dev/radio%d\n",
1975 dev->radio_dev->num);
1976 }
1977
1978 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1979 dev->vdev->num, dev->vbi_dev->num);
1980
1981 return 0;
1982}
1983
1984
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001985/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001986 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001987 * allocates and inits the device structs, registers i2c bus and v4l device
1988 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001989static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001990 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001991{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001992 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001993 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001994 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001995 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001996 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001997
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001998 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001999 mutex_init(&dev->lock);
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002000 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002001 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002002 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002003 init_waitqueue_head(&dev->wait_frame);
2004 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002005
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002006 dev->em28xx_write_regs = em28xx_write_regs;
2007 dev->em28xx_read_reg = em28xx_read_reg;
2008 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2009 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2010 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03002011 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002012
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002013 em28xx_pre_card_setup(dev);
2014
2015 errCode = em28xx_config(dev);
2016 if (errCode) {
2017 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002018 return -ENOMEM;
2019 }
2020
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002021 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002022 errCode = em28xx_i2c_register(dev);
2023 if (errCode < 0) {
2024 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2025 __func__, errCode);
2026 return errCode;
2027 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002028
2029 /* Do board specific init and eeprom reading */
2030 em28xx_card_setup(dev);
2031
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002032 /* Configure audio */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002033 errCode = em28xx_audio_analog_set(dev);
2034 if (errCode < 0) {
2035 em28xx_errdev("%s: em28xx_audio_analog_set - errCode [%d]!\n",
2036 __func__, errCode);
2037 return errCode;
2038 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002039
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002040 /* configure the device */
2041 em28xx_config_i2c(dev);
2042
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002043 /* set default norm */
2044 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002045
2046 maxw = norm_maxw(dev);
2047 maxh = norm_maxh(dev);
2048
2049 /* set default image size */
2050 dev->width = maxw;
2051 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002052 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002053 dev->hscale = 0;
2054 dev->vscale = 0;
2055 dev->ctl_input = 2;
2056
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002057 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002058 if (errCode < 0) {
2059 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2060 __func__, errCode);
2061 return errCode;
2062 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002063
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002064 /* init video dma queues */
2065 INIT_LIST_HEAD(&dev->vidq.active);
2066 INIT_LIST_HEAD(&dev->vidq.queued);
2067
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002068
Sascha Sommer5a804152007-11-03 21:22:38 -03002069 if (dev->has_msp34xx) {
2070 /* Send a reset to other chips via gpio */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002071 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2072 if (errCode < 0) {
2073 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2074 __func__, errCode);
2075 return errCode;
2076 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002077 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002078
2079 errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2080 if (errCode < 0) {
2081 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2082 __func__, errCode);
2083 return errCode;
2084 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002085 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002086 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002087
Sascha Sommer5a804152007-11-03 21:22:38 -03002088 video_mux(dev, 0);
2089
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002090 mutex_lock(&em28xx_devlist_mutex);
2091 list_add_tail(&dev->devlist, &em28xx_devlist);
2092 retval = register_analog_devices(dev);
2093 if (retval < 0) {
2094 em28xx_release_resources(dev);
2095 mutex_unlock(&em28xx_devlist_mutex);
2096 goto fail_reg_devices;
2097 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002098
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002099 mutex_lock(&em28xx_extension_devlist_lock);
2100 if (!list_empty(&em28xx_extension_devlist)) {
2101 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2102 if (ops->id)
2103 ops->init(dev);
2104 }
2105 }
2106 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002107 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002108
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002109 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002110
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002111fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002112 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002113 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002114}
2115
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002116#if defined(CONFIG_MODULES) && defined(MODULE)
2117static void request_module_async(struct work_struct *work)
2118{
2119 struct em28xx *dev = container_of(work,
2120 struct em28xx, request_module_wk);
2121
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002122 if (dev->has_audio_class)
2123 request_module("snd-usb-audio");
2124 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002125 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002126
2127 if (dev->has_dvb)
2128 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002129}
2130
2131static void request_modules(struct em28xx *dev)
2132{
2133 INIT_WORK(&dev->request_module_wk, request_module_async);
2134 schedule_work(&dev->request_module_wk);
2135}
2136#else
2137#define request_modules(dev)
2138#endif /* CONFIG_MODULES */
2139
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002140/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002141 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002142 * checks for supported devices
2143 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002144static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002145 const struct usb_device_id *id)
2146{
2147 const struct usb_endpoint_descriptor *endpoint;
2148 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002149 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002150 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002151 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002152 int i, nr, ifnum, isoc_pipe;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002153
2154 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002155 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2156
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002157 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002158 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2159 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002160
2161 /* Don't register audio interfaces */
2162 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002163 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002164 udev->descriptor.idVendor,
2165 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002166 ifnum,
2167 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002168
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002169 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002170 return -ENODEV;
2171 }
2172
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002173 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002174 udev->descriptor.idVendor,
2175 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002176 ifnum,
2177 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002178
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002179 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002180
Michael Opdenacker59c51592007-05-09 08:57:56 +02002181 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002182 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2183 USB_ENDPOINT_XFER_ISOC &&
2184 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2185 {
2186 /* It's a newer em2874/em2875 device */
2187 isoc_pipe = 0;
2188 } else {
2189 isoc_pipe = 1;
2190 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2191 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2192 USB_ENDPOINT_XFER_ISOC) {
2193 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2194 em28xx_devused &= ~(1<<nr);
2195 return -ENODEV;
2196 }
2197 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2198 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2199 em28xx_devused &= ~(1<<nr);
2200 return -ENODEV;
2201 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202 }
2203
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002204 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002205 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2206 EM28XX_MAXBOARDS);
2207 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002208 return -ENOMEM;
2209 }
2210
2211 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002212 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002213 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002214 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002215 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002216 return -ENOMEM;
2217 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002218
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002219 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002220 dev->devno = nr;
2221 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002222 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002223
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002224 /* Checks if audio is provided by some interface */
2225 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2226 uif = udev->config->interface[i];
2227 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2228 dev->has_audio_class = 1;
2229 break;
2230 }
2231 }
2232
2233 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2234 dev->has_audio_class ? "Has" : "Doesn't have");
2235
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002236 /* compute alternate max packet sizes */
2237 uif = udev->actconfig->interface[0];
2238
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002239 dev->num_alt = uif->num_altsetting;
2240 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2241/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2242 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2243
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002244 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002245 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002246 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002247 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002248 return -ENOMEM;
2249 }
2250
2251 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002252 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002253 wMaxPacketSize);
2254 dev->alt_max_pkt_size[i] =
2255 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002256 em28xx_info("Alternate setting %i, max size= %i\n", i,
2257 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002258 }
2259
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002260 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002261 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002262
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002263 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002264 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002265 if (retval) {
2266 em28xx_devused &= ~(1<<dev->devno);
2267 kfree(dev);
2268
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002269 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002270 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002271
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002272 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002273
2274 /* save our data pointer in this interface device */
2275 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002276
2277 request_modules(dev);
2278
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002279 return 0;
2280}
2281
2282/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002283 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002284 * called when the device gets diconencted
2285 * video device will be unregistered on v4l2_close in case it is still open
2286 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002287static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002288{
Sascha Sommer5a804152007-11-03 21:22:38 -03002289 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002290 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002291
2292 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002293 usb_set_intfdata(interface, NULL);
2294
2295 if (!dev)
2296 return;
2297
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002298 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002299
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002300 /* wait until all current v4l2 io is finished then deallocate
2301 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002302 mutex_lock(&dev->lock);
2303
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002304 wake_up_interruptible_all(&dev->open);
2305
2306 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002307 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002308 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002309 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002310 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002311
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002312 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002313 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002314 dev->state |= DEV_DISCONNECTED;
2315 wake_up_interruptible(&dev->wait_frame);
2316 wake_up_interruptible(&dev->wait_stream);
2317 } else {
2318 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002319 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002320 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002321 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002322
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002323 mutex_lock(&em28xx_extension_devlist_lock);
2324 if (!list_empty(&em28xx_extension_devlist)) {
2325 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2326 ops->fini(dev);
2327 }
2328 }
2329 mutex_unlock(&em28xx_extension_devlist_lock);
2330
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002331 if (!dev->users) {
2332 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002333 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002334 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002335}
2336
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002337static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002338 .name = "em28xx",
2339 .probe = em28xx_usb_probe,
2340 .disconnect = em28xx_usb_disconnect,
2341 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002342};
2343
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002344static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002345{
2346 int result;
2347
2348 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002349 (EM28XX_VERSION_CODE >> 16) & 0xff,
2350 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002351#ifdef SNAPSHOT
2352 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2353 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2354#endif
2355
2356 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002357 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002358 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002359 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002360 " usb_register failed. Error number %d.\n", result);
2361
2362 return result;
2363}
2364
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002365static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002366{
2367 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002368 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002369}
2370
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002371module_init(em28xx_module_init);
2372module_exit(em28xx_module_exit);