blob: eea304f75176a1ca18e38785defddb8932b61ae4 [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 Chehab4ac97912005-11-08 21:37:43 -08006 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -08007 Sascha Sommer <saschasommer@freenet.de>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/init.h>
25#include <linux/list.h>
26#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/usb.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080029#include <linux/i2c.h>
Mauro Carvalho Chehabb296fc62005-11-08 21:38:37 -080030#include <linux/version.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080031#include <linux/video_decoder.h>
Ingo Molnar1e4baed2006-01-15 07:52:23 -020032#include <linux/mutex.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080033
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080034#include "em28xx.h"
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -080035#include <media/tuner.h>
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -020036#include <media/v4l2-common.h>
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080037
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080038#define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
39 "Markus Rechberger <mrechberger@gmail.com>, " \
40 "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \
41 "Sascha Sommer <saschasommer@freenet.de>"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080042
Mauro Carvalho Chehabf7abcd32005-11-08 21:38:25 -080043#define DRIVER_NAME "em28xx"
44#define DRIVER_DESC "Empia em28xx based USB video device driver"
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080045#define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080046
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080047#define em28xx_videodbg(fmt, arg...) do {\
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080048 if (video_debug) \
49 printk(KERN_INFO "%s %s :"fmt, \
Jean Delvaref85c6572005-12-19 08:53:59 -020050 dev->name, __FUNCTION__ , ##arg); } while (0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080051
52MODULE_AUTHOR(DRIVER_AUTHOR);
53MODULE_DESCRIPTION(DRIVER_DESC);
54MODULE_LICENSE("GPL");
55
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080056static LIST_HEAD(em28xx_devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -080057
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -080058static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -080059module_param_array(card, int, NULL, 0444);
60MODULE_PARM_DESC(card,"card type");
61
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080062static int tuner = -1;
63module_param(tuner, int, 0444);
64MODULE_PARM_DESC(tuner, "tuner type");
65
66static unsigned int video_debug = 0;
67module_param(video_debug,int,0644);
68MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
69
70/* supported tv norms */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -080071static struct em28xx_tvnorm tvnorms[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -080072 {
73 .name = "PAL",
74 .id = V4L2_STD_PAL,
75 .mode = VIDEO_MODE_PAL,
76 }, {
77 .name = "NTSC",
78 .id = V4L2_STD_NTSC,
79 .mode = VIDEO_MODE_NTSC,
80 }, {
81 .name = "SECAM",
82 .id = V4L2_STD_SECAM,
83 .mode = VIDEO_MODE_SECAM,
84 }, {
85 .name = "PAL-M",
86 .id = V4L2_STD_PAL_M,
87 .mode = VIDEO_MODE_PAL,
88 }
89};
90
Markus Rechbergera38a7d42005-11-08 21:38:06 -080091static const unsigned char saa7114_i2c_init[] = {
92 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0,
93 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a,
94 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42,
95 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff,
96 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff,
97 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff,
98 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00,
99 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00,
100 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0,
101 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06,
102 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67,
103 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd,
104 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00,
105 0xbe,0x00,0xbf,0x00
106};
107
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800108#define TVNORMS ARRAY_SIZE(tvnorms)
109
110/* supported controls */
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200111/* Common to all boards */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800112static struct v4l2_queryctrl em28xx_qctrl[] = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800113 {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200114 .id = V4L2_CID_AUDIO_VOLUME,
115 .type = V4L2_CTRL_TYPE_INTEGER,
116 .name = "Volume",
117 .minimum = 0x0,
118 .maximum = 0x1f,
119 .step = 0x1,
120 .default_value = 0x1f,
121 .flags = 0,
122 },{
123 .id = V4L2_CID_AUDIO_MUTE,
124 .type = V4L2_CTRL_TYPE_BOOLEAN,
125 .name = "Mute",
126 .minimum = 0,
127 .maximum = 1,
128 .step = 1,
129 .default_value = 1,
130 .flags = 0,
131 }
132};
133
134/* FIXME: These are specific to saa711x - should be moved to its code */
135static struct v4l2_queryctrl saa711x_qctrl[] = {
136 {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800137 .id = V4L2_CID_BRIGHTNESS,
138 .type = V4L2_CTRL_TYPE_INTEGER,
139 .name = "Brightness",
140 .minimum = -128,
141 .maximum = 127,
142 .step = 1,
143 .default_value = 0,
144 .flags = 0,
145 },{
146 .id = V4L2_CID_CONTRAST,
147 .type = V4L2_CTRL_TYPE_INTEGER,
148 .name = "Contrast",
149 .minimum = 0x0,
150 .maximum = 0x1f,
151 .step = 0x1,
152 .default_value = 0x10,
153 .flags = 0,
154 },{
155 .id = V4L2_CID_SATURATION,
156 .type = V4L2_CTRL_TYPE_INTEGER,
157 .name = "Saturation",
158 .minimum = 0x0,
159 .maximum = 0x1f,
160 .step = 0x1,
161 .default_value = 0x10,
162 .flags = 0,
163 },{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800164 .id = V4L2_CID_RED_BALANCE,
165 .type = V4L2_CTRL_TYPE_INTEGER,
166 .name = "Red chroma balance",
167 .minimum = -128,
168 .maximum = 127,
169 .step = 1,
170 .default_value = 0,
171 .flags = 0,
172 },{
173 .id = V4L2_CID_BLUE_BALANCE,
174 .type = V4L2_CTRL_TYPE_INTEGER,
175 .name = "Blue chroma balance",
176 .minimum = -128,
177 .maximum = 127,
178 .step = 1,
179 .default_value = 0,
180 .flags = 0,
181 },{
182 .id = V4L2_CID_GAMMA,
183 .type = V4L2_CTRL_TYPE_INTEGER,
184 .name = "Gamma",
185 .minimum = 0x0,
186 .maximum = 0x3f,
187 .step = 0x1,
188 .default_value = 0x20,
189 .flags = 0,
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200190 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800191};
192
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800193static struct usb_driver em28xx_usb_driver;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800194
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200195static DEFINE_MUTEX(em28xx_sysfs_lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800196static DECLARE_RWSEM(em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800197
198/********************* v4l2 interface ******************************************/
199
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800200/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800201 * em28xx_config()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800202 * inits registers with sane defaults
203 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800204static int em28xx_config(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800205{
206
207 /* Sets I2C speed to 100 KHz */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800208 em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800209
210 /* enable vbi capturing */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800211 em28xx_audio_usb_mute(dev, 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800212 dev->mute = 1; /* maybe not the right place... */
213 dev->volume = 0x1f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800214 em28xx_audio_analog_set(dev);
215 em28xx_audio_analog_setup(dev);
216 em28xx_outfmt_set_yuv422(dev);
217 em28xx_colorlevels_set_default(dev);
218 em28xx_compression_disable(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800219
220 return 0;
221}
222
223/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800224 * em28xx_config_i2c()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800225 * configure i2c attached devices
226 */
Adrian Bunk943a4902005-12-01 00:51:35 -0800227static void em28xx_config_i2c(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800228{
229 struct v4l2_frequency f;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800230 struct video_decoder_init em28xx_vdi = {.data = NULL };
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800231
232
233 /* configure decoder */
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800234 if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800235 em28xx_vdi.data=saa7114_i2c_init;
236 em28xx_vdi.len=sizeof(saa7114_i2c_init);
Markus Rechbergera38a7d42005-11-08 21:38:06 -0800237 }
238
239
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800240 em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi);
241 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input);
242/* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */
243/* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */
244/* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */
245/* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800246
247 /* configure tuner */
248 f.tuner = 0;
249 f.type = V4L2_TUNER_ANALOG_TV;
250 f.frequency = 9076; /* FIXME:remove magic number */
251 dev->ctl_freq = f.frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800252 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800253
254 /* configure tda9887 */
255
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800256
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800257/* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800258}
259
260/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800261 * em28xx_empty_framequeues()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800262 * prepare queues for incoming and outgoing frames
263 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800264static void em28xx_empty_framequeues(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800265{
266 u32 i;
267
268 INIT_LIST_HEAD(&dev->inqueue);
269 INIT_LIST_HEAD(&dev->outqueue);
270
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800271 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800272 dev->frame[i].state = F_UNUSED;
273 dev->frame[i].buf.bytesused = 0;
274 }
275}
276
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800277static void video_mux(struct em28xx *dev, int index)
278{
279 int input, ainput;
280
281 input = INPUT(index)->vmux;
282 dev->ctl_input = index;
283 dev->ctl_ainput = INPUT(index)->amux;
284
285 em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input);
286
287
288 em28xx_videodbg("Setting input index=%d, vmux=%d, amux=%d\n",index,input,dev->ctl_ainput);
289
290 if (dev->has_msp34xx) {
Mauro Carvalho Chehab9bb13a62006-01-09 15:25:37 -0200291 if (dev->i2s_speed)
292 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed);
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800293 em28xx_i2c_call_clients(dev, VIDIOC_S_AUDIO, &dev->ctl_ainput);
294 ainput = EM28XX_AUDIO_SRC_TUNER;
295 em28xx_audio_source(dev, ainput);
296 } else {
297 switch (dev->ctl_ainput) {
298 case 0:
299 ainput = EM28XX_AUDIO_SRC_TUNER;
300 break;
301 default:
302 ainput = EM28XX_AUDIO_SRC_LINE;
303 }
304 em28xx_audio_source(dev, ainput);
305 }
306}
307
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800308/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800309 * em28xx_v4l2_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800310 * inits the device and starts isoc transfer
311 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800312static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800313{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800314 int minor = iminor(inode);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800315 int errCode = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800316 struct em28xx *h,*dev = NULL;
Markus Rechberger9c755412005-11-08 21:37:52 -0800317 struct list_head *list;
318
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800319 list_for_each(list,&em28xx_devlist) {
320 h = list_entry(list, struct em28xx, devlist);
Markus Rechberger9c755412005-11-08 21:37:52 -0800321 if (h->vdev->minor == minor) {
322 dev = h;
323 }
324 }
325
326 filp->private_data=dev;
327
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800328
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800329 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800330
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800331 if (!down_read_trylock(&em28xx_disconnect))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800332 return -ERESTARTSYS;
333
334 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800335 em28xx_warn("this driver can be opened only once\n");
336 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800337 return -EBUSY;
338 }
339
340/* if(dev->vbi_dev->minor == minor){
341 dev->type=V4L2_BUF_TYPE_VBI_CAPTURE;
342 }*/
343 if (dev->vdev->minor == minor) {
344 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
345 }
346
347 init_MUTEX(&dev->fileop_lock); /* to 1 == available */
348 spin_lock_init(&dev->queue_lock);
349 init_waitqueue_head(&dev->wait_frame);
350 init_waitqueue_head(&dev->wait_stream);
351
352 down(&dev->lock);
353
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800354 em28xx_set_alternate(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800355
356 dev->width = norm_maxw(dev);
357 dev->height = norm_maxh(dev);
358 dev->frame_size = dev->width * dev->height * 2;
359 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
360 dev->bytesperline = dev->width * 2;
361 dev->hscale = 0;
362 dev->vscale = 0;
363
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800364 em28xx_capture_start(dev, 1);
365 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800366
367 /* start the transfer */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800368 errCode = em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800369 if (errCode)
370 goto err;
371
372 dev->users++;
373 filp->private_data = dev;
374 dev->io = IO_NONE;
375 dev->stream = STREAM_OFF;
376 dev->num_frames = 0;
377
378 /* prepare queues */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800379 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800380
381 dev->state |= DEV_INITIALIZED;
382
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800383 video_mux(dev, 0);
384
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800385 err:
386 up(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800387 up_read(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800388 return errCode;
389}
390
391/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800392 * em28xx_realease_resources()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800393 * unregisters the v4l2,i2c and usb devices
394 * called when the device gets disconected or at module unload
395*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800396static void em28xx_release_resources(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800397{
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200398 mutex_lock(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800399
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800400 em28xx_info("V4L2 device /dev/video%d deregistered\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800401 dev->vdev->minor);
Markus Rechberger9c755412005-11-08 21:37:52 -0800402 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800403 video_unregister_device(dev->vdev);
404/* video_unregister_device(dev->vbi_dev); */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800405 em28xx_i2c_unregister(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800406 usb_put_dev(dev->udev);
Ingo Molnar1e4baed2006-01-15 07:52:23 -0200407 mutex_unlock(&em28xx_sysfs_lock);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800408}
409
410/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800411 * em28xx_v4l2_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800412 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls
413 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800414static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800415{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800416 int errCode;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800417 struct em28xx *dev=filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800418
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800419 em28xx_videodbg("users=%d\n", dev->users);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800420
421 down(&dev->lock);
422
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800423 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800424
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800425 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800426
427 /* the device is already disconnect, free the remaining resources */
428 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800429 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800430 up(&dev->lock);
431 kfree(dev);
432 return 0;
433 }
434
435 /* set alternate 0 */
436 dev->alt = 0;
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800437 em28xx_videodbg("setting alternate 0\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800438 errCode = usb_set_interface(dev->udev, 0, 0);
439 if (errCode < 0) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800440 em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800441 errCode);
442 }
443
444 dev->users--;
445 wake_up_interruptible_nr(&dev->open, 1);
446 up(&dev->lock);
447 return 0;
448}
449
450/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800451 * em28xx_v4l2_read()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800452 * will allocate buffers when called for the first time
453 */
454static ssize_t
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800455em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800456 loff_t * f_pos)
457{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800458 struct em28xx_frame_t *f, *i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800459 unsigned long lock_flags;
460 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800461 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800462
463 if (down_interruptible(&dev->fileop_lock))
464 return -ERESTARTSYS;
465
466 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800467 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800468 up(&dev->fileop_lock);
469 return -ENODEV;
470 }
471
472 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800473 em28xx_videodbg("device misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800474 up(&dev->fileop_lock);
475 return -EIO;
476 }
477
478 if (dev->io == IO_MMAP) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800479 em28xx_videodbg ("IO method is set to mmap; close and open"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800480 " the device again to choose the read method\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800481 up(&dev->fileop_lock);
482 return -EINVAL;
483 }
484
485 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800486 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
487 em28xx_errdev("read failed, not enough memory\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800488 up(&dev->fileop_lock);
489 return -ENOMEM;
490 }
491 dev->io = IO_READ;
492 dev->stream = STREAM_ON;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800493 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800494 }
495
496 if (!count) {
497 up(&dev->fileop_lock);
498 return 0;
499 }
500
501 if (list_empty(&dev->outqueue)) {
502 if (filp->f_flags & O_NONBLOCK) {
503 up(&dev->fileop_lock);
504 return -EAGAIN;
505 }
506 ret = wait_event_interruptible
507 (dev->wait_frame,
508 (!list_empty(&dev->outqueue)) ||
509 (dev->state & DEV_DISCONNECTED));
510 if (ret) {
511 up(&dev->fileop_lock);
512 return ret;
513 }
514 if (dev->state & DEV_DISCONNECTED) {
515 up(&dev->fileop_lock);
516 return -ENODEV;
517 }
518 }
519
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800520 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800521
522 spin_lock_irqsave(&dev->queue_lock, lock_flags);
523 list_for_each_entry(i, &dev->outqueue, frame)
524 i->state = F_UNUSED;
525 INIT_LIST_HEAD(&dev->outqueue);
526 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
527
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800528 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800529
530 if (count > f->buf.length)
531 count = f->buf.length;
532
533 if (copy_to_user(buf, f->bufmem, count)) {
534 up(&dev->fileop_lock);
535 return -EFAULT;
536 }
537 *f_pos += count;
538
539 up(&dev->fileop_lock);
540
541 return count;
542}
543
544/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800545 * em28xx_v4l2_poll()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800546 * will allocate buffers when called for the first time
547 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800548static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800549{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800550 unsigned int mask = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800551 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800552
553 if (down_interruptible(&dev->fileop_lock))
554 return POLLERR;
555
556 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800557 em28xx_videodbg("device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800558 } else if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800559 em28xx_videodbg("device is misconfigured; close and open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800560 } else {
561 if (dev->io == IO_NONE) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800562 if (!em28xx_request_buffers
563 (dev, EM28XX_NUM_READ_FRAMES)) {
564 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800565 ("poll() failed, not enough memory\n");
566 } else {
567 dev->io = IO_READ;
568 dev->stream = STREAM_ON;
569 }
570 }
571
572 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800573 em28xx_queue_unusedframes(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800574 poll_wait(filp, &dev->wait_frame, wait);
575
576 if (!list_empty(&dev->outqueue))
577 mask |= POLLIN | POLLRDNORM;
578
579 up(&dev->fileop_lock);
580
581 return mask;
582 }
583 }
584
585 up(&dev->fileop_lock);
586 return POLLERR;
587}
588
589/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800590 * em28xx_vm_open()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800591 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800592static void em28xx_vm_open(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800593{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800594 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800595 f->vma_use_count++;
596}
597
598/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800599 * em28xx_vm_close()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800600 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800601static void em28xx_vm_close(struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800602{
603 /* NOTE: buffers are not freed here */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800604 struct em28xx_frame_t *f = vma->vm_private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800605 f->vma_use_count--;
606}
607
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800608static struct vm_operations_struct em28xx_vm_ops = {
609 .open = em28xx_vm_open,
610 .close = em28xx_vm_close,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800611};
612
613/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800614 * em28xx_v4l2_mmap()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800615 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800616static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800617{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800618 unsigned long size = vma->vm_end - vma->vm_start,
Sascha Sommer3639c862005-12-12 00:37:30 -0800619 start = vma->vm_start;
620 void *pos;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800621 u32 i;
Markus Rechberger9c755412005-11-08 21:37:52 -0800622
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800623 struct em28xx *dev = filp->private_data;
Markus Rechberger9c755412005-11-08 21:37:52 -0800624
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800625 if (down_interruptible(&dev->fileop_lock))
626 return -ERESTARTSYS;
627
628 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800629 em28xx_videodbg("mmap: device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800630 up(&dev->fileop_lock);
631 return -ENODEV;
632 }
633
634 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800635 em28xx_videodbg ("mmap: Device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800636 "open it again\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800637 up(&dev->fileop_lock);
638 return -EIO;
639 }
640
641 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) ||
642 size != PAGE_ALIGN(dev->frame[0].buf.length)) {
643 up(&dev->fileop_lock);
644 return -EINVAL;
645 }
646
647 for (i = 0; i < dev->num_frames; i++) {
648 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
649 break;
650 }
651 if (i == dev->num_frames) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800652 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800653 up(&dev->fileop_lock);
654 return -EINVAL;
655 }
656
657 /* VM_IO is eventually going to replace PageReserved altogether */
658 vma->vm_flags |= VM_IO;
659 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
660
Sascha Sommer3639c862005-12-12 00:37:30 -0800661 pos = dev->frame[i].bufmem;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800662 while (size > 0) { /* size is page-aligned */
Sascha Sommer3639c862005-12-12 00:37:30 -0800663 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
664 em28xx_videodbg("mmap: vm_insert_page failed\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800665 up(&dev->fileop_lock);
666 return -EAGAIN;
667 }
668 start += PAGE_SIZE;
669 pos += PAGE_SIZE;
670 size -= PAGE_SIZE;
671 }
672
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800673 vma->vm_ops = &em28xx_vm_ops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800674 vma->vm_private_data = &dev->frame[i];
675
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800676 em28xx_vm_open(vma);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800677 up(&dev->fileop_lock);
678 return 0;
679}
680
681/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800682 * em28xx_get_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800683 * return the current saturation, brightness or contrast, mute state
684 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800685static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800686{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800687 switch (ctrl->id) {
688 case V4L2_CID_AUDIO_MUTE:
689 ctrl->value = dev->mute;
690 return 0;
691 case V4L2_CID_AUDIO_VOLUME:
692 ctrl->value = dev->volume;
693 return 0;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200694 default:
695 return -EINVAL;
696 }
697}
698
699/*FIXME: should be moved to saa711x */
700static int saa711x_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
701{
702 s32 tmp;
703 switch (ctrl->id) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800704 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800705 if ((tmp = em28xx_brightness_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800706 return -EIO;
707 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
708 return 0;
709 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800710 if ((ctrl->value = em28xx_contrast_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800711 return -EIO;
712 return 0;
713 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800714 if ((ctrl->value = em28xx_saturation_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800715 return -EIO;
716 return 0;
717 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800718 if ((tmp = em28xx_v_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800719 return -EIO;
720 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
721 return 0;
722 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800723 if ((tmp = em28xx_u_balance_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800724 return -EIO;
725 ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */
726 return 0;
727 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800728 if ((ctrl->value = em28xx_gamma_get(dev)) < 0)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800729 return -EIO;
730 return 0;
731 default:
732 return -EINVAL;
733 }
734}
735
736/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800737 * em28xx_set_ctrl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800738 * mute or set new saturation, brightness or contrast
739 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800740static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800741{
742 switch (ctrl->id) {
743 case V4L2_CID_AUDIO_MUTE:
744 if (ctrl->value != dev->mute) {
745 dev->mute = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800746 em28xx_audio_usb_mute(dev, ctrl->value);
747 return em28xx_audio_analog_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800748 }
749 return 0;
750 case V4L2_CID_AUDIO_VOLUME:
751 dev->volume = ctrl->value;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800752 return em28xx_audio_analog_set(dev);
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -0200753 default:
754 return -EINVAL;
755 }
756}
757
758/*FIXME: should be moved to saa711x */
759static int saa711x_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
760{
761 switch (ctrl->id) {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800762 case V4L2_CID_BRIGHTNESS:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800763 return em28xx_brightness_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800764 case V4L2_CID_CONTRAST:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800765 return em28xx_contrast_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800766 case V4L2_CID_SATURATION:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800767 return em28xx_saturation_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800768 case V4L2_CID_RED_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800769 return em28xx_v_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800770 case V4L2_CID_BLUE_BALANCE:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800771 return em28xx_u_balance_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800772 case V4L2_CID_GAMMA:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800773 return em28xx_gamma_set(dev, ctrl->value);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800774 default:
775 return -EINVAL;
776 }
777}
778
779/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800780 * em28xx_stream_interrupt()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800781 * stops streaming
782 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800783static int em28xx_stream_interrupt(struct em28xx *dev)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800784{
785 int ret = 0;
786
787 /* stop reading from the device */
788
789 dev->stream = STREAM_INTERRUPT;
790 ret = wait_event_timeout(dev->wait_stream,
791 (dev->stream == STREAM_OFF) ||
792 (dev->state & DEV_DISCONNECTED),
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800793 EM28XX_URB_TIMEOUT);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800794 if (dev->state & DEV_DISCONNECTED)
795 return -ENODEV;
796 else if (ret) {
797 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800798 em28xx_videodbg("device is misconfigured; close and "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -0800799 "open /dev/video%d again\n", dev->vdev->minor);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800800 return ret;
801 }
802
803 return 0;
804}
805
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800806static int em28xx_set_norm(struct em28xx *dev, int width, int height)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800807{
808 unsigned int hscale, vscale;
809 unsigned int maxh, maxw;
810
811 maxw = norm_maxw(dev);
812 maxh = norm_maxh(dev);
813
814 /* width must even because of the YUYV format */
815 /* height must be even because of interlacing */
816 height &= 0xfffe;
817 width &= 0xfffe;
818
819 if (height < 32)
820 height = 32;
821 if (height > maxh)
822 height = maxh;
823 if (width < 48)
824 width = 48;
825 if (width > maxw)
826 width = maxw;
827
828 if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
829 hscale = 0x3fff;
830 width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
831
832 if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
833 vscale = 0x3fff;
834 height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
835
836 /* set new image size */
837 dev->width = width;
838 dev->height = height;
839 dev->frame_size = dev->width * dev->height * 2;
840 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
841 dev->bytesperline = dev->width * 2;
842 dev->hscale = hscale;
843 dev->vscale = vscale;
844
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800845 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800846
847 return 0;
848}
849
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800850/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800851 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800852 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800853 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800854 * copying is done already, arg is a kernel pointer.
855 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800856static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
857 struct em28xx *dev, unsigned int cmd, void *arg,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800858 v4l2_kioctl driver_ioctl)
859{
860 int ret;
861
862 switch (cmd) {
863 /* ---------- tv norms ---------- */
864 case VIDIOC_ENUMSTD:
865 {
866 struct v4l2_standard *e = arg;
867 unsigned int i;
868
869 i = e->index;
870 if (i >= TVNORMS)
871 return -EINVAL;
872 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
873 tvnorms[e->index].name);
874 e->index = i;
875 if (ret < 0)
876 return ret;
877 return 0;
878 }
879 case VIDIOC_G_STD:
880 {
881 v4l2_std_id *id = arg;
882
883 *id = dev->tvnorm->id;
884 return 0;
885 }
886 case VIDIOC_S_STD:
887 {
888 v4l2_std_id *id = arg;
889 unsigned int i;
890
891 for (i = 0; i < TVNORMS; i++)
892 if (*id == tvnorms[i].id)
893 break;
894 if (i == TVNORMS)
895 for (i = 0; i < TVNORMS; i++)
896 if (*id & tvnorms[i].id)
897 break;
898 if (i == TVNORMS)
899 return -EINVAL;
900
901 down(&dev->lock);
902 dev->tvnorm = &tvnorms[i];
903
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800904 em28xx_set_norm(dev, dev->width, dev->height);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800905
906/*
907 dev->width=norm_maxw(dev);
908 dev->height=norm_maxh(dev);
909 dev->frame_size=dev->width*dev->height*2;
910 dev->field_size=dev->frame_size>>1;
911 dev->bytesperline=dev->width*2;
912 dev->hscale=0;
913 dev->vscale=0;
914
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800915 em28xx_resolution_set(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800916*/
917/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800918 em28xx_uninit_isoc(dev);
919 em28xx_set_alternate(dev);
920 em28xx_capture_start(dev, 1);
921 em28xx_resolution_set(dev);
922 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800923*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800924 em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800925 &tvnorms[i].mode);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800926 em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800927 &dev->tvnorm->id);
928
929 up(&dev->lock);
930
931 return 0;
932 }
933
934 /* ------ input switching ---------- */
935 case VIDIOC_ENUMINPUT:
936 {
937 struct v4l2_input *i = arg;
938 unsigned int n;
939 static const char *iname[] = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800940 [EM28XX_VMUX_COMPOSITE1] = "Composite1",
941 [EM28XX_VMUX_COMPOSITE2] = "Composite2",
942 [EM28XX_VMUX_COMPOSITE3] = "Composite3",
943 [EM28XX_VMUX_COMPOSITE4] = "Composite4",
944 [EM28XX_VMUX_SVIDEO] = "S-Video",
945 [EM28XX_VMUX_TELEVISION] = "Television",
946 [EM28XX_VMUX_CABLE] = "Cable TV",
947 [EM28XX_VMUX_DVB] = "DVB",
948 [EM28XX_VMUX_DEBUG] = "for debug only",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800949 };
950
951 n = i->index;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800952 if (n >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800953 return -EINVAL;
954 if (0 == INPUT(n)->type)
955 return -EINVAL;
956 memset(i, 0, sizeof(*i));
957 i->index = n;
958 i->type = V4L2_INPUT_TYPE_CAMERA;
959 strcpy(i->name, iname[INPUT(n)->type]);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800960 if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
961 (EM28XX_VMUX_CABLE == INPUT(n)->type))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800962 i->type = V4L2_INPUT_TYPE_TUNER;
963 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
964 i->std |= tvnorms[n].id;
965 return 0;
966 }
967
968 case VIDIOC_G_INPUT:
969 {
970 int *i = arg;
971 *i = dev->ctl_input;
972
973 return 0;
974 }
975
976 case VIDIOC_S_INPUT:
977 {
978 int *index = arg;
979
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -0800980 if (*index >= MAX_EM28XX_INPUT)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -0800981 return -EINVAL;
982 if (0 == INPUT(*index)->type)
983 return -EINVAL;
984
985 down(&dev->lock);
986 video_mux(dev, *index);
987 up(&dev->lock);
988
989 return 0;
990 }
991
992 case VIDIOC_G_AUDIO:
993 {
994 struct v4l2_audio *a = arg;
995 unsigned int index = a->index;
996
997 if (a->index > 1)
998 return -EINVAL;
999 memset(a, 0, sizeof(*a));
1000 index = dev->ctl_ainput;
1001
1002 if (index == 0) {
1003 strcpy(a->name, "Television");
1004 } else {
1005 strcpy(a->name, "Line In");
1006 }
1007 a->capability = V4L2_AUDCAP_STEREO;
1008 a->index = index;
1009 return 0;
1010 }
1011
1012 case VIDIOC_S_AUDIO:
1013 {
1014 struct v4l2_audio *a = arg;
1015 if (a->index != dev->ctl_ainput)
1016 return -EINVAL;
1017
1018 return 0;
1019 }
1020
1021 /* --- controls ---------------------------------------------- */
1022 case VIDIOC_QUERYCTRL:
1023 {
1024 struct v4l2_queryctrl *qc = arg;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001025 int i, id=qc->id;
1026
1027 memset(qc,0,sizeof(*qc));
1028 qc->id=id;
1029
1030 if (!dev->has_msp34xx) {
1031 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1032 if (qc->id && qc->id == em28xx_qctrl[i].id) {
1033 memcpy(qc, &(em28xx_qctrl[i]),
1034 sizeof(*qc));
1035 return 0;
1036 }
1037 }
1038 }
1039 if (dev->decoder == EM28XX_TVP5150) {
1040 em28xx_i2c_call_clients(dev,cmd,qc);
1041 if (qc->type)
1042 return 0;
1043 else
1044 return -EINVAL;
1045 }
1046 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1047 if (qc->id && qc->id == saa711x_qctrl[i].id) {
1048 memcpy(qc, &(saa711x_qctrl[i]),
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001049 sizeof(*qc));
1050 return 0;
1051 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001052 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001053
1054 return -EINVAL;
1055 }
1056
1057 case VIDIOC_G_CTRL:
1058 {
1059 struct v4l2_control *ctrl = arg;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001060 int retval=-EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001061
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001062 if (!dev->has_msp34xx)
1063 retval=em28xx_get_ctrl(dev, ctrl);
1064 if (retval==-EINVAL) {
1065 if (dev->decoder == EM28XX_TVP5150) {
1066 em28xx_i2c_call_clients(dev,cmd,arg);
1067 return 0;
1068 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001069
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001070 return saa711x_get_ctrl(dev, ctrl);
1071 } else return retval;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001072 }
1073
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001074 case VIDIOC_S_CTRL:
1075 {
1076 struct v4l2_control *ctrl = arg;
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001077 u8 i;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001078
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001079 if (!dev->has_msp34xx){
1080 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1081 if (ctrl->id == em28xx_qctrl[i].id) {
1082 if (ctrl->value <
1083 em28xx_qctrl[i].minimum
1084 || ctrl->value >
1085 em28xx_qctrl[i].maximum)
1086 return -ERANGE;
1087 return em28xx_set_ctrl(dev, ctrl);
1088 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001089 }
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001090 }
1091
1092 if (dev->decoder == EM28XX_TVP5150) {
1093 em28xx_i2c_call_clients(dev,cmd,arg);
1094 return 0;
Hans Verkuil7865c442006-01-09 15:25:14 -02001095 } else if (!dev->has_msp34xx) {
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001096 for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
1097 if (ctrl->id == em28xx_qctrl[i].id) {
1098 if (ctrl->value <
1099 em28xx_qctrl[i].minimum
1100 || ctrl->value >
1101 em28xx_qctrl[i].maximum)
1102 return -ERANGE;
1103 return em28xx_set_ctrl(dev, ctrl);
1104 }
1105 }
1106 for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
1107 if (ctrl->id == saa711x_qctrl[i].id) {
1108 if (ctrl->value <
1109 saa711x_qctrl[i].minimum
1110 || ctrl->value >
1111 saa711x_qctrl[i].maximum)
1112 return -ERANGE;
1113 return saa711x_set_ctrl(dev, ctrl);
1114 }
1115 }
1116 }
1117
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001118 return -EINVAL;
1119 }
1120
1121 /* --- tuner ioctls ------------------------------------------ */
1122 case VIDIOC_G_TUNER:
1123 {
1124 struct v4l2_tuner *t = arg;
1125 int status = 0;
1126
1127 if (0 != t->index)
1128 return -EINVAL;
1129
1130 memset(t, 0, sizeof(*t));
1131 strcpy(t->name, "Tuner");
1132 t->type = V4L2_TUNER_ANALOG_TV;
1133 t->capability = V4L2_TUNER_CAP_NORM;
1134 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1135/* t->signal = 0xffff;*/
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001136/* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001137 /* No way to get signal strength? */
1138 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001139 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001140 &status);
1141 up(&dev->lock);
1142 t->signal =
1143 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1144
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001145 em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001146 t->afc);
1147 return 0;
1148 }
1149 case VIDIOC_S_TUNER:
1150 {
1151 struct v4l2_tuner *t = arg;
1152 int status = 0;
1153
1154 if (0 != t->index)
1155 return -EINVAL;
1156 memset(t, 0, sizeof(*t));
1157 strcpy(t->name, "Tuner");
1158 t->type = V4L2_TUNER_ANALOG_TV;
1159 t->capability = V4L2_TUNER_CAP_NORM;
1160 t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
1161/* t->signal = 0xffff; */
1162 /* No way to get signal strength? */
1163 down(&dev->lock);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001164 em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001165 &status);
1166 up(&dev->lock);
1167 t->signal =
1168 (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
1169
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001170 em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001171 t->signal, t->afc);
1172 return 0;
1173 }
1174 case VIDIOC_G_FREQUENCY:
1175 {
1176 struct v4l2_frequency *f = arg;
1177
1178 memset(f, 0, sizeof(*f));
1179 f->type = V4L2_TUNER_ANALOG_TV;
1180 f->frequency = dev->ctl_freq;
1181
1182 return 0;
1183 }
1184 case VIDIOC_S_FREQUENCY:
1185 {
1186 struct v4l2_frequency *f = arg;
1187
1188 if (0 != f->tuner)
1189 return -EINVAL;
1190
1191 if (V4L2_TUNER_ANALOG_TV != f->type)
1192 return -EINVAL;
1193
1194 down(&dev->lock);
1195 dev->ctl_freq = f->frequency;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001196 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001197 up(&dev->lock);
1198 return 0;
1199 }
1200
1201 case VIDIOC_CROPCAP:
1202 {
1203 struct v4l2_cropcap *cc = arg;
1204
1205 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001206 return -EINVAL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001207 cc->bounds.left = 0;
1208 cc->bounds.top = 0;
1209 cc->bounds.width = dev->width;
1210 cc->bounds.height = dev->height;
1211 cc->defrect = cc->bounds;
1212 cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
1213 cc->pixelaspect.denominator = 59;
1214 return 0;
1215 }
1216 case VIDIOC_STREAMON:
1217 {
1218 int *type = arg;
1219
1220 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1221 || dev->io != IO_MMAP)
1222 return -EINVAL;
1223
1224 if (list_empty(&dev->inqueue))
1225 return -EINVAL;
1226
1227 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
1228
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001229 em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001230
1231 return 0;
1232 }
1233 case VIDIOC_STREAMOFF:
1234 {
1235 int *type = arg;
1236 int ret;
1237
1238 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1239 || dev->io != IO_MMAP)
1240 return -EINVAL;
1241
1242 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001243 em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001244 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001245 return ret;
1246 }
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001247 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001248
1249 return 0;
1250 }
1251 default:
1252 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1253 driver_ioctl);
1254 }
1255 return 0;
1256}
1257
1258/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001259 * em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001260 * This function is _not_ called directly, but from
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001261 * em28xx_v4l2_ioctl. Userspace
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001262 * copying is done already, arg is a kernel pointer.
1263 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001264static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001265 unsigned int cmd, void *arg)
1266{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001267 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001268
1269 if (!dev)
1270 return -ENODEV;
1271
1272 if (video_debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -02001273 v4l_print_ioctl(dev->name,cmd);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001274
1275 switch (cmd) {
1276
1277 /* --- capabilities ------------------------------------------ */
1278 case VIDIOC_QUERYCAP:
1279 {
1280 struct v4l2_capability *cap = arg;
1281
1282 memset(cap, 0, sizeof(*cap));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001283 strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
1284 strlcpy(cap->card, em28xx_boards[dev->model].name,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001285 sizeof(cap->card));
1286 strlcpy(cap->bus_info, dev->udev->dev.bus_id,
1287 sizeof(cap->bus_info));
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001288 cap->version = EM28XX_VERSION_CODE;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001289 cap->capabilities =
1290 V4L2_CAP_VIDEO_CAPTURE |
1291 V4L2_CAP_AUDIO |
1292 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1293 if (dev->has_tuner)
1294 cap->capabilities |= V4L2_CAP_TUNER;
1295 return 0;
1296 }
1297
1298 /* --- capture ioctls ---------------------------------------- */
1299 case VIDIOC_ENUM_FMT:
1300 {
1301 struct v4l2_fmtdesc *fmtd = arg;
1302
1303 if (fmtd->index != 0)
1304 return -EINVAL;
1305 memset(fmtd, 0, sizeof(*fmtd));
1306 fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1307 strcpy(fmtd->description, "Packed YUY2");
1308 fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
1309 memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
1310 return 0;
1311 }
1312
1313 case VIDIOC_G_FMT:
1314 {
1315 struct v4l2_format *format = arg;
1316
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001317 em28xx_videodbg("VIDIOC_G_FMT: type=%s\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001318 format->type ==
1319 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1320 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1321 V4L2_BUF_TYPE_VBI_CAPTURE ?
1322 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1323 "not supported");
1324
1325 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1326 return -EINVAL;
1327
1328 format->fmt.pix.width = dev->width;
1329 format->fmt.pix.height = dev->height;
1330 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1331 format->fmt.pix.bytesperline = dev->bytesperline;
1332 format->fmt.pix.sizeimage = dev->frame_size;
1333 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1334 format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
1335
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001336 em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001337 dev->height);
1338 return 0;
1339 }
1340
1341 case VIDIOC_TRY_FMT:
1342 case VIDIOC_S_FMT:
1343 {
1344 struct v4l2_format *format = arg;
1345 u32 i;
1346 int ret = 0;
1347 int width = format->fmt.pix.width;
1348 int height = format->fmt.pix.height;
1349 unsigned int hscale, vscale;
1350 unsigned int maxh, maxw;
1351
1352 maxw = norm_maxw(dev);
1353 maxh = norm_maxh(dev);
1354
1355/* int both_fields; */
1356
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001357 em28xx_videodbg("%s: type=%s\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001358 cmd ==
1359 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1360 "VIDIOC_S_FMT",
1361 format->type ==
1362 V4L2_BUF_TYPE_VIDEO_CAPTURE ?
1363 "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type ==
1364 V4L2_BUF_TYPE_VBI_CAPTURE ?
1365 "V4L2_BUF_TYPE_VBI_CAPTURE " :
1366 "not supported");
1367
1368 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1369 return -EINVAL;
1370
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001371 em28xx_videodbg("%s: requested %dx%d\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001372 cmd ==
1373 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1374 "VIDIOC_S_FMT", format->fmt.pix.width,
1375 format->fmt.pix.height);
1376
1377 /* FIXME: Move some code away from here */
1378 /* width must even because of the YUYV format */
1379 /* height must be even because of interlacing */
1380 height &= 0xfffe;
1381 width &= 0xfffe;
1382
1383 if (height < 32)
1384 height = 32;
1385 if (height > maxh)
1386 height = maxh;
1387 if (width < 48)
1388 width = 48;
1389 if (width > maxw)
1390 width = maxw;
1391
Sascha Sommer74458e62005-11-08 21:37:33 -08001392 if(dev->is_em2800){
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001393 /* the em2800 can only scale down to 50% */
Sascha Sommer74458e62005-11-08 21:37:33 -08001394 if(height % (maxh / 2))
1395 height=maxh;
1396 if(width % (maxw / 2))
1397 width=maxw;
Sascha Sommer52c02fc2005-11-08 21:38:10 -08001398 /* according to empiatech support */
1399 /* the MaxPacketSize is to small to support */
1400 /* framesizes larger than 640x480 @ 30 fps */
1401 /* or 640x576 @ 25 fps. As this would cut */
1402 /* of a part of the image we prefer */
1403 /* 360x576 or 360x480 for now */
Sascha Sommer74458e62005-11-08 21:37:33 -08001404 if(width == maxw && height == maxh)
1405 width /= 2;
1406 }
1407
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001408 if ((hscale =
1409 (((unsigned long)maxw) << 12) / width - 4096L) >=
1410 0x4000)
1411 hscale = 0x3fff;
1412 width =
1413 (((unsigned long)maxw) << 12) / (hscale + 4096L);
1414
1415 if ((vscale =
1416 (((unsigned long)maxh) << 12) / height - 4096L) >=
1417 0x4000)
1418 vscale = 0x3fff;
1419 height =
1420 (((unsigned long)maxh) << 12) / (vscale + 4096L);
1421
1422 format->fmt.pix.width = width;
1423 format->fmt.pix.height = height;
1424 format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
1425 format->fmt.pix.bytesperline = width * 2;
1426 format->fmt.pix.sizeimage = width * 2 * height;
1427 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1428 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1429
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001430 em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001431 cmd ==
1432 VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" :
1433 "VIDIOC_S_FMT", format->fmt.pix.width,
1434 format->fmt.pix.height, hscale, vscale);
1435
1436 if (cmd == VIDIOC_TRY_FMT)
1437 return 0;
1438
1439 for (i = 0; i < dev->num_frames; i++)
1440 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001441 em28xx_videodbg("VIDIOC_S_FMT failed. "
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001442 "Unmap the buffers first.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001443 return -EINVAL;
1444 }
1445
1446 /* stop io in case it is already in progress */
1447 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001448 em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001449 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001450 return ret;
1451 }
1452
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001453 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001454 dev->io = IO_NONE;
1455
1456 /* set new image size */
1457 dev->width = width;
1458 dev->height = height;
1459 dev->frame_size = dev->width * dev->height * 2;
1460 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1461 dev->bytesperline = dev->width * 2;
1462 dev->hscale = hscale;
1463 dev->vscale = vscale;
1464/* dev->both_fileds = both_fileds; */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001465 em28xx_uninit_isoc(dev);
1466 em28xx_set_alternate(dev);
1467 em28xx_capture_start(dev, 1);
1468 em28xx_resolution_set(dev);
1469 em28xx_init_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001470
1471 return 0;
1472 }
1473
1474 /* --- streaming capture ------------------------------------- */
1475 case VIDIOC_REQBUFS:
1476 {
1477 struct v4l2_requestbuffers *rb = arg;
1478 u32 i;
1479 int ret;
1480
1481 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1482 rb->memory != V4L2_MEMORY_MMAP)
1483 return -EINVAL;
1484
1485 if (dev->io == IO_READ) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001486 em28xx_videodbg ("method is set to read;"
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001487 " close and open the device again to"
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001488 " choose the mmap I/O method\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001489 return -EINVAL;
1490 }
1491
1492 for (i = 0; i < dev->num_frames; i++)
1493 if (dev->frame[i].vma_use_count) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001494 em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001495 return -EINVAL;
1496 }
1497
1498 if (dev->stream == STREAM_ON) {
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001499 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001500 if ((ret = em28xx_stream_interrupt(dev)))
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001501 return ret;
1502 }
1503
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001504 em28xx_empty_framequeues(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001505
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001506 em28xx_release_buffers(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001507 if (rb->count)
1508 rb->count =
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001509 em28xx_request_buffers(dev, rb->count);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001510
1511 dev->frame_current = NULL;
1512
Mauro Carvalho Chehabeac94352005-11-08 21:38:43 -08001513 em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001514 rb->count);
1515 dev->io = rb->count ? IO_MMAP : IO_NONE;
1516 return 0;
1517 }
1518
1519 case VIDIOC_QUERYBUF:
1520 {
1521 struct v4l2_buffer *b = arg;
1522
1523 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1524 b->index >= dev->num_frames || dev->io != IO_MMAP)
1525 return -EINVAL;
1526
1527 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1528
1529 if (dev->frame[b->index].vma_use_count) {
1530 b->flags |= V4L2_BUF_FLAG_MAPPED;
1531 }
1532 if (dev->frame[b->index].state == F_DONE)
1533 b->flags |= V4L2_BUF_FLAG_DONE;
1534 else if (dev->frame[b->index].state != F_UNUSED)
1535 b->flags |= V4L2_BUF_FLAG_QUEUED;
1536 return 0;
1537 }
1538 case VIDIOC_QBUF:
1539 {
1540 struct v4l2_buffer *b = arg;
1541 unsigned long lock_flags;
1542
1543 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1544 b->index >= dev->num_frames || dev->io != IO_MMAP) {
1545 return -EINVAL;
1546 }
1547
1548 if (dev->frame[b->index].state != F_UNUSED) {
1549 return -EAGAIN;
1550 }
1551 dev->frame[b->index].state = F_QUEUED;
1552
1553 /* add frame to fifo */
1554 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1555 list_add_tail(&dev->frame[b->index].frame,
1556 &dev->inqueue);
1557 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1558
1559 return 0;
1560 }
1561 case VIDIOC_DQBUF:
1562 {
1563 struct v4l2_buffer *b = arg;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001564 struct em28xx_frame_t *f;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001565 unsigned long lock_flags;
1566 int ret = 0;
1567
1568 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
1569 || dev->io != IO_MMAP)
1570 return -EINVAL;
1571
1572 if (list_empty(&dev->outqueue)) {
1573 if (dev->stream == STREAM_OFF)
1574 return -EINVAL;
1575 if (filp->f_flags & O_NONBLOCK)
1576 return -EAGAIN;
1577 ret = wait_event_interruptible
1578 (dev->wait_frame,
1579 (!list_empty(&dev->outqueue)) ||
1580 (dev->state & DEV_DISCONNECTED));
1581 if (ret)
1582 return ret;
1583 if (dev->state & DEV_DISCONNECTED)
1584 return -ENODEV;
1585 }
1586
1587 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1588 f = list_entry(dev->outqueue.next,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001589 struct em28xx_frame_t, frame);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001590 list_del(dev->outqueue.next);
1591 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1592
1593 f->state = F_UNUSED;
1594 memcpy(b, &f->buf, sizeof(*b));
1595
1596 if (f->vma_use_count)
1597 b->flags |= V4L2_BUF_FLAG_MAPPED;
1598
1599 return 0;
1600 }
1601 default:
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001602 return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
1603 em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001604 }
1605 return 0;
1606}
1607
1608/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001609 * em28xx_v4l2_ioctl()
1610 * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001611 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001612static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001613 unsigned int cmd, unsigned long arg)
1614{
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001615 int ret = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001616 struct em28xx *dev = filp->private_data;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001617
1618 if (down_interruptible(&dev->fileop_lock))
1619 return -ERESTARTSYS;
1620
1621 if (dev->state & DEV_DISCONNECTED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001622 em28xx_errdev("v4l2 ioctl: device not present\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001623 up(&dev->fileop_lock);
1624 return -ENODEV;
1625 }
1626
1627 if (dev->state & DEV_MISCONFIGURED) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001628 em28xx_errdev
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001629 ("v4l2 ioctl: device is misconfigured; close and open it again\n");
1630 up(&dev->fileop_lock);
1631 return -EIO;
1632 }
1633
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001634 ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001635
1636 up(&dev->fileop_lock);
1637
1638 return ret;
1639}
1640
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001641static struct file_operations em28xx_v4l_fops = {
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001642 .owner = THIS_MODULE,
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001643 .open = em28xx_v4l2_open,
1644 .release = em28xx_v4l2_close,
1645 .ioctl = em28xx_v4l2_ioctl,
1646 .read = em28xx_v4l2_read,
1647 .poll = em28xx_v4l2_poll,
1648 .mmap = em28xx_v4l2_mmap,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001649 .llseek = no_llseek,
Mauro Carvalho Chehab17cbe2e2006-01-09 15:24:58 -02001650 .compat_ioctl = v4l_compat_ioctl32,
1651
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001652};
1653
1654/******************************** usb interface *****************************************/
1655
1656/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001657 * em28xx_init_dev()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001658 * allocates and inits the device structs, registers i2c bus and v4l device
1659 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001660static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001661 int minor, int model)
1662{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001663 struct em28xx *dev = *devhandle;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001664 int retval = -ENOMEM;
1665 int errCode, i;
1666 unsigned int maxh, maxw;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001667
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001668 dev->udev = udev;
1669 dev->model = model;
1670 init_MUTEX(&dev->lock);
1671 init_waitqueue_head(&dev->open);
1672
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001673 dev->em28xx_write_regs = em28xx_write_regs;
1674 dev->em28xx_read_reg = em28xx_read_reg;
1675 dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
1676 dev->em28xx_write_regs_req = em28xx_write_regs_req;
1677 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1678 dev->is_em2800 = em28xx_boards[model].is_em2800;
1679 dev->has_tuner = em28xx_boards[model].has_tuner;
1680 dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
1681 dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
1682 dev->decoder = em28xx_boards[model].decoder;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001683
1684 if (tuner >= 0)
1685 dev->tuner_type = tuner;
1686 else
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001687 dev->tuner_type = em28xx_boards[model].tuner_type;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001688
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001689 dev->video_inputs = em28xx_boards[model].vchannels;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001690
1691 for (i = 0; i < TVNORMS; i++)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001692 if (em28xx_boards[model].norm == tvnorms[i].mode)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001693 break;
1694 if (i == TVNORMS)
1695 i = 0;
1696
1697 dev->tvnorm = &tvnorms[i]; /* set default norm */
1698
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001699 em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001700
1701 maxw = norm_maxw(dev);
1702 maxh = norm_maxh(dev);
1703
1704 /* set default image size */
1705 dev->width = maxw;
1706 dev->height = maxh;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001707 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001708 dev->field_size = dev->width * dev->height;
1709 dev->frame_size =
1710 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1711 dev->bytesperline = dev->width * 2;
1712 dev->hscale = 0;
1713 dev->vscale = 0;
1714 dev->ctl_input = 2;
1715
1716 /* setup video picture settings for saa7113h */
1717 memset(&dev->vpic, 0, sizeof(dev->vpic));
1718 dev->vpic.colour = 128 << 8;
1719 dev->vpic.hue = 128 << 8;
1720 dev->vpic.brightness = 128 << 8;
1721 dev->vpic.contrast = 192 << 8;
1722 dev->vpic.whiteness = 128 << 8; /* This one isn't used */
1723 dev->vpic.depth = 16;
1724 dev->vpic.palette = VIDEO_PALETTE_YUV422;
1725
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001726#ifdef CONFIG_MODULES
1727 /* request some modules */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001728 if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001729 request_module("saa711x");
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001730 if (dev->decoder == EM28XX_TVP5150)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001731 request_module("tvp5150");
1732 if (dev->has_tuner)
1733 request_module("tuner");
1734 if (dev->tda9887_conf)
1735 request_module("tda9887");
1736#endif
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001737 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001738 if (errCode) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001739 em28xx_errdev("error configuring device\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001740 kfree(dev);
1741 return -ENOMEM;
1742 }
1743
1744 down(&dev->lock);
1745 /* register i2c bus */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001746 em28xx_i2c_register(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001747
1748 /* Do board specific init and eeprom reading */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001749 em28xx_card_setup(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001750
1751 /* configure the device */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001752 em28xx_config_i2c(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001753
1754 up(&dev->lock);
1755
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001756 errCode = em28xx_config(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001757
1758#ifdef CONFIG_MODULES
1759 if (dev->has_msp34xx)
1760 request_module("msp3400");
1761#endif
1762 /* allocate and fill v4l2 device struct */
1763 dev->vdev = video_device_alloc();
1764 if (NULL == dev->vdev) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001765 em28xx_errdev("cannot allocate video_device.\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001766 kfree(dev);
1767 return -ENOMEM;
1768 }
1769
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001770 dev->vdev->type = VID_TYPE_CAPTURE;
1771 if (dev->has_tuner)
1772 dev->vdev->type |= VID_TYPE_TUNER;
1773 dev->vdev->hardware = 0;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001774 dev->vdev->fops = &em28xx_v4l_fops;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001775 dev->vdev->minor = -1;
1776 dev->vdev->dev = &dev->udev->dev;
1777 dev->vdev->release = video_device_release;
1778 snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001779 "em28xx video");
1780 list_add_tail(&dev->devlist,&em28xx_devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001781
1782 /* register v4l2 device */
1783 down(&dev->lock);
1784 if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001785 em28xx_errdev("unable to register video device (error=%i).\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001786 retval);
1787 up(&dev->lock);
Markus Rechberger9c755412005-11-08 21:37:52 -08001788 list_del(&dev->devlist);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001789 video_device_release(dev->vdev);
1790 kfree(dev);
1791 return -ENODEV;
1792 }
1793 if (dev->has_msp34xx) {
1794 /* Send a reset to other chips via gpio */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001795 em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001796 udelay(2500);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001797 em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001798 udelay(2500);
1799
1800 }
1801 video_mux(dev, 0);
1802
1803 up(&dev->lock);
1804
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001805 em28xx_info("V4L2 device registered as /dev/video%d\n",
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001806 dev->vdev->minor);
1807
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001808 return 0;
1809}
1810
1811/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001812 * em28xx_usb_probe()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001813 * checks for supported devices
1814 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001815static int em28xx_usb_probe(struct usb_interface *interface,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001816 const struct usb_device_id *id)
1817{
1818 const struct usb_endpoint_descriptor *endpoint;
1819 struct usb_device *udev;
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001820 struct usb_interface *uif;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001821 struct em28xx *dev = NULL;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001822 int retval = -ENODEV;
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001823 int model,i,nr,ifnum;
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001824
1825 udev = usb_get_dev(interface_to_usbdev(interface));
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001826 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
1827
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001828
1829 /* Don't register audio interfaces */
1830 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001831 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehab91cad0f2005-11-08 21:38:13 -08001832 udev->descriptor.idVendor,udev->descriptor.idProduct,
1833 ifnum,
1834 interface->altsetting[0].desc.bInterfaceClass);
1835 return -ENODEV;
1836 }
1837
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001838 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001839 udev->descriptor.idVendor,udev->descriptor.idProduct,
1840 ifnum,
1841 interface->altsetting[0].desc.bInterfaceClass);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001842
Mauro Carvalho Chehabd5e52652005-11-08 21:37:32 -08001843 endpoint = &interface->cur_altsetting->endpoint[1].desc;
1844
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001845 /* check if the the device has the iso in endpoint at the correct place */
1846 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1847 USB_ENDPOINT_XFER_ISOC) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001848 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001849 return -ENODEV;
1850 }
1851 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001852 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001853 return -ENODEV;
1854 }
1855
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001856 model=id->driver_info;
1857 nr=interface->minor;
1858
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001859 if (nr>EM28XX_MAXBOARDS) {
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001860 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001861 return -ENOMEM;
1862 }
1863
1864 /* allocate memory for our device state and initialize it */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001865 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001866 if (dev == NULL) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001867 em28xx_err(DRIVER_NAME ": out of memory!\n");
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001868 return -ENOMEM;
1869 }
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001870
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001871 /* compute alternate max packet sizes */
1872 uif = udev->actconfig->interface[0];
1873
1874 dev->num_alt=uif->num_altsetting;
1875 printk(DRIVER_NAME ": Alternate settings: %i\n",dev->num_alt);
1876// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
1877 dev->alt_max_pkt_size = kmalloc(32*
1878 dev->num_alt,GFP_KERNEL);
1879 if (dev->alt_max_pkt_size == NULL) {
1880 em28xx_err(DRIVER_NAME ": out of memory!\n");
1881 return -ENOMEM;
1882 }
1883
1884 for (i = 0; i < dev->num_alt ; i++) {
1885 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1886 wMaxPacketSize);
1887 dev->alt_max_pkt_size[i] =
1888 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1889 printk(DRIVER_NAME ": Alternate setting %i, max size= %i\n",i,
1890 dev->alt_max_pkt_size[i]);
1891 }
1892
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001893 snprintf(dev->name, 29, "em28xx #%d", nr);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001894
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001895 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001896 model=card[nr];
1897
1898 if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
1899 printk( "%s: Your board has no eeprom inside it and thus can't\n"
1900 "%s: be autodetected. Please pass card=<n> insmod option to\n"
1901 "%s: workaround that. Redirect complaints to the vendor of\n"
1902 "%s: the TV card. Best regards,\n"
1903 "%s: -- tux\n",
1904 dev->name,dev->name,dev->name,dev->name,dev->name);
1905 printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
1906 dev->name);
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001907 for (i = 0; i < em28xx_bcount; i++) {
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001908 printk("%s: card=%d -> %s\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001909 dev->name, i, em28xx_boards[i].name);
Mauro Carvalho Chehab596d92d2005-11-08 21:37:24 -08001910 }
1911 }
1912
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001913 /* allocate device struct */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001914 retval = em28xx_init_dev(&dev, udev, nr, model);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001915 if (retval)
1916 return retval;
1917
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001918 em28xx_info("Found %s\n", em28xx_boards[model].name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001919
1920 /* save our data pointer in this interface device */
1921 usb_set_intfdata(interface, dev);
1922 return 0;
1923}
1924
1925/*
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001926 * em28xx_usb_disconnect()
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001927 * called when the device gets diconencted
1928 * video device will be unregistered on v4l2_close in case it is still open
1929 */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001930static void em28xx_usb_disconnect(struct usb_interface *interface)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001931{
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001932 struct em28xx *dev = usb_get_intfdata(interface);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001933 usb_set_intfdata(interface, NULL);
1934
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001935/*FIXME: IR should be disconnected */
1936
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001937 if (!dev)
1938 return;
1939
Mauro Carvalho Chehabc0477ad2006-01-09 15:25:14 -02001940
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001941 down_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001942
1943 down(&dev->lock);
1944
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001945 em28xx_info("disconnecting %s\n", dev->vdev->name);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001946
1947 wake_up_interruptible_all(&dev->open);
1948
1949 if (dev->users) {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001950 em28xx_warn
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001951 ("device /dev/video%d is open! Deregistration and memory "
1952 "deallocation are deferred on close.\n", dev->vdev->minor);
1953 dev->state |= DEV_MISCONFIGURED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001954 em28xx_uninit_isoc(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001955 dev->state |= DEV_DISCONNECTED;
1956 wake_up_interruptible(&dev->wait_frame);
1957 wake_up_interruptible(&dev->wait_stream);
1958 } else {
1959 dev->state |= DEV_DISCONNECTED;
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001960 em28xx_release_resources(dev);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001961 }
1962
1963 up(&dev->lock);
1964
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001965 if (!dev->users) {
1966 kfree(dev->alt_max_pkt_size);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001967 kfree(dev);
Mauro Carvalho Chehab9d4d9c02005-11-08 21:38:52 -08001968 }
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001969
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001970 up_write(&em28xx_disconnect);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001971}
1972
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001973static struct usb_driver em28xx_usb_driver = {
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001974 .name = "em28xx",
1975 .probe = em28xx_usb_probe,
1976 .disconnect = em28xx_usb_disconnect,
1977 .id_table = em28xx_id_table,
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001978};
1979
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001980static int __init em28xx_module_init(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001981{
1982 int result;
1983
1984 printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001985 (EM28XX_VERSION_CODE >> 16) & 0xff,
1986 (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001987#ifdef SNAPSHOT
1988 printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
1989 SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
1990#endif
1991
1992 /* register this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001993 result = usb_register(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001994 if (result)
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08001995 em28xx_err(DRIVER_NAME
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08001996 " usb_register failed. Error number %d.\n", result);
1997
1998 return result;
1999}
2000
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002001static void __exit em28xx_module_exit(void)
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002002{
2003 /* deregister this driver with the USB subsystem */
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002004 usb_deregister(&em28xx_usb_driver);
akpm@osdl.orga6c2ba22005-11-08 21:37:07 -08002005}
2006
Mauro Carvalho Chehab3acf2802005-11-08 21:38:27 -08002007module_init(em28xx_module_init);
2008module_exit(em28xx_module_exit);