blob: c4db1aebdcd725b71d47f4f738564d8114f159d5 [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080036#include <linux/video_decoder.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 Verkuil2474ed42006-03-19 12:35:57 -030041#include <media/msp3400.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
44 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030045 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080046 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080047
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080048#define DRIVER_NAME "em28xx"
49#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030050#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080052#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080053 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020055 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080056
57MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_LICENSE("GPL");
60
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080061static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080062
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080063static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020064static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080066module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020067module_param_array(video_nr, int, NULL, 0444);
68module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080069MODULE_PARM_DESC(card,"card type");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020070MODULE_PARM_DESC(video_nr,"video device numbers");
71MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080072
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080073static unsigned int video_debug = 0;
74module_param(video_debug,int,0644);
75MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
76
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020077/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
78static unsigned long em28xx_devused;
79
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080080/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080081static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080082 {
83 .name = "PAL",
84 .id = V4L2_STD_PAL,
85 .mode = VIDEO_MODE_PAL,
86 }, {
87 .name = "NTSC",
88 .id = V4L2_STD_NTSC,
89 .mode = VIDEO_MODE_NTSC,
90 }, {
91 .name = "SECAM",
92 .id = V4L2_STD_SECAM,
93 .mode = VIDEO_MODE_SECAM,
94 }, {
95 .name = "PAL-M",
96 .id = V4L2_STD_PAL_M,
97 .mode = VIDEO_MODE_PAL,
98 }
99};
100
101#define TVNORMS ARRAY_SIZE(tvnorms)
102
103/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200104/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800105static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800106 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200107 .id = V4L2_CID_AUDIO_VOLUME,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "Volume",
110 .minimum = 0x0,
111 .maximum = 0x1f,
112 .step = 0x1,
113 .default_value = 0x1f,
114 .flags = 0,
115 },{
116 .id = V4L2_CID_AUDIO_MUTE,
117 .type = V4L2_CTRL_TYPE_BOOLEAN,
118 .name = "Mute",
119 .minimum = 0,
120 .maximum = 1,
121 .step = 1,
122 .default_value = 1,
123 .flags = 0,
124 }
125};
126
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800127static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800128
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800129
130/********************* v4l2 interface ******************************************/
131
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800132/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800133 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800134 * inits registers with sane defaults
135 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800136static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800137{
138
139 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300140 if (!dev->is_em2800)
141 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800142
143 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200144
Markus Rechberger9475fb12006-02-27 00:07:34 -0300145/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
146/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200147 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
148
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800149 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800150 dev->mute = 1; /* maybe not the right place... */
151 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800152 em28xx_audio_analog_set(dev);
153 em28xx_audio_analog_setup(dev);
154 em28xx_outfmt_set_yuv422(dev);
155 em28xx_colorlevels_set_default(dev);
156 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800157
158 return 0;
159}
160
161/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800162 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800163 * configure i2c attached devices
164 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800165static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800166{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300167 struct v4l2_routing route;
168
169 route.input = INPUT(dev->ctl_input)->vmux;
170 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100171 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300172 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300173 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800174}
175
176/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800177 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800178 * prepare queues for incoming and outgoing frames
179 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800180static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800181{
182 u32 i;
183
184 INIT_LIST_HEAD(&dev->inqueue);
185 INIT_LIST_HEAD(&dev->outqueue);
186
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800187 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800188 dev->frame[i].state = F_UNUSED;
189 dev->frame[i].buf.bytesused = 0;
190 }
191}
192
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800193static void video_mux(struct em28xx *dev, int index)
194{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300195 int ainput;
196 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800197
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300198 route.input = INPUT(index)->vmux;
199 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800200 dev->ctl_input = index;
201 dev->ctl_ainput = INPUT(index)->amux;
202
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300203 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800204
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800205 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200206 if (dev->i2s_speed)
207 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300208 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300209 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300210 /* Note: this is msp3400 specific */
211 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800212 ainput = EM28XX_AUDIO_SRC_TUNER;
213 em28xx_audio_source(dev, ainput);
214 } else {
215 switch (dev->ctl_ainput) {
Markus Rechberger9475fb12006-02-27 00:07:34 -0300216 case 0:
217 ainput = EM28XX_AUDIO_SRC_TUNER;
218 break;
219 default:
220 ainput = EM28XX_AUDIO_SRC_LINE;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800221 }
222 em28xx_audio_source(dev, ainput);
223 }
224}
225
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300226/* Usage lock check functions */
227static int res_get(struct em28xx_fh *fh)
228{
229 struct em28xx *dev = fh->dev;
230 int rc = 0;
231
232 /* This instance already has stream_on */
233 if (fh->stream_on)
234 return rc;
235
236 mutex_lock(&dev->lock);
237
238 if (dev->stream_on)
239 rc = -EINVAL;
240 else {
241 dev->stream_on = 1;
242 fh->stream_on = 1;
243 }
244
245 mutex_unlock(&dev->lock);
246 return rc;
247}
248
249static int res_check(struct em28xx_fh *fh)
250{
251 return (fh->stream_on);
252}
253
254static void res_free(struct em28xx_fh *fh)
255{
256 struct em28xx *dev = fh->dev;
257
258 mutex_lock(&dev->lock);
259 fh->stream_on = 0;
260 dev->stream_on = 0;
261 mutex_unlock(&dev->lock);
262}
263
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800264/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300265 * em28xx_vm_open()
266 */
267static void em28xx_vm_open(struct vm_area_struct *vma)
268{
269 struct em28xx_frame_t *f = vma->vm_private_data;
270 f->vma_use_count++;
271}
272
273/*
274 * em28xx_vm_close()
275 */
276static void em28xx_vm_close(struct vm_area_struct *vma)
277{
278 /* NOTE: buffers are not freed here */
279 struct em28xx_frame_t *f = vma->vm_private_data;
280
281 if (f->vma_use_count)
282 f->vma_use_count--;
283}
284
285static struct vm_operations_struct em28xx_vm_ops = {
286 .open = em28xx_vm_open,
287 .close = em28xx_vm_close,
288};
289
290
291/*
292 * em28xx_get_ctrl()
293 * return the current saturation, brightness or contrast, mute state
294 */
295static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
296{
297 switch (ctrl->id) {
298 case V4L2_CID_AUDIO_MUTE:
299 ctrl->value = dev->mute;
300 return 0;
301 case V4L2_CID_AUDIO_VOLUME:
302 ctrl->value = dev->volume;
303 return 0;
304 default:
305 return -EINVAL;
306 }
307}
308
309/*
310 * em28xx_set_ctrl()
311 * mute or set new saturation, brightness or contrast
312 */
313static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
314{
315 switch (ctrl->id) {
316 case V4L2_CID_AUDIO_MUTE:
317 if (ctrl->value != dev->mute) {
318 dev->mute = ctrl->value;
319 em28xx_audio_usb_mute(dev, ctrl->value);
320 return em28xx_audio_analog_set(dev);
321 }
322 return 0;
323 case V4L2_CID_AUDIO_VOLUME:
324 dev->volume = ctrl->value;
325 return em28xx_audio_analog_set(dev);
326 default:
327 return -EINVAL;
328 }
329}
330
331/*
332 * em28xx_stream_interrupt()
333 * stops streaming
334 */
335static int em28xx_stream_interrupt(struct em28xx *dev)
336{
337 int rc = 0;
338
339 /* stop reading from the device */
340
341 dev->stream = STREAM_INTERRUPT;
342 rc = wait_event_timeout(dev->wait_stream,
343 (dev->stream == STREAM_OFF) ||
344 (dev->state & DEV_DISCONNECTED),
345 EM28XX_URB_TIMEOUT);
346
347 if (rc) {
348 dev->state |= DEV_MISCONFIGURED;
349 em28xx_videodbg("device is misconfigured; close and "
350 "open /dev/video%d again\n",
351 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
352 return rc;
353 }
354
355 return 0;
356}
357
358
359static int check_dev(struct em28xx *dev)
360{
361 if (dev->state & DEV_DISCONNECTED) {
362 em28xx_errdev("v4l2 ioctl: device not present\n");
363 return -ENODEV;
364 }
365
366 if (dev->state & DEV_MISCONFIGURED) {
367 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
368 "close and open it again\n");
369 return -EIO;
370 }
371 return 0;
372}
373
374static void get_scale(struct em28xx *dev,
375 unsigned int width, unsigned int height,
376 unsigned int *hscale, unsigned int *vscale)
377{
378 unsigned int maxw = norm_maxw(dev);
379 unsigned int maxh = norm_maxh(dev);
380
381 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
382 if (*hscale >= 0x4000)
383 *hscale = 0x3fff;
384
385 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
386 if (*vscale >= 0x4000)
387 *vscale = 0x3fff;
388}
389
390/* ------------------------------------------------------------------
391 IOCTL vidioc handling
392 ------------------------------------------------------------------*/
393
394static int vidioc_g_fmt_cap(struct file *file, void *priv,
395 struct v4l2_format *f)
396{
397 struct em28xx_fh *fh = priv;
398 struct em28xx *dev = fh->dev;
399
400 mutex_lock(&dev->lock);
401
402 f->fmt.pix.width = dev->width;
403 f->fmt.pix.height = dev->height;
404 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
405 f->fmt.pix.bytesperline = dev->bytesperline;
406 f->fmt.pix.sizeimage = dev->frame_size;
407 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
408
409 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
410 f->fmt.pix.field = dev->interlaced ?
411 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
412
413 mutex_unlock(&dev->lock);
414 return 0;
415}
416
417static int vidioc_try_fmt_cap(struct file *file, void *priv,
418 struct v4l2_format *f)
419{
420 struct em28xx_fh *fh = priv;
421 struct em28xx *dev = fh->dev;
422 int width = f->fmt.pix.width;
423 int height = f->fmt.pix.height;
424 unsigned int maxw = norm_maxw(dev);
425 unsigned int maxh = norm_maxh(dev);
426 unsigned int hscale, vscale;
427
428 /* width must even because of the YUYV format
429 height must be even because of interlacing */
430 height &= 0xfffe;
431 width &= 0xfffe;
432
433 if (height < 32)
434 height = 32;
435 if (height > maxh)
436 height = maxh;
437 if (width < 48)
438 width = 48;
439 if (width > maxw)
440 width = maxw;
441
442 mutex_lock(&dev->lock);
443
444 if (dev->is_em2800) {
445 /* the em2800 can only scale down to 50% */
446 if (height % (maxh / 2))
447 height = maxh;
448 if (width % (maxw / 2))
449 width = maxw;
450 /* according to empiatech support */
451 /* the MaxPacketSize is to small to support */
452 /* framesizes larger than 640x480 @ 30 fps */
453 /* or 640x576 @ 25 fps. As this would cut */
454 /* of a part of the image we prefer */
455 /* 360x576 or 360x480 for now */
456 if (width == maxw && height == maxh)
457 width /= 2;
458 }
459
460 get_scale(dev, width, height, &hscale, &vscale);
461
462 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
463 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
464
465 f->fmt.pix.width = width;
466 f->fmt.pix.height = height;
467 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
468 f->fmt.pix.bytesperline = width * 2;
469 f->fmt.pix.sizeimage = width * 2 * height;
470 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
471 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
472
473 mutex_unlock(&dev->lock);
474 return 0;
475}
476
477static int vidioc_s_fmt_cap(struct file *file, void *priv,
478 struct v4l2_format *f)
479{
480 struct em28xx_fh *fh = priv;
481 struct em28xx *dev = fh->dev;
482 int rc, i;
483
484 rc = check_dev(dev);
485 if (rc < 0)
486 return rc;
487
488 vidioc_try_fmt_cap(file, priv, f);
489
490 mutex_lock(&dev->lock);
491
492 for (i = 0; i < dev->num_frames; i++)
493 if (dev->frame[i].vma_use_count) {
494 em28xx_videodbg("VIDIOC_S_FMT failed. "
495 "Unmap the buffers first.\n");
496 rc = -EINVAL;
497 goto err;
498 }
499
500 /* stop io in case it is already in progress */
501 if (dev->stream == STREAM_ON) {
502 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
503 rc = em28xx_stream_interrupt(dev);
504 if (rc < 0)
505 goto err;
506 }
507
508 em28xx_release_buffers(dev);
509 dev->io = IO_NONE;
510
511 /* set new image size */
512 dev->width = f->fmt.pix.width;
513 dev->height = f->fmt.pix.height;
514 dev->frame_size = dev->width * dev->height * 2;
515 dev->field_size = dev->frame_size >> 1;
516 dev->bytesperline = dev->width * 2;
517 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
518
519 /* FIXME: This is really weird! Why capture is starting with
520 this ioctl ???
521 */
522 em28xx_uninit_isoc(dev);
523 em28xx_set_alternate(dev);
524 em28xx_capture_start(dev, 1);
525 em28xx_resolution_set(dev);
526 em28xx_init_isoc(dev);
527 rc = 0;
528
529err:
530 mutex_unlock(&dev->lock);
531 return rc;
532}
533
534static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
535{
536 struct em28xx_fh *fh = priv;
537 struct em28xx *dev = fh->dev;
538 struct v4l2_format f;
539 unsigned int i;
540 int rc;
541
542 rc = check_dev(dev);
543 if (rc < 0)
544 return rc;
545
546 for (i = 0; i < TVNORMS; i++)
547 if (*norm == tvnorms[i].id)
548 break;
549 if (i == TVNORMS)
550 for (i = 0; i < TVNORMS; i++)
551 if (*norm & tvnorms[i].id)
552 break;
553 if (i == TVNORMS)
554 return -EINVAL;
555
556 *norm = tvnorms[i].id;
557
558 mutex_lock(&dev->lock);
559 dev->tvnorm = &tvnorms[i];
560 mutex_unlock(&dev->lock);
561
562 /* Adjusts width/height, if needed */
563 f.fmt.pix.width = dev->width;
564 f.fmt.pix.height = dev->height;
565 vidioc_try_fmt_cap(file, priv, &f);
566
567 mutex_lock(&dev->lock);
568
569 /* set new image size */
570 dev->width = f.fmt.pix.width;
571 dev->height = f.fmt.pix.height;
572 dev->frame_size = dev->width * dev->height * 2;
573 dev->field_size = dev->frame_size >> 1;
574 dev->bytesperline = dev->width * 2;
575 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
576
577 em28xx_resolution_set(dev);
578 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
579
580 mutex_unlock(&dev->lock);
581 return 0;
582}
583
584static const char *iname[] = {
585 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
586 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
587 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
588 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
589 [EM28XX_VMUX_SVIDEO] = "S-Video",
590 [EM28XX_VMUX_TELEVISION] = "Television",
591 [EM28XX_VMUX_CABLE] = "Cable TV",
592 [EM28XX_VMUX_DVB] = "DVB",
593 [EM28XX_VMUX_DEBUG] = "for debug only",
594};
595
596static int vidioc_enum_input(struct file *file, void *priv,
597 struct v4l2_input *i)
598{
599 struct em28xx_fh *fh = priv;
600 struct em28xx *dev = fh->dev;
601 unsigned int n;
602
603 n = i->index;
604 if (n >= MAX_EM28XX_INPUT)
605 return -EINVAL;
606 if (0 == INPUT(n)->type)
607 return -EINVAL;
608
609 i->index = n;
610 i->type = V4L2_INPUT_TYPE_CAMERA;
611
612 strcpy(i->name, iname[INPUT(n)->type]);
613
614 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
615 (EM28XX_VMUX_CABLE == INPUT(n)->type))
616 i->type = V4L2_INPUT_TYPE_TUNER;
617
618 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
619 i->std |= tvnorms[n].id;
620
621 return 0;
622}
623
624static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
625{
626 struct em28xx_fh *fh = priv;
627 struct em28xx *dev = fh->dev;
628
629 *i = dev->ctl_input;
630
631 return 0;
632}
633
634static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
635{
636 struct em28xx_fh *fh = priv;
637 struct em28xx *dev = fh->dev;
638 int rc;
639
640 rc = check_dev(dev);
641 if (rc < 0)
642 return rc;
643
644 if (i >= MAX_EM28XX_INPUT)
645 return -EINVAL;
646 if (0 == INPUT(i)->type)
647 return -EINVAL;
648
649 mutex_lock(&dev->lock);
650
651 video_mux(dev, i);
652
653 mutex_unlock(&dev->lock);
654 return 0;
655}
656
657static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
658{
659 struct em28xx_fh *fh = priv;
660 struct em28xx *dev = fh->dev;
661 unsigned int index = a->index;
662
663 if (a->index > 1)
664 return -EINVAL;
665
666 index = dev->ctl_ainput;
667
668 if (index == 0) {
669 strcpy(a->name, "Television");
670 } else {
671 strcpy(a->name, "Line In");
672 }
673 a->capability = V4L2_AUDCAP_STEREO;
674 a->index = index;
675
676 return 0;
677}
678
679static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
680{
681 struct em28xx_fh *fh = priv;
682 struct em28xx *dev = fh->dev;
683
684 if (a->index != dev->ctl_ainput)
685 return -EINVAL;
686
687 return 0;
688}
689
690static int vidioc_queryctrl(struct file *file, void *priv,
691 struct v4l2_queryctrl *qc)
692{
693 struct em28xx_fh *fh = priv;
694 struct em28xx *dev = fh->dev;
695 int id = qc->id;
696 int i;
697 int rc;
698
699 rc = check_dev(dev);
700 if (rc < 0)
701 return rc;
702
703 memset(qc, 0, sizeof(*qc));
704
705 qc->id = id;
706
707 if (!dev->has_msp34xx) {
708 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
709 if (qc->id && qc->id == em28xx_qctrl[i].id) {
710 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
711 return 0;
712 }
713 }
714 }
715 mutex_lock(&dev->lock);
716 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
717 mutex_unlock(&dev->lock);
718
719 if (qc->type)
720 return 0;
721 else
722 return -EINVAL;
723}
724
725static int vidioc_g_ctrl(struct file *file, void *priv,
726 struct v4l2_control *ctrl)
727{
728 struct em28xx_fh *fh = priv;
729 struct em28xx *dev = fh->dev;
730 int rc;
731
732 rc = check_dev(dev);
733 if (rc < 0)
734 return rc;
735 mutex_lock(&dev->lock);
736
737 if (!dev->has_msp34xx)
738 rc = em28xx_get_ctrl(dev, ctrl);
739 else
740 rc = -EINVAL;
741
742 if (rc == -EINVAL) {
743 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
744 rc = 0;
745 }
746
747 mutex_unlock(&dev->lock);
748 return rc;
749}
750
751static int vidioc_s_ctrl(struct file *file, void *priv,
752 struct v4l2_control *ctrl)
753{
754 struct em28xx_fh *fh = priv;
755 struct em28xx *dev = fh->dev;
756 u8 i;
757 int rc;
758
759 rc = check_dev(dev);
760 if (rc < 0)
761 return rc;
762
763 mutex_lock(&dev->lock);
764
765 if (dev->has_msp34xx)
766 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
767 else {
768 rc = 1;
769 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
770 if (ctrl->id == em28xx_qctrl[i].id) {
771 if (ctrl->value < em28xx_qctrl[i].minimum ||
772 ctrl->value > em28xx_qctrl[i].maximum) {
773 rc = -ERANGE;
774 break;
775 }
776
777 rc = em28xx_set_ctrl(dev, ctrl);
778 break;
779 }
780 }
781 }
782
783 /* Control not found - try to send it to the attached devices */
784 if (rc == 1) {
785 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
786 rc = 0;
787 }
788
789 mutex_unlock(&dev->lock);
790 return rc;
791}
792
793static int vidioc_g_tuner(struct file *file, void *priv,
794 struct v4l2_tuner *t)
795{
796 struct em28xx_fh *fh = priv;
797 struct em28xx *dev = fh->dev;
798 int rc;
799
800 rc = check_dev(dev);
801 if (rc < 0)
802 return rc;
803
804 if (0 != t->index)
805 return -EINVAL;
806
807 strcpy(t->name, "Tuner");
808
809 mutex_lock(&dev->lock);
810
811 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
812
813 mutex_unlock(&dev->lock);
814 return 0;
815}
816
817static int vidioc_s_tuner(struct file *file, void *priv,
818 struct v4l2_tuner *t)
819{
820 struct em28xx_fh *fh = priv;
821 struct em28xx *dev = fh->dev;
822 int rc;
823
824 rc = check_dev(dev);
825 if (rc < 0)
826 return rc;
827
828 if (0 != t->index)
829 return -EINVAL;
830
831 mutex_lock(&dev->lock);
832
833 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
834
835 mutex_unlock(&dev->lock);
836 return 0;
837}
838
839static int vidioc_g_frequency(struct file *file, void *priv,
840 struct v4l2_frequency *f)
841{
842 struct em28xx_fh *fh = priv;
843 struct em28xx *dev = fh->dev;
844
845 f->type = V4L2_TUNER_ANALOG_TV;
846 f->frequency = dev->ctl_freq;
847
848 return 0;
849}
850
851static int vidioc_s_frequency(struct file *file, void *priv,
852 struct v4l2_frequency *f)
853{
854 struct em28xx_fh *fh = priv;
855 struct em28xx *dev = fh->dev;
856 int rc;
857
858 rc = check_dev(dev);
859 if (rc < 0)
860 return rc;
861
862 if (0 != f->tuner)
863 return -EINVAL;
864
865 if (V4L2_TUNER_ANALOG_TV != f->type)
866 return -EINVAL;
867
868 mutex_lock(&dev->lock);
869
870 dev->ctl_freq = f->frequency;
871 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
872
873 mutex_unlock(&dev->lock);
874 return 0;
875}
876
877static int vidioc_cropcap(struct file *file, void *priv,
878 struct v4l2_cropcap *cc)
879{
880 struct em28xx_fh *fh = priv;
881 struct em28xx *dev = fh->dev;
882
883 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
884 return -EINVAL;
885
886 cc->bounds.left = 0;
887 cc->bounds.top = 0;
888 cc->bounds.width = dev->width;
889 cc->bounds.height = dev->height;
890 cc->defrect = cc->bounds;
891 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
892 cc->pixelaspect.denominator = 59;
893
894 return 0;
895}
896
897static int vidioc_streamon(struct file *file, void *priv,
898 enum v4l2_buf_type type)
899{
900 struct em28xx_fh *fh = priv;
901 struct em28xx *dev = fh->dev;
902 int rc;
903
904 rc = check_dev(dev);
905 if (rc < 0)
906 return rc;
907
908 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
909 return -EINVAL;
910
911 if (list_empty(&dev->inqueue))
912 return -EINVAL;
913
914 mutex_lock(&dev->lock);
915
916 if (unlikely(res_get(fh) < 0)) {
917 mutex_unlock(&dev->lock);
918 return -EBUSY;
919 }
920
921 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
922
923 mutex_unlock(&dev->lock);
924 return 0;
925}
926
927static int vidioc_streamoff(struct file *file, void *priv,
928 enum v4l2_buf_type type)
929{
930 struct em28xx_fh *fh = priv;
931 struct em28xx *dev = fh->dev;
932 int rc;
933
934 rc = check_dev(dev);
935 if (rc < 0)
936 return rc;
937
938 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
939 return -EINVAL;
940
941 mutex_lock(&dev->lock);
942
943 if (dev->stream == STREAM_ON) {
944 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
945 rc = em28xx_stream_interrupt(dev);
946 if (rc < 0) {
947 mutex_unlock(&dev->lock);
948 return rc;
949 }
950 }
951
952 em28xx_empty_framequeues(dev);
953
954 mutex_unlock(&dev->lock);
955 return 0;
956}
957
958static int vidioc_querycap(struct file *file, void *priv,
959 struct v4l2_capability *cap)
960{
961 struct em28xx_fh *fh = priv;
962 struct em28xx *dev = fh->dev;
963
964 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
965 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
966 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
967
968 cap->version = EM28XX_VERSION_CODE;
969
970 cap->capabilities =
971 V4L2_CAP_SLICED_VBI_CAPTURE |
972 V4L2_CAP_VIDEO_CAPTURE |
973 V4L2_CAP_AUDIO |
974 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
975
976 if (dev->has_tuner)
977 cap->capabilities |= V4L2_CAP_TUNER;
978
979 return 0;
980}
981
982static int vidioc_enum_fmt_cap(struct file *file, void *priv,
983 struct v4l2_fmtdesc *fmtd)
984{
985 if (fmtd->index != 0)
986 return -EINVAL;
987
988 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
989 strcpy(fmtd->description, "Packed YUY2");
990 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
991 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
992
993 return 0;
994}
995
996/* Sliced VBI ioctls */
997static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
998 struct v4l2_format *f)
999{
1000 struct em28xx_fh *fh = priv;
1001 struct em28xx *dev = fh->dev;
1002 int rc;
1003
1004 rc = check_dev(dev);
1005 if (rc < 0)
1006 return rc;
1007
1008 mutex_lock(&dev->lock);
1009
1010 f->fmt.sliced.service_set = 0;
1011
1012 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1013
1014 if (f->fmt.sliced.service_set == 0)
1015 rc = -EINVAL;
1016
1017 mutex_unlock(&dev->lock);
1018 return rc;
1019}
1020
1021static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
1022 struct v4l2_format *f)
1023{
1024 struct em28xx_fh *fh = priv;
1025 struct em28xx *dev = fh->dev;
1026 int rc;
1027
1028 rc = check_dev(dev);
1029 if (rc < 0)
1030 return rc;
1031
1032 mutex_lock(&dev->lock);
1033 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
1034 mutex_unlock(&dev->lock);
1035
1036 if (f->fmt.sliced.service_set == 0)
1037 return -EINVAL;
1038
1039 return 0;
1040}
1041
1042
1043static int vidioc_reqbufs(struct file *file, void *priv,
1044 struct v4l2_requestbuffers *rb)
1045{
1046 struct em28xx_fh *fh = priv;
1047 struct em28xx *dev = fh->dev;
1048 u32 i;
1049 int rc;
1050
1051 rc = check_dev(dev);
1052 if (rc < 0)
1053 return rc;
1054
1055 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1056 rb->memory != V4L2_MEMORY_MMAP)
1057 return -EINVAL;
1058
1059 if (dev->io == IO_READ) {
1060 em28xx_videodbg("method is set to read;"
1061 " close and open the device again to"
1062 " choose the mmap I/O method\n");
1063 return -EINVAL;
1064 }
1065
1066 for (i = 0; i < dev->num_frames; i++)
1067 if (dev->frame[i].vma_use_count) {
1068 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1069 "previous buffers are still mapped\n");
1070 return -EINVAL;
1071 }
1072
1073 mutex_lock(&dev->lock);
1074
1075 if (dev->stream == STREAM_ON) {
1076 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1077 rc = em28xx_stream_interrupt(dev);
1078 if (rc < 0) {
1079 mutex_unlock(&dev->lock);
1080 return rc;
1081 }
1082 }
1083
1084 em28xx_empty_framequeues(dev);
1085
1086 em28xx_release_buffers(dev);
1087 if (rb->count)
1088 rb->count = em28xx_request_buffers(dev, rb->count);
1089
1090 dev->frame_current = NULL;
1091 dev->io = rb->count ? IO_MMAP : IO_NONE;
1092
1093 mutex_unlock(&dev->lock);
1094 return 0;
1095}
1096
1097static int vidioc_querybuf(struct file *file, void *priv,
1098 struct v4l2_buffer *b)
1099{
1100 struct em28xx_fh *fh = priv;
1101 struct em28xx *dev = fh->dev;
1102 int rc;
1103
1104 rc = check_dev(dev);
1105 if (rc < 0)
1106 return rc;
1107
1108 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1109 b->index >= dev->num_frames || dev->io != IO_MMAP)
1110 return -EINVAL;
1111
1112 mutex_lock(&dev->lock);
1113
1114 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1115
1116 if (dev->frame[b->index].vma_use_count)
1117 b->flags |= V4L2_BUF_FLAG_MAPPED;
1118
1119 if (dev->frame[b->index].state == F_DONE)
1120 b->flags |= V4L2_BUF_FLAG_DONE;
1121 else if (dev->frame[b->index].state != F_UNUSED)
1122 b->flags |= V4L2_BUF_FLAG_QUEUED;
1123
1124 mutex_unlock(&dev->lock);
1125 return 0;
1126}
1127
1128static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1129{
1130 struct em28xx_fh *fh = priv;
1131 struct em28xx *dev = fh->dev;
1132 unsigned long lock_flags;
1133 int rc;
1134
1135 rc = check_dev(dev);
1136 if (rc < 0)
1137 return rc;
1138
1139 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1140 b->index >= dev->num_frames)
1141 return -EINVAL;
1142
1143 if (dev->frame[b->index].state != F_UNUSED)
1144 return -EAGAIN;
1145
1146 dev->frame[b->index].state = F_QUEUED;
1147
1148 /* add frame to fifo */
1149 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1150 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1151 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1152
1153 return 0;
1154}
1155
1156static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1157{
1158 struct em28xx_fh *fh = priv;
1159 struct em28xx *dev = fh->dev;
1160 int rc;
1161 struct em28xx_frame_t *f;
1162 unsigned long lock_flags;
1163
1164 rc = check_dev(dev);
1165 if (rc < 0)
1166 return rc;
1167
1168 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1169 return -EINVAL;
1170
1171 if (list_empty(&dev->outqueue)) {
1172 if (dev->stream == STREAM_OFF)
1173 return -EINVAL;
1174
1175 if (file->f_flags & O_NONBLOCK)
1176 return -EAGAIN;
1177
1178 rc = wait_event_interruptible(dev->wait_frame,
1179 (!list_empty(&dev->outqueue)) ||
1180 (dev->state & DEV_DISCONNECTED));
1181 if (rc)
1182 return rc;
1183
1184 if (dev->state & DEV_DISCONNECTED)
1185 return -ENODEV;
1186 }
1187
1188 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1189 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1190 list_del(dev->outqueue.next);
1191 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1192
1193 f->state = F_UNUSED;
1194 memcpy(b, &f->buf, sizeof(*b));
1195
1196 if (f->vma_use_count)
1197 b->flags |= V4L2_BUF_FLAG_MAPPED;
1198
1199 return 0;
1200}
1201
1202/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001203 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001204 * inits the device and starts isoc transfer
1205 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001206static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001207{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001208 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001209 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001210 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001211 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001212
Trent Piephoa991f442007-10-10 05:37:43 -03001213 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001214 if (h->vdev->minor == minor) {
1215 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001216 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1217 }
1218 if (h->vbi_dev->minor == minor) {
1219 dev = h;
1220 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001221 }
1222 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001223 if (NULL == dev)
1224 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001225
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001226 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1227 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001228
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001229 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001230
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001231 if (!fh) {
1232 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1233 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001234 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001235 mutex_lock(&dev->lock);
1236 fh->dev = dev;
1237 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001238
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001239 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001240 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001241
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001242 dev->width = norm_maxw(dev);
1243 dev->height = norm_maxh(dev);
1244 dev->frame_size = dev->width * dev->height * 2;
1245 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1246 dev->bytesperline = dev->width * 2;
1247 dev->hscale = 0;
1248 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001249
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001250 em28xx_capture_start(dev, 1);
1251 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001252
Markus Rechbergere5d4a562006-02-07 06:49:13 -02001253
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001254 /* start the transfer */
1255 errCode = em28xx_init_isoc(dev);
1256 if (errCode)
1257 goto err;
1258
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001259 em28xx_empty_framequeues(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001260 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001261
1262 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001263
Ingo Molnar3593cab2006-02-07 06:49:14 -02001264err:
1265 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001266 return errCode;
1267}
1268
1269/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001270 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001271 * unregisters the v4l2,i2c and usb devices
1272 * called when the device gets disconected or at module unload
1273*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001274static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001275{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001276
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001277 /*FIXME: I2C IR should be disconnected */
1278
1279 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1280 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1281 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001282 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001283 video_unregister_device(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001284 video_unregister_device(dev->vbi_dev);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001285 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001286 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001287
1288
1289 /* Mark device as unused */
1290 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001291}
1292
1293/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001294 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001295 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1296 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001297static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001298{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001299 struct em28xx_fh *fh = filp->private_data;
1300 struct em28xx *dev = fh->dev;
1301 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001302
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001303 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001304
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001305
1306 if (res_check(fh))
1307 res_free(fh);
1308
Ingo Molnar3593cab2006-02-07 06:49:14 -02001309 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001310
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001311 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001312 em28xx_uninit_isoc(dev);
1313 em28xx_release_buffers(dev);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001314 dev->io = IO_NONE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001315
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001316 /* the device is already disconnect,
1317 free the remaining resources */
1318 if (dev->state & DEV_DISCONNECTED) {
1319 em28xx_release_resources(dev);
1320 mutex_unlock(&dev->lock);
1321 kfree(dev);
1322 return 0;
1323 }
1324
1325 /* set alternate 0 */
1326 dev->alt = 0;
1327 em28xx_videodbg("setting alternate 0\n");
1328 errCode = usb_set_interface(dev->udev, 0, 0);
1329 if (errCode < 0) {
1330 em28xx_errdev("cannot change alternate number to "
1331 "0 (error=%i)\n", errCode);
1332 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001333 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001334 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001335 dev->users--;
1336 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001337 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001338 return 0;
1339}
1340
1341/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001342 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001343 * will allocate buffers when called for the first time
1344 */
1345static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001346em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001347 loff_t * f_pos)
1348{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001349 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001350 unsigned long lock_flags;
1351 int ret = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001352 struct em28xx_fh *fh = filp->private_data;
1353 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001354
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001355 /* FIXME: read() is not prepared to allow changing the video
1356 resolution while streaming. Seems a bug at em28xx_set_fmt
1357 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001358
1359 if (unlikely(res_get(fh) < 0))
1360 return -EBUSY;
1361
Sascha Sommer5a804152007-11-03 21:22:38 -03001362 mutex_lock(&dev->lock);
1363
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001364 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001365 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001366
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001367 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1368 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1369 em28xx_videodbg("not supported yet! ...\n");
1370 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001371 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001372 return -EFAULT;
1373 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001374 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001375 return (1);
1376 }
1377 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1378 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1379 em28xx_videodbg("not supported yet! ...\n");
1380 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001381 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001382 return -EFAULT;
1383 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001384 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001385 return (1);
1386 }
1387
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001388 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001389 em28xx_videodbg("device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001390 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001391 return -ENODEV;
1392 }
1393
1394 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001395 em28xx_videodbg("device misconfigured; close and open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001396 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001397 return -EIO;
1398 }
1399
1400 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001401 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001402 " the device again to choose the read method\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001403 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001404 return -EINVAL;
1405 }
1406
1407 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001408 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1409 em28xx_errdev("read failed, not enough memory\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001410 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001411 return -ENOMEM;
1412 }
1413 dev->io = IO_READ;
1414 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001415 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001416 }
1417
1418 if (!count) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001419 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001420 return 0;
1421 }
1422
1423 if (list_empty(&dev->outqueue)) {
1424 if (filp->f_flags & O_NONBLOCK) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001425 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001426 return -EAGAIN;
1427 }
1428 ret = wait_event_interruptible
1429 (dev->wait_frame,
1430 (!list_empty(&dev->outqueue)) ||
1431 (dev->state & DEV_DISCONNECTED));
1432 if (ret) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001433 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001434 return ret;
1435 }
1436 if (dev->state & DEV_DISCONNECTED) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001437 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001438 return -ENODEV;
1439 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001440 dev->video_bytesread = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001441 }
1442
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001443 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001444
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001445 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001446
1447 if (count > f->buf.length)
1448 count = f->buf.length;
1449
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001450 if ((dev->video_bytesread + count) > dev->frame_size)
1451 count = dev->frame_size - dev->video_bytesread;
1452
1453 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1454 em28xx_err("Error while copying to user\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001455 return -EFAULT;
1456 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001457 dev->video_bytesread += count;
1458
1459 if (dev->video_bytesread == dev->frame_size) {
1460 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1461 list_for_each_entry(i, &dev->outqueue, frame)
1462 i->state = F_UNUSED;
1463 INIT_LIST_HEAD(&dev->outqueue);
1464 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1465
1466 em28xx_queue_unusedframes(dev);
1467 dev->video_bytesread = 0;
1468 }
1469
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001470 *f_pos += count;
1471
Sascha Sommer5a804152007-11-03 21:22:38 -03001472 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001473
1474 return count;
1475}
1476
1477/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001478 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001479 * will allocate buffers when called for the first time
1480 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001481static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001482{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001483 unsigned int mask = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001484 struct em28xx_fh *fh = filp->private_data;
1485 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001486
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001487 if (unlikely(res_get(fh) < 0))
1488 return POLLERR;
1489
Sascha Sommer5a804152007-11-03 21:22:38 -03001490 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001491
1492 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001493 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001494 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001495 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001496 } else {
1497 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001498 if (!em28xx_request_buffers
1499 (dev, EM28XX_NUM_READ_FRAMES)) {
1500 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001501 ("poll() failed, not enough memory\n");
1502 } else {
1503 dev->io = IO_READ;
1504 dev->stream = STREAM_ON;
1505 }
1506 }
1507
1508 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001509 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001510 poll_wait(filp, &dev->wait_frame, wait);
1511
1512 if (!list_empty(&dev->outqueue))
1513 mask |= POLLIN | POLLRDNORM;
1514
Sascha Sommer5a804152007-11-03 21:22:38 -03001515 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001516
1517 return mask;
1518 }
1519 }
1520
Sascha Sommer5a804152007-11-03 21:22:38 -03001521 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001522 return POLLERR;
1523}
1524
1525/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001526 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001527 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001528static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001529{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001530 struct em28xx_fh *fh = filp->private_data;
1531 struct em28xx *dev = fh->dev;
1532 unsigned long size = vma->vm_end - vma->vm_start;
1533 unsigned long start = vma->vm_start;
1534 void *pos;
1535 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -08001536
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001537 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001538 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001539
1540 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001541
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001542 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001543 em28xx_videodbg("mmap: device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001544 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001545 return -ENODEV;
1546 }
1547
1548 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001549 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001550 "open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001551 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552 return -EIO;
1553 }
1554
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001555 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001556 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001557 return -EINVAL;
1558 }
1559
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001560 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1561 size = PAGE_ALIGN(dev->frame[0].buf.length);
1562
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001563 for (i = 0; i < dev->num_frames; i++) {
1564 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1565 break;
1566 }
1567 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001568 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001569 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570 return -EINVAL;
1571 }
1572
1573 /* VM_IO is eventually going to replace PageReserved altogether */
1574 vma->vm_flags |= VM_IO;
1575 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1576
Sascha Sommer3639c862005-12-12 00:37:30 -08001577 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001578 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -08001579 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1580 em28xx_videodbg("mmap: vm_insert_page failed\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001581 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001582 return -EAGAIN;
1583 }
1584 start += PAGE_SIZE;
1585 pos += PAGE_SIZE;
1586 size -= PAGE_SIZE;
1587 }
1588
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001589 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590 vma->vm_private_data = &dev->frame[i];
1591
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001592 em28xx_vm_open(vma);
Sascha Sommer5a804152007-11-03 21:22:38 -03001593 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001594 return 0;
1595}
1596
Arjan van de Venfa027c22007-02-12 00:55:33 -08001597static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001598 .owner = THIS_MODULE,
1599 .open = em28xx_v4l2_open,
1600 .release = em28xx_v4l2_close,
1601 .read = em28xx_v4l2_read,
1602 .poll = em28xx_v4l2_poll,
1603 .mmap = em28xx_v4l2_mmap,
1604 .ioctl = video_ioctl2,
1605 .llseek = no_llseek,
1606 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001607};
1608
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001609static const struct video_device em28xx_video_template = {
1610 .fops = &em28xx_v4l_fops,
1611 .release = video_device_release,
1612
1613 .minor = -1,
1614 .vidioc_querycap = vidioc_querycap,
1615 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1616 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1617 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1618 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1619 .vidioc_g_audio = vidioc_g_audio,
1620 .vidioc_s_audio = vidioc_s_audio,
1621 .vidioc_cropcap = vidioc_cropcap,
1622
1623 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1624 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1625 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1626
1627 .vidioc_reqbufs = vidioc_reqbufs,
1628 .vidioc_querybuf = vidioc_querybuf,
1629 .vidioc_qbuf = vidioc_qbuf,
1630 .vidioc_dqbuf = vidioc_dqbuf,
1631 .vidioc_s_std = vidioc_s_std,
1632 .vidioc_enum_input = vidioc_enum_input,
1633 .vidioc_g_input = vidioc_g_input,
1634 .vidioc_s_input = vidioc_s_input,
1635 .vidioc_queryctrl = vidioc_queryctrl,
1636 .vidioc_g_ctrl = vidioc_g_ctrl,
1637 .vidioc_s_ctrl = vidioc_s_ctrl,
1638 .vidioc_streamon = vidioc_streamon,
1639 .vidioc_streamoff = vidioc_streamoff,
1640 .vidioc_g_tuner = vidioc_g_tuner,
1641 .vidioc_s_tuner = vidioc_s_tuner,
1642 .vidioc_g_frequency = vidioc_g_frequency,
1643 .vidioc_s_frequency = vidioc_s_frequency,
1644
1645 .tvnorms = V4L2_STD_ALL,
1646 .current_norm = V4L2_STD_NTSC_M,
1647};
1648
1649
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001650/******************************** usb interface *****************************************/
1651
1652/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001653 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001654 * allocates and inits the device structs, registers i2c bus and v4l device
1655 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001656static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001657 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001659 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001660 int retval = -ENOMEM;
1661 int errCode, i;
1662 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001663
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001665 mutex_init(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001666 spin_lock_init(&dev->queue_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001667 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001668 init_waitqueue_head(&dev->wait_frame);
1669 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001670
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001671 dev->em28xx_write_regs = em28xx_write_regs;
1672 dev->em28xx_read_reg = em28xx_read_reg;
1673 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1674 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1675 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001676 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001677
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001678 em28xx_pre_card_setup(dev);
1679
1680 errCode = em28xx_config(dev);
1681 if (errCode) {
1682 em28xx_errdev("error configuring device\n");
1683 em28xx_devused &= ~(1<<dev->devno);
1684 kfree(dev);
1685 return -ENOMEM;
1686 }
1687
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001688 /* register i2c bus */
1689 em28xx_i2c_register(dev);
1690
1691 /* Do board specific init and eeprom reading */
1692 em28xx_card_setup(dev);
1693
1694 /* configure the device */
1695 em28xx_config_i2c(dev);
1696
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001697 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001698 if (em28xx_boards[dev->model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001699 break;
1700 if (i == TVNORMS)
1701 i = 0;
1702
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001703 dev->tvnorm = &tvnorms[i]; /* set default norm */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001704
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001705 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001706
1707 maxw = norm_maxw(dev);
1708 maxh = norm_maxh(dev);
1709
1710 /* set default image size */
1711 dev->width = maxw;
1712 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001713 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001714 dev->field_size = dev->width * dev->height;
1715 dev->frame_size =
1716 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1717 dev->bytesperline = dev->width * 2;
1718 dev->hscale = 0;
1719 dev->vscale = 0;
1720 dev->ctl_input = 2;
1721
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001722 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001723
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001724 /* allocate and fill video video_device struct */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001725 dev->vdev = video_device_alloc();
1726 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001727 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001728 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001729 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001730 return -ENOMEM;
1731 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001732 memcpy(dev->vdev, &em28xx_video_template,
1733 sizeof(em28xx_video_template));
1734 dev->vdev->type = VID_TYPE_CAPTURE;
1735 if (dev->has_tuner)
1736 dev->vdev->type |= VID_TYPE_TUNER;
1737 dev->vdev->dev = &dev->udev->dev;
1738 snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name),
1739 "%s#%d %s", "em28xx", dev->devno, "video");
1740 dev->vdev->current_norm = dev->tvnorm->id;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001741
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001742 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001743 dev->vbi_dev = video_device_alloc();
1744 if (NULL == dev->vbi_dev) {
1745 em28xx_errdev("cannot allocate video_device.\n");
1746 kfree(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001747 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001748 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001749 return -ENOMEM;
1750 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001751 memcpy(dev->vbi_dev, &em28xx_video_template,
1752 sizeof(em28xx_video_template));
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001753 dev->vbi_dev->type = VFL_TYPE_VBI;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001754 dev->vbi_dev->dev = &dev->udev->dev;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001755 snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name),
1756 "%s#%d %s", "em28xx", dev->devno, "vbi");
1757 dev->vbi_dev->current_norm = dev->tvnorm->id;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001758
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001759 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001760
Sascha Sommer5a804152007-11-03 21:22:38 -03001761 if (dev->has_msp34xx) {
1762 /* Send a reset to other chips via gpio */
1763 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1764 msleep(3);
1765 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1766 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03001767 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001768
Sascha Sommer5a804152007-11-03 21:22:38 -03001769 video_mux(dev, 0);
1770
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001771 /* register v4l2 video video_device */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001772 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1773 video_nr[dev->devno]))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001774 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001775 retval);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001776 mutex_unlock(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001777 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001778 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001779 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001780 kfree(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781 return -ENODEV;
1782 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001783
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001784 /* register v4l2 vbi video_device */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001785 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1786 vbi_nr[dev->devno]) < 0) {
1787 printk("unable to register vbi device\n");
Ingo Molnar3593cab2006-02-07 06:49:14 -02001788 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001789 list_del(&dev->devlist);
1790 video_device_release(dev->vbi_dev);
1791 video_device_release(dev->vdev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001792 em28xx_devused&=~(1<<dev->devno);
Eric Sesterhenn7c908fb2006-04-11 18:19:33 -03001793 kfree(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001794 return -ENODEV;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001795 }
1796
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001797 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1798 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1799 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001800
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001801 return 0;
1802}
1803
1804/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001805 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806 * checks for supported devices
1807 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001808static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001809 const struct usb_device_id *id)
1810{
1811 const struct usb_endpoint_descriptor *endpoint;
1812 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001813 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001814 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001815 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001816 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001817
1818 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001819 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1820
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001821 /* Check to see next free device and mark as used */
1822 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
1823 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001824
1825 /* Don't register audio interfaces */
1826 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001827 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001828 udev->descriptor.idVendor,udev->descriptor.idProduct,
1829 ifnum,
1830 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001831
1832 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001833 return -ENODEV;
1834 }
1835
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001836 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001837 udev->descriptor.idVendor,udev->descriptor.idProduct,
1838 ifnum,
1839 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001840
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001841 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1842
Michael Opdenacker59c51592007-05-09 08:57:56 +02001843 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001844 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1845 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001846 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001847 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001848 return -ENODEV;
1849 }
1850 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001851 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001852 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001853 return -ENODEV;
1854 }
1855
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03001856 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001857 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001858 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001859 return -ENOMEM;
1860 }
1861
1862 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001863 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001864 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001865 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001866 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001867 return -ENOMEM;
1868 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001869
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001870 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001871 dev->devno = nr;
1872 dev->model = id->driver_info;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001873
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001874 /* compute alternate max packet sizes */
1875 uif = udev->actconfig->interface[0];
1876
1877 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001878 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001879// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1880 dev->alt_max_pkt_size = kmalloc(32*
1881 dev->num_alt,GFP_KERNEL);
1882 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001883 em28xx_errdev("out of memory!\n");
1884 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02001885 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001886 return -ENOMEM;
1887 }
1888
1889 for (i = 0; i < dev->num_alt ; i++) {
1890 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1891 wMaxPacketSize);
1892 dev->alt_max_pkt_size[i] =
1893 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001894 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001895 dev->alt_max_pkt_size[i]);
1896 }
1897
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001898 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001899 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001900
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001901 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001902 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001903 if (retval)
1904 return retval;
1905
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001906 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001907
1908 /* save our data pointer in this interface device */
1909 usb_set_intfdata(interface, dev);
1910 return 0;
1911}
1912
1913/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001914 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001915 * called when the device gets diconencted
1916 * video device will be unregistered on v4l2_close in case it is still open
1917 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001918static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919{
Sascha Sommer5a804152007-11-03 21:22:38 -03001920 struct em28xx *dev;
1921
1922 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001923 usb_set_intfdata(interface, NULL);
1924
1925 if (!dev)
1926 return;
1927
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001928 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001929
Sascha Sommer5a804152007-11-03 21:22:38 -03001930 /* wait until all current v4l2 io is finished then deallocate resources */
1931 mutex_lock(&dev->lock);
1932
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001933 wake_up_interruptible_all(&dev->open);
1934
1935 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001936 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001937 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001938 "deallocation are deferred on close.\n",
1939 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
1940
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001941 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001942 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001943 dev->state |= DEV_DISCONNECTED;
1944 wake_up_interruptible(&dev->wait_frame);
1945 wake_up_interruptible(&dev->wait_stream);
1946 } else {
1947 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001948 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001949 }
1950
Sascha Sommer5a804152007-11-03 21:22:38 -03001951
Ingo Molnar3593cab2006-02-07 06:49:14 -02001952 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001953
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001954 if (!dev->users) {
1955 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001956 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001957 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001958
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001959}
1960
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001961static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001962 .name = "em28xx",
1963 .probe = em28xx_usb_probe,
1964 .disconnect = em28xx_usb_disconnect,
1965 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001966};
1967
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001968static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001969{
1970 int result;
1971
1972 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001973 (EM28XX_VERSION_CODE >> 16) & 0xff,
1974 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001975#ifdef SNAPSHOT
1976 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1977 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1978#endif
1979
1980 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001981 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001982 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001983 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001984 " usb_register failed. Error number %d.\n", result);
1985
1986 return result;
1987}
1988
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001989static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001990{
1991 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001992 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001993}
1994
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001995module_init(em28xx_module_init);
1996module_exit(em28xx_module_exit);