]>
Commit | Line | Data |
---|---|---|
a6c2ba28 | 1 | /* |
f7abcd38 | 2 | em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices |
a6c2ba28 | 3 | |
f7abcd38 MCC |
4 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
5 | Markus Rechberger <mrechberger@gmail.com> | |
4ac97914 | 6 | Mauro Carvalho Chehab <mchehab@brturbo.com.br> |
f7abcd38 | 7 | Sascha Sommer <saschasommer@freenet.de> |
a6c2ba28 | 8 | |
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> | |
a6c2ba28 | 29 | #include <linux/i2c.h> |
a6c2ba28 | 30 | #include <linux/video_decoder.h> |
31 | ||
f7abcd38 | 32 | #include "em28xx.h" |
d5e52653 | 33 | #include <media/tuner.h> |
a6c2ba28 | 34 | |
f7abcd38 MCC |
35 | #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ |
36 | "Markus Rechberger <mrechberger@gmail.com>, " \ | |
37 | "Mauro Carvalho Chehab <mchehab@brturbo.com.br>, " \ | |
38 | "Sascha Sommer <saschasommer@freenet.de>" | |
a6c2ba28 | 39 | |
f7abcd38 MCC |
40 | #define DRIVER_NAME "em28xx" |
41 | #define DRIVER_DESC "Empia em28xx based USB video device driver" | |
3acf2809 | 42 | #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 0, 1) |
a6c2ba28 | 43 | |
3acf2809 | 44 | #define em28xx_videodbg(fmt, arg...) do {\ |
4ac97914 MCC |
45 | if (video_debug) \ |
46 | printk(KERN_INFO "%s %s :"fmt, \ | |
47 | dev->name, __FUNCTION__ , ##arg); } while (0) | |
a6c2ba28 | 48 | |
49 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
50 | MODULE_DESCRIPTION(DRIVER_DESC); | |
51 | MODULE_LICENSE("GPL"); | |
52 | ||
3acf2809 | 53 | static LIST_HEAD(em28xx_devlist); |
9c75541f | 54 | |
3acf2809 | 55 | static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
596d92d5 MCC |
56 | |
57 | module_param_array(card, int, NULL, 0444); | |
58 | MODULE_PARM_DESC(card,"card type"); | |
59 | ||
a6c2ba28 | 60 | static int tuner = -1; |
61 | module_param(tuner, int, 0444); | |
62 | MODULE_PARM_DESC(tuner, "tuner type"); | |
63 | ||
64 | static unsigned int video_debug = 0; | |
65 | module_param(video_debug,int,0644); | |
66 | MODULE_PARM_DESC(video_debug,"enable debug messages [video]"); | |
67 | ||
68 | /* supported tv norms */ | |
3acf2809 | 69 | static struct em28xx_tvnorm tvnorms[] = { |
a6c2ba28 | 70 | { |
71 | .name = "PAL", | |
72 | .id = V4L2_STD_PAL, | |
73 | .mode = VIDEO_MODE_PAL, | |
74 | }, { | |
75 | .name = "NTSC", | |
76 | .id = V4L2_STD_NTSC, | |
77 | .mode = VIDEO_MODE_NTSC, | |
78 | }, { | |
79 | .name = "SECAM", | |
80 | .id = V4L2_STD_SECAM, | |
81 | .mode = VIDEO_MODE_SECAM, | |
82 | }, { | |
83 | .name = "PAL-M", | |
84 | .id = V4L2_STD_PAL_M, | |
85 | .mode = VIDEO_MODE_PAL, | |
86 | } | |
87 | }; | |
88 | ||
a38a7d4f MR |
89 | static const unsigned char saa7114_i2c_init[] = { |
90 | 0x00,0x00,0x01,0x08,0x02,0xc4,0x03,0x30,0x04,0x90,0x05,0x90,0x06,0xeb,0x07,0xe0, | |
91 | 0x08,0x88,0x09,0x40,0x0a,0x80,0x0b,0x44,0x0c,0x40,0x0d,0x00,0x0e,0x81,0x0f,0x2a, | |
92 | 0x10,0x06,0x11,0x00,0x12,0xc8,0x13,0x80,0x14,0x00,0x15,0x11,0x16,0x01,0x17,0x42, | |
93 | 0x18,0x40,0x19,0x80,0x40,0x00,0x41,0xff,0x42,0xff,0x43,0xff,0x44,0xff,0x45,0xff, | |
94 | 0x46,0xff,0x47,0xff,0x48,0xff,0x49,0xff,0x4a,0xff,0x4b,0xff,0x4c,0xff,0x4d,0xff, | |
95 | 0x4e,0xff,0x4f,0xff,0x50,0xff,0x51,0xff,0x52,0xff,0x53,0xff,0x54,0x5f,0x55,0xff, | |
96 | 0x56,0xff,0x57,0xff,0x58,0x00,0x59,0x47,0x5a,0x03,0x5b,0x03,0x5d,0x3e,0x5e,0x00, | |
97 | 0x80,0x1c,0x83,0x01,0x84,0xa5,0x85,0x10,0x86,0x45,0x87,0x41,0x88,0xf0,0x88,0x00, | |
98 | 0x88,0xf0,0x90,0x00,0x91,0x08,0x92,0x00,0x93,0x80,0x94,0x08,0x95,0x00,0x96,0xc0, | |
99 | 0x97,0x02,0x98,0x13,0x99,0x00,0x9a,0x38,0x9b,0x01,0x9c,0x80,0x9d,0x02,0x9e,0x06, | |
100 | 0x9f,0x01,0xa0,0x01,0xa1,0x00,0xa2,0x00,0xa4,0x80,0xa5,0x36,0xa6,0x36,0xa8,0x67, | |
101 | 0xa9,0x04,0xaa,0x00,0xac,0x33,0xad,0x02,0xae,0x00,0xb0,0xcd,0xb1,0x04,0xb2,0xcd, | |
102 | 0xb3,0x04,0xb4,0x01,0xb8,0x00,0xb9,0x00,0xba,0x00,0xbb,0x00,0xbc,0x00,0xbd,0x00, | |
103 | 0xbe,0x00,0xbf,0x00 | |
104 | }; | |
105 | ||
a6c2ba28 | 106 | #define TVNORMS ARRAY_SIZE(tvnorms) |
107 | ||
108 | /* supported controls */ | |
3acf2809 | 109 | static struct v4l2_queryctrl em28xx_qctrl[] = { |
a6c2ba28 | 110 | { |
111 | .id = V4L2_CID_BRIGHTNESS, | |
112 | .type = V4L2_CTRL_TYPE_INTEGER, | |
113 | .name = "Brightness", | |
114 | .minimum = -128, | |
115 | .maximum = 127, | |
116 | .step = 1, | |
117 | .default_value = 0, | |
118 | .flags = 0, | |
119 | },{ | |
120 | .id = V4L2_CID_CONTRAST, | |
121 | .type = V4L2_CTRL_TYPE_INTEGER, | |
122 | .name = "Contrast", | |
123 | .minimum = 0x0, | |
124 | .maximum = 0x1f, | |
125 | .step = 0x1, | |
126 | .default_value = 0x10, | |
127 | .flags = 0, | |
128 | },{ | |
129 | .id = V4L2_CID_SATURATION, | |
130 | .type = V4L2_CTRL_TYPE_INTEGER, | |
131 | .name = "Saturation", | |
132 | .minimum = 0x0, | |
133 | .maximum = 0x1f, | |
134 | .step = 0x1, | |
135 | .default_value = 0x10, | |
136 | .flags = 0, | |
137 | },{ | |
138 | .id = V4L2_CID_AUDIO_VOLUME, | |
139 | .type = V4L2_CTRL_TYPE_INTEGER, | |
140 | .name = "Volume", | |
141 | .minimum = 0x0, | |
142 | .maximum = 0x1f, | |
143 | .step = 0x1, | |
144 | .default_value = 0x1f, | |
145 | .flags = 0, | |
146 | },{ | |
147 | .id = V4L2_CID_AUDIO_MUTE, | |
148 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
149 | .name = "Mute", | |
150 | .minimum = 0, | |
151 | .maximum = 1, | |
152 | .step = 1, | |
153 | .default_value = 1, | |
154 | .flags = 0, | |
155 | },{ | |
156 | .id = V4L2_CID_RED_BALANCE, | |
157 | .type = V4L2_CTRL_TYPE_INTEGER, | |
158 | .name = "Red chroma balance", | |
159 | .minimum = -128, | |
160 | .maximum = 127, | |
161 | .step = 1, | |
162 | .default_value = 0, | |
163 | .flags = 0, | |
164 | },{ | |
165 | .id = V4L2_CID_BLUE_BALANCE, | |
166 | .type = V4L2_CTRL_TYPE_INTEGER, | |
167 | .name = "Blue chroma balance", | |
168 | .minimum = -128, | |
169 | .maximum = 127, | |
170 | .step = 1, | |
171 | .default_value = 0, | |
172 | .flags = 0, | |
173 | },{ | |
174 | .id = V4L2_CID_GAMMA, | |
175 | .type = V4L2_CTRL_TYPE_INTEGER, | |
176 | .name = "Gamma", | |
177 | .minimum = 0x0, | |
178 | .maximum = 0x3f, | |
179 | .step = 0x1, | |
180 | .default_value = 0x20, | |
181 | .flags = 0, | |
182 | } | |
183 | }; | |
184 | ||
3acf2809 | 185 | static struct usb_driver em28xx_usb_driver; |
a6c2ba28 | 186 | |
3acf2809 MCC |
187 | static DECLARE_MUTEX(em28xx_sysfs_lock); |
188 | static DECLARE_RWSEM(em28xx_disconnect); | |
a6c2ba28 | 189 | |
190 | /********************* v4l2 interface ******************************************/ | |
191 | ||
192 | static inline unsigned long kvirt_to_pa(unsigned long adr) | |
193 | { | |
194 | unsigned long kva, ret; | |
195 | ||
196 | kva = (unsigned long)page_address(vmalloc_to_page((void *)adr)); | |
197 | kva |= adr & (PAGE_SIZE - 1); | |
198 | ret = __pa(kva); | |
199 | return ret; | |
200 | } | |
201 | ||
202 | /* | |
3acf2809 | 203 | * em28xx_config() |
a6c2ba28 | 204 | * inits registers with sane defaults |
205 | */ | |
3acf2809 | 206 | static int em28xx_config(struct em28xx *dev) |
a6c2ba28 | 207 | { |
208 | ||
209 | /* Sets I2C speed to 100 KHz */ | |
3acf2809 | 210 | em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1); |
a6c2ba28 | 211 | |
212 | /* enable vbi capturing */ | |
3acf2809 | 213 | em28xx_audio_usb_mute(dev, 1); |
a6c2ba28 | 214 | dev->mute = 1; /* maybe not the right place... */ |
215 | dev->volume = 0x1f; | |
3acf2809 MCC |
216 | em28xx_audio_analog_set(dev); |
217 | em28xx_audio_analog_setup(dev); | |
218 | em28xx_outfmt_set_yuv422(dev); | |
219 | em28xx_colorlevels_set_default(dev); | |
220 | em28xx_compression_disable(dev); | |
a6c2ba28 | 221 | |
222 | return 0; | |
223 | } | |
224 | ||
225 | /* | |
3acf2809 | 226 | * em28xx_config_i2c() |
a6c2ba28 | 227 | * configure i2c attached devices |
228 | */ | |
3acf2809 | 229 | void em28xx_config_i2c(struct em28xx *dev) |
a6c2ba28 | 230 | { |
231 | struct v4l2_frequency f; | |
3acf2809 | 232 | struct video_decoder_init em28xx_vdi = {.data = NULL }; |
a6c2ba28 | 233 | |
234 | ||
235 | /* configure decoder */ | |
a38a7d4f | 236 | if(dev->model == EM2820_BOARD_MSI_VOX_USB_2){ |
3acf2809 MCC |
237 | em28xx_vdi.data=saa7114_i2c_init; |
238 | em28xx_vdi.len=sizeof(saa7114_i2c_init); | |
a38a7d4f MR |
239 | } |
240 | ||
241 | ||
3acf2809 MCC |
242 | em28xx_i2c_call_clients(dev, DECODER_INIT, &em28xx_vdi); |
243 | em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &dev->ctl_input); | |
244 | /* em28xx_i2c_call_clients(dev,DECODER_SET_PICTURE, &dev->vpic); */ | |
245 | /* em28xx_i2c_call_clients(dev,DECODER_SET_NORM,&dev->tvnorm->id); */ | |
246 | /* em28xx_i2c_call_clients(dev,DECODER_ENABLE_OUTPUT,&output); */ | |
247 | /* em28xx_i2c_call_clients(dev,DECODER_DUMP, NULL); */ | |
a6c2ba28 | 248 | |
249 | /* configure tuner */ | |
250 | f.tuner = 0; | |
251 | f.type = V4L2_TUNER_ANALOG_TV; | |
252 | f.frequency = 9076; /* FIXME:remove magic number */ | |
253 | dev->ctl_freq = f.frequency; | |
3acf2809 | 254 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); |
a6c2ba28 | 255 | |
256 | /* configure tda9887 */ | |
257 | ||
a6c2ba28 | 258 | |
3acf2809 | 259 | /* em28xx_i2c_call_clients(dev,VIDIOC_S_STD,&dev->tvnorm->id); */ |
a6c2ba28 | 260 | } |
261 | ||
262 | /* | |
3acf2809 | 263 | * em28xx_empty_framequeues() |
a6c2ba28 | 264 | * prepare queues for incoming and outgoing frames |
265 | */ | |
3acf2809 | 266 | static void em28xx_empty_framequeues(struct em28xx *dev) |
a6c2ba28 | 267 | { |
268 | u32 i; | |
269 | ||
270 | INIT_LIST_HEAD(&dev->inqueue); | |
271 | INIT_LIST_HEAD(&dev->outqueue); | |
272 | ||
3acf2809 | 273 | for (i = 0; i < EM28XX_NUM_FRAMES; i++) { |
a6c2ba28 | 274 | dev->frame[i].state = F_UNUSED; |
275 | dev->frame[i].buf.bytesused = 0; | |
276 | } | |
277 | } | |
278 | ||
279 | /* | |
3acf2809 | 280 | * em28xx_v4l2_open() |
a6c2ba28 | 281 | * inits the device and starts isoc transfer |
282 | */ | |
3acf2809 | 283 | static int em28xx_v4l2_open(struct inode *inode, struct file *filp) |
a6c2ba28 | 284 | { |
a6c2ba28 | 285 | int minor = iminor(inode); |
a6c2ba28 | 286 | int errCode = 0; |
3acf2809 | 287 | struct em28xx *h,*dev = NULL; |
9c75541f MR |
288 | struct list_head *list; |
289 | ||
3acf2809 MCC |
290 | list_for_each(list,&em28xx_devlist) { |
291 | h = list_entry(list, struct em28xx, devlist); | |
9c75541f MR |
292 | if (h->vdev->minor == minor) { |
293 | dev = h; | |
294 | } | |
295 | } | |
296 | ||
297 | filp->private_data=dev; | |
298 | ||
a6c2ba28 | 299 | |
3acf2809 | 300 | em28xx_videodbg("users=%d", dev->users); |
a6c2ba28 | 301 | |
3acf2809 | 302 | if (!down_read_trylock(&em28xx_disconnect)) |
a6c2ba28 | 303 | return -ERESTARTSYS; |
304 | ||
305 | if (dev->users) { | |
3acf2809 MCC |
306 | em28xx_warn("this driver can be opened only once\n"); |
307 | up_read(&em28xx_disconnect); | |
a6c2ba28 | 308 | return -EBUSY; |
309 | } | |
310 | ||
311 | /* if(dev->vbi_dev->minor == minor){ | |
312 | dev->type=V4L2_BUF_TYPE_VBI_CAPTURE; | |
313 | }*/ | |
314 | if (dev->vdev->minor == minor) { | |
315 | dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
316 | } | |
317 | ||
318 | init_MUTEX(&dev->fileop_lock); /* to 1 == available */ | |
319 | spin_lock_init(&dev->queue_lock); | |
320 | init_waitqueue_head(&dev->wait_frame); | |
321 | init_waitqueue_head(&dev->wait_stream); | |
322 | ||
323 | down(&dev->lock); | |
324 | ||
3acf2809 | 325 | em28xx_set_alternate(dev); |
a6c2ba28 | 326 | |
327 | dev->width = norm_maxw(dev); | |
328 | dev->height = norm_maxh(dev); | |
329 | dev->frame_size = dev->width * dev->height * 2; | |
330 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ | |
331 | dev->bytesperline = dev->width * 2; | |
332 | dev->hscale = 0; | |
333 | dev->vscale = 0; | |
334 | ||
3acf2809 MCC |
335 | em28xx_capture_start(dev, 1); |
336 | em28xx_resolution_set(dev); | |
a6c2ba28 | 337 | |
338 | /* start the transfer */ | |
3acf2809 | 339 | errCode = em28xx_init_isoc(dev); |
a6c2ba28 | 340 | if (errCode) |
341 | goto err; | |
342 | ||
343 | dev->users++; | |
344 | filp->private_data = dev; | |
345 | dev->io = IO_NONE; | |
346 | dev->stream = STREAM_OFF; | |
347 | dev->num_frames = 0; | |
348 | ||
349 | /* prepare queues */ | |
3acf2809 | 350 | em28xx_empty_framequeues(dev); |
a6c2ba28 | 351 | |
352 | dev->state |= DEV_INITIALIZED; | |
353 | ||
354 | err: | |
355 | up(&dev->lock); | |
3acf2809 | 356 | up_read(&em28xx_disconnect); |
a6c2ba28 | 357 | return errCode; |
358 | } | |
359 | ||
360 | /* | |
3acf2809 | 361 | * em28xx_realease_resources() |
a6c2ba28 | 362 | * unregisters the v4l2,i2c and usb devices |
363 | * called when the device gets disconected or at module unload | |
364 | */ | |
3acf2809 | 365 | static void em28xx_release_resources(struct em28xx *dev) |
a6c2ba28 | 366 | { |
3acf2809 | 367 | down(&em28xx_sysfs_lock); |
a6c2ba28 | 368 | |
3acf2809 | 369 | em28xx_info("V4L2 device /dev/video%d deregistered\n", |
a6c2ba28 | 370 | dev->vdev->minor); |
9c75541f | 371 | list_del(&dev->devlist); |
a6c2ba28 | 372 | video_unregister_device(dev->vdev); |
373 | /* video_unregister_device(dev->vbi_dev); */ | |
3acf2809 | 374 | em28xx_i2c_unregister(dev); |
a6c2ba28 | 375 | usb_put_dev(dev->udev); |
3acf2809 | 376 | up(&em28xx_sysfs_lock); |
a6c2ba28 | 377 | } |
378 | ||
379 | /* | |
3acf2809 | 380 | * em28xx_v4l2_close() |
a6c2ba28 | 381 | * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls |
382 | */ | |
3acf2809 | 383 | static int em28xx_v4l2_close(struct inode *inode, struct file *filp) |
a6c2ba28 | 384 | { |
a6c2ba28 | 385 | int errCode; |
3acf2809 | 386 | struct em28xx *dev=filp->private_data; |
a6c2ba28 | 387 | |
3acf2809 | 388 | em28xx_videodbg("users=%d", dev->users); |
a6c2ba28 | 389 | |
390 | down(&dev->lock); | |
391 | ||
3acf2809 | 392 | em28xx_uninit_isoc(dev); |
a6c2ba28 | 393 | |
3acf2809 | 394 | em28xx_release_buffers(dev); |
a6c2ba28 | 395 | |
396 | /* the device is already disconnect, free the remaining resources */ | |
397 | if (dev->state & DEV_DISCONNECTED) { | |
3acf2809 | 398 | em28xx_release_resources(dev); |
a6c2ba28 | 399 | up(&dev->lock); |
400 | kfree(dev); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | /* set alternate 0 */ | |
405 | dev->alt = 0; | |
3acf2809 | 406 | em28xx_videodbg("setting alternate 0"); |
a6c2ba28 | 407 | errCode = usb_set_interface(dev->udev, 0, 0); |
408 | if (errCode < 0) { | |
3acf2809 | 409 | em28xx_errdev ("cannot change alternate number to 0 (error=%i)\n", |
a6c2ba28 | 410 | errCode); |
411 | } | |
412 | ||
413 | dev->users--; | |
414 | wake_up_interruptible_nr(&dev->open, 1); | |
415 | up(&dev->lock); | |
416 | return 0; | |
417 | } | |
418 | ||
419 | /* | |
3acf2809 | 420 | * em28xx_v4l2_read() |
a6c2ba28 | 421 | * will allocate buffers when called for the first time |
422 | */ | |
423 | static ssize_t | |
3acf2809 | 424 | em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, |
a6c2ba28 | 425 | loff_t * f_pos) |
426 | { | |
3acf2809 | 427 | struct em28xx_frame_t *f, *i; |
a6c2ba28 | 428 | unsigned long lock_flags; |
429 | int ret = 0; | |
3acf2809 | 430 | struct em28xx *dev = filp->private_data; |
a6c2ba28 | 431 | |
432 | if (down_interruptible(&dev->fileop_lock)) | |
433 | return -ERESTARTSYS; | |
434 | ||
435 | if (dev->state & DEV_DISCONNECTED) { | |
3acf2809 | 436 | em28xx_videodbg("device not present"); |
a6c2ba28 | 437 | up(&dev->fileop_lock); |
438 | return -ENODEV; | |
439 | } | |
440 | ||
441 | if (dev->state & DEV_MISCONFIGURED) { | |
3acf2809 | 442 | em28xx_videodbg("device misconfigured; close and open it again"); |
a6c2ba28 | 443 | up(&dev->fileop_lock); |
444 | return -EIO; | |
445 | } | |
446 | ||
447 | if (dev->io == IO_MMAP) { | |
3acf2809 | 448 | em28xx_videodbg ("IO method is set to mmap; close and open" |
a6c2ba28 | 449 | " the device again to choose the read method"); |
450 | up(&dev->fileop_lock); | |
451 | return -EINVAL; | |
452 | } | |
453 | ||
454 | if (dev->io == IO_NONE) { | |
3acf2809 MCC |
455 | if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) { |
456 | em28xx_errdev("read failed, not enough memory\n"); | |
a6c2ba28 | 457 | up(&dev->fileop_lock); |
458 | return -ENOMEM; | |
459 | } | |
460 | dev->io = IO_READ; | |
461 | dev->stream = STREAM_ON; | |
3acf2809 | 462 | em28xx_queue_unusedframes(dev); |
a6c2ba28 | 463 | } |
464 | ||
465 | if (!count) { | |
466 | up(&dev->fileop_lock); | |
467 | return 0; | |
468 | } | |
469 | ||
470 | if (list_empty(&dev->outqueue)) { | |
471 | if (filp->f_flags & O_NONBLOCK) { | |
472 | up(&dev->fileop_lock); | |
473 | return -EAGAIN; | |
474 | } | |
475 | ret = wait_event_interruptible | |
476 | (dev->wait_frame, | |
477 | (!list_empty(&dev->outqueue)) || | |
478 | (dev->state & DEV_DISCONNECTED)); | |
479 | if (ret) { | |
480 | up(&dev->fileop_lock); | |
481 | return ret; | |
482 | } | |
483 | if (dev->state & DEV_DISCONNECTED) { | |
484 | up(&dev->fileop_lock); | |
485 | return -ENODEV; | |
486 | } | |
487 | } | |
488 | ||
3acf2809 | 489 | f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame); |
a6c2ba28 | 490 | |
491 | spin_lock_irqsave(&dev->queue_lock, lock_flags); | |
492 | list_for_each_entry(i, &dev->outqueue, frame) | |
493 | i->state = F_UNUSED; | |
494 | INIT_LIST_HEAD(&dev->outqueue); | |
495 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); | |
496 | ||
3acf2809 | 497 | em28xx_queue_unusedframes(dev); |
a6c2ba28 | 498 | |
499 | if (count > f->buf.length) | |
500 | count = f->buf.length; | |
501 | ||
502 | if (copy_to_user(buf, f->bufmem, count)) { | |
503 | up(&dev->fileop_lock); | |
504 | return -EFAULT; | |
505 | } | |
506 | *f_pos += count; | |
507 | ||
508 | up(&dev->fileop_lock); | |
509 | ||
510 | return count; | |
511 | } | |
512 | ||
513 | /* | |
3acf2809 | 514 | * em28xx_v4l2_poll() |
a6c2ba28 | 515 | * will allocate buffers when called for the first time |
516 | */ | |
3acf2809 | 517 | static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) |
a6c2ba28 | 518 | { |
a6c2ba28 | 519 | unsigned int mask = 0; |
3acf2809 | 520 | struct em28xx *dev = filp->private_data; |
a6c2ba28 | 521 | |
522 | if (down_interruptible(&dev->fileop_lock)) | |
523 | return POLLERR; | |
524 | ||
525 | if (dev->state & DEV_DISCONNECTED) { | |
3acf2809 | 526 | em28xx_videodbg("device not present"); |
a6c2ba28 | 527 | } else if (dev->state & DEV_MISCONFIGURED) { |
3acf2809 | 528 | em28xx_videodbg("device is misconfigured; close and open it again"); |
a6c2ba28 | 529 | } else { |
530 | if (dev->io == IO_NONE) { | |
3acf2809 MCC |
531 | if (!em28xx_request_buffers |
532 | (dev, EM28XX_NUM_READ_FRAMES)) { | |
533 | em28xx_warn | |
a6c2ba28 | 534 | ("poll() failed, not enough memory\n"); |
535 | } else { | |
536 | dev->io = IO_READ; | |
537 | dev->stream = STREAM_ON; | |
538 | } | |
539 | } | |
540 | ||
541 | if (dev->io == IO_READ) { | |
3acf2809 | 542 | em28xx_queue_unusedframes(dev); |
a6c2ba28 | 543 | poll_wait(filp, &dev->wait_frame, wait); |
544 | ||
545 | if (!list_empty(&dev->outqueue)) | |
546 | mask |= POLLIN | POLLRDNORM; | |
547 | ||
548 | up(&dev->fileop_lock); | |
549 | ||
550 | return mask; | |
551 | } | |
552 | } | |
553 | ||
554 | up(&dev->fileop_lock); | |
555 | return POLLERR; | |
556 | } | |
557 | ||
558 | /* | |
3acf2809 | 559 | * em28xx_vm_open() |
a6c2ba28 | 560 | */ |
3acf2809 | 561 | static void em28xx_vm_open(struct vm_area_struct *vma) |
a6c2ba28 | 562 | { |
3acf2809 | 563 | struct em28xx_frame_t *f = vma->vm_private_data; |
a6c2ba28 | 564 | f->vma_use_count++; |
565 | } | |
566 | ||
567 | /* | |
3acf2809 | 568 | * em28xx_vm_close() |
a6c2ba28 | 569 | */ |
3acf2809 | 570 | static void em28xx_vm_close(struct vm_area_struct *vma) |
a6c2ba28 | 571 | { |
572 | /* NOTE: buffers are not freed here */ | |
3acf2809 | 573 | struct em28xx_frame_t *f = vma->vm_private_data; |
a6c2ba28 | 574 | f->vma_use_count--; |
575 | } | |
576 | ||
3acf2809 MCC |
577 | static struct vm_operations_struct em28xx_vm_ops = { |
578 | .open = em28xx_vm_open, | |
579 | .close = em28xx_vm_close, | |
a6c2ba28 | 580 | }; |
581 | ||
582 | /* | |
3acf2809 | 583 | * em28xx_v4l2_mmap() |
a6c2ba28 | 584 | */ |
3acf2809 | 585 | static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) |
a6c2ba28 | 586 | { |
a6c2ba28 | 587 | unsigned long size = vma->vm_end - vma->vm_start, |
588 | start = vma->vm_start, pos, page; | |
589 | u32 i; | |
9c75541f | 590 | |
3acf2809 | 591 | struct em28xx *dev = filp->private_data; |
9c75541f | 592 | |
a6c2ba28 | 593 | if (down_interruptible(&dev->fileop_lock)) |
594 | return -ERESTARTSYS; | |
595 | ||
596 | if (dev->state & DEV_DISCONNECTED) { | |
3acf2809 | 597 | em28xx_videodbg("mmap: device not present"); |
a6c2ba28 | 598 | up(&dev->fileop_lock); |
599 | return -ENODEV; | |
600 | } | |
601 | ||
602 | if (dev->state & DEV_MISCONFIGURED) { | |
3acf2809 | 603 | em28xx_videodbg ("mmap: Device is misconfigured; close and " |
a6c2ba28 | 604 | "open it again"); |
605 | up(&dev->fileop_lock); | |
606 | return -EIO; | |
607 | } | |
608 | ||
609 | if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE) || | |
610 | size != PAGE_ALIGN(dev->frame[0].buf.length)) { | |
611 | up(&dev->fileop_lock); | |
612 | return -EINVAL; | |
613 | } | |
614 | ||
615 | for (i = 0; i < dev->num_frames; i++) { | |
616 | if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff) | |
617 | break; | |
618 | } | |
619 | if (i == dev->num_frames) { | |
3acf2809 | 620 | em28xx_videodbg("mmap: user supplied mapping address is out of range"); |
a6c2ba28 | 621 | up(&dev->fileop_lock); |
622 | return -EINVAL; | |
623 | } | |
624 | ||
625 | /* VM_IO is eventually going to replace PageReserved altogether */ | |
626 | vma->vm_flags |= VM_IO; | |
627 | vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */ | |
628 | ||
629 | pos = (unsigned long)dev->frame[i].bufmem; | |
630 | while (size > 0) { /* size is page-aligned */ | |
631 | page = vmalloc_to_pfn((void *)pos); | |
632 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, | |
633 | vma->vm_page_prot)) { | |
3acf2809 | 634 | em28xx_videodbg("mmap: rename page map failed"); |
a6c2ba28 | 635 | up(&dev->fileop_lock); |
636 | return -EAGAIN; | |
637 | } | |
638 | start += PAGE_SIZE; | |
639 | pos += PAGE_SIZE; | |
640 | size -= PAGE_SIZE; | |
641 | } | |
642 | ||
3acf2809 | 643 | vma->vm_ops = &em28xx_vm_ops; |
a6c2ba28 | 644 | vma->vm_private_data = &dev->frame[i]; |
645 | ||
3acf2809 | 646 | em28xx_vm_open(vma); |
a6c2ba28 | 647 | up(&dev->fileop_lock); |
648 | return 0; | |
649 | } | |
650 | ||
651 | /* | |
3acf2809 | 652 | * em28xx_get_ctrl() |
a6c2ba28 | 653 | * return the current saturation, brightness or contrast, mute state |
654 | */ | |
3acf2809 | 655 | static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl) |
a6c2ba28 | 656 | { |
657 | s32 tmp; | |
658 | switch (ctrl->id) { | |
659 | case V4L2_CID_AUDIO_MUTE: | |
660 | ctrl->value = dev->mute; | |
661 | return 0; | |
662 | case V4L2_CID_AUDIO_VOLUME: | |
663 | ctrl->value = dev->volume; | |
664 | return 0; | |
665 | case V4L2_CID_BRIGHTNESS: | |
3acf2809 | 666 | if ((tmp = em28xx_brightness_get(dev)) < 0) |
a6c2ba28 | 667 | return -EIO; |
668 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ | |
669 | return 0; | |
670 | case V4L2_CID_CONTRAST: | |
3acf2809 | 671 | if ((ctrl->value = em28xx_contrast_get(dev)) < 0) |
a6c2ba28 | 672 | return -EIO; |
673 | return 0; | |
674 | case V4L2_CID_SATURATION: | |
3acf2809 | 675 | if ((ctrl->value = em28xx_saturation_get(dev)) < 0) |
a6c2ba28 | 676 | return -EIO; |
677 | return 0; | |
678 | case V4L2_CID_RED_BALANCE: | |
3acf2809 | 679 | if ((tmp = em28xx_v_balance_get(dev)) < 0) |
a6c2ba28 | 680 | return -EIO; |
681 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ | |
682 | return 0; | |
683 | case V4L2_CID_BLUE_BALANCE: | |
3acf2809 | 684 | if ((tmp = em28xx_u_balance_get(dev)) < 0) |
a6c2ba28 | 685 | return -EIO; |
686 | ctrl->value = (s32) ((s8) tmp); /* FIXME: clenaer way to extend sign? */ | |
687 | return 0; | |
688 | case V4L2_CID_GAMMA: | |
3acf2809 | 689 | if ((ctrl->value = em28xx_gamma_get(dev)) < 0) |
a6c2ba28 | 690 | return -EIO; |
691 | return 0; | |
692 | default: | |
693 | return -EINVAL; | |
694 | } | |
695 | } | |
696 | ||
697 | /* | |
3acf2809 | 698 | * em28xx_set_ctrl() |
a6c2ba28 | 699 | * mute or set new saturation, brightness or contrast |
700 | */ | |
3acf2809 | 701 | static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) |
a6c2ba28 | 702 | { |
703 | switch (ctrl->id) { | |
704 | case V4L2_CID_AUDIO_MUTE: | |
705 | if (ctrl->value != dev->mute) { | |
706 | dev->mute = ctrl->value; | |
3acf2809 MCC |
707 | em28xx_audio_usb_mute(dev, ctrl->value); |
708 | return em28xx_audio_analog_set(dev); | |
a6c2ba28 | 709 | } |
710 | return 0; | |
711 | case V4L2_CID_AUDIO_VOLUME: | |
712 | dev->volume = ctrl->value; | |
3acf2809 | 713 | return em28xx_audio_analog_set(dev); |
a6c2ba28 | 714 | case V4L2_CID_BRIGHTNESS: |
3acf2809 | 715 | return em28xx_brightness_set(dev, ctrl->value); |
a6c2ba28 | 716 | case V4L2_CID_CONTRAST: |
3acf2809 | 717 | return em28xx_contrast_set(dev, ctrl->value); |
a6c2ba28 | 718 | case V4L2_CID_SATURATION: |
3acf2809 | 719 | return em28xx_saturation_set(dev, ctrl->value); |
a6c2ba28 | 720 | case V4L2_CID_RED_BALANCE: |
3acf2809 | 721 | return em28xx_v_balance_set(dev, ctrl->value); |
a6c2ba28 | 722 | case V4L2_CID_BLUE_BALANCE: |
3acf2809 | 723 | return em28xx_u_balance_set(dev, ctrl->value); |
a6c2ba28 | 724 | case V4L2_CID_GAMMA: |
3acf2809 | 725 | return em28xx_gamma_set(dev, ctrl->value); |
a6c2ba28 | 726 | default: |
727 | return -EINVAL; | |
728 | } | |
729 | } | |
730 | ||
731 | /* | |
3acf2809 | 732 | * em28xx_stream_interrupt() |
a6c2ba28 | 733 | * stops streaming |
734 | */ | |
3acf2809 | 735 | static int em28xx_stream_interrupt(struct em28xx *dev) |
a6c2ba28 | 736 | { |
737 | int ret = 0; | |
738 | ||
739 | /* stop reading from the device */ | |
740 | ||
741 | dev->stream = STREAM_INTERRUPT; | |
742 | ret = wait_event_timeout(dev->wait_stream, | |
743 | (dev->stream == STREAM_OFF) || | |
744 | (dev->state & DEV_DISCONNECTED), | |
3acf2809 | 745 | EM28XX_URB_TIMEOUT); |
a6c2ba28 | 746 | if (dev->state & DEV_DISCONNECTED) |
747 | return -ENODEV; | |
748 | else if (ret) { | |
749 | dev->state |= DEV_MISCONFIGURED; | |
3acf2809 | 750 | em28xx_videodbg("device is misconfigured; close and " |
a6c2ba28 | 751 | "open /dev/video%d again", dev->vdev->minor); |
752 | return ret; | |
753 | } | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
3acf2809 | 758 | static int em28xx_set_norm(struct em28xx *dev, int width, int height) |
a6c2ba28 | 759 | { |
760 | unsigned int hscale, vscale; | |
761 | unsigned int maxh, maxw; | |
762 | ||
763 | maxw = norm_maxw(dev); | |
764 | maxh = norm_maxh(dev); | |
765 | ||
766 | /* width must even because of the YUYV format */ | |
767 | /* height must be even because of interlacing */ | |
768 | height &= 0xfffe; | |
769 | width &= 0xfffe; | |
770 | ||
771 | if (height < 32) | |
772 | height = 32; | |
773 | if (height > maxh) | |
774 | height = maxh; | |
775 | if (width < 48) | |
776 | width = 48; | |
777 | if (width > maxw) | |
778 | width = maxw; | |
779 | ||
780 | if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000) | |
781 | hscale = 0x3fff; | |
782 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); | |
783 | ||
784 | if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000) | |
785 | vscale = 0x3fff; | |
786 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); | |
787 | ||
788 | /* set new image size */ | |
789 | dev->width = width; | |
790 | dev->height = height; | |
791 | dev->frame_size = dev->width * dev->height * 2; | |
792 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ | |
793 | dev->bytesperline = dev->width * 2; | |
794 | dev->hscale = hscale; | |
795 | dev->vscale = vscale; | |
796 | ||
3acf2809 | 797 | em28xx_resolution_set(dev); |
a6c2ba28 | 798 | |
799 | return 0; | |
800 | } | |
801 | ||
3acf2809 | 802 | static void video_mux(struct em28xx *dev, int index) |
a6c2ba28 | 803 | { |
804 | int input, ainput; | |
805 | ||
806 | input = INPUT(index)->vmux; | |
807 | dev->ctl_input = index; | |
808 | ||
3acf2809 | 809 | em28xx_i2c_call_clients(dev, DECODER_SET_INPUT, &input); |
a6c2ba28 | 810 | |
811 | dev->ctl_ainput = INPUT(index)->amux; | |
812 | ||
813 | switch (dev->ctl_ainput) { | |
814 | case 0: | |
3acf2809 | 815 | ainput = EM28XX_AUDIO_SRC_TUNER; |
a6c2ba28 | 816 | break; |
817 | default: | |
3acf2809 | 818 | ainput = EM28XX_AUDIO_SRC_LINE; |
a6c2ba28 | 819 | } |
820 | ||
3acf2809 | 821 | em28xx_audio_source(dev, ainput); |
a6c2ba28 | 822 | } |
823 | ||
824 | /* | |
3acf2809 | 825 | * em28xx_v4l2_do_ioctl() |
a6c2ba28 | 826 | * This function is _not_ called directly, but from |
3acf2809 | 827 | * em28xx_v4l2_ioctl. Userspace |
a6c2ba28 | 828 | * copying is done already, arg is a kernel pointer. |
829 | */ | |
3acf2809 MCC |
830 | static int em28xx_do_ioctl(struct inode *inode, struct file *filp, |
831 | struct em28xx *dev, unsigned int cmd, void *arg, | |
a6c2ba28 | 832 | v4l2_kioctl driver_ioctl) |
833 | { | |
834 | int ret; | |
835 | ||
836 | switch (cmd) { | |
837 | /* ---------- tv norms ---------- */ | |
838 | case VIDIOC_ENUMSTD: | |
839 | { | |
840 | struct v4l2_standard *e = arg; | |
841 | unsigned int i; | |
842 | ||
843 | i = e->index; | |
844 | if (i >= TVNORMS) | |
845 | return -EINVAL; | |
846 | ret = v4l2_video_std_construct(e, tvnorms[e->index].id, | |
847 | tvnorms[e->index].name); | |
848 | e->index = i; | |
849 | if (ret < 0) | |
850 | return ret; | |
851 | return 0; | |
852 | } | |
853 | case VIDIOC_G_STD: | |
854 | { | |
855 | v4l2_std_id *id = arg; | |
856 | ||
857 | *id = dev->tvnorm->id; | |
858 | return 0; | |
859 | } | |
860 | case VIDIOC_S_STD: | |
861 | { | |
862 | v4l2_std_id *id = arg; | |
863 | unsigned int i; | |
864 | ||
865 | for (i = 0; i < TVNORMS; i++) | |
866 | if (*id == tvnorms[i].id) | |
867 | break; | |
868 | if (i == TVNORMS) | |
869 | for (i = 0; i < TVNORMS; i++) | |
870 | if (*id & tvnorms[i].id) | |
871 | break; | |
872 | if (i == TVNORMS) | |
873 | return -EINVAL; | |
874 | ||
875 | down(&dev->lock); | |
876 | dev->tvnorm = &tvnorms[i]; | |
877 | ||
3acf2809 | 878 | em28xx_set_norm(dev, dev->width, dev->height); |
a6c2ba28 | 879 | |
880 | /* | |
881 | dev->width=norm_maxw(dev); | |
882 | dev->height=norm_maxh(dev); | |
883 | dev->frame_size=dev->width*dev->height*2; | |
884 | dev->field_size=dev->frame_size>>1; | |
885 | dev->bytesperline=dev->width*2; | |
886 | dev->hscale=0; | |
887 | dev->vscale=0; | |
888 | ||
3acf2809 | 889 | em28xx_resolution_set(dev); |
a6c2ba28 | 890 | */ |
891 | /* | |
3acf2809 MCC |
892 | em28xx_uninit_isoc(dev); |
893 | em28xx_set_alternate(dev); | |
894 | em28xx_capture_start(dev, 1); | |
895 | em28xx_resolution_set(dev); | |
896 | em28xx_init_isoc(dev); | |
a6c2ba28 | 897 | */ |
3acf2809 | 898 | em28xx_i2c_call_clients(dev, DECODER_SET_NORM, |
a6c2ba28 | 899 | &tvnorms[i].mode); |
3acf2809 | 900 | em28xx_i2c_call_clients(dev, VIDIOC_S_STD, |
a6c2ba28 | 901 | &dev->tvnorm->id); |
902 | ||
903 | up(&dev->lock); | |
904 | ||
905 | return 0; | |
906 | } | |
907 | ||
908 | /* ------ input switching ---------- */ | |
909 | case VIDIOC_ENUMINPUT: | |
910 | { | |
911 | struct v4l2_input *i = arg; | |
912 | unsigned int n; | |
913 | static const char *iname[] = { | |
3acf2809 MCC |
914 | [EM28XX_VMUX_COMPOSITE1] = "Composite1", |
915 | [EM28XX_VMUX_COMPOSITE2] = "Composite2", | |
916 | [EM28XX_VMUX_COMPOSITE3] = "Composite3", | |
917 | [EM28XX_VMUX_COMPOSITE4] = "Composite4", | |
918 | [EM28XX_VMUX_SVIDEO] = "S-Video", | |
919 | [EM28XX_VMUX_TELEVISION] = "Television", | |
920 | [EM28XX_VMUX_CABLE] = "Cable TV", | |
921 | [EM28XX_VMUX_DVB] = "DVB", | |
922 | [EM28XX_VMUX_DEBUG] = "for debug only", | |
a6c2ba28 | 923 | }; |
924 | ||
925 | n = i->index; | |
3acf2809 | 926 | if (n >= MAX_EM28XX_INPUT) |
a6c2ba28 | 927 | return -EINVAL; |
928 | if (0 == INPUT(n)->type) | |
929 | return -EINVAL; | |
930 | memset(i, 0, sizeof(*i)); | |
931 | i->index = n; | |
932 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
933 | strcpy(i->name, iname[INPUT(n)->type]); | |
3acf2809 MCC |
934 | if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) || |
935 | (EM28XX_VMUX_CABLE == INPUT(n)->type)) | |
a6c2ba28 | 936 | i->type = V4L2_INPUT_TYPE_TUNER; |
937 | for (n = 0; n < ARRAY_SIZE(tvnorms); n++) | |
938 | i->std |= tvnorms[n].id; | |
939 | return 0; | |
940 | } | |
941 | ||
942 | case VIDIOC_G_INPUT: | |
943 | { | |
944 | int *i = arg; | |
945 | *i = dev->ctl_input; | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | case VIDIOC_S_INPUT: | |
951 | { | |
952 | int *index = arg; | |
953 | ||
3acf2809 | 954 | if (*index >= MAX_EM28XX_INPUT) |
a6c2ba28 | 955 | return -EINVAL; |
956 | if (0 == INPUT(*index)->type) | |
957 | return -EINVAL; | |
958 | ||
959 | down(&dev->lock); | |
960 | video_mux(dev, *index); | |
961 | up(&dev->lock); | |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
966 | case VIDIOC_G_AUDIO: | |
967 | { | |
968 | struct v4l2_audio *a = arg; | |
969 | unsigned int index = a->index; | |
970 | ||
971 | if (a->index > 1) | |
972 | return -EINVAL; | |
973 | memset(a, 0, sizeof(*a)); | |
974 | index = dev->ctl_ainput; | |
975 | ||
976 | if (index == 0) { | |
977 | strcpy(a->name, "Television"); | |
978 | } else { | |
979 | strcpy(a->name, "Line In"); | |
980 | } | |
981 | a->capability = V4L2_AUDCAP_STEREO; | |
982 | a->index = index; | |
983 | return 0; | |
984 | } | |
985 | ||
986 | case VIDIOC_S_AUDIO: | |
987 | { | |
988 | struct v4l2_audio *a = arg; | |
989 | if (a->index != dev->ctl_ainput) | |
990 | return -EINVAL; | |
991 | ||
992 | return 0; | |
993 | } | |
994 | ||
995 | /* --- controls ---------------------------------------------- */ | |
996 | case VIDIOC_QUERYCTRL: | |
997 | { | |
998 | struct v4l2_queryctrl *qc = arg; | |
999 | u8 i, n; | |
3acf2809 | 1000 | n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]); |
a6c2ba28 | 1001 | for (i = 0; i < n; i++) |
3acf2809 MCC |
1002 | if (qc->id && qc->id == em28xx_qctrl[i].id) { |
1003 | memcpy(qc, &(em28xx_qctrl[i]), | |
a6c2ba28 | 1004 | sizeof(*qc)); |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | return -EINVAL; | |
1009 | } | |
1010 | ||
1011 | case VIDIOC_G_CTRL: | |
1012 | { | |
1013 | struct v4l2_control *ctrl = arg; | |
1014 | ||
1015 | ||
3acf2809 | 1016 | return em28xx_get_ctrl(dev, ctrl); |
a6c2ba28 | 1017 | } |
1018 | ||
1019 | case VIDIOC_S_CTRL_OLD: /* ??? */ | |
1020 | case VIDIOC_S_CTRL: | |
1021 | { | |
1022 | struct v4l2_control *ctrl = arg; | |
1023 | u8 i, n; | |
1024 | ||
1025 | ||
3acf2809 | 1026 | n = sizeof(em28xx_qctrl) / sizeof(em28xx_qctrl[0]); |
a6c2ba28 | 1027 | for (i = 0; i < n; i++) |
3acf2809 | 1028 | if (ctrl->id == em28xx_qctrl[i].id) { |
a6c2ba28 | 1029 | if (ctrl->value < |
3acf2809 | 1030 | em28xx_qctrl[i].minimum |
a6c2ba28 | 1031 | || ctrl->value > |
3acf2809 | 1032 | em28xx_qctrl[i].maximum) |
a6c2ba28 | 1033 | return -ERANGE; |
1034 | ||
3acf2809 | 1035 | return em28xx_set_ctrl(dev, ctrl); |
a6c2ba28 | 1036 | } |
1037 | return -EINVAL; | |
1038 | } | |
1039 | ||
1040 | /* --- tuner ioctls ------------------------------------------ */ | |
1041 | case VIDIOC_G_TUNER: | |
1042 | { | |
1043 | struct v4l2_tuner *t = arg; | |
1044 | int status = 0; | |
1045 | ||
1046 | if (0 != t->index) | |
1047 | return -EINVAL; | |
1048 | ||
1049 | memset(t, 0, sizeof(*t)); | |
1050 | strcpy(t->name, "Tuner"); | |
1051 | t->type = V4L2_TUNER_ANALOG_TV; | |
1052 | t->capability = V4L2_TUNER_CAP_NORM; | |
1053 | t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */ | |
1054 | /* t->signal = 0xffff;*/ | |
3acf2809 | 1055 | /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/ |
a6c2ba28 | 1056 | /* No way to get signal strength? */ |
1057 | down(&dev->lock); | |
3acf2809 | 1058 | em28xx_i2c_call_clients(dev, DECODER_GET_STATUS, |
a6c2ba28 | 1059 | &status); |
1060 | up(&dev->lock); | |
1061 | t->signal = | |
1062 | (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0; | |
1063 | ||
3acf2809 | 1064 | em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x", t->signal, |
a6c2ba28 | 1065 | t->afc); |
1066 | return 0; | |
1067 | } | |
1068 | case VIDIOC_S_TUNER: | |
1069 | { | |
1070 | struct v4l2_tuner *t = arg; | |
1071 | int status = 0; | |
1072 | ||
1073 | if (0 != t->index) | |
1074 | return -EINVAL; | |
1075 | memset(t, 0, sizeof(*t)); | |
1076 | strcpy(t->name, "Tuner"); | |
1077 | t->type = V4L2_TUNER_ANALOG_TV; | |
1078 | t->capability = V4L2_TUNER_CAP_NORM; | |
1079 | t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */ | |
1080 | /* t->signal = 0xffff; */ | |
1081 | /* No way to get signal strength? */ | |
1082 | down(&dev->lock); | |
3acf2809 | 1083 | em28xx_i2c_call_clients(dev, DECODER_GET_STATUS, |
a6c2ba28 | 1084 | &status); |
1085 | up(&dev->lock); | |
1086 | t->signal = | |
1087 | (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0; | |
1088 | ||
3acf2809 | 1089 | em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n", |
a6c2ba28 | 1090 | t->signal, t->afc); |
1091 | return 0; | |
1092 | } | |
1093 | case VIDIOC_G_FREQUENCY: | |
1094 | { | |
1095 | struct v4l2_frequency *f = arg; | |
1096 | ||
1097 | memset(f, 0, sizeof(*f)); | |
1098 | f->type = V4L2_TUNER_ANALOG_TV; | |
1099 | f->frequency = dev->ctl_freq; | |
1100 | ||
1101 | return 0; | |
1102 | } | |
1103 | case VIDIOC_S_FREQUENCY: | |
1104 | { | |
1105 | struct v4l2_frequency *f = arg; | |
1106 | ||
1107 | if (0 != f->tuner) | |
1108 | return -EINVAL; | |
1109 | ||
1110 | if (V4L2_TUNER_ANALOG_TV != f->type) | |
1111 | return -EINVAL; | |
1112 | ||
1113 | down(&dev->lock); | |
1114 | dev->ctl_freq = f->frequency; | |
3acf2809 | 1115 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f); |
a6c2ba28 | 1116 | up(&dev->lock); |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | case VIDIOC_CROPCAP: | |
1121 | { | |
1122 | struct v4l2_cropcap *cc = arg; | |
1123 | ||
1124 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
596d92d5 | 1125 | return -EINVAL; |
a6c2ba28 | 1126 | cc->bounds.left = 0; |
1127 | cc->bounds.top = 0; | |
1128 | cc->bounds.width = dev->width; | |
1129 | cc->bounds.height = dev->height; | |
1130 | cc->defrect = cc->bounds; | |
1131 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ | |
1132 | cc->pixelaspect.denominator = 59; | |
1133 | return 0; | |
1134 | } | |
1135 | case VIDIOC_STREAMON: | |
1136 | { | |
1137 | int *type = arg; | |
1138 | ||
1139 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE | |
1140 | || dev->io != IO_MMAP) | |
1141 | return -EINVAL; | |
1142 | ||
1143 | if (list_empty(&dev->inqueue)) | |
1144 | return -EINVAL; | |
1145 | ||
1146 | dev->stream = STREAM_ON; /* FIXME: Start video capture here? */ | |
1147 | ||
3acf2809 | 1148 | em28xx_videodbg("VIDIOC_STREAMON: starting stream"); |
a6c2ba28 | 1149 | |
1150 | return 0; | |
1151 | } | |
1152 | case VIDIOC_STREAMOFF: | |
1153 | { | |
1154 | int *type = arg; | |
1155 | int ret; | |
1156 | ||
1157 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE | |
1158 | || dev->io != IO_MMAP) | |
1159 | return -EINVAL; | |
1160 | ||
1161 | if (dev->stream == STREAM_ON) { | |
3acf2809 MCC |
1162 | em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream"); |
1163 | if ((ret = em28xx_stream_interrupt(dev))) | |
a6c2ba28 | 1164 | return ret; |
1165 | } | |
3acf2809 | 1166 | em28xx_empty_framequeues(dev); |
a6c2ba28 | 1167 | |
1168 | return 0; | |
1169 | } | |
1170 | default: | |
1171 | return v4l_compat_translate_ioctl(inode, filp, cmd, arg, | |
1172 | driver_ioctl); | |
1173 | } | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | /* | |
3acf2809 | 1178 | * em28xx_v4l2_do_ioctl() |
a6c2ba28 | 1179 | * This function is _not_ called directly, but from |
3acf2809 | 1180 | * em28xx_v4l2_ioctl. Userspace |
a6c2ba28 | 1181 | * copying is done already, arg is a kernel pointer. |
1182 | */ | |
3acf2809 | 1183 | static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp, |
a6c2ba28 | 1184 | unsigned int cmd, void *arg) |
1185 | { | |
3acf2809 | 1186 | struct em28xx *dev = filp->private_data; |
a6c2ba28 | 1187 | |
1188 | if (!dev) | |
1189 | return -ENODEV; | |
1190 | ||
1191 | if (video_debug > 1) | |
3acf2809 | 1192 | em28xx_print_ioctl(dev->name,cmd); |
a6c2ba28 | 1193 | |
1194 | switch (cmd) { | |
1195 | ||
1196 | /* --- capabilities ------------------------------------------ */ | |
1197 | case VIDIOC_QUERYCAP: | |
1198 | { | |
1199 | struct v4l2_capability *cap = arg; | |
1200 | ||
1201 | memset(cap, 0, sizeof(*cap)); | |
3acf2809 MCC |
1202 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); |
1203 | strlcpy(cap->card, em28xx_boards[dev->model].name, | |
a6c2ba28 | 1204 | sizeof(cap->card)); |
1205 | strlcpy(cap->bus_info, dev->udev->dev.bus_id, | |
1206 | sizeof(cap->bus_info)); | |
3acf2809 | 1207 | cap->version = EM28XX_VERSION_CODE; |
a6c2ba28 | 1208 | cap->capabilities = |
1209 | V4L2_CAP_VIDEO_CAPTURE | | |
1210 | V4L2_CAP_AUDIO | | |
1211 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
1212 | if (dev->has_tuner) | |
1213 | cap->capabilities |= V4L2_CAP_TUNER; | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | /* --- capture ioctls ---------------------------------------- */ | |
1218 | case VIDIOC_ENUM_FMT: | |
1219 | { | |
1220 | struct v4l2_fmtdesc *fmtd = arg; | |
1221 | ||
1222 | if (fmtd->index != 0) | |
1223 | return -EINVAL; | |
1224 | memset(fmtd, 0, sizeof(*fmtd)); | |
1225 | fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1226 | strcpy(fmtd->description, "Packed YUY2"); | |
1227 | fmtd->pixelformat = V4L2_PIX_FMT_YUYV; | |
1228 | memset(fmtd->reserved, 0, sizeof(fmtd->reserved)); | |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | case VIDIOC_G_FMT: | |
1233 | { | |
1234 | struct v4l2_format *format = arg; | |
1235 | ||
3acf2809 | 1236 | em28xx_videodbg("VIDIOC_G_FMT: type=%s", |
a6c2ba28 | 1237 | format->type == |
1238 | V4L2_BUF_TYPE_VIDEO_CAPTURE ? | |
1239 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type == | |
1240 | V4L2_BUF_TYPE_VBI_CAPTURE ? | |
1241 | "V4L2_BUF_TYPE_VBI_CAPTURE " : | |
1242 | "not supported"); | |
1243 | ||
1244 | if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1245 | return -EINVAL; | |
1246 | ||
1247 | format->fmt.pix.width = dev->width; | |
1248 | format->fmt.pix.height = dev->height; | |
1249 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; | |
1250 | format->fmt.pix.bytesperline = dev->bytesperline; | |
1251 | format->fmt.pix.sizeimage = dev->frame_size; | |
1252 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1253 | format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ | |
1254 | ||
3acf2809 | 1255 | em28xx_videodbg("VIDIOC_G_FMT: %dx%d", dev->width, |
a6c2ba28 | 1256 | dev->height); |
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | case VIDIOC_TRY_FMT: | |
1261 | case VIDIOC_S_FMT: | |
1262 | { | |
1263 | struct v4l2_format *format = arg; | |
1264 | u32 i; | |
1265 | int ret = 0; | |
1266 | int width = format->fmt.pix.width; | |
1267 | int height = format->fmt.pix.height; | |
1268 | unsigned int hscale, vscale; | |
1269 | unsigned int maxh, maxw; | |
1270 | ||
1271 | maxw = norm_maxw(dev); | |
1272 | maxh = norm_maxh(dev); | |
1273 | ||
1274 | /* int both_fields; */ | |
1275 | ||
3acf2809 | 1276 | em28xx_videodbg("%s: type=%s", |
a6c2ba28 | 1277 | cmd == |
1278 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : | |
1279 | "VIDIOC_S_FMT", | |
1280 | format->type == | |
1281 | V4L2_BUF_TYPE_VIDEO_CAPTURE ? | |
1282 | "V4L2_BUF_TYPE_VIDEO_CAPTURE" : format->type == | |
1283 | V4L2_BUF_TYPE_VBI_CAPTURE ? | |
1284 | "V4L2_BUF_TYPE_VBI_CAPTURE " : | |
1285 | "not supported"); | |
1286 | ||
1287 | if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1288 | return -EINVAL; | |
1289 | ||
3acf2809 | 1290 | em28xx_videodbg("%s: requested %dx%d", |
a6c2ba28 | 1291 | cmd == |
1292 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : | |
1293 | "VIDIOC_S_FMT", format->fmt.pix.width, | |
1294 | format->fmt.pix.height); | |
1295 | ||
1296 | /* FIXME: Move some code away from here */ | |
1297 | /* width must even because of the YUYV format */ | |
1298 | /* height must be even because of interlacing */ | |
1299 | height &= 0xfffe; | |
1300 | width &= 0xfffe; | |
1301 | ||
1302 | if (height < 32) | |
1303 | height = 32; | |
1304 | if (height > maxh) | |
1305 | height = maxh; | |
1306 | if (width < 48) | |
1307 | width = 48; | |
1308 | if (width > maxw) | |
1309 | width = maxw; | |
1310 | ||
74458e6c | 1311 | if(dev->is_em2800){ |
52c02fcd | 1312 | /* the em2800 can only scale down to 50% */ |
74458e6c SS |
1313 | if(height % (maxh / 2)) |
1314 | height=maxh; | |
1315 | if(width % (maxw / 2)) | |
1316 | width=maxw; | |
52c02fcd SS |
1317 | /* according to empiatech support */ |
1318 | /* the MaxPacketSize is to small to support */ | |
1319 | /* framesizes larger than 640x480 @ 30 fps */ | |
1320 | /* or 640x576 @ 25 fps. As this would cut */ | |
1321 | /* of a part of the image we prefer */ | |
1322 | /* 360x576 or 360x480 for now */ | |
74458e6c SS |
1323 | if(width == maxw && height == maxh) |
1324 | width /= 2; | |
1325 | } | |
1326 | ||
a6c2ba28 | 1327 | if ((hscale = |
1328 | (((unsigned long)maxw) << 12) / width - 4096L) >= | |
1329 | 0x4000) | |
1330 | hscale = 0x3fff; | |
1331 | width = | |
1332 | (((unsigned long)maxw) << 12) / (hscale + 4096L); | |
1333 | ||
1334 | if ((vscale = | |
1335 | (((unsigned long)maxh) << 12) / height - 4096L) >= | |
1336 | 0x4000) | |
1337 | vscale = 0x3fff; | |
1338 | height = | |
1339 | (((unsigned long)maxh) << 12) / (vscale + 4096L); | |
1340 | ||
1341 | format->fmt.pix.width = width; | |
1342 | format->fmt.pix.height = height; | |
1343 | format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; | |
1344 | format->fmt.pix.bytesperline = width * 2; | |
1345 | format->fmt.pix.sizeimage = width * 2 * height; | |
1346 | format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1347 | format->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
1348 | ||
3acf2809 | 1349 | em28xx_videodbg("%s: returned %dx%d (%d, %d)", |
a6c2ba28 | 1350 | cmd == |
1351 | VIDIOC_TRY_FMT ? "VIDIOC_TRY_FMT" : | |
1352 | "VIDIOC_S_FMT", format->fmt.pix.width, | |
1353 | format->fmt.pix.height, hscale, vscale); | |
1354 | ||
1355 | if (cmd == VIDIOC_TRY_FMT) | |
1356 | return 0; | |
1357 | ||
1358 | for (i = 0; i < dev->num_frames; i++) | |
1359 | if (dev->frame[i].vma_use_count) { | |
3acf2809 | 1360 | em28xx_videodbg("VIDIOC_S_FMT failed. " |
a6c2ba28 | 1361 | "Unmap the buffers first."); |
1362 | return -EINVAL; | |
1363 | } | |
1364 | ||
1365 | /* stop io in case it is already in progress */ | |
1366 | if (dev->stream == STREAM_ON) { | |
3acf2809 MCC |
1367 | em28xx_videodbg("VIDIOC_SET_FMT: interupting stream"); |
1368 | if ((ret = em28xx_stream_interrupt(dev))) | |
a6c2ba28 | 1369 | return ret; |
1370 | } | |
1371 | ||
3acf2809 | 1372 | em28xx_release_buffers(dev); |
a6c2ba28 | 1373 | dev->io = IO_NONE; |
1374 | ||
1375 | /* set new image size */ | |
1376 | dev->width = width; | |
1377 | dev->height = height; | |
1378 | dev->frame_size = dev->width * dev->height * 2; | |
1379 | dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */ | |
1380 | dev->bytesperline = dev->width * 2; | |
1381 | dev->hscale = hscale; | |
1382 | dev->vscale = vscale; | |
1383 | /* dev->both_fileds = both_fileds; */ | |
3acf2809 MCC |
1384 | em28xx_uninit_isoc(dev); |
1385 | em28xx_set_alternate(dev); | |
1386 | em28xx_capture_start(dev, 1); | |
1387 | em28xx_resolution_set(dev); | |
1388 | em28xx_init_isoc(dev); | |
a6c2ba28 | 1389 | |
1390 | return 0; | |
1391 | } | |
1392 | ||
1393 | /* --- streaming capture ------------------------------------- */ | |
1394 | case VIDIOC_REQBUFS: | |
1395 | { | |
1396 | struct v4l2_requestbuffers *rb = arg; | |
1397 | u32 i; | |
1398 | int ret; | |
1399 | ||
1400 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
1401 | rb->memory != V4L2_MEMORY_MMAP) | |
1402 | return -EINVAL; | |
1403 | ||
1404 | if (dev->io == IO_READ) { | |
3acf2809 | 1405 | em28xx_videodbg ("method is set to read;" |
a6c2ba28 | 1406 | " close and open the device again to" |
1407 | " choose the mmap I/O method"); | |
1408 | return -EINVAL; | |
1409 | } | |
1410 | ||
1411 | for (i = 0; i < dev->num_frames; i++) | |
1412 | if (dev->frame[i].vma_use_count) { | |
3acf2809 | 1413 | em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped"); |
a6c2ba28 | 1414 | return -EINVAL; |
1415 | } | |
1416 | ||
1417 | if (dev->stream == STREAM_ON) { | |
3acf2809 MCC |
1418 | em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream"); |
1419 | if ((ret = em28xx_stream_interrupt(dev))) | |
a6c2ba28 | 1420 | return ret; |
1421 | } | |
1422 | ||
3acf2809 | 1423 | em28xx_empty_framequeues(dev); |
a6c2ba28 | 1424 | |
3acf2809 | 1425 | em28xx_release_buffers(dev); |
a6c2ba28 | 1426 | if (rb->count) |
1427 | rb->count = | |
3acf2809 | 1428 | em28xx_request_buffers(dev, rb->count); |
a6c2ba28 | 1429 | |
1430 | dev->frame_current = NULL; | |
1431 | ||
3acf2809 | 1432 | em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i", |
a6c2ba28 | 1433 | rb->count); |
1434 | dev->io = rb->count ? IO_MMAP : IO_NONE; | |
1435 | return 0; | |
1436 | } | |
1437 | ||
1438 | case VIDIOC_QUERYBUF: | |
1439 | { | |
1440 | struct v4l2_buffer *b = arg; | |
1441 | ||
1442 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
1443 | b->index >= dev->num_frames || dev->io != IO_MMAP) | |
1444 | return -EINVAL; | |
1445 | ||
1446 | memcpy(b, &dev->frame[b->index].buf, sizeof(*b)); | |
1447 | ||
1448 | if (dev->frame[b->index].vma_use_count) { | |
1449 | b->flags |= V4L2_BUF_FLAG_MAPPED; | |
1450 | } | |
1451 | if (dev->frame[b->index].state == F_DONE) | |
1452 | b->flags |= V4L2_BUF_FLAG_DONE; | |
1453 | else if (dev->frame[b->index].state != F_UNUSED) | |
1454 | b->flags |= V4L2_BUF_FLAG_QUEUED; | |
1455 | return 0; | |
1456 | } | |
1457 | case VIDIOC_QBUF: | |
1458 | { | |
1459 | struct v4l2_buffer *b = arg; | |
1460 | unsigned long lock_flags; | |
1461 | ||
1462 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || | |
1463 | b->index >= dev->num_frames || dev->io != IO_MMAP) { | |
1464 | return -EINVAL; | |
1465 | } | |
1466 | ||
1467 | if (dev->frame[b->index].state != F_UNUSED) { | |
1468 | return -EAGAIN; | |
1469 | } | |
1470 | dev->frame[b->index].state = F_QUEUED; | |
1471 | ||
1472 | /* add frame to fifo */ | |
1473 | spin_lock_irqsave(&dev->queue_lock, lock_flags); | |
1474 | list_add_tail(&dev->frame[b->index].frame, | |
1475 | &dev->inqueue); | |
1476 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); | |
1477 | ||
1478 | return 0; | |
1479 | } | |
1480 | case VIDIOC_DQBUF: | |
1481 | { | |
1482 | struct v4l2_buffer *b = arg; | |
3acf2809 | 1483 | struct em28xx_frame_t *f; |
a6c2ba28 | 1484 | unsigned long lock_flags; |
1485 | int ret = 0; | |
1486 | ||
1487 | if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE | |
1488 | || dev->io != IO_MMAP) | |
1489 | return -EINVAL; | |
1490 | ||
1491 | if (list_empty(&dev->outqueue)) { | |
1492 | if (dev->stream == STREAM_OFF) | |
1493 | return -EINVAL; | |
1494 | if (filp->f_flags & O_NONBLOCK) | |
1495 | return -EAGAIN; | |
1496 | ret = wait_event_interruptible | |
1497 | (dev->wait_frame, | |
1498 | (!list_empty(&dev->outqueue)) || | |
1499 | (dev->state & DEV_DISCONNECTED)); | |
1500 | if (ret) | |
1501 | return ret; | |
1502 | if (dev->state & DEV_DISCONNECTED) | |
1503 | return -ENODEV; | |
1504 | } | |
1505 | ||
1506 | spin_lock_irqsave(&dev->queue_lock, lock_flags); | |
1507 | f = list_entry(dev->outqueue.next, | |
3acf2809 | 1508 | struct em28xx_frame_t, frame); |
a6c2ba28 | 1509 | list_del(dev->outqueue.next); |
1510 | spin_unlock_irqrestore(&dev->queue_lock, lock_flags); | |
1511 | ||
1512 | f->state = F_UNUSED; | |
1513 | memcpy(b, &f->buf, sizeof(*b)); | |
1514 | ||
1515 | if (f->vma_use_count) | |
1516 | b->flags |= V4L2_BUF_FLAG_MAPPED; | |
1517 | ||
1518 | return 0; | |
1519 | } | |
1520 | default: | |
3acf2809 MCC |
1521 | return em28xx_do_ioctl(inode, filp, dev, cmd, arg, |
1522 | em28xx_video_do_ioctl); | |
a6c2ba28 | 1523 | } |
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | /* | |
3acf2809 MCC |
1528 | * em28xx_v4l2_ioctl() |
1529 | * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl() | |
a6c2ba28 | 1530 | */ |
3acf2809 | 1531 | static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp, |
a6c2ba28 | 1532 | unsigned int cmd, unsigned long arg) |
1533 | { | |
a6c2ba28 | 1534 | int ret = 0; |
3acf2809 | 1535 | struct em28xx *dev = filp->private_data; |
a6c2ba28 | 1536 | |
1537 | if (down_interruptible(&dev->fileop_lock)) | |
1538 | return -ERESTARTSYS; | |
1539 | ||
1540 | if (dev->state & DEV_DISCONNECTED) { | |
3acf2809 | 1541 | em28xx_errdev("v4l2 ioctl: device not present\n"); |
a6c2ba28 | 1542 | up(&dev->fileop_lock); |
1543 | return -ENODEV; | |
1544 | } | |
1545 | ||
1546 | if (dev->state & DEV_MISCONFIGURED) { | |
3acf2809 | 1547 | em28xx_errdev |
a6c2ba28 | 1548 | ("v4l2 ioctl: device is misconfigured; close and open it again\n"); |
1549 | up(&dev->fileop_lock); | |
1550 | return -EIO; | |
1551 | } | |
1552 | ||
3acf2809 | 1553 | ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl); |
a6c2ba28 | 1554 | |
1555 | up(&dev->fileop_lock); | |
1556 | ||
1557 | return ret; | |
1558 | } | |
1559 | ||
3acf2809 | 1560 | static struct file_operations em28xx_v4l_fops = { |
a6c2ba28 | 1561 | .owner = THIS_MODULE, |
3acf2809 MCC |
1562 | .open = em28xx_v4l2_open, |
1563 | .release = em28xx_v4l2_close, | |
1564 | .ioctl = em28xx_v4l2_ioctl, | |
1565 | .read = em28xx_v4l2_read, | |
1566 | .poll = em28xx_v4l2_poll, | |
1567 | .mmap = em28xx_v4l2_mmap, | |
a6c2ba28 | 1568 | .llseek = no_llseek, |
1569 | }; | |
1570 | ||
1571 | /******************************** usb interface *****************************************/ | |
1572 | ||
1573 | /* | |
3acf2809 | 1574 | * em28xx_init_dev() |
a6c2ba28 | 1575 | * allocates and inits the device structs, registers i2c bus and v4l device |
1576 | */ | |
3acf2809 | 1577 | static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, |
a6c2ba28 | 1578 | int minor, int model) |
1579 | { | |
3acf2809 | 1580 | struct em28xx *dev = *devhandle; |
a6c2ba28 | 1581 | int retval = -ENOMEM; |
1582 | int errCode, i; | |
1583 | unsigned int maxh, maxw; | |
1584 | struct usb_interface *uif; | |
1585 | ||
a6c2ba28 | 1586 | dev->udev = udev; |
1587 | dev->model = model; | |
1588 | init_MUTEX(&dev->lock); | |
1589 | init_waitqueue_head(&dev->open); | |
1590 | ||
3acf2809 MCC |
1591 | dev->em28xx_write_regs = em28xx_write_regs; |
1592 | dev->em28xx_read_reg = em28xx_read_reg; | |
1593 | dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len; | |
1594 | dev->em28xx_write_regs_req = em28xx_write_regs_req; | |
1595 | dev->em28xx_read_reg_req = em28xx_read_reg_req; | |
1596 | dev->is_em2800 = em28xx_boards[model].is_em2800; | |
1597 | dev->has_tuner = em28xx_boards[model].has_tuner; | |
1598 | dev->has_msp34xx = em28xx_boards[model].has_msp34xx; | |
1599 | dev->tda9887_conf = em28xx_boards[model].tda9887_conf; | |
1600 | dev->decoder = em28xx_boards[model].decoder; | |
a6c2ba28 | 1601 | |
1602 | if (tuner >= 0) | |
1603 | dev->tuner_type = tuner; | |
1604 | else | |
3acf2809 | 1605 | dev->tuner_type = em28xx_boards[model].tuner_type; |
a6c2ba28 | 1606 | |
3acf2809 | 1607 | dev->video_inputs = em28xx_boards[model].vchannels; |
a6c2ba28 | 1608 | |
1609 | for (i = 0; i < TVNORMS; i++) | |
3acf2809 | 1610 | if (em28xx_boards[model].norm == tvnorms[i].mode) |
a6c2ba28 | 1611 | break; |
1612 | if (i == TVNORMS) | |
1613 | i = 0; | |
1614 | ||
1615 | dev->tvnorm = &tvnorms[i]; /* set default norm */ | |
1616 | ||
3acf2809 | 1617 | em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name); |
a6c2ba28 | 1618 | |
1619 | maxw = norm_maxw(dev); | |
1620 | maxh = norm_maxh(dev); | |
1621 | ||
1622 | /* set default image size */ | |
1623 | dev->width = maxw; | |
1624 | dev->height = maxh; | |
3acf2809 | 1625 | dev->interlaced = EM28XX_INTERLACED_DEFAULT; |
a6c2ba28 | 1626 | dev->field_size = dev->width * dev->height; |
1627 | dev->frame_size = | |
1628 | dev->interlaced ? dev->field_size << 1 : dev->field_size; | |
1629 | dev->bytesperline = dev->width * 2; | |
1630 | dev->hscale = 0; | |
1631 | dev->vscale = 0; | |
1632 | dev->ctl_input = 2; | |
1633 | ||
1634 | /* setup video picture settings for saa7113h */ | |
1635 | memset(&dev->vpic, 0, sizeof(dev->vpic)); | |
1636 | dev->vpic.colour = 128 << 8; | |
1637 | dev->vpic.hue = 128 << 8; | |
1638 | dev->vpic.brightness = 128 << 8; | |
1639 | dev->vpic.contrast = 192 << 8; | |
1640 | dev->vpic.whiteness = 128 << 8; /* This one isn't used */ | |
1641 | dev->vpic.depth = 16; | |
1642 | dev->vpic.palette = VIDEO_PALETTE_YUV422; | |
1643 | ||
1644 | /* compute alternate max packet sizes */ | |
1645 | uif = dev->udev->actconfig->interface[0]; | |
1646 | dev->alt_max_pkt_size[0] = 0; | |
3acf2809 | 1647 | for (i = 1; i <= EM28XX_MAX_ALT && i < uif->num_altsetting ; i++) { |
a6c2ba28 | 1648 | u16 tmp = |
1649 | le16_to_cpu(uif->altsetting[i].endpoint[1].desc. | |
1650 | wMaxPacketSize); | |
1651 | dev->alt_max_pkt_size[i] = | |
1652 | (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); | |
1653 | } | |
1654 | ||
1655 | #ifdef CONFIG_MODULES | |
1656 | /* request some modules */ | |
3acf2809 | 1657 | if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114) |
91cad0f2 | 1658 | request_module("saa711x"); |
3acf2809 | 1659 | if (dev->decoder == EM28XX_TVP5150) |
a6c2ba28 | 1660 | request_module("tvp5150"); |
1661 | if (dev->has_tuner) | |
1662 | request_module("tuner"); | |
1663 | if (dev->tda9887_conf) | |
1664 | request_module("tda9887"); | |
1665 | #endif | |
3acf2809 | 1666 | errCode = em28xx_config(dev); |
a6c2ba28 | 1667 | if (errCode) { |
3acf2809 | 1668 | em28xx_errdev("error configuring device\n"); |
a6c2ba28 | 1669 | kfree(dev); |
1670 | return -ENOMEM; | |
1671 | } | |
1672 | ||
1673 | down(&dev->lock); | |
1674 | /* register i2c bus */ | |
3acf2809 | 1675 | em28xx_i2c_register(dev); |
a6c2ba28 | 1676 | |
1677 | /* Do board specific init and eeprom reading */ | |
3acf2809 | 1678 | em28xx_card_setup(dev); |
a6c2ba28 | 1679 | |
1680 | /* configure the device */ | |
3acf2809 | 1681 | em28xx_config_i2c(dev); |
a6c2ba28 | 1682 | |
1683 | up(&dev->lock); | |
1684 | ||
3acf2809 | 1685 | errCode = em28xx_config(dev); |
a6c2ba28 | 1686 | |
1687 | #ifdef CONFIG_MODULES | |
1688 | if (dev->has_msp34xx) | |
1689 | request_module("msp3400"); | |
1690 | #endif | |
1691 | /* allocate and fill v4l2 device struct */ | |
1692 | dev->vdev = video_device_alloc(); | |
1693 | if (NULL == dev->vdev) { | |
3acf2809 | 1694 | em28xx_errdev("cannot allocate video_device.\n"); |
a6c2ba28 | 1695 | kfree(dev); |
1696 | return -ENOMEM; | |
1697 | } | |
1698 | ||
a6c2ba28 | 1699 | dev->vdev->type = VID_TYPE_CAPTURE; |
1700 | if (dev->has_tuner) | |
1701 | dev->vdev->type |= VID_TYPE_TUNER; | |
1702 | dev->vdev->hardware = 0; | |
3acf2809 | 1703 | dev->vdev->fops = &em28xx_v4l_fops; |
a6c2ba28 | 1704 | dev->vdev->minor = -1; |
1705 | dev->vdev->dev = &dev->udev->dev; | |
1706 | dev->vdev->release = video_device_release; | |
1707 | snprintf(dev->vdev->name, sizeof(dev->vdev->name), "%s", | |
3acf2809 MCC |
1708 | "em28xx video"); |
1709 | list_add_tail(&dev->devlist,&em28xx_devlist); | |
a6c2ba28 | 1710 | |
1711 | /* register v4l2 device */ | |
1712 | down(&dev->lock); | |
1713 | if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1))) { | |
3acf2809 | 1714 | em28xx_errdev("unable to register video device (error=%i).\n", |
a6c2ba28 | 1715 | retval); |
1716 | up(&dev->lock); | |
9c75541f | 1717 | list_del(&dev->devlist); |
a6c2ba28 | 1718 | video_device_release(dev->vdev); |
1719 | kfree(dev); | |
1720 | return -ENODEV; | |
1721 | } | |
1722 | if (dev->has_msp34xx) { | |
1723 | /* Send a reset to other chips via gpio */ | |
3acf2809 | 1724 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1); |
a6c2ba28 | 1725 | udelay(2500); |
3acf2809 | 1726 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1); |
a6c2ba28 | 1727 | udelay(2500); |
1728 | ||
1729 | } | |
1730 | video_mux(dev, 0); | |
1731 | ||
1732 | up(&dev->lock); | |
1733 | ||
3acf2809 | 1734 | em28xx_info("V4L2 device registered as /dev/video%d\n", |
a6c2ba28 | 1735 | dev->vdev->minor); |
1736 | ||
a6c2ba28 | 1737 | return 0; |
1738 | } | |
1739 | ||
1740 | /* | |
3acf2809 | 1741 | * em28xx_usb_probe() |
a6c2ba28 | 1742 | * checks for supported devices |
1743 | */ | |
3acf2809 | 1744 | static int em28xx_usb_probe(struct usb_interface *interface, |
a6c2ba28 | 1745 | const struct usb_device_id *id) |
1746 | { | |
1747 | const struct usb_endpoint_descriptor *endpoint; | |
1748 | struct usb_device *udev; | |
3acf2809 | 1749 | struct em28xx *dev = NULL; |
a6c2ba28 | 1750 | int retval = -ENODEV; |
d5e52653 | 1751 | int model,i,nr,ifnum; |
a6c2ba28 | 1752 | |
1753 | udev = usb_get_dev(interface_to_usbdev(interface)); | |
d5e52653 MCC |
1754 | ifnum = interface->altsetting[0].desc.bInterfaceNumber; |
1755 | ||
a6c2ba28 | 1756 | |
596d92d5 | 1757 | /* Don't register audio interfaces */ |
91cad0f2 | 1758 | if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) { |
3acf2809 | 1759 | em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n", |
91cad0f2 MCC |
1760 | udev->descriptor.idVendor,udev->descriptor.idProduct, |
1761 | ifnum, | |
1762 | interface->altsetting[0].desc.bInterfaceClass); | |
596d92d5 | 1763 | return -ENODEV; |
91cad0f2 MCC |
1764 | } |
1765 | ||
3acf2809 | 1766 | em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n", |
91cad0f2 MCC |
1767 | udev->descriptor.idVendor,udev->descriptor.idProduct, |
1768 | ifnum, | |
1769 | interface->altsetting[0].desc.bInterfaceClass); | |
596d92d5 | 1770 | |
d5e52653 MCC |
1771 | endpoint = &interface->cur_altsetting->endpoint[1].desc; |
1772 | ||
a6c2ba28 | 1773 | /* check if the the device has the iso in endpoint at the correct place */ |
1774 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != | |
1775 | USB_ENDPOINT_XFER_ISOC) { | |
3acf2809 | 1776 | em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); |
a6c2ba28 | 1777 | return -ENODEV; |
1778 | } | |
1779 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { | |
3acf2809 | 1780 | em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); |
a6c2ba28 | 1781 | return -ENODEV; |
1782 | } | |
1783 | ||
596d92d5 MCC |
1784 | model=id->driver_info; |
1785 | nr=interface->minor; | |
1786 | ||
3acf2809 MCC |
1787 | if (nr>EM28XX_MAXBOARDS) { |
1788 | printk ("em28xx: Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS); | |
596d92d5 MCC |
1789 | return -ENOMEM; |
1790 | } | |
1791 | ||
1792 | /* allocate memory for our device state and initialize it */ | |
1793 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | |
1794 | if (dev == NULL) { | |
3acf2809 | 1795 | em28xx_err(DRIVER_NAME ": out of memory!\n"); |
596d92d5 MCC |
1796 | return -ENOMEM; |
1797 | } | |
1798 | memset(dev, 0, sizeof(*dev)); | |
1799 | ||
3acf2809 | 1800 | snprintf(dev->name, 29, "em28xx #%d", nr); |
596d92d5 | 1801 | |
3acf2809 | 1802 | if ((card[nr]>=0)&&(card[nr]<em28xx_bcount)) |
596d92d5 MCC |
1803 | model=card[nr]; |
1804 | ||
1805 | if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) { | |
1806 | printk( "%s: Your board has no eeprom inside it and thus can't\n" | |
1807 | "%s: be autodetected. Please pass card=<n> insmod option to\n" | |
1808 | "%s: workaround that. Redirect complaints to the vendor of\n" | |
1809 | "%s: the TV card. Best regards,\n" | |
1810 | "%s: -- tux\n", | |
1811 | dev->name,dev->name,dev->name,dev->name,dev->name); | |
1812 | printk("%s: Here is a list of valid choices for the card=<n> insmod option:\n", | |
1813 | dev->name); | |
3acf2809 | 1814 | for (i = 0; i < em28xx_bcount; i++) { |
596d92d5 | 1815 | printk("%s: card=%d -> %s\n", |
3acf2809 | 1816 | dev->name, i, em28xx_boards[i].name); |
596d92d5 MCC |
1817 | } |
1818 | } | |
1819 | ||
a6c2ba28 | 1820 | /* allocate device struct */ |
3acf2809 | 1821 | retval = em28xx_init_dev(&dev, udev, nr, model); |
a6c2ba28 | 1822 | if (retval) |
1823 | return retval; | |
1824 | ||
3acf2809 | 1825 | em28xx_info("Found %s\n", em28xx_boards[model].name); |
a6c2ba28 | 1826 | |
1827 | /* save our data pointer in this interface device */ | |
1828 | usb_set_intfdata(interface, dev); | |
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | /* | |
3acf2809 | 1833 | * em28xx_usb_disconnect() |
a6c2ba28 | 1834 | * called when the device gets diconencted |
1835 | * video device will be unregistered on v4l2_close in case it is still open | |
1836 | */ | |
3acf2809 | 1837 | static void em28xx_usb_disconnect(struct usb_interface *interface) |
a6c2ba28 | 1838 | { |
3acf2809 | 1839 | struct em28xx *dev = usb_get_intfdata(interface); |
a6c2ba28 | 1840 | usb_set_intfdata(interface, NULL); |
1841 | ||
1842 | if (!dev) | |
1843 | return; | |
1844 | ||
3acf2809 | 1845 | down_write(&em28xx_disconnect); |
a6c2ba28 | 1846 | |
1847 | down(&dev->lock); | |
1848 | ||
3acf2809 | 1849 | em28xx_info("disconnecting %s\n", dev->vdev->name); |
a6c2ba28 | 1850 | |
1851 | wake_up_interruptible_all(&dev->open); | |
1852 | ||
1853 | if (dev->users) { | |
3acf2809 | 1854 | em28xx_warn |
a6c2ba28 | 1855 | ("device /dev/video%d is open! Deregistration and memory " |
1856 | "deallocation are deferred on close.\n", dev->vdev->minor); | |
1857 | dev->state |= DEV_MISCONFIGURED; | |
3acf2809 | 1858 | em28xx_uninit_isoc(dev); |
a6c2ba28 | 1859 | dev->state |= DEV_DISCONNECTED; |
1860 | wake_up_interruptible(&dev->wait_frame); | |
1861 | wake_up_interruptible(&dev->wait_stream); | |
1862 | } else { | |
1863 | dev->state |= DEV_DISCONNECTED; | |
3acf2809 | 1864 | em28xx_release_resources(dev); |
a6c2ba28 | 1865 | } |
1866 | ||
1867 | up(&dev->lock); | |
1868 | ||
1869 | if (!dev->users) | |
1870 | kfree(dev); | |
1871 | ||
3acf2809 | 1872 | up_write(&em28xx_disconnect); |
a6c2ba28 | 1873 | |
1874 | } | |
1875 | ||
3acf2809 | 1876 | static struct usb_driver em28xx_usb_driver = { |
a6c2ba28 | 1877 | .owner = THIS_MODULE, |
3acf2809 MCC |
1878 | .name = "em28xx", |
1879 | .probe = em28xx_usb_probe, | |
1880 | .disconnect = em28xx_usb_disconnect, | |
1881 | .id_table = em28xx_id_table, | |
a6c2ba28 | 1882 | }; |
1883 | ||
3acf2809 | 1884 | static int __init em28xx_module_init(void) |
a6c2ba28 | 1885 | { |
1886 | int result; | |
1887 | ||
1888 | printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n", | |
3acf2809 MCC |
1889 | (EM28XX_VERSION_CODE >> 16) & 0xff, |
1890 | (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff); | |
a6c2ba28 | 1891 | #ifdef SNAPSHOT |
1892 | printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n", | |
1893 | SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100); | |
1894 | #endif | |
1895 | ||
1896 | /* register this driver with the USB subsystem */ | |
3acf2809 | 1897 | result = usb_register(&em28xx_usb_driver); |
a6c2ba28 | 1898 | if (result) |
3acf2809 | 1899 | em28xx_err(DRIVER_NAME |
a6c2ba28 | 1900 | " usb_register failed. Error number %d.\n", result); |
1901 | ||
1902 | return result; | |
1903 | } | |
1904 | ||
3acf2809 | 1905 | static void __exit em28xx_module_exit(void) |
a6c2ba28 | 1906 | { |
1907 | /* deregister this driver with the USB subsystem */ | |
3acf2809 | 1908 | usb_deregister(&em28xx_usb_driver); |
a6c2ba28 | 1909 | } |
1910 | ||
3acf2809 MCC |
1911 | module_init(em28xx_module_init); |
1912 | module_exit(em28xx_module_exit); |