blob: de95ca874241e0b39115244e47ef9dd8a02de1fe [file] [log] [blame]
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001/*
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08002 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08003
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08004 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 Mauro Carvalho Chehab <mchehab@infradead.org>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
Mauro Carvalho Chehab439090d2006-01-23 17:10:54 -02009 Some parts based on SN9C10x PC Camera Controllers GPL driver made
10 by Luca Risolia <luca.risolia@studio.unibo.it>
11
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080012 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27#include <linux/init.h>
28#include <linux/list.h>
29#include <linux/module.h>
30#include <linux/kernel.h>
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020031#include <linux/bitmap.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080032#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080034#include <linux/version.h>
Trent Piepho6d35c8f2007-11-01 01:16:09 -030035#include <linux/mm.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020036#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#include "em28xx.h"
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020039#include <media/v4l2-common.h>
Hans Verkuil2474ed42006-03-19 12:35:57 -030040#include <media/msp3400.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080041
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080042#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
43 "Markus Rechberger <mrechberger@gmail.com>, " \
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -030044 "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080045 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080047#define DRIVER_NAME "em28xx"
48#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -030049#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080050
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080051#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080052 if (video_debug) \
53 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020054 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080055
56MODULE_AUTHOR(DRIVER_AUTHOR);
57MODULE_DESCRIPTION(DRIVER_DESC);
58MODULE_LICENSE("GPL");
59
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080060static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080061
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080062static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020063static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030064static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
65static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
66
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080067module_param_array(card, int, NULL, 0444);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020068module_param_array(video_nr, int, NULL, 0444);
69module_param_array(vbi_nr, int, NULL, 0444);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -030070module_param_array(radio_nr, int, NULL, 0444);
71MODULE_PARM_DESC(card, "card type");
72MODULE_PARM_DESC(video_nr, "video device numbers");
73MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
74MODULE_PARM_DESC(radio_nr, "radio device numbers");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080075
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080076static unsigned int video_debug = 0;
77module_param(video_debug,int,0644);
78MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
79
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -020080/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
81static unsigned long em28xx_devused;
82
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080083/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020084/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080085static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080086 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020087 .id = V4L2_CID_AUDIO_VOLUME,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Volume",
90 .minimum = 0x0,
91 .maximum = 0x1f,
92 .step = 0x1,
93 .default_value = 0x1f,
94 .flags = 0,
95 },{
96 .id = V4L2_CID_AUDIO_MUTE,
97 .type = V4L2_CTRL_TYPE_BOOLEAN,
98 .name = "Mute",
99 .minimum = 0,
100 .maximum = 1,
101 .step = 1,
102 .default_value = 1,
103 .flags = 0,
104 }
105};
106
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800107static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800108
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800109
110/********************* v4l2 interface ******************************************/
111
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800112/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800113 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800114 * inits registers with sane defaults
115 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800116static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800117{
118
119 /* Sets I2C speed to 100 KHz */
Sascha Sommer2b2c93a2007-11-03 16:48:01 -0300120 if (!dev->is_em2800)
121 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800122
123 /* enable vbi capturing */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200124
Markus Rechberger9475fb12006-02-27 00:07:34 -0300125/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */
126/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -0200127 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1);
128
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800129 dev->mute = 1; /* maybe not the right place... */
130 dev->volume = 0x1f;
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300131
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800132 em28xx_outfmt_set_yuv422(dev);
133 em28xx_colorlevels_set_default(dev);
134 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800135
136 return 0;
137}
138
139/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800140 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800141 * configure i2c attached devices
142 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800143static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800144{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300145 struct v4l2_routing route;
146
147 route.input = INPUT(dev->ctl_input)->vmux;
148 route.output = 0;
Al Viro663d1ba2006-10-10 22:48:37 +0100149 em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300150 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabf5762e42006-03-13 13:31:31 -0300151 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800152}
153
154/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800155 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800156 * prepare queues for incoming and outgoing frames
157 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800158static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800159{
160 u32 i;
161
162 INIT_LIST_HEAD(&dev->inqueue);
163 INIT_LIST_HEAD(&dev->outqueue);
164
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800165 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800166 dev->frame[i].state = F_UNUSED;
167 dev->frame[i].buf.bytesused = 0;
168 }
169}
170
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800171static void video_mux(struct em28xx *dev, int index)
172{
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300173 struct v4l2_routing route;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800174
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300175 route.input = INPUT(index)->vmux;
176 route.output = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800177 dev->ctl_input = index;
178 dev->ctl_ainput = INPUT(index)->amux;
179
Hans Verkuilc7c0b342006-04-02 13:35:00 -0300180 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800181
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800182 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200183 if (dev->i2s_speed)
184 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300185 route.input = dev->ctl_ainput;
Hans Verkuil07151722006-04-01 18:03:23 -0300186 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300187 /* Note: this is msp3400 specific */
188 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800189 }
Mauro Carvalho Chehab539c96d2008-01-05 09:53:54 -0300190
191 em28xx_set_audio_source(dev);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800192}
193
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -0300194/* Usage lock check functions */
195static int res_get(struct em28xx_fh *fh)
196{
197 struct em28xx *dev = fh->dev;
198 int rc = 0;
199
200 /* This instance already has stream_on */
201 if (fh->stream_on)
202 return rc;
203
204 mutex_lock(&dev->lock);
205
206 if (dev->stream_on)
207 rc = -EINVAL;
208 else {
209 dev->stream_on = 1;
210 fh->stream_on = 1;
211 }
212
213 mutex_unlock(&dev->lock);
214 return rc;
215}
216
217static int res_check(struct em28xx_fh *fh)
218{
219 return (fh->stream_on);
220}
221
222static void res_free(struct em28xx_fh *fh)
223{
224 struct em28xx *dev = fh->dev;
225
226 mutex_lock(&dev->lock);
227 fh->stream_on = 0;
228 dev->stream_on = 0;
229 mutex_unlock(&dev->lock);
230}
231
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800232/*
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300233 * em28xx_vm_open()
234 */
235static void em28xx_vm_open(struct vm_area_struct *vma)
236{
237 struct em28xx_frame_t *f = vma->vm_private_data;
238 f->vma_use_count++;
239}
240
241/*
242 * em28xx_vm_close()
243 */
244static void em28xx_vm_close(struct vm_area_struct *vma)
245{
246 /* NOTE: buffers are not freed here */
247 struct em28xx_frame_t *f = vma->vm_private_data;
248
249 if (f->vma_use_count)
250 f->vma_use_count--;
251}
252
253static struct vm_operations_struct em28xx_vm_ops = {
254 .open = em28xx_vm_open,
255 .close = em28xx_vm_close,
256};
257
258
259/*
260 * em28xx_get_ctrl()
261 * return the current saturation, brightness or contrast, mute state
262 */
263static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
264{
265 switch (ctrl->id) {
266 case V4L2_CID_AUDIO_MUTE:
267 ctrl->value = dev->mute;
268 return 0;
269 case V4L2_CID_AUDIO_VOLUME:
270 ctrl->value = dev->volume;
271 return 0;
272 default:
273 return -EINVAL;
274 }
275}
276
277/*
278 * em28xx_set_ctrl()
279 * mute or set new saturation, brightness or contrast
280 */
281static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
282{
283 switch (ctrl->id) {
284 case V4L2_CID_AUDIO_MUTE:
285 if (ctrl->value != dev->mute) {
286 dev->mute = ctrl->value;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300287 return em28xx_audio_analog_set(dev);
288 }
289 return 0;
290 case V4L2_CID_AUDIO_VOLUME:
291 dev->volume = ctrl->value;
292 return em28xx_audio_analog_set(dev);
293 default:
294 return -EINVAL;
295 }
296}
297
298/*
299 * em28xx_stream_interrupt()
300 * stops streaming
301 */
302static int em28xx_stream_interrupt(struct em28xx *dev)
303{
304 int rc = 0;
305
306 /* stop reading from the device */
307
308 dev->stream = STREAM_INTERRUPT;
309 rc = wait_event_timeout(dev->wait_stream,
310 (dev->stream == STREAM_OFF) ||
311 (dev->state & DEV_DISCONNECTED),
312 EM28XX_URB_TIMEOUT);
313
314 if (rc) {
315 dev->state |= DEV_MISCONFIGURED;
316 em28xx_videodbg("device is misconfigured; close and "
317 "open /dev/video%d again\n",
318 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
319 return rc;
320 }
321
322 return 0;
323}
324
325
326static int check_dev(struct em28xx *dev)
327{
328 if (dev->state & DEV_DISCONNECTED) {
329 em28xx_errdev("v4l2 ioctl: device not present\n");
330 return -ENODEV;
331 }
332
333 if (dev->state & DEV_MISCONFIGURED) {
334 em28xx_errdev("v4l2 ioctl: device is misconfigured; "
335 "close and open it again\n");
336 return -EIO;
337 }
338 return 0;
339}
340
341static void get_scale(struct em28xx *dev,
342 unsigned int width, unsigned int height,
343 unsigned int *hscale, unsigned int *vscale)
344{
345 unsigned int maxw = norm_maxw(dev);
346 unsigned int maxh = norm_maxh(dev);
347
348 *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
349 if (*hscale >= 0x4000)
350 *hscale = 0x3fff;
351
352 *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
353 if (*vscale >= 0x4000)
354 *vscale = 0x3fff;
355}
356
357/* ------------------------------------------------------------------
358 IOCTL vidioc handling
359 ------------------------------------------------------------------*/
360
361static int vidioc_g_fmt_cap(struct file *file, void *priv,
362 struct v4l2_format *f)
363{
364 struct em28xx_fh *fh = priv;
365 struct em28xx *dev = fh->dev;
366
367 mutex_lock(&dev->lock);
368
369 f->fmt.pix.width = dev->width;
370 f->fmt.pix.height = dev->height;
371 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
372 f->fmt.pix.bytesperline = dev->bytesperline;
373 f->fmt.pix.sizeimage = dev->frame_size;
374 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
375
376 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
377 f->fmt.pix.field = dev->interlaced ?
378 V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
379
380 mutex_unlock(&dev->lock);
381 return 0;
382}
383
384static int vidioc_try_fmt_cap(struct file *file, void *priv,
385 struct v4l2_format *f)
386{
387 struct em28xx_fh *fh = priv;
388 struct em28xx *dev = fh->dev;
389 int width = f->fmt.pix.width;
390 int height = f->fmt.pix.height;
391 unsigned int maxw = norm_maxw(dev);
392 unsigned int maxh = norm_maxh(dev);
393 unsigned int hscale, vscale;
394
395 /* width must even because of the YUYV format
396 height must be even because of interlacing */
397 height &= 0xfffe;
398 width &= 0xfffe;
399
400 if (height < 32)
401 height = 32;
402 if (height > maxh)
403 height = maxh;
404 if (width < 48)
405 width = 48;
406 if (width > maxw)
407 width = maxw;
408
409 mutex_lock(&dev->lock);
410
411 if (dev->is_em2800) {
412 /* the em2800 can only scale down to 50% */
413 if (height % (maxh / 2))
414 height = maxh;
415 if (width % (maxw / 2))
416 width = maxw;
417 /* according to empiatech support */
418 /* the MaxPacketSize is to small to support */
419 /* framesizes larger than 640x480 @ 30 fps */
420 /* or 640x576 @ 25 fps. As this would cut */
421 /* of a part of the image we prefer */
422 /* 360x576 or 360x480 for now */
423 if (width == maxw && height == maxh)
424 width /= 2;
425 }
426
427 get_scale(dev, width, height, &hscale, &vscale);
428
429 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
430 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
431
432 f->fmt.pix.width = width;
433 f->fmt.pix.height = height;
434 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
435 f->fmt.pix.bytesperline = width * 2;
436 f->fmt.pix.sizeimage = width * 2 * height;
437 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
438 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
439
440 mutex_unlock(&dev->lock);
441 return 0;
442}
443
444static int vidioc_s_fmt_cap(struct file *file, void *priv,
445 struct v4l2_format *f)
446{
447 struct em28xx_fh *fh = priv;
448 struct em28xx *dev = fh->dev;
449 int rc, i;
450
451 rc = check_dev(dev);
452 if (rc < 0)
453 return rc;
454
455 vidioc_try_fmt_cap(file, priv, f);
456
457 mutex_lock(&dev->lock);
458
459 for (i = 0; i < dev->num_frames; i++)
460 if (dev->frame[i].vma_use_count) {
461 em28xx_videodbg("VIDIOC_S_FMT failed. "
462 "Unmap the buffers first.\n");
463 rc = -EINVAL;
464 goto err;
465 }
466
467 /* stop io in case it is already in progress */
468 if (dev->stream == STREAM_ON) {
469 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
470 rc = em28xx_stream_interrupt(dev);
471 if (rc < 0)
472 goto err;
473 }
474
475 em28xx_release_buffers(dev);
476 dev->io = IO_NONE;
477
478 /* set new image size */
479 dev->width = f->fmt.pix.width;
480 dev->height = f->fmt.pix.height;
481 dev->frame_size = dev->width * dev->height * 2;
482 dev->field_size = dev->frame_size >> 1;
483 dev->bytesperline = dev->width * 2;
484 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
485
486 /* FIXME: This is really weird! Why capture is starting with
487 this ioctl ???
488 */
489 em28xx_uninit_isoc(dev);
490 em28xx_set_alternate(dev);
491 em28xx_capture_start(dev, 1);
492 em28xx_resolution_set(dev);
493 em28xx_init_isoc(dev);
494 rc = 0;
495
496err:
497 mutex_unlock(&dev->lock);
498 return rc;
499}
500
501static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
502{
503 struct em28xx_fh *fh = priv;
504 struct em28xx *dev = fh->dev;
505 struct v4l2_format f;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300506 int rc;
507
508 rc = check_dev(dev);
509 if (rc < 0)
510 return rc;
511
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300512 mutex_lock(&dev->lock);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300513 dev->norm = *norm;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300514 mutex_unlock(&dev->lock);
515
516 /* Adjusts width/height, if needed */
517 f.fmt.pix.width = dev->width;
518 f.fmt.pix.height = dev->height;
519 vidioc_try_fmt_cap(file, priv, &f);
520
521 mutex_lock(&dev->lock);
522
523 /* set new image size */
524 dev->width = f.fmt.pix.width;
525 dev->height = f.fmt.pix.height;
526 dev->frame_size = dev->width * dev->height * 2;
527 dev->field_size = dev->frame_size >> 1;
528 dev->bytesperline = dev->width * 2;
529 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
530
531 em28xx_resolution_set(dev);
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300532 em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300533
534 mutex_unlock(&dev->lock);
535 return 0;
536}
537
538static const char *iname[] = {
539 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
540 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
541 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
542 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
543 [EM28XX_VMUX_SVIDEO] = "S-Video",
544 [EM28XX_VMUX_TELEVISION] = "Television",
545 [EM28XX_VMUX_CABLE] = "Cable TV",
546 [EM28XX_VMUX_DVB] = "DVB",
547 [EM28XX_VMUX_DEBUG] = "for debug only",
548};
549
550static int vidioc_enum_input(struct file *file, void *priv,
551 struct v4l2_input *i)
552{
553 struct em28xx_fh *fh = priv;
554 struct em28xx *dev = fh->dev;
555 unsigned int n;
556
557 n = i->index;
558 if (n >= MAX_EM28XX_INPUT)
559 return -EINVAL;
560 if (0 == INPUT(n)->type)
561 return -EINVAL;
562
563 i->index = n;
564 i->type = V4L2_INPUT_TYPE_CAMERA;
565
566 strcpy(i->name, iname[INPUT(n)->type]);
567
568 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
569 (EM28XX_VMUX_CABLE == INPUT(n)->type))
570 i->type = V4L2_INPUT_TYPE_TUNER;
571
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -0300572 i->std = dev->vdev->tvnorms;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300573
574 return 0;
575}
576
577static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
578{
579 struct em28xx_fh *fh = priv;
580 struct em28xx *dev = fh->dev;
581
582 *i = dev->ctl_input;
583
584 return 0;
585}
586
587static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
588{
589 struct em28xx_fh *fh = priv;
590 struct em28xx *dev = fh->dev;
591 int rc;
592
593 rc = check_dev(dev);
594 if (rc < 0)
595 return rc;
596
597 if (i >= MAX_EM28XX_INPUT)
598 return -EINVAL;
599 if (0 == INPUT(i)->type)
600 return -EINVAL;
601
602 mutex_lock(&dev->lock);
603
604 video_mux(dev, i);
605
606 mutex_unlock(&dev->lock);
607 return 0;
608}
609
610static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
611{
612 struct em28xx_fh *fh = priv;
613 struct em28xx *dev = fh->dev;
614 unsigned int index = a->index;
615
616 if (a->index > 1)
617 return -EINVAL;
618
619 index = dev->ctl_ainput;
620
621 if (index == 0) {
622 strcpy(a->name, "Television");
623 } else {
624 strcpy(a->name, "Line In");
625 }
626 a->capability = V4L2_AUDCAP_STEREO;
627 a->index = index;
628
629 return 0;
630}
631
632static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
633{
634 struct em28xx_fh *fh = priv;
635 struct em28xx *dev = fh->dev;
636
637 if (a->index != dev->ctl_ainput)
638 return -EINVAL;
639
640 return 0;
641}
642
643static int vidioc_queryctrl(struct file *file, void *priv,
644 struct v4l2_queryctrl *qc)
645{
646 struct em28xx_fh *fh = priv;
647 struct em28xx *dev = fh->dev;
648 int id = qc->id;
649 int i;
650 int rc;
651
652 rc = check_dev(dev);
653 if (rc < 0)
654 return rc;
655
656 memset(qc, 0, sizeof(*qc));
657
658 qc->id = id;
659
660 if (!dev->has_msp34xx) {
661 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
662 if (qc->id && qc->id == em28xx_qctrl[i].id) {
663 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
664 return 0;
665 }
666 }
667 }
668 mutex_lock(&dev->lock);
669 em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
670 mutex_unlock(&dev->lock);
671
672 if (qc->type)
673 return 0;
674 else
675 return -EINVAL;
676}
677
678static int vidioc_g_ctrl(struct file *file, void *priv,
679 struct v4l2_control *ctrl)
680{
681 struct em28xx_fh *fh = priv;
682 struct em28xx *dev = fh->dev;
683 int rc;
684
685 rc = check_dev(dev);
686 if (rc < 0)
687 return rc;
688 mutex_lock(&dev->lock);
689
690 if (!dev->has_msp34xx)
691 rc = em28xx_get_ctrl(dev, ctrl);
692 else
693 rc = -EINVAL;
694
695 if (rc == -EINVAL) {
696 em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
697 rc = 0;
698 }
699
700 mutex_unlock(&dev->lock);
701 return rc;
702}
703
704static int vidioc_s_ctrl(struct file *file, void *priv,
705 struct v4l2_control *ctrl)
706{
707 struct em28xx_fh *fh = priv;
708 struct em28xx *dev = fh->dev;
709 u8 i;
710 int rc;
711
712 rc = check_dev(dev);
713 if (rc < 0)
714 return rc;
715
716 mutex_lock(&dev->lock);
717
718 if (dev->has_msp34xx)
719 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
720 else {
721 rc = 1;
722 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
723 if (ctrl->id == em28xx_qctrl[i].id) {
724 if (ctrl->value < em28xx_qctrl[i].minimum ||
725 ctrl->value > em28xx_qctrl[i].maximum) {
726 rc = -ERANGE;
727 break;
728 }
729
730 rc = em28xx_set_ctrl(dev, ctrl);
731 break;
732 }
733 }
734 }
735
736 /* Control not found - try to send it to the attached devices */
737 if (rc == 1) {
738 em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
739 rc = 0;
740 }
741
742 mutex_unlock(&dev->lock);
743 return rc;
744}
745
746static int vidioc_g_tuner(struct file *file, void *priv,
747 struct v4l2_tuner *t)
748{
749 struct em28xx_fh *fh = priv;
750 struct em28xx *dev = fh->dev;
751 int rc;
752
753 rc = check_dev(dev);
754 if (rc < 0)
755 return rc;
756
757 if (0 != t->index)
758 return -EINVAL;
759
760 strcpy(t->name, "Tuner");
761
762 mutex_lock(&dev->lock);
763
764 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
765
766 mutex_unlock(&dev->lock);
767 return 0;
768}
769
770static int vidioc_s_tuner(struct file *file, void *priv,
771 struct v4l2_tuner *t)
772{
773 struct em28xx_fh *fh = priv;
774 struct em28xx *dev = fh->dev;
775 int rc;
776
777 rc = check_dev(dev);
778 if (rc < 0)
779 return rc;
780
781 if (0 != t->index)
782 return -EINVAL;
783
784 mutex_lock(&dev->lock);
785
786 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
787
788 mutex_unlock(&dev->lock);
789 return 0;
790}
791
792static int vidioc_g_frequency(struct file *file, void *priv,
793 struct v4l2_frequency *f)
794{
795 struct em28xx_fh *fh = priv;
796 struct em28xx *dev = fh->dev;
797
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -0300798 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300799 f->frequency = dev->ctl_freq;
800
801 return 0;
802}
803
804static int vidioc_s_frequency(struct file *file, void *priv,
805 struct v4l2_frequency *f)
806{
807 struct em28xx_fh *fh = priv;
808 struct em28xx *dev = fh->dev;
809 int rc;
810
811 rc = check_dev(dev);
812 if (rc < 0)
813 return rc;
814
815 if (0 != f->tuner)
816 return -EINVAL;
817
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -0300818 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
819 return -EINVAL;
820 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -0300821 return -EINVAL;
822
823 mutex_lock(&dev->lock);
824
825 dev->ctl_freq = f->frequency;
826 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
827
828 mutex_unlock(&dev->lock);
829 return 0;
830}
831
832static int vidioc_cropcap(struct file *file, void *priv,
833 struct v4l2_cropcap *cc)
834{
835 struct em28xx_fh *fh = priv;
836 struct em28xx *dev = fh->dev;
837
838 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
839 return -EINVAL;
840
841 cc->bounds.left = 0;
842 cc->bounds.top = 0;
843 cc->bounds.width = dev->width;
844 cc->bounds.height = dev->height;
845 cc->defrect = cc->bounds;
846 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
847 cc->pixelaspect.denominator = 59;
848
849 return 0;
850}
851
852static int vidioc_streamon(struct file *file, void *priv,
853 enum v4l2_buf_type type)
854{
855 struct em28xx_fh *fh = priv;
856 struct em28xx *dev = fh->dev;
857 int rc;
858
859 rc = check_dev(dev);
860 if (rc < 0)
861 return rc;
862
863 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
864 return -EINVAL;
865
866 if (list_empty(&dev->inqueue))
867 return -EINVAL;
868
869 mutex_lock(&dev->lock);
870
871 if (unlikely(res_get(fh) < 0)) {
872 mutex_unlock(&dev->lock);
873 return -EBUSY;
874 }
875
876 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
877
878 mutex_unlock(&dev->lock);
879 return 0;
880}
881
882static int vidioc_streamoff(struct file *file, void *priv,
883 enum v4l2_buf_type type)
884{
885 struct em28xx_fh *fh = priv;
886 struct em28xx *dev = fh->dev;
887 int rc;
888
889 rc = check_dev(dev);
890 if (rc < 0)
891 return rc;
892
893 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
894 return -EINVAL;
895
896 mutex_lock(&dev->lock);
897
898 if (dev->stream == STREAM_ON) {
899 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
900 rc = em28xx_stream_interrupt(dev);
901 if (rc < 0) {
902 mutex_unlock(&dev->lock);
903 return rc;
904 }
905 }
906
907 em28xx_empty_framequeues(dev);
908
909 mutex_unlock(&dev->lock);
910 return 0;
911}
912
913static int vidioc_querycap(struct file *file, void *priv,
914 struct v4l2_capability *cap)
915{
916 struct em28xx_fh *fh = priv;
917 struct em28xx *dev = fh->dev;
918
919 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
920 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
921 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
922
923 cap->version = EM28XX_VERSION_CODE;
924
925 cap->capabilities =
926 V4L2_CAP_SLICED_VBI_CAPTURE |
927 V4L2_CAP_VIDEO_CAPTURE |
928 V4L2_CAP_AUDIO |
929 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
930
931 if (dev->has_tuner)
932 cap->capabilities |= V4L2_CAP_TUNER;
933
934 return 0;
935}
936
937static int vidioc_enum_fmt_cap(struct file *file, void *priv,
938 struct v4l2_fmtdesc *fmtd)
939{
940 if (fmtd->index != 0)
941 return -EINVAL;
942
943 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
944 strcpy(fmtd->description, "Packed YUY2");
945 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
946 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
947
948 return 0;
949}
950
951/* Sliced VBI ioctls */
952static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv,
953 struct v4l2_format *f)
954{
955 struct em28xx_fh *fh = priv;
956 struct em28xx *dev = fh->dev;
957 int rc;
958
959 rc = check_dev(dev);
960 if (rc < 0)
961 return rc;
962
963 mutex_lock(&dev->lock);
964
965 f->fmt.sliced.service_set = 0;
966
967 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
968
969 if (f->fmt.sliced.service_set == 0)
970 rc = -EINVAL;
971
972 mutex_unlock(&dev->lock);
973 return rc;
974}
975
976static int vidioc_try_set_vbi_capture(struct file *file, void *priv,
977 struct v4l2_format *f)
978{
979 struct em28xx_fh *fh = priv;
980 struct em28xx *dev = fh->dev;
981 int rc;
982
983 rc = check_dev(dev);
984 if (rc < 0)
985 return rc;
986
987 mutex_lock(&dev->lock);
988 em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
989 mutex_unlock(&dev->lock);
990
991 if (f->fmt.sliced.service_set == 0)
992 return -EINVAL;
993
994 return 0;
995}
996
997
998static int vidioc_reqbufs(struct file *file, void *priv,
999 struct v4l2_requestbuffers *rb)
1000{
1001 struct em28xx_fh *fh = priv;
1002 struct em28xx *dev = fh->dev;
1003 u32 i;
1004 int rc;
1005
1006 rc = check_dev(dev);
1007 if (rc < 0)
1008 return rc;
1009
1010 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1011 rb->memory != V4L2_MEMORY_MMAP)
1012 return -EINVAL;
1013
1014 if (dev->io == IO_READ) {
1015 em28xx_videodbg("method is set to read;"
1016 " close and open the device again to"
1017 " choose the mmap I/O method\n");
1018 return -EINVAL;
1019 }
1020
1021 for (i = 0; i < dev->num_frames; i++)
1022 if (dev->frame[i].vma_use_count) {
1023 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1024 "previous buffers are still mapped\n");
1025 return -EINVAL;
1026 }
1027
1028 mutex_lock(&dev->lock);
1029
1030 if (dev->stream == STREAM_ON) {
1031 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1032 rc = em28xx_stream_interrupt(dev);
1033 if (rc < 0) {
1034 mutex_unlock(&dev->lock);
1035 return rc;
1036 }
1037 }
1038
1039 em28xx_empty_framequeues(dev);
1040
1041 em28xx_release_buffers(dev);
1042 if (rb->count)
1043 rb->count = em28xx_request_buffers(dev, rb->count);
1044
1045 dev->frame_current = NULL;
1046 dev->io = rb->count ? IO_MMAP : IO_NONE;
1047
1048 mutex_unlock(&dev->lock);
1049 return 0;
1050}
1051
1052static int vidioc_querybuf(struct file *file, void *priv,
1053 struct v4l2_buffer *b)
1054{
1055 struct em28xx_fh *fh = priv;
1056 struct em28xx *dev = fh->dev;
1057 int rc;
1058
1059 rc = check_dev(dev);
1060 if (rc < 0)
1061 return rc;
1062
1063 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1064 b->index >= dev->num_frames || dev->io != IO_MMAP)
1065 return -EINVAL;
1066
1067 mutex_lock(&dev->lock);
1068
1069 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1070
1071 if (dev->frame[b->index].vma_use_count)
1072 b->flags |= V4L2_BUF_FLAG_MAPPED;
1073
1074 if (dev->frame[b->index].state == F_DONE)
1075 b->flags |= V4L2_BUF_FLAG_DONE;
1076 else if (dev->frame[b->index].state != F_UNUSED)
1077 b->flags |= V4L2_BUF_FLAG_QUEUED;
1078
1079 mutex_unlock(&dev->lock);
1080 return 0;
1081}
1082
1083static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1084{
1085 struct em28xx_fh *fh = priv;
1086 struct em28xx *dev = fh->dev;
1087 unsigned long lock_flags;
1088 int rc;
1089
1090 rc = check_dev(dev);
1091 if (rc < 0)
1092 return rc;
1093
1094 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP ||
1095 b->index >= dev->num_frames)
1096 return -EINVAL;
1097
1098 if (dev->frame[b->index].state != F_UNUSED)
1099 return -EAGAIN;
1100
1101 dev->frame[b->index].state = F_QUEUED;
1102
1103 /* add frame to fifo */
1104 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1105 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1106 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1107
1108 return 0;
1109}
1110
1111static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1112{
1113 struct em28xx_fh *fh = priv;
1114 struct em28xx *dev = fh->dev;
1115 int rc;
1116 struct em28xx_frame_t *f;
1117 unsigned long lock_flags;
1118
1119 rc = check_dev(dev);
1120 if (rc < 0)
1121 return rc;
1122
1123 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
1124 return -EINVAL;
1125
1126 if (list_empty(&dev->outqueue)) {
1127 if (dev->stream == STREAM_OFF)
1128 return -EINVAL;
1129
1130 if (file->f_flags & O_NONBLOCK)
1131 return -EAGAIN;
1132
1133 rc = wait_event_interruptible(dev->wait_frame,
1134 (!list_empty(&dev->outqueue)) ||
1135 (dev->state & DEV_DISCONNECTED));
1136 if (rc)
1137 return rc;
1138
1139 if (dev->state & DEV_DISCONNECTED)
1140 return -ENODEV;
1141 }
1142
1143 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1144 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1145 list_del(dev->outqueue.next);
1146 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1147
1148 f->state = F_UNUSED;
1149 memcpy(b, &f->buf, sizeof(*b));
1150
1151 if (f->vma_use_count)
1152 b->flags |= V4L2_BUF_FLAG_MAPPED;
1153
1154 return 0;
1155}
1156
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001157/* ----------------------------------------------------------- */
1158/* RADIO ESPECIFIC IOCTLS */
1159/* ----------------------------------------------------------- */
1160
1161static int radio_querycap(struct file *file, void *priv,
1162 struct v4l2_capability *cap)
1163{
1164 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1165
1166 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1167 strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
1168 strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
1169
1170 cap->version = EM28XX_VERSION_CODE;
1171 cap->capabilities = V4L2_CAP_TUNER;
1172 return 0;
1173}
1174
1175static int radio_g_tuner(struct file *file, void *priv,
1176 struct v4l2_tuner *t)
1177{
1178 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1179
1180 if (unlikely(t->index > 0))
1181 return -EINVAL;
1182
1183 strcpy(t->name, "Radio");
1184 t->type = V4L2_TUNER_RADIO;
1185
1186 em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
1187 return 0;
1188}
1189
1190static int radio_enum_input(struct file *file, void *priv,
1191 struct v4l2_input *i)
1192{
1193 if (i->index != 0)
1194 return -EINVAL;
1195 strcpy(i->name, "Radio");
1196 i->type = V4L2_INPUT_TYPE_TUNER;
1197
1198 return 0;
1199}
1200
1201static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1202{
1203 if (unlikely(a->index))
1204 return -EINVAL;
1205
1206 strcpy(a->name, "Radio");
1207 return 0;
1208}
1209
1210static int radio_s_tuner(struct file *file, void *priv,
1211 struct v4l2_tuner *t)
1212{
1213 struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
1214
1215 if (0 != t->index)
1216 return -EINVAL;
1217
1218 em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
1219
1220 return 0;
1221}
1222
1223static int radio_s_audio(struct file *file, void *fh,
1224 struct v4l2_audio *a)
1225{
1226 return 0;
1227}
1228
1229static int radio_s_input(struct file *file, void *fh, unsigned int i)
1230{
1231 return 0;
1232}
1233
1234static int radio_queryctrl(struct file *file, void *priv,
1235 struct v4l2_queryctrl *qc)
1236{
1237 int i;
1238
1239 if (qc->id < V4L2_CID_BASE ||
1240 qc->id >= V4L2_CID_LASTP1)
1241 return -EINVAL;
1242
1243 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1244 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1245 memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
1246 return 0;
1247 }
1248 }
1249
1250 return -EINVAL;
1251}
1252
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001253/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001254 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001255 * inits the device and starts isoc transfer
1256 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001257static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001258{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001259 int minor = iminor(inode);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001260 int errCode = 0, radio = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001261 struct em28xx *h,*dev = NULL;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001262 struct em28xx_fh *fh;
Markus Rechberger9c755412005-11-08 21:37:52 -08001263
Trent Piephoa991f442007-10-10 05:37:43 -03001264 list_for_each_entry(h, &em28xx_devlist, devlist) {
Markus Rechberger9c755412005-11-08 21:37:52 -08001265 if (h->vdev->minor == minor) {
1266 dev = h;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001267 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1268 }
1269 if (h->vbi_dev->minor == minor) {
1270 dev = h;
1271 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE;
Markus Rechberger9c755412005-11-08 21:37:52 -08001272 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001273 if (h->radio_dev &&
1274 h->radio_dev->minor == minor) {
1275 radio = 1;
1276 dev = h;
1277 }
Markus Rechberger9c755412005-11-08 21:37:52 -08001278 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001279 if (NULL == dev)
1280 return -ENODEV;
Markus Rechberger9c755412005-11-08 21:37:52 -08001281
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001282 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1283 minor,v4l2_type_names[dev->type],dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001284
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001285 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001286
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001287 if (!fh) {
1288 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1289 return -ENOMEM;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001290 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001291 mutex_lock(&dev->lock);
1292 fh->dev = dev;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001293 fh->radio = radio;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001294 filp->private_data = fh;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001295
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001296 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001297 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001298
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001299 dev->width = norm_maxw(dev);
1300 dev->height = norm_maxh(dev);
1301 dev->frame_size = dev->width * dev->height * 2;
1302 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1303 dev->bytesperline = dev->width * 2;
1304 dev->hscale = 0;
1305 dev->vscale = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001306
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001307 em28xx_capture_start(dev, 1);
1308 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001309
Markus Rechbergere5d4a562006-02-07 06:49:13 -02001310
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001311 /* start the transfer */
1312 errCode = em28xx_init_isoc(dev);
1313 if (errCode)
1314 goto err;
1315
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001316 em28xx_empty_framequeues(dev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001317 }
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001318 if (fh->radio) {
1319 em28xx_videodbg("video_open: setting radio device\n");
1320 em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
1321 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001322
1323 dev->users++;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001324
Ingo Molnar3593cab2006-02-07 06:49:14 -02001325err:
1326 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001327 return errCode;
1328}
1329
1330/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001331 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001332 * unregisters the v4l2,i2c and usb devices
1333 * called when the device gets disconected or at module unload
1334*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001335static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001336{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001337
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001338 /*FIXME: I2C IR should be disconnected */
1339
1340 em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
1341 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1342 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
Markus Rechberger9c755412005-11-08 21:37:52 -08001343 list_del(&dev->devlist);
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001344 if (dev->radio_dev) {
1345 if (-1 != dev->radio_dev->minor)
1346 video_unregister_device(dev->radio_dev);
1347 else
1348 video_device_release(dev->radio_dev);
1349 dev->radio_dev = NULL;
1350 }
1351 if (dev->vbi_dev) {
1352 if (-1 != dev->vbi_dev->minor)
1353 video_unregister_device(dev->vbi_dev);
1354 else
1355 video_device_release(dev->vbi_dev);
1356 dev->vbi_dev = NULL;
1357 }
1358 if (dev->vdev) {
1359 if (-1 != dev->vdev->minor)
1360 video_unregister_device(dev->vdev);
1361 else
1362 video_device_release(dev->vdev);
1363 dev->vdev = NULL;
1364 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001365 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001366 usb_put_dev(dev->udev);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001367
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001368 /* Mark device as unused */
1369 em28xx_devused&=~(1<<dev->devno);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001370}
1371
1372/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001373 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001374 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
1375 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001376static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001377{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001378 struct em28xx_fh *fh = filp->private_data;
1379 struct em28xx *dev = fh->dev;
1380 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001381
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001382 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001383
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001384
1385 if (res_check(fh))
1386 res_free(fh);
1387
Ingo Molnar3593cab2006-02-07 06:49:14 -02001388 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001389
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001390 if (dev->users == 1) {
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001391 em28xx_uninit_isoc(dev);
1392 em28xx_release_buffers(dev);
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001393 dev->io = IO_NONE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001394
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001395 /* the device is already disconnect,
1396 free the remaining resources */
1397 if (dev->state & DEV_DISCONNECTED) {
1398 em28xx_release_resources(dev);
1399 mutex_unlock(&dev->lock);
1400 kfree(dev);
1401 return 0;
1402 }
1403
1404 /* set alternate 0 */
1405 dev->alt = 0;
1406 em28xx_videodbg("setting alternate 0\n");
1407 errCode = usb_set_interface(dev->udev, 0, 0);
1408 if (errCode < 0) {
1409 em28xx_errdev("cannot change alternate number to "
1410 "0 (error=%i)\n", errCode);
1411 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001412 }
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001413 kfree(fh);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001414 dev->users--;
1415 wake_up_interruptible_nr(&dev->open, 1);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001416 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001417 return 0;
1418}
1419
1420/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001421 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001422 * will allocate buffers when called for the first time
1423 */
1424static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001425em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001426 loff_t * f_pos)
1427{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001428 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001429 unsigned long lock_flags;
1430 int ret = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001431 struct em28xx_fh *fh = filp->private_data;
1432 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001433
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001434 /* FIXME: read() is not prepared to allow changing the video
1435 resolution while streaming. Seems a bug at em28xx_set_fmt
1436 */
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001437
1438 if (unlikely(res_get(fh) < 0))
1439 return -EBUSY;
1440
Sascha Sommer5a804152007-11-03 21:22:38 -03001441 mutex_lock(&dev->lock);
1442
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001443 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001444 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001445
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001446 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1447 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n");
1448 em28xx_videodbg("not supported yet! ...\n");
1449 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001450 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001451 return -EFAULT;
1452 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001453 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001454 return (1);
1455 }
1456 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1457 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1458 em28xx_videodbg("not supported yet! ...\n");
1459 if (copy_to_user(buf, "", 1)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001460 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001461 return -EFAULT;
1462 }
Sascha Sommer5a804152007-11-03 21:22:38 -03001463 mutex_unlock(&dev->lock);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001464 return (1);
1465 }
1466
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001467 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001468 em28xx_videodbg("device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001469 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001470 return -ENODEV;
1471 }
1472
1473 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001474 em28xx_videodbg("device misconfigured; close and open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001475 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001476 return -EIO;
1477 }
1478
1479 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001480 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001481 " the device again to choose the read method\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001482 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001483 return -EINVAL;
1484 }
1485
1486 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001487 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1488 em28xx_errdev("read failed, not enough memory\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001489 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001490 return -ENOMEM;
1491 }
1492 dev->io = IO_READ;
1493 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001494 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001495 }
1496
1497 if (!count) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001498 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001499 return 0;
1500 }
1501
1502 if (list_empty(&dev->outqueue)) {
1503 if (filp->f_flags & O_NONBLOCK) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001504 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001505 return -EAGAIN;
1506 }
1507 ret = wait_event_interruptible
1508 (dev->wait_frame,
1509 (!list_empty(&dev->outqueue)) ||
1510 (dev->state & DEV_DISCONNECTED));
1511 if (ret) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001512 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001513 return ret;
1514 }
1515 if (dev->state & DEV_DISCONNECTED) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001516 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001517 return -ENODEV;
1518 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001519 dev->video_bytesread = 0;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001520 }
1521
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001522 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001523
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001524 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001525
1526 if (count > f->buf.length)
1527 count = f->buf.length;
1528
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001529 if ((dev->video_bytesread + count) > dev->frame_size)
1530 count = dev->frame_size - dev->video_bytesread;
1531
1532 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1533 em28xx_err("Error while copying to user\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001534 return -EFAULT;
1535 }
Mauro Carvalho Chehab9e31ced2007-11-11 01:13:49 -03001536 dev->video_bytesread += count;
1537
1538 if (dev->video_bytesread == dev->frame_size) {
1539 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1540 list_for_each_entry(i, &dev->outqueue, frame)
1541 i->state = F_UNUSED;
1542 INIT_LIST_HEAD(&dev->outqueue);
1543 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1544
1545 em28xx_queue_unusedframes(dev);
1546 dev->video_bytesread = 0;
1547 }
1548
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001549 *f_pos += count;
1550
Sascha Sommer5a804152007-11-03 21:22:38 -03001551 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001552
1553 return count;
1554}
1555
1556/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001557 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001558 * will allocate buffers when called for the first time
1559 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001560static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001561{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001562 unsigned int mask = 0;
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001563 struct em28xx_fh *fh = filp->private_data;
1564 struct em28xx *dev = fh->dev;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001565
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001566 if (unlikely(res_get(fh) < 0))
1567 return POLLERR;
1568
Sascha Sommer5a804152007-11-03 21:22:38 -03001569 mutex_lock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001570
1571 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001572 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001573 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001574 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001575 } else {
1576 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001577 if (!em28xx_request_buffers
1578 (dev, EM28XX_NUM_READ_FRAMES)) {
1579 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001580 ("poll() failed, not enough memory\n");
1581 } else {
1582 dev->io = IO_READ;
1583 dev->stream = STREAM_ON;
1584 }
1585 }
1586
1587 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001588 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001589 poll_wait(filp, &dev->wait_frame, wait);
1590
1591 if (!list_empty(&dev->outqueue))
1592 mask |= POLLIN | POLLRDNORM;
1593
Sascha Sommer5a804152007-11-03 21:22:38 -03001594 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001595
1596 return mask;
1597 }
1598 }
1599
Sascha Sommer5a804152007-11-03 21:22:38 -03001600 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001601 return POLLERR;
1602}
1603
1604/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001605 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001606 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001607static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001608{
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001609 struct em28xx_fh *fh = filp->private_data;
1610 struct em28xx *dev = fh->dev;
1611 unsigned long size = vma->vm_end - vma->vm_start;
1612 unsigned long start = vma->vm_start;
1613 void *pos;
1614 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -08001615
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001616 if (unlikely(res_get(fh) < 0))
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001617 return -EBUSY;
Mauro Carvalho Chehaba2254522007-11-11 01:08:26 -03001618
1619 mutex_lock(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001620
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001621 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001622 em28xx_videodbg("mmap: device not present\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001623 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001624 return -ENODEV;
1625 }
1626
1627 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001628 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001629 "open it again\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001630 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001631 return -EIO;
1632 }
1633
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001634 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
Sascha Sommer5a804152007-11-03 21:22:38 -03001635 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001636 return -EINVAL;
1637 }
1638
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001639 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1640 size = PAGE_ALIGN(dev->frame[0].buf.length);
1641
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642 for (i = 0; i < dev->num_frames; i++) {
1643 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1644 break;
1645 }
1646 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001647 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001648 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001649 return -EINVAL;
1650 }
1651
1652 /* VM_IO is eventually going to replace PageReserved altogether */
1653 vma->vm_flags |= VM_IO;
1654 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1655
Sascha Sommer3639c862005-12-12 00:37:30 -08001656 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001657 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -08001658 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1659 em28xx_videodbg("mmap: vm_insert_page failed\n");
Sascha Sommer5a804152007-11-03 21:22:38 -03001660 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661 return -EAGAIN;
1662 }
1663 start += PAGE_SIZE;
1664 pos += PAGE_SIZE;
1665 size -= PAGE_SIZE;
1666 }
1667
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001668 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001669 vma->vm_private_data = &dev->frame[i];
1670
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001671 em28xx_vm_open(vma);
Sascha Sommer5a804152007-11-03 21:22:38 -03001672 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001673 return 0;
1674}
1675
Arjan van de Venfa027c22007-02-12 00:55:33 -08001676static const struct file_operations em28xx_v4l_fops = {
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001677 .owner = THIS_MODULE,
1678 .open = em28xx_v4l2_open,
1679 .release = em28xx_v4l2_close,
1680 .read = em28xx_v4l2_read,
1681 .poll = em28xx_v4l2_poll,
1682 .mmap = em28xx_v4l2_mmap,
1683 .ioctl = video_ioctl2,
1684 .llseek = no_llseek,
1685 .compat_ioctl = v4l_compat_ioctl32,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001686};
1687
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001688static const struct file_operations radio_fops = {
1689 .owner = THIS_MODULE,
1690 .open = em28xx_v4l2_open,
1691 .release = em28xx_v4l2_close,
1692 .ioctl = video_ioctl2,
1693 .compat_ioctl = v4l_compat_ioctl32,
1694 .llseek = no_llseek,
1695};
1696
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001697static const struct video_device em28xx_video_template = {
1698 .fops = &em28xx_v4l_fops,
1699 .release = video_device_release,
1700
1701 .minor = -1,
1702 .vidioc_querycap = vidioc_querycap,
1703 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1704 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1705 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1706 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1707 .vidioc_g_audio = vidioc_g_audio,
1708 .vidioc_s_audio = vidioc_s_audio,
1709 .vidioc_cropcap = vidioc_cropcap,
1710
1711 .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture,
1712 .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1713 .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture,
1714
1715 .vidioc_reqbufs = vidioc_reqbufs,
1716 .vidioc_querybuf = vidioc_querybuf,
1717 .vidioc_qbuf = vidioc_qbuf,
1718 .vidioc_dqbuf = vidioc_dqbuf,
1719 .vidioc_s_std = vidioc_s_std,
1720 .vidioc_enum_input = vidioc_enum_input,
1721 .vidioc_g_input = vidioc_g_input,
1722 .vidioc_s_input = vidioc_s_input,
1723 .vidioc_queryctrl = vidioc_queryctrl,
1724 .vidioc_g_ctrl = vidioc_g_ctrl,
1725 .vidioc_s_ctrl = vidioc_s_ctrl,
1726 .vidioc_streamon = vidioc_streamon,
1727 .vidioc_streamoff = vidioc_streamoff,
1728 .vidioc_g_tuner = vidioc_g_tuner,
1729 .vidioc_s_tuner = vidioc_s_tuner,
1730 .vidioc_g_frequency = vidioc_g_frequency,
1731 .vidioc_s_frequency = vidioc_s_frequency,
1732
1733 .tvnorms = V4L2_STD_ALL,
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001734 .current_norm = V4L2_STD_PAL,
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001735};
1736
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001737static struct video_device em28xx_radio_template = {
1738 .name = "em28xx-radio",
1739 .type = VID_TYPE_TUNER,
1740 .fops = &radio_fops,
1741 .minor = -1,
1742 .vidioc_querycap = radio_querycap,
1743 .vidioc_g_tuner = radio_g_tuner,
1744 .vidioc_enum_input = radio_enum_input,
1745 .vidioc_g_audio = radio_g_audio,
1746 .vidioc_s_tuner = radio_s_tuner,
1747 .vidioc_s_audio = radio_s_audio,
1748 .vidioc_s_input = radio_s_input,
1749 .vidioc_queryctrl = radio_queryctrl,
1750 .vidioc_g_ctrl = vidioc_g_ctrl,
1751 .vidioc_s_ctrl = vidioc_s_ctrl,
1752 .vidioc_g_frequency = vidioc_g_frequency,
1753 .vidioc_s_frequency = vidioc_s_frequency,
1754};
1755
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001756/******************************** usb interface *****************************************/
1757
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001758
1759static LIST_HEAD(em28xx_extension_devlist);
1760static DEFINE_MUTEX(em28xx_extension_devlist_lock);
1761
1762int em28xx_register_extension(struct em28xx_ops *ops)
1763{
1764 struct em28xx *h, *dev = NULL;
1765
1766 list_for_each_entry(h, &em28xx_devlist, devlist)
1767 dev = h;
1768
1769 mutex_lock(&em28xx_extension_devlist_lock);
1770 list_add_tail(&ops->next, &em28xx_extension_devlist);
1771 if (dev)
1772 ops->init(dev);
1773
1774 printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
1775 mutex_unlock(&em28xx_extension_devlist_lock);
1776
1777 return 0;
1778}
1779EXPORT_SYMBOL(em28xx_register_extension);
1780
1781void em28xx_unregister_extension(struct em28xx_ops *ops)
1782{
1783 struct em28xx *h, *dev = NULL;
1784
1785 list_for_each_entry(h, &em28xx_devlist, devlist)
1786 dev = h;
1787
1788 if (dev)
1789 ops->fini(dev);
1790
1791 mutex_lock(&em28xx_extension_devlist_lock);
1792 printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
1793 list_del(&ops->next);
1794 mutex_unlock(&em28xx_extension_devlist_lock);
1795}
1796EXPORT_SYMBOL(em28xx_unregister_extension);
1797
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001798struct video_device *em28xx_vdev_init(struct em28xx *dev,
1799 const struct video_device *template,
1800 const int type,
1801 const char *type_name)
1802{
1803 struct video_device *vfd;
1804
1805 vfd = video_device_alloc();
1806 if (NULL == vfd)
1807 return NULL;
1808 *vfd = *template;
1809 vfd->minor = -1;
1810 vfd->dev = &dev->udev->dev;
1811 vfd->release = video_device_release;
1812 vfd->type = type;
1813
1814 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1815 dev->name, type_name);
1816
1817 return vfd;
1818}
1819
1820
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001821/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001822 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001823 * allocates and inits the device structs, registers i2c bus and v4l device
1824 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001825static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001826 int minor)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001827{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001828 struct em28xx_ops *ops = NULL;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001829 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001830 int retval = -ENOMEM;
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001831 int errCode;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001832 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001833
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001834 dev->udev = udev;
Ingo Molnar3593cab2006-02-07 06:49:14 -02001835 mutex_init(&dev->lock);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001836 spin_lock_init(&dev->queue_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001837 init_waitqueue_head(&dev->open);
Mauro Carvalho Chehaba3a048c2007-11-10 22:21:01 -03001838 init_waitqueue_head(&dev->wait_frame);
1839 init_waitqueue_head(&dev->wait_stream);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001840
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001841 dev->em28xx_write_regs = em28xx_write_regs;
1842 dev->em28xx_read_reg = em28xx_read_reg;
1843 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1844 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1845 dev->em28xx_read_reg_req = em28xx_read_reg_req;
Sascha Sommerfad7b952007-11-04 08:06:48 -03001846 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001847
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03001848 errCode = em28xx_read_reg(dev, CHIPID_REG);
1849 if (errCode >= 0)
1850 em28xx_info("em28xx chip ID = %d\n", errCode);
1851
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001852 em28xx_pre_card_setup(dev);
1853
1854 errCode = em28xx_config(dev);
1855 if (errCode) {
1856 em28xx_errdev("error configuring device\n");
1857 em28xx_devused &= ~(1<<dev->devno);
1858 kfree(dev);
1859 return -ENOMEM;
1860 }
1861
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001862 /* register i2c bus */
1863 em28xx_i2c_register(dev);
1864
1865 /* Do board specific init and eeprom reading */
1866 em28xx_card_setup(dev);
1867
Mauro Carvalho Chehab3abee532008-01-05 17:01:41 -03001868 /* Configure audio */
1869 em28xx_audio_analog_set(dev);
1870
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03001871 /* configure the device */
1872 em28xx_config_i2c(dev);
1873
Mauro Carvalho Chehab7d497f82007-11-11 14:15:34 -03001874 /* set default norm */
1875 dev->norm = em28xx_video_template.current_norm;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001876
1877 maxw = norm_maxw(dev);
1878 maxh = norm_maxh(dev);
1879
1880 /* set default image size */
1881 dev->width = maxw;
1882 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001883 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001884 dev->field_size = dev->width * dev->height;
1885 dev->frame_size =
1886 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1887 dev->bytesperline = dev->width * 2;
1888 dev->hscale = 0;
1889 dev->vscale = 0;
1890 dev->ctl_input = 2;
1891
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001892 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001893
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001894 list_add_tail(&dev->devlist, &em28xx_devlist);
1895
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001896 /* allocate and fill video video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001897 dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template,
1898 VID_TYPE_CAPTURE, "video");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001899 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001900 em28xx_errdev("cannot allocate video_device.\n");
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001901 goto fail_unreg;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001902 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001903 if (dev->has_tuner)
1904 dev->vdev->type |= VID_TYPE_TUNER;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001905
1906 /* register v4l2 video video_device */
1907 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
1908 video_nr[dev->devno]);
1909 if (retval) {
1910 em28xx_errdev("unable to register video device (error=%i).\n",
1911 retval);
1912 goto fail_unreg;
1913 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001914
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001915 /* Allocate and fill vbi video_device struct */
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001916 dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
1917 VFL_TYPE_VBI, "vbi");
1918 /* register v4l2 vbi video_device */
1919 if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1920 vbi_nr[dev->devno]) < 0) {
1921 em28xx_errdev("unable to register vbi device\n");
1922 retval = -ENODEV;
1923 goto fail_unreg;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001924 }
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001925
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001926 if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
1927 dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
1928 VFL_TYPE_RADIO, "radio");
1929 if (NULL == dev->radio_dev) {
1930 em28xx_errdev("cannot allocate video_device.\n");
1931 goto fail_unreg;
1932 }
1933 retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
1934 radio_nr[dev->devno]);
1935 if (retval < 0) {
1936 em28xx_errdev("can't register radio device\n");
1937 goto fail_unreg;
1938 }
1939 em28xx_info("Registered radio device as /dev/radio%d\n",
1940 dev->radio_dev->minor & 0x1f);
1941 }
1942
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001943
Sascha Sommer5a804152007-11-03 21:22:38 -03001944 if (dev->has_msp34xx) {
1945 /* Send a reset to other chips via gpio */
1946 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
1947 msleep(3);
1948 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
1949 msleep(3);
Sascha Sommer5a804152007-11-03 21:22:38 -03001950 }
Mauro Carvalho Chehab195a4ef2007-11-11 13:17:17 -03001951
Sascha Sommer5a804152007-11-03 21:22:38 -03001952 video_mux(dev, 0);
1953
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02001954 em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
1955 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
1956 dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001957
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03001958 mutex_lock(&em28xx_extension_devlist_lock);
1959 if (!list_empty(&em28xx_extension_devlist)) {
1960 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
1961 if (ops->id)
1962 ops->init(dev);
1963 }
1964 }
1965 mutex_unlock(&em28xx_extension_devlist_lock);
1966
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001967 return 0;
Mauro Carvalho Chehab0be43752008-01-05 17:22:01 -03001968
1969fail_unreg:
1970 em28xx_release_resources(dev);
1971 mutex_unlock(&dev->lock);
1972 kfree(dev);
1973 return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001974}
1975
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03001976#if defined(CONFIG_MODULES) && defined(MODULE)
1977static void request_module_async(struct work_struct *work)
1978{
1979 struct em28xx *dev = container_of(work,
1980 struct em28xx, request_module_wk);
1981
Mauro Carvalho Chehab3f4dfe22008-01-06 09:54:17 -03001982 if (dev->has_audio_class)
1983 request_module("snd-usb-audio");
1984 else
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03001985 request_module("em28xx-alsa");
1986}
1987
1988static void request_modules(struct em28xx *dev)
1989{
1990 INIT_WORK(&dev->request_module_wk, request_module_async);
1991 schedule_work(&dev->request_module_wk);
1992}
1993#else
1994#define request_modules(dev)
1995#endif /* CONFIG_MODULES */
1996
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001997/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001998 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001999 * checks for supported devices
2000 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002001static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002002 const struct usb_device_id *id)
2003{
2004 const struct usb_endpoint_descriptor *endpoint;
2005 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002006 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002007 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002008 int retval = -ENODEV;
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002009 int i, nr, ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002010
2011 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002012 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2013
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002014 /* Check to see next free device and mark as used */
2015 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
2016 em28xx_devused|=1<<nr;
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002017
2018 /* Don't register audio interfaces */
2019 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002020 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002021 udev->descriptor.idVendor,udev->descriptor.idProduct,
2022 ifnum,
2023 interface->altsetting[0].desc.bInterfaceClass);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002024
2025 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08002026 return -ENODEV;
2027 }
2028
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002029 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002030 udev->descriptor.idVendor,udev->descriptor.idProduct,
2031 ifnum,
2032 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002033
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08002034 endpoint = &interface->cur_altsetting->endpoint[1].desc;
2035
Michael Opdenacker59c51592007-05-09 08:57:56 +02002036 /* check if the device has the iso in endpoint at the correct place */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002037 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2038 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002039 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002040 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002041 return -ENODEV;
2042 }
2043 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002044 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002045 em28xx_devused&=~(1<<nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002046 return -ENODEV;
2047 }
2048
Mauro Carvalho Chehab19478842006-03-14 17:24:57 -03002049 if (nr >= EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002050 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002051 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002052 return -ENOMEM;
2053 }
2054
2055 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02002056 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002057 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002058 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002059 em28xx_devused&=~(1<<nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002060 return -ENOMEM;
2061 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002062
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002063 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002064 dev->devno = nr;
2065 dev->model = id->driver_info;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002066
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002067 /* Checks if audio is provided by some interface */
2068 for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
2069 uif = udev->config->interface[i];
2070 if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2071 dev->has_audio_class = 1;
2072 break;
2073 }
2074 }
2075
2076 printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
2077 dev->has_audio_class ? "Has" : "Doesn't have");
2078
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002079 /* compute alternate max packet sizes */
2080 uif = udev->actconfig->interface[0];
2081
2082 dev->num_alt=uif->num_altsetting;
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002083 em28xx_info("Alternate settings: %i\n",dev->num_alt);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002084// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
2085 dev->alt_max_pkt_size = kmalloc(32*
2086 dev->num_alt,GFP_KERNEL);
2087 if (dev->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002088 em28xx_errdev("out of memory!\n");
2089 em28xx_devused&=~(1<<nr);
Jesper Juhl1207cf842007-08-09 23:02:36 +02002090 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002091 return -ENOMEM;
2092 }
2093
2094 for (i = 0; i < dev->num_alt ; i++) {
2095 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
2096 wMaxPacketSize);
2097 dev->alt_max_pkt_size[i] =
2098 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002099 em28xx_info("Alternate setting %i, max size= %i\n",i,
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002100 dev->alt_max_pkt_size[i]);
2101 }
2102
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002103 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002104 dev->model = card[nr];
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08002105
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002106 /* allocate device struct */
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002107 retval = em28xx_init_dev(&dev, udev, nr);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002108 if (retval)
2109 return retval;
2110
Mauro Carvalho Chehab03910cc2007-11-03 21:20:59 -03002111 em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002112
2113 /* save our data pointer in this interface device */
2114 usb_set_intfdata(interface, dev);
Mauro Carvalho Chehabd7448a82008-01-05 09:59:03 -03002115
2116 request_modules(dev);
2117
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002118 return 0;
2119}
2120
2121/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002122 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002123 * called when the device gets diconencted
2124 * video device will be unregistered on v4l2_close in case it is still open
2125 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002126static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002127{
Sascha Sommer5a804152007-11-03 21:22:38 -03002128 struct em28xx *dev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002129 struct em28xx_ops *ops = NULL;
Sascha Sommer5a804152007-11-03 21:22:38 -03002130
2131 dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002132 usb_set_intfdata(interface, NULL);
2133
2134 if (!dev)
2135 return;
2136
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002137 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002138
Sascha Sommer5a804152007-11-03 21:22:38 -03002139 /* wait until all current v4l2 io is finished then deallocate resources */
2140 mutex_lock(&dev->lock);
2141
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002142 wake_up_interruptible_all(&dev->open);
2143
2144 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002145 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002146 ("device /dev/video%d is open! Deregistration and memory "
Mauro Carvalho Chehabe5589be2006-01-23 17:11:08 -02002147 "deallocation are deferred on close.\n",
2148 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
2149
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002150 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002151 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002152 dev->state |= DEV_DISCONNECTED;
2153 wake_up_interruptible(&dev->wait_frame);
2154 wake_up_interruptible(&dev->wait_stream);
2155 } else {
2156 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002157 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002158 }
Ingo Molnar3593cab2006-02-07 06:49:14 -02002159 mutex_unlock(&dev->lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002160
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03002161 mutex_lock(&em28xx_extension_devlist_lock);
2162 if (!list_empty(&em28xx_extension_devlist)) {
2163 list_for_each_entry(ops, &em28xx_extension_devlist, next) {
2164 ops->fini(dev);
2165 }
2166 }
2167 mutex_unlock(&em28xx_extension_devlist_lock);
2168
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002169 if (!dev->users) {
2170 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002171 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08002172 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002173}
2174
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002175static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002176 .name = "em28xx",
2177 .probe = em28xx_usb_probe,
2178 .disconnect = em28xx_usb_disconnect,
2179 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002180};
2181
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002182static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002183{
2184 int result;
2185
2186 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002187 (EM28XX_VERSION_CODE >> 16) & 0xff,
2188 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002189#ifdef SNAPSHOT
2190 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
2191 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
2192#endif
2193
2194 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002195 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002196 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002197 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002198 " usb_register failed. Error number %d.\n", result);
2199
2200 return result;
2201}
2202
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002203static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002204{
2205 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002206 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002207}
2208
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002209module_init(em28xx_module_init);
2210module_exit(em28xx_module_exit);