blob: 1b56e78b7744b876591116fcdf21125634b79407 [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 Chehab381aaba2008-12-20 07:43:34 -0300396 /* Ask tuner to go to analog or radio 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;
Mauro Carvalho Chehab381aaba2008-12-20 07:43:34 -0300399 f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300400
401 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
402
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300403 return 0;
404}
405
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300406/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300407static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
408{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300409 struct em28xx_fh *fh = vq->priv_data;
410 struct em28xx *dev = fh->dev;
411 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300412 if (in_interrupt())
413 BUG();
414
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300415 /* We used to wait for the buffer to finish here, but this didn't work
416 because, as we were keeping the state as VIDEOBUF_QUEUED,
417 videobuf_queue_cancel marked it as finished for us.
418 (Also, it could wedge forever if the hardware was misconfigured.)
419
420 This should be safe; by the time we get here, the buffer isn't
421 queued anymore. If we ever start marking the buffers as
422 VIDEOBUF_ACTIVE, it won't be, though.
423 */
424 spin_lock_irqsave(&dev->slock, flags);
425 if (dev->isoc_ctl.buf == buf)
426 dev->isoc_ctl.buf = NULL;
427 spin_unlock_irqrestore(&dev->slock, flags);
428
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300429 videobuf_vmalloc_free(&buf->vb);
430 buf->vb.state = VIDEOBUF_NEEDS_INIT;
431}
432
433static int
434buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
435 enum v4l2_field field)
436{
437 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300438 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439 struct em28xx *dev = fh->dev;
440 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300441
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300442 /* FIXME: It assumes depth = 16 */
443 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300444 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300445
446 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
447 return -EINVAL;
448
Brandon Philips05612972008-04-13 14:57:01 -0300449 buf->vb.width = dev->width;
450 buf->vb.height = dev->height;
451 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300452
453 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300454 rc = videobuf_iolock(vq, &buf->vb, NULL);
455 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300456 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 }
458
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300460 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300461
462 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300463 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
464 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300465 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300466 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300467 goto fail;
468 }
469
470 buf->vb.state = VIDEOBUF_PREPARED;
471 return 0;
472
473fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300474 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300475 return rc;
476}
477
478static void
479buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
480{
481 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
482 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300483 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300484 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300485
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300486 buf->vb.state = VIDEOBUF_QUEUED;
487 list_add_tail(&buf->vb.queue, &vidq->active);
488
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300489}
490
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300491static void buffer_release(struct videobuf_queue *vq,
492 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300496 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300498 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300499
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300500 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300501}
502
503static struct videobuf_queue_ops em28xx_video_qops = {
504 .buf_setup = buffer_setup,
505 .buf_prepare = buffer_prepare,
506 .buf_queue = buffer_queue,
507 .buf_release = buffer_release,
508};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300510/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800511
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800512/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800513 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800514 * inits registers with sane defaults
515 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800516static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800517{
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300518 int retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800519
520 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300521 if (!dev->board.is_em2800) {
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300522 retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -0300523 if (retval < 0) {
524 em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
525 __func__, retval);
526 return retval;
527 }
528 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800529
530 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200531
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -0300532/* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
533/* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
534 em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200535
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800536 dev->mute = 1; /* maybe not the right place... */
537 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300538
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800539 em28xx_outfmt_set_yuv422(dev);
540 em28xx_colorlevels_set_default(dev);
541 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800542
543 return 0;
544}
545
546/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800547 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800548 * configure i2c attached devices
549 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800550static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800551{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300552 struct v4l2_routing route;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300553 int zero = 0;
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300554
555 route.input = INPUT(dev->ctl_input)->vmux;
556 route.output = 0;
Devin Heitmueller231ffc92008-12-16 23:09:35 -0300557 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300558 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300559 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800560}
561
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800562static void video_mux(struct em28xx *dev, int index)
563{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300564 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800565
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300566 route.input = INPUT(index)->vmux;
567 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800568 dev->ctl_input = index;
569 dev->ctl_ainput = INPUT(index)->amux;
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300570 dev->ctl_aoutput = INPUT(index)->aout;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800571
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300572 if (!dev->ctl_aoutput)
573 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
574
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300575 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800576
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300577 if (dev->board.has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300578 if (dev->i2s_speed) {
579 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
580 &dev->i2s_speed);
581 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300582 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300583 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300584 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300585 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
586 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800587 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300588
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300589 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800590}
591
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300592/* Usage lock check functions */
593static int res_get(struct em28xx_fh *fh)
594{
595 struct em28xx *dev = fh->dev;
596 int rc = 0;
597
598 /* This instance already has stream_on */
599 if (fh->stream_on)
600 return rc;
601
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300602 if (dev->stream_on)
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -0300603 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300604
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300605 dev->stream_on = 1;
606 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300607 return rc;
608}
609
610static int res_check(struct em28xx_fh *fh)
611{
612 return (fh->stream_on);
613}
614
615static void res_free(struct em28xx_fh *fh)
616{
617 struct em28xx *dev = fh->dev;
618
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300619 fh->stream_on = 0;
620 dev->stream_on = 0;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300621}
622
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800623/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300624 * em28xx_get_ctrl()
625 * return the current saturation, brightness or contrast, mute state
626 */
627static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
628{
629 switch (ctrl->id) {
630 case V4L2_CID_AUDIO_MUTE:
631 ctrl->value = dev->mute;
632 return 0;
633 case V4L2_CID_AUDIO_VOLUME:
634 ctrl->value = dev->volume;
635 return 0;
636 default:
637 return -EINVAL;
638 }
639}
640
641/*
642 * em28xx_set_ctrl()
643 * mute or set new saturation, brightness or contrast
644 */
645static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
646{
647 switch (ctrl->id) {
648 case V4L2_CID_AUDIO_MUTE:
649 if (ctrl->value != dev->mute) {
650 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300651 return em28xx_audio_analog_set(dev);
652 }
653 return 0;
654 case V4L2_CID_AUDIO_VOLUME:
655 dev->volume = ctrl->value;
656 return em28xx_audio_analog_set(dev);
657 default:
658 return -EINVAL;
659 }
660}
661
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300662static int check_dev(struct em28xx *dev)
663{
664 if (dev->state & DEV_DISCONNECTED) {
665 em28xx_errdev("v4l2 ioctl: device not present\n");
666 return -ENODEV;
667 }
668
669 if (dev->state & DEV_MISCONFIGURED) {
670 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
671 "close and open it again\n");
672 return -EIO;
673 }
674 return 0;
675}
676
677static void get_scale(struct em28xx *dev,
678 unsigned int width, unsigned int height,
679 unsigned int *hscale, unsigned int *vscale)
680{
681 unsigned int maxw = norm_maxw(dev);
682 unsigned int maxh = norm_maxh(dev);
683
684 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
685 if (*hscale >= 0x4000)
686 *hscale = 0x3fff;
687
688 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
689 if (*vscale >= 0x4000)
690 *vscale = 0x3fff;
691}
692
693/* ------------------------------------------------------------------
694 IOCTL vidioc handling
695 ------------------------------------------------------------------*/
696
Hans Verkuil78b526a2008-05-28 12:16:41 -0300697static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300698 struct v4l2_format *f)
699{
700 struct em28xx_fh *fh = priv;
701 struct em28xx *dev = fh->dev;
702
703 mutex_lock(&dev->lock);
704
705 f->fmt.pix.width = dev->width;
706 f->fmt.pix.height = dev->height;
707 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300708 f->fmt.pix.bytesperline = dev->width * 2;
709 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300710 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
711
712 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
713 f->fmt.pix.field = dev->interlaced ?
714 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
715
716 mutex_unlock(&dev->lock);
717 return 0;
718}
719
Hans Verkuil78b526a2008-05-28 12:16:41 -0300720static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300721 struct v4l2_format *f)
722{
723 struct em28xx_fh *fh = priv;
724 struct em28xx *dev = fh->dev;
725 int width = f->fmt.pix.width;
726 int height = f->fmt.pix.height;
727 unsigned int maxw = norm_maxw(dev);
728 unsigned int maxh = norm_maxh(dev);
729 unsigned int hscale, vscale;
730
731 /* width must even because of the YUYV format
732 height must be even because of interlacing */
733 height &= 0xfffe;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300734 width &= 0xfffe;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300735
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300736 if (unlikely(height < 32))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300737 height = 32;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300738 if (unlikely(height > maxh))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300739 height = maxh;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300740 if (unlikely(width < 48))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300741 width = 48;
Mauro Carvalho Chehabcf8c91c2008-12-16 20:36:13 -0300742 if (unlikely(width > maxw))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300743 width = maxw;
744
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300745 if (dev->board.is_em2800) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300746 /* the em2800 can only scale down to 50% */
747 if (height % (maxh / 2))
748 height = maxh;
749 if (width % (maxw / 2))
750 width = maxw;
751 /* according to empiatech support */
752 /* the MaxPacketSize is to small to support */
753 /* framesizes larger than 640x480 @ 30 fps */
754 /* or 640x576 @ 25 fps. As this would cut */
755 /* of a part of the image we prefer */
756 /* 360x576 or 360x480 for now */
757 if (width == maxw && height == maxh)
758 width /= 2;
759 }
760
761 get_scale(dev, width, height, &hscale, &vscale);
762
763 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
764 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
765
766 f->fmt.pix.width = width;
767 f->fmt.pix.height = height;
768 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
769 f->fmt.pix.bytesperline = width * 2;
770 f->fmt.pix.sizeimage = width * 2 * height;
771 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
772 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
773
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300774 return 0;
775}
776
Hans Verkuil78b526a2008-05-28 12:16:41 -0300777static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300778 struct v4l2_format *f)
779{
780 struct em28xx_fh *fh = priv;
781 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300782 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300783
784 rc = check_dev(dev);
785 if (rc < 0)
786 return rc;
787
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300788 mutex_lock(&dev->lock);
789
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300790 vidioc_try_fmt_vid_cap(file, priv, f);
791
Brandon Philips05612972008-04-13 14:57:01 -0300792 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
793 em28xx_errdev("%s queue busy\n", __func__);
794 rc = -EBUSY;
795 goto out;
796 }
797
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300798 if (dev->stream_on && !fh->stream_on) {
799 em28xx_errdev("%s device in use by another fh\n", __func__);
800 rc = -EBUSY;
801 goto out;
802 }
803
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804 /* set new image size */
805 dev->width = f->fmt.pix.width;
806 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300807 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
808
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300809 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300811
Brandon Philips05612972008-04-13 14:57:01 -0300812 rc = 0;
813
814out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300815 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300816 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817}
818
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300819static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300820{
821 struct em28xx_fh *fh = priv;
822 struct em28xx *dev = fh->dev;
823 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300824 int rc;
825
826 rc = check_dev(dev);
827 if (rc < 0)
828 return rc;
829
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300830 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300831 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300832
833 /* Adjusts width/height, if needed */
834 f.fmt.pix.width = dev->width;
835 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300836 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300838 /* set new image size */
839 dev->width = f.fmt.pix.width;
840 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
842
843 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300844 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300845
846 mutex_unlock(&dev->lock);
847 return 0;
848}
849
850static const char *iname[] = {
851 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
852 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
853 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
854 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
855 [EM28XX_VMUX_SVIDEO] = "S-Video",
856 [EM28XX_VMUX_TELEVISION] = "Television",
857 [EM28XX_VMUX_CABLE] = "Cable TV",
858 [EM28XX_VMUX_DVB] = "DVB",
859 [EM28XX_VMUX_DEBUG] = "for debug only",
860};
861
862static int vidioc_enum_input(struct file *file, void *priv,
863 struct v4l2_input *i)
864{
865 struct em28xx_fh *fh = priv;
866 struct em28xx *dev = fh->dev;
867 unsigned int n;
868
869 n = i->index;
870 if (n >= MAX_EM28XX_INPUT)
871 return -EINVAL;
872 if (0 == INPUT(n)->type)
873 return -EINVAL;
874
875 i->index = n;
876 i->type = V4L2_INPUT_TYPE_CAMERA;
877
878 strcpy(i->name, iname[INPUT(n)->type]);
879
880 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
881 (EM28XX_VMUX_CABLE == INPUT(n)->type))
882 i->type = V4L2_INPUT_TYPE_TUNER;
883
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300884 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300885
886 return 0;
887}
888
889static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
890{
891 struct em28xx_fh *fh = priv;
892 struct em28xx *dev = fh->dev;
893
894 *i = dev->ctl_input;
895
896 return 0;
897}
898
899static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
900{
901 struct em28xx_fh *fh = priv;
902 struct em28xx *dev = fh->dev;
903 int rc;
904
905 rc = check_dev(dev);
906 if (rc < 0)
907 return rc;
908
909 if (i >= MAX_EM28XX_INPUT)
910 return -EINVAL;
911 if (0 == INPUT(i)->type)
912 return -EINVAL;
913
914 mutex_lock(&dev->lock);
915
916 video_mux(dev, i);
917
918 mutex_unlock(&dev->lock);
919 return 0;
920}
921
922static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
923{
924 struct em28xx_fh *fh = priv;
925 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300926
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300927 switch (a->index) {
928 case EM28XX_AMUX_VIDEO:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300929 strcpy(a->name, "Television");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300930 break;
931 case EM28XX_AMUX_LINE_IN:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932 strcpy(a->name, "Line In");
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300933 break;
934 case EM28XX_AMUX_VIDEO2:
935 strcpy(a->name, "Television alt");
936 break;
937 case EM28XX_AMUX_PHONE:
938 strcpy(a->name, "Phone");
939 break;
940 case EM28XX_AMUX_MIC:
941 strcpy(a->name, "Mic");
942 break;
943 case EM28XX_AMUX_CD:
944 strcpy(a->name, "CD");
945 break;
946 case EM28XX_AMUX_AUX:
947 strcpy(a->name, "Aux");
948 break;
949 case EM28XX_AMUX_PCM_OUT:
950 strcpy(a->name, "PCM");
951 break;
952 default:
953 return -EINVAL;
954 }
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300955
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300956 a->index = dev->ctl_ainput;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300957 a->capability = V4L2_AUDCAP_STEREO;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300958
959 return 0;
960}
961
962static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
963{
964 struct em28xx_fh *fh = priv;
965 struct em28xx *dev = fh->dev;
966
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300967 mutex_lock(&dev->lock);
968
Mauro Carvalho Chehab35ae6f02008-11-20 12:40:51 -0300969 dev->ctl_ainput = INPUT(a->index)->amux;
970 dev->ctl_aoutput = INPUT(a->index)->aout;
Mauro Carvalho Chehabe879b8e2008-11-20 13:39:39 -0300971
972 if (!dev->ctl_aoutput)
973 dev->ctl_aoutput = EM28XX_AOUT_MASTER;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -0300974
975 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300976 return 0;
977}
978
979static int vidioc_queryctrl(struct file *file, void *priv,
980 struct v4l2_queryctrl *qc)
981{
982 struct em28xx_fh *fh = priv;
983 struct em28xx *dev = fh->dev;
984 int id = qc->id;
985 int i;
986 int rc;
987
988 rc = check_dev(dev);
989 if (rc < 0)
990 return rc;
991
992 memset(qc, 0, sizeof(*qc));
993
994 qc->id = id;
995
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -0300996 if (!dev->board.has_msp34xx) {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300997 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
998 if (qc->id && qc->id == em28xx_qctrl[i].id) {
999 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1000 return 0;
1001 }
1002 }
1003 }
1004 mutex_lock(&dev->lock);
1005 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
1006 mutex_unlock(&dev->lock);
1007
1008 if (qc->type)
1009 return 0;
1010 else
1011 return -EINVAL;
1012}
1013
1014static int vidioc_g_ctrl(struct file *file, void *priv,
1015 struct v4l2_control *ctrl)
1016{
1017 struct em28xx_fh *fh = priv;
1018 struct em28xx *dev = fh->dev;
1019 int rc;
1020
1021 rc = check_dev(dev);
1022 if (rc < 0)
1023 return rc;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001024
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001025 mutex_lock(&dev->lock);
1026
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001027 if (!dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001028 rc = em28xx_get_ctrl(dev, ctrl);
1029 else
1030 rc = -EINVAL;
1031
1032 if (rc == -EINVAL) {
1033 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1034 rc = 0;
1035 }
1036
1037 mutex_unlock(&dev->lock);
1038 return rc;
1039}
1040
1041static int vidioc_s_ctrl(struct file *file, void *priv,
1042 struct v4l2_control *ctrl)
1043{
1044 struct em28xx_fh *fh = priv;
1045 struct em28xx *dev = fh->dev;
1046 u8 i;
1047 int rc;
1048
1049 rc = check_dev(dev);
1050 if (rc < 0)
1051 return rc;
1052
1053 mutex_lock(&dev->lock);
1054
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03001055 if (dev->board.has_msp34xx)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001056 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1057 else {
1058 rc = 1;
1059 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1060 if (ctrl->id == em28xx_qctrl[i].id) {
1061 if (ctrl->value < em28xx_qctrl[i].minimum ||
1062 ctrl->value > em28xx_qctrl[i].maximum) {
1063 rc = -ERANGE;
1064 break;
1065 }
1066
1067 rc = em28xx_set_ctrl(dev, ctrl);
1068 break;
1069 }
1070 }
1071 }
1072
1073 /* Control not found - try to send it to the attached devices */
1074 if (rc == 1) {
1075 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1076 rc = 0;
1077 }
1078
1079 mutex_unlock(&dev->lock);
1080 return rc;
1081}
1082
1083static int vidioc_g_tuner(struct file *file, void *priv,
1084 struct v4l2_tuner *t)
1085{
1086 struct em28xx_fh *fh = priv;
1087 struct em28xx *dev = fh->dev;
1088 int rc;
1089
1090 rc = check_dev(dev);
1091 if (rc < 0)
1092 return rc;
1093
1094 if (0 != t->index)
1095 return -EINVAL;
1096
1097 strcpy(t->name, "Tuner");
1098
1099 mutex_lock(&dev->lock);
1100
1101 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1102
1103 mutex_unlock(&dev->lock);
1104 return 0;
1105}
1106
1107static int vidioc_s_tuner(struct file *file, void *priv,
1108 struct v4l2_tuner *t)
1109{
1110 struct em28xx_fh *fh = priv;
1111 struct em28xx *dev = fh->dev;
1112 int rc;
1113
1114 rc = check_dev(dev);
1115 if (rc < 0)
1116 return rc;
1117
1118 if (0 != t->index)
1119 return -EINVAL;
1120
1121 mutex_lock(&dev->lock);
1122
1123 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1124
1125 mutex_unlock(&dev->lock);
1126 return 0;
1127}
1128
1129static int vidioc_g_frequency(struct file *file, void *priv,
1130 struct v4l2_frequency *f)
1131{
1132 struct em28xx_fh *fh = priv;
1133 struct em28xx *dev = fh->dev;
1134
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001135 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001136 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001137 f->frequency = dev->ctl_freq;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001138 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001139
1140 return 0;
1141}
1142
1143static int vidioc_s_frequency(struct file *file, void *priv,
1144 struct v4l2_frequency *f)
1145{
1146 struct em28xx_fh *fh = priv;
1147 struct em28xx *dev = fh->dev;
1148 int rc;
1149
1150 rc = check_dev(dev);
1151 if (rc < 0)
1152 return rc;
1153
1154 if (0 != f->tuner)
1155 return -EINVAL;
1156
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001157 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1158 return -EINVAL;
1159 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001160 return -EINVAL;
1161
1162 mutex_lock(&dev->lock);
1163
1164 dev->ctl_freq = f->frequency;
1165 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1166
1167 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001168
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001169 return 0;
1170}
1171
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001172#ifdef CONFIG_VIDEO_ADV_DEBUG
1173static int em28xx_reg_len(int reg)
1174{
1175 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001176 case EM28XX_R40_AC97LSB:
1177 case EM28XX_R30_HSCALELOW:
1178 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001179 return 2;
1180 default:
1181 return 1;
1182 }
1183}
1184
1185static int vidioc_g_register(struct file *file, void *priv,
1186 struct v4l2_register *reg)
1187{
1188 struct em28xx_fh *fh = priv;
1189 struct em28xx *dev = fh->dev;
1190 int ret;
1191
1192 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1193 return -EINVAL;
1194
1195 if (em28xx_reg_len(reg->reg) == 1) {
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001196 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001197 ret = em28xx_read_reg(dev, reg->reg);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001198 mutex_unlock(&dev->lock);
1199
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001200 if (ret < 0)
1201 return ret;
1202
1203 reg->val = ret;
1204 } else {
Al Viroa954b662008-05-21 00:32:51 -03001205 __le64 val = 0;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001206 mutex_lock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001207 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1208 reg->reg, (char *)&val, 2);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001209 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001210 if (ret < 0)
1211 return ret;
1212
Al Viroa954b662008-05-21 00:32:51 -03001213 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001214 }
1215
1216 return 0;
1217}
1218
1219static int vidioc_s_register(struct file *file, void *priv,
1220 struct v4l2_register *reg)
1221{
1222 struct em28xx_fh *fh = priv;
1223 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001224 __le64 buf;
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001225 int rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001226
Al Viroa954b662008-05-21 00:32:51 -03001227 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001228
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001229 mutex_lock(&dev->lock);
1230 rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
1231 em28xx_reg_len(reg->reg));
1232 mutex_unlock(&dev->lock);
1233
1234 return rc;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001235}
1236#endif
1237
1238
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001239static int vidioc_cropcap(struct file *file, void *priv,
1240 struct v4l2_cropcap *cc)
1241{
1242 struct em28xx_fh *fh = priv;
1243 struct em28xx *dev = fh->dev;
1244
1245 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1246 return -EINVAL;
1247
1248 cc->bounds.left = 0;
1249 cc->bounds.top = 0;
1250 cc->bounds.width = dev->width;
1251 cc->bounds.height = dev->height;
1252 cc->defrect = cc->bounds;
1253 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1254 cc->pixelaspect.denominator = 59;
1255
1256 return 0;
1257}
1258
1259static int vidioc_streamon(struct file *file, void *priv,
1260 enum v4l2_buf_type type)
1261{
1262 struct em28xx_fh *fh = priv;
1263 struct em28xx *dev = fh->dev;
1264 int rc;
1265
1266 rc = check_dev(dev);
1267 if (rc < 0)
1268 return rc;
1269
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001270
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001271 mutex_lock(&dev->lock);
1272 rc = res_get(fh);
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001273
1274 if (unlikely(rc < 0))
1275 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001276
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001277 rc = videobuf_streamon(&fh->vb_vidq);
1278
1279 mutex_unlock(&dev->lock);
1280
1281 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001282}
1283
1284static int vidioc_streamoff(struct file *file, void *priv,
1285 enum v4l2_buf_type type)
1286{
1287 struct em28xx_fh *fh = priv;
1288 struct em28xx *dev = fh->dev;
1289 int rc;
1290
1291 rc = check_dev(dev);
1292 if (rc < 0)
1293 return rc;
1294
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001295 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1296 return -EINVAL;
1297 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001298 return -EINVAL;
1299
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001300 mutex_lock(&dev->lock);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001301
1302 videobuf_streamoff(&fh->vb_vidq);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001303 res_free(fh);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001304
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001305 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001306
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001307 return 0;
1308}
1309
1310static int vidioc_querycap(struct file *file, void *priv,
1311 struct v4l2_capability *cap)
1312{
1313 struct em28xx_fh *fh = priv;
1314 struct em28xx *dev = fh->dev;
1315
1316 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1317 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001318 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001319
1320 cap->version = EM28XX_VERSION_CODE;
1321
1322 cap->capabilities =
1323 V4L2_CAP_SLICED_VBI_CAPTURE |
1324 V4L2_CAP_VIDEO_CAPTURE |
1325 V4L2_CAP_AUDIO |
1326 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1327
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001328 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001329 cap->capabilities |= V4L2_CAP_TUNER;
1330
1331 return 0;
1332}
1333
Hans Verkuil78b526a2008-05-28 12:16:41 -03001334static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001335 struct v4l2_fmtdesc *fmtd)
1336{
1337 if (fmtd->index != 0)
1338 return -EINVAL;
1339
1340 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1341 strcpy(fmtd->description, "Packed YUY2");
1342 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1343 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1344
1345 return 0;
1346}
1347
1348/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001349static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001350 struct v4l2_format *f)
1351{
1352 struct em28xx_fh *fh = priv;
1353 struct em28xx *dev = fh->dev;
1354 int rc;
1355
1356 rc = check_dev(dev);
1357 if (rc < 0)
1358 return rc;
1359
1360 mutex_lock(&dev->lock);
1361
1362 f->fmt.sliced.service_set = 0;
1363
1364 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1365
1366 if (f->fmt.sliced.service_set == 0)
1367 rc = -EINVAL;
1368
1369 mutex_unlock(&dev->lock);
1370 return rc;
1371}
1372
Hans Verkuil78b526a2008-05-28 12:16:41 -03001373static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374 struct v4l2_format *f)
1375{
1376 struct em28xx_fh *fh = priv;
1377 struct em28xx *dev = fh->dev;
1378 int rc;
1379
1380 rc = check_dev(dev);
1381 if (rc < 0)
1382 return rc;
1383
1384 mutex_lock(&dev->lock);
1385 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1386 mutex_unlock(&dev->lock);
1387
1388 if (f->fmt.sliced.service_set == 0)
1389 return -EINVAL;
1390
1391 return 0;
1392}
1393
1394
1395static int vidioc_reqbufs(struct file *file, void *priv,
1396 struct v4l2_requestbuffers *rb)
1397{
1398 struct em28xx_fh *fh = priv;
1399 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001400 int rc;
1401
1402 rc = check_dev(dev);
1403 if (rc < 0)
1404 return rc;
1405
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001406 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001407}
1408
1409static int vidioc_querybuf(struct file *file, void *priv,
1410 struct v4l2_buffer *b)
1411{
1412 struct em28xx_fh *fh = priv;
1413 struct em28xx *dev = fh->dev;
1414 int rc;
1415
1416 rc = check_dev(dev);
1417 if (rc < 0)
1418 return rc;
1419
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001420 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001421}
1422
1423static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1424{
1425 struct em28xx_fh *fh = priv;
1426 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001427 int rc;
1428
1429 rc = check_dev(dev);
1430 if (rc < 0)
1431 return rc;
1432
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001433 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001434}
1435
1436static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1437{
1438 struct em28xx_fh *fh = priv;
1439 struct em28xx *dev = fh->dev;
1440 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001441
1442 rc = check_dev(dev);
1443 if (rc < 0)
1444 return rc;
1445
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001446 return (videobuf_dqbuf(&fh->vb_vidq, b,
1447 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001448}
1449
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001450#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001451static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001452{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001453 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001454
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001455 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001456}
1457#endif
1458
1459
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001460/* ----------------------------------------------------------- */
1461/* RADIO ESPECIFIC IOCTLS */
1462/* ----------------------------------------------------------- */
1463
1464static int radio_querycap(struct file *file, void *priv,
1465 struct v4l2_capability *cap)
1466{
1467 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1468
1469 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1470 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
Kay Sieversaf128a12008-10-30 00:51:46 -03001471 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001472
1473 cap->version = EM28XX_VERSION_CODE;
1474 cap->capabilities = V4L2_CAP_TUNER;
1475 return 0;
1476}
1477
1478static int radio_g_tuner(struct file *file, void *priv,
1479 struct v4l2_tuner *t)
1480{
1481 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1482
1483 if (unlikely(t->index > 0))
1484 return -EINVAL;
1485
1486 strcpy(t->name, "Radio");
1487 t->type = V4L2_TUNER_RADIO;
1488
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001489 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001490 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001491 mutex_unlock(&dev->lock);
1492
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001493 return 0;
1494}
1495
1496static int radio_enum_input(struct file *file, void *priv,
1497 struct v4l2_input *i)
1498{
1499 if (i->index != 0)
1500 return -EINVAL;
1501 strcpy(i->name, "Radio");
1502 i->type = V4L2_INPUT_TYPE_TUNER;
1503
1504 return 0;
1505}
1506
1507static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1508{
1509 if (unlikely(a->index))
1510 return -EINVAL;
1511
1512 strcpy(a->name, "Radio");
1513 return 0;
1514}
1515
1516static int radio_s_tuner(struct file *file, void *priv,
1517 struct v4l2_tuner *t)
1518{
1519 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1520
1521 if (0 != t->index)
1522 return -EINVAL;
1523
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001524 mutex_lock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001525 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001526 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001527
1528 return 0;
1529}
1530
1531static int radio_s_audio(struct file *file, void *fh,
1532 struct v4l2_audio *a)
1533{
1534 return 0;
1535}
1536
1537static int radio_s_input(struct file *file, void *fh, unsigned int i)
1538{
1539 return 0;
1540}
1541
1542static int radio_queryctrl(struct file *file, void *priv,
1543 struct v4l2_queryctrl *qc)
1544{
1545 int i;
1546
1547 if (qc->id < V4L2_CID_BASE ||
1548 qc->id >= V4L2_CID_LASTP1)
1549 return -EINVAL;
1550
1551 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1552 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1553 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1554 return 0;
1555 }
1556 }
1557
1558 return -EINVAL;
1559}
1560
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001561/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001562 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001563 * inits the device and starts isoc transfer
1564 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001565static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001566{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001567 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001568 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001569 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001570 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001571 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001572
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001573 mutex_lock(&em28xx_devlist_mutex);
Trent Piephoa991f442007-10-10 05:37:43 -03001574 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001575 if (h->vdev->minor == minor) {
1576 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001577 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001578 }
1579 if (h->vbi_dev->minor == minor) {
1580 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001581 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001582 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001583 if (h->radio_dev &&
1584 h->radio_dev->minor == minor) {
1585 radio = 1;
1586 dev = h;
1587 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001588 }
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001589 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03001590
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001591 if (NULL == dev)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001592 return -ENODEV;
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001593
1594 mutex_lock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001595
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001596 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001597 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001598
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001599
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001600 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001601 if (!fh) {
1602 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001603 mutex_unlock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001604 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001605 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001606 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001607 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001608 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001609 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001610
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001611 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001612 dev->width = norm_maxw(dev);
1613 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001614 dev->hscale = 0;
1615 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001616
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001617 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001618 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001619 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001620
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001621 /* Needed, since GPIO might have disabled power of
1622 some i2c device
1623 */
1624 em28xx_config_i2c(dev);
1625
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001626 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001627 if (fh->radio) {
1628 em28xx_videodbg("video_open: setting radio device\n");
1629 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1630 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001631
1632 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001633
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001634 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1635 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1636 sizeof(struct em28xx_buffer), fh);
1637
Ingo Molnar3593cab2006-02-07 06:49:14 -02001638 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001639
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001640 return errCode;
1641}
1642
1643/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001644 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001645 * unregisters the v4l2,i2c and usb devices
1646 * called when the device gets disconected or at module unload
1647*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001648static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001649{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001650
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001651 /*FIXME: I2C IR should be disconnected */
1652
Markus Rechberger9c755412005-11-08 21:37:52 -08001653 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001654 if (dev->sbutton_input_dev)
1655 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehaba924a492008-11-12 08:41:29 -03001656
1657 if (dev->ir)
1658 em28xx_ir_fini(dev);
1659
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001660 if (dev->radio_dev) {
1661 if (-1 != dev->radio_dev->minor)
1662 video_unregister_device(dev->radio_dev);
1663 else
1664 video_device_release(dev->radio_dev);
1665 dev->radio_dev = NULL;
1666 }
1667 if (dev->vbi_dev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001668 em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
1669 dev->vbi_dev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001670 if (-1 != dev->vbi_dev->minor)
1671 video_unregister_device(dev->vbi_dev);
1672 else
1673 video_device_release(dev->vbi_dev);
1674 dev->vbi_dev = NULL;
1675 }
1676 if (dev->vdev) {
Devin Heitmueller49240442008-11-12 02:05:15 -03001677 em28xx_info("V4L2 device /dev/video%d deregistered\n",
1678 dev->vdev->num);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001679 if (-1 != dev->vdev->minor)
1680 video_unregister_device(dev->vdev);
1681 else
1682 video_device_release(dev->vdev);
1683 dev->vdev = NULL;
1684 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001685 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001686 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001687
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001688 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001689 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690}
1691
1692/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001693 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001694 * stops streaming and deallocates all resources allocated by the v4l2
1695 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001696 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001697static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001698{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001699 struct em28xx_fh *fh = filp->private_data;
1700 struct em28xx *dev = fh->dev;
1701 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001702
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001703 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001705
Mauro Carvalho Chehab78313642008-12-16 20:00:49 -03001706 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001707 if (res_check(fh))
1708 res_free(fh);
1709
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001710 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001711 videobuf_stop(&fh->vb_vidq);
1712 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001713
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001714 /* the device is already disconnect,
1715 free the remaining resources */
1716 if (dev->state & DEV_DISCONNECTED) {
1717 em28xx_release_resources(dev);
1718 mutex_unlock(&dev->lock);
1719 kfree(dev);
1720 return 0;
1721 }
1722
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03001723 /* Save some power by putting tuner to sleep */
1724 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1725
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001726 /* do this before setting alternate! */
1727 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehab2fe3e2e2008-11-27 09:10:40 -03001728 em28xx_set_mode(dev, EM28XX_SUSPEND);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001729
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001730 /* set alternate 0 */
1731 dev->alt = 0;
1732 em28xx_videodbg("setting alternate 0\n");
1733 errCode = usb_set_interface(dev->udev, 0, 0);
1734 if (errCode < 0) {
1735 em28xx_errdev("cannot change alternate number to "
1736 "0 (error=%i)\n", errCode);
1737 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001739 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001740 dev->users--;
1741 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001742 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001743 return 0;
1744}
1745
1746/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001747 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001748 * will allocate buffers when called for the first time
1749 */
1750static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001751em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001752 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001754 struct em28xx_fh *fh = filp->private_data;
1755 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001756 int rc;
1757
1758 rc = check_dev(dev);
1759 if (rc < 0)
1760 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001761
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001762 /* FIXME: read() is not prepared to allow changing the video
1763 resolution while streaming. Seems a bug at em28xx_set_fmt
1764 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001765
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001766 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001767 mutex_lock(&dev->lock);
1768 rc = res_get(fh);
1769 mutex_unlock(&dev->lock);
1770
1771 if (unlikely(rc < 0))
1772 return rc;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001773
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001774 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1775 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001776 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001777 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001778}
1779
1780/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001781 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001782 * will allocate buffers when called for the first time
1783 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001784static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001785{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001786 struct em28xx_fh *fh = filp->private_data;
1787 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001788 int rc;
1789
1790 rc = check_dev(dev);
1791 if (rc < 0)
1792 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001793
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001794 mutex_lock(&dev->lock);
1795 rc = res_get(fh);
1796 mutex_unlock(&dev->lock);
1797
1798 if (unlikely(rc < 0))
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001799 return POLLERR;
1800
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001801 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1802 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001803
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001804 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001805}
1806
1807/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001808 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001809 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001810static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001811{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001812 struct em28xx_fh *fh = filp->private_data;
1813 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001814 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001815
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001816 rc = check_dev(dev);
1817 if (rc < 0)
1818 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001819
Mauro Carvalho Chehab29b59412008-12-16 20:19:24 -03001820 mutex_lock(&dev->lock);
1821 rc = res_get(fh);
1822 mutex_unlock(&dev->lock);
1823
1824 if (unlikely(rc < 0))
1825 return rc;
1826
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001827 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001828
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001829 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1830 (unsigned long)vma->vm_start,
1831 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1832 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001833
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001834 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001835}
1836
Arjan van de Venfa027c22007-02-12 00:55:33 -08001837static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001838 .owner = THIS_MODULE,
1839 .open = em28xx_v4l2_open,
1840 .release = em28xx_v4l2_close,
1841 .read = em28xx_v4l2_read,
1842 .poll = em28xx_v4l2_poll,
1843 .mmap = em28xx_v4l2_mmap,
1844 .ioctl = video_ioctl2,
1845 .llseek = no_llseek,
1846 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847};
1848
Hans Verkuila3998102008-07-21 02:57:38 -03001849static const struct v4l2_ioctl_ops video_ioctl_ops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001850 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001851 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1852 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1853 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1854 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001855 .vidioc_g_audio = vidioc_g_audio,
1856 .vidioc_s_audio = vidioc_s_audio,
1857 .vidioc_cropcap = vidioc_cropcap,
1858
Hans Verkuil78b526a2008-05-28 12:16:41 -03001859 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1860 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1861 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001862
1863 .vidioc_reqbufs = vidioc_reqbufs,
1864 .vidioc_querybuf = vidioc_querybuf,
1865 .vidioc_qbuf = vidioc_qbuf,
1866 .vidioc_dqbuf = vidioc_dqbuf,
1867 .vidioc_s_std = vidioc_s_std,
1868 .vidioc_enum_input = vidioc_enum_input,
1869 .vidioc_g_input = vidioc_g_input,
1870 .vidioc_s_input = vidioc_s_input,
1871 .vidioc_queryctrl = vidioc_queryctrl,
1872 .vidioc_g_ctrl = vidioc_g_ctrl,
1873 .vidioc_s_ctrl = vidioc_s_ctrl,
1874 .vidioc_streamon = vidioc_streamon,
1875 .vidioc_streamoff = vidioc_streamoff,
1876 .vidioc_g_tuner = vidioc_g_tuner,
1877 .vidioc_s_tuner = vidioc_s_tuner,
1878 .vidioc_g_frequency = vidioc_g_frequency,
1879 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001880#ifdef CONFIG_VIDEO_ADV_DEBUG
1881 .vidioc_g_register = vidioc_g_register,
1882 .vidioc_s_register = vidioc_s_register,
1883#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001884#ifdef CONFIG_VIDEO_V4L1_COMPAT
1885 .vidiocgmbuf = vidiocgmbuf,
1886#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001887};
1888
1889static const struct video_device em28xx_video_template = {
1890 .fops = &em28xx_v4l_fops,
1891 .release = video_device_release,
1892 .ioctl_ops = &video_ioctl_ops,
1893
1894 .minor = -1,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001895
1896 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001897 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001898};
1899
Hans Verkuila3998102008-07-21 02:57:38 -03001900static const struct file_operations radio_fops = {
1901 .owner = THIS_MODULE,
1902 .open = em28xx_v4l2_open,
1903 .release = em28xx_v4l2_close,
1904 .ioctl = video_ioctl2,
1905 .compat_ioctl = v4l_compat_ioctl32,
1906 .llseek = no_llseek,
1907};
1908
1909static const struct v4l2_ioctl_ops radio_ioctl_ops = {
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001910 .vidioc_querycap = radio_querycap,
1911 .vidioc_g_tuner = radio_g_tuner,
1912 .vidioc_enum_input = radio_enum_input,
1913 .vidioc_g_audio = radio_g_audio,
1914 .vidioc_s_tuner = radio_s_tuner,
1915 .vidioc_s_audio = radio_s_audio,
1916 .vidioc_s_input = radio_s_input,
1917 .vidioc_queryctrl = radio_queryctrl,
1918 .vidioc_g_ctrl = vidioc_g_ctrl,
1919 .vidioc_s_ctrl = vidioc_s_ctrl,
1920 .vidioc_g_frequency = vidioc_g_frequency,
1921 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001922#ifdef CONFIG_VIDEO_ADV_DEBUG
1923 .vidioc_g_register = vidioc_g_register,
1924 .vidioc_s_register = vidioc_s_register,
1925#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001926};
1927
Hans Verkuila3998102008-07-21 02:57:38 -03001928static struct video_device em28xx_radio_template = {
1929 .name = "em28xx-radio",
Hans Verkuila3998102008-07-21 02:57:38 -03001930 .fops = &radio_fops,
1931 .ioctl_ops = &radio_ioctl_ops,
1932 .minor = -1,
1933};
1934
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001935/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001936
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001937
1938static LIST_HEAD(em28xx_extension_devlist);
1939static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1940
1941int em28xx_register_extension(struct em28xx_ops *ops)
1942{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001943 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001944
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001945 mutex_lock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001946 mutex_lock(&em28xx_extension_devlist_lock);
1947 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001948 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1949 if (dev)
1950 ops->init(dev);
1951 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001952 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1953 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001954 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001955 return 0;
1956}
1957EXPORT_SYMBOL(em28xx_register_extension);
1958
1959void em28xx_unregister_extension(struct em28xx_ops *ops)
1960{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001961 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001962
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001963 mutex_lock(&em28xx_devlist_mutex);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001964 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1965 if (dev)
1966 ops->fini(dev);
1967 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001968
1969 mutex_lock(&em28xx_extension_devlist_lock);
1970 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1971 list_del(&ops->next);
1972 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001973 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001974}
1975EXPORT_SYMBOL(em28xx_unregister_extension);
1976
Adrian Bunk532fe652008-01-28 22:10:48 -03001977static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1978 const struct video_device *template,
Adrian Bunk532fe652008-01-28 22:10:48 -03001979 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001980{
1981 struct video_device *vfd;
1982
1983 vfd = video_device_alloc();
1984 if (NULL == vfd)
1985 return NULL;
1986 *vfd = *template;
1987 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001988 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001989 vfd->release = video_device_release;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001990 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001991
1992 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1993 dev->name, type_name);
1994
1995 return vfd;
1996}
1997
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03001998static int register_analog_devices(struct em28xx *dev)
1999{
2000 int ret;
2001
2002 /* allocate and fill video video_device struct */
2003 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
2004 if (!dev->vdev) {
2005 em28xx_errdev("cannot allocate video_device.\n");
2006 return -ENODEV;
2007 }
2008
2009 /* register v4l2 video video_device */
2010 ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
2011 video_nr[dev->devno]);
2012 if (ret) {
2013 em28xx_errdev("unable to register video device (error=%i).\n",
2014 ret);
2015 return ret;
2016 }
2017
2018 /* Allocate and fill vbi video_device struct */
2019 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
2020
2021 /* register v4l2 vbi video_device */
2022 ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
2023 vbi_nr[dev->devno]);
2024 if (ret < 0) {
2025 em28xx_errdev("unable to register vbi device\n");
2026 return ret;
2027 }
2028
2029 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2030 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
2031 if (!dev->radio_dev) {
2032 em28xx_errdev("cannot allocate video_device.\n");
2033 return -ENODEV;
2034 }
2035 ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2036 radio_nr[dev->devno]);
2037 if (ret < 0) {
2038 em28xx_errdev("can't register radio device\n");
2039 return ret;
2040 }
2041 em28xx_info("Registered radio device as /dev/radio%d\n",
2042 dev->radio_dev->num);
2043 }
2044
2045 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2046 dev->vdev->num, dev->vbi_dev->num);
2047
2048 return 0;
2049}
2050
2051
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002052/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002053 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002054 * allocates and inits the device structs, registers i2c bus and v4l device
2055 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002056static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002057 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002058{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002059 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002060 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002061 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002062 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002063 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002064
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002065 dev->udev = udev;
Mauro Carvalho Chehabf2a2e492008-11-19 06:17:44 -03002066 mutex_init(&dev->ctrl_urb_lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03002067 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002068 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03002069 init_waitqueue_head(&dev->wait_frame);
2070 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002071
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002072 dev->em28xx_write_regs = em28xx_write_regs;
2073 dev->em28xx_read_reg = em28xx_read_reg;
2074 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
2075 dev->em28xx_write_regs_req = em28xx_write_regs_req;
2076 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002077 dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002078
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002079 em28xx_pre_card_setup(dev);
2080
2081 errCode = em28xx_config(dev);
2082 if (errCode) {
2083 em28xx_errdev("error configuring device\n");
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002084 return -ENOMEM;
2085 }
2086
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002087 /* register i2c bus */
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002088 errCode = em28xx_i2c_register(dev);
2089 if (errCode < 0) {
2090 em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
2091 __func__, errCode);
2092 return errCode;
2093 }
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002094
2095 /* Do board specific init and eeprom reading */
2096 em28xx_card_setup(dev);
2097
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002098 /* Configure audio */
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002099 errCode = em28xx_audio_setup(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002100 if (errCode < 0) {
Mauro Carvalho Chehab35643942008-11-19 12:01:33 -03002101 em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002102 __func__, errCode);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002103 }
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03002104
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002105 /* configure the device */
2106 em28xx_config_i2c(dev);
2107
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03002108 /* set default norm */
2109 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002110
2111 maxw = norm_maxw(dev);
2112 maxh = norm_maxh(dev);
2113
2114 /* set default image size */
2115 dev->width = maxw;
2116 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002117 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118 dev->hscale = 0;
2119 dev->vscale = 0;
2120 dev->ctl_input = 2;
2121
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002122 errCode = em28xx_config(dev);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002123 if (errCode < 0) {
2124 em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
2125 __func__, errCode);
2126 return errCode;
2127 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002128
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002129 /* init video dma queues */
2130 INIT_LIST_HEAD(&dev->vidq.active);
2131 INIT_LIST_HEAD(&dev->vidq.queued);
2132
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002133
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002134 if (dev->board.has_msp34xx) {
Sascha Sommer5a804152007-11-03 21:22:38 -03002135 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002136 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002137 if (errCode < 0) {
2138 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
2139 __func__, errCode);
2140 return errCode;
2141 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002142 msleep(3);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002143
Mauro Carvalho Chehab2a29a0d2008-11-26 16:28:42 -03002144 errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
Douglas Schilling Landgraff2a01a02008-09-08 03:27:20 -03002145 if (errCode < 0) {
2146 em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
2147 __func__, errCode);
2148 return errCode;
2149 }
Sascha Sommer5a804152007-11-03 21:22:38 -03002150 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002151 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002152
Sascha Sommer5a804152007-11-03 21:22:38 -03002153 video_mux(dev, 0);
2154
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002155 mutex_lock(&em28xx_devlist_mutex);
2156 list_add_tail(&dev->devlist, &em28xx_devlist);
2157 retval = register_analog_devices(dev);
2158 if (retval < 0) {
2159 em28xx_release_resources(dev);
2160 mutex_unlock(&em28xx_devlist_mutex);
2161 goto fail_reg_devices;
2162 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002163
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002164 mutex_lock(&em28xx_extension_devlist_lock);
2165 if (!list_empty(&em28xx_extension_devlist)) {
2166 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2167 if (ops->id)
2168 ops->init(dev);
2169 }
2170 }
2171 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002172 mutex_unlock(&em28xx_devlist_mutex);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002173
Mauro Carvalho Chehabeb6c9632008-12-05 10:39:12 -03002174 /* Save some power by putting tuner to sleep */
2175 em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
2176
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002177 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002178
Mauro Carvalho Chehab818a5572008-11-20 10:30:26 -03002179fail_reg_devices:
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002180 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002181}
2182
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002183#if defined(CONFIG_MODULES) && defined(MODULE)
2184static void request_module_async(struct work_struct *work)
2185{
2186 struct em28xx *dev = container_of(work,
2187 struct em28xx, request_module_wk);
2188
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002189 if (dev->has_audio_class)
2190 request_module("snd-usb-audio");
Devin Heitmueller24a613e2008-11-12 02:05:19 -03002191 else if (dev->has_alsa_audio)
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002192 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002193
Mauro Carvalho Chehab505b6d02008-11-25 09:39:50 -03002194 if (dev->board.has_dvb)
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002195 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002196}
2197
2198static void request_modules(struct em28xx *dev)
2199{
2200 INIT_WORK(&dev->request_module_wk, request_module_async);
2201 schedule_work(&dev->request_module_wk);
2202}
2203#else
2204#define request_modules(dev)
2205#endif /* CONFIG_MODULES */
2206
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002208 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002209 * checks for supported devices
2210 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002211static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002212 const struct usb_device_id *id)
2213{
2214 const struct usb_endpoint_descriptor *endpoint;
2215 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002216 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002217 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002218 int retval = -ENODEV;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002219 int i, nr, ifnum, isoc_pipe;
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002220 char *speed;
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002221 char descr[255] = "";
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222
2223 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002224 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2225
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002226 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002227 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2228 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002229
2230 /* Don't register audio interfaces */
2231 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002232 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
2233 "interface %i, class %i\n",
2234 le16_to_cpu(udev->descriptor.idVendor),
2235 le16_to_cpu(udev->descriptor.idProduct),
2236 ifnum,
2237 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002238
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002239 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002240 return -ENODEV;
2241 }
2242
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002243 endpoint = &interface->cur_altsetting->endpoint[0].desc;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002244
Michael Opdenacker59c51592007-05-09 08:57:56 +02002245 /* check if the device has the iso in endpoint at the correct place */
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002246 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
2247 USB_ENDPOINT_XFER_ISOC &&
2248 (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
2249 {
2250 /* It's a newer em2874/em2875 device */
2251 isoc_pipe = 0;
2252 } else {
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002253 int check_interface = 1;
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002254 isoc_pipe = 1;
2255 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2256 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002257 USB_ENDPOINT_XFER_ISOC)
2258 check_interface = 0;
2259
2260 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
2261 check_interface = 0;
2262
2263 if (!check_interface) {
2264 em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
2265 "interface %i, class %i found.\n",
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002266 le16_to_cpu(udev->descriptor.idVendor),
2267 le16_to_cpu(udev->descriptor.idProduct),
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002268 ifnum,
2269 interface->altsetting[0].desc.bInterfaceClass);
2270
2271 em28xx_err(DRIVER_NAME " This is an anciliary "
2272 "interface not used by the driver\n");
2273
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002274 em28xx_devused &= ~(1<<nr);
2275 return -ENODEV;
2276 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002277 }
2278
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002279 switch (udev->speed) {
2280 case USB_SPEED_LOW:
2281 speed = "1.5";
2282 break;
2283 case USB_SPEED_UNKNOWN:
2284 case USB_SPEED_FULL:
2285 speed = "12";
2286 break;
2287 case USB_SPEED_HIGH:
2288 speed = "480";
2289 break;
2290 default:
2291 speed = "unknown";
2292 }
2293
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002294 if (udev->manufacturer)
2295 strlcpy(descr, udev->manufacturer, sizeof(descr));
2296
2297 if (udev->product) {
2298 if (*descr)
2299 strlcat(descr, " ", sizeof(descr));
2300 strlcat(descr, udev->product, sizeof(descr));
2301 }
2302 if (*descr)
2303 strlcat(descr, " ", sizeof(descr));
2304
2305 printk(DRIVER_NAME ": New device %s@ %s Mbps "
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002306 "(%04x:%04x, interface %d, class %d)\n",
Mauro Carvalho Chehab8c239982008-11-28 23:46:43 -03002307 descr,
Mauro Carvalho Chehab6a18eaf2008-11-27 14:32:17 -03002308 speed,
2309 le16_to_cpu(udev->descriptor.idVendor),
2310 le16_to_cpu(udev->descriptor.idProduct),
2311 ifnum,
2312 interface->altsetting->desc.bInterfaceNumber);
Mauro Carvalho Chehabc9455fb2008-11-22 10:50:38 -03002313
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002314 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002315 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2316 EM28XX_MAXBOARDS);
2317 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002318 return -ENOMEM;
2319 }
2320
2321 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002322 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002323 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002324 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002325 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002326 return -ENOMEM;
2327 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002328
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002329 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002330 dev->devno = nr;
2331 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002332 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002333
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002334 /* Checks if audio is provided by some interface */
2335 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2336 uif = udev->config->interface[i];
2337 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2338 dev->has_audio_class = 1;
2339 break;
2340 }
2341 }
2342
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002343 /* compute alternate max packet sizes */
2344 uif = udev->actconfig->interface[0];
2345
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002346 dev->num_alt = uif->num_altsetting;
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002347 em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002348/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2349 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2350
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002351 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002352 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002353 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002354 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002355 return -ENOMEM;
2356 }
2357
2358 for (i = 0; i < dev->num_alt ; i++) {
Devin Heitmueller95ea4702008-11-12 02:02:17 -03002359 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002360 wMaxPacketSize);
2361 dev->alt_max_pkt_size[i] =
2362 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehab1bee0182008-11-25 10:06:21 -03002363 em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002364 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002365 }
2366
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002367 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002368 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002369
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002370 /* allocate device struct */
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002371 mutex_init(&dev->lock);
2372 mutex_lock(&dev->lock);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002373 retval = em28xx_init_dev(&dev, udev, nr);
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002374 if (retval) {
2375 em28xx_devused &= ~(1<<dev->devno);
2376 kfree(dev);
2377
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002378 return retval;
Mauro Carvalho Chehab625ff162008-11-18 10:23:19 -03002379 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002380
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002381 /* save our data pointer in this interface device */
2382 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002383
2384 request_modules(dev);
2385
Mauro Carvalho Chehabefc52a92008-12-16 22:04:56 -03002386 /* Should be the last thing to do, to avoid newer udev's to
2387 open the device before fully initializing it
2388 */
2389 mutex_unlock(&dev->lock);
2390
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002391 return 0;
2392}
2393
2394/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002395 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002396 * called when the device gets diconencted
2397 * video device will be unregistered on v4l2_close in case it is still open
2398 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002399static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002400{
Sascha Sommer5a804152007-11-03 21:22:38 -03002401 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002402 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002403
2404 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002405 usb_set_intfdata(interface, NULL);
2406
2407 if (!dev)
2408 return;
2409
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002410 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002411
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002412 /* wait until all current v4l2 io is finished then deallocate
2413 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002414 mutex_lock(&dev->lock);
2415
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002416 wake_up_interruptible_all(&dev->open);
2417
2418 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002419 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002420 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002421 "deallocation are deferred on close.\n",
Hans Verkuildd896012008-10-04 08:36:54 -03002422 dev->vdev->num);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002423
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002424 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002425 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002426 dev->state |= DEV_DISCONNECTED;
2427 wake_up_interruptible(&dev->wait_frame);
2428 wake_up_interruptible(&dev->wait_stream);
2429 } else {
2430 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002431 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002432 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002433 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002434
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002435 mutex_lock(&em28xx_extension_devlist_lock);
2436 if (!list_empty(&em28xx_extension_devlist)) {
2437 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2438 ops->fini(dev);
2439 }
2440 }
2441 mutex_unlock(&em28xx_extension_devlist_lock);
2442
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002443 if (!dev->users) {
2444 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002445 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002446 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002447}
2448
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002449static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002450 .name = "em28xx",
2451 .probe = em28xx_usb_probe,
2452 .disconnect = em28xx_usb_disconnect,
2453 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002454};
2455
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002456static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002457{
2458 int result;
2459
2460 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002461 (EM28XX_VERSION_CODE >> 16) & 0xff,
2462 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002463#ifdef SNAPSHOT
2464 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2465 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2466#endif
2467
2468 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002469 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002470 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002471 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002472 " usb_register failed. Error number %d.\n", result);
2473
2474 return result;
2475}
2476
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002477static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002478{
2479 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002480 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002481}
2482
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002483module_init(em28xx_module_init);
2484module_exit(em28xx_module_exit);