Commit | Line | Data |
---|---|---|
e0d3bafd | 1 | /* |
cde4362f MCC |
2 | cx231xx-video.c - driver for Conexant Cx23100/101/102 |
3 | USB video capture devices | |
e0d3bafd SD |
4 | |
5 | Copyright (C) 2008 <srinivasa.deevi at conexant dot com> | |
84b5dbf3 MCC |
6 | Based on em28xx driver |
7 | Based on cx23885 driver | |
8 | Based on cx88 driver | |
e0d3bafd SD |
9 | |
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 2 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
589dadf2 | 25 | #include "cx231xx.h" |
e0d3bafd SD |
26 | #include <linux/init.h> |
27 | #include <linux/list.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/kernel.h> | |
30 | #include <linux/bitmap.h> | |
e0d3bafd | 31 | #include <linux/i2c.h> |
e0d3bafd SD |
32 | #include <linux/mm.h> |
33 | #include <linux/mutex.h> | |
5a0e3ad6 | 34 | #include <linux/slab.h> |
e0d3bafd SD |
35 | |
36 | #include <media/v4l2-common.h> | |
37 | #include <media/v4l2-ioctl.h> | |
1d08a4fa | 38 | #include <media/v4l2-event.h> |
e0d3bafd SD |
39 | #include <media/msp3400.h> |
40 | #include <media/tuner.h> | |
41 | ||
42 | #include "dvb_frontend.h" | |
43 | ||
e0d3bafd SD |
44 | #include "cx231xx-vbi.h" |
45 | ||
3d28cf3e | 46 | #define CX231XX_VERSION "0.0.3" |
818fdf34 | 47 | |
e0d3bafd SD |
48 | #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>" |
49 | #define DRIVER_DESC "Conexant cx231xx based USB video device driver" | |
50 | ||
e0d3bafd SD |
51 | #define cx231xx_videodbg(fmt, arg...) do {\ |
52 | if (video_debug) \ | |
53 | printk(KERN_INFO "%s %s :"fmt, \ | |
54 | dev->name, __func__ , ##arg); } while (0) | |
55 | ||
56 | static unsigned int isoc_debug; | |
57 | module_param(isoc_debug, int, 0644); | |
58 | MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); | |
59 | ||
60 | #define cx231xx_isocdbg(fmt, arg...) \ | |
61 | do {\ | |
62 | if (isoc_debug) { \ | |
63 | printk(KERN_INFO "%s %s :"fmt, \ | |
64 | dev->name, __func__ , ##arg); \ | |
65 | } \ | |
66 | } while (0) | |
67 | ||
68 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
69 | MODULE_DESCRIPTION(DRIVER_DESC); | |
70 | MODULE_LICENSE("GPL"); | |
1990d50b | 71 | MODULE_VERSION(CX231XX_VERSION); |
e0d3bafd | 72 | |
e0d3bafd SD |
73 | static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; |
74 | static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; | |
75 | static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; | |
76 | static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET }; | |
77 | ||
84b5dbf3 | 78 | module_param_array(card, int, NULL, 0444); |
e0d3bafd SD |
79 | module_param_array(video_nr, int, NULL, 0444); |
80 | module_param_array(vbi_nr, int, NULL, 0444); | |
81 | module_param_array(radio_nr, int, NULL, 0444); | |
82 | ||
84b5dbf3 | 83 | MODULE_PARM_DESC(card, "card type"); |
e0d3bafd | 84 | MODULE_PARM_DESC(video_nr, "video device numbers"); |
84b5dbf3 | 85 | MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); |
e0d3bafd SD |
86 | MODULE_PARM_DESC(radio_nr, "radio device numbers"); |
87 | ||
88 | static unsigned int video_debug; | |
89 | module_param(video_debug, int, 0644); | |
90 | MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); | |
91 | ||
e0d3bafd SD |
92 | /* supported video standards */ |
93 | static struct cx231xx_fmt format[] = { | |
94 | { | |
84b5dbf3 MCC |
95 | .name = "16bpp YUY2, 4:2:2, packed", |
96 | .fourcc = V4L2_PIX_FMT_YUYV, | |
97 | .depth = 16, | |
98 | .reg = 0, | |
99 | }, | |
e0d3bafd SD |
100 | }; |
101 | ||
e0d3bafd | 102 | |
3d263114 MCC |
103 | static int cx231xx_enable_analog_tuner(struct cx231xx *dev) |
104 | { | |
105 | #ifdef CONFIG_MEDIA_CONTROLLER | |
106 | struct media_device *mdev = dev->media_dev; | |
107 | struct media_entity *entity, *decoder = NULL, *source; | |
108 | struct media_link *link, *found_link = NULL; | |
109 | int i, ret, active_links = 0; | |
110 | ||
111 | if (!mdev) | |
112 | return 0; | |
113 | ||
114 | /* | |
115 | * This will find the tuner that is connected into the decoder. | |
116 | * Technically, this is not 100% correct, as the device may be | |
117 | * using an analog input instead of the tuner. However, as we can't | |
118 | * do DVB streaming while the DMA engine is being used for V4L2, | |
119 | * this should be enough for the actual needs. | |
120 | */ | |
121 | media_device_for_each_entity(entity, mdev) { | |
122 | if (entity->type == MEDIA_ENT_T_V4L2_SUBDEV_DECODER) { | |
123 | decoder = entity; | |
124 | break; | |
125 | } | |
126 | } | |
127 | if (!decoder) | |
128 | return 0; | |
129 | ||
130 | for (i = 0; i < decoder->num_links; i++) { | |
131 | link = &decoder->links[i]; | |
132 | if (link->sink->entity == decoder) { | |
133 | found_link = link; | |
134 | if (link->flags & MEDIA_LNK_FL_ENABLED) | |
135 | active_links++; | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | if (active_links == 1 || !found_link) | |
141 | return 0; | |
142 | ||
143 | source = found_link->source->entity; | |
144 | for (i = 0; i < source->num_links; i++) { | |
145 | struct media_entity *sink; | |
146 | int flags = 0; | |
147 | ||
148 | link = &source->links[i]; | |
149 | sink = link->sink->entity; | |
150 | ||
151 | if (sink == entity) | |
152 | flags = MEDIA_LNK_FL_ENABLED; | |
153 | ||
154 | ret = media_entity_setup_link(link, flags); | |
155 | if (ret) { | |
156 | dev_err(dev->dev, | |
157 | "Couldn't change link %s->%s to %s. Error %d\n", | |
158 | source->name, sink->name, | |
159 | flags ? "enabled" : "disabled", | |
160 | ret); | |
161 | return ret; | |
162 | } else | |
163 | dev_dbg(dev->dev, | |
164 | "link %s->%s was %s\n", | |
165 | source->name, sink->name, | |
166 | flags ? "ENABLED" : "disabled"); | |
167 | } | |
168 | #endif | |
169 | return 0; | |
170 | } | |
171 | ||
e0d3bafd SD |
172 | /* ------------------------------------------------------------------ |
173 | Video buffer and parser functions | |
174 | ------------------------------------------------------------------*/ | |
175 | ||
176 | /* | |
177 | * Announces that a buffer were filled and request the next | |
178 | */ | |
179 | static inline void buffer_filled(struct cx231xx *dev, | |
84b5dbf3 MCC |
180 | struct cx231xx_dmaqueue *dma_q, |
181 | struct cx231xx_buffer *buf) | |
e0d3bafd SD |
182 | { |
183 | /* Advice that buffer was filled */ | |
184 | cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); | |
185 | buf->vb.state = VIDEOBUF_DONE; | |
186 | buf->vb.field_count++; | |
8e6057b5 | 187 | v4l2_get_timestamp(&buf->vb.ts); |
e0d3bafd | 188 | |
64fbf444 PB |
189 | if (dev->USE_ISO) |
190 | dev->video_mode.isoc_ctl.buf = NULL; | |
191 | else | |
192 | dev->video_mode.bulk_ctl.buf = NULL; | |
e0d3bafd SD |
193 | |
194 | list_del(&buf->vb.queue); | |
195 | wake_up(&buf->vb.done); | |
196 | } | |
197 | ||
84b5dbf3 | 198 | static inline void print_err_status(struct cx231xx *dev, int packet, int status) |
e0d3bafd SD |
199 | { |
200 | char *errmsg = "Unknown"; | |
201 | ||
202 | switch (status) { | |
203 | case -ENOENT: | |
204 | errmsg = "unlinked synchronuously"; | |
205 | break; | |
206 | case -ECONNRESET: | |
207 | errmsg = "unlinked asynchronuously"; | |
208 | break; | |
209 | case -ENOSR: | |
210 | errmsg = "Buffer error (overrun)"; | |
211 | break; | |
212 | case -EPIPE: | |
213 | errmsg = "Stalled (device not responding)"; | |
214 | break; | |
215 | case -EOVERFLOW: | |
216 | errmsg = "Babble (bad cable?)"; | |
217 | break; | |
218 | case -EPROTO: | |
219 | errmsg = "Bit-stuff error (bad cable?)"; | |
220 | break; | |
221 | case -EILSEQ: | |
222 | errmsg = "CRC/Timeout (could be anything)"; | |
223 | break; | |
224 | case -ETIME: | |
225 | errmsg = "Device does not respond"; | |
226 | break; | |
227 | } | |
228 | if (packet < 0) { | |
84b5dbf3 | 229 | cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg); |
e0d3bafd SD |
230 | } else { |
231 | cx231xx_isocdbg("URB packet %d, status %d [%s].\n", | |
84b5dbf3 | 232 | packet, status, errmsg); |
e0d3bafd SD |
233 | } |
234 | } | |
235 | ||
236 | /* | |
237 | * video-buf generic routine to get the next available buffer | |
238 | */ | |
239 | static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q, | |
84b5dbf3 | 240 | struct cx231xx_buffer **buf) |
e0d3bafd | 241 | { |
84b5dbf3 MCC |
242 | struct cx231xx_video_mode *vmode = |
243 | container_of(dma_q, struct cx231xx_video_mode, vidq); | |
244 | struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode); | |
e0d3bafd SD |
245 | |
246 | char *outp; | |
247 | ||
e0d3bafd SD |
248 | if (list_empty(&dma_q->active)) { |
249 | cx231xx_isocdbg("No active queue to serve\n"); | |
64fbf444 PB |
250 | if (dev->USE_ISO) |
251 | dev->video_mode.isoc_ctl.buf = NULL; | |
252 | else | |
253 | dev->video_mode.bulk_ctl.buf = NULL; | |
e0d3bafd SD |
254 | *buf = NULL; |
255 | return; | |
256 | } | |
257 | ||
258 | /* Get the next buffer */ | |
259 | *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue); | |
260 | ||
25985edc | 261 | /* Cleans up buffer - Useful for testing for frame/URB loss */ |
e0d3bafd SD |
262 | outp = videobuf_to_vmalloc(&(*buf)->vb); |
263 | memset(outp, 0, (*buf)->vb.size); | |
264 | ||
64fbf444 PB |
265 | if (dev->USE_ISO) |
266 | dev->video_mode.isoc_ctl.buf = *buf; | |
267 | else | |
268 | dev->video_mode.bulk_ctl.buf = *buf; | |
e0d3bafd SD |
269 | |
270 | return; | |
271 | } | |
272 | ||
273 | /* | |
274 | * Controls the isoc copy of each urb packet | |
275 | */ | |
276 | static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb) | |
277 | { | |
84b5dbf3 | 278 | struct cx231xx_dmaqueue *dma_q = urb->context; |
4df16f70 | 279 | int i; |
e0d3bafd | 280 | unsigned char *p_buffer; |
84b5dbf3 MCC |
281 | u32 bytes_parsed = 0, buffer_size = 0; |
282 | u8 sav_eav = 0; | |
e0d3bafd SD |
283 | |
284 | if (!dev) | |
285 | return 0; | |
286 | ||
990862a2 | 287 | if (dev->state & DEV_DISCONNECTED) |
e0d3bafd SD |
288 | return 0; |
289 | ||
290 | if (urb->status < 0) { | |
291 | print_err_status(dev, -1, urb->status); | |
292 | if (urb->status == -ENOENT) | |
293 | return 0; | |
294 | } | |
295 | ||
e0d3bafd SD |
296 | for (i = 0; i < urb->number_of_packets; i++) { |
297 | int status = urb->iso_frame_desc[i].status; | |
298 | ||
299 | if (status < 0) { | |
300 | print_err_status(dev, i, status); | |
301 | if (urb->iso_frame_desc[i].status != -EPROTO) | |
302 | continue; | |
303 | } | |
304 | ||
84b5dbf3 | 305 | if (urb->iso_frame_desc[i].actual_length <= 0) { |
e0d3bafd SD |
306 | /* cx231xx_isocdbg("packet %d is empty",i); - spammy */ |
307 | continue; | |
308 | } | |
309 | if (urb->iso_frame_desc[i].actual_length > | |
84b5dbf3 | 310 | dev->video_mode.max_pkt_size) { |
e0d3bafd SD |
311 | cx231xx_isocdbg("packet bigger than packet size"); |
312 | continue; | |
313 | } | |
314 | ||
84b5dbf3 | 315 | /* get buffer pointer and length */ |
e0d3bafd | 316 | p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
84b5dbf3 MCC |
317 | buffer_size = urb->iso_frame_desc[i].actual_length; |
318 | bytes_parsed = 0; | |
319 | ||
320 | if (dma_q->is_partial_line) { | |
b9255176 | 321 | /* Handle the case of a partial line */ |
84b5dbf3 MCC |
322 | sav_eav = dma_q->last_sav; |
323 | } else { | |
b9255176 SD |
324 | /* Check for a SAV/EAV overlapping |
325 | the buffer boundary */ | |
84b5dbf3 MCC |
326 | sav_eav = |
327 | cx231xx_find_boundary_SAV_EAV(p_buffer, | |
328 | dma_q->partial_buf, | |
329 | &bytes_parsed); | |
330 | } | |
e0d3bafd | 331 | |
84b5dbf3 | 332 | sav_eav &= 0xF0; |
cde4362f MCC |
333 | /* Get the first line if we have some portion of an SAV/EAV from |
334 | the last buffer or a partial line */ | |
84b5dbf3 | 335 | if (sav_eav) { |
cde4362f | 336 | bytes_parsed += cx231xx_get_video_line(dev, dma_q, |
b9255176 SD |
337 | sav_eav, /* SAV/EAV */ |
338 | p_buffer + bytes_parsed, /* p_buffer */ | |
339 | buffer_size - bytes_parsed);/* buf size */ | |
84b5dbf3 | 340 | } |
e0d3bafd | 341 | |
84b5dbf3 MCC |
342 | /* Now parse data that is completely in this buffer */ |
343 | /* dma_q->is_partial_line = 0; */ | |
e0d3bafd | 344 | |
84b5dbf3 MCC |
345 | while (bytes_parsed < buffer_size) { |
346 | u32 bytes_used = 0; | |
e0d3bafd | 347 | |
b9255176 SD |
348 | sav_eav = cx231xx_find_next_SAV_EAV( |
349 | p_buffer + bytes_parsed, /* p_buffer */ | |
350 | buffer_size - bytes_parsed, /* buf size */ | |
351 | &bytes_used);/* bytes used to get SAV/EAV */ | |
e0d3bafd | 352 | |
84b5dbf3 | 353 | bytes_parsed += bytes_used; |
e0d3bafd | 354 | |
84b5dbf3 MCC |
355 | sav_eav &= 0xF0; |
356 | if (sav_eav && (bytes_parsed < buffer_size)) { | |
cde4362f | 357 | bytes_parsed += cx231xx_get_video_line(dev, |
b9255176 SD |
358 | dma_q, sav_eav, /* SAV/EAV */ |
359 | p_buffer + bytes_parsed,/* p_buffer */ | |
360 | buffer_size - bytes_parsed);/*buf size*/ | |
84b5dbf3 MCC |
361 | } |
362 | } | |
e0d3bafd | 363 | |
cde4362f MCC |
364 | /* Save the last four bytes of the buffer so we can check the |
365 | buffer boundary condition next time */ | |
84b5dbf3 MCC |
366 | memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4); |
367 | bytes_parsed = 0; | |
e0d3bafd | 368 | |
84b5dbf3 | 369 | } |
4df16f70 | 370 | return 1; |
e0d3bafd SD |
371 | } |
372 | ||
64fbf444 PB |
373 | static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb) |
374 | { | |
64fbf444 | 375 | struct cx231xx_dmaqueue *dma_q = urb->context; |
64fbf444 PB |
376 | unsigned char *p_buffer; |
377 | u32 bytes_parsed = 0, buffer_size = 0; | |
378 | u8 sav_eav = 0; | |
379 | ||
380 | if (!dev) | |
381 | return 0; | |
382 | ||
990862a2 | 383 | if (dev->state & DEV_DISCONNECTED) |
64fbf444 PB |
384 | return 0; |
385 | ||
386 | if (urb->status < 0) { | |
387 | print_err_status(dev, -1, urb->status); | |
388 | if (urb->status == -ENOENT) | |
389 | return 0; | |
390 | } | |
391 | ||
64fbf444 PB |
392 | if (1) { |
393 | ||
394 | /* get buffer pointer and length */ | |
395 | p_buffer = urb->transfer_buffer; | |
396 | buffer_size = urb->actual_length; | |
397 | bytes_parsed = 0; | |
398 | ||
399 | if (dma_q->is_partial_line) { | |
400 | /* Handle the case of a partial line */ | |
401 | sav_eav = dma_q->last_sav; | |
402 | } else { | |
403 | /* Check for a SAV/EAV overlapping | |
404 | the buffer boundary */ | |
405 | sav_eav = | |
406 | cx231xx_find_boundary_SAV_EAV(p_buffer, | |
407 | dma_q->partial_buf, | |
408 | &bytes_parsed); | |
409 | } | |
410 | ||
411 | sav_eav &= 0xF0; | |
412 | /* Get the first line if we have some portion of an SAV/EAV from | |
413 | the last buffer or a partial line */ | |
414 | if (sav_eav) { | |
415 | bytes_parsed += cx231xx_get_video_line(dev, dma_q, | |
416 | sav_eav, /* SAV/EAV */ | |
417 | p_buffer + bytes_parsed, /* p_buffer */ | |
418 | buffer_size - bytes_parsed);/* buf size */ | |
419 | } | |
420 | ||
421 | /* Now parse data that is completely in this buffer */ | |
422 | /* dma_q->is_partial_line = 0; */ | |
423 | ||
424 | while (bytes_parsed < buffer_size) { | |
425 | u32 bytes_used = 0; | |
426 | ||
427 | sav_eav = cx231xx_find_next_SAV_EAV( | |
428 | p_buffer + bytes_parsed, /* p_buffer */ | |
429 | buffer_size - bytes_parsed, /* buf size */ | |
430 | &bytes_used);/* bytes used to get SAV/EAV */ | |
431 | ||
432 | bytes_parsed += bytes_used; | |
433 | ||
434 | sav_eav &= 0xF0; | |
435 | if (sav_eav && (bytes_parsed < buffer_size)) { | |
436 | bytes_parsed += cx231xx_get_video_line(dev, | |
437 | dma_q, sav_eav, /* SAV/EAV */ | |
438 | p_buffer + bytes_parsed,/* p_buffer */ | |
439 | buffer_size - bytes_parsed);/*buf size*/ | |
440 | } | |
441 | } | |
442 | ||
443 | /* Save the last four bytes of the buffer so we can check the | |
444 | buffer boundary condition next time */ | |
445 | memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4); | |
446 | bytes_parsed = 0; | |
447 | ||
448 | } | |
4df16f70 | 449 | return 1; |
64fbf444 PB |
450 | } |
451 | ||
452 | ||
cde4362f MCC |
453 | u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf, |
454 | u32 *p_bytes_used) | |
e0d3bafd | 455 | { |
84b5dbf3 MCC |
456 | u32 bytes_used; |
457 | u8 boundary_bytes[8]; | |
458 | u8 sav_eav = 0; | |
459 | ||
460 | *p_bytes_used = 0; | |
e0d3bafd | 461 | |
84b5dbf3 MCC |
462 | /* Create an array of the last 4 bytes of the last buffer and the first |
463 | 4 bytes of the current buffer. */ | |
e0d3bafd | 464 | |
84b5dbf3 MCC |
465 | memcpy(boundary_bytes, partial_buf, 4); |
466 | memcpy(boundary_bytes + 4, p_buffer, 4); | |
e0d3bafd | 467 | |
84b5dbf3 | 468 | /* Check for the SAV/EAV in the boundary buffer */ |
cde4362f MCC |
469 | sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8, |
470 | &bytes_used); | |
e0d3bafd | 471 | |
84b5dbf3 MCC |
472 | if (sav_eav) { |
473 | /* found a boundary SAV/EAV. Updates the bytes used to reflect | |
474 | only those used in the new buffer */ | |
475 | *p_bytes_used = bytes_used - 4; | |
476 | } | |
e0d3bafd | 477 | |
84b5dbf3 | 478 | return sav_eav; |
e0d3bafd SD |
479 | } |
480 | ||
cde4362f | 481 | u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used) |
84b5dbf3 MCC |
482 | { |
483 | u32 i; | |
484 | u8 sav_eav = 0; | |
e0d3bafd | 485 | |
cde4362f MCC |
486 | /* |
487 | * Don't search if the buffer size is less than 4. It causes a page | |
488 | * fault since buffer_size - 4 evaluates to a large number in that | |
489 | * case. | |
490 | */ | |
84b5dbf3 MCC |
491 | if (buffer_size < 4) { |
492 | *p_bytes_used = buffer_size; | |
493 | return 0; | |
494 | } | |
e0d3bafd | 495 | |
84b5dbf3 | 496 | for (i = 0; i < (buffer_size - 3); i++) { |
e0d3bafd | 497 | |
84b5dbf3 MCC |
498 | if ((p_buffer[i] == 0xFF) && |
499 | (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) { | |
e0d3bafd | 500 | |
84b5dbf3 MCC |
501 | *p_bytes_used = i + 4; |
502 | sav_eav = p_buffer[i + 3]; | |
503 | return sav_eav; | |
504 | } | |
505 | } | |
e0d3bafd | 506 | |
84b5dbf3 MCC |
507 | *p_bytes_used = buffer_size; |
508 | return 0; | |
509 | } | |
e0d3bafd | 510 | |
cde4362f MCC |
511 | u32 cx231xx_get_video_line(struct cx231xx *dev, |
512 | struct cx231xx_dmaqueue *dma_q, u8 sav_eav, | |
513 | u8 *p_buffer, u32 buffer_size) | |
84b5dbf3 MCC |
514 | { |
515 | u32 bytes_copied = 0; | |
516 | int current_field = -1; | |
517 | ||
518 | switch (sav_eav) { | |
519 | case SAV_ACTIVE_VIDEO_FIELD1: | |
cde4362f MCC |
520 | /* looking for skipped line which occurred in PAL 720x480 mode. |
521 | In this case, there will be no active data contained | |
522 | between the SAV and EAV */ | |
523 | if ((buffer_size > 3) && (p_buffer[0] == 0xFF) && | |
524 | (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) && | |
525 | ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || | |
526 | (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) || | |
527 | (p_buffer[3] == EAV_VBLANK_FIELD1) || | |
528 | (p_buffer[3] == EAV_VBLANK_FIELD2))) | |
84b5dbf3 | 529 | return bytes_copied; |
84b5dbf3 MCC |
530 | current_field = 1; |
531 | break; | |
e0d3bafd | 532 | |
84b5dbf3 | 533 | case SAV_ACTIVE_VIDEO_FIELD2: |
cde4362f MCC |
534 | /* looking for skipped line which occurred in PAL 720x480 mode. |
535 | In this case, there will be no active data contained between | |
536 | the SAV and EAV */ | |
537 | if ((buffer_size > 3) && (p_buffer[0] == 0xFF) && | |
538 | (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) && | |
539 | ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) || | |
540 | (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) || | |
541 | (p_buffer[3] == EAV_VBLANK_FIELD1) || | |
542 | (p_buffer[3] == EAV_VBLANK_FIELD2))) | |
84b5dbf3 | 543 | return bytes_copied; |
84b5dbf3 MCC |
544 | current_field = 2; |
545 | break; | |
546 | } | |
e0d3bafd | 547 | |
84b5dbf3 | 548 | dma_q->last_sav = sav_eav; |
e0d3bafd | 549 | |
cde4362f MCC |
550 | bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer, |
551 | buffer_size, current_field); | |
84b5dbf3 MCC |
552 | |
553 | return bytes_copied; | |
e0d3bafd SD |
554 | } |
555 | ||
cde4362f MCC |
556 | u32 cx231xx_copy_video_line(struct cx231xx *dev, |
557 | struct cx231xx_dmaqueue *dma_q, u8 *p_line, | |
84b5dbf3 | 558 | u32 length, int field_number) |
e0d3bafd | 559 | { |
84b5dbf3 MCC |
560 | u32 bytes_to_copy; |
561 | struct cx231xx_buffer *buf; | |
562 | u32 _line_size = dev->width * 2; | |
e0d3bafd | 563 | |
cde4362f | 564 | if (dma_q->current_field != field_number) |
84b5dbf3 | 565 | cx231xx_reset_video_buffer(dev, dma_q); |
e0d3bafd | 566 | |
84b5dbf3 | 567 | /* get the buffer pointer */ |
64fbf444 PB |
568 | if (dev->USE_ISO) |
569 | buf = dev->video_mode.isoc_ctl.buf; | |
570 | else | |
571 | buf = dev->video_mode.bulk_ctl.buf; | |
e0d3bafd | 572 | |
84b5dbf3 MCC |
573 | /* Remember the field number for next time */ |
574 | dma_q->current_field = field_number; | |
e0d3bafd | 575 | |
84b5dbf3 MCC |
576 | bytes_to_copy = dma_q->bytes_left_in_line; |
577 | if (bytes_to_copy > length) | |
578 | bytes_to_copy = length; | |
e0d3bafd | 579 | |
84b5dbf3 MCC |
580 | if (dma_q->lines_completed >= dma_q->lines_per_field) { |
581 | dma_q->bytes_left_in_line -= bytes_to_copy; | |
cde4362f MCC |
582 | dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? |
583 | 0 : 1; | |
84b5dbf3 MCC |
584 | return 0; |
585 | } | |
e0d3bafd | 586 | |
84b5dbf3 | 587 | dma_q->is_partial_line = 1; |
e0d3bafd | 588 | |
84b5dbf3 MCC |
589 | /* If we don't have a buffer, just return the number of bytes we would |
590 | have copied if we had a buffer. */ | |
591 | if (!buf) { | |
592 | dma_q->bytes_left_in_line -= bytes_to_copy; | |
cde4362f MCC |
593 | dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) |
594 | ? 0 : 1; | |
84b5dbf3 MCC |
595 | return bytes_to_copy; |
596 | } | |
e0d3bafd | 597 | |
84b5dbf3 MCC |
598 | /* copy the data to video buffer */ |
599 | cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy); | |
e0d3bafd | 600 | |
84b5dbf3 MCC |
601 | dma_q->pos += bytes_to_copy; |
602 | dma_q->bytes_left_in_line -= bytes_to_copy; | |
e0d3bafd | 603 | |
84b5dbf3 | 604 | if (dma_q->bytes_left_in_line == 0) { |
84b5dbf3 MCC |
605 | dma_q->bytes_left_in_line = _line_size; |
606 | dma_q->lines_completed++; | |
607 | dma_q->is_partial_line = 0; | |
e0d3bafd | 608 | |
84b5dbf3 | 609 | if (cx231xx_is_buffer_done(dev, dma_q) && buf) { |
84b5dbf3 | 610 | buffer_filled(dev, dma_q, buf); |
e0d3bafd | 611 | |
84b5dbf3 MCC |
612 | dma_q->pos = 0; |
613 | buf = NULL; | |
614 | dma_q->lines_completed = 0; | |
615 | } | |
616 | } | |
e0d3bafd | 617 | |
84b5dbf3 | 618 | return bytes_to_copy; |
e0d3bafd SD |
619 | } |
620 | ||
84b5dbf3 MCC |
621 | void cx231xx_reset_video_buffer(struct cx231xx *dev, |
622 | struct cx231xx_dmaqueue *dma_q) | |
e0d3bafd | 623 | { |
84b5dbf3 | 624 | struct cx231xx_buffer *buf; |
e0d3bafd | 625 | |
84b5dbf3 MCC |
626 | /* handle the switch from field 1 to field 2 */ |
627 | if (dma_q->current_field == 1) { | |
cde4362f | 628 | if (dma_q->lines_completed >= dma_q->lines_per_field) |
84b5dbf3 | 629 | dma_q->field1_done = 1; |
cde4362f | 630 | else |
84b5dbf3 | 631 | dma_q->field1_done = 0; |
84b5dbf3 | 632 | } |
e0d3bafd | 633 | |
64fbf444 PB |
634 | if (dev->USE_ISO) |
635 | buf = dev->video_mode.isoc_ctl.buf; | |
636 | else | |
637 | buf = dev->video_mode.bulk_ctl.buf; | |
e0d3bafd | 638 | |
84b5dbf3 | 639 | if (buf == NULL) { |
84b5dbf3 MCC |
640 | /* first try to get the buffer */ |
641 | get_next_buf(dma_q, &buf); | |
e0d3bafd | 642 | |
84b5dbf3 MCC |
643 | dma_q->pos = 0; |
644 | dma_q->field1_done = 0; | |
645 | dma_q->current_field = -1; | |
646 | } | |
e0d3bafd | 647 | |
84b5dbf3 MCC |
648 | /* reset the counters */ |
649 | dma_q->bytes_left_in_line = dev->width << 1; | |
650 | dma_q->lines_completed = 0; | |
e0d3bafd SD |
651 | } |
652 | ||
653 | int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q, | |
cde4362f | 654 | u8 *p_buffer, u32 bytes_to_copy) |
e0d3bafd | 655 | { |
84b5dbf3 MCC |
656 | u8 *p_out_buffer = NULL; |
657 | u32 current_line_bytes_copied = 0; | |
658 | struct cx231xx_buffer *buf; | |
659 | u32 _line_size = dev->width << 1; | |
660 | void *startwrite; | |
661 | int offset, lencopy; | |
e0d3bafd | 662 | |
64fbf444 PB |
663 | if (dev->USE_ISO) |
664 | buf = dev->video_mode.isoc_ctl.buf; | |
665 | else | |
666 | buf = dev->video_mode.bulk_ctl.buf; | |
e0d3bafd | 667 | |
84b5dbf3 MCC |
668 | if (buf == NULL) |
669 | return -1; | |
e0d3bafd SD |
670 | |
671 | p_out_buffer = videobuf_to_vmalloc(&buf->vb); | |
672 | ||
84b5dbf3 | 673 | current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line; |
e0d3bafd | 674 | |
84b5dbf3 MCC |
675 | /* Offset field 2 one line from the top of the buffer */ |
676 | offset = (dma_q->current_field == 1) ? 0 : _line_size; | |
e0d3bafd | 677 | |
84b5dbf3 MCC |
678 | /* Offset for field 2 */ |
679 | startwrite = p_out_buffer + offset; | |
e0d3bafd | 680 | |
84b5dbf3 MCC |
681 | /* lines already completed in the current field */ |
682 | startwrite += (dma_q->lines_completed * _line_size * 2); | |
e0d3bafd | 683 | |
84b5dbf3 MCC |
684 | /* bytes already completed in the current line */ |
685 | startwrite += current_line_bytes_copied; | |
e0d3bafd | 686 | |
cde4362f MCC |
687 | lencopy = dma_q->bytes_left_in_line > bytes_to_copy ? |
688 | bytes_to_copy : dma_q->bytes_left_in_line; | |
e0d3bafd | 689 | |
cde4362f | 690 | if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size)) |
84b5dbf3 | 691 | return 0; |
e0d3bafd | 692 | |
84b5dbf3 MCC |
693 | /* The below copies the UYVY data straight into video buffer */ |
694 | cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy); | |
e0d3bafd | 695 | |
84b5dbf3 | 696 | return 0; |
e0d3bafd SD |
697 | } |
698 | ||
cde4362f | 699 | void cx231xx_swab(u16 *from, u16 *to, u16 len) |
e0d3bafd | 700 | { |
84b5dbf3 | 701 | u16 i; |
e0d3bafd | 702 | |
84b5dbf3 MCC |
703 | if (len <= 0) |
704 | return; | |
e0d3bafd | 705 | |
cde4362f | 706 | for (i = 0; i < len / 2; i++) |
84b5dbf3 | 707 | to[i] = (from[i] << 8) | (from[i] >> 8); |
e0d3bafd SD |
708 | } |
709 | ||
84b5dbf3 | 710 | u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q) |
e0d3bafd | 711 | { |
84b5dbf3 | 712 | u8 buffer_complete = 0; |
e0d3bafd | 713 | |
84b5dbf3 | 714 | /* Dual field stream */ |
cde4362f MCC |
715 | buffer_complete = ((dma_q->current_field == 2) && |
716 | (dma_q->lines_completed >= dma_q->lines_per_field) && | |
717 | dma_q->field1_done); | |
e0d3bafd | 718 | |
84b5dbf3 | 719 | return buffer_complete; |
e0d3bafd SD |
720 | } |
721 | ||
e0d3bafd SD |
722 | /* ------------------------------------------------------------------ |
723 | Videobuf operations | |
724 | ------------------------------------------------------------------*/ | |
725 | ||
726 | static int | |
727 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) | |
728 | { | |
729 | struct cx231xx_fh *fh = vq->priv_data; | |
84b5dbf3 | 730 | struct cx231xx *dev = fh->dev; |
e0d3bafd | 731 | |
cde4362f | 732 | *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3; |
e0d3bafd SD |
733 | if (0 == *count) |
734 | *count = CX231XX_DEF_BUF; | |
735 | ||
736 | if (*count < CX231XX_MIN_BUF) | |
737 | *count = CX231XX_MIN_BUF; | |
738 | ||
3d263114 MCC |
739 | |
740 | cx231xx_enable_analog_tuner(dev); | |
741 | ||
e0d3bafd SD |
742 | return 0; |
743 | } | |
744 | ||
745 | /* This is called *without* dev->slock held; please keep it that way */ | |
746 | static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf) | |
747 | { | |
84b5dbf3 MCC |
748 | struct cx231xx_fh *fh = vq->priv_data; |
749 | struct cx231xx *dev = fh->dev; | |
e0d3bafd | 750 | unsigned long flags = 0; |
cde4362f | 751 | |
09f2082e | 752 | BUG_ON(in_interrupt()); |
e0d3bafd SD |
753 | |
754 | /* We used to wait for the buffer to finish here, but this didn't work | |
755 | because, as we were keeping the state as VIDEOBUF_QUEUED, | |
756 | videobuf_queue_cancel marked it as finished for us. | |
757 | (Also, it could wedge forever if the hardware was misconfigured.) | |
758 | ||
759 | This should be safe; by the time we get here, the buffer isn't | |
760 | queued anymore. If we ever start marking the buffers as | |
761 | VIDEOBUF_ACTIVE, it won't be, though. | |
84b5dbf3 | 762 | */ |
e0d3bafd | 763 | spin_lock_irqsave(&dev->video_mode.slock, flags); |
64fbf444 PB |
764 | if (dev->USE_ISO) { |
765 | if (dev->video_mode.isoc_ctl.buf == buf) | |
766 | dev->video_mode.isoc_ctl.buf = NULL; | |
767 | } else { | |
768 | if (dev->video_mode.bulk_ctl.buf == buf) | |
769 | dev->video_mode.bulk_ctl.buf = NULL; | |
770 | } | |
e0d3bafd SD |
771 | spin_unlock_irqrestore(&dev->video_mode.slock, flags); |
772 | ||
773 | videobuf_vmalloc_free(&buf->vb); | |
774 | buf->vb.state = VIDEOBUF_NEEDS_INIT; | |
775 | } | |
776 | ||
777 | static int | |
778 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, | |
84b5dbf3 | 779 | enum v4l2_field field) |
e0d3bafd | 780 | { |
84b5dbf3 MCC |
781 | struct cx231xx_fh *fh = vq->priv_data; |
782 | struct cx231xx_buffer *buf = | |
783 | container_of(vb, struct cx231xx_buffer, vb); | |
784 | struct cx231xx *dev = fh->dev; | |
785 | int rc = 0, urb_init = 0; | |
e0d3bafd SD |
786 | |
787 | /* The only currently supported format is 16 bits/pixel */ | |
cde4362f MCC |
788 | buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth |
789 | + 7) >> 3; | |
84b5dbf3 | 790 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
e0d3bafd SD |
791 | return -EINVAL; |
792 | ||
84b5dbf3 | 793 | buf->vb.width = dev->width; |
e0d3bafd | 794 | buf->vb.height = dev->height; |
84b5dbf3 | 795 | buf->vb.field = field; |
e0d3bafd SD |
796 | |
797 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { | |
798 | rc = videobuf_iolock(vq, &buf->vb, NULL); | |
799 | if (rc < 0) | |
800 | goto fail; | |
801 | } | |
802 | ||
64fbf444 PB |
803 | if (dev->USE_ISO) { |
804 | if (!dev->video_mode.isoc_ctl.num_bufs) | |
805 | urb_init = 1; | |
806 | } else { | |
807 | if (!dev->video_mode.bulk_ctl.num_bufs) | |
808 | urb_init = 1; | |
809 | } | |
336fea92 | 810 | dev_dbg(dev->dev, |
b7085c08 MCC |
811 | "urb_init=%d dev->video_mode.max_pkt_size=%d\n", |
812 | urb_init, dev->video_mode.max_pkt_size); | |
e0d3bafd | 813 | if (urb_init) { |
64fbf444 PB |
814 | dev->mode_tv = 0; |
815 | if (dev->USE_ISO) | |
816 | rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS, | |
84b5dbf3 MCC |
817 | CX231XX_NUM_BUFS, |
818 | dev->video_mode.max_pkt_size, | |
819 | cx231xx_isoc_copy); | |
64fbf444 PB |
820 | else |
821 | rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS, | |
822 | CX231XX_NUM_BUFS, | |
823 | dev->video_mode.max_pkt_size, | |
824 | cx231xx_bulk_copy); | |
e0d3bafd SD |
825 | if (rc < 0) |
826 | goto fail; | |
827 | } | |
828 | ||
829 | buf->vb.state = VIDEOBUF_PREPARED; | |
0f0fa90b | 830 | |
e0d3bafd SD |
831 | return 0; |
832 | ||
cde4362f | 833 | fail: |
e0d3bafd SD |
834 | free_buffer(vq, buf); |
835 | return rc; | |
836 | } | |
837 | ||
84b5dbf3 | 838 | static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
e0d3bafd | 839 | { |
84b5dbf3 MCC |
840 | struct cx231xx_buffer *buf = |
841 | container_of(vb, struct cx231xx_buffer, vb); | |
842 | struct cx231xx_fh *fh = vq->priv_data; | |
843 | struct cx231xx *dev = fh->dev; | |
844 | struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq; | |
e0d3bafd SD |
845 | |
846 | buf->vb.state = VIDEOBUF_QUEUED; | |
847 | list_add_tail(&buf->vb.queue, &vidq->active); | |
848 | ||
849 | } | |
850 | ||
851 | static void buffer_release(struct videobuf_queue *vq, | |
84b5dbf3 | 852 | struct videobuf_buffer *vb) |
e0d3bafd | 853 | { |
84b5dbf3 MCC |
854 | struct cx231xx_buffer *buf = |
855 | container_of(vb, struct cx231xx_buffer, vb); | |
856 | struct cx231xx_fh *fh = vq->priv_data; | |
857 | struct cx231xx *dev = (struct cx231xx *)fh->dev; | |
e0d3bafd SD |
858 | |
859 | cx231xx_isocdbg("cx231xx: called buffer_release\n"); | |
860 | ||
861 | free_buffer(vq, buf); | |
862 | } | |
863 | ||
864 | static struct videobuf_queue_ops cx231xx_video_qops = { | |
84b5dbf3 MCC |
865 | .buf_setup = buffer_setup, |
866 | .buf_prepare = buffer_prepare, | |
867 | .buf_queue = buffer_queue, | |
868 | .buf_release = buffer_release, | |
e0d3bafd SD |
869 | }; |
870 | ||
871 | /********************* v4l2 interface **************************************/ | |
872 | ||
e0d3bafd SD |
873 | void video_mux(struct cx231xx *dev, int index) |
874 | { | |
e0d3bafd SD |
875 | dev->video_input = index; |
876 | dev->ctl_ainput = INPUT(index)->amux; | |
877 | ||
84b5dbf3 | 878 | cx231xx_set_video_input_mux(dev, index); |
e0d3bafd | 879 | |
5325b427 | 880 | cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0); |
e0d3bafd | 881 | |
84b5dbf3 | 882 | cx231xx_set_audio_input(dev, dev->ctl_ainput); |
e0d3bafd | 883 | |
336fea92 | 884 | dev_dbg(dev->dev, "video_mux : %d\n", index); |
e0d3bafd | 885 | |
84b5dbf3 MCC |
886 | /* do mode control overrides if required */ |
887 | cx231xx_do_mode_ctrl_overrides(dev); | |
e0d3bafd SD |
888 | } |
889 | ||
890 | /* Usage lock check functions */ | |
891 | static int res_get(struct cx231xx_fh *fh) | |
892 | { | |
84b5dbf3 MCC |
893 | struct cx231xx *dev = fh->dev; |
894 | int rc = 0; | |
e0d3bafd SD |
895 | |
896 | /* This instance already has stream_on */ | |
897 | if (fh->stream_on) | |
898 | return rc; | |
899 | ||
84b5dbf3 MCC |
900 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
901 | if (dev->stream_on) | |
902 | return -EBUSY; | |
903 | dev->stream_on = 1; | |
904 | } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { | |
905 | if (dev->vbi_stream_on) | |
906 | return -EBUSY; | |
907 | dev->vbi_stream_on = 1; | |
908 | } else | |
909 | return -EINVAL; | |
e0d3bafd | 910 | |
84b5dbf3 | 911 | fh->stream_on = 1; |
e0d3bafd SD |
912 | |
913 | return rc; | |
914 | } | |
915 | ||
916 | static int res_check(struct cx231xx_fh *fh) | |
917 | { | |
cde4362f | 918 | return fh->stream_on; |
e0d3bafd SD |
919 | } |
920 | ||
921 | static void res_free(struct cx231xx_fh *fh) | |
922 | { | |
84b5dbf3 | 923 | struct cx231xx *dev = fh->dev; |
e0d3bafd SD |
924 | |
925 | fh->stream_on = 0; | |
926 | ||
84b5dbf3 MCC |
927 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
928 | dev->stream_on = 0; | |
929 | if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) | |
930 | dev->vbi_stream_on = 0; | |
e0d3bafd SD |
931 | } |
932 | ||
933 | static int check_dev(struct cx231xx *dev) | |
934 | { | |
935 | if (dev->state & DEV_DISCONNECTED) { | |
336fea92 | 936 | dev_err(dev->dev, "v4l2 ioctl: device not present\n"); |
e0d3bafd SD |
937 | return -ENODEV; |
938 | } | |
e0d3bafd SD |
939 | return 0; |
940 | } | |
941 | ||
e0d3bafd SD |
942 | /* ------------------------------------------------------------------ |
943 | IOCTL vidioc handling | |
944 | ------------------------------------------------------------------*/ | |
945 | ||
946 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | |
84b5dbf3 | 947 | struct v4l2_format *f) |
e0d3bafd | 948 | { |
84b5dbf3 MCC |
949 | struct cx231xx_fh *fh = priv; |
950 | struct cx231xx *dev = fh->dev; | |
e0d3bafd | 951 | |
e0d3bafd SD |
952 | f->fmt.pix.width = dev->width; |
953 | f->fmt.pix.height = dev->height; | |
1ebcad77 JP |
954 | f->fmt.pix.pixelformat = dev->format->fourcc; |
955 | f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3; | |
84b5dbf3 | 956 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; |
e0d3bafd SD |
957 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
958 | ||
959 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
960 | ||
e0d3bafd SD |
961 | return 0; |
962 | } | |
963 | ||
964 | static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc) | |
965 | { | |
966 | unsigned int i; | |
967 | ||
968 | for (i = 0; i < ARRAY_SIZE(format); i++) | |
969 | if (format[i].fourcc == fourcc) | |
970 | return &format[i]; | |
971 | ||
972 | return NULL; | |
973 | } | |
974 | ||
975 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | |
84b5dbf3 | 976 | struct v4l2_format *f) |
e0d3bafd | 977 | { |
84b5dbf3 MCC |
978 | struct cx231xx_fh *fh = priv; |
979 | struct cx231xx *dev = fh->dev; | |
9bd0e8d7 TP |
980 | unsigned int width = f->fmt.pix.width; |
981 | unsigned int height = f->fmt.pix.height; | |
84b5dbf3 MCC |
982 | unsigned int maxw = norm_maxw(dev); |
983 | unsigned int maxh = norm_maxh(dev); | |
84b5dbf3 | 984 | struct cx231xx_fmt *fmt; |
e0d3bafd SD |
985 | |
986 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
987 | if (!fmt) { | |
988 | cx231xx_videodbg("Fourcc format (%08x) invalid.\n", | |
84b5dbf3 | 989 | f->fmt.pix.pixelformat); |
e0d3bafd SD |
990 | return -EINVAL; |
991 | } | |
992 | ||
993 | /* width must even because of the YUYV format | |
994 | height must be even because of interlacing */ | |
9bd0e8d7 | 995 | v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0); |
e0d3bafd | 996 | |
e0d3bafd SD |
997 | f->fmt.pix.width = width; |
998 | f->fmt.pix.height = height; | |
999 | f->fmt.pix.pixelformat = fmt->fourcc; | |
8b735c13 | 1000 | f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3; |
e0d3bafd SD |
1001 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; |
1002 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
1003 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | |
84b5dbf3 | 1009 | struct v4l2_format *f) |
e0d3bafd | 1010 | { |
84b5dbf3 MCC |
1011 | struct cx231xx_fh *fh = priv; |
1012 | struct cx231xx *dev = fh->dev; | |
1013 | int rc; | |
1014 | struct cx231xx_fmt *fmt; | |
ebf984bb HV |
1015 | struct v4l2_subdev_format format = { |
1016 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1017 | }; | |
e0d3bafd SD |
1018 | |
1019 | rc = check_dev(dev); | |
1020 | if (rc < 0) | |
1021 | return rc; | |
1022 | ||
84b5dbf3 | 1023 | vidioc_try_fmt_vid_cap(file, priv, f); |
e0d3bafd | 1024 | |
84b5dbf3 | 1025 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
643800d5 MCC |
1026 | if (!fmt) |
1027 | return -EINVAL; | |
e0d3bafd SD |
1028 | |
1029 | if (videobuf_queue_is_busy(&fh->vb_vidq)) { | |
336fea92 | 1030 | dev_err(dev->dev, "%s: queue busy\n", __func__); |
643800d5 | 1031 | return -EBUSY; |
e0d3bafd SD |
1032 | } |
1033 | ||
1034 | if (dev->stream_on && !fh->stream_on) { | |
336fea92 | 1035 | dev_err(dev->dev, |
b7085c08 | 1036 | "%s: device in use by another fh\n", __func__); |
643800d5 | 1037 | return -EBUSY; |
e0d3bafd SD |
1038 | } |
1039 | ||
1040 | /* set new image size */ | |
1041 | dev->width = f->fmt.pix.width; | |
1042 | dev->height = f->fmt.pix.height; | |
84b5dbf3 | 1043 | dev->format = fmt; |
e0d3bafd | 1044 | |
ebf984bb HV |
1045 | v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED); |
1046 | call_all(dev, pad, set_fmt, NULL, &format); | |
1047 | v4l2_fill_pix_format(&f->fmt.pix, &format.format); | |
e0d3bafd | 1048 | |
e0d3bafd SD |
1049 | return rc; |
1050 | } | |
1051 | ||
64fbf444 | 1052 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id) |
e0d3bafd | 1053 | { |
84b5dbf3 MCC |
1054 | struct cx231xx_fh *fh = priv; |
1055 | struct cx231xx *dev = fh->dev; | |
e0d3bafd SD |
1056 | |
1057 | *id = dev->norm; | |
1058 | return 0; | |
1059 | } | |
1060 | ||
314527ac | 1061 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) |
e0d3bafd | 1062 | { |
84b5dbf3 MCC |
1063 | struct cx231xx_fh *fh = priv; |
1064 | struct cx231xx *dev = fh->dev; | |
ebf984bb HV |
1065 | struct v4l2_subdev_format format = { |
1066 | .which = V4L2_SUBDEV_FORMAT_ACTIVE, | |
1067 | }; | |
84b5dbf3 | 1068 | int rc; |
e0d3bafd SD |
1069 | |
1070 | rc = check_dev(dev); | |
1071 | if (rc < 0) | |
1072 | return rc; | |
1073 | ||
314527ac | 1074 | if (dev->norm == norm) |
d61072a4 HV |
1075 | return 0; |
1076 | ||
1077 | if (videobuf_queue_is_busy(&fh->vb_vidq)) | |
1078 | return -EBUSY; | |
e0d3bafd | 1079 | |
314527ac | 1080 | dev->norm = norm; |
e0d3bafd | 1081 | |
e0d3bafd | 1082 | /* Adjusts width/height, if needed */ |
d61072a4 HV |
1083 | dev->width = 720; |
1084 | dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480; | |
e0d3bafd | 1085 | |
8774bed9 | 1086 | call_all(dev, video, s_std, dev->norm); |
435b4f78 DH |
1087 | |
1088 | /* We need to reset basic properties in the decoder related to | |
1089 | resolution (since a standard change effects things like the number | |
1090 | of lines in VACT, etc) */ | |
ebf984bb HV |
1091 | format.format.code = MEDIA_BUS_FMT_FIXED; |
1092 | format.format.width = dev->width; | |
1093 | format.format.height = dev->height; | |
1094 | call_all(dev, pad, set_fmt, NULL, &format); | |
e0d3bafd | 1095 | |
84b5dbf3 MCC |
1096 | /* do mode control overrides */ |
1097 | cx231xx_do_mode_ctrl_overrides(dev); | |
e0d3bafd SD |
1098 | |
1099 | return 0; | |
1100 | } | |
1101 | ||
1102 | static const char *iname[] = { | |
1103 | [CX231XX_VMUX_COMPOSITE1] = "Composite1", | |
cde4362f | 1104 | [CX231XX_VMUX_SVIDEO] = "S-Video", |
e0d3bafd | 1105 | [CX231XX_VMUX_TELEVISION] = "Television", |
cde4362f MCC |
1106 | [CX231XX_VMUX_CABLE] = "Cable TV", |
1107 | [CX231XX_VMUX_DVB] = "DVB", | |
1108 | [CX231XX_VMUX_DEBUG] = "for debug only", | |
e0d3bafd SD |
1109 | }; |
1110 | ||
b86d1544 | 1111 | int cx231xx_enum_input(struct file *file, void *priv, |
84b5dbf3 | 1112 | struct v4l2_input *i) |
e0d3bafd | 1113 | { |
84b5dbf3 MCC |
1114 | struct cx231xx_fh *fh = priv; |
1115 | struct cx231xx *dev = fh->dev; | |
de99d532 DH |
1116 | u32 gen_stat; |
1117 | unsigned int ret, n; | |
e0d3bafd SD |
1118 | |
1119 | n = i->index; | |
1120 | if (n >= MAX_CX231XX_INPUT) | |
1121 | return -EINVAL; | |
1122 | if (0 == INPUT(n)->type) | |
1123 | return -EINVAL; | |
1124 | ||
1125 | i->index = n; | |
1126 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
1127 | ||
1128 | strcpy(i->name, iname[INPUT(n)->type]); | |
1129 | ||
1130 | if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) || | |
84b5dbf3 | 1131 | (CX231XX_VMUX_CABLE == INPUT(n)->type)) |
e0d3bafd SD |
1132 | i->type = V4L2_INPUT_TYPE_TUNER; |
1133 | ||
60acf187 | 1134 | i->std = dev->vdev.tvnorms; |
e0d3bafd | 1135 | |
de99d532 DH |
1136 | /* If they are asking about the active input, read signal status */ |
1137 | if (n == dev->video_input) { | |
1138 | ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS, | |
1139 | GEN_STAT, 2, &gen_stat, 4); | |
1140 | if (ret > 0) { | |
1141 | if ((gen_stat & FLD_VPRES) == 0x00) | |
1142 | i->status |= V4L2_IN_ST_NO_SIGNAL; | |
1143 | if ((gen_stat & FLD_HLOCK) == 0x00) | |
1144 | i->status |= V4L2_IN_ST_NO_H_LOCK; | |
1145 | } | |
1146 | } | |
1147 | ||
e0d3bafd SD |
1148 | return 0; |
1149 | } | |
1150 | ||
b86d1544 | 1151 | int cx231xx_g_input(struct file *file, void *priv, unsigned int *i) |
e0d3bafd | 1152 | { |
84b5dbf3 MCC |
1153 | struct cx231xx_fh *fh = priv; |
1154 | struct cx231xx *dev = fh->dev; | |
e0d3bafd SD |
1155 | |
1156 | *i = dev->video_input; | |
1157 | ||
1158 | return 0; | |
1159 | } | |
1160 | ||
b86d1544 | 1161 | int cx231xx_s_input(struct file *file, void *priv, unsigned int i) |
e0d3bafd | 1162 | { |
84b5dbf3 MCC |
1163 | struct cx231xx_fh *fh = priv; |
1164 | struct cx231xx *dev = fh->dev; | |
1165 | int rc; | |
e0d3bafd | 1166 | |
64fbf444 | 1167 | dev->mode_tv = 0; |
e0d3bafd SD |
1168 | rc = check_dev(dev); |
1169 | if (rc < 0) | |
1170 | return rc; | |
1171 | ||
1172 | if (i >= MAX_CX231XX_INPUT) | |
1173 | return -EINVAL; | |
1174 | if (0 == INPUT(i)->type) | |
1175 | return -EINVAL; | |
1176 | ||
e0d3bafd SD |
1177 | video_mux(dev, i); |
1178 | ||
c09d6695 DH |
1179 | if (INPUT(i)->type == CX231XX_VMUX_TELEVISION || |
1180 | INPUT(i)->type == CX231XX_VMUX_CABLE) { | |
1181 | /* There's a tuner, so reset the standard and put it on the | |
1182 | last known frequency (since it was probably powered down | |
1183 | until now */ | |
8774bed9 | 1184 | call_all(dev, video, s_std, dev->norm); |
c09d6695 DH |
1185 | } |
1186 | ||
e0d3bafd SD |
1187 | return 0; |
1188 | } | |
1189 | ||
b86d1544 | 1190 | int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) |
e0d3bafd | 1191 | { |
84b5dbf3 MCC |
1192 | struct cx231xx_fh *fh = priv; |
1193 | struct cx231xx *dev = fh->dev; | |
1194 | int rc; | |
e0d3bafd SD |
1195 | |
1196 | rc = check_dev(dev); | |
1197 | if (rc < 0) | |
1198 | return rc; | |
1199 | ||
1200 | if (0 != t->index) | |
1201 | return -EINVAL; | |
1202 | ||
1203 | strcpy(t->name, "Tuner"); | |
1204 | ||
84b5dbf3 | 1205 | t->type = V4L2_TUNER_ANALOG_TV; |
e0d3bafd | 1206 | t->capability = V4L2_TUNER_CAP_NORM; |
84b5dbf3 MCC |
1207 | t->rangehigh = 0xffffffffUL; |
1208 | t->signal = 0xffff; /* LOCKED */ | |
b251f957 | 1209 | call_all(dev, tuner, g_tuner, t); |
e0d3bafd SD |
1210 | |
1211 | return 0; | |
1212 | } | |
1213 | ||
2f73c7c5 | 1214 | int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t) |
e0d3bafd | 1215 | { |
84b5dbf3 MCC |
1216 | struct cx231xx_fh *fh = priv; |
1217 | struct cx231xx *dev = fh->dev; | |
1218 | int rc; | |
e0d3bafd SD |
1219 | |
1220 | rc = check_dev(dev); | |
1221 | if (rc < 0) | |
1222 | return rc; | |
1223 | ||
1224 | if (0 != t->index) | |
1225 | return -EINVAL; | |
1226 | #if 0 | |
b1196126 | 1227 | call_all(dev, tuner, s_tuner, t); |
e0d3bafd SD |
1228 | #endif |
1229 | return 0; | |
1230 | } | |
1231 | ||
b86d1544 | 1232 | int cx231xx_g_frequency(struct file *file, void *priv, |
84b5dbf3 | 1233 | struct v4l2_frequency *f) |
e0d3bafd | 1234 | { |
84b5dbf3 MCC |
1235 | struct cx231xx_fh *fh = priv; |
1236 | struct cx231xx *dev = fh->dev; | |
e0d3bafd | 1237 | |
b251f957 HV |
1238 | if (f->tuner) |
1239 | return -EINVAL; | |
1240 | ||
e0d3bafd SD |
1241 | f->frequency = dev->ctl_freq; |
1242 | ||
e0d3bafd SD |
1243 | return 0; |
1244 | } | |
1245 | ||
b86d1544 | 1246 | int cx231xx_s_frequency(struct file *file, void *priv, |
b530a447 | 1247 | const struct v4l2_frequency *f) |
e0d3bafd | 1248 | { |
84b5dbf3 MCC |
1249 | struct cx231xx_fh *fh = priv; |
1250 | struct cx231xx *dev = fh->dev; | |
b530a447 | 1251 | struct v4l2_frequency new_freq = *f; |
84b5dbf3 | 1252 | int rc; |
64fbf444 PB |
1253 | u32 if_frequency = 5400000; |
1254 | ||
336fea92 | 1255 | dev_dbg(dev->dev, |
b7085c08 MCC |
1256 | "Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n", |
1257 | f->frequency, f->type); | |
e0d3bafd SD |
1258 | |
1259 | rc = check_dev(dev); | |
1260 | if (rc < 0) | |
1261 | return rc; | |
1262 | ||
1263 | if (0 != f->tuner) | |
1264 | return -EINVAL; | |
1265 | ||
84b5dbf3 MCC |
1266 | /* set pre channel change settings in DIF first */ |
1267 | rc = cx231xx_tuner_pre_channel_change(dev); | |
e0d3bafd | 1268 | |
64fbf444 | 1269 | call_all(dev, tuner, s_frequency, f); |
b530a447 HV |
1270 | call_all(dev, tuner, g_frequency, &new_freq); |
1271 | dev->ctl_freq = new_freq.frequency; | |
e0d3bafd | 1272 | |
84b5dbf3 MCC |
1273 | /* set post channel change settings in DIF first */ |
1274 | rc = cx231xx_tuner_post_channel_change(dev); | |
e0d3bafd | 1275 | |
64fbf444 PB |
1276 | if (dev->tuner_type == TUNER_NXP_TDA18271) { |
1277 | if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443)) | |
1278 | if_frequency = 5400000; /*5.4MHz */ | |
1279 | else if (dev->norm & V4L2_STD_B) | |
1280 | if_frequency = 6000000; /*6.0MHz */ | |
1281 | else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK)) | |
1282 | if_frequency = 6900000; /*6.9MHz */ | |
1283 | else if (dev->norm & V4L2_STD_GH) | |
1284 | if_frequency = 7100000; /*7.1MHz */ | |
1285 | else if (dev->norm & V4L2_STD_PAL_I) | |
1286 | if_frequency = 7250000; /*7.25MHz */ | |
1287 | else if (dev->norm & V4L2_STD_SECAM_L) | |
1288 | if_frequency = 6900000; /*6.9MHz */ | |
1289 | else if (dev->norm & V4L2_STD_SECAM_LC) | |
1290 | if_frequency = 1250000; /*1.25MHz */ | |
1291 | ||
336fea92 | 1292 | dev_dbg(dev->dev, |
b7085c08 | 1293 | "if_frequency is set to %d\n", if_frequency); |
64fbf444 PB |
1294 | cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1); |
1295 | ||
1296 | update_HH_register_after_set_DIF(dev); | |
1297 | } | |
1298 | ||
336fea92 | 1299 | dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency); |
e0d3bafd SD |
1300 | |
1301 | return rc; | |
1302 | } | |
1303 | ||
08fe9f7d HV |
1304 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
1305 | ||
1306 | int cx231xx_g_chip_info(struct file *file, void *fh, | |
1307 | struct v4l2_dbg_chip_info *chip) | |
fddd14c8 | 1308 | { |
08fe9f7d HV |
1309 | switch (chip->match.addr) { |
1310 | case 0: /* Cx231xx - internal registers */ | |
1311 | return 0; | |
1312 | case 1: /* AFE - read byte */ | |
1313 | strlcpy(chip->name, "AFE (byte)", sizeof(chip->name)); | |
1314 | return 0; | |
1315 | case 2: /* Video Block - read byte */ | |
1316 | strlcpy(chip->name, "Video (byte)", sizeof(chip->name)); | |
1317 | return 0; | |
1318 | case 3: /* I2S block - read byte */ | |
1319 | strlcpy(chip->name, "I2S (byte)", sizeof(chip->name)); | |
1320 | return 0; | |
1321 | case 4: /* AFE - read dword */ | |
1322 | strlcpy(chip->name, "AFE (dword)", sizeof(chip->name)); | |
1323 | return 0; | |
1324 | case 5: /* Video Block - read dword */ | |
1325 | strlcpy(chip->name, "Video (dword)", sizeof(chip->name)); | |
1326 | return 0; | |
1327 | case 6: /* I2S Block - read dword */ | |
1328 | strlcpy(chip->name, "I2S (dword)", sizeof(chip->name)); | |
fddd14c8 HV |
1329 | return 0; |
1330 | } | |
1331 | return -EINVAL; | |
1332 | } | |
1333 | ||
b86d1544 | 1334 | int cx231xx_g_register(struct file *file, void *priv, |
e0d3bafd SD |
1335 | struct v4l2_dbg_register *reg) |
1336 | { | |
84b5dbf3 MCC |
1337 | struct cx231xx_fh *fh = priv; |
1338 | struct cx231xx *dev = fh->dev; | |
08fe9f7d | 1339 | int ret; |
84b5dbf3 MCC |
1340 | u8 value[4] = { 0, 0, 0, 0 }; |
1341 | u32 data = 0; | |
1342 | ||
08fe9f7d HV |
1343 | switch (reg->match.addr) { |
1344 | case 0: /* Cx231xx - internal registers */ | |
1345 | ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER, | |
1346 | (u16)reg->reg, value, 4); | |
1347 | reg->val = value[0] | value[1] << 8 | | |
1348 | value[2] << 16 | value[3] << 24; | |
04ae4cf2 | 1349 | reg->size = 4; |
08fe9f7d HV |
1350 | break; |
1351 | case 1: /* AFE - read byte */ | |
1352 | ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS, | |
1353 | (u16)reg->reg, 2, &data, 1); | |
1354 | reg->val = data; | |
04ae4cf2 | 1355 | reg->size = 1; |
08fe9f7d HV |
1356 | break; |
1357 | case 2: /* Video Block - read byte */ | |
1358 | ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS, | |
1359 | (u16)reg->reg, 2, &data, 1); | |
1360 | reg->val = data; | |
04ae4cf2 | 1361 | reg->size = 1; |
08fe9f7d HV |
1362 | break; |
1363 | case 3: /* I2S block - read byte */ | |
1364 | ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS, | |
1365 | (u16)reg->reg, 1, &data, 1); | |
1366 | reg->val = data; | |
04ae4cf2 | 1367 | reg->size = 1; |
08fe9f7d HV |
1368 | break; |
1369 | case 4: /* AFE - read dword */ | |
1370 | ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS, | |
1371 | (u16)reg->reg, 2, &data, 4); | |
1372 | reg->val = data; | |
04ae4cf2 | 1373 | reg->size = 4; |
08fe9f7d HV |
1374 | break; |
1375 | case 5: /* Video Block - read dword */ | |
1376 | ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS, | |
1377 | (u16)reg->reg, 2, &data, 4); | |
1378 | reg->val = data; | |
04ae4cf2 | 1379 | reg->size = 4; |
08fe9f7d HV |
1380 | break; |
1381 | case 6: /* I2S Block - read dword */ | |
1382 | ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS, | |
1383 | (u16)reg->reg, 1, &data, 4); | |
1384 | reg->val = data; | |
04ae4cf2 | 1385 | reg->size = 4; |
08fe9f7d | 1386 | break; |
84b5dbf3 | 1387 | default: |
08fe9f7d | 1388 | return -EINVAL; |
84b5dbf3 | 1389 | } |
08fe9f7d | 1390 | return ret < 0 ? ret : 0; |
e0d3bafd SD |
1391 | } |
1392 | ||
b86d1544 | 1393 | int cx231xx_s_register(struct file *file, void *priv, |
977ba3b1 | 1394 | const struct v4l2_dbg_register *reg) |
e0d3bafd | 1395 | { |
84b5dbf3 MCC |
1396 | struct cx231xx_fh *fh = priv; |
1397 | struct cx231xx *dev = fh->dev; | |
08fe9f7d | 1398 | int ret; |
84b5dbf3 | 1399 | u8 data[4] = { 0, 0, 0, 0 }; |
e0d3bafd | 1400 | |
08fe9f7d HV |
1401 | switch (reg->match.addr) { |
1402 | case 0: /* cx231xx internal registers */ | |
1403 | data[0] = (u8) reg->val; | |
1404 | data[1] = (u8) (reg->val >> 8); | |
1405 | data[2] = (u8) (reg->val >> 16); | |
1406 | data[3] = (u8) (reg->val >> 24); | |
1407 | ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER, | |
1408 | (u16)reg->reg, data, 4); | |
1409 | break; | |
1410 | case 1: /* AFE - write byte */ | |
1411 | ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS, | |
1412 | (u16)reg->reg, 2, reg->val, 1); | |
1413 | break; | |
1414 | case 2: /* Video Block - write byte */ | |
1415 | ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS, | |
1416 | (u16)reg->reg, 2, reg->val, 1); | |
1417 | break; | |
1418 | case 3: /* I2S block - write byte */ | |
1419 | ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS, | |
1420 | (u16)reg->reg, 1, reg->val, 1); | |
1421 | break; | |
1422 | case 4: /* AFE - write dword */ | |
1423 | ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS, | |
1424 | (u16)reg->reg, 2, reg->val, 4); | |
1425 | break; | |
1426 | case 5: /* Video Block - write dword */ | |
1427 | ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS, | |
1428 | (u16)reg->reg, 2, reg->val, 4); | |
84b5dbf3 | 1429 | break; |
08fe9f7d HV |
1430 | case 6: /* I2S block - write dword */ |
1431 | ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS, | |
1432 | (u16)reg->reg, 1, reg->val, 4); | |
1433 | break; | |
1434 | default: | |
1435 | return -EINVAL; | |
84b5dbf3 | 1436 | } |
08fe9f7d | 1437 | return ret < 0 ? ret : 0; |
e0d3bafd SD |
1438 | } |
1439 | #endif | |
1440 | ||
e0d3bafd | 1441 | static int vidioc_cropcap(struct file *file, void *priv, |
84b5dbf3 | 1442 | struct v4l2_cropcap *cc) |
e0d3bafd | 1443 | { |
84b5dbf3 MCC |
1444 | struct cx231xx_fh *fh = priv; |
1445 | struct cx231xx *dev = fh->dev; | |
e0d3bafd SD |
1446 | |
1447 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
1448 | return -EINVAL; | |
1449 | ||
1450 | cc->bounds.left = 0; | |
1451 | cc->bounds.top = 0; | |
1452 | cc->bounds.width = dev->width; | |
1453 | cc->bounds.height = dev->height; | |
1454 | cc->defrect = cc->bounds; | |
1455 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ | |
1456 | cc->pixelaspect.denominator = 59; | |
1457 | ||
1458 | return 0; | |
1459 | } | |
1460 | ||
1461 | static int vidioc_streamon(struct file *file, void *priv, | |
84b5dbf3 | 1462 | enum v4l2_buf_type type) |
e0d3bafd | 1463 | { |
84b5dbf3 MCC |
1464 | struct cx231xx_fh *fh = priv; |
1465 | struct cx231xx *dev = fh->dev; | |
1466 | int rc; | |
e0d3bafd SD |
1467 | |
1468 | rc = check_dev(dev); | |
1469 | if (rc < 0) | |
1470 | return rc; | |
1471 | ||
e0d3bafd SD |
1472 | rc = res_get(fh); |
1473 | ||
1474 | if (likely(rc >= 0)) | |
1475 | rc = videobuf_streamon(&fh->vb_vidq); | |
1476 | ||
b1196126 SD |
1477 | call_all(dev, video, s_stream, 1); |
1478 | ||
e0d3bafd SD |
1479 | return rc; |
1480 | } | |
1481 | ||
1482 | static int vidioc_streamoff(struct file *file, void *priv, | |
84b5dbf3 | 1483 | enum v4l2_buf_type type) |
e0d3bafd | 1484 | { |
84b5dbf3 MCC |
1485 | struct cx231xx_fh *fh = priv; |
1486 | struct cx231xx *dev = fh->dev; | |
1487 | int rc; | |
e0d3bafd SD |
1488 | |
1489 | rc = check_dev(dev); | |
1490 | if (rc < 0) | |
1491 | return rc; | |
1492 | ||
e0d3bafd SD |
1493 | if (type != fh->type) |
1494 | return -EINVAL; | |
1495 | ||
b1196126 SD |
1496 | cx25840_call(dev, video, s_stream, 0); |
1497 | ||
e0d3bafd SD |
1498 | videobuf_streamoff(&fh->vb_vidq); |
1499 | res_free(fh); | |
1500 | ||
e0d3bafd SD |
1501 | return 0; |
1502 | } | |
1503 | ||
bc08734c | 1504 | int cx231xx_querycap(struct file *file, void *priv, |
84b5dbf3 | 1505 | struct v4l2_capability *cap) |
e0d3bafd | 1506 | { |
4bc837d4 | 1507 | struct video_device *vdev = video_devdata(file); |
84b5dbf3 MCC |
1508 | struct cx231xx_fh *fh = priv; |
1509 | struct cx231xx *dev = fh->dev; | |
e0d3bafd SD |
1510 | |
1511 | strlcpy(cap->driver, "cx231xx", sizeof(cap->driver)); | |
1512 | strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card)); | |
2c6beca8 | 1513 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
e0d3bafd | 1514 | |
530e01e7 HV |
1515 | if (vdev->vfl_type == VFL_TYPE_RADIO) |
1516 | cap->device_caps = V4L2_CAP_RADIO; | |
1517 | else { | |
06c46003 | 1518 | cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
530e01e7 HV |
1519 | if (vdev->vfl_type == VFL_TYPE_VBI) |
1520 | cap->device_caps |= V4L2_CAP_VBI_CAPTURE; | |
1521 | else | |
1522 | cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE; | |
1523 | } | |
e0d3bafd | 1524 | if (dev->tuner_type != TUNER_ABSENT) |
4bc837d4 | 1525 | cap->device_caps |= V4L2_CAP_TUNER; |
06c46003 | 1526 | cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE | |
4bc837d4 | 1527 | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE | |
530e01e7 | 1528 | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS; |
60acf187 | 1529 | if (video_is_registered(&dev->radio_dev)) |
530e01e7 | 1530 | cap->capabilities |= V4L2_CAP_RADIO; |
e0d3bafd SD |
1531 | |
1532 | return 0; | |
1533 | } | |
1534 | ||
84b5dbf3 MCC |
1535 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
1536 | struct v4l2_fmtdesc *f) | |
e0d3bafd | 1537 | { |
84b5dbf3 | 1538 | if (unlikely(f->index >= ARRAY_SIZE(format))) |
e0d3bafd SD |
1539 | return -EINVAL; |
1540 | ||
1541 | strlcpy(f->description, format[f->index].name, sizeof(f->description)); | |
1542 | f->pixelformat = format[f->index].fourcc; | |
1543 | ||
1544 | return 0; | |
1545 | } | |
1546 | ||
e0d3bafd SD |
1547 | /* RAW VBI ioctls */ |
1548 | ||
1549 | static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, | |
84b5dbf3 | 1550 | struct v4l2_format *f) |
e0d3bafd | 1551 | { |
84b5dbf3 MCC |
1552 | struct cx231xx_fh *fh = priv; |
1553 | struct cx231xx *dev = fh->dev; | |
6264722c | 1554 | |
adc0356e | 1555 | f->fmt.vbi.sampling_rate = 6750000 * 4; |
e0d3bafd SD |
1556 | f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH; |
1557 | f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; | |
adc0356e | 1558 | f->fmt.vbi.offset = 0; |
e0d3bafd | 1559 | f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ? |
84b5dbf3 | 1560 | PAL_VBI_START_LINE : NTSC_VBI_START_LINE; |
e0d3bafd | 1561 | f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ? |
84b5dbf3 | 1562 | PAL_VBI_LINES : NTSC_VBI_LINES; |
e0d3bafd | 1563 | f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ? |
84b5dbf3 | 1564 | PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263; |
e0d3bafd | 1565 | f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; |
6264722c | 1566 | memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved)); |
e0d3bafd SD |
1567 | |
1568 | return 0; | |
1569 | ||
1570 | } | |
1571 | ||
1572 | static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv, | |
84b5dbf3 | 1573 | struct v4l2_format *f) |
e0d3bafd | 1574 | { |
84b5dbf3 MCC |
1575 | struct cx231xx_fh *fh = priv; |
1576 | struct cx231xx *dev = fh->dev; | |
e0d3bafd | 1577 | |
adc0356e | 1578 | f->fmt.vbi.sampling_rate = 6750000 * 4; |
e0d3bafd SD |
1579 | f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH; |
1580 | f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; | |
adc0356e | 1581 | f->fmt.vbi.offset = 0; |
e0d3bafd SD |
1582 | f->fmt.vbi.flags = 0; |
1583 | f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ? | |
84b5dbf3 | 1584 | PAL_VBI_START_LINE : NTSC_VBI_START_LINE; |
e0d3bafd | 1585 | f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ? |
84b5dbf3 | 1586 | PAL_VBI_LINES : NTSC_VBI_LINES; |
e0d3bafd | 1587 | f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ? |
84b5dbf3 | 1588 | PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263; |
e0d3bafd | 1589 | f->fmt.vbi.count[1] = f->fmt.vbi.count[0]; |
6264722c | 1590 | memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved)); |
e0d3bafd SD |
1591 | |
1592 | return 0; | |
1593 | ||
1594 | } | |
1595 | ||
6264722c HV |
1596 | static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv, |
1597 | struct v4l2_format *f) | |
1598 | { | |
1599 | struct cx231xx_fh *fh = priv; | |
1600 | struct cx231xx *dev = fh->dev; | |
1601 | ||
1602 | if (dev->vbi_stream_on && !fh->stream_on) { | |
336fea92 | 1603 | dev_err(dev->dev, |
b7085c08 | 1604 | "%s device in use by another fh\n", __func__); |
6264722c HV |
1605 | return -EBUSY; |
1606 | } | |
1607 | return vidioc_try_fmt_vbi_cap(file, priv, f); | |
1608 | } | |
1609 | ||
e0d3bafd SD |
1610 | static int vidioc_reqbufs(struct file *file, void *priv, |
1611 | struct v4l2_requestbuffers *rb) | |
1612 | { | |
84b5dbf3 MCC |
1613 | struct cx231xx_fh *fh = priv; |
1614 | struct cx231xx *dev = fh->dev; | |
1615 | int rc; | |
e0d3bafd SD |
1616 | |
1617 | rc = check_dev(dev); | |
1618 | if (rc < 0) | |
1619 | return rc; | |
1620 | ||
cde4362f | 1621 | return videobuf_reqbufs(&fh->vb_vidq, rb); |
e0d3bafd SD |
1622 | } |
1623 | ||
84b5dbf3 | 1624 | static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b) |
e0d3bafd | 1625 | { |
84b5dbf3 MCC |
1626 | struct cx231xx_fh *fh = priv; |
1627 | struct cx231xx *dev = fh->dev; | |
1628 | int rc; | |
e0d3bafd SD |
1629 | |
1630 | rc = check_dev(dev); | |
1631 | if (rc < 0) | |
1632 | return rc; | |
1633 | ||
cde4362f | 1634 | return videobuf_querybuf(&fh->vb_vidq, b); |
e0d3bafd SD |
1635 | } |
1636 | ||
1637 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) | |
1638 | { | |
84b5dbf3 MCC |
1639 | struct cx231xx_fh *fh = priv; |
1640 | struct cx231xx *dev = fh->dev; | |
1641 | int rc; | |
e0d3bafd SD |
1642 | |
1643 | rc = check_dev(dev); | |
1644 | if (rc < 0) | |
1645 | return rc; | |
1646 | ||
cde4362f | 1647 | return videobuf_qbuf(&fh->vb_vidq, b); |
e0d3bafd SD |
1648 | } |
1649 | ||
1650 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) | |
1651 | { | |
84b5dbf3 MCC |
1652 | struct cx231xx_fh *fh = priv; |
1653 | struct cx231xx *dev = fh->dev; | |
1654 | int rc; | |
e0d3bafd SD |
1655 | |
1656 | rc = check_dev(dev); | |
1657 | if (rc < 0) | |
1658 | return rc; | |
1659 | ||
cde4362f | 1660 | return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK); |
e0d3bafd SD |
1661 | } |
1662 | ||
e0d3bafd SD |
1663 | /* ----------------------------------------------------------- */ |
1664 | /* RADIO ESPECIFIC IOCTLS */ | |
1665 | /* ----------------------------------------------------------- */ | |
1666 | ||
84b5dbf3 | 1667 | static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) |
e0d3bafd SD |
1668 | { |
1669 | struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev; | |
1670 | ||
530e01e7 | 1671 | if (t->index) |
e0d3bafd SD |
1672 | return -EINVAL; |
1673 | ||
1674 | strcpy(t->name, "Radio"); | |
e0d3bafd | 1675 | |
530e01e7 | 1676 | call_all(dev, tuner, g_tuner, t); |
e0d3bafd | 1677 | |
e0d3bafd SD |
1678 | return 0; |
1679 | } | |
2f73c7c5 | 1680 | static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t) |
e0d3bafd SD |
1681 | { |
1682 | struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev; | |
1683 | ||
b86d1544 | 1684 | if (t->index) |
e0d3bafd SD |
1685 | return -EINVAL; |
1686 | ||
b1196126 | 1687 | call_all(dev, tuner, s_tuner, t); |
e0d3bafd SD |
1688 | |
1689 | return 0; | |
1690 | } | |
1691 | ||
e0d3bafd SD |
1692 | /* |
1693 | * cx231xx_v4l2_open() | |
1694 | * inits the device and starts isoc transfer | |
1695 | */ | |
1696 | static int cx231xx_v4l2_open(struct file *filp) | |
1697 | { | |
4df16f70 | 1698 | int radio = 0; |
63b0d5ad LP |
1699 | struct video_device *vdev = video_devdata(filp); |
1700 | struct cx231xx *dev = video_drvdata(filp); | |
e0d3bafd SD |
1701 | struct cx231xx_fh *fh; |
1702 | enum v4l2_buf_type fh_type = 0; | |
1703 | ||
63b0d5ad LP |
1704 | switch (vdev->vfl_type) { |
1705 | case VFL_TYPE_GRABBER: | |
1706 | fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1707 | break; | |
1708 | case VFL_TYPE_VBI: | |
1709 | fh_type = V4L2_BUF_TYPE_VBI_CAPTURE; | |
1710 | break; | |
1711 | case VFL_TYPE_RADIO: | |
1712 | radio = 1; | |
1713 | break; | |
1714 | } | |
e0d3bafd | 1715 | |
50462eb0 LP |
1716 | cx231xx_videodbg("open dev=%s type=%s users=%d\n", |
1717 | video_device_node_name(vdev), v4l2_type_names[fh_type], | |
1718 | dev->users); | |
e0d3bafd SD |
1719 | |
1720 | #if 0 | |
1721 | errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE); | |
1722 | if (errCode < 0) { | |
336fea92 | 1723 | dev_err(dev->dev, |
b7085c08 | 1724 | "Device locked on digital mode. Can't open analog\n"); |
e0d3bafd SD |
1725 | return -EBUSY; |
1726 | } | |
1727 | #endif | |
1728 | ||
1729 | fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL); | |
b7085c08 | 1730 | if (!fh) |
e0d3bafd | 1731 | return -ENOMEM; |
1f7e073d HV |
1732 | if (mutex_lock_interruptible(&dev->lock)) { |
1733 | kfree(fh); | |
1734 | return -ERESTARTSYS; | |
1735 | } | |
e0d3bafd | 1736 | fh->dev = dev; |
e0d3bafd SD |
1737 | fh->type = fh_type; |
1738 | filp->private_data = fh; | |
1d08a4fa | 1739 | v4l2_fh_init(&fh->fh, vdev); |
e0d3bafd SD |
1740 | |
1741 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { | |
84b5dbf3 | 1742 | /* Power up in Analog TV mode */ |
2f861387 | 1743 | if (dev->board.external_av) |
64fbf444 PB |
1744 | cx231xx_set_power_mode(dev, |
1745 | POLARIS_AVMODE_ENXTERNAL_AV); | |
1746 | else | |
1747 | cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV); | |
e0d3bafd SD |
1748 | |
1749 | #if 0 | |
1750 | cx231xx_set_mode(dev, CX231XX_ANALOG_MODE); | |
1751 | #endif | |
e0d3bafd | 1752 | |
84b5dbf3 MCC |
1753 | /* set video alternate setting */ |
1754 | cx231xx_set_video_alternate(dev); | |
e0d3bafd SD |
1755 | |
1756 | /* Needed, since GPIO might have disabled power of | |
1757 | some i2c device */ | |
1758 | cx231xx_config_i2c(dev); | |
1759 | ||
1760 | /* device needs to be initialized before isoc transfer */ | |
84b5dbf3 | 1761 | dev->video_input = dev->video_input > 2 ? 2 : dev->video_input; |
e0d3bafd SD |
1762 | |
1763 | } | |
71590765 | 1764 | if (radio) { |
e0d3bafd SD |
1765 | cx231xx_videodbg("video_open: setting radio device\n"); |
1766 | ||
1767 | /* cx231xx_start_radio(dev); */ | |
1768 | ||
b1196126 | 1769 | call_all(dev, tuner, s_radio); |
e0d3bafd SD |
1770 | } |
1771 | ||
1772 | dev->users++; | |
1773 | ||
cde4362f MCC |
1774 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1775 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops, | |
1776 | NULL, &dev->video_mode.slock, | |
1777 | fh->type, V4L2_FIELD_INTERLACED, | |
08bff03e | 1778 | sizeof(struct cx231xx_buffer), |
643800d5 | 1779 | fh, &dev->lock); |
84b5dbf3 | 1780 | if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { |
cde4362f MCC |
1781 | /* Set the required alternate setting VBI interface works in |
1782 | Bulk mode only */ | |
2f861387 | 1783 | cx231xx_set_alt_setting(dev, INDEX_VANC, 0); |
e0d3bafd | 1784 | |
cde4362f MCC |
1785 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops, |
1786 | NULL, &dev->vbi_mode.slock, | |
1787 | fh->type, V4L2_FIELD_SEQ_TB, | |
08bff03e | 1788 | sizeof(struct cx231xx_buffer), |
643800d5 | 1789 | fh, &dev->lock); |
84b5dbf3 | 1790 | } |
1f7e073d | 1791 | mutex_unlock(&dev->lock); |
1d08a4fa | 1792 | v4l2_fh_add(&fh->fh); |
e0d3bafd | 1793 | |
4df16f70 | 1794 | return 0; |
e0d3bafd SD |
1795 | } |
1796 | ||
1797 | /* | |
1798 | * cx231xx_realease_resources() | |
1799 | * unregisters the v4l2,i2c and usb devices | |
1800 | * called when the device gets disconected or at module unload | |
1801 | */ | |
1802 | void cx231xx_release_analog_resources(struct cx231xx *dev) | |
1803 | { | |
1804 | ||
1805 | /*FIXME: I2C IR should be disconnected */ | |
1806 | ||
60acf187 HV |
1807 | if (video_is_registered(&dev->radio_dev)) |
1808 | video_unregister_device(&dev->radio_dev); | |
1809 | if (video_is_registered(&dev->vbi_dev)) { | |
336fea92 | 1810 | dev_info(dev->dev, "V4L2 device %s deregistered\n", |
60acf187 HV |
1811 | video_device_node_name(&dev->vbi_dev)); |
1812 | video_unregister_device(&dev->vbi_dev); | |
e0d3bafd | 1813 | } |
60acf187 | 1814 | if (video_is_registered(&dev->vdev)) { |
336fea92 | 1815 | dev_info(dev->dev, "V4L2 device %s deregistered\n", |
60acf187 | 1816 | video_device_node_name(&dev->vdev)); |
64fbf444 | 1817 | |
2f861387 | 1818 | if (dev->board.has_417) |
64fbf444 PB |
1819 | cx231xx_417_unregister(dev); |
1820 | ||
60acf187 | 1821 | video_unregister_device(&dev->vdev); |
e0d3bafd | 1822 | } |
d2370f8e HV |
1823 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
1824 | v4l2_ctrl_handler_free(&dev->radio_ctrl_handler); | |
e0d3bafd SD |
1825 | } |
1826 | ||
1827 | /* | |
1f7e073d | 1828 | * cx231xx_close() |
e0d3bafd SD |
1829 | * stops streaming and deallocates all resources allocated by the v4l2 |
1830 | * calls and ioctls | |
1831 | */ | |
1f7e073d | 1832 | static int cx231xx_close(struct file *filp) |
e0d3bafd | 1833 | { |
84b5dbf3 MCC |
1834 | struct cx231xx_fh *fh = filp->private_data; |
1835 | struct cx231xx *dev = fh->dev; | |
e0d3bafd SD |
1836 | |
1837 | cx231xx_videodbg("users=%d\n", dev->users); | |
1838 | ||
64fbf444 | 1839 | cx231xx_videodbg("users=%d\n", dev->users); |
e0d3bafd SD |
1840 | if (res_check(fh)) |
1841 | res_free(fh); | |
1842 | ||
2f861387 MCC |
1843 | /* |
1844 | * To workaround error number=-71 on EP0 for VideoGrabber, | |
1845 | * need exclude following. | |
1846 | * FIXME: It is probably safe to remove most of these, as we're | |
1847 | * now avoiding the alternate setting for INDEX_VANC | |
1848 | */ | |
1849 | if (!dev->board.no_alt_vanc) | |
64fbf444 PB |
1850 | if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { |
1851 | videobuf_stop(&fh->vb_vidq); | |
1852 | videobuf_mmap_free(&fh->vb_vidq); | |
1853 | ||
1854 | /* the device is already disconnect, | |
1855 | free the remaining resources */ | |
1856 | if (dev->state & DEV_DISCONNECTED) { | |
1857 | if (atomic_read(&dev->devlist_count) > 0) { | |
1858 | cx231xx_release_resources(dev); | |
266e8ae3 | 1859 | fh->dev = NULL; |
64fbf444 PB |
1860 | return 0; |
1861 | } | |
64fbf444 PB |
1862 | return 0; |
1863 | } | |
84b5dbf3 | 1864 | |
64fbf444 PB |
1865 | /* do this before setting alternate! */ |
1866 | cx231xx_uninit_vbi_isoc(dev); | |
1867 | ||
1868 | /* set alternate 0 */ | |
1869 | if (!dev->vbi_or_sliced_cc_mode) | |
1870 | cx231xx_set_alt_setting(dev, INDEX_VANC, 0); | |
1871 | else | |
1872 | cx231xx_set_alt_setting(dev, INDEX_HANC, 0); | |
1873 | ||
1d08a4fa HV |
1874 | v4l2_fh_del(&fh->fh); |
1875 | v4l2_fh_exit(&fh->fh); | |
64fbf444 PB |
1876 | kfree(fh); |
1877 | dev->users--; | |
1878 | wake_up_interruptible_nr(&dev->open, 1); | |
84b5dbf3 MCC |
1879 | return 0; |
1880 | } | |
1881 | ||
1d08a4fa | 1882 | v4l2_fh_del(&fh->fh); |
990862a2 MCC |
1883 | dev->users--; |
1884 | if (!dev->users) { | |
e0d3bafd SD |
1885 | videobuf_stop(&fh->vb_vidq); |
1886 | videobuf_mmap_free(&fh->vb_vidq); | |
1887 | ||
1888 | /* the device is already disconnect, | |
1889 | free the remaining resources */ | |
1890 | if (dev->state & DEV_DISCONNECTED) { | |
1891 | cx231xx_release_resources(dev); | |
266e8ae3 | 1892 | fh->dev = NULL; |
e0d3bafd SD |
1893 | return 0; |
1894 | } | |
1895 | ||
84b5dbf3 | 1896 | /* Save some power by putting tuner to sleep */ |
622b828a | 1897 | call_all(dev, core, s_power, 0); |
e0d3bafd SD |
1898 | |
1899 | /* do this before setting alternate! */ | |
64fbf444 PB |
1900 | if (dev->USE_ISO) |
1901 | cx231xx_uninit_isoc(dev); | |
1902 | else | |
1903 | cx231xx_uninit_bulk(dev); | |
e0d3bafd SD |
1904 | cx231xx_set_mode(dev, CX231XX_SUSPEND); |
1905 | ||
1906 | /* set alternate 0 */ | |
1907 | cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0); | |
1908 | } | |
1d08a4fa | 1909 | v4l2_fh_exit(&fh->fh); |
e0d3bafd | 1910 | kfree(fh); |
e0d3bafd | 1911 | wake_up_interruptible_nr(&dev->open, 1); |
e0d3bafd SD |
1912 | return 0; |
1913 | } | |
1914 | ||
1f7e073d HV |
1915 | static int cx231xx_v4l2_close(struct file *filp) |
1916 | { | |
1917 | struct cx231xx_fh *fh = filp->private_data; | |
1918 | struct cx231xx *dev = fh->dev; | |
1919 | int rc; | |
1920 | ||
1921 | mutex_lock(&dev->lock); | |
1922 | rc = cx231xx_close(filp); | |
1923 | mutex_unlock(&dev->lock); | |
1924 | return rc; | |
1925 | } | |
1926 | ||
e0d3bafd SD |
1927 | /* |
1928 | * cx231xx_v4l2_read() | |
1929 | * will allocate buffers when called for the first time | |
1930 | */ | |
1931 | static ssize_t | |
cde4362f MCC |
1932 | cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count, |
1933 | loff_t *pos) | |
e0d3bafd SD |
1934 | { |
1935 | struct cx231xx_fh *fh = filp->private_data; | |
1936 | struct cx231xx *dev = fh->dev; | |
1937 | int rc; | |
1938 | ||
1939 | rc = check_dev(dev); | |
1940 | if (rc < 0) | |
1941 | return rc; | |
1942 | ||
84b5dbf3 MCC |
1943 | if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || |
1944 | (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) { | |
e0d3bafd | 1945 | rc = res_get(fh); |
e0d3bafd SD |
1946 | |
1947 | if (unlikely(rc < 0)) | |
1948 | return rc; | |
1949 | ||
1f7e073d HV |
1950 | if (mutex_lock_interruptible(&dev->lock)) |
1951 | return -ERESTARTSYS; | |
1952 | rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, | |
84b5dbf3 | 1953 | filp->f_flags & O_NONBLOCK); |
1f7e073d HV |
1954 | mutex_unlock(&dev->lock); |
1955 | return rc; | |
e0d3bafd SD |
1956 | } |
1957 | return 0; | |
1958 | } | |
1959 | ||
1960 | /* | |
1961 | * cx231xx_v4l2_poll() | |
1962 | * will allocate buffers when called for the first time | |
1963 | */ | |
64fbf444 | 1964 | static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait) |
e0d3bafd | 1965 | { |
1d08a4fa | 1966 | unsigned long req_events = poll_requested_events(wait); |
e0d3bafd SD |
1967 | struct cx231xx_fh *fh = filp->private_data; |
1968 | struct cx231xx *dev = fh->dev; | |
1d08a4fa | 1969 | unsigned res = 0; |
e0d3bafd SD |
1970 | int rc; |
1971 | ||
1972 | rc = check_dev(dev); | |
1973 | if (rc < 0) | |
1d08a4fa | 1974 | return POLLERR; |
e0d3bafd | 1975 | |
e0d3bafd | 1976 | rc = res_get(fh); |
e0d3bafd SD |
1977 | |
1978 | if (unlikely(rc < 0)) | |
1979 | return POLLERR; | |
1980 | ||
1d08a4fa HV |
1981 | if (v4l2_event_pending(&fh->fh)) |
1982 | res |= POLLPRI; | |
1983 | else | |
1984 | poll_wait(filp, &fh->fh.wait, wait); | |
1985 | ||
1986 | if (!(req_events & (POLLIN | POLLRDNORM))) | |
1987 | return res; | |
1988 | ||
84b5dbf3 | 1989 | if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) || |
1f7e073d | 1990 | (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) { |
1f7e073d | 1991 | mutex_lock(&dev->lock); |
1d08a4fa | 1992 | res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait); |
1f7e073d HV |
1993 | mutex_unlock(&dev->lock); |
1994 | return res; | |
1995 | } | |
1d08a4fa | 1996 | return res | POLLERR; |
e0d3bafd SD |
1997 | } |
1998 | ||
1999 | /* | |
2000 | * cx231xx_v4l2_mmap() | |
2001 | */ | |
2002 | static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) | |
2003 | { | |
84b5dbf3 MCC |
2004 | struct cx231xx_fh *fh = filp->private_data; |
2005 | struct cx231xx *dev = fh->dev; | |
2006 | int rc; | |
e0d3bafd SD |
2007 | |
2008 | rc = check_dev(dev); | |
2009 | if (rc < 0) | |
2010 | return rc; | |
2011 | ||
e0d3bafd | 2012 | rc = res_get(fh); |
e0d3bafd SD |
2013 | |
2014 | if (unlikely(rc < 0)) | |
2015 | return rc; | |
2016 | ||
1f7e073d HV |
2017 | if (mutex_lock_interruptible(&dev->lock)) |
2018 | return -ERESTARTSYS; | |
e0d3bafd | 2019 | rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); |
1f7e073d | 2020 | mutex_unlock(&dev->lock); |
e0d3bafd SD |
2021 | |
2022 | cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", | |
84b5dbf3 MCC |
2023 | (unsigned long)vma->vm_start, |
2024 | (unsigned long)vma->vm_end - | |
2025 | (unsigned long)vma->vm_start, rc); | |
e0d3bafd SD |
2026 | |
2027 | return rc; | |
2028 | } | |
2029 | ||
2030 | static const struct v4l2_file_operations cx231xx_v4l_fops = { | |
cde4362f MCC |
2031 | .owner = THIS_MODULE, |
2032 | .open = cx231xx_v4l2_open, | |
84b5dbf3 | 2033 | .release = cx231xx_v4l2_close, |
cde4362f MCC |
2034 | .read = cx231xx_v4l2_read, |
2035 | .poll = cx231xx_v4l2_poll, | |
2036 | .mmap = cx231xx_v4l2_mmap, | |
643800d5 | 2037 | .unlocked_ioctl = video_ioctl2, |
e0d3bafd SD |
2038 | }; |
2039 | ||
2040 | static const struct v4l2_ioctl_ops video_ioctl_ops = { | |
bc08734c | 2041 | .vidioc_querycap = cx231xx_querycap, |
cde4362f MCC |
2042 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
2043 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
2044 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
2045 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
2046 | .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, | |
2047 | .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap, | |
6264722c | 2048 | .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap, |
cde4362f | 2049 | .vidioc_cropcap = vidioc_cropcap, |
cde4362f MCC |
2050 | .vidioc_reqbufs = vidioc_reqbufs, |
2051 | .vidioc_querybuf = vidioc_querybuf, | |
2052 | .vidioc_qbuf = vidioc_qbuf, | |
2053 | .vidioc_dqbuf = vidioc_dqbuf, | |
2054 | .vidioc_s_std = vidioc_s_std, | |
2055 | .vidioc_g_std = vidioc_g_std, | |
b86d1544 HV |
2056 | .vidioc_enum_input = cx231xx_enum_input, |
2057 | .vidioc_g_input = cx231xx_g_input, | |
2058 | .vidioc_s_input = cx231xx_s_input, | |
cde4362f MCC |
2059 | .vidioc_streamon = vidioc_streamon, |
2060 | .vidioc_streamoff = vidioc_streamoff, | |
b86d1544 HV |
2061 | .vidioc_g_tuner = cx231xx_g_tuner, |
2062 | .vidioc_s_tuner = cx231xx_s_tuner, | |
2063 | .vidioc_g_frequency = cx231xx_g_frequency, | |
2064 | .vidioc_s_frequency = cx231xx_s_frequency, | |
e0d3bafd | 2065 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
08fe9f7d | 2066 | .vidioc_g_chip_info = cx231xx_g_chip_info, |
b86d1544 HV |
2067 | .vidioc_g_register = cx231xx_g_register, |
2068 | .vidioc_s_register = cx231xx_s_register, | |
e0d3bafd | 2069 | #endif |
1d08a4fa HV |
2070 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
2071 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
e0d3bafd SD |
2072 | }; |
2073 | ||
2074 | static struct video_device cx231xx_vbi_template; | |
2075 | ||
2076 | static const struct video_device cx231xx_video_template = { | |
cde4362f | 2077 | .fops = &cx231xx_v4l_fops, |
60acf187 | 2078 | .release = video_device_release_empty, |
cde4362f | 2079 | .ioctl_ops = &video_ioctl_ops, |
cde4362f | 2080 | .tvnorms = V4L2_STD_ALL, |
e0d3bafd SD |
2081 | }; |
2082 | ||
2083 | static const struct v4l2_file_operations radio_fops = { | |
cde4362f MCC |
2084 | .owner = THIS_MODULE, |
2085 | .open = cx231xx_v4l2_open, | |
84b5dbf3 | 2086 | .release = cx231xx_v4l2_close, |
1d08a4fa | 2087 | .poll = v4l2_ctrl_poll, |
1265f080 | 2088 | .unlocked_ioctl = video_ioctl2, |
e0d3bafd SD |
2089 | }; |
2090 | ||
2091 | static const struct v4l2_ioctl_ops radio_ioctl_ops = { | |
bc08734c | 2092 | .vidioc_querycap = cx231xx_querycap, |
cde4362f | 2093 | .vidioc_g_tuner = radio_g_tuner, |
cde4362f | 2094 | .vidioc_s_tuner = radio_s_tuner, |
b86d1544 HV |
2095 | .vidioc_g_frequency = cx231xx_g_frequency, |
2096 | .vidioc_s_frequency = cx231xx_s_frequency, | |
e0d3bafd | 2097 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
08fe9f7d | 2098 | .vidioc_g_chip_info = cx231xx_g_chip_info, |
b86d1544 HV |
2099 | .vidioc_g_register = cx231xx_g_register, |
2100 | .vidioc_s_register = cx231xx_s_register, | |
e0d3bafd | 2101 | #endif |
1d08a4fa HV |
2102 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
2103 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
e0d3bafd SD |
2104 | }; |
2105 | ||
2106 | static struct video_device cx231xx_radio_template = { | |
cde4362f MCC |
2107 | .name = "cx231xx-radio", |
2108 | .fops = &radio_fops, | |
84b5dbf3 | 2109 | .ioctl_ops = &radio_ioctl_ops, |
e0d3bafd SD |
2110 | }; |
2111 | ||
2112 | /******************************** usb interface ******************************/ | |
2113 | ||
60acf187 HV |
2114 | static void cx231xx_vdev_init(struct cx231xx *dev, |
2115 | struct video_device *vfd, | |
2116 | const struct video_device *template, | |
2117 | const char *type_name) | |
e0d3bafd | 2118 | { |
e0d3bafd | 2119 | *vfd = *template; |
b1196126 | 2120 | vfd->v4l2_dev = &dev->v4l2_dev; |
60acf187 | 2121 | vfd->release = video_device_release_empty; |
643800d5 | 2122 | vfd->lock = &dev->lock; |
e0d3bafd | 2123 | |
84b5dbf3 | 2124 | snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name); |
e0d3bafd | 2125 | |
63b0d5ad | 2126 | video_set_drvdata(vfd, dev); |
06c46003 HV |
2127 | if (dev->tuner_type == TUNER_ABSENT) { |
2128 | v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY); | |
2129 | v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY); | |
2130 | v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER); | |
2131 | v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER); | |
2132 | } | |
e0d3bafd SD |
2133 | } |
2134 | ||
2135 | int cx231xx_register_analog_devices(struct cx231xx *dev) | |
2136 | { | |
2137 | int ret; | |
2138 | ||
336fea92 | 2139 | dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION); |
e0d3bafd SD |
2140 | |
2141 | /* set default norm */ | |
a25a7012 | 2142 | dev->norm = V4L2_STD_PAL; |
e0d3bafd SD |
2143 | dev->width = norm_maxw(dev); |
2144 | dev->height = norm_maxh(dev); | |
2145 | dev->interlaced = 0; | |
e0d3bafd SD |
2146 | |
2147 | /* Analog specific initialization */ | |
2148 | dev->format = &format[0]; | |
6e6a2ba9 DH |
2149 | |
2150 | /* Set the initial input */ | |
2151 | video_mux(dev, dev->video_input); | |
e0d3bafd | 2152 | |
8774bed9 | 2153 | call_all(dev, video, s_std, dev->norm); |
d61072a4 | 2154 | |
d2370f8e HV |
2155 | v4l2_ctrl_handler_init(&dev->ctrl_handler, 10); |
2156 | v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5); | |
2157 | ||
2158 | if (dev->sd_cx25840) { | |
2159 | v4l2_ctrl_add_handler(&dev->ctrl_handler, | |
2160 | dev->sd_cx25840->ctrl_handler, NULL); | |
2161 | v4l2_ctrl_add_handler(&dev->radio_ctrl_handler, | |
2162 | dev->sd_cx25840->ctrl_handler, | |
2163 | v4l2_ctrl_radio_filter); | |
2164 | } | |
2165 | ||
2166 | if (dev->ctrl_handler.error) | |
2167 | return dev->ctrl_handler.error; | |
2168 | if (dev->radio_ctrl_handler.error) | |
2169 | return dev->radio_ctrl_handler.error; | |
e0d3bafd SD |
2170 | |
2171 | /* enable vbi capturing */ | |
84b5dbf3 | 2172 | /* write code here... */ |
e0d3bafd SD |
2173 | |
2174 | /* allocate and fill video video_device struct */ | |
60acf187 | 2175 | cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video"); |
b6a40e72 MCC |
2176 | #if defined(CONFIG_MEDIA_CONTROLLER) |
2177 | dev->video_pad.flags = MEDIA_PAD_FL_SINK; | |
60acf187 | 2178 | ret = media_entity_init(&dev->vdev.entity, 1, &dev->video_pad, 0); |
b6a40e72 MCC |
2179 | if (ret < 0) |
2180 | dev_err(dev->dev, "failed to initialize video media entity!\n"); | |
2181 | #endif | |
60acf187 | 2182 | dev->vdev.ctrl_handler = &dev->ctrl_handler; |
e0d3bafd | 2183 | /* register v4l2 video video_device */ |
60acf187 | 2184 | ret = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, |
84b5dbf3 | 2185 | video_nr[dev->devno]); |
e0d3bafd | 2186 | if (ret) { |
336fea92 | 2187 | dev_err(dev->dev, |
b7085c08 | 2188 | "unable to register video device (error=%i).\n", |
ed0e3729 | 2189 | ret); |
e0d3bafd SD |
2190 | return ret; |
2191 | } | |
2192 | ||
336fea92 | 2193 | dev_info(dev->dev, "Registered video device %s [v4l2]\n", |
60acf187 | 2194 | video_device_node_name(&dev->vdev)); |
e0d3bafd | 2195 | |
84b5dbf3 | 2196 | /* Initialize VBI template */ |
3724dde9 | 2197 | cx231xx_vbi_template = cx231xx_video_template; |
84b5dbf3 | 2198 | strcpy(cx231xx_vbi_template.name, "cx231xx-vbi"); |
e0d3bafd SD |
2199 | |
2200 | /* Allocate and fill vbi video_device struct */ | |
60acf187 | 2201 | cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi"); |
e0d3bafd | 2202 | |
b6a40e72 MCC |
2203 | #if defined(CONFIG_MEDIA_CONTROLLER) |
2204 | dev->vbi_pad.flags = MEDIA_PAD_FL_SINK; | |
60acf187 | 2205 | ret = media_entity_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad, 0); |
b6a40e72 MCC |
2206 | if (ret < 0) |
2207 | dev_err(dev->dev, "failed to initialize vbi media entity!\n"); | |
2208 | #endif | |
60acf187 | 2209 | dev->vbi_dev.ctrl_handler = &dev->ctrl_handler; |
e0d3bafd | 2210 | /* register v4l2 vbi video_device */ |
60acf187 | 2211 | ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, |
84b5dbf3 | 2212 | vbi_nr[dev->devno]); |
e0d3bafd | 2213 | if (ret < 0) { |
336fea92 | 2214 | dev_err(dev->dev, "unable to register vbi device\n"); |
e0d3bafd SD |
2215 | return ret; |
2216 | } | |
2217 | ||
336fea92 | 2218 | dev_info(dev->dev, "Registered VBI device %s\n", |
60acf187 | 2219 | video_device_node_name(&dev->vbi_dev)); |
e0d3bafd SD |
2220 | |
2221 | if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) { | |
60acf187 HV |
2222 | cx231xx_vdev_init(dev, &dev->radio_dev, |
2223 | &cx231xx_radio_template, "radio"); | |
2224 | dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler; | |
2225 | ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO, | |
e0d3bafd SD |
2226 | radio_nr[dev->devno]); |
2227 | if (ret < 0) { | |
336fea92 | 2228 | dev_err(dev->dev, |
b7085c08 | 2229 | "can't register radio device\n"); |
e0d3bafd SD |
2230 | return ret; |
2231 | } | |
336fea92 | 2232 | dev_info(dev->dev, "Registered radio device as %s\n", |
60acf187 | 2233 | video_device_node_name(&dev->radio_dev)); |
e0d3bafd SD |
2234 | } |
2235 | ||
e0d3bafd SD |
2236 | return 0; |
2237 | } |