]>
Commit | Line | Data |
---|---|---|
a6c2ba28 | 1 | /* |
6ea54d93 DSL |
2 | em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB |
3 | video capture devices | |
a6c2ba28 | 4 | |
f7abcd38 MCC |
5 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
6 | Markus Rechberger <mrechberger@gmail.com> | |
2e7c6dc3 | 7 | Mauro Carvalho Chehab <mchehab@infradead.org> |
f7abcd38 | 8 | Sascha Sommer <saschasommer@freenet.de> |
a6c2ba28 | 9 | |
439090d7 MCC |
10 | Some parts based on SN9C10x PC Camera Controllers GPL driver made |
11 | by Luca Risolia <luca.risolia@studio.unibo.it> | |
12 | ||
a6c2ba28 | 13 | This program is free software; you can redistribute it and/or modify |
14 | it under the terms of the GNU General Public License as published by | |
15 | the Free Software Foundation; either version 2 of the License, or | |
16 | (at your option) any later version. | |
17 | ||
18 | This program is distributed in the hope that it will be useful, | |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
22 | ||
23 | You should have received a copy of the GNU General Public License | |
24 | along with this program; if not, write to the Free Software | |
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
26 | */ | |
27 | ||
28 | #include <linux/init.h> | |
29 | #include <linux/list.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/kernel.h> | |
e5589bef | 32 | #include <linux/bitmap.h> |
a6c2ba28 | 33 | #include <linux/usb.h> |
a6c2ba28 | 34 | #include <linux/i2c.h> |
b296fc60 | 35 | #include <linux/version.h> |
6d35c8f6 | 36 | #include <linux/mm.h> |
1e4baed3 | 37 | #include <linux/mutex.h> |
a6c2ba28 | 38 | |
f7abcd38 | 39 | #include "em28xx.h" |
c0477ad9 | 40 | #include <media/v4l2-common.h> |
2474ed44 | 41 | #include <media/msp3400.h> |
ed086314 | 42 | #include <media/tuner.h> |
a6c2ba28 | 43 | |
f7abcd38 MCC |
44 | #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ |
45 | "Markus Rechberger <mrechberger@gmail.com>, " \ | |
2e7c6dc3 | 46 | "Mauro Carvalho Chehab <mchehab@infradead.org>, " \ |
f7abcd38 | 47 | "Sascha Sommer <saschasommer@freenet.de>" |
a6c2ba28 | 48 | |
f7abcd38 MCC |
49 | #define DRIVER_NAME "em28xx" |
50 | #define DRIVER_DESC "Empia em28xx based USB video device driver" | |
195a4ef6 | 51 | #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0) |
a6c2ba28 | 52 | |
3acf2809 | 53 | #define em28xx_videodbg(fmt, arg...) do {\ |
4ac97914 MCC |
54 | if (video_debug) \ |
55 | printk(KERN_INFO "%s %s :"fmt, \ | |
d80e134d | 56 | dev->name, __func__ , ##arg); } while (0) |
a6c2ba28 | 57 | |
ad0ebb96 | 58 | static unsigned int isoc_debug; |
f245e549 MCC |
59 | module_param(isoc_debug, int, 0644); |
60 | MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); | |
ad0ebb96 | 61 | |
6ea54d93 DSL |
62 | #define em28xx_isocdbg(fmt, arg...) \ |
63 | do {\ | |
64 | if (isoc_debug) { \ | |
ad0ebb96 | 65 | printk(KERN_INFO "%s %s :"fmt, \ |
6ea54d93 DSL |
66 | dev->name, __func__ , ##arg); \ |
67 | } \ | |
68 | } while (0) | |
ad0ebb96 | 69 | |
a6c2ba28 | 70 | MODULE_AUTHOR(DRIVER_AUTHOR); |
71 | MODULE_DESCRIPTION(DRIVER_DESC); | |
72 | MODULE_LICENSE("GPL"); | |
73 | ||
3acf2809 | 74 | static LIST_HEAD(em28xx_devlist); |
9c75541f | 75 | |
9d4d9c05 | 76 | static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
e5589bef | 77 | static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
0be43754 MCC |
78 | static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
79 | static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; | |
80 | ||
596d92d5 | 81 | module_param_array(card, int, NULL, 0444); |
e5589bef MCC |
82 | module_param_array(video_nr, int, NULL, 0444); |
83 | module_param_array(vbi_nr, int, NULL, 0444); | |
0be43754 MCC |
84 | module_param_array(radio_nr, int, NULL, 0444); |
85 | MODULE_PARM_DESC(card, "card type"); | |
86 | MODULE_PARM_DESC(video_nr, "video device numbers"); | |
87 | MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); | |
88 | MODULE_PARM_DESC(radio_nr, "radio device numbers"); | |
596d92d5 | 89 | |
ff699e6b | 90 | static unsigned int video_debug; |
6ea54d93 DSL |
91 | module_param(video_debug, int, 0644); |
92 | MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); | |
a6c2ba28 | 93 | |
e5589bef MCC |
94 | /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */ |
95 | static unsigned long em28xx_devused; | |
96 | ||
a6c2ba28 | 97 | /* supported controls */ |
c0477ad9 | 98 | /* Common to all boards */ |
3acf2809 | 99 | static struct v4l2_queryctrl em28xx_qctrl[] = { |
c0477ad9 MCC |
100 | { |
101 | .id = V4L2_CID_AUDIO_VOLUME, | |
102 | .type = V4L2_CTRL_TYPE_INTEGER, | |
103 | .name = "Volume", | |
104 | .minimum = 0x0, | |
105 | .maximum = 0x1f, | |
106 | .step = 0x1, | |
107 | .default_value = 0x1f, | |
108 | .flags = 0, | |
6ea54d93 | 109 | }, { |
c0477ad9 MCC |
110 | .id = V4L2_CID_AUDIO_MUTE, |
111 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
112 | .name = "Mute", | |
113 | .minimum = 0, | |
114 | .maximum = 1, | |
115 | .step = 1, | |
116 | .default_value = 1, | |
117 | .flags = 0, | |
118 | } | |
119 | }; | |
120 | ||
3acf2809 | 121 | static struct usb_driver em28xx_usb_driver; |
a6c2ba28 | 122 | |
ad0ebb96 MCC |
123 | /* ------------------------------------------------------------------ |
124 | DMA and thread functions | |
125 | ------------------------------------------------------------------*/ | |
126 | ||
127 | /* | |
128 | * Announces that a buffer were filled and request the next | |
129 | */ | |
f245e549 | 130 | static inline void buffer_filled(struct em28xx *dev, |
ad0ebb96 MCC |
131 | struct em28xx_dmaqueue *dma_q, |
132 | struct em28xx_buffer *buf) | |
133 | { | |
ad0ebb96 MCC |
134 | /* Advice that buffer was filled */ |
135 | em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); | |
136 | buf->vb.state = VIDEOBUF_DONE; | |
137 | buf->vb.field_count++; | |
138 | do_gettimeofday(&buf->vb.ts); | |
139 | ||
cb784724 MCC |
140 | dev->isoc_ctl.buf = NULL; |
141 | ||
ad0ebb96 MCC |
142 | list_del(&buf->vb.queue); |
143 | wake_up(&buf->vb.done); | |
144 | } | |
145 | ||
146 | /* | |
147 | * Identify the buffer header type and properly handles | |
148 | */ | |
149 | static void em28xx_copy_video(struct em28xx *dev, | |
150 | struct em28xx_dmaqueue *dma_q, | |
151 | struct em28xx_buffer *buf, | |
152 | unsigned char *p, | |
153 | unsigned char *outp, unsigned long len) | |
154 | { | |
155 | void *fieldstart, *startwrite, *startread; | |
f245e549 | 156 | int linesdone, currlinedone, offset, lencopy, remain; |
44dc733c | 157 | int bytesperline = dev->width << 1; |
ad0ebb96 MCC |
158 | |
159 | if (dma_q->pos + len > buf->vb.size) | |
160 | len = buf->vb.size - dma_q->pos; | |
161 | ||
d7aa8020 | 162 | if (p[0] != 0x88 && p[0] != 0x22) { |
ad0ebb96 MCC |
163 | em28xx_isocdbg("frame is not complete\n"); |
164 | len += 4; | |
165 | } else | |
f245e549 | 166 | p += 4; |
ad0ebb96 MCC |
167 | |
168 | startread = p; | |
169 | remain = len; | |
170 | ||
171 | /* Interlaces frame */ | |
172 | if (buf->top_field) | |
173 | fieldstart = outp; | |
174 | else | |
44dc733c | 175 | fieldstart = outp + bytesperline; |
ad0ebb96 | 176 | |
44dc733c MCC |
177 | linesdone = dma_q->pos / bytesperline; |
178 | currlinedone = dma_q->pos % bytesperline; | |
179 | offset = linesdone * bytesperline * 2 + currlinedone; | |
ad0ebb96 | 180 | startwrite = fieldstart + offset; |
44dc733c | 181 | lencopy = bytesperline - currlinedone; |
ad0ebb96 MCC |
182 | lencopy = lencopy > remain ? remain : lencopy; |
183 | ||
f245e549 | 184 | if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { |
ea8df7e0 | 185 | em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", |
f245e549 MCC |
186 | ((char *)startwrite + lencopy) - |
187 | ((char *)outp + buf->vb.size)); | |
188 | lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite; | |
d7aa8020 | 189 | } |
e0fadfd3 AT |
190 | if (lencopy <= 0) |
191 | return; | |
d7aa8020 | 192 | memcpy(startwrite, startread, lencopy); |
ad0ebb96 MCC |
193 | |
194 | remain -= lencopy; | |
195 | ||
196 | while (remain > 0) { | |
44dc733c | 197 | startwrite += lencopy + bytesperline; |
ad0ebb96 | 198 | startread += lencopy; |
44dc733c | 199 | if (bytesperline > remain) |
ad0ebb96 MCC |
200 | lencopy = remain; |
201 | else | |
44dc733c | 202 | lencopy = bytesperline; |
ad0ebb96 | 203 | |
f245e549 | 204 | if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { |
ea8df7e0 | 205 | em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", |
f245e549 MCC |
206 | ((char *)startwrite + lencopy) - |
207 | ((char *)outp + buf->vb.size)); | |
208 | lencopy = remain = (char *)outp + buf->vb.size - | |
209 | (char *)startwrite; | |
d7aa8020 | 210 | } |
f245e549 MCC |
211 | if (lencopy <= 0) |
212 | break; | |
d7aa8020 AT |
213 | |
214 | memcpy(startwrite, startread, lencopy); | |
ad0ebb96 MCC |
215 | |
216 | remain -= lencopy; | |
217 | } | |
218 | ||
219 | dma_q->pos += len; | |
220 | } | |
221 | ||
f245e549 | 222 | static inline void print_err_status(struct em28xx *dev, |
ad0ebb96 MCC |
223 | int packet, int status) |
224 | { | |
225 | char *errmsg = "Unknown"; | |
226 | ||
f245e549 | 227 | switch (status) { |
ad0ebb96 MCC |
228 | case -ENOENT: |
229 | errmsg = "unlinked synchronuously"; | |
230 | break; | |
231 | case -ECONNRESET: | |
232 | errmsg = "unlinked asynchronuously"; | |
233 | break; | |
234 | case -ENOSR: | |
235 | errmsg = "Buffer error (overrun)"; | |
236 | break; | |
237 | case -EPIPE: | |
238 | errmsg = "Stalled (device not responding)"; | |
239 | break; | |
240 | case -EOVERFLOW: | |
241 | errmsg = "Babble (bad cable?)"; | |
242 | break; | |
243 | case -EPROTO: | |
244 | errmsg = "Bit-stuff error (bad cable?)"; | |
245 | break; | |
246 | case -EILSEQ: | |
247 | errmsg = "CRC/Timeout (could be anything)"; | |
248 | break; | |
249 | case -ETIME: | |
250 | errmsg = "Device does not respond"; | |
251 | break; | |
252 | } | |
f245e549 | 253 | if (packet < 0) { |
ad0ebb96 MCC |
254 | em28xx_isocdbg("URB status %d [%s].\n", status, errmsg); |
255 | } else { | |
256 | em28xx_isocdbg("URB packet %d, status %d [%s].\n", | |
257 | packet, status, errmsg); | |
258 | } | |
259 | } | |
260 | ||
261 | /* | |
262 | * video-buf generic routine to get the next available buffer | |
263 | */ | |
3b5fa928 | 264 | static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, |
ad0ebb96 MCC |
265 | struct em28xx_buffer **buf) |
266 | { | |
267 | struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); | |
dbecb44c | 268 | char *outp; |
ad0ebb96 | 269 | |
dbecb44c MCC |
270 | if (list_empty(&dma_q->active)) { |
271 | em28xx_isocdbg("No active queue to serve\n"); | |
dbecb44c MCC |
272 | dev->isoc_ctl.buf = NULL; |
273 | *buf = NULL; | |
3b5fa928 | 274 | return; |
dbecb44c MCC |
275 | } |
276 | ||
dbecb44c MCC |
277 | /* Get the next buffer */ |
278 | *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); | |
279 | ||
dbecb44c MCC |
280 | /* Cleans up buffer - Usefull for testing for frame/URB loss */ |
281 | outp = videobuf_to_vmalloc(&(*buf)->vb); | |
282 | memset(outp, 0, (*buf)->vb.size); | |
cb784724 MCC |
283 | |
284 | dev->isoc_ctl.buf = *buf; | |
285 | ||
3b5fa928 | 286 | return; |
ad0ebb96 MCC |
287 | } |
288 | ||
289 | /* | |
290 | * Controls the isoc copy of each urb packet | |
291 | */ | |
579f72e4 | 292 | static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb) |
ad0ebb96 | 293 | { |
d7aa8020 | 294 | struct em28xx_buffer *buf; |
ad0ebb96 | 295 | struct em28xx_dmaqueue *dma_q = urb->context; |
3b5fa928 | 296 | unsigned char *outp = NULL; |
ad0ebb96 | 297 | int i, len = 0, rc = 1; |
d7aa8020 | 298 | unsigned char *p; |
ad0ebb96 MCC |
299 | |
300 | if (!dev) | |
301 | return 0; | |
302 | ||
303 | if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) | |
304 | return 0; | |
305 | ||
f245e549 MCC |
306 | if (urb->status < 0) { |
307 | print_err_status(dev, -1, urb->status); | |
ad0ebb96 MCC |
308 | if (urb->status == -ENOENT) |
309 | return 0; | |
310 | } | |
311 | ||
3b5fa928 AT |
312 | buf = dev->isoc_ctl.buf; |
313 | if (buf != NULL) | |
314 | outp = videobuf_to_vmalloc(&buf->vb); | |
d7aa8020 | 315 | |
ad0ebb96 MCC |
316 | for (i = 0; i < urb->number_of_packets; i++) { |
317 | int status = urb->iso_frame_desc[i].status; | |
318 | ||
f245e549 MCC |
319 | if (status < 0) { |
320 | print_err_status(dev, i, status); | |
ad0ebb96 MCC |
321 | if (urb->iso_frame_desc[i].status != -EPROTO) |
322 | continue; | |
323 | } | |
324 | ||
f245e549 | 325 | len = urb->iso_frame_desc[i].actual_length - 4; |
ad0ebb96 MCC |
326 | |
327 | if (urb->iso_frame_desc[i].actual_length <= 0) { | |
d7aa8020 | 328 | /* em28xx_isocdbg("packet %d is empty",i); - spammy */ |
ad0ebb96 MCC |
329 | continue; |
330 | } | |
331 | if (urb->iso_frame_desc[i].actual_length > | |
332 | dev->max_pkt_size) { | |
333 | em28xx_isocdbg("packet bigger than packet size"); | |
334 | continue; | |
335 | } | |
336 | ||
337 | p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
ad0ebb96 MCC |
338 | |
339 | /* FIXME: incomplete buffer checks where removed to make | |
340 | logic simpler. Impacts of those changes should be evaluated | |
341 | */ | |
b4916f8c MCC |
342 | if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { |
343 | em28xx_isocdbg("VBI HEADER!!!\n"); | |
344 | /* FIXME: Should add vbi copy */ | |
345 | continue; | |
346 | } | |
d7aa8020 | 347 | if (p[0] == 0x22 && p[1] == 0x5a) { |
78bb3949 MCC |
348 | em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], |
349 | len, (p[2] & 1)? "odd" : "even"); | |
d7aa8020 | 350 | |
3b5fa928 AT |
351 | if (!(p[2] & 1)) { |
352 | if (buf != NULL) | |
353 | buffer_filled(dev, dma_q, buf); | |
354 | get_next_buf(dma_q, &buf); | |
355 | if (buf == NULL) | |
356 | outp = NULL; | |
357 | else | |
358 | outp = videobuf_to_vmalloc(&buf->vb); | |
359 | } | |
360 | ||
361 | if (buf != NULL) { | |
362 | if (p[2] & 1) | |
363 | buf->top_field = 0; | |
364 | else | |
365 | buf->top_field = 1; | |
e0fadfd3 | 366 | } |
b4916f8c | 367 | |
ad0ebb96 | 368 | dma_q->pos = 0; |
ad0ebb96 | 369 | } |
3b5fa928 AT |
370 | if (buf != NULL) |
371 | em28xx_copy_video(dev, dma_q, buf, p, outp, len); | |
ad0ebb96 MCC |
372 | } |
373 | return rc; | |
374 | } | |
375 | ||
ad0ebb96 MCC |
376 | /* ------------------------------------------------------------------ |
377 | Videobuf operations | |
378 | ------------------------------------------------------------------*/ | |
379 | ||
380 | static int | |
381 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) | |
382 | { | |
383 | struct em28xx_fh *fh = vq->priv_data; | |
d2d9fbfd MCC |
384 | struct em28xx *dev = fh->dev; |
385 | struct v4l2_frequency f; | |
ad0ebb96 | 386 | |
d7aa8020 | 387 | *size = 16 * fh->dev->width * fh->dev->height >> 3; |
ad0ebb96 MCC |
388 | if (0 == *count) |
389 | *count = EM28XX_DEF_BUF; | |
390 | ||
f245e549 MCC |
391 | if (*count < EM28XX_MIN_BUF) |
392 | *count = EM28XX_MIN_BUF; | |
ad0ebb96 | 393 | |
d2d9fbfd | 394 | /* Ask tuner to go to analog mode */ |
6ea54d93 | 395 | memset(&f, 0, sizeof(f)); |
d2d9fbfd MCC |
396 | f.frequency = dev->ctl_freq; |
397 | ||
398 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); | |
399 | ||
ad0ebb96 MCC |
400 | return 0; |
401 | } | |
402 | ||
3b5fa928 | 403 | /* This is called *without* dev->slock held; please keep it that way */ |
ad0ebb96 MCC |
404 | static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) |
405 | { | |
3b5fa928 AT |
406 | struct em28xx_fh *fh = vq->priv_data; |
407 | struct em28xx *dev = fh->dev; | |
408 | unsigned long flags = 0; | |
ad0ebb96 MCC |
409 | if (in_interrupt()) |
410 | BUG(); | |
411 | ||
3b5fa928 AT |
412 | /* We used to wait for the buffer to finish here, but this didn't work |
413 | because, as we were keeping the state as VIDEOBUF_QUEUED, | |
414 | videobuf_queue_cancel marked it as finished for us. | |
415 | (Also, it could wedge forever if the hardware was misconfigured.) | |
416 | ||
417 | This should be safe; by the time we get here, the buffer isn't | |
418 | queued anymore. If we ever start marking the buffers as | |
419 | VIDEOBUF_ACTIVE, it won't be, though. | |
420 | */ | |
421 | spin_lock_irqsave(&dev->slock, flags); | |
422 | if (dev->isoc_ctl.buf == buf) | |
423 | dev->isoc_ctl.buf = NULL; | |
424 | spin_unlock_irqrestore(&dev->slock, flags); | |
425 | ||
ad0ebb96 MCC |
426 | videobuf_vmalloc_free(&buf->vb); |
427 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
428 | } | |
429 | ||
430 | static int | |
431 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, | |
432 | enum v4l2_field field) | |
433 | { | |
434 | struct em28xx_fh *fh = vq->priv_data; | |
f245e549 | 435 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); |
ad0ebb96 MCC |
436 | struct em28xx *dev = fh->dev; |
437 | int rc = 0, urb_init = 0; | |
ad0ebb96 | 438 | |
ad0ebb96 MCC |
439 | /* FIXME: It assumes depth = 16 */ |
440 | /* The only currently supported format is 16 bits/pixel */ | |
d7aa8020 | 441 | buf->vb.size = 16 * dev->width * dev->height >> 3; |
ad0ebb96 MCC |
442 | |
443 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) | |
444 | return -EINVAL; | |
445 | ||
05612975 BP |
446 | buf->vb.width = dev->width; |
447 | buf->vb.height = dev->height; | |
448 | buf->vb.field = field; | |
ad0ebb96 MCC |
449 | |
450 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { | |
d7aa8020 AT |
451 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
452 | if (rc < 0) | |
ad0ebb96 | 453 | goto fail; |
ad0ebb96 MCC |
454 | } |
455 | ||
ad0ebb96 | 456 | if (!dev->isoc_ctl.num_bufs) |
f245e549 | 457 | urb_init = 1; |
ad0ebb96 MCC |
458 | |
459 | if (urb_init) { | |
579f72e4 AT |
460 | rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS, |
461 | EM28XX_NUM_BUFS, dev->max_pkt_size, | |
c67ec53f | 462 | em28xx_isoc_copy); |
f245e549 | 463 | if (rc < 0) |
ad0ebb96 MCC |
464 | goto fail; |
465 | } | |
466 | ||
467 | buf->vb.state = VIDEOBUF_PREPARED; | |
468 | return 0; | |
469 | ||
470 | fail: | |
f245e549 | 471 | free_buffer(vq, buf); |
ad0ebb96 MCC |
472 | return rc; |
473 | } | |
474 | ||
475 | static void | |
476 | buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) | |
477 | { | |
478 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); | |
479 | struct em28xx_fh *fh = vq->priv_data; | |
f245e549 | 480 | struct em28xx *dev = fh->dev; |
ad0ebb96 | 481 | struct em28xx_dmaqueue *vidq = &dev->vidq; |
ad0ebb96 | 482 | |
d7aa8020 AT |
483 | buf->vb.state = VIDEOBUF_QUEUED; |
484 | list_add_tail(&buf->vb.queue, &vidq->active); | |
485 | ||
ad0ebb96 MCC |
486 | } |
487 | ||
6ea54d93 DSL |
488 | static void buffer_release(struct videobuf_queue *vq, |
489 | struct videobuf_buffer *vb) | |
ad0ebb96 | 490 | { |
f245e549 | 491 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); |
ad0ebb96 | 492 | struct em28xx_fh *fh = vq->priv_data; |
f245e549 | 493 | struct em28xx *dev = (struct em28xx *)fh->dev; |
ad0ebb96 | 494 | |
d7aa8020 | 495 | em28xx_isocdbg("em28xx: called buffer_release\n"); |
ad0ebb96 | 496 | |
f245e549 | 497 | free_buffer(vq, buf); |
ad0ebb96 MCC |
498 | } |
499 | ||
500 | static struct videobuf_queue_ops em28xx_video_qops = { | |
501 | .buf_setup = buffer_setup, | |
502 | .buf_prepare = buffer_prepare, | |
503 | .buf_queue = buffer_queue, | |
504 | .buf_release = buffer_release, | |
505 | }; | |
a6c2ba28 | 506 | |
6ea54d93 | 507 | /********************* v4l2 interface **************************************/ |
a6c2ba28 | 508 | |
a6c2ba28 | 509 | /* |
3acf2809 | 510 | * em28xx_config() |
a6c2ba28 | 511 | * inits registers with sane defaults |
512 | */ | |
3acf2809 | 513 | static int em28xx_config(struct em28xx *dev) |
a6c2ba28 | 514 | { |
515 | ||
516 | /* Sets I2C speed to 100 KHz */ | |
2b2c93ac SS |
517 | if (!dev->is_em2800) |
518 | em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1); | |
a6c2ba28 | 519 | |
520 | /* enable vbi capturing */ | |
e5589bef | 521 | |
6ea54d93 DSL |
522 | /* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */ |
523 | /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */ | |
524 | em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1); | |
e5589bef | 525 | |
a6c2ba28 | 526 | dev->mute = 1; /* maybe not the right place... */ |
527 | dev->volume = 0x1f; | |
539c96d0 | 528 | |
3acf2809 MCC |
529 | em28xx_outfmt_set_yuv422(dev); |
530 | em28xx_colorlevels_set_default(dev); | |
531 | em28xx_compression_disable(dev); | |
a6c2ba28 | 532 | |
533 | return 0; | |
534 | } | |
535 | ||
536 | /* | |
3acf2809 | 537 | * em28xx_config_i2c() |
a6c2ba28 | 538 | * configure i2c attached devices |
539 | */ | |
943a4902 | 540 | static void em28xx_config_i2c(struct em28xx *dev) |
a6c2ba28 | 541 | { |
c7c0b34c HV |
542 | struct v4l2_routing route; |
543 | ||
544 | route.input = INPUT(dev->ctl_input)->vmux; | |
545 | route.output = 0; | |
663d1ba2 | 546 | em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL); |
c7c0b34c | 547 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); |
f5762e44 | 548 | em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); |
a6c2ba28 | 549 | } |
550 | ||
eac94356 MCC |
551 | static void video_mux(struct em28xx *dev, int index) |
552 | { | |
c7c0b34c | 553 | struct v4l2_routing route; |
eac94356 | 554 | |
c7c0b34c HV |
555 | route.input = INPUT(index)->vmux; |
556 | route.output = 0; | |
eac94356 MCC |
557 | dev->ctl_input = index; |
558 | dev->ctl_ainput = INPUT(index)->amux; | |
559 | ||
c7c0b34c | 560 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); |
eac94356 | 561 | |
eac94356 | 562 | if (dev->has_msp34xx) { |
6ea54d93 DSL |
563 | if (dev->i2s_speed) { |
564 | em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, | |
565 | &dev->i2s_speed); | |
566 | } | |
2474ed44 | 567 | route.input = dev->ctl_ainput; |
07151724 | 568 | route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); |
2474ed44 | 569 | /* Note: this is msp3400 specific */ |
6ea54d93 DSL |
570 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, |
571 | &route); | |
eac94356 | 572 | } |
539c96d0 | 573 | |
00b8730f | 574 | em28xx_audio_analog_set(dev); |
eac94356 MCC |
575 | } |
576 | ||
a225452e MCC |
577 | /* Usage lock check functions */ |
578 | static int res_get(struct em28xx_fh *fh) | |
579 | { | |
580 | struct em28xx *dev = fh->dev; | |
581 | int rc = 0; | |
582 | ||
583 | /* This instance already has stream_on */ | |
584 | if (fh->stream_on) | |
585 | return rc; | |
586 | ||
a225452e | 587 | if (dev->stream_on) |
e74153d4 | 588 | return -EINVAL; |
a225452e | 589 | |
e74153d4 MCC |
590 | mutex_lock(&dev->lock); |
591 | dev->stream_on = 1; | |
592 | fh->stream_on = 1; | |
a225452e MCC |
593 | mutex_unlock(&dev->lock); |
594 | return rc; | |
595 | } | |
596 | ||
597 | static int res_check(struct em28xx_fh *fh) | |
598 | { | |
599 | return (fh->stream_on); | |
600 | } | |
601 | ||
602 | static void res_free(struct em28xx_fh *fh) | |
603 | { | |
604 | struct em28xx *dev = fh->dev; | |
605 | ||
606 | mutex_lock(&dev->lock); | |
607 | fh->stream_on = 0; | |
608 | dev->stream_on = 0; | |
609 | mutex_unlock(&dev->lock); | |
610 | } | |
611 | ||
195a4ef6 MCC |
612 | /* |
613 | * em28xx_get_ctrl() | |
614 | * return the current saturation, brightness or contrast, mute state | |
615 | */ | |
616 | static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl) | |
617 | { | |
618 | switch (ctrl->id) { | |
619 | case V4L2_CID_AUDIO_MUTE: | |
620 | ctrl->value = dev->mute; | |
621 | return 0; | |
622 | case V4L2_CID_AUDIO_VOLUME: | |
623 | ctrl->value = dev->volume; | |
624 | return 0; | |
625 | default: | |
626 | return -EINVAL; | |
a6c2ba28 | 627 | } |
195a4ef6 | 628 | } |
a6c2ba28 | 629 | |
195a4ef6 MCC |
630 | /* |
631 | * em28xx_set_ctrl() | |
632 | * mute or set new saturation, brightness or contrast | |
633 | */ | |
634 | static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) | |
635 | { | |
636 | switch (ctrl->id) { | |
637 | case V4L2_CID_AUDIO_MUTE: | |
638 | if (ctrl->value != dev->mute) { | |
639 | dev->mute = ctrl->value; | |
195a4ef6 MCC |
640 | return em28xx_audio_analog_set(dev); |
641 | } | |
642 | return 0; | |
643 | case V4L2_CID_AUDIO_VOLUME: | |
644 | dev->volume = ctrl->value; | |
645 | return em28xx_audio_analog_set(dev); | |
646 | default: | |
647 | return -EINVAL; | |
648 | } | |
649 | } | |
a6c2ba28 | 650 | |
195a4ef6 MCC |
651 | static int check_dev(struct em28xx *dev) |
652 | { | |
653 | if (dev->state & DEV_DISCONNECTED) { | |
654 | em28xx_errdev("v4l2 ioctl: device not present\n"); | |
655 | return -ENODEV; | |
e5589bef | 656 | } |
a6c2ba28 | 657 | |
195a4ef6 MCC |
658 | if (dev->state & DEV_MISCONFIGURED) { |
659 | em28xx_errdev("v4l2 ioctl: device is misconfigured; " | |
660 | "close and open it again\n"); | |
661 | return -EIO; | |
662 | } | |
663 | return 0; | |
664 | } | |
a6c2ba28 | 665 | |
195a4ef6 MCC |
666 | static void get_scale(struct em28xx *dev, |
667 | unsigned int width, unsigned int height, | |
668 | unsigned int *hscale, unsigned int *vscale) | |
669 | { | |
670 | unsigned int maxw = norm_maxw(dev); | |
671 | unsigned int maxh = norm_maxh(dev); | |
672 | ||
673 | *hscale = (((unsigned long)maxw) << 12) / width - 4096L; | |
674 | if (*hscale >= 0x4000) | |
675 | *hscale = 0x3fff; | |
676 | ||
677 | *vscale = (((unsigned long)maxh) << 12) / height - 4096L; | |
678 | if (*vscale >= 0x4000) | |
679 | *vscale = 0x3fff; | |
a6c2ba28 | 680 | } |
681 | ||
195a4ef6 MCC |
682 | /* ------------------------------------------------------------------ |
683 | IOCTL vidioc handling | |
684 | ------------------------------------------------------------------*/ | |
685 | ||
686 | static int vidioc_g_fmt_cap(struct file *file, void *priv, | |
687 | struct v4l2_format *f) | |
a6c2ba28 | 688 | { |
195a4ef6 MCC |
689 | struct em28xx_fh *fh = priv; |
690 | struct em28xx *dev = fh->dev; | |
a6c2ba28 | 691 | |
195a4ef6 | 692 | mutex_lock(&dev->lock); |
e5589bef | 693 | |
195a4ef6 MCC |
694 | f->fmt.pix.width = dev->width; |
695 | f->fmt.pix.height = dev->height; | |
696 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; | |
44dc733c MCC |
697 | f->fmt.pix.bytesperline = dev->width * 2; |
698 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; | |
195a4ef6 | 699 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
e5589bef | 700 | |
195a4ef6 MCC |
701 | /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ |
702 | f->fmt.pix.field = dev->interlaced ? | |
703 | V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; | |
e5589bef | 704 | |
195a4ef6 MCC |
705 | mutex_unlock(&dev->lock); |
706 | return 0; | |
a6c2ba28 | 707 | } |
708 | ||
195a4ef6 MCC |
709 | static int vidioc_try_fmt_cap(struct file *file, void *priv, |
710 | struct v4l2_format *f) | |
a6c2ba28 | 711 | { |
195a4ef6 MCC |
712 | struct em28xx_fh *fh = priv; |
713 | struct em28xx *dev = fh->dev; | |
714 | int width = f->fmt.pix.width; | |
715 | int height = f->fmt.pix.height; | |
716 | unsigned int maxw = norm_maxw(dev); | |
717 | unsigned int maxh = norm_maxh(dev); | |
718 | unsigned int hscale, vscale; | |
719 | ||
720 | /* width must even because of the YUYV format | |
721 | height must be even because of interlacing */ | |
722 | height &= 0xfffe; | |
723 | width &= 0xfffe; | |
a6c2ba28 | 724 | |
195a4ef6 MCC |
725 | if (height < 32) |
726 | height = 32; | |
727 | if (height > maxh) | |
728 | height = maxh; | |
729 | if (width < 48) | |
730 | width = 48; | |
731 | if (width > maxw) | |
732 | width = maxw; | |
a6c2ba28 | 733 | |
195a4ef6 | 734 | mutex_lock(&dev->lock); |
a225452e | 735 | |
195a4ef6 MCC |
736 | if (dev->is_em2800) { |
737 | /* the em2800 can only scale down to 50% */ | |
738 | if (height % (maxh / 2)) | |
739 | height = maxh; | |
740 | if (width % (maxw / 2)) | |
741 | width = maxw; | |
742 | /* according to empiatech support */ | |
743 | /* the MaxPacketSize is to small to support */ | |
744 | /* framesizes larger than 640x480 @ 30 fps */ | |
745 | /* or 640x576 @ 25 fps. As this would cut */ | |
746 | /* of a part of the image we prefer */ | |
747 | /* 360x576 or 360x480 for now */ | |
748 | if (width == maxw && height == maxh) | |
749 | width /= 2; | |
750 | } | |
a225452e | 751 | |
195a4ef6 | 752 | get_scale(dev, width, height, &hscale, &vscale); |
a6c2ba28 | 753 | |
195a4ef6 MCC |
754 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); |
755 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); | |
a6c2ba28 | 756 | |
195a4ef6 MCC |
757 | f->fmt.pix.width = width; |
758 | f->fmt.pix.height = height; | |
759 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; | |
760 | f->fmt.pix.bytesperline = width * 2; | |
761 | f->fmt.pix.sizeimage = width * 2 * height; | |
762 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
763 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
a6c2ba28 | 764 | |
3593cab5 | 765 | mutex_unlock(&dev->lock); |
a6c2ba28 | 766 | return 0; |
767 | } | |
768 | ||
195a4ef6 MCC |
769 | static int vidioc_s_fmt_cap(struct file *file, void *priv, |
770 | struct v4l2_format *f) | |
a6c2ba28 | 771 | { |
195a4ef6 MCC |
772 | struct em28xx_fh *fh = priv; |
773 | struct em28xx *dev = fh->dev; | |
ad0ebb96 | 774 | int rc; |
a6c2ba28 | 775 | |
195a4ef6 MCC |
776 | rc = check_dev(dev); |
777 | if (rc < 0) | |
778 | return rc; | |
a225452e | 779 | |
195a4ef6 | 780 | vidioc_try_fmt_cap(file, priv, f); |
a225452e | 781 | |
5a80415b SS |
782 | mutex_lock(&dev->lock); |
783 | ||
05612975 BP |
784 | if (videobuf_queue_is_busy(&fh->vb_vidq)) { |
785 | em28xx_errdev("%s queue busy\n", __func__); | |
786 | rc = -EBUSY; | |
787 | goto out; | |
788 | } | |
789 | ||
0ea13e6e AT |
790 | if (dev->stream_on && !fh->stream_on) { |
791 | em28xx_errdev("%s device in use by another fh\n", __func__); | |
792 | rc = -EBUSY; | |
793 | goto out; | |
794 | } | |
795 | ||
195a4ef6 MCC |
796 | /* set new image size */ |
797 | dev->width = f->fmt.pix.width; | |
798 | dev->height = f->fmt.pix.height; | |
195a4ef6 | 799 | get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); |
a6c2ba28 | 800 | |
195a4ef6 | 801 | em28xx_set_alternate(dev); |
195a4ef6 | 802 | em28xx_resolution_set(dev); |
195a4ef6 | 803 | |
05612975 BP |
804 | rc = 0; |
805 | ||
806 | out: | |
195a4ef6 | 807 | mutex_unlock(&dev->lock); |
05612975 | 808 | return rc; |
195a4ef6 MCC |
809 | } |
810 | ||
6ea54d93 | 811 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm) |
195a4ef6 MCC |
812 | { |
813 | struct em28xx_fh *fh = priv; | |
814 | struct em28xx *dev = fh->dev; | |
815 | struct v4l2_format f; | |
195a4ef6 MCC |
816 | int rc; |
817 | ||
818 | rc = check_dev(dev); | |
819 | if (rc < 0) | |
820 | return rc; | |
821 | ||
195a4ef6 | 822 | mutex_lock(&dev->lock); |
7d497f8a | 823 | dev->norm = *norm; |
195a4ef6 | 824 | mutex_unlock(&dev->lock); |
a6c2ba28 | 825 | |
195a4ef6 MCC |
826 | /* Adjusts width/height, if needed */ |
827 | f.fmt.pix.width = dev->width; | |
828 | f.fmt.pix.height = dev->height; | |
829 | vidioc_try_fmt_cap(file, priv, &f); | |
a6c2ba28 | 830 | |
195a4ef6 | 831 | mutex_lock(&dev->lock); |
a6c2ba28 | 832 | |
195a4ef6 MCC |
833 | /* set new image size */ |
834 | dev->width = f.fmt.pix.width; | |
835 | dev->height = f.fmt.pix.height; | |
195a4ef6 | 836 | get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); |
a6c2ba28 | 837 | |
195a4ef6 | 838 | em28xx_resolution_set(dev); |
7d497f8a | 839 | em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm); |
a6c2ba28 | 840 | |
195a4ef6 MCC |
841 | mutex_unlock(&dev->lock); |
842 | return 0; | |
843 | } | |
9e31ced8 | 844 | |
195a4ef6 MCC |
845 | static const char *iname[] = { |
846 | [EM28XX_VMUX_COMPOSITE1] = "Composite1", | |
847 | [EM28XX_VMUX_COMPOSITE2] = "Composite2", | |
848 | [EM28XX_VMUX_COMPOSITE3] = "Composite3", | |
849 | [EM28XX_VMUX_COMPOSITE4] = "Composite4", | |
850 | [EM28XX_VMUX_SVIDEO] = "S-Video", | |
851 | [EM28XX_VMUX_TELEVISION] = "Television", | |
852 | [EM28XX_VMUX_CABLE] = "Cable TV", | |
853 | [EM28XX_VMUX_DVB] = "DVB", | |
854 | [EM28XX_VMUX_DEBUG] = "for debug only", | |
855 | }; | |
9e31ced8 | 856 | |
195a4ef6 MCC |
857 | static int vidioc_enum_input(struct file *file, void *priv, |
858 | struct v4l2_input *i) | |
859 | { | |
860 | struct em28xx_fh *fh = priv; | |
861 | struct em28xx *dev = fh->dev; | |
862 | unsigned int n; | |
9e31ced8 | 863 | |
195a4ef6 MCC |
864 | n = i->index; |
865 | if (n >= MAX_EM28XX_INPUT) | |
866 | return -EINVAL; | |
867 | if (0 == INPUT(n)->type) | |
868 | return -EINVAL; | |
9e31ced8 | 869 | |
195a4ef6 MCC |
870 | i->index = n; |
871 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
a6c2ba28 | 872 | |
195a4ef6 | 873 | strcpy(i->name, iname[INPUT(n)->type]); |
a6c2ba28 | 874 | |
195a4ef6 MCC |
875 | if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) || |
876 | (EM28XX_VMUX_CABLE == INPUT(n)->type)) | |
877 | i->type = V4L2_INPUT_TYPE_TUNER; | |
878 | ||
7d497f8a | 879 | i->std = dev->vdev->tvnorms; |
195a4ef6 MCC |
880 | |
881 | return 0; | |
a6c2ba28 | 882 | } |
883 | ||
195a4ef6 | 884 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
a6c2ba28 | 885 | { |
195a4ef6 MCC |
886 | struct em28xx_fh *fh = priv; |
887 | struct em28xx *dev = fh->dev; | |
a6c2ba28 | 888 | |
195a4ef6 MCC |
889 | *i = dev->ctl_input; |
890 | ||
891 | return 0; | |
892 | } | |
893 | ||
894 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) | |
895 | { | |
896 | struct em28xx_fh *fh = priv; | |
897 | struct em28xx *dev = fh->dev; | |
898 | int rc; | |
899 | ||
900 | rc = check_dev(dev); | |
901 | if (rc < 0) | |
902 | return rc; | |
903 | ||
904 | if (i >= MAX_EM28XX_INPUT) | |
905 | return -EINVAL; | |
906 | if (0 == INPUT(i)->type) | |
907 | return -EINVAL; | |
a225452e | 908 | |
5a80415b | 909 | mutex_lock(&dev->lock); |
a6c2ba28 | 910 | |
195a4ef6 MCC |
911 | video_mux(dev, i); |
912 | ||
913 | mutex_unlock(&dev->lock); | |
914 | return 0; | |
915 | } | |
916 | ||
917 | static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) | |
918 | { | |
919 | struct em28xx_fh *fh = priv; | |
920 | struct em28xx *dev = fh->dev; | |
921 | unsigned int index = a->index; | |
922 | ||
923 | if (a->index > 1) | |
924 | return -EINVAL; | |
925 | ||
926 | index = dev->ctl_ainput; | |
927 | ||
6ea54d93 | 928 | if (index == 0) |
195a4ef6 | 929 | strcpy(a->name, "Television"); |
6ea54d93 | 930 | else |
195a4ef6 | 931 | strcpy(a->name, "Line In"); |
6ea54d93 | 932 | |
195a4ef6 MCC |
933 | a->capability = V4L2_AUDCAP_STEREO; |
934 | a->index = index; | |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
939 | static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a) | |
940 | { | |
941 | struct em28xx_fh *fh = priv; | |
942 | struct em28xx *dev = fh->dev; | |
943 | ||
944 | if (a->index != dev->ctl_ainput) | |
945 | return -EINVAL; | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | static int vidioc_queryctrl(struct file *file, void *priv, | |
951 | struct v4l2_queryctrl *qc) | |
952 | { | |
953 | struct em28xx_fh *fh = priv; | |
954 | struct em28xx *dev = fh->dev; | |
955 | int id = qc->id; | |
956 | int i; | |
957 | int rc; | |
958 | ||
959 | rc = check_dev(dev); | |
960 | if (rc < 0) | |
961 | return rc; | |
962 | ||
963 | memset(qc, 0, sizeof(*qc)); | |
964 | ||
965 | qc->id = id; | |
966 | ||
967 | if (!dev->has_msp34xx) { | |
968 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { | |
969 | if (qc->id && qc->id == em28xx_qctrl[i].id) { | |
970 | memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc)); | |
971 | return 0; | |
a6c2ba28 | 972 | } |
973 | } | |
195a4ef6 MCC |
974 | } |
975 | mutex_lock(&dev->lock); | |
976 | em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc); | |
977 | mutex_unlock(&dev->lock); | |
a6c2ba28 | 978 | |
195a4ef6 MCC |
979 | if (qc->type) |
980 | return 0; | |
981 | else | |
982 | return -EINVAL; | |
983 | } | |
a6c2ba28 | 984 | |
195a4ef6 MCC |
985 | static int vidioc_g_ctrl(struct file *file, void *priv, |
986 | struct v4l2_control *ctrl) | |
987 | { | |
988 | struct em28xx_fh *fh = priv; | |
989 | struct em28xx *dev = fh->dev; | |
990 | int rc; | |
a6c2ba28 | 991 | |
195a4ef6 MCC |
992 | rc = check_dev(dev); |
993 | if (rc < 0) | |
994 | return rc; | |
995 | mutex_lock(&dev->lock); | |
a6c2ba28 | 996 | |
195a4ef6 MCC |
997 | if (!dev->has_msp34xx) |
998 | rc = em28xx_get_ctrl(dev, ctrl); | |
999 | else | |
1000 | rc = -EINVAL; | |
1001 | ||
1002 | if (rc == -EINVAL) { | |
1003 | em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl); | |
1004 | rc = 0; | |
1005 | } | |
1006 | ||
1007 | mutex_unlock(&dev->lock); | |
1008 | return rc; | |
1009 | } | |
1010 | ||
1011 | static int vidioc_s_ctrl(struct file *file, void *priv, | |
1012 | struct v4l2_control *ctrl) | |
1013 | { | |
1014 | struct em28xx_fh *fh = priv; | |
1015 | struct em28xx *dev = fh->dev; | |
1016 | u8 i; | |
1017 | int rc; | |
1018 | ||
1019 | rc = check_dev(dev); | |
1020 | if (rc < 0) | |
1021 | return rc; | |
1022 | ||
1023 | mutex_lock(&dev->lock); | |
1024 | ||
1025 | if (dev->has_msp34xx) | |
1026 | em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl); | |
1027 | else { | |
1028 | rc = 1; | |
1029 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { | |
1030 | if (ctrl->id == em28xx_qctrl[i].id) { | |
1031 | if (ctrl->value < em28xx_qctrl[i].minimum || | |
1032 | ctrl->value > em28xx_qctrl[i].maximum) { | |
1033 | rc = -ERANGE; | |
1034 | break; | |
1035 | } | |
1036 | ||
1037 | rc = em28xx_set_ctrl(dev, ctrl); | |
1038 | break; | |
1039 | } | |
a6c2ba28 | 1040 | } |
1041 | } | |
1042 | ||
195a4ef6 MCC |
1043 | /* Control not found - try to send it to the attached devices */ |
1044 | if (rc == 1) { | |
1045 | em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl); | |
1046 | rc = 0; | |
1047 | } | |
1048 | ||
5a80415b | 1049 | mutex_unlock(&dev->lock); |
195a4ef6 | 1050 | return rc; |
a6c2ba28 | 1051 | } |
1052 | ||
195a4ef6 MCC |
1053 | static int vidioc_g_tuner(struct file *file, void *priv, |
1054 | struct v4l2_tuner *t) | |
a6c2ba28 | 1055 | { |
195a4ef6 MCC |
1056 | struct em28xx_fh *fh = priv; |
1057 | struct em28xx *dev = fh->dev; | |
1058 | int rc; | |
1059 | ||
1060 | rc = check_dev(dev); | |
1061 | if (rc < 0) | |
1062 | return rc; | |
1063 | ||
1064 | if (0 != t->index) | |
1065 | return -EINVAL; | |
1066 | ||
1067 | strcpy(t->name, "Tuner"); | |
1068 | ||
1069 | mutex_lock(&dev->lock); | |
1070 | ||
1071 | em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t); | |
1072 | ||
1073 | mutex_unlock(&dev->lock); | |
1074 | return 0; | |
a6c2ba28 | 1075 | } |
1076 | ||
195a4ef6 MCC |
1077 | static int vidioc_s_tuner(struct file *file, void *priv, |
1078 | struct v4l2_tuner *t) | |
a6c2ba28 | 1079 | { |
195a4ef6 MCC |
1080 | struct em28xx_fh *fh = priv; |
1081 | struct em28xx *dev = fh->dev; | |
1082 | int rc; | |
63337dd3 | 1083 | |
195a4ef6 MCC |
1084 | rc = check_dev(dev); |
1085 | if (rc < 0) | |
1086 | return rc; | |
1087 | ||
1088 | if (0 != t->index) | |
1089 | return -EINVAL; | |
1090 | ||
1091 | mutex_lock(&dev->lock); | |
1092 | ||
1093 | em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t); | |
1094 | ||
1095 | mutex_unlock(&dev->lock); | |
1096 | return 0; | |
a6c2ba28 | 1097 | } |
1098 | ||
195a4ef6 MCC |
1099 | static int vidioc_g_frequency(struct file *file, void *priv, |
1100 | struct v4l2_frequency *f) | |
1101 | { | |
1102 | struct em28xx_fh *fh = priv; | |
1103 | struct em28xx *dev = fh->dev; | |
a6c2ba28 | 1104 | |
0be43754 | 1105 | f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; |
195a4ef6 MCC |
1106 | f->frequency = dev->ctl_freq; |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
1111 | static int vidioc_s_frequency(struct file *file, void *priv, | |
1112 | struct v4l2_frequency *f) | |
a6c2ba28 | 1113 | { |
195a4ef6 MCC |
1114 | struct em28xx_fh *fh = priv; |
1115 | struct em28xx *dev = fh->dev; | |
1116 | int rc; | |
9c75541f | 1117 | |
195a4ef6 MCC |
1118 | rc = check_dev(dev); |
1119 | if (rc < 0) | |
1120 | return rc; | |
1121 | ||
1122 | if (0 != f->tuner) | |
1123 | return -EINVAL; | |
1124 | ||
0be43754 MCC |
1125 | if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) |
1126 | return -EINVAL; | |
1127 | if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) | |
195a4ef6 | 1128 | return -EINVAL; |
a225452e MCC |
1129 | |
1130 | mutex_lock(&dev->lock); | |
a3a048ce | 1131 | |
195a4ef6 MCC |
1132 | dev->ctl_freq = f->frequency; |
1133 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f); | |
a6c2ba28 | 1134 | |
195a4ef6 MCC |
1135 | mutex_unlock(&dev->lock); |
1136 | return 0; | |
1137 | } | |
a6c2ba28 | 1138 | |
1e7ad56f MCC |
1139 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1140 | static int em28xx_reg_len(int reg) | |
1141 | { | |
1142 | switch (reg) { | |
41facaa4 MCC |
1143 | case EM28XX_R40_AC97LSB: |
1144 | case EM28XX_R30_HSCALELOW: | |
1145 | case EM28XX_R32_VSCALELOW: | |
1e7ad56f MCC |
1146 | return 2; |
1147 | default: | |
1148 | return 1; | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | static int vidioc_g_register(struct file *file, void *priv, | |
1153 | struct v4l2_register *reg) | |
1154 | { | |
1155 | struct em28xx_fh *fh = priv; | |
1156 | struct em28xx *dev = fh->dev; | |
1157 | int ret; | |
1158 | ||
1159 | if (!v4l2_chip_match_host(reg->match_type, reg->match_chip)) | |
1160 | return -EINVAL; | |
1161 | ||
1162 | if (em28xx_reg_len(reg->reg) == 1) { | |
1163 | ret = em28xx_read_reg(dev, reg->reg); | |
1164 | if (ret < 0) | |
1165 | return ret; | |
1166 | ||
1167 | reg->val = ret; | |
1168 | } else { | |
a954b668 | 1169 | __le64 val = 0; |
1e7ad56f MCC |
1170 | ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS, |
1171 | reg->reg, (char *)&val, 2); | |
1172 | if (ret < 0) | |
1173 | return ret; | |
1174 | ||
a954b668 | 1175 | reg->val = le64_to_cpu(val); |
1e7ad56f MCC |
1176 | } |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | static int vidioc_s_register(struct file *file, void *priv, | |
1182 | struct v4l2_register *reg) | |
1183 | { | |
1184 | struct em28xx_fh *fh = priv; | |
1185 | struct em28xx *dev = fh->dev; | |
a954b668 | 1186 | __le64 buf; |
1e7ad56f | 1187 | |
a954b668 | 1188 | buf = cpu_to_le64(reg->val); |
1e7ad56f MCC |
1189 | |
1190 | return em28xx_write_regs(dev, reg->reg, (char *)&buf, | |
1191 | em28xx_reg_len(reg->reg)); | |
1192 | } | |
1193 | #endif | |
1194 | ||
1195 | ||
195a4ef6 MCC |
1196 | static int vidioc_cropcap(struct file *file, void *priv, |
1197 | struct v4l2_cropcap *cc) | |
1198 | { | |
1199 | struct em28xx_fh *fh = priv; | |
1200 | struct em28xx *dev = fh->dev; | |
1201 | ||
1202 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
a6c2ba28 | 1203 | return -EINVAL; |
a6c2ba28 | 1204 | |
195a4ef6 MCC |
1205 | cc->bounds.left = 0; |
1206 | cc->bounds.top = 0; | |
1207 | cc->bounds.width = dev->width; | |
1208 | cc->bounds.height = dev->height; | |
1209 | cc->defrect = cc->bounds; | |
1210 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ | |
1211 | cc->pixelaspect.denominator = 59; | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | static int vidioc_streamon(struct file *file, void *priv, | |
1217 | enum v4l2_buf_type type) | |
1218 | { | |
1219 | struct em28xx_fh *fh = priv; | |
1220 | struct em28xx *dev = fh->dev; | |
1221 | int rc; | |
1222 | ||
1223 | rc = check_dev(dev); | |
1224 | if (rc < 0) | |
1225 | return rc; | |
1226 | ||
195a4ef6 | 1227 | |
ad0ebb96 | 1228 | if (unlikely(res_get(fh) < 0)) |
195a4ef6 | 1229 | return -EBUSY; |
a6c2ba28 | 1230 | |
ad0ebb96 | 1231 | return (videobuf_streamon(&fh->vb_vidq)); |
195a4ef6 MCC |
1232 | } |
1233 | ||
1234 | static int vidioc_streamoff(struct file *file, void *priv, | |
1235 | enum v4l2_buf_type type) | |
1236 | { | |
1237 | struct em28xx_fh *fh = priv; | |
1238 | struct em28xx *dev = fh->dev; | |
1239 | int rc; | |
1240 | ||
1241 | rc = check_dev(dev); | |
1242 | if (rc < 0) | |
1243 | return rc; | |
1244 | ||
ad0ebb96 MCC |
1245 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1246 | return -EINVAL; | |
1247 | if (type != fh->type) | |
195a4ef6 MCC |
1248 | return -EINVAL; |
1249 | ||
ad0ebb96 MCC |
1250 | videobuf_streamoff(&fh->vb_vidq); |
1251 | res_free(fh); | |
a6c2ba28 | 1252 | |
a6c2ba28 | 1253 | return 0; |
1254 | } | |
1255 | ||
195a4ef6 MCC |
1256 | static int vidioc_querycap(struct file *file, void *priv, |
1257 | struct v4l2_capability *cap) | |
a6c2ba28 | 1258 | { |
195a4ef6 MCC |
1259 | struct em28xx_fh *fh = priv; |
1260 | struct em28xx *dev = fh->dev; | |
1261 | ||
1262 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); | |
1263 | strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); | |
1264 | strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info)); | |
1265 | ||
1266 | cap->version = EM28XX_VERSION_CODE; | |
1267 | ||
1268 | cap->capabilities = | |
1269 | V4L2_CAP_SLICED_VBI_CAPTURE | | |
1270 | V4L2_CAP_VIDEO_CAPTURE | | |
1271 | V4L2_CAP_AUDIO | | |
1272 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
1273 | ||
ed086314 | 1274 | if (dev->tuner_type != TUNER_ABSENT) |
195a4ef6 MCC |
1275 | cap->capabilities |= V4L2_CAP_TUNER; |
1276 | ||
1277 | return 0; | |
c0477ad9 MCC |
1278 | } |
1279 | ||
195a4ef6 MCC |
1280 | static int vidioc_enum_fmt_cap(struct file *file, void *priv, |
1281 | struct v4l2_fmtdesc *fmtd) | |
a6c2ba28 | 1282 | { |
195a4ef6 | 1283 | if (fmtd->index != 0) |
c0477ad9 | 1284 | return -EINVAL; |
195a4ef6 MCC |
1285 | |
1286 | fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1287 | strcpy(fmtd->description, "Packed YUY2"); | |
1288 | fmtd->pixelformat = V4L2_PIX_FMT_YUYV; | |
1289 | memset(fmtd->reserved, 0, sizeof(fmtd->reserved)); | |
1290 | ||
1291 | return 0; | |
c0477ad9 MCC |
1292 | } |
1293 | ||
195a4ef6 MCC |
1294 | /* Sliced VBI ioctls */ |
1295 | static int vidioc_g_fmt_vbi_capture(struct file *file, void *priv, | |
1296 | struct v4l2_format *f) | |
a6c2ba28 | 1297 | { |
195a4ef6 MCC |
1298 | struct em28xx_fh *fh = priv; |
1299 | struct em28xx *dev = fh->dev; | |
1300 | int rc; | |
a6c2ba28 | 1301 | |
195a4ef6 MCC |
1302 | rc = check_dev(dev); |
1303 | if (rc < 0) | |
1304 | return rc; | |
a6c2ba28 | 1305 | |
195a4ef6 MCC |
1306 | mutex_lock(&dev->lock); |
1307 | ||
1308 | f->fmt.sliced.service_set = 0; | |
1309 | ||
1310 | em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f); | |
1311 | ||
1312 | if (f->fmt.sliced.service_set == 0) | |
1313 | rc = -EINVAL; | |
1314 | ||
1315 | mutex_unlock(&dev->lock); | |
1316 | return rc; | |
1317 | } | |
1318 | ||
1319 | static int vidioc_try_set_vbi_capture(struct file *file, void *priv, | |
1320 | struct v4l2_format *f) | |
1321 | { | |
1322 | struct em28xx_fh *fh = priv; | |
1323 | struct em28xx *dev = fh->dev; | |
1324 | int rc; | |
1325 | ||
1326 | rc = check_dev(dev); | |
1327 | if (rc < 0) | |
1328 | return rc; | |
1329 | ||
1330 | mutex_lock(&dev->lock); | |
1331 | em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f); | |
1332 | mutex_unlock(&dev->lock); | |
1333 | ||
1334 | if (f->fmt.sliced.service_set == 0) | |
1335 | return -EINVAL; | |
a6c2ba28 | 1336 | |
1337 | return 0; | |
1338 | } | |
1339 | ||
195a4ef6 MCC |
1340 | |
1341 | static int vidioc_reqbufs(struct file *file, void *priv, | |
1342 | struct v4l2_requestbuffers *rb) | |
a6c2ba28 | 1343 | { |
195a4ef6 MCC |
1344 | struct em28xx_fh *fh = priv; |
1345 | struct em28xx *dev = fh->dev; | |
195a4ef6 | 1346 | int rc; |
a6c2ba28 | 1347 | |
195a4ef6 MCC |
1348 | rc = check_dev(dev); |
1349 | if (rc < 0) | |
1350 | return rc; | |
a6c2ba28 | 1351 | |
ad0ebb96 | 1352 | return (videobuf_reqbufs(&fh->vb_vidq, rb)); |
a6c2ba28 | 1353 | } |
1354 | ||
195a4ef6 MCC |
1355 | static int vidioc_querybuf(struct file *file, void *priv, |
1356 | struct v4l2_buffer *b) | |
e5589bef | 1357 | { |
195a4ef6 MCC |
1358 | struct em28xx_fh *fh = priv; |
1359 | struct em28xx *dev = fh->dev; | |
1360 | int rc; | |
e5589bef | 1361 | |
195a4ef6 MCC |
1362 | rc = check_dev(dev); |
1363 | if (rc < 0) | |
1364 | return rc; | |
2d50f847 | 1365 | |
ad0ebb96 | 1366 | return (videobuf_querybuf(&fh->vb_vidq, b)); |
195a4ef6 MCC |
1367 | } |
1368 | ||
1369 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) | |
1370 | { | |
1371 | struct em28xx_fh *fh = priv; | |
1372 | struct em28xx *dev = fh->dev; | |
195a4ef6 MCC |
1373 | int rc; |
1374 | ||
1375 | rc = check_dev(dev); | |
1376 | if (rc < 0) | |
1377 | return rc; | |
1378 | ||
ad0ebb96 | 1379 | return (videobuf_qbuf(&fh->vb_vidq, b)); |
e5589bef MCC |
1380 | } |
1381 | ||
195a4ef6 | 1382 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) |
2d50f847 | 1383 | { |
195a4ef6 MCC |
1384 | struct em28xx_fh *fh = priv; |
1385 | struct em28xx *dev = fh->dev; | |
1386 | int rc; | |
195a4ef6 MCC |
1387 | |
1388 | rc = check_dev(dev); | |
1389 | if (rc < 0) | |
1390 | return rc; | |
2d50f847 | 1391 | |
ad0ebb96 MCC |
1392 | return (videobuf_dqbuf(&fh->vb_vidq, b, |
1393 | file->f_flags & O_NONBLOCK)); | |
1394 | } | |
2d50f847 | 1395 | |
ad0ebb96 | 1396 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
f245e549 | 1397 | static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) |
ad0ebb96 | 1398 | { |
f245e549 | 1399 | struct em28xx_fh *fh = priv; |
2d50f847 | 1400 | |
f245e549 | 1401 | return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8); |
195a4ef6 | 1402 | } |
ad0ebb96 MCC |
1403 | #endif |
1404 | ||
2d50f847 | 1405 | |
0be43754 MCC |
1406 | /* ----------------------------------------------------------- */ |
1407 | /* RADIO ESPECIFIC IOCTLS */ | |
1408 | /* ----------------------------------------------------------- */ | |
1409 | ||
1410 | static int radio_querycap(struct file *file, void *priv, | |
1411 | struct v4l2_capability *cap) | |
1412 | { | |
1413 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; | |
1414 | ||
1415 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); | |
1416 | strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); | |
1417 | strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info)); | |
1418 | ||
1419 | cap->version = EM28XX_VERSION_CODE; | |
1420 | cap->capabilities = V4L2_CAP_TUNER; | |
1421 | return 0; | |
1422 | } | |
1423 | ||
1424 | static int radio_g_tuner(struct file *file, void *priv, | |
1425 | struct v4l2_tuner *t) | |
1426 | { | |
1427 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; | |
1428 | ||
1429 | if (unlikely(t->index > 0)) | |
1430 | return -EINVAL; | |
1431 | ||
1432 | strcpy(t->name, "Radio"); | |
1433 | t->type = V4L2_TUNER_RADIO; | |
1434 | ||
1435 | em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t); | |
1436 | return 0; | |
1437 | } | |
1438 | ||
1439 | static int radio_enum_input(struct file *file, void *priv, | |
1440 | struct v4l2_input *i) | |
1441 | { | |
1442 | if (i->index != 0) | |
1443 | return -EINVAL; | |
1444 | strcpy(i->name, "Radio"); | |
1445 | i->type = V4L2_INPUT_TYPE_TUNER; | |
1446 | ||
1447 | return 0; | |
1448 | } | |
1449 | ||
1450 | static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a) | |
1451 | { | |
1452 | if (unlikely(a->index)) | |
1453 | return -EINVAL; | |
1454 | ||
1455 | strcpy(a->name, "Radio"); | |
1456 | return 0; | |
1457 | } | |
1458 | ||
1459 | static int radio_s_tuner(struct file *file, void *priv, | |
1460 | struct v4l2_tuner *t) | |
1461 | { | |
1462 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; | |
1463 | ||
1464 | if (0 != t->index) | |
1465 | return -EINVAL; | |
1466 | ||
1467 | em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t); | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | static int radio_s_audio(struct file *file, void *fh, | |
1473 | struct v4l2_audio *a) | |
1474 | { | |
1475 | return 0; | |
1476 | } | |
1477 | ||
1478 | static int radio_s_input(struct file *file, void *fh, unsigned int i) | |
1479 | { | |
1480 | return 0; | |
1481 | } | |
1482 | ||
1483 | static int radio_queryctrl(struct file *file, void *priv, | |
1484 | struct v4l2_queryctrl *qc) | |
1485 | { | |
1486 | int i; | |
1487 | ||
1488 | if (qc->id < V4L2_CID_BASE || | |
1489 | qc->id >= V4L2_CID_LASTP1) | |
1490 | return -EINVAL; | |
1491 | ||
1492 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { | |
1493 | if (qc->id && qc->id == em28xx_qctrl[i].id) { | |
1494 | memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc)); | |
1495 | return 0; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | return -EINVAL; | |
1500 | } | |
1501 | ||
195a4ef6 MCC |
1502 | /* |
1503 | * em28xx_v4l2_open() | |
1504 | * inits the device and starts isoc transfer | |
1505 | */ | |
1506 | static int em28xx_v4l2_open(struct inode *inode, struct file *filp) | |
1507 | { | |
1508 | int minor = iminor(inode); | |
0be43754 | 1509 | int errCode = 0, radio = 0; |
6ea54d93 | 1510 | struct em28xx *h, *dev = NULL; |
195a4ef6 | 1511 | struct em28xx_fh *fh; |
d7aa8020 | 1512 | enum v4l2_buf_type fh_type = 0; |
2d50f847 | 1513 | |
195a4ef6 MCC |
1514 | list_for_each_entry(h, &em28xx_devlist, devlist) { |
1515 | if (h->vdev->minor == minor) { | |
1516 | dev = h; | |
d7aa8020 | 1517 | fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
195a4ef6 MCC |
1518 | } |
1519 | if (h->vbi_dev->minor == minor) { | |
1520 | dev = h; | |
d7aa8020 | 1521 | fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; |
195a4ef6 | 1522 | } |
0be43754 MCC |
1523 | if (h->radio_dev && |
1524 | h->radio_dev->minor == minor) { | |
1525 | radio = 1; | |
1526 | dev = h; | |
1527 | } | |
2d50f847 | 1528 | } |
195a4ef6 MCC |
1529 | if (NULL == dev) |
1530 | return -ENODEV; | |
2d50f847 | 1531 | |
195a4ef6 | 1532 | em28xx_videodbg("open minor=%d type=%s users=%d\n", |
d7aa8020 | 1533 | minor, v4l2_type_names[fh_type], dev->users); |
2d50f847 | 1534 | |
9aeb4b05 | 1535 | |
c67ec53f | 1536 | fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL); |
195a4ef6 MCC |
1537 | if (!fh) { |
1538 | em28xx_errdev("em28xx-video.c: Out of memory?!\n"); | |
1539 | return -ENOMEM; | |
1540 | } | |
1541 | mutex_lock(&dev->lock); | |
1542 | fh->dev = dev; | |
0be43754 | 1543 | fh->radio = radio; |
d7aa8020 | 1544 | fh->type = fh_type; |
195a4ef6 | 1545 | filp->private_data = fh; |
9aeb4b05 | 1546 | |
d7aa8020 | 1547 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { |
195a4ef6 MCC |
1548 | dev->width = norm_maxw(dev); |
1549 | dev->height = norm_maxh(dev); | |
195a4ef6 MCC |
1550 | dev->hscale = 0; |
1551 | dev->vscale = 0; | |
2d50f847 | 1552 | |
c67ec53f | 1553 | em28xx_set_mode(dev, EM28XX_ANALOG_MODE); |
3687e1e6 | 1554 | em28xx_set_alternate(dev); |
195a4ef6 | 1555 | em28xx_resolution_set(dev); |
2d50f847 | 1556 | |
c67ec53f MCC |
1557 | /* Needed, since GPIO might have disabled power of |
1558 | some i2c device | |
1559 | */ | |
1560 | em28xx_config_i2c(dev); | |
1561 | ||
2d50f847 | 1562 | } |
0be43754 MCC |
1563 | if (fh->radio) { |
1564 | em28xx_videodbg("video_open: setting radio device\n"); | |
1565 | em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL); | |
1566 | } | |
2d50f847 | 1567 | |
195a4ef6 | 1568 | dev->users++; |
2d50f847 | 1569 | |
ad0ebb96 MCC |
1570 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops, |
1571 | NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED, | |
1572 | sizeof(struct em28xx_buffer), fh); | |
1573 | ||
195a4ef6 | 1574 | mutex_unlock(&dev->lock); |
c67ec53f | 1575 | |
195a4ef6 | 1576 | return errCode; |
2d50f847 | 1577 | } |
e5589bef | 1578 | |
a6c2ba28 | 1579 | /* |
195a4ef6 MCC |
1580 | * em28xx_realease_resources() |
1581 | * unregisters the v4l2,i2c and usb devices | |
1582 | * called when the device gets disconected or at module unload | |
1583 | */ | |
1584 | static void em28xx_release_resources(struct em28xx *dev) | |
a6c2ba28 | 1585 | { |
a6c2ba28 | 1586 | |
195a4ef6 | 1587 | /*FIXME: I2C IR should be disconnected */ |
a6c2ba28 | 1588 | |
195a4ef6 MCC |
1589 | em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n", |
1590 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, | |
1591 | dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); | |
1592 | list_del(&dev->devlist); | |
0be43754 MCC |
1593 | if (dev->radio_dev) { |
1594 | if (-1 != dev->radio_dev->minor) | |
1595 | video_unregister_device(dev->radio_dev); | |
1596 | else | |
1597 | video_device_release(dev->radio_dev); | |
1598 | dev->radio_dev = NULL; | |
1599 | } | |
1600 | if (dev->vbi_dev) { | |
1601 | if (-1 != dev->vbi_dev->minor) | |
1602 | video_unregister_device(dev->vbi_dev); | |
1603 | else | |
1604 | video_device_release(dev->vbi_dev); | |
1605 | dev->vbi_dev = NULL; | |
1606 | } | |
1607 | if (dev->vdev) { | |
1608 | if (-1 != dev->vdev->minor) | |
1609 | video_unregister_device(dev->vdev); | |
1610 | else | |
1611 | video_device_release(dev->vdev); | |
1612 | dev->vdev = NULL; | |
1613 | } | |
195a4ef6 MCC |
1614 | em28xx_i2c_unregister(dev); |
1615 | usb_put_dev(dev->udev); | |
a6c2ba28 | 1616 | |
195a4ef6 | 1617 | /* Mark device as unused */ |
6ea54d93 | 1618 | em28xx_devused &= ~(1<<dev->devno); |
195a4ef6 | 1619 | } |
a6c2ba28 | 1620 | |
195a4ef6 MCC |
1621 | /* |
1622 | * em28xx_v4l2_close() | |
6ea54d93 DSL |
1623 | * stops streaming and deallocates all resources allocated by the v4l2 |
1624 | * calls and ioctls | |
195a4ef6 MCC |
1625 | */ |
1626 | static int em28xx_v4l2_close(struct inode *inode, struct file *filp) | |
1627 | { | |
1628 | struct em28xx_fh *fh = filp->private_data; | |
1629 | struct em28xx *dev = fh->dev; | |
1630 | int errCode; | |
a6c2ba28 | 1631 | |
195a4ef6 | 1632 | em28xx_videodbg("users=%d\n", dev->users); |
a6c2ba28 | 1633 | |
a6c2ba28 | 1634 | |
195a4ef6 MCC |
1635 | if (res_check(fh)) |
1636 | res_free(fh); | |
a6c2ba28 | 1637 | |
195a4ef6 | 1638 | mutex_lock(&dev->lock); |
a6c2ba28 | 1639 | |
195a4ef6 | 1640 | if (dev->users == 1) { |
ad0ebb96 MCC |
1641 | videobuf_stop(&fh->vb_vidq); |
1642 | videobuf_mmap_free(&fh->vb_vidq); | |
a6c2ba28 | 1643 | |
195a4ef6 MCC |
1644 | /* the device is already disconnect, |
1645 | free the remaining resources */ | |
1646 | if (dev->state & DEV_DISCONNECTED) { | |
1647 | em28xx_release_resources(dev); | |
1648 | mutex_unlock(&dev->lock); | |
1649 | kfree(dev); | |
1650 | return 0; | |
1651 | } | |
a6c2ba28 | 1652 | |
d7aa8020 AT |
1653 | /* do this before setting alternate! */ |
1654 | em28xx_uninit_isoc(dev); | |
c67ec53f | 1655 | em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED); |
d7aa8020 | 1656 | |
195a4ef6 MCC |
1657 | /* set alternate 0 */ |
1658 | dev->alt = 0; | |
1659 | em28xx_videodbg("setting alternate 0\n"); | |
1660 | errCode = usb_set_interface(dev->udev, 0, 0); | |
1661 | if (errCode < 0) { | |
1662 | em28xx_errdev("cannot change alternate number to " | |
1663 | "0 (error=%i)\n", errCode); | |
1664 | } | |
9aeb4b05 | 1665 | } |
195a4ef6 MCC |
1666 | kfree(fh); |
1667 | dev->users--; | |
1668 | wake_up_interruptible_nr(&dev->open, 1); | |
1669 | mutex_unlock(&dev->lock); | |
1670 | return 0; | |
1671 | } | |
a6c2ba28 | 1672 | |
195a4ef6 MCC |
1673 | /* |
1674 | * em28xx_v4l2_read() | |
1675 | * will allocate buffers when called for the first time | |
1676 | */ | |
1677 | static ssize_t | |
6ea54d93 | 1678 | em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count, |
f245e549 | 1679 | loff_t *pos) |
195a4ef6 | 1680 | { |
195a4ef6 MCC |
1681 | struct em28xx_fh *fh = filp->private_data; |
1682 | struct em28xx *dev = fh->dev; | |
ad0ebb96 MCC |
1683 | int rc; |
1684 | ||
1685 | rc = check_dev(dev); | |
1686 | if (rc < 0) | |
1687 | return rc; | |
a6c2ba28 | 1688 | |
195a4ef6 MCC |
1689 | /* FIXME: read() is not prepared to allow changing the video |
1690 | resolution while streaming. Seems a bug at em28xx_set_fmt | |
1691 | */ | |
a6c2ba28 | 1692 | |
ad0ebb96 MCC |
1693 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
1694 | if (unlikely(res_get(fh))) | |
1695 | return -EBUSY; | |
195a4ef6 | 1696 | |
ad0ebb96 MCC |
1697 | return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, |
1698 | filp->f_flags & O_NONBLOCK); | |
195a4ef6 | 1699 | } |
ad0ebb96 | 1700 | return 0; |
a6c2ba28 | 1701 | } |
1702 | ||
1703 | /* | |
195a4ef6 MCC |
1704 | * em28xx_v4l2_poll() |
1705 | * will allocate buffers when called for the first time | |
a6c2ba28 | 1706 | */ |
195a4ef6 | 1707 | static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) |
a6c2ba28 | 1708 | { |
a3a048ce | 1709 | struct em28xx_fh *fh = filp->private_data; |
195a4ef6 | 1710 | struct em28xx *dev = fh->dev; |
ad0ebb96 MCC |
1711 | int rc; |
1712 | ||
1713 | rc = check_dev(dev); | |
1714 | if (rc < 0) | |
1715 | return rc; | |
a6c2ba28 | 1716 | |
195a4ef6 MCC |
1717 | if (unlikely(res_get(fh) < 0)) |
1718 | return POLLERR; | |
a6c2ba28 | 1719 | |
ad0ebb96 MCC |
1720 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) |
1721 | return POLLERR; | |
a6c2ba28 | 1722 | |
ad0ebb96 | 1723 | return videobuf_poll_stream(filp, &fh->vb_vidq, wait); |
195a4ef6 | 1724 | } |
a6c2ba28 | 1725 | |
195a4ef6 MCC |
1726 | /* |
1727 | * em28xx_v4l2_mmap() | |
1728 | */ | |
1729 | static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) | |
1730 | { | |
1731 | struct em28xx_fh *fh = filp->private_data; | |
1732 | struct em28xx *dev = fh->dev; | |
ad0ebb96 | 1733 | int rc; |
a6c2ba28 | 1734 | |
195a4ef6 MCC |
1735 | if (unlikely(res_get(fh) < 0)) |
1736 | return -EBUSY; | |
a6c2ba28 | 1737 | |
ad0ebb96 MCC |
1738 | rc = check_dev(dev); |
1739 | if (rc < 0) | |
1740 | return rc; | |
a6c2ba28 | 1741 | |
ad0ebb96 | 1742 | rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); |
a6c2ba28 | 1743 | |
ad0ebb96 MCC |
1744 | em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", |
1745 | (unsigned long)vma->vm_start, | |
1746 | (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, | |
1747 | rc); | |
a6c2ba28 | 1748 | |
ad0ebb96 | 1749 | return rc; |
a6c2ba28 | 1750 | } |
1751 | ||
fa027c2a | 1752 | static const struct file_operations em28xx_v4l_fops = { |
195a4ef6 MCC |
1753 | .owner = THIS_MODULE, |
1754 | .open = em28xx_v4l2_open, | |
1755 | .release = em28xx_v4l2_close, | |
1756 | .read = em28xx_v4l2_read, | |
1757 | .poll = em28xx_v4l2_poll, | |
1758 | .mmap = em28xx_v4l2_mmap, | |
1759 | .ioctl = video_ioctl2, | |
1760 | .llseek = no_llseek, | |
1761 | .compat_ioctl = v4l_compat_ioctl32, | |
1762 | }; | |
17cbe2e5 | 1763 | |
0be43754 MCC |
1764 | static const struct file_operations radio_fops = { |
1765 | .owner = THIS_MODULE, | |
1766 | .open = em28xx_v4l2_open, | |
1767 | .release = em28xx_v4l2_close, | |
1768 | .ioctl = video_ioctl2, | |
1769 | .compat_ioctl = v4l_compat_ioctl32, | |
1770 | .llseek = no_llseek, | |
1771 | }; | |
1772 | ||
195a4ef6 MCC |
1773 | static const struct video_device em28xx_video_template = { |
1774 | .fops = &em28xx_v4l_fops, | |
1775 | .release = video_device_release, | |
1776 | ||
1777 | .minor = -1, | |
1778 | .vidioc_querycap = vidioc_querycap, | |
1779 | .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, | |
1780 | .vidioc_g_fmt_cap = vidioc_g_fmt_cap, | |
1781 | .vidioc_try_fmt_cap = vidioc_try_fmt_cap, | |
1782 | .vidioc_s_fmt_cap = vidioc_s_fmt_cap, | |
1783 | .vidioc_g_audio = vidioc_g_audio, | |
1784 | .vidioc_s_audio = vidioc_s_audio, | |
1785 | .vidioc_cropcap = vidioc_cropcap, | |
1786 | ||
1787 | .vidioc_g_fmt_vbi_capture = vidioc_g_fmt_vbi_capture, | |
1788 | .vidioc_try_fmt_vbi_capture = vidioc_try_set_vbi_capture, | |
1789 | .vidioc_s_fmt_vbi_capture = vidioc_try_set_vbi_capture, | |
1790 | ||
1791 | .vidioc_reqbufs = vidioc_reqbufs, | |
1792 | .vidioc_querybuf = vidioc_querybuf, | |
1793 | .vidioc_qbuf = vidioc_qbuf, | |
1794 | .vidioc_dqbuf = vidioc_dqbuf, | |
1795 | .vidioc_s_std = vidioc_s_std, | |
1796 | .vidioc_enum_input = vidioc_enum_input, | |
1797 | .vidioc_g_input = vidioc_g_input, | |
1798 | .vidioc_s_input = vidioc_s_input, | |
1799 | .vidioc_queryctrl = vidioc_queryctrl, | |
1800 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
1801 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1802 | .vidioc_streamon = vidioc_streamon, | |
1803 | .vidioc_streamoff = vidioc_streamoff, | |
1804 | .vidioc_g_tuner = vidioc_g_tuner, | |
1805 | .vidioc_s_tuner = vidioc_s_tuner, | |
1806 | .vidioc_g_frequency = vidioc_g_frequency, | |
1807 | .vidioc_s_frequency = vidioc_s_frequency, | |
1e7ad56f MCC |
1808 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1809 | .vidioc_g_register = vidioc_g_register, | |
1810 | .vidioc_s_register = vidioc_s_register, | |
1811 | #endif | |
ad0ebb96 MCC |
1812 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
1813 | .vidiocgmbuf = vidiocgmbuf, | |
1814 | #endif | |
195a4ef6 MCC |
1815 | |
1816 | .tvnorms = V4L2_STD_ALL, | |
7d497f8a | 1817 | .current_norm = V4L2_STD_PAL, |
a6c2ba28 | 1818 | }; |
1819 | ||
0be43754 MCC |
1820 | static struct video_device em28xx_radio_template = { |
1821 | .name = "em28xx-radio", | |
1822 | .type = VID_TYPE_TUNER, | |
1823 | .fops = &radio_fops, | |
1824 | .minor = -1, | |
1825 | .vidioc_querycap = radio_querycap, | |
1826 | .vidioc_g_tuner = radio_g_tuner, | |
1827 | .vidioc_enum_input = radio_enum_input, | |
1828 | .vidioc_g_audio = radio_g_audio, | |
1829 | .vidioc_s_tuner = radio_s_tuner, | |
1830 | .vidioc_s_audio = radio_s_audio, | |
1831 | .vidioc_s_input = radio_s_input, | |
1832 | .vidioc_queryctrl = radio_queryctrl, | |
1833 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
1834 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1835 | .vidioc_g_frequency = vidioc_g_frequency, | |
1836 | .vidioc_s_frequency = vidioc_s_frequency, | |
1e7ad56f MCC |
1837 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1838 | .vidioc_g_register = vidioc_g_register, | |
1839 | .vidioc_s_register = vidioc_s_register, | |
1840 | #endif | |
0be43754 MCC |
1841 | }; |
1842 | ||
6ea54d93 | 1843 | /******************************** usb interface ******************************/ |
a6c2ba28 | 1844 | |
6d79468d MCC |
1845 | |
1846 | static LIST_HEAD(em28xx_extension_devlist); | |
1847 | static DEFINE_MUTEX(em28xx_extension_devlist_lock); | |
1848 | ||
1849 | int em28xx_register_extension(struct em28xx_ops *ops) | |
1850 | { | |
0367ca1b | 1851 | struct em28xx *dev = NULL; |
6d79468d MCC |
1852 | |
1853 | mutex_lock(&em28xx_extension_devlist_lock); | |
1854 | list_add_tail(&ops->next, &em28xx_extension_devlist); | |
0367ca1b DH |
1855 | list_for_each_entry(dev, &em28xx_devlist, devlist) { |
1856 | if (dev) | |
1857 | ops->init(dev); | |
1858 | } | |
6d79468d MCC |
1859 | printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name); |
1860 | mutex_unlock(&em28xx_extension_devlist_lock); | |
6d79468d MCC |
1861 | return 0; |
1862 | } | |
1863 | EXPORT_SYMBOL(em28xx_register_extension); | |
1864 | ||
1865 | void em28xx_unregister_extension(struct em28xx_ops *ops) | |
1866 | { | |
0367ca1b | 1867 | struct em28xx *dev = NULL; |
6d79468d | 1868 | |
0367ca1b DH |
1869 | list_for_each_entry(dev, &em28xx_devlist, devlist) { |
1870 | if (dev) | |
1871 | ops->fini(dev); | |
1872 | } | |
6d79468d MCC |
1873 | |
1874 | mutex_lock(&em28xx_extension_devlist_lock); | |
1875 | printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name); | |
1876 | list_del(&ops->next); | |
1877 | mutex_unlock(&em28xx_extension_devlist_lock); | |
1878 | } | |
1879 | EXPORT_SYMBOL(em28xx_unregister_extension); | |
1880 | ||
532fe652 AB |
1881 | static struct video_device *em28xx_vdev_init(struct em28xx *dev, |
1882 | const struct video_device *template, | |
1883 | const int type, | |
1884 | const char *type_name) | |
0be43754 MCC |
1885 | { |
1886 | struct video_device *vfd; | |
1887 | ||
1888 | vfd = video_device_alloc(); | |
1889 | if (NULL == vfd) | |
1890 | return NULL; | |
1891 | *vfd = *template; | |
1892 | vfd->minor = -1; | |
1893 | vfd->dev = &dev->udev->dev; | |
1894 | vfd->release = video_device_release; | |
1895 | vfd->type = type; | |
e9e6040d | 1896 | vfd->debug = video_debug; |
0be43754 MCC |
1897 | |
1898 | snprintf(vfd->name, sizeof(vfd->name), "%s %s", | |
1899 | dev->name, type_name); | |
1900 | ||
1901 | return vfd; | |
1902 | } | |
1903 | ||
1904 | ||
a6c2ba28 | 1905 | /* |
3acf2809 | 1906 | * em28xx_init_dev() |
a6c2ba28 | 1907 | * allocates and inits the device structs, registers i2c bus and v4l device |
1908 | */ | |
3acf2809 | 1909 | static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev, |
03910cc3 | 1910 | int minor) |
a6c2ba28 | 1911 | { |
6d79468d | 1912 | struct em28xx_ops *ops = NULL; |
3acf2809 | 1913 | struct em28xx *dev = *devhandle; |
a6c2ba28 | 1914 | int retval = -ENOMEM; |
7d497f8a | 1915 | int errCode; |
a6c2ba28 | 1916 | unsigned int maxh, maxw; |
a6c2ba28 | 1917 | |
a6c2ba28 | 1918 | dev->udev = udev; |
3593cab5 | 1919 | mutex_init(&dev->lock); |
d7aa8020 | 1920 | spin_lock_init(&dev->slock); |
a6c2ba28 | 1921 | init_waitqueue_head(&dev->open); |
a3a048ce MCC |
1922 | init_waitqueue_head(&dev->wait_frame); |
1923 | init_waitqueue_head(&dev->wait_stream); | |
a6c2ba28 | 1924 | |
3acf2809 MCC |
1925 | dev->em28xx_write_regs = em28xx_write_regs; |
1926 | dev->em28xx_read_reg = em28xx_read_reg; | |
1927 | dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len; | |
1928 | dev->em28xx_write_regs_req = em28xx_write_regs_req; | |
1929 | dev->em28xx_read_reg_req = em28xx_read_reg_req; | |
fad7b958 | 1930 | dev->is_em2800 = em28xx_boards[dev->model].is_em2800; |
a6c2ba28 | 1931 | |
a94e95b4 | 1932 | em28xx_pre_card_setup(dev); |
03910cc3 | 1933 | |
3acf2809 | 1934 | errCode = em28xx_config(dev); |
a6c2ba28 | 1935 | if (errCode) { |
3acf2809 | 1936 | em28xx_errdev("error configuring device\n"); |
03910cc3 | 1937 | em28xx_devused &= ~(1<<dev->devno); |
7c908fbb | 1938 | kfree(dev); |
a6c2ba28 | 1939 | return -ENOMEM; |
1940 | } | |
1941 | ||
a6c2ba28 | 1942 | /* register i2c bus */ |
3acf2809 | 1943 | em28xx_i2c_register(dev); |
a6c2ba28 | 1944 | |
1945 | /* Do board specific init and eeprom reading */ | |
3acf2809 | 1946 | em28xx_card_setup(dev); |
a6c2ba28 | 1947 | |
3abee53e MCC |
1948 | /* Configure audio */ |
1949 | em28xx_audio_analog_set(dev); | |
1950 | ||
a6c2ba28 | 1951 | /* configure the device */ |
3acf2809 | 1952 | em28xx_config_i2c(dev); |
a6c2ba28 | 1953 | |
7d497f8a MCC |
1954 | /* set default norm */ |
1955 | dev->norm = em28xx_video_template.current_norm; | |
03910cc3 MCC |
1956 | |
1957 | maxw = norm_maxw(dev); | |
1958 | maxh = norm_maxh(dev); | |
1959 | ||
1960 | /* set default image size */ | |
1961 | dev->width = maxw; | |
1962 | dev->height = maxh; | |
1963 | dev->interlaced = EM28XX_INTERLACED_DEFAULT; | |
03910cc3 MCC |
1964 | dev->hscale = 0; |
1965 | dev->vscale = 0; | |
1966 | dev->ctl_input = 2; | |
1967 | ||
3acf2809 | 1968 | errCode = em28xx_config(dev); |
a6c2ba28 | 1969 | |
0be43754 MCC |
1970 | list_add_tail(&dev->devlist, &em28xx_devlist); |
1971 | ||
195a4ef6 | 1972 | /* allocate and fill video video_device struct */ |
0be43754 MCC |
1973 | dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, |
1974 | VID_TYPE_CAPTURE, "video"); | |
a6c2ba28 | 1975 | if (NULL == dev->vdev) { |
3acf2809 | 1976 | em28xx_errdev("cannot allocate video_device.\n"); |
0be43754 | 1977 | goto fail_unreg; |
e5589bef | 1978 | } |
ed086314 | 1979 | if (dev->tuner_type != TUNER_ABSENT) |
195a4ef6 | 1980 | dev->vdev->type |= VID_TYPE_TUNER; |
0be43754 MCC |
1981 | |
1982 | /* register v4l2 video video_device */ | |
1983 | retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, | |
1984 | video_nr[dev->devno]); | |
1985 | if (retval) { | |
1986 | em28xx_errdev("unable to register video device (error=%i).\n", | |
1987 | retval); | |
1988 | goto fail_unreg; | |
1989 | } | |
e5589bef | 1990 | |
195a4ef6 | 1991 | /* Allocate and fill vbi video_device struct */ |
0be43754 MCC |
1992 | dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, |
1993 | VFL_TYPE_VBI, "vbi"); | |
1994 | /* register v4l2 vbi video_device */ | |
1995 | if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI, | |
1996 | vbi_nr[dev->devno]) < 0) { | |
1997 | em28xx_errdev("unable to register vbi device\n"); | |
1998 | retval = -ENODEV; | |
1999 | goto fail_unreg; | |
2000 | } | |
2001 | ||
2002 | if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) { | |
2003 | dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, | |
2004 | VFL_TYPE_RADIO, "radio"); | |
2005 | if (NULL == dev->radio_dev) { | |
2006 | em28xx_errdev("cannot allocate video_device.\n"); | |
2007 | goto fail_unreg; | |
2008 | } | |
2009 | retval = video_register_device(dev->radio_dev, VFL_TYPE_RADIO, | |
2010 | radio_nr[dev->devno]); | |
2011 | if (retval < 0) { | |
2012 | em28xx_errdev("can't register radio device\n"); | |
2013 | goto fail_unreg; | |
2014 | } | |
2015 | em28xx_info("Registered radio device as /dev/radio%d\n", | |
2016 | dev->radio_dev->minor & 0x1f); | |
a6c2ba28 | 2017 | } |
e5589bef | 2018 | |
ad0ebb96 MCC |
2019 | /* init video dma queues */ |
2020 | INIT_LIST_HEAD(&dev->vidq.active); | |
2021 | INIT_LIST_HEAD(&dev->vidq.queued); | |
2022 | ||
a6c2ba28 | 2023 | |
5a80415b SS |
2024 | if (dev->has_msp34xx) { |
2025 | /* Send a reset to other chips via gpio */ | |
2026 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1); | |
2027 | msleep(3); | |
2028 | em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1); | |
2029 | msleep(3); | |
5a80415b | 2030 | } |
195a4ef6 | 2031 | |
5a80415b SS |
2032 | video_mux(dev, 0); |
2033 | ||
e5589bef MCC |
2034 | em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n", |
2035 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN, | |
2036 | dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN); | |
a6c2ba28 | 2037 | |
6d79468d MCC |
2038 | mutex_lock(&em28xx_extension_devlist_lock); |
2039 | if (!list_empty(&em28xx_extension_devlist)) { | |
2040 | list_for_each_entry(ops, &em28xx_extension_devlist, next) { | |
2041 | if (ops->id) | |
2042 | ops->init(dev); | |
2043 | } | |
2044 | } | |
2045 | mutex_unlock(&em28xx_extension_devlist_lock); | |
2046 | ||
a6c2ba28 | 2047 | return 0; |
0be43754 MCC |
2048 | |
2049 | fail_unreg: | |
2050 | em28xx_release_resources(dev); | |
2051 | mutex_unlock(&dev->lock); | |
2052 | kfree(dev); | |
2053 | return retval; | |
a6c2ba28 | 2054 | } |
2055 | ||
d7448a8d MCC |
2056 | #if defined(CONFIG_MODULES) && defined(MODULE) |
2057 | static void request_module_async(struct work_struct *work) | |
2058 | { | |
2059 | struct em28xx *dev = container_of(work, | |
2060 | struct em28xx, request_module_wk); | |
2061 | ||
3f4dfe2a MCC |
2062 | if (dev->has_audio_class) |
2063 | request_module("snd-usb-audio"); | |
2064 | else | |
d7448a8d | 2065 | request_module("em28xx-alsa"); |
3aefb79a MCC |
2066 | |
2067 | if (dev->has_dvb) | |
2068 | request_module("em28xx-dvb"); | |
d7448a8d MCC |
2069 | } |
2070 | ||
2071 | static void request_modules(struct em28xx *dev) | |
2072 | { | |
2073 | INIT_WORK(&dev->request_module_wk, request_module_async); | |
2074 | schedule_work(&dev->request_module_wk); | |
2075 | } | |
2076 | #else | |
2077 | #define request_modules(dev) | |
2078 | #endif /* CONFIG_MODULES */ | |
2079 | ||
a6c2ba28 | 2080 | /* |
3acf2809 | 2081 | * em28xx_usb_probe() |
a6c2ba28 | 2082 | * checks for supported devices |
2083 | */ | |
3acf2809 | 2084 | static int em28xx_usb_probe(struct usb_interface *interface, |
a6c2ba28 | 2085 | const struct usb_device_id *id) |
2086 | { | |
2087 | const struct usb_endpoint_descriptor *endpoint; | |
2088 | struct usb_device *udev; | |
9d4d9c05 | 2089 | struct usb_interface *uif; |
3acf2809 | 2090 | struct em28xx *dev = NULL; |
a6c2ba28 | 2091 | int retval = -ENODEV; |
03910cc3 | 2092 | int i, nr, ifnum; |
a6c2ba28 | 2093 | |
2094 | udev = usb_get_dev(interface_to_usbdev(interface)); | |
d5e52653 MCC |
2095 | ifnum = interface->altsetting[0].desc.bInterfaceNumber; |
2096 | ||
e5589bef | 2097 | /* Check to see next free device and mark as used */ |
6ea54d93 DSL |
2098 | nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS); |
2099 | em28xx_devused |= 1<<nr; | |
a6c2ba28 | 2100 | |
596d92d5 | 2101 | /* Don't register audio interfaces */ |
91cad0f2 | 2102 | if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) { |
3acf2809 | 2103 | em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n", |
6ea54d93 DSL |
2104 | udev->descriptor.idVendor, |
2105 | udev->descriptor.idProduct, | |
91cad0f2 MCC |
2106 | ifnum, |
2107 | interface->altsetting[0].desc.bInterfaceClass); | |
e5589bef | 2108 | |
6ea54d93 | 2109 | em28xx_devused &= ~(1<<nr); |
596d92d5 | 2110 | return -ENODEV; |
91cad0f2 MCC |
2111 | } |
2112 | ||
3acf2809 | 2113 | em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n", |
6ea54d93 DSL |
2114 | udev->descriptor.idVendor, |
2115 | udev->descriptor.idProduct, | |
91cad0f2 MCC |
2116 | ifnum, |
2117 | interface->altsetting[0].desc.bInterfaceClass); | |
596d92d5 | 2118 | |
d5e52653 MCC |
2119 | endpoint = &interface->cur_altsetting->endpoint[1].desc; |
2120 | ||
59c51591 | 2121 | /* check if the device has the iso in endpoint at the correct place */ |
a6c2ba28 | 2122 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != |
2123 | USB_ENDPOINT_XFER_ISOC) { | |
3acf2809 | 2124 | em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); |
6ea54d93 | 2125 | em28xx_devused &= ~(1<<nr); |
a6c2ba28 | 2126 | return -ENODEV; |
2127 | } | |
2128 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { | |
3acf2809 | 2129 | em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); |
6ea54d93 | 2130 | em28xx_devused &= ~(1<<nr); |
a6c2ba28 | 2131 | return -ENODEV; |
2132 | } | |
2133 | ||
19478843 | 2134 | if (nr >= EM28XX_MAXBOARDS) { |
6ea54d93 DSL |
2135 | printk(DRIVER_NAME ": Supports only %i em28xx boards.\n", |
2136 | EM28XX_MAXBOARDS); | |
2137 | em28xx_devused &= ~(1<<nr); | |
596d92d5 MCC |
2138 | return -ENOMEM; |
2139 | } | |
2140 | ||
2141 | /* allocate memory for our device state and initialize it */ | |
7408187d | 2142 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
596d92d5 | 2143 | if (dev == NULL) { |
3acf2809 | 2144 | em28xx_err(DRIVER_NAME ": out of memory!\n"); |
6ea54d93 | 2145 | em28xx_devused &= ~(1<<nr); |
596d92d5 MCC |
2146 | return -ENOMEM; |
2147 | } | |
596d92d5 | 2148 | |
e5589bef | 2149 | snprintf(dev->name, 29, "em28xx #%d", nr); |
03910cc3 MCC |
2150 | dev->devno = nr; |
2151 | dev->model = id->driver_info; | |
3687e1e6 | 2152 | dev->alt = -1; |
e5589bef | 2153 | |
d7448a8d MCC |
2154 | /* Checks if audio is provided by some interface */ |
2155 | for (i = 0; i < udev->config->desc.bNumInterfaces; i++) { | |
2156 | uif = udev->config->interface[i]; | |
2157 | if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) { | |
2158 | dev->has_audio_class = 1; | |
2159 | break; | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | printk(KERN_INFO DRIVER_NAME " %s usb audio class\n", | |
2164 | dev->has_audio_class ? "Has" : "Doesn't have"); | |
2165 | ||
9d4d9c05 MCC |
2166 | /* compute alternate max packet sizes */ |
2167 | uif = udev->actconfig->interface[0]; | |
2168 | ||
6ea54d93 DSL |
2169 | dev->num_alt = uif->num_altsetting; |
2170 | em28xx_info("Alternate settings: %i\n", dev->num_alt); | |
2171 | /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */ | |
2172 | dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL); | |
2173 | ||
9d4d9c05 | 2174 | if (dev->alt_max_pkt_size == NULL) { |
e5589bef | 2175 | em28xx_errdev("out of memory!\n"); |
6ea54d93 | 2176 | em28xx_devused &= ~(1<<nr); |
1207cf84 | 2177 | kfree(dev); |
9d4d9c05 MCC |
2178 | return -ENOMEM; |
2179 | } | |
2180 | ||
2181 | for (i = 0; i < dev->num_alt ; i++) { | |
2182 | u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc. | |
2183 | wMaxPacketSize); | |
2184 | dev->alt_max_pkt_size[i] = | |
2185 | (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); | |
6ea54d93 DSL |
2186 | em28xx_info("Alternate setting %i, max size= %i\n", i, |
2187 | dev->alt_max_pkt_size[i]); | |
9d4d9c05 MCC |
2188 | } |
2189 | ||
6ea54d93 | 2190 | if ((card[nr] >= 0) && (card[nr] < em28xx_bcount)) |
03910cc3 | 2191 | dev->model = card[nr]; |
596d92d5 | 2192 | |
a6c2ba28 | 2193 | /* allocate device struct */ |
03910cc3 | 2194 | retval = em28xx_init_dev(&dev, udev, nr); |
a6c2ba28 | 2195 | if (retval) |
2196 | return retval; | |
2197 | ||
03910cc3 | 2198 | em28xx_info("Found %s\n", em28xx_boards[dev->model].name); |
a6c2ba28 | 2199 | |
2200 | /* save our data pointer in this interface device */ | |
2201 | usb_set_intfdata(interface, dev); | |
d7448a8d MCC |
2202 | |
2203 | request_modules(dev); | |
2204 | ||
a6c2ba28 | 2205 | return 0; |
2206 | } | |
2207 | ||
2208 | /* | |
3acf2809 | 2209 | * em28xx_usb_disconnect() |
a6c2ba28 | 2210 | * called when the device gets diconencted |
2211 | * video device will be unregistered on v4l2_close in case it is still open | |
2212 | */ | |
3acf2809 | 2213 | static void em28xx_usb_disconnect(struct usb_interface *interface) |
a6c2ba28 | 2214 | { |
5a80415b | 2215 | struct em28xx *dev; |
6d79468d | 2216 | struct em28xx_ops *ops = NULL; |
5a80415b SS |
2217 | |
2218 | dev = usb_get_intfdata(interface); | |
a6c2ba28 | 2219 | usb_set_intfdata(interface, NULL); |
2220 | ||
2221 | if (!dev) | |
2222 | return; | |
2223 | ||
5a80415b | 2224 | em28xx_info("disconnecting %s\n", dev->vdev->name); |
a6c2ba28 | 2225 | |
6ea54d93 DSL |
2226 | /* wait until all current v4l2 io is finished then deallocate |
2227 | resources */ | |
3593cab5 | 2228 | mutex_lock(&dev->lock); |
a6c2ba28 | 2229 | |
a6c2ba28 | 2230 | wake_up_interruptible_all(&dev->open); |
2231 | ||
2232 | if (dev->users) { | |
3acf2809 | 2233 | em28xx_warn |
a6c2ba28 | 2234 | ("device /dev/video%d is open! Deregistration and memory " |
e5589bef MCC |
2235 | "deallocation are deferred on close.\n", |
2236 | dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN); | |
2237 | ||
a6c2ba28 | 2238 | dev->state |= DEV_MISCONFIGURED; |
3acf2809 | 2239 | em28xx_uninit_isoc(dev); |
a6c2ba28 | 2240 | dev->state |= DEV_DISCONNECTED; |
2241 | wake_up_interruptible(&dev->wait_frame); | |
2242 | wake_up_interruptible(&dev->wait_stream); | |
2243 | } else { | |
2244 | dev->state |= DEV_DISCONNECTED; | |
3acf2809 | 2245 | em28xx_release_resources(dev); |
a6c2ba28 | 2246 | } |
3593cab5 | 2247 | mutex_unlock(&dev->lock); |
a6c2ba28 | 2248 | |
6d79468d MCC |
2249 | mutex_lock(&em28xx_extension_devlist_lock); |
2250 | if (!list_empty(&em28xx_extension_devlist)) { | |
2251 | list_for_each_entry(ops, &em28xx_extension_devlist, next) { | |
2252 | ops->fini(dev); | |
2253 | } | |
2254 | } | |
2255 | mutex_unlock(&em28xx_extension_devlist_lock); | |
2256 | ||
9d4d9c05 MCC |
2257 | if (!dev->users) { |
2258 | kfree(dev->alt_max_pkt_size); | |
a6c2ba28 | 2259 | kfree(dev); |
9d4d9c05 | 2260 | } |
a6c2ba28 | 2261 | } |
2262 | ||
3acf2809 | 2263 | static struct usb_driver em28xx_usb_driver = { |
3acf2809 MCC |
2264 | .name = "em28xx", |
2265 | .probe = em28xx_usb_probe, | |
2266 | .disconnect = em28xx_usb_disconnect, | |
2267 | .id_table = em28xx_id_table, | |
a6c2ba28 | 2268 | }; |
2269 | ||
3acf2809 | 2270 | static int __init em28xx_module_init(void) |
a6c2ba28 | 2271 | { |
2272 | int result; | |
2273 | ||
2274 | printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n", | |
3acf2809 MCC |
2275 | (EM28XX_VERSION_CODE >> 16) & 0xff, |
2276 | (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff); | |
a6c2ba28 | 2277 | #ifdef SNAPSHOT |
2278 | printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n", | |
2279 | SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100); | |
2280 | #endif | |
2281 | ||
2282 | /* register this driver with the USB subsystem */ | |
3acf2809 | 2283 | result = usb_register(&em28xx_usb_driver); |
a6c2ba28 | 2284 | if (result) |
3acf2809 | 2285 | em28xx_err(DRIVER_NAME |
a6c2ba28 | 2286 | " usb_register failed. Error number %d.\n", result); |
2287 | ||
2288 | return result; | |
2289 | } | |
2290 | ||
3acf2809 | 2291 | static void __exit em28xx_module_exit(void) |
a6c2ba28 | 2292 | { |
2293 | /* deregister this driver with the USB subsystem */ | |
3acf2809 | 2294 | usb_deregister(&em28xx_usb_driver); |
a6c2ba28 | 2295 | } |
2296 | ||
3acf2809 MCC |
2297 | module_init(em28xx_module_init); |
2298 | module_exit(em28xx_module_exit); |