blob: 67c62eaa5b6d6cda7521e7a52e40416e2e8fa78d [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);
Markus Rechberger9c755412005-11-08 21:37:52 -080076
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080077static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020078static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030079static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
80static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
81
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080082module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020083module_param_array(video_nr, int, NULL, 0444);
84module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030085module_param_array(radio_nr, int, NULL, 0444);
86MODULE_PARM_DESC(card, "card type");
87MODULE_PARM_DESC(video_nr, "video device numbers");
88MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
89MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080090
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030091static unsigned int video_debug;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -030092module_param(video_debug, int, 0644);
93MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080094
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020095/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
96static unsigned long em28xx_devused;
97
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080098/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020099/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800100static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800101 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200102 .id = V4L2_CID_AUDIO_VOLUME,
103 .type = V4L2_CTRL_TYPE_INTEGER,
104 .name = "Volume",
105 .minimum = 0x0,
106 .maximum = 0x1f,
107 .step = 0x1,
108 .default_value = 0x1f,
109 .flags = 0,
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300110 }, {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200111 .id = V4L2_CID_AUDIO_MUTE,
112 .type = V4L2_CTRL_TYPE_BOOLEAN,
113 .name = "Mute",
114 .minimum = 0,
115 .maximum = 1,
116 .step = 1,
117 .default_value = 1,
118 .flags = 0,
119 }
120};
121
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800122static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800123
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300124/* ------------------------------------------------------------------
125 DMA and thread functions
126 ------------------------------------------------------------------*/
127
128/*
129 * Announces that a buffer were filled and request the next
130 */
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300131static inline void buffer_filled(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300132 struct em28xx_dmaqueue *dma_q,
133 struct em28xx_buffer *buf)
134{
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300135 /* Advice that buffer was filled */
136 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
137 buf->vb.state = VIDEOBUF_DONE;
138 buf->vb.field_count++;
139 do_gettimeofday(&buf->vb.ts);
140
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300141 dev->isoc_ctl.buf = NULL;
142
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300143 list_del(&buf->vb.queue);
144 wake_up(&buf->vb.done);
145}
146
147/*
148 * Identify the buffer header type and properly handles
149 */
150static void em28xx_copy_video(struct em28xx *dev,
151 struct em28xx_dmaqueue *dma_q,
152 struct em28xx_buffer *buf,
153 unsigned char *p,
154 unsigned char *outp, unsigned long len)
155{
156 void *fieldstart, *startwrite, *startread;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300157 int linesdone, currlinedone, offset, lencopy, remain;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300158 int bytesperline = dev->width << 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300159
160 if (dma_q->pos + len > buf->vb.size)
161 len = buf->vb.size - dma_q->pos;
162
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300163 if (p[0] != 0x88 && p[0] != 0x22) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300164 em28xx_isocdbg("frame is not complete\n");
165 len += 4;
166 } else
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300167 p += 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300168
169 startread = p;
170 remain = len;
171
172 /* Interlaces frame */
173 if (buf->top_field)
174 fieldstart = outp;
175 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300176 fieldstart = outp + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300177
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300178 linesdone = dma_q->pos / bytesperline;
179 currlinedone = dma_q->pos % bytesperline;
180 offset = linesdone * bytesperline * 2 + currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300181 startwrite = fieldstart + offset;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300182 lencopy = bytesperline - currlinedone;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300183 lencopy = lencopy > remain ? remain : lencopy;
184
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300185 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300186 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300187 ((char *)startwrite + lencopy) -
188 ((char *)outp + buf->vb.size));
189 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300190 }
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300191 if (lencopy <= 0)
192 return;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300193 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300194
195 remain -= lencopy;
196
197 while (remain > 0) {
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300198 startwrite += lencopy + bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300199 startread += lencopy;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300200 if (bytesperline > remain)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300201 lencopy = remain;
202 else
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300203 lencopy = bytesperline;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300204
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300205 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
Mauro Carvalho Chehabea8df7e2008-04-13 14:39:29 -0300206 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300207 ((char *)startwrite + lencopy) -
208 ((char *)outp + buf->vb.size));
209 lencopy = remain = (char *)outp + buf->vb.size -
210 (char *)startwrite;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300211 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300212 if (lencopy <= 0)
213 break;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300214
215 memcpy(startwrite, startread, lencopy);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300216
217 remain -= lencopy;
218 }
219
220 dma_q->pos += len;
221}
222
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300223static inline void print_err_status(struct em28xx *dev,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300224 int packet, int status)
225{
226 char *errmsg = "Unknown";
227
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300228 switch (status) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300229 case -ENOENT:
230 errmsg = "unlinked synchronuously";
231 break;
232 case -ECONNRESET:
233 errmsg = "unlinked asynchronuously";
234 break;
235 case -ENOSR:
236 errmsg = "Buffer error (overrun)";
237 break;
238 case -EPIPE:
239 errmsg = "Stalled (device not responding)";
240 break;
241 case -EOVERFLOW:
242 errmsg = "Babble (bad cable?)";
243 break;
244 case -EPROTO:
245 errmsg = "Bit-stuff error (bad cable?)";
246 break;
247 case -EILSEQ:
248 errmsg = "CRC/Timeout (could be anything)";
249 break;
250 case -ETIME:
251 errmsg = "Device does not respond";
252 break;
253 }
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300254 if (packet < 0) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300255 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
256 } else {
257 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
258 packet, status, errmsg);
259 }
260}
261
262/*
263 * video-buf generic routine to get the next available buffer
264 */
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300265static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300266 struct em28xx_buffer **buf)
267{
268 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300269 char *outp;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300270
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300271 if (list_empty(&dma_q->active)) {
272 em28xx_isocdbg("No active queue to serve\n");
273 dev->isoc_ctl.buf = NULL;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300274 *buf = NULL;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300275 return;
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300276 }
277
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300278 /* Get the next buffer */
279 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
280
Mauro Carvalho Chehabdbecb442008-04-13 15:08:55 -0300281 /* Cleans up buffer - Usefull for testing for frame/URB loss */
282 outp = videobuf_to_vmalloc(&(*buf)->vb);
283 memset(outp, 0, (*buf)->vb.size);
Mauro Carvalho Chehabcb784722008-04-13 15:06:52 -0300284
285 dev->isoc_ctl.buf = *buf;
286
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300287 return;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300288}
289
290/*
291 * Controls the isoc copy of each urb packet
292 */
Aidan Thornton579f72e2008-04-17 21:40:16 -0300293static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300294{
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300295 struct em28xx_buffer *buf;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300296 struct em28xx_dmaqueue *dma_q = urb->context;
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300297 unsigned char *outp = NULL;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300298 int i, len = 0, rc = 1;
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300299 unsigned char *p;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300300
301 if (!dev)
302 return 0;
303
304 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
305 return 0;
306
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300307 if (urb->status < 0) {
308 print_err_status(dev, -1, urb->status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300309 if (urb->status == -ENOENT)
310 return 0;
311 }
312
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300313 buf = dev->isoc_ctl.buf;
314 if (buf != NULL)
315 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300316
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300317 for (i = 0; i < urb->number_of_packets; i++) {
318 int status = urb->iso_frame_desc[i].status;
319
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300320 if (status < 0) {
321 print_err_status(dev, i, status);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300322 if (urb->iso_frame_desc[i].status != -EPROTO)
323 continue;
324 }
325
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300326 len = urb->iso_frame_desc[i].actual_length - 4;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300327
328 if (urb->iso_frame_desc[i].actual_length <= 0) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300329 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300330 continue;
331 }
332 if (urb->iso_frame_desc[i].actual_length >
333 dev->max_pkt_size) {
334 em28xx_isocdbg("packet bigger than packet size");
335 continue;
336 }
337
338 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300339
340 /* FIXME: incomplete buffer checks where removed to make
341 logic simpler. Impacts of those changes should be evaluated
342 */
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300343 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
344 em28xx_isocdbg("VBI HEADER!!!\n");
345 /* FIXME: Should add vbi copy */
346 continue;
347 }
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300348 if (p[0] == 0x22 && p[1] == 0x5a) {
Mauro Carvalho Chehab78bb3942008-04-13 14:56:25 -0300349 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
350 len, (p[2] & 1)? "odd" : "even");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300351
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300352 if (!(p[2] & 1)) {
353 if (buf != NULL)
354 buffer_filled(dev, dma_q, buf);
355 get_next_buf(dma_q, &buf);
356 if (buf == NULL)
357 outp = NULL;
358 else
359 outp = videobuf_to_vmalloc(&buf->vb);
Aidan Thorntone0fadfd342008-04-13 14:56:02 -0300360 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300361
362 if (buf != NULL) {
363 if (p[2] & 1)
364 buf->top_field = 0;
365 else
366 buf->top_field = 1;
367 }
Mauro Carvalho Chehabb4916f82008-04-13 15:09:14 -0300368
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300369 dma_q->pos = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300370 }
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300371 if (buf != NULL)
372 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300373 }
374 return rc;
375}
376
377/* ------------------------------------------------------------------
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300378 Videobuf operations
379 ------------------------------------------------------------------*/
380
381static int
382buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
383{
384 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300385 struct em28xx *dev = fh->dev;
386 struct v4l2_frequency f;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300387
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300388 *size = 16 * fh->dev->width * fh->dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300389 if (0 == *count)
390 *count = EM28XX_DEF_BUF;
391
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300392 if (*count < EM28XX_MIN_BUF)
393 *count = EM28XX_MIN_BUF;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300394
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300395 /* Ask tuner to go to analog mode */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300396 memset(&f, 0, sizeof(f));
Mauro Carvalho Chehabd2d9fbf2008-04-17 21:38:53 -0300397 f.frequency = dev->ctl_freq;
398
399 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
400
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300401 return 0;
402}
403
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300404/* This is called *without* dev->slock held; please keep it that way */
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300405static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
406{
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300407 struct em28xx_fh *fh = vq->priv_data;
408 struct em28xx *dev = fh->dev;
409 unsigned long flags = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300410 if (in_interrupt())
411 BUG();
412
Aidan Thornton3b5fa922008-04-13 15:09:36 -0300413 /* We used to wait for the buffer to finish here, but this didn't work
414 because, as we were keeping the state as VIDEOBUF_QUEUED,
415 videobuf_queue_cancel marked it as finished for us.
416 (Also, it could wedge forever if the hardware was misconfigured.)
417
418 This should be safe; by the time we get here, the buffer isn't
419 queued anymore. If we ever start marking the buffers as
420 VIDEOBUF_ACTIVE, it won't be, though.
421 */
422 spin_lock_irqsave(&dev->slock, flags);
423 if (dev->isoc_ctl.buf == buf)
424 dev->isoc_ctl.buf = NULL;
425 spin_unlock_irqrestore(&dev->slock, flags);
426
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300427 videobuf_vmalloc_free(&buf->vb);
428 buf->vb.state = VIDEOBUF_NEEDS_INIT;
429}
430
431static int
432buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
433 enum v4l2_field field)
434{
435 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300436 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300437 struct em28xx *dev = fh->dev;
438 int rc = 0, urb_init = 0;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300439
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300440 /* FIXME: It assumes depth = 16 */
441 /* The only currently supported format is 16 bits/pixel */
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300442 buf->vb.size = 16 * dev->width * dev->height >> 3;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300443
444 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
445 return -EINVAL;
446
Brandon Philips05612972008-04-13 14:57:01 -0300447 buf->vb.width = dev->width;
448 buf->vb.height = dev->height;
449 buf->vb.field = field;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300450
451 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300452 rc = videobuf_iolock(vq, &buf->vb, NULL);
453 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300454 goto fail;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300455 }
456
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300457 if (!dev->isoc_ctl.num_bufs)
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300458 urb_init = 1;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300459
460 if (urb_init) {
Aidan Thornton579f72e2008-04-17 21:40:16 -0300461 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
462 EM28XX_NUM_BUFS, dev->max_pkt_size,
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -0300463 em28xx_isoc_copy);
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300464 if (rc < 0)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300465 goto fail;
466 }
467
468 buf->vb.state = VIDEOBUF_PREPARED;
469 return 0;
470
471fail:
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300472 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300473 return rc;
474}
475
476static void
477buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
478{
479 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
480 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300481 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300482 struct em28xx_dmaqueue *vidq = &dev->vidq;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300483
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300484 buf->vb.state = VIDEOBUF_QUEUED;
485 list_add_tail(&buf->vb.queue, &vidq->active);
486
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300487}
488
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300489static void buffer_release(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300491{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300492 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300493 struct em28xx_fh *fh = vq->priv_data;
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300494 struct em28xx *dev = (struct em28xx *)fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300495
Aidan Thorntond7aa8022008-04-13 14:38:47 -0300496 em28xx_isocdbg("em28xx: called buffer_release\n");
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300497
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -0300498 free_buffer(vq, buf);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300499}
500
501static struct videobuf_queue_ops em28xx_video_qops = {
502 .buf_setup = buffer_setup,
503 .buf_prepare = buffer_prepare,
504 .buf_queue = buffer_queue,
505 .buf_release = buffer_release,
506};
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800507
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300508/********************* v4l2 interface **************************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800509
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800510/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800511 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800512 * inits registers with sane defaults
513 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800514static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800515{
516
517 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300518 if (!dev->is_em2800)
519 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800520
521 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200522
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300523/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
524/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
525 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200526
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800527 dev->mute = 1; /* maybe not the right place... */
528 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300529
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800530 em28xx_outfmt_set_yuv422(dev);
531 em28xx_colorlevels_set_default(dev);
532 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800533
534 return 0;
535}
536
537/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800538 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800539 * configure i2c attached devices
540 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800541static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800542{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300543 struct v4l2_routing route;
544
545 route.input = INPUT(dev->ctl_input)->vmux;
546 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100547 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300548 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300549 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550}
551
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800552static void video_mux(struct em28xx *dev, int index)
553{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300554 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800555
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300556 route.input = INPUT(index)->vmux;
557 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800558 dev->ctl_input = index;
559 dev->ctl_ainput = INPUT(index)->amux;
560
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300561 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800562
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800563 if (dev->has_msp34xx) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300564 if (dev->i2s_speed) {
565 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
566 &dev->i2s_speed);
567 }
Hans Verkuil2474ed42006-03-19 12:35:57 -0300568 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300569 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300570 /* Note: this is msp3400 specific */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300571 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
572 &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800573 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300574
Mauro Carvalho Chehab00b87302008-02-06 18:34:13 -0300575 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800576}
577
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300578/* Usage lock check functions */
579static int res_get(struct em28xx_fh *fh)
580{
581 struct em28xx *dev = fh->dev;
582 int rc = 0;
583
584 /* This instance already has stream_on */
585 if (fh->stream_on)
586 return rc;
587
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300588 if (dev->stream_on)
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300589 return -EINVAL;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300590
Mauro Carvalho Chehabe74153d2008-04-13 14:55:38 -0300591 mutex_lock(&dev->lock);
592 dev->stream_on = 1;
593 fh->stream_on = 1;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300594 mutex_unlock(&dev->lock);
595 return rc;
596}
597
598static int res_check(struct em28xx_fh *fh)
599{
600 return (fh->stream_on);
601}
602
603static void res_free(struct em28xx_fh *fh)
604{
605 struct em28xx *dev = fh->dev;
606
607 mutex_lock(&dev->lock);
608 fh->stream_on = 0;
609 dev->stream_on = 0;
610 mutex_unlock(&dev->lock);
611}
612
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800613/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300614 * em28xx_get_ctrl()
615 * return the current saturation, brightness or contrast, mute state
616 */
617static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
618{
619 switch (ctrl->id) {
620 case V4L2_CID_AUDIO_MUTE:
621 ctrl->value = dev->mute;
622 return 0;
623 case V4L2_CID_AUDIO_VOLUME:
624 ctrl->value = dev->volume;
625 return 0;
626 default:
627 return -EINVAL;
628 }
629}
630
631/*
632 * em28xx_set_ctrl()
633 * mute or set new saturation, brightness or contrast
634 */
635static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
636{
637 switch (ctrl->id) {
638 case V4L2_CID_AUDIO_MUTE:
639 if (ctrl->value != dev->mute) {
640 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300641 return em28xx_audio_analog_set(dev);
642 }
643 return 0;
644 case V4L2_CID_AUDIO_VOLUME:
645 dev->volume = ctrl->value;
646 return em28xx_audio_analog_set(dev);
647 default:
648 return -EINVAL;
649 }
650}
651
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300652static int check_dev(struct em28xx *dev)
653{
654 if (dev->state & DEV_DISCONNECTED) {
655 em28xx_errdev("v4l2 ioctl: device not present\n");
656 return -ENODEV;
657 }
658
659 if (dev->state & DEV_MISCONFIGURED) {
660 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
661 "close and open it again\n");
662 return -EIO;
663 }
664 return 0;
665}
666
667static void get_scale(struct em28xx *dev,
668 unsigned int width, unsigned int height,
669 unsigned int *hscale, unsigned int *vscale)
670{
671 unsigned int maxw = norm_maxw(dev);
672 unsigned int maxh = norm_maxh(dev);
673
674 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
675 if (*hscale >= 0x4000)
676 *hscale = 0x3fff;
677
678 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
679 if (*vscale >= 0x4000)
680 *vscale = 0x3fff;
681}
682
683/* ------------------------------------------------------------------
684 IOCTL vidioc handling
685 ------------------------------------------------------------------*/
686
Hans Verkuil78b526a2008-05-28 12:16:41 -0300687static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300688 struct v4l2_format *f)
689{
690 struct em28xx_fh *fh = priv;
691 struct em28xx *dev = fh->dev;
692
693 mutex_lock(&dev->lock);
694
695 f->fmt.pix.width = dev->width;
696 f->fmt.pix.height = dev->height;
697 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab44dc7332008-04-13 15:11:08 -0300698 f->fmt.pix.bytesperline = dev->width * 2;
699 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300700 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
701
702 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
703 f->fmt.pix.field = dev->interlaced ?
704 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
705
706 mutex_unlock(&dev->lock);
707 return 0;
708}
709
Hans Verkuil78b526a2008-05-28 12:16:41 -0300710static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300711 struct v4l2_format *f)
712{
713 struct em28xx_fh *fh = priv;
714 struct em28xx *dev = fh->dev;
715 int width = f->fmt.pix.width;
716 int height = f->fmt.pix.height;
717 unsigned int maxw = norm_maxw(dev);
718 unsigned int maxh = norm_maxh(dev);
719 unsigned int hscale, vscale;
720
721 /* width must even because of the YUYV format
722 height must be even because of interlacing */
723 height &= 0xfffe;
724 width &= 0xfffe;
725
726 if (height < 32)
727 height = 32;
728 if (height > maxh)
729 height = maxh;
730 if (width < 48)
731 width = 48;
732 if (width > maxw)
733 width = maxw;
734
735 mutex_lock(&dev->lock);
736
737 if (dev->is_em2800) {
738 /* the em2800 can only scale down to 50% */
739 if (height % (maxh / 2))
740 height = maxh;
741 if (width % (maxw / 2))
742 width = maxw;
743 /* according to empiatech support */
744 /* the MaxPacketSize is to small to support */
745 /* framesizes larger than 640x480 @ 30 fps */
746 /* or 640x576 @ 25 fps. As this would cut */
747 /* of a part of the image we prefer */
748 /* 360x576 or 360x480 for now */
749 if (width == maxw && height == maxh)
750 width /= 2;
751 }
752
753 get_scale(dev, width, height, &hscale, &vscale);
754
755 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
756 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
757
758 f->fmt.pix.width = width;
759 f->fmt.pix.height = height;
760 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
761 f->fmt.pix.bytesperline = width * 2;
762 f->fmt.pix.sizeimage = width * 2 * height;
763 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
764 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
765
766 mutex_unlock(&dev->lock);
767 return 0;
768}
769
Hans Verkuil78b526a2008-05-28 12:16:41 -0300770static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300771 struct v4l2_format *f)
772{
773 struct em28xx_fh *fh = priv;
774 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -0300775 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300776
777 rc = check_dev(dev);
778 if (rc < 0)
779 return rc;
780
Hans Verkuil78b526a2008-05-28 12:16:41 -0300781 vidioc_try_fmt_vid_cap(file, priv, f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300782
783 mutex_lock(&dev->lock);
784
Brandon Philips05612972008-04-13 14:57:01 -0300785 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
786 em28xx_errdev("%s queue busy\n", __func__);
787 rc = -EBUSY;
788 goto out;
789 }
790
Aidan Thornton0ea13e62008-04-13 15:02:24 -0300791 if (dev->stream_on && !fh->stream_on) {
792 em28xx_errdev("%s device in use by another fh\n", __func__);
793 rc = -EBUSY;
794 goto out;
795 }
796
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300797 /* set new image size */
798 dev->width = f->fmt.pix.width;
799 dev->height = f->fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300800 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
801
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300802 em28xx_set_alternate(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300803 em28xx_resolution_set(dev);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300804
Brandon Philips05612972008-04-13 14:57:01 -0300805 rc = 0;
806
807out:
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300808 mutex_unlock(&dev->lock);
Brandon Philips05612972008-04-13 14:57:01 -0300809 return rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300810}
811
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300812static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300813{
814 struct em28xx_fh *fh = priv;
815 struct em28xx *dev = fh->dev;
816 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300817 int rc;
818
819 rc = check_dev(dev);
820 if (rc < 0)
821 return rc;
822
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300823 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300824 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300825 mutex_unlock(&dev->lock);
826
827 /* Adjusts width/height, if needed */
828 f.fmt.pix.width = dev->width;
829 f.fmt.pix.height = dev->height;
Hans Verkuil78b526a2008-05-28 12:16:41 -0300830 vidioc_try_fmt_vid_cap(file, priv, &f);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300831
832 mutex_lock(&dev->lock);
833
834 /* set new image size */
835 dev->width = f.fmt.pix.width;
836 dev->height = f.fmt.pix.height;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300837 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
838
839 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300840 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300841
842 mutex_unlock(&dev->lock);
843 return 0;
844}
845
846static const char *iname[] = {
847 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
848 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
849 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
850 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
851 [EM28XX_VMUX_SVIDEO] = "S-Video",
852 [EM28XX_VMUX_TELEVISION] = "Television",
853 [EM28XX_VMUX_CABLE] = "Cable TV",
854 [EM28XX_VMUX_DVB] = "DVB",
855 [EM28XX_VMUX_DEBUG] = "for debug only",
856};
857
858static int vidioc_enum_input(struct file *file, void *priv,
859 struct v4l2_input *i)
860{
861 struct em28xx_fh *fh = priv;
862 struct em28xx *dev = fh->dev;
863 unsigned int n;
864
865 n = i->index;
866 if (n >= MAX_EM28XX_INPUT)
867 return -EINVAL;
868 if (0 == INPUT(n)->type)
869 return -EINVAL;
870
871 i->index = n;
872 i->type = V4L2_INPUT_TYPE_CAMERA;
873
874 strcpy(i->name, iname[INPUT(n)->type]);
875
876 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
877 (EM28XX_VMUX_CABLE == INPUT(n)->type))
878 i->type = V4L2_INPUT_TYPE_TUNER;
879
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300880 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300881
882 return 0;
883}
884
885static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
886{
887 struct em28xx_fh *fh = priv;
888 struct em28xx *dev = fh->dev;
889
890 *i = dev->ctl_input;
891
892 return 0;
893}
894
895static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
896{
897 struct em28xx_fh *fh = priv;
898 struct em28xx *dev = fh->dev;
899 int rc;
900
901 rc = check_dev(dev);
902 if (rc < 0)
903 return rc;
904
905 if (i >= MAX_EM28XX_INPUT)
906 return -EINVAL;
907 if (0 == INPUT(i)->type)
908 return -EINVAL;
909
910 mutex_lock(&dev->lock);
911
912 video_mux(dev, i);
913
914 mutex_unlock(&dev->lock);
915 return 0;
916}
917
918static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
919{
920 struct em28xx_fh *fh = priv;
921 struct em28xx *dev = fh->dev;
922 unsigned int index = a->index;
923
924 if (a->index > 1)
925 return -EINVAL;
926
927 index = dev->ctl_ainput;
928
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300929 if (index == 0)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300930 strcpy(a->name, "Television");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300931 else
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300932 strcpy(a->name, "Line In");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -0300933
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300934 a->capability = V4L2_AUDCAP_STEREO;
935 a->index = index;
936
937 return 0;
938}
939
940static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
941{
942 struct em28xx_fh *fh = priv;
943 struct em28xx *dev = fh->dev;
944
945 if (a->index != dev->ctl_ainput)
946 return -EINVAL;
947
948 return 0;
949}
950
951static int vidioc_queryctrl(struct file *file, void *priv,
952 struct v4l2_queryctrl *qc)
953{
954 struct em28xx_fh *fh = priv;
955 struct em28xx *dev = fh->dev;
956 int id = qc->id;
957 int i;
958 int rc;
959
960 rc = check_dev(dev);
961 if (rc < 0)
962 return rc;
963
964 memset(qc, 0, sizeof(*qc));
965
966 qc->id = id;
967
968 if (!dev->has_msp34xx) {
969 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
970 if (qc->id && qc->id == em28xx_qctrl[i].id) {
971 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
972 return 0;
973 }
974 }
975 }
976 mutex_lock(&dev->lock);
977 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
978 mutex_unlock(&dev->lock);
979
980 if (qc->type)
981 return 0;
982 else
983 return -EINVAL;
984}
985
986static int vidioc_g_ctrl(struct file *file, void *priv,
987 struct v4l2_control *ctrl)
988{
989 struct em28xx_fh *fh = priv;
990 struct em28xx *dev = fh->dev;
991 int rc;
992
993 rc = check_dev(dev);
994 if (rc < 0)
995 return rc;
996 mutex_lock(&dev->lock);
997
998 if (!dev->has_msp34xx)
999 rc = em28xx_get_ctrl(dev, ctrl);
1000 else
1001 rc = -EINVAL;
1002
1003 if (rc == -EINVAL) {
1004 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
1005 rc = 0;
1006 }
1007
1008 mutex_unlock(&dev->lock);
1009 return rc;
1010}
1011
1012static int vidioc_s_ctrl(struct file *file, void *priv,
1013 struct v4l2_control *ctrl)
1014{
1015 struct em28xx_fh *fh = priv;
1016 struct em28xx *dev = fh->dev;
1017 u8 i;
1018 int rc;
1019
1020 rc = check_dev(dev);
1021 if (rc < 0)
1022 return rc;
1023
1024 mutex_lock(&dev->lock);
1025
1026 if (dev->has_msp34xx)
1027 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1028 else {
1029 rc = 1;
1030 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1031 if (ctrl->id == em28xx_qctrl[i].id) {
1032 if (ctrl->value < em28xx_qctrl[i].minimum ||
1033 ctrl->value > em28xx_qctrl[i].maximum) {
1034 rc = -ERANGE;
1035 break;
1036 }
1037
1038 rc = em28xx_set_ctrl(dev, ctrl);
1039 break;
1040 }
1041 }
1042 }
1043
1044 /* Control not found - try to send it to the attached devices */
1045 if (rc == 1) {
1046 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
1047 rc = 0;
1048 }
1049
1050 mutex_unlock(&dev->lock);
1051 return rc;
1052}
1053
1054static int vidioc_g_tuner(struct file *file, void *priv,
1055 struct v4l2_tuner *t)
1056{
1057 struct em28xx_fh *fh = priv;
1058 struct em28xx *dev = fh->dev;
1059 int rc;
1060
1061 rc = check_dev(dev);
1062 if (rc < 0)
1063 return rc;
1064
1065 if (0 != t->index)
1066 return -EINVAL;
1067
1068 strcpy(t->name, "Tuner");
1069
1070 mutex_lock(&dev->lock);
1071
1072 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1073
1074 mutex_unlock(&dev->lock);
1075 return 0;
1076}
1077
1078static int vidioc_s_tuner(struct file *file, void *priv,
1079 struct v4l2_tuner *t)
1080{
1081 struct em28xx_fh *fh = priv;
1082 struct em28xx *dev = fh->dev;
1083 int rc;
1084
1085 rc = check_dev(dev);
1086 if (rc < 0)
1087 return rc;
1088
1089 if (0 != t->index)
1090 return -EINVAL;
1091
1092 mutex_lock(&dev->lock);
1093
1094 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1095
1096 mutex_unlock(&dev->lock);
1097 return 0;
1098}
1099
1100static int vidioc_g_frequency(struct file *file, void *priv,
1101 struct v4l2_frequency *f)
1102{
1103 struct em28xx_fh *fh = priv;
1104 struct em28xx *dev = fh->dev;
1105
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001106 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001107 f->frequency = dev->ctl_freq;
1108
1109 return 0;
1110}
1111
1112static int vidioc_s_frequency(struct file *file, void *priv,
1113 struct v4l2_frequency *f)
1114{
1115 struct em28xx_fh *fh = priv;
1116 struct em28xx *dev = fh->dev;
1117 int rc;
1118
1119 rc = check_dev(dev);
1120 if (rc < 0)
1121 return rc;
1122
1123 if (0 != f->tuner)
1124 return -EINVAL;
1125
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001126 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1127 return -EINVAL;
1128 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001129 return -EINVAL;
1130
1131 mutex_lock(&dev->lock);
1132
1133 dev->ctl_freq = f->frequency;
1134 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
1135
1136 mutex_unlock(&dev->lock);
1137 return 0;
1138}
1139
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001140#ifdef CONFIG_VIDEO_ADV_DEBUG
1141static int em28xx_reg_len(int reg)
1142{
1143 switch (reg) {
Mauro Carvalho Chehab41facaa2008-04-17 21:44:58 -03001144 case EM28XX_R40_AC97LSB:
1145 case EM28XX_R30_HSCALELOW:
1146 case EM28XX_R32_VSCALELOW:
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001147 return 2;
1148 default:
1149 return 1;
1150 }
1151}
1152
1153static int vidioc_g_register(struct file *file, void *priv,
1154 struct v4l2_register *reg)
1155{
1156 struct em28xx_fh *fh = priv;
1157 struct em28xx *dev = fh->dev;
1158 int ret;
1159
1160 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1161 return -EINVAL;
1162
1163 if (em28xx_reg_len(reg->reg) == 1) {
1164 ret = em28xx_read_reg(dev, reg->reg);
1165 if (ret < 0)
1166 return ret;
1167
1168 reg->val = ret;
1169 } else {
Al Viroa954b662008-05-21 00:32:51 -03001170 __le64 val = 0;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001171 ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
1172 reg->reg, (char *)&val, 2);
1173 if (ret < 0)
1174 return ret;
1175
Al Viroa954b662008-05-21 00:32:51 -03001176 reg->val = le64_to_cpu(val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001177 }
1178
1179 return 0;
1180}
1181
1182static int vidioc_s_register(struct file *file, void *priv,
1183 struct v4l2_register *reg)
1184{
1185 struct em28xx_fh *fh = priv;
1186 struct em28xx *dev = fh->dev;
Al Viroa954b662008-05-21 00:32:51 -03001187 __le64 buf;
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001188
Al Viroa954b662008-05-21 00:32:51 -03001189 buf = cpu_to_le64(reg->val);
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001190
1191 return em28xx_write_regs(dev, reg->reg, (char *)&buf,
1192 em28xx_reg_len(reg->reg));
1193}
1194#endif
1195
1196
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001197static int vidioc_cropcap(struct file *file, void *priv,
1198 struct v4l2_cropcap *cc)
1199{
1200 struct em28xx_fh *fh = priv;
1201 struct em28xx *dev = fh->dev;
1202
1203 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1204 return -EINVAL;
1205
1206 cc->bounds.left = 0;
1207 cc->bounds.top = 0;
1208 cc->bounds.width = dev->width;
1209 cc->bounds.height = dev->height;
1210 cc->defrect = cc->bounds;
1211 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1212 cc->pixelaspect.denominator = 59;
1213
1214 return 0;
1215}
1216
1217static int vidioc_streamon(struct file *file, void *priv,
1218 enum v4l2_buf_type type)
1219{
1220 struct em28xx_fh *fh = priv;
1221 struct em28xx *dev = fh->dev;
1222 int rc;
1223
1224 rc = check_dev(dev);
1225 if (rc < 0)
1226 return rc;
1227
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001228
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001229 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001230 return -EBUSY;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001231
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001232 return (videobuf_streamon(&fh->vb_vidq));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001233}
1234
1235static int vidioc_streamoff(struct file *file, void *priv,
1236 enum v4l2_buf_type type)
1237{
1238 struct em28xx_fh *fh = priv;
1239 struct em28xx *dev = fh->dev;
1240 int rc;
1241
1242 rc = check_dev(dev);
1243 if (rc < 0)
1244 return rc;
1245
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001246 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1247 return -EINVAL;
1248 if (type != fh->type)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001249 return -EINVAL;
1250
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001251 videobuf_streamoff(&fh->vb_vidq);
1252 res_free(fh);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001254 return 0;
1255}
1256
1257static int vidioc_querycap(struct file *file, void *priv,
1258 struct v4l2_capability *cap)
1259{
1260 struct em28xx_fh *fh = priv;
1261 struct em28xx *dev = fh->dev;
1262
1263 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1264 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1265 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1266
1267 cap->version = EM28XX_VERSION_CODE;
1268
1269 cap->capabilities =
1270 V4L2_CAP_SLICED_VBI_CAPTURE |
1271 V4L2_CAP_VIDEO_CAPTURE |
1272 V4L2_CAP_AUDIO |
1273 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1274
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001275 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001276 cap->capabilities |= V4L2_CAP_TUNER;
1277
1278 return 0;
1279}
1280
Hans Verkuil78b526a2008-05-28 12:16:41 -03001281static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001282 struct v4l2_fmtdesc *fmtd)
1283{
1284 if (fmtd->index != 0)
1285 return -EINVAL;
1286
1287 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288 strcpy(fmtd->description, "Packed YUY2");
1289 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1290 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1291
1292 return 0;
1293}
1294
1295/* Sliced VBI ioctls */
Hans Verkuil78b526a2008-05-28 12:16:41 -03001296static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001297 struct v4l2_format *f)
1298{
1299 struct em28xx_fh *fh = priv;
1300 struct em28xx *dev = fh->dev;
1301 int rc;
1302
1303 rc = check_dev(dev);
1304 if (rc < 0)
1305 return rc;
1306
1307 mutex_lock(&dev->lock);
1308
1309 f->fmt.sliced.service_set = 0;
1310
1311 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1312
1313 if (f->fmt.sliced.service_set == 0)
1314 rc = -EINVAL;
1315
1316 mutex_unlock(&dev->lock);
1317 return rc;
1318}
1319
Hans Verkuil78b526a2008-05-28 12:16:41 -03001320static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001321 struct v4l2_format *f)
1322{
1323 struct em28xx_fh *fh = priv;
1324 struct em28xx *dev = fh->dev;
1325 int rc;
1326
1327 rc = check_dev(dev);
1328 if (rc < 0)
1329 return rc;
1330
1331 mutex_lock(&dev->lock);
1332 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1333 mutex_unlock(&dev->lock);
1334
1335 if (f->fmt.sliced.service_set == 0)
1336 return -EINVAL;
1337
1338 return 0;
1339}
1340
1341
1342static int vidioc_reqbufs(struct file *file, void *priv,
1343 struct v4l2_requestbuffers *rb)
1344{
1345 struct em28xx_fh *fh = priv;
1346 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001347 int rc;
1348
1349 rc = check_dev(dev);
1350 if (rc < 0)
1351 return rc;
1352
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001353 return (videobuf_reqbufs(&fh->vb_vidq, rb));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001354}
1355
1356static int vidioc_querybuf(struct file *file, void *priv,
1357 struct v4l2_buffer *b)
1358{
1359 struct em28xx_fh *fh = priv;
1360 struct em28xx *dev = fh->dev;
1361 int rc;
1362
1363 rc = check_dev(dev);
1364 if (rc < 0)
1365 return rc;
1366
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001367 return (videobuf_querybuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001368}
1369
1370static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1371{
1372 struct em28xx_fh *fh = priv;
1373 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001374 int rc;
1375
1376 rc = check_dev(dev);
1377 if (rc < 0)
1378 return rc;
1379
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001380 return (videobuf_qbuf(&fh->vb_vidq, b));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001381}
1382
1383static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1384{
1385 struct em28xx_fh *fh = priv;
1386 struct em28xx *dev = fh->dev;
1387 int rc;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001388
1389 rc = check_dev(dev);
1390 if (rc < 0)
1391 return rc;
1392
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001393 return (videobuf_dqbuf(&fh->vb_vidq, b,
1394 file->f_flags & O_NONBLOCK));
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001395}
1396
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001397#ifdef CONFIG_VIDEO_V4L1_COMPAT
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001398static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001399{
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001400 struct em28xx_fh *fh = priv;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001401
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001402 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001403}
1404#endif
1405
1406
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001407/* ----------------------------------------------------------- */
1408/* RADIO ESPECIFIC IOCTLS */
1409/* ----------------------------------------------------------- */
1410
1411static int radio_querycap(struct file *file, void *priv,
1412 struct v4l2_capability *cap)
1413{
1414 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1415
1416 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1417 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1418 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1419
1420 cap->version = EM28XX_VERSION_CODE;
1421 cap->capabilities = V4L2_CAP_TUNER;
1422 return 0;
1423}
1424
1425static int radio_g_tuner(struct file *file, void *priv,
1426 struct v4l2_tuner *t)
1427{
1428 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1429
1430 if (unlikely(t->index > 0))
1431 return -EINVAL;
1432
1433 strcpy(t->name, "Radio");
1434 t->type = V4L2_TUNER_RADIO;
1435
1436 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1437 return 0;
1438}
1439
1440static int radio_enum_input(struct file *file, void *priv,
1441 struct v4l2_input *i)
1442{
1443 if (i->index != 0)
1444 return -EINVAL;
1445 strcpy(i->name, "Radio");
1446 i->type = V4L2_INPUT_TYPE_TUNER;
1447
1448 return 0;
1449}
1450
1451static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1452{
1453 if (unlikely(a->index))
1454 return -EINVAL;
1455
1456 strcpy(a->name, "Radio");
1457 return 0;
1458}
1459
1460static int radio_s_tuner(struct file *file, void *priv,
1461 struct v4l2_tuner *t)
1462{
1463 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1464
1465 if (0 != t->index)
1466 return -EINVAL;
1467
1468 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1469
1470 return 0;
1471}
1472
1473static int radio_s_audio(struct file *file, void *fh,
1474 struct v4l2_audio *a)
1475{
1476 return 0;
1477}
1478
1479static int radio_s_input(struct file *file, void *fh, unsigned int i)
1480{
1481 return 0;
1482}
1483
1484static int radio_queryctrl(struct file *file, void *priv,
1485 struct v4l2_queryctrl *qc)
1486{
1487 int i;
1488
1489 if (qc->id < V4L2_CID_BASE ||
1490 qc->id >= V4L2_CID_LASTP1)
1491 return -EINVAL;
1492
1493 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1494 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1495 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1496 return 0;
1497 }
1498 }
1499
1500 return -EINVAL;
1501}
1502
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001503/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001504 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001505 * inits the device and starts isoc transfer
1506 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001507static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001508{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001509 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001510 int errCode = 0, radio = 0;
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001511 struct em28xx *h, *dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001512 struct em28xx_fh *fh;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001513 enum v4l2_buf_type fh_type = 0;
Markus Rechberger9c755412005-11-08 21:37:52 -08001514
Trent Piephoa991f442007-10-10 05:37:43 -03001515 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001516 if (h->vdev->minor == minor) {
1517 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001518 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001519 }
1520 if (h->vbi_dev->minor == minor) {
1521 dev = h;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001522 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001523 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001524 if (h->radio_dev &&
1525 h->radio_dev->minor == minor) {
1526 radio = 1;
1527 dev = h;
1528 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001529 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001530 if (NULL == dev)
1531 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001532
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001533 em28xx_videodbg("open minor=%d type=%s users=%d\n",
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001534 minor, v4l2_type_names[fh_type], dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001535
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001536
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001537 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001538 if (!fh) {
1539 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1540 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001541 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001542 mutex_lock(&dev->lock);
1543 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001544 fh->radio = radio;
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001545 fh->type = fh_type;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001546 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001547
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001548 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001549 dev->width = norm_maxw(dev);
1550 dev->height = norm_maxh(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001551 dev->hscale = 0;
1552 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001553
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001554 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03001555 em28xx_set_alternate(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001556 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001558 /* Needed, since GPIO might have disabled power of
1559 some i2c device
1560 */
1561 em28xx_config_i2c(dev);
1562
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001563 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001564 if (fh->radio) {
1565 em28xx_videodbg("video_open: setting radio device\n");
1566 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1567 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001568
1569 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001571 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1572 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1573 sizeof(struct em28xx_buffer), fh);
1574
Ingo Molnar3593cab2006-02-07 06:49:14 -02001575 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001576
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001577 return errCode;
1578}
1579
1580/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001581 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582 * unregisters the v4l2,i2c and usb devices
1583 * called when the device gets disconected or at module unload
1584*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001585static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001586{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001587
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001588 /*FIXME: I2C IR should be disconnected */
1589
1590 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1591 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1592 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001593 list_del(&dev->devlist);
Devin Heitmuellera9fc52b2008-06-28 08:57:06 -03001594 if (dev->sbutton_input_dev)
1595 em28xx_deregister_snapshot_button(dev);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001596 if (dev->radio_dev) {
1597 if (-1 != dev->radio_dev->minor)
1598 video_unregister_device(dev->radio_dev);
1599 else
1600 video_device_release(dev->radio_dev);
1601 dev->radio_dev = NULL;
1602 }
1603 if (dev->vbi_dev) {
1604 if (-1 != dev->vbi_dev->minor)
1605 video_unregister_device(dev->vbi_dev);
1606 else
1607 video_device_release(dev->vbi_dev);
1608 dev->vbi_dev = NULL;
1609 }
1610 if (dev->vdev) {
1611 if (-1 != dev->vdev->minor)
1612 video_unregister_device(dev->vdev);
1613 else
1614 video_device_release(dev->vdev);
1615 dev->vdev = NULL;
1616 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001617 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001618 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001619
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001620 /* Mark device as unused */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001621 em28xx_devused &= ~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001622}
1623
1624/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001625 * em28xx_v4l2_close()
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001626 * stops streaming and deallocates all resources allocated by the v4l2
1627 * calls and ioctls
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001628 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001629static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001630{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001631 struct em28xx_fh *fh = filp->private_data;
1632 struct em28xx *dev = fh->dev;
1633 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001634
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001635 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001636
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001637
1638 if (res_check(fh))
1639 res_free(fh);
1640
Ingo Molnar3593cab2006-02-07 06:49:14 -02001641 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001643 if (dev->users == 1) {
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001644 videobuf_stop(&fh->vb_vidq);
1645 videobuf_mmap_free(&fh->vb_vidq);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001646
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001647 /* the device is already disconnect,
1648 free the remaining resources */
1649 if (dev->state & DEV_DISCONNECTED) {
1650 em28xx_release_resources(dev);
1651 mutex_unlock(&dev->lock);
1652 kfree(dev);
1653 return 0;
1654 }
1655
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001656 /* do this before setting alternate! */
1657 em28xx_uninit_isoc(dev);
Mauro Carvalho Chehabc67ec532008-04-17 21:48:00 -03001658 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001659
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001660 /* set alternate 0 */
1661 dev->alt = 0;
1662 em28xx_videodbg("setting alternate 0\n");
1663 errCode = usb_set_interface(dev->udev, 0, 0);
1664 if (errCode < 0) {
1665 em28xx_errdev("cannot change alternate number to "
1666 "0 (error=%i)\n", errCode);
1667 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001668 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001669 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670 dev->users--;
1671 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001672 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673 return 0;
1674}
1675
1676/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001677 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001678 * will allocate buffers when called for the first time
1679 */
1680static ssize_t
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001681em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
Mauro Carvalho Chehabf245e542008-04-13 14:41:23 -03001682 loff_t *pos)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001683{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001684 struct em28xx_fh *fh = filp->private_data;
1685 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001686 int rc;
1687
1688 rc = check_dev(dev);
1689 if (rc < 0)
1690 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001691
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001692 /* FIXME: read() is not prepared to allow changing the video
1693 resolution while streaming. Seems a bug at em28xx_set_fmt
1694 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001695
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001696 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1697 if (unlikely(res_get(fh)))
1698 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001699
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001700 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1701 filp->f_flags & O_NONBLOCK);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001702 }
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001703 return 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704}
1705
1706/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001707 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708 * will allocate buffers when called for the first time
1709 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001710static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001711{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001712 struct em28xx_fh *fh = filp->private_data;
1713 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001714 int rc;
1715
1716 rc = check_dev(dev);
1717 if (rc < 0)
1718 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001719
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001720 if (unlikely(res_get(fh) < 0))
1721 return POLLERR;
1722
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001723 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1724 return POLLERR;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001726 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001727}
1728
1729/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001730 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001732static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001733{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001734 struct em28xx_fh *fh = filp->private_data;
1735 struct em28xx *dev = fh->dev;
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001736 int rc;
Markus Rechberger9c755412005-11-08 21:37:52 -08001737
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001738 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001739 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001740
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001741 rc = check_dev(dev);
1742 if (rc < 0)
1743 return rc;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001744
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001745 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001746
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001747 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1748 (unsigned long)vma->vm_start,
1749 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1750 rc);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001751
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001752 return rc;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753}
1754
Arjan van de Venfa027c22007-02-12 00:55:33 -08001755static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001756 .owner = THIS_MODULE,
1757 .open = em28xx_v4l2_open,
1758 .release = em28xx_v4l2_close,
1759 .read = em28xx_v4l2_read,
1760 .poll = em28xx_v4l2_poll,
1761 .mmap = em28xx_v4l2_mmap,
1762 .ioctl = video_ioctl2,
1763 .llseek = no_llseek,
1764 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001765};
1766
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001767static const struct file_operations radio_fops = {
1768 .owner = THIS_MODULE,
1769 .open = em28xx_v4l2_open,
1770 .release = em28xx_v4l2_close,
1771 .ioctl = video_ioctl2,
1772 .compat_ioctl = v4l_compat_ioctl32,
1773 .llseek = no_llseek,
1774};
1775
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001776static const struct video_device em28xx_video_template = {
1777 .fops = &em28xx_v4l_fops,
1778 .release = video_device_release,
1779
1780 .minor = -1,
1781 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001782 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1783 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1784 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1785 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001786 .vidioc_g_audio = vidioc_g_audio,
1787 .vidioc_s_audio = vidioc_s_audio,
1788 .vidioc_cropcap = vidioc_cropcap,
1789
Hans Verkuil78b526a2008-05-28 12:16:41 -03001790 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1791 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1792 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001793
1794 .vidioc_reqbufs = vidioc_reqbufs,
1795 .vidioc_querybuf = vidioc_querybuf,
1796 .vidioc_qbuf = vidioc_qbuf,
1797 .vidioc_dqbuf = vidioc_dqbuf,
1798 .vidioc_s_std = vidioc_s_std,
1799 .vidioc_enum_input = vidioc_enum_input,
1800 .vidioc_g_input = vidioc_g_input,
1801 .vidioc_s_input = vidioc_s_input,
1802 .vidioc_queryctrl = vidioc_queryctrl,
1803 .vidioc_g_ctrl = vidioc_g_ctrl,
1804 .vidioc_s_ctrl = vidioc_s_ctrl,
1805 .vidioc_streamon = vidioc_streamon,
1806 .vidioc_streamoff = vidioc_streamoff,
1807 .vidioc_g_tuner = vidioc_g_tuner,
1808 .vidioc_s_tuner = vidioc_s_tuner,
1809 .vidioc_g_frequency = vidioc_g_frequency,
1810 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001811#ifdef CONFIG_VIDEO_ADV_DEBUG
1812 .vidioc_g_register = vidioc_g_register,
1813 .vidioc_s_register = vidioc_s_register,
1814#endif
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03001815#ifdef CONFIG_VIDEO_V4L1_COMPAT
1816 .vidiocgmbuf = vidiocgmbuf,
1817#endif
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001818
1819 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001820 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001821};
1822
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001823static struct video_device em28xx_radio_template = {
1824 .name = "em28xx-radio",
1825 .type = VID_TYPE_TUNER,
1826 .fops = &radio_fops,
1827 .minor = -1,
1828 .vidioc_querycap = radio_querycap,
1829 .vidioc_g_tuner = radio_g_tuner,
1830 .vidioc_enum_input = radio_enum_input,
1831 .vidioc_g_audio = radio_g_audio,
1832 .vidioc_s_tuner = radio_s_tuner,
1833 .vidioc_s_audio = radio_s_audio,
1834 .vidioc_s_input = radio_s_input,
1835 .vidioc_queryctrl = radio_queryctrl,
1836 .vidioc_g_ctrl = vidioc_g_ctrl,
1837 .vidioc_s_ctrl = vidioc_s_ctrl,
1838 .vidioc_g_frequency = vidioc_g_frequency,
1839 .vidioc_s_frequency = vidioc_s_frequency,
Mauro Carvalho Chehab1e7ad562008-02-06 09:00:41 -03001840#ifdef CONFIG_VIDEO_ADV_DEBUG
1841 .vidioc_g_register = vidioc_g_register,
1842 .vidioc_s_register = vidioc_s_register,
1843#endif
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001844};
1845
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03001846/******************************** usb interface ******************************/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001848
1849static LIST_HEAD(em28xx_extension_devlist);
1850static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1851
1852int em28xx_register_extension(struct em28xx_ops *ops)
1853{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001854 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001855
1856 mutex_lock(&em28xx_extension_devlist_lock);
1857 list_add_tail(&ops->next, &em28xx_extension_devlist);
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001858 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1859 if (dev)
1860 ops->init(dev);
1861 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001862 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1863 mutex_unlock(&em28xx_extension_devlist_lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001864 return 0;
1865}
1866EXPORT_SYMBOL(em28xx_register_extension);
1867
1868void em28xx_unregister_extension(struct em28xx_ops *ops)
1869{
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001870 struct em28xx *dev = NULL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001871
Devin Heitmueller0367ca12008-06-09 14:58:04 -03001872 list_for_each_entry(dev, &em28xx_devlist, devlist) {
1873 if (dev)
1874 ops->fini(dev);
1875 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001876
1877 mutex_lock(&em28xx_extension_devlist_lock);
1878 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1879 list_del(&ops->next);
1880 mutex_unlock(&em28xx_extension_devlist_lock);
1881}
1882EXPORT_SYMBOL(em28xx_unregister_extension);
1883
Adrian Bunk532fe652008-01-28 22:10:48 -03001884static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1885 const struct video_device *template,
1886 const int type,
1887 const char *type_name)
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001888{
1889 struct video_device *vfd;
1890
1891 vfd = video_device_alloc();
1892 if (NULL == vfd)
1893 return NULL;
1894 *vfd = *template;
1895 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -03001896 vfd->parent = &dev->udev->dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001897 vfd->release = video_device_release;
1898 vfd->type = type;
Mauro Carvalho Chehabe9e60402008-04-13 15:05:47 -03001899 vfd->debug = video_debug;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001900
1901 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1902 dev->name, type_name);
1903
1904 return vfd;
1905}
1906
1907
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001908/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001909 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001910 * allocates and inits the device structs, registers i2c bus and v4l device
1911 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001912static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001913 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001914{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001915 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001916 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001917 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001918 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001920
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001921 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001922 mutex_init(&dev->lock);
Aidan Thorntond7aa8022008-04-13 14:38:47 -03001923 spin_lock_init(&dev->slock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001924 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001925 init_waitqueue_head(&dev->wait_frame);
1926 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001928 dev->em28xx_write_regs = em28xx_write_regs;
1929 dev->em28xx_read_reg = em28xx_read_reg;
1930 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1931 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1932 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001933 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001934
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001935 em28xx_pre_card_setup(dev);
1936
1937 errCode = em28xx_config(dev);
1938 if (errCode) {
1939 em28xx_errdev("error configuring device\n");
1940 em28xx_devused &= ~(1<<dev->devno);
1941 kfree(dev);
1942 return -ENOMEM;
1943 }
1944
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001945 /* register i2c bus */
1946 em28xx_i2c_register(dev);
1947
1948 /* Do board specific init and eeprom reading */
1949 em28xx_card_setup(dev);
1950
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001951 /* Configure audio */
1952 em28xx_audio_analog_set(dev);
1953
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001954 /* configure the device */
1955 em28xx_config_i2c(dev);
1956
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001957 /* set default norm */
1958 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001959
1960 maxw = norm_maxw(dev);
1961 maxh = norm_maxh(dev);
1962
1963 /* set default image size */
1964 dev->width = maxw;
1965 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001966 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001967 dev->hscale = 0;
1968 dev->vscale = 0;
1969 dev->ctl_input = 2;
1970
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001971 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001972
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001973 list_add_tail(&dev->devlist, &em28xx_devlist);
1974
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001975 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001976 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1977 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001978 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001979 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001980 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001981 }
Mauro Carvalho Chehabed086312008-01-24 06:59:20 -03001982 if (dev->tuner_type != TUNER_ABSENT)
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001983 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001984
1985 /* register v4l2 video video_device */
1986 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1987 video_nr[dev->devno]);
1988 if (retval) {
1989 em28xx_errdev("unable to register video device (error=%i).\n",
1990 retval);
1991 goto fail_unreg;
1992 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001993
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001994 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001995 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1996 VFL_TYPE_VBI, "vbi");
1997 /* register v4l2 vbi video_device */
1998 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1999 vbi_nr[dev->devno]) < 0) {
2000 em28xx_errdev("unable to register vbi device\n");
2001 retval = -ENODEV;
2002 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002003 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002004
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002005 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
2006 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
2007 VFL_TYPE_RADIO, "radio");
2008 if (NULL == dev->radio_dev) {
2009 em28xx_errdev("cannot allocate video_device.\n");
2010 goto fail_unreg;
2011 }
2012 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
2013 radio_nr[dev->devno]);
2014 if (retval < 0) {
2015 em28xx_errdev("can't register radio device\n");
2016 goto fail_unreg;
2017 }
2018 em28xx_info("Registered radio device as /dev/radio%d\n",
2019 dev->radio_dev->minor & 0x1f);
2020 }
2021
Mauro Carvalho Chehabad0ebb92008-04-13 14:37:52 -03002022 /* init video dma queues */
2023 INIT_LIST_HEAD(&dev->vidq.active);
2024 INIT_LIST_HEAD(&dev->vidq.queued);
2025
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002026
Sascha Sommer5a804152007-11-03 21:22:38 -03002027 if (dev->has_msp34xx) {
2028 /* Send a reset to other chips via gpio */
2029 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
2030 msleep(3);
2031 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
2032 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03002033 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03002034
Sascha Sommer5a804152007-11-03 21:22:38 -03002035 video_mux(dev, 0);
2036
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002037 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
2038 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
2039 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002040
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002041 mutex_lock(&em28xx_extension_devlist_lock);
2042 if (!list_empty(&em28xx_extension_devlist)) {
2043 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2044 if (ops->id)
2045 ops->init(dev);
2046 }
2047 }
2048 mutex_unlock(&em28xx_extension_devlist_lock);
2049
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002050 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03002051
2052fail_unreg:
2053 em28xx_release_resources(dev);
2054 mutex_unlock(&dev->lock);
2055 kfree(dev);
2056 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002057}
2058
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002059#if defined(CONFIG_MODULES) && defined(MODULE)
2060static void request_module_async(struct work_struct *work)
2061{
2062 struct em28xx *dev = container_of(work,
2063 struct em28xx, request_module_wk);
2064
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03002065 if (dev->has_audio_class)
2066 request_module("snd-usb-audio");
2067 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002068 request_module("em28xx-alsa");
Mauro Carvalho Chehab3aefb792008-04-17 21:36:41 -03002069
2070 if (dev->has_dvb)
2071 request_module("em28xx-dvb");
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002072}
2073
2074static void request_modules(struct em28xx *dev)
2075{
2076 INIT_WORK(&dev->request_module_wk, request_module_async);
2077 schedule_work(&dev->request_module_wk);
2078}
2079#else
2080#define request_modules(dev)
2081#endif /* CONFIG_MODULES */
2082
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002083/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002084 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002085 * checks for supported devices
2086 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002087static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002088 const struct usb_device_id *id)
2089{
2090 const struct usb_endpoint_descriptor *endpoint;
2091 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002092 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002093 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002094 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002095 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002096
2097 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002098 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2099
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002100 /* Check to see next free device and mark as used */
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002101 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2102 em28xx_devused |= 1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002103
2104 /* Don't register audio interfaces */
2105 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002106 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002107 udev->descriptor.idVendor,
2108 udev->descriptor.idProduct,
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002109 ifnum,
2110 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002111
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002112 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002113 return -ENODEV;
2114 }
2115
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002116 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002117 udev->descriptor.idVendor,
2118 udev->descriptor.idProduct,
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002119 ifnum,
2120 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002121
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002122 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2123
Michael Opdenacker59c51592007-05-09 08:57:56 +02002124 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002125 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2126 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002127 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002128 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002129 return -ENODEV;
2130 }
2131 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002132 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002133 em28xx_devused &= ~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002134 return -ENODEV;
2135 }
2136
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002137 if (nr >= EM28XX_MAXBOARDS) {
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002138 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2139 EM28XX_MAXBOARDS);
2140 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002141 return -ENOMEM;
2142 }
2143
2144 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002145 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002146 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002147 em28xx_err(DRIVER_NAME ": out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002148 em28xx_devused &= ~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002149 return -ENOMEM;
2150 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002151
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002152 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002153 dev->devno = nr;
2154 dev->model = id->driver_info;
Mauro Carvalho Chehab3687e1e2008-02-08 15:44:25 -03002155 dev->alt = -1;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002156
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002157 /* Checks if audio is provided by some interface */
2158 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2159 uif = udev->config->interface[i];
2160 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2161 dev->has_audio_class = 1;
2162 break;
2163 }
2164 }
2165
2166 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2167 dev->has_audio_class ? "Has" : "Doesn't have");
2168
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002169 /* compute alternate max packet sizes */
2170 uif = udev->actconfig->interface[0];
2171
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002172 dev->num_alt = uif->num_altsetting;
2173 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2174/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2175 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2176
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002177 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002178 em28xx_errdev("out of memory!\n");
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002179 em28xx_devused &= ~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002180 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002181 return -ENOMEM;
2182 }
2183
2184 for (i = 0; i < dev->num_alt ; i++) {
2185 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2186 wMaxPacketSize);
2187 dev->alt_max_pkt_size[i] =
2188 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002189 em28xx_info("Alternate setting %i, max size= %i\n", i,
2190 dev->alt_max_pkt_size[i]);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002191 }
2192
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002193 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002194 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002195
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002196 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002197 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198 if (retval)
2199 return retval;
2200
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002201 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002202
2203 /* save our data pointer in this interface device */
2204 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002205
2206 request_modules(dev);
2207
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002208 return 0;
2209}
2210
2211/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002212 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002213 * called when the device gets diconencted
2214 * video device will be unregistered on v4l2_close in case it is still open
2215 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002216static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002217{
Sascha Sommer5a804152007-11-03 21:22:38 -03002218 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002219 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002220
2221 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002222 usb_set_intfdata(interface, NULL);
2223
2224 if (!dev)
2225 return;
2226
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002227 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002228
Douglas Schilling Landgraf6ea54d92008-04-17 21:41:10 -03002229 /* wait until all current v4l2 io is finished then deallocate
2230 resources */
Sascha Sommer5a804152007-11-03 21:22:38 -03002231 mutex_lock(&dev->lock);
2232
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002233 wake_up_interruptible_all(&dev->open);
2234
2235 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002236 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002237 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002238 "deallocation are deferred on close.\n",
2239 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2240
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002241 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002242 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002243 dev->state |= DEV_DISCONNECTED;
2244 wake_up_interruptible(&dev->wait_frame);
2245 wake_up_interruptible(&dev->wait_stream);
2246 } else {
2247 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002248 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002249 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002250 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002251
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002252 mutex_lock(&em28xx_extension_devlist_lock);
2253 if (!list_empty(&em28xx_extension_devlist)) {
2254 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2255 ops->fini(dev);
2256 }
2257 }
2258 mutex_unlock(&em28xx_extension_devlist_lock);
2259
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002260 if (!dev->users) {
2261 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002262 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002263 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002264}
2265
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002266static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002267 .name = "em28xx",
2268 .probe = em28xx_usb_probe,
2269 .disconnect = em28xx_usb_disconnect,
2270 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002271};
2272
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002273static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002274{
2275 int result;
2276
2277 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002278 (EM28XX_VERSION_CODE >> 16) & 0xff,
2279 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002280#ifdef SNAPSHOT
2281 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2282 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2283#endif
2284
2285 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002286 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002287 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002288 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002289 " usb_register failed. Error number %d.\n", result);
2290
2291 return result;
2292}
2293
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002294static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002295{
2296 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002297 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002298}
2299
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002300module_init(em28xx_module_init);
2301module_exit(em28xx_module_exit);