akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1 | /* |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 2 | em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB |
| 3 | video capture devices |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 4 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 5 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
| 6 | Markus Rechberger <mrechberger@gmail.com> |
Mauro Carvalho Chehab | 2e7c6dc | 2006-04-03 07:53:40 -0300 | [diff] [blame] | 7 | Mauro Carvalho Chehab <mchehab@infradead.org> |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 8 | Sascha Sommer <saschasommer@freenet.de> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 9 | |
Mauro Carvalho Chehab | 439090d | 2006-01-23 17:10:54 -0200 | [diff] [blame] | 10 | Some parts based on SN9C10x PC Camera Controllers GPL driver made |
| 11 | by Luca Risolia <luca.risolia@studio.unibo.it> |
| 12 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 32 | #include <linux/bitmap.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 33 | #include <linux/usb.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 34 | #include <linux/i2c.h> |
Mauro Carvalho Chehab | b296fc6 | 2005-11-08 21:38:37 -0800 | [diff] [blame] | 35 | #include <linux/version.h> |
Trent Piepho | 6d35c8f | 2007-11-01 01:16:09 -0300 | [diff] [blame] | 36 | #include <linux/mm.h> |
Ingo Molnar | 1e4baed | 2006-01-15 07:52:23 -0200 | [diff] [blame] | 37 | #include <linux/mutex.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 38 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 39 | #include "em28xx.h" |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 40 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 41 | #include <media/v4l2-ioctl.h> |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 42 | #include <media/v4l2-chip-ident.h> |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 43 | #include <media/msp3400.h> |
Mauro Carvalho Chehab | ed08631 | 2008-01-24 06:59:20 -0300 | [diff] [blame] | 44 | #include <media/tuner.h> |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 45 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 46 | #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \ |
| 47 | "Markus Rechberger <mrechberger@gmail.com>, " \ |
Mauro Carvalho Chehab | 2e7c6dc | 2006-04-03 07:53:40 -0300 | [diff] [blame] | 48 | "Mauro Carvalho Chehab <mchehab@infradead.org>, " \ |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 49 | "Sascha Sommer <saschasommer@freenet.de>" |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 50 | |
Mauro Carvalho Chehab | f7abcd3 | 2005-11-08 21:38:25 -0800 | [diff] [blame] | 51 | #define DRIVER_DESC "Empia em28xx based USB video device driver" |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 52 | #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 1) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 53 | |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 54 | #define em28xx_videodbg(fmt, arg...) do {\ |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 55 | if (video_debug) \ |
| 56 | printk(KERN_INFO "%s %s :"fmt, \ |
Harvey Harrison | d80e134 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 57 | dev->name, __func__ , ##arg); } while (0) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 58 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 59 | static unsigned int isoc_debug; |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 60 | module_param(isoc_debug, int, 0644); |
| 61 | MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]"); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 62 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 63 | #define em28xx_isocdbg(fmt, arg...) \ |
| 64 | do {\ |
| 65 | if (isoc_debug) { \ |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 66 | printk(KERN_INFO "%s %s :"fmt, \ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 67 | dev->name, __func__ , ##arg); \ |
| 68 | } \ |
| 69 | } while (0) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 70 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 71 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 72 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 73 | MODULE_LICENSE("GPL"); |
| 74 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 75 | static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 76 | static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
| 77 | static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET }; |
| 78 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 79 | module_param_array(video_nr, int, NULL, 0444); |
| 80 | module_param_array(vbi_nr, int, NULL, 0444); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 81 | module_param_array(radio_nr, int, NULL, 0444); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 82 | MODULE_PARM_DESC(video_nr, "video device numbers"); |
| 83 | MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); |
| 84 | MODULE_PARM_DESC(radio_nr, "radio device numbers"); |
Mauro Carvalho Chehab | 596d92d | 2005-11-08 21:37:24 -0800 | [diff] [blame] | 85 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 86 | static unsigned int video_debug; |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 87 | module_param(video_debug, int, 0644); |
| 88 | MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 89 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 90 | /* supported video standards */ |
| 91 | static struct em28xx_fmt format[] = { |
| 92 | { |
| 93 | .name = "16bpp YUY2, 4:2:2, packed", |
| 94 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 95 | .depth = 16, |
Devin Heitmueller | 3fbf930 | 2008-12-29 23:34:37 -0300 | [diff] [blame] | 96 | .reg = EM28XX_OUTFMT_YUV422_Y0UY1V, |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 97 | }, |
| 98 | }; |
| 99 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 100 | /* supported controls */ |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 101 | /* Common to all boards */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 102 | static struct v4l2_queryctrl em28xx_qctrl[] = { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 103 | { |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 104 | .id = V4L2_CID_AUDIO_VOLUME, |
| 105 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 106 | .name = "Volume", |
| 107 | .minimum = 0x0, |
| 108 | .maximum = 0x1f, |
| 109 | .step = 0x1, |
| 110 | .default_value = 0x1f, |
| 111 | .flags = 0, |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 112 | }, { |
Mauro Carvalho Chehab | c0477ad | 2006-01-09 15:25:14 -0200 | [diff] [blame] | 113 | .id = V4L2_CID_AUDIO_MUTE, |
| 114 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 115 | .name = "Mute", |
| 116 | .minimum = 0, |
| 117 | .maximum = 1, |
| 118 | .step = 1, |
| 119 | .default_value = 1, |
| 120 | .flags = 0, |
| 121 | } |
| 122 | }; |
| 123 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 124 | /* ------------------------------------------------------------------ |
| 125 | DMA and thread functions |
| 126 | ------------------------------------------------------------------*/ |
| 127 | |
| 128 | /* |
| 129 | * Announces that a buffer were filled and request the next |
| 130 | */ |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 131 | static inline void buffer_filled(struct em28xx *dev, |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 132 | struct em28xx_dmaqueue *dma_q, |
| 133 | struct em28xx_buffer *buf) |
| 134 | { |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 135 | /* Advice that buffer was filled */ |
| 136 | em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); |
| 137 | buf->vb.state = VIDEOBUF_DONE; |
| 138 | buf->vb.field_count++; |
| 139 | do_gettimeofday(&buf->vb.ts); |
| 140 | |
Mauro Carvalho Chehab | cb78472 | 2008-04-13 15:06:52 -0300 | [diff] [blame] | 141 | dev->isoc_ctl.buf = NULL; |
| 142 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 143 | list_del(&buf->vb.queue); |
| 144 | wake_up(&buf->vb.done); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Identify the buffer header type and properly handles |
| 149 | */ |
| 150 | static void em28xx_copy_video(struct em28xx *dev, |
| 151 | struct em28xx_dmaqueue *dma_q, |
| 152 | struct em28xx_buffer *buf, |
| 153 | unsigned char *p, |
| 154 | unsigned char *outp, unsigned long len) |
| 155 | { |
| 156 | void *fieldstart, *startwrite, *startread; |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 157 | int linesdone, currlinedone, offset, lencopy, remain; |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 158 | int bytesperline = dev->width << 1; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 159 | |
| 160 | if (dma_q->pos + len > buf->vb.size) |
| 161 | len = buf->vb.size - dma_q->pos; |
| 162 | |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 163 | if (p[0] != 0x88 && p[0] != 0x22) { |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 164 | em28xx_isocdbg("frame is not complete\n"); |
| 165 | len += 4; |
| 166 | } else |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 167 | p += 4; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 168 | |
| 169 | startread = p; |
| 170 | remain = len; |
| 171 | |
| 172 | /* Interlaces frame */ |
| 173 | if (buf->top_field) |
| 174 | fieldstart = outp; |
| 175 | else |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 176 | fieldstart = outp + bytesperline; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 177 | |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 178 | linesdone = dma_q->pos / bytesperline; |
| 179 | currlinedone = dma_q->pos % bytesperline; |
| 180 | offset = linesdone * bytesperline * 2 + currlinedone; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 181 | startwrite = fieldstart + offset; |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 182 | lencopy = bytesperline - currlinedone; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 183 | lencopy = lencopy > remain ? remain : lencopy; |
| 184 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 185 | if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { |
Mauro Carvalho Chehab | ea8df7e | 2008-04-13 14:39:29 -0300 | [diff] [blame] | 186 | em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n", |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 187 | ((char *)startwrite + lencopy) - |
| 188 | ((char *)outp + buf->vb.size)); |
| 189 | lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 190 | } |
Aidan Thornton | e0fadfd34 | 2008-04-13 14:56:02 -0300 | [diff] [blame] | 191 | if (lencopy <= 0) |
| 192 | return; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 193 | memcpy(startwrite, startread, lencopy); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 194 | |
| 195 | remain -= lencopy; |
| 196 | |
| 197 | while (remain > 0) { |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 198 | startwrite += lencopy + bytesperline; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 199 | startread += lencopy; |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 200 | if (bytesperline > remain) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 201 | lencopy = remain; |
| 202 | else |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 203 | lencopy = bytesperline; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 204 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 205 | if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { |
Mauro Carvalho Chehab | ea8df7e | 2008-04-13 14:39:29 -0300 | [diff] [blame] | 206 | em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n", |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 207 | ((char *)startwrite + lencopy) - |
| 208 | ((char *)outp + buf->vb.size)); |
| 209 | lencopy = remain = (char *)outp + buf->vb.size - |
| 210 | (char *)startwrite; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 211 | } |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 212 | if (lencopy <= 0) |
| 213 | break; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 214 | |
| 215 | memcpy(startwrite, startread, lencopy); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 216 | |
| 217 | remain -= lencopy; |
| 218 | } |
| 219 | |
| 220 | dma_q->pos += len; |
| 221 | } |
| 222 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 223 | static inline void print_err_status(struct em28xx *dev, |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 224 | int packet, int status) |
| 225 | { |
| 226 | char *errmsg = "Unknown"; |
| 227 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 228 | switch (status) { |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 229 | case -ENOENT: |
| 230 | errmsg = "unlinked synchronuously"; |
| 231 | break; |
| 232 | case -ECONNRESET: |
| 233 | errmsg = "unlinked asynchronuously"; |
| 234 | break; |
| 235 | case -ENOSR: |
| 236 | errmsg = "Buffer error (overrun)"; |
| 237 | break; |
| 238 | case -EPIPE: |
| 239 | errmsg = "Stalled (device not responding)"; |
| 240 | break; |
| 241 | case -EOVERFLOW: |
| 242 | errmsg = "Babble (bad cable?)"; |
| 243 | break; |
| 244 | case -EPROTO: |
| 245 | errmsg = "Bit-stuff error (bad cable?)"; |
| 246 | break; |
| 247 | case -EILSEQ: |
| 248 | errmsg = "CRC/Timeout (could be anything)"; |
| 249 | break; |
| 250 | case -ETIME: |
| 251 | errmsg = "Device does not respond"; |
| 252 | break; |
| 253 | } |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 254 | if (packet < 0) { |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 255 | em28xx_isocdbg("URB status %d [%s].\n", status, errmsg); |
| 256 | } else { |
| 257 | em28xx_isocdbg("URB packet %d, status %d [%s].\n", |
| 258 | packet, status, errmsg); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * video-buf generic routine to get the next available buffer |
| 264 | */ |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 265 | static inline void get_next_buf(struct em28xx_dmaqueue *dma_q, |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 266 | struct em28xx_buffer **buf) |
| 267 | { |
| 268 | struct em28xx *dev = container_of(dma_q, struct em28xx, vidq); |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 269 | char *outp; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 270 | |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 271 | if (list_empty(&dma_q->active)) { |
| 272 | em28xx_isocdbg("No active queue to serve\n"); |
| 273 | dev->isoc_ctl.buf = NULL; |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 274 | *buf = NULL; |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 275 | return; |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 276 | } |
| 277 | |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 278 | /* Get the next buffer */ |
| 279 | *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue); |
| 280 | |
Mauro Carvalho Chehab | dbecb44 | 2008-04-13 15:08:55 -0300 | [diff] [blame] | 281 | /* Cleans up buffer - Usefull for testing for frame/URB loss */ |
| 282 | outp = videobuf_to_vmalloc(&(*buf)->vb); |
| 283 | memset(outp, 0, (*buf)->vb.size); |
Mauro Carvalho Chehab | cb78472 | 2008-04-13 15:06:52 -0300 | [diff] [blame] | 284 | |
| 285 | dev->isoc_ctl.buf = *buf; |
| 286 | |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 287 | return; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Controls the isoc copy of each urb packet |
| 292 | */ |
Aidan Thornton | 579f72e | 2008-04-17 21:40:16 -0300 | [diff] [blame] | 293 | static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 294 | { |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 295 | struct em28xx_buffer *buf; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 296 | struct em28xx_dmaqueue *dma_q = urb->context; |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 297 | unsigned char *outp = NULL; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 298 | int i, len = 0, rc = 1; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 299 | unsigned char *p; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 300 | |
| 301 | if (!dev) |
| 302 | return 0; |
| 303 | |
| 304 | if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED)) |
| 305 | return 0; |
| 306 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 307 | if (urb->status < 0) { |
| 308 | print_err_status(dev, -1, urb->status); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 309 | if (urb->status == -ENOENT) |
| 310 | return 0; |
| 311 | } |
| 312 | |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 313 | buf = dev->isoc_ctl.buf; |
| 314 | if (buf != NULL) |
| 315 | outp = videobuf_to_vmalloc(&buf->vb); |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 316 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 317 | for (i = 0; i < urb->number_of_packets; i++) { |
| 318 | int status = urb->iso_frame_desc[i].status; |
| 319 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 320 | if (status < 0) { |
| 321 | print_err_status(dev, i, status); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 322 | if (urb->iso_frame_desc[i].status != -EPROTO) |
| 323 | continue; |
| 324 | } |
| 325 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 326 | len = urb->iso_frame_desc[i].actual_length - 4; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 327 | |
| 328 | if (urb->iso_frame_desc[i].actual_length <= 0) { |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 329 | /* em28xx_isocdbg("packet %d is empty",i); - spammy */ |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 330 | continue; |
| 331 | } |
| 332 | if (urb->iso_frame_desc[i].actual_length > |
| 333 | dev->max_pkt_size) { |
| 334 | em28xx_isocdbg("packet bigger than packet size"); |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 339 | |
| 340 | /* FIXME: incomplete buffer checks where removed to make |
| 341 | logic simpler. Impacts of those changes should be evaluated |
| 342 | */ |
Mauro Carvalho Chehab | b4916f8 | 2008-04-13 15:09:14 -0300 | [diff] [blame] | 343 | if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) { |
| 344 | em28xx_isocdbg("VBI HEADER!!!\n"); |
| 345 | /* FIXME: Should add vbi copy */ |
| 346 | continue; |
| 347 | } |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 348 | if (p[0] == 0x22 && p[1] == 0x5a) { |
Mauro Carvalho Chehab | 78bb394 | 2008-04-13 14:56:25 -0300 | [diff] [blame] | 349 | em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2], |
| 350 | len, (p[2] & 1)? "odd" : "even"); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 351 | |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 352 | if (!(p[2] & 1)) { |
| 353 | if (buf != NULL) |
| 354 | buffer_filled(dev, dma_q, buf); |
| 355 | get_next_buf(dma_q, &buf); |
| 356 | if (buf == NULL) |
| 357 | outp = NULL; |
| 358 | else |
| 359 | outp = videobuf_to_vmalloc(&buf->vb); |
Aidan Thornton | e0fadfd34 | 2008-04-13 14:56:02 -0300 | [diff] [blame] | 360 | } |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 361 | |
| 362 | if (buf != NULL) { |
| 363 | if (p[2] & 1) |
| 364 | buf->top_field = 0; |
| 365 | else |
| 366 | buf->top_field = 1; |
| 367 | } |
Mauro Carvalho Chehab | b4916f8 | 2008-04-13 15:09:14 -0300 | [diff] [blame] | 368 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 369 | dma_q->pos = 0; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 370 | } |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 371 | if (buf != NULL) |
| 372 | em28xx_copy_video(dev, dma_q, buf, p, outp, len); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 373 | } |
| 374 | return rc; |
| 375 | } |
| 376 | |
| 377 | /* ------------------------------------------------------------------ |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 378 | Videobuf operations |
| 379 | ------------------------------------------------------------------*/ |
| 380 | |
| 381 | static int |
| 382 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) |
| 383 | { |
| 384 | struct em28xx_fh *fh = vq->priv_data; |
Mauro Carvalho Chehab | d2d9fbf | 2008-04-17 21:38:53 -0300 | [diff] [blame] | 385 | struct em28xx *dev = fh->dev; |
| 386 | struct v4l2_frequency f; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 387 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 388 | *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3; |
| 389 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 390 | if (0 == *count) |
| 391 | *count = EM28XX_DEF_BUF; |
| 392 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 393 | if (*count < EM28XX_MIN_BUF) |
| 394 | *count = EM28XX_MIN_BUF; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 395 | |
Mauro Carvalho Chehab | 381aaba | 2008-12-20 07:43:34 -0300 | [diff] [blame] | 396 | /* Ask tuner to go to analog or radio mode */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 397 | memset(&f, 0, sizeof(f)); |
Mauro Carvalho Chehab | d2d9fbf | 2008-04-17 21:38:53 -0300 | [diff] [blame] | 398 | f.frequency = dev->ctl_freq; |
Mauro Carvalho Chehab | 381aaba | 2008-12-20 07:43:34 -0300 | [diff] [blame] | 399 | f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; |
Mauro Carvalho Chehab | d2d9fbf | 2008-04-17 21:38:53 -0300 | [diff] [blame] | 400 | |
| 401 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f); |
| 402 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 406 | /* This is called *without* dev->slock held; please keep it that way */ |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 407 | static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf) |
| 408 | { |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 409 | struct em28xx_fh *fh = vq->priv_data; |
| 410 | struct em28xx *dev = fh->dev; |
| 411 | unsigned long flags = 0; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 412 | if (in_interrupt()) |
| 413 | BUG(); |
| 414 | |
Aidan Thornton | 3b5fa92 | 2008-04-13 15:09:36 -0300 | [diff] [blame] | 415 | /* We used to wait for the buffer to finish here, but this didn't work |
| 416 | because, as we were keeping the state as VIDEOBUF_QUEUED, |
| 417 | videobuf_queue_cancel marked it as finished for us. |
| 418 | (Also, it could wedge forever if the hardware was misconfigured.) |
| 419 | |
| 420 | This should be safe; by the time we get here, the buffer isn't |
| 421 | queued anymore. If we ever start marking the buffers as |
| 422 | VIDEOBUF_ACTIVE, it won't be, though. |
| 423 | */ |
| 424 | spin_lock_irqsave(&dev->slock, flags); |
| 425 | if (dev->isoc_ctl.buf == buf) |
| 426 | dev->isoc_ctl.buf = NULL; |
| 427 | spin_unlock_irqrestore(&dev->slock, flags); |
| 428 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 429 | videobuf_vmalloc_free(&buf->vb); |
| 430 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 431 | } |
| 432 | |
| 433 | static int |
| 434 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, |
| 435 | enum v4l2_field field) |
| 436 | { |
| 437 | struct em28xx_fh *fh = vq->priv_data; |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 438 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 439 | struct em28xx *dev = fh->dev; |
| 440 | int rc = 0, urb_init = 0; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 441 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 442 | buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 443 | |
| 444 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
| 445 | return -EINVAL; |
| 446 | |
Brandon Philips | 0561297 | 2008-04-13 14:57:01 -0300 | [diff] [blame] | 447 | buf->vb.width = dev->width; |
| 448 | buf->vb.height = dev->height; |
| 449 | buf->vb.field = field; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 450 | |
| 451 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 452 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
| 453 | if (rc < 0) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 454 | goto fail; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 455 | } |
| 456 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 457 | if (!dev->isoc_ctl.num_bufs) |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 458 | urb_init = 1; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 459 | |
| 460 | if (urb_init) { |
Aidan Thornton | 579f72e | 2008-04-17 21:40:16 -0300 | [diff] [blame] | 461 | rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS, |
| 462 | EM28XX_NUM_BUFS, dev->max_pkt_size, |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 463 | em28xx_isoc_copy); |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 464 | if (rc < 0) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 465 | goto fail; |
| 466 | } |
| 467 | |
| 468 | buf->vb.state = VIDEOBUF_PREPARED; |
| 469 | return 0; |
| 470 | |
| 471 | fail: |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 472 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 478 | { |
| 479 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); |
| 480 | struct em28xx_fh *fh = vq->priv_data; |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 481 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 482 | struct em28xx_dmaqueue *vidq = &dev->vidq; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 483 | |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 484 | buf->vb.state = VIDEOBUF_QUEUED; |
| 485 | list_add_tail(&buf->vb.queue, &vidq->active); |
| 486 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 487 | } |
| 488 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 489 | static void buffer_release(struct videobuf_queue *vq, |
| 490 | struct videobuf_buffer *vb) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 491 | { |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 492 | struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 493 | struct em28xx_fh *fh = vq->priv_data; |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 494 | struct em28xx *dev = (struct em28xx *)fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 495 | |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 496 | em28xx_isocdbg("em28xx: called buffer_release\n"); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 497 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 498 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static struct videobuf_queue_ops em28xx_video_qops = { |
| 502 | .buf_setup = buffer_setup, |
| 503 | .buf_prepare = buffer_prepare, |
| 504 | .buf_queue = buffer_queue, |
| 505 | .buf_release = buffer_release, |
| 506 | }; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 507 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 508 | /********************* v4l2 interface **************************************/ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 509 | |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 510 | static void video_mux(struct em28xx *dev, int index) |
| 511 | { |
Hans Verkuil | c7c0b34 | 2006-04-02 13:35:00 -0300 | [diff] [blame] | 512 | struct v4l2_routing route; |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 513 | |
Hans Verkuil | c7c0b34 | 2006-04-02 13:35:00 -0300 | [diff] [blame] | 514 | route.input = INPUT(index)->vmux; |
| 515 | route.output = 0; |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 516 | dev->ctl_input = index; |
| 517 | dev->ctl_ainput = INPUT(index)->amux; |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 518 | dev->ctl_aoutput = INPUT(index)->aout; |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 519 | |
Mauro Carvalho Chehab | e879b8e | 2008-11-20 13:39:39 -0300 | [diff] [blame] | 520 | if (!dev->ctl_aoutput) |
| 521 | dev->ctl_aoutput = EM28XX_AOUT_MASTER; |
| 522 | |
Hans Verkuil | c7c0b34 | 2006-04-02 13:35:00 -0300 | [diff] [blame] | 523 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 524 | |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 525 | if (dev->board.has_msp34xx) { |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 526 | if (dev->i2s_speed) { |
| 527 | em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, |
| 528 | &dev->i2s_speed); |
| 529 | } |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 530 | route.input = dev->ctl_ainput; |
Hans Verkuil | 0715172 | 2006-04-01 18:03:23 -0300 | [diff] [blame] | 531 | route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); |
Hans Verkuil | 2474ed4 | 2006-03-19 12:35:57 -0300 | [diff] [blame] | 532 | /* Note: this is msp3400 specific */ |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 533 | em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, |
| 534 | &route); |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 535 | } |
Mauro Carvalho Chehab | 539c96d | 2008-01-05 09:53:54 -0300 | [diff] [blame] | 536 | |
Mauro Carvalho Chehab | 00b8730 | 2008-02-06 18:34:13 -0300 | [diff] [blame] | 537 | em28xx_audio_analog_set(dev); |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 540 | /* Usage lock check functions */ |
| 541 | static int res_get(struct em28xx_fh *fh) |
| 542 | { |
| 543 | struct em28xx *dev = fh->dev; |
| 544 | int rc = 0; |
| 545 | |
| 546 | /* This instance already has stream_on */ |
| 547 | if (fh->stream_on) |
| 548 | return rc; |
| 549 | |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 550 | if (dev->stream_on) |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 551 | return -EBUSY; |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 552 | |
Mauro Carvalho Chehab | e74153d | 2008-04-13 14:55:38 -0300 | [diff] [blame] | 553 | dev->stream_on = 1; |
| 554 | fh->stream_on = 1; |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 555 | return rc; |
| 556 | } |
| 557 | |
| 558 | static int res_check(struct em28xx_fh *fh) |
| 559 | { |
| 560 | return (fh->stream_on); |
| 561 | } |
| 562 | |
| 563 | static void res_free(struct em28xx_fh *fh) |
| 564 | { |
| 565 | struct em28xx *dev = fh->dev; |
| 566 | |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 567 | fh->stream_on = 0; |
| 568 | dev->stream_on = 0; |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 569 | } |
| 570 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 571 | /* |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 572 | * em28xx_get_ctrl() |
| 573 | * return the current saturation, brightness or contrast, mute state |
| 574 | */ |
| 575 | static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl) |
| 576 | { |
| 577 | switch (ctrl->id) { |
| 578 | case V4L2_CID_AUDIO_MUTE: |
| 579 | ctrl->value = dev->mute; |
| 580 | return 0; |
| 581 | case V4L2_CID_AUDIO_VOLUME: |
| 582 | ctrl->value = dev->volume; |
| 583 | return 0; |
| 584 | default: |
| 585 | return -EINVAL; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * em28xx_set_ctrl() |
| 591 | * mute or set new saturation, brightness or contrast |
| 592 | */ |
| 593 | static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl) |
| 594 | { |
| 595 | switch (ctrl->id) { |
| 596 | case V4L2_CID_AUDIO_MUTE: |
| 597 | if (ctrl->value != dev->mute) { |
| 598 | dev->mute = ctrl->value; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 599 | return em28xx_audio_analog_set(dev); |
| 600 | } |
| 601 | return 0; |
| 602 | case V4L2_CID_AUDIO_VOLUME: |
| 603 | dev->volume = ctrl->value; |
| 604 | return em28xx_audio_analog_set(dev); |
| 605 | default: |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | } |
| 609 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 610 | static int check_dev(struct em28xx *dev) |
| 611 | { |
| 612 | if (dev->state & DEV_DISCONNECTED) { |
| 613 | em28xx_errdev("v4l2 ioctl: device not present\n"); |
| 614 | return -ENODEV; |
| 615 | } |
| 616 | |
| 617 | if (dev->state & DEV_MISCONFIGURED) { |
| 618 | em28xx_errdev("v4l2 ioctl: device is misconfigured; " |
| 619 | "close and open it again\n"); |
| 620 | return -EIO; |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static void get_scale(struct em28xx *dev, |
| 626 | unsigned int width, unsigned int height, |
| 627 | unsigned int *hscale, unsigned int *vscale) |
| 628 | { |
| 629 | unsigned int maxw = norm_maxw(dev); |
| 630 | unsigned int maxh = norm_maxh(dev); |
| 631 | |
| 632 | *hscale = (((unsigned long)maxw) << 12) / width - 4096L; |
| 633 | if (*hscale >= 0x4000) |
| 634 | *hscale = 0x3fff; |
| 635 | |
| 636 | *vscale = (((unsigned long)maxh) << 12) / height - 4096L; |
| 637 | if (*vscale >= 0x4000) |
| 638 | *vscale = 0x3fff; |
| 639 | } |
| 640 | |
| 641 | /* ------------------------------------------------------------------ |
| 642 | IOCTL vidioc handling |
| 643 | ------------------------------------------------------------------*/ |
| 644 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 645 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 646 | struct v4l2_format *f) |
| 647 | { |
| 648 | struct em28xx_fh *fh = priv; |
| 649 | struct em28xx *dev = fh->dev; |
| 650 | |
| 651 | mutex_lock(&dev->lock); |
| 652 | |
| 653 | f->fmt.pix.width = dev->width; |
| 654 | f->fmt.pix.height = dev->height; |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 655 | f->fmt.pix.pixelformat = dev->format->fourcc; |
| 656 | f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3; |
Mauro Carvalho Chehab | 44dc733 | 2008-04-13 15:11:08 -0300 | [diff] [blame] | 657 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 658 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 659 | |
| 660 | /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ |
| 661 | f->fmt.pix.field = dev->interlaced ? |
| 662 | V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; |
| 663 | |
| 664 | mutex_unlock(&dev->lock); |
| 665 | return 0; |
| 666 | } |
| 667 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 668 | static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc) |
| 669 | { |
| 670 | unsigned int i; |
| 671 | |
| 672 | for (i = 0; i < ARRAY_SIZE(format); i++) |
| 673 | if (format[i].fourcc == fourcc) |
| 674 | return &format[i]; |
| 675 | |
| 676 | return NULL; |
| 677 | } |
| 678 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 679 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 680 | struct v4l2_format *f) |
| 681 | { |
| 682 | struct em28xx_fh *fh = priv; |
| 683 | struct em28xx *dev = fh->dev; |
| 684 | int width = f->fmt.pix.width; |
| 685 | int height = f->fmt.pix.height; |
| 686 | unsigned int maxw = norm_maxw(dev); |
| 687 | unsigned int maxh = norm_maxh(dev); |
| 688 | unsigned int hscale, vscale; |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 689 | struct em28xx_fmt *fmt; |
| 690 | |
| 691 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 692 | if (!fmt) { |
| 693 | em28xx_videodbg("Fourcc format (%08x) invalid.\n", |
| 694 | f->fmt.pix.pixelformat); |
| 695 | return -EINVAL; |
| 696 | } |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 697 | |
| 698 | /* width must even because of the YUYV format |
| 699 | height must be even because of interlacing */ |
| 700 | height &= 0xfffe; |
Mauro Carvalho Chehab | cf8c91c | 2008-12-16 20:36:13 -0300 | [diff] [blame] | 701 | width &= 0xfffe; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 702 | |
Mauro Carvalho Chehab | cf8c91c | 2008-12-16 20:36:13 -0300 | [diff] [blame] | 703 | if (unlikely(height < 32)) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 704 | height = 32; |
Mauro Carvalho Chehab | cf8c91c | 2008-12-16 20:36:13 -0300 | [diff] [blame] | 705 | if (unlikely(height > maxh)) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 706 | height = maxh; |
Mauro Carvalho Chehab | cf8c91c | 2008-12-16 20:36:13 -0300 | [diff] [blame] | 707 | if (unlikely(width < 48)) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 708 | width = 48; |
Mauro Carvalho Chehab | cf8c91c | 2008-12-16 20:36:13 -0300 | [diff] [blame] | 709 | if (unlikely(width > maxw)) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 710 | width = maxw; |
| 711 | |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 712 | if (dev->board.is_em2800) { |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 713 | /* the em2800 can only scale down to 50% */ |
| 714 | if (height % (maxh / 2)) |
| 715 | height = maxh; |
| 716 | if (width % (maxw / 2)) |
| 717 | width = maxw; |
| 718 | /* according to empiatech support */ |
| 719 | /* the MaxPacketSize is to small to support */ |
| 720 | /* framesizes larger than 640x480 @ 30 fps */ |
| 721 | /* or 640x576 @ 25 fps. As this would cut */ |
| 722 | /* of a part of the image we prefer */ |
| 723 | /* 360x576 or 360x480 for now */ |
| 724 | if (width == maxw && height == maxh) |
| 725 | width /= 2; |
| 726 | } |
| 727 | |
| 728 | get_scale(dev, width, height, &hscale, &vscale); |
| 729 | |
| 730 | width = (((unsigned long)maxw) << 12) / (hscale + 4096L); |
| 731 | height = (((unsigned long)maxh) << 12) / (vscale + 4096L); |
| 732 | |
| 733 | f->fmt.pix.width = width; |
| 734 | f->fmt.pix.height = height; |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 735 | f->fmt.pix.pixelformat = fmt->fourcc; |
| 736 | f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3; |
| 737 | f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 738 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 739 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 740 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 744 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 745 | struct v4l2_format *f) |
| 746 | { |
| 747 | struct em28xx_fh *fh = priv; |
| 748 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 749 | int rc; |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 750 | struct em28xx_fmt *fmt; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 751 | |
| 752 | rc = check_dev(dev); |
| 753 | if (rc < 0) |
| 754 | return rc; |
| 755 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 756 | mutex_lock(&dev->lock); |
| 757 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 758 | vidioc_try_fmt_vid_cap(file, priv, f); |
| 759 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 760 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
Mauro Carvalho Chehab | 5db0b5e | 2008-12-22 06:14:31 -0300 | [diff] [blame] | 761 | if (!fmt) { |
| 762 | rc = -EINVAL; |
| 763 | goto out; |
| 764 | } |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 765 | |
Brandon Philips | 0561297 | 2008-04-13 14:57:01 -0300 | [diff] [blame] | 766 | if (videobuf_queue_is_busy(&fh->vb_vidq)) { |
| 767 | em28xx_errdev("%s queue busy\n", __func__); |
| 768 | rc = -EBUSY; |
| 769 | goto out; |
| 770 | } |
| 771 | |
Aidan Thornton | 0ea13e6 | 2008-04-13 15:02:24 -0300 | [diff] [blame] | 772 | if (dev->stream_on && !fh->stream_on) { |
| 773 | em28xx_errdev("%s device in use by another fh\n", __func__); |
| 774 | rc = -EBUSY; |
| 775 | goto out; |
| 776 | } |
| 777 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 778 | /* set new image size */ |
| 779 | dev->width = f->fmt.pix.width; |
| 780 | dev->height = f->fmt.pix.height; |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 781 | dev->format = fmt; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 782 | get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); |
| 783 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 784 | em28xx_set_alternate(dev); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 785 | em28xx_resolution_set(dev); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 786 | |
Brandon Philips | 0561297 | 2008-04-13 14:57:01 -0300 | [diff] [blame] | 787 | rc = 0; |
| 788 | |
| 789 | out: |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 790 | mutex_unlock(&dev->lock); |
Brandon Philips | 0561297 | 2008-04-13 14:57:01 -0300 | [diff] [blame] | 791 | return rc; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 792 | } |
| 793 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 794 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 795 | { |
| 796 | struct em28xx_fh *fh = priv; |
| 797 | struct em28xx *dev = fh->dev; |
| 798 | struct v4l2_format f; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 799 | int rc; |
| 800 | |
| 801 | rc = check_dev(dev); |
| 802 | if (rc < 0) |
| 803 | return rc; |
| 804 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 805 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 7d497f8 | 2007-11-11 14:15:34 -0300 | [diff] [blame] | 806 | dev->norm = *norm; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 807 | |
| 808 | /* Adjusts width/height, if needed */ |
| 809 | f.fmt.pix.width = dev->width; |
| 810 | f.fmt.pix.height = dev->height; |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 811 | vidioc_try_fmt_vid_cap(file, priv, &f); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 812 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 813 | /* set new image size */ |
| 814 | dev->width = f.fmt.pix.width; |
| 815 | dev->height = f.fmt.pix.height; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 816 | get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); |
| 817 | |
| 818 | em28xx_resolution_set(dev); |
Mauro Carvalho Chehab | 7d497f8 | 2007-11-11 14:15:34 -0300 | [diff] [blame] | 819 | em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 820 | |
| 821 | mutex_unlock(&dev->lock); |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static const char *iname[] = { |
| 826 | [EM28XX_VMUX_COMPOSITE1] = "Composite1", |
| 827 | [EM28XX_VMUX_COMPOSITE2] = "Composite2", |
| 828 | [EM28XX_VMUX_COMPOSITE3] = "Composite3", |
| 829 | [EM28XX_VMUX_COMPOSITE4] = "Composite4", |
| 830 | [EM28XX_VMUX_SVIDEO] = "S-Video", |
| 831 | [EM28XX_VMUX_TELEVISION] = "Television", |
| 832 | [EM28XX_VMUX_CABLE] = "Cable TV", |
| 833 | [EM28XX_VMUX_DVB] = "DVB", |
| 834 | [EM28XX_VMUX_DEBUG] = "for debug only", |
| 835 | }; |
| 836 | |
| 837 | static int vidioc_enum_input(struct file *file, void *priv, |
| 838 | struct v4l2_input *i) |
| 839 | { |
| 840 | struct em28xx_fh *fh = priv; |
| 841 | struct em28xx *dev = fh->dev; |
| 842 | unsigned int n; |
| 843 | |
| 844 | n = i->index; |
| 845 | if (n >= MAX_EM28XX_INPUT) |
| 846 | return -EINVAL; |
| 847 | if (0 == INPUT(n)->type) |
| 848 | return -EINVAL; |
| 849 | |
| 850 | i->index = n; |
| 851 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 852 | |
| 853 | strcpy(i->name, iname[INPUT(n)->type]); |
| 854 | |
| 855 | if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) || |
| 856 | (EM28XX_VMUX_CABLE == INPUT(n)->type)) |
| 857 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 858 | |
Mauro Carvalho Chehab | 7d497f8 | 2007-11-11 14:15:34 -0300 | [diff] [blame] | 859 | i->std = dev->vdev->tvnorms; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 865 | { |
| 866 | struct em28xx_fh *fh = priv; |
| 867 | struct em28xx *dev = fh->dev; |
| 868 | |
| 869 | *i = dev->ctl_input; |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 875 | { |
| 876 | struct em28xx_fh *fh = priv; |
| 877 | struct em28xx *dev = fh->dev; |
| 878 | int rc; |
| 879 | |
| 880 | rc = check_dev(dev); |
| 881 | if (rc < 0) |
| 882 | return rc; |
| 883 | |
| 884 | if (i >= MAX_EM28XX_INPUT) |
| 885 | return -EINVAL; |
| 886 | if (0 == INPUT(i)->type) |
| 887 | return -EINVAL; |
| 888 | |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 889 | dev->ctl_input = i; |
| 890 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 891 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 892 | video_mux(dev, dev->ctl_input); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 893 | mutex_unlock(&dev->lock); |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) |
| 898 | { |
| 899 | struct em28xx_fh *fh = priv; |
| 900 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 901 | |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 902 | switch (a->index) { |
| 903 | case EM28XX_AMUX_VIDEO: |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 904 | strcpy(a->name, "Television"); |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 905 | break; |
| 906 | case EM28XX_AMUX_LINE_IN: |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 907 | strcpy(a->name, "Line In"); |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 908 | break; |
| 909 | case EM28XX_AMUX_VIDEO2: |
| 910 | strcpy(a->name, "Television alt"); |
| 911 | break; |
| 912 | case EM28XX_AMUX_PHONE: |
| 913 | strcpy(a->name, "Phone"); |
| 914 | break; |
| 915 | case EM28XX_AMUX_MIC: |
| 916 | strcpy(a->name, "Mic"); |
| 917 | break; |
| 918 | case EM28XX_AMUX_CD: |
| 919 | strcpy(a->name, "CD"); |
| 920 | break; |
| 921 | case EM28XX_AMUX_AUX: |
| 922 | strcpy(a->name, "Aux"); |
| 923 | break; |
| 924 | case EM28XX_AMUX_PCM_OUT: |
| 925 | strcpy(a->name, "PCM"); |
| 926 | break; |
| 927 | default: |
| 928 | return -EINVAL; |
| 929 | } |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 930 | |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 931 | a->index = dev->ctl_ainput; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 932 | a->capability = V4L2_AUDCAP_STEREO; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a) |
| 938 | { |
| 939 | struct em28xx_fh *fh = priv; |
| 940 | struct em28xx *dev = fh->dev; |
| 941 | |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 942 | |
| 943 | if (a->index >= MAX_EM28XX_INPUT) |
| 944 | return -EINVAL; |
| 945 | if (0 == INPUT(a->index)->type) |
| 946 | return -EINVAL; |
| 947 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 948 | mutex_lock(&dev->lock); |
| 949 | |
Mauro Carvalho Chehab | 35ae6f0 | 2008-11-20 12:40:51 -0300 | [diff] [blame] | 950 | dev->ctl_ainput = INPUT(a->index)->amux; |
| 951 | dev->ctl_aoutput = INPUT(a->index)->aout; |
Mauro Carvalho Chehab | e879b8e | 2008-11-20 13:39:39 -0300 | [diff] [blame] | 952 | |
| 953 | if (!dev->ctl_aoutput) |
| 954 | dev->ctl_aoutput = EM28XX_AOUT_MASTER; |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 955 | |
| 956 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 961 | struct v4l2_queryctrl *qc) |
| 962 | { |
| 963 | struct em28xx_fh *fh = priv; |
| 964 | struct em28xx *dev = fh->dev; |
| 965 | int id = qc->id; |
| 966 | int i; |
| 967 | int rc; |
| 968 | |
| 969 | rc = check_dev(dev); |
| 970 | if (rc < 0) |
| 971 | return rc; |
| 972 | |
| 973 | memset(qc, 0, sizeof(*qc)); |
| 974 | |
| 975 | qc->id = id; |
| 976 | |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 977 | if (!dev->board.has_msp34xx) { |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 978 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { |
| 979 | if (qc->id && qc->id == em28xx_qctrl[i].id) { |
| 980 | memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc)); |
| 981 | return 0; |
| 982 | } |
| 983 | } |
| 984 | } |
| 985 | mutex_lock(&dev->lock); |
| 986 | em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc); |
| 987 | mutex_unlock(&dev->lock); |
| 988 | |
| 989 | if (qc->type) |
| 990 | return 0; |
| 991 | else |
| 992 | return -EINVAL; |
| 993 | } |
| 994 | |
| 995 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 996 | struct v4l2_control *ctrl) |
| 997 | { |
| 998 | struct em28xx_fh *fh = priv; |
| 999 | struct em28xx *dev = fh->dev; |
| 1000 | int rc; |
| 1001 | |
| 1002 | rc = check_dev(dev); |
| 1003 | if (rc < 0) |
| 1004 | return rc; |
Mauro Carvalho Chehab | b6070f0 | 2008-12-22 06:20:32 -0300 | [diff] [blame] | 1005 | rc = 0; |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1006 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1007 | mutex_lock(&dev->lock); |
| 1008 | |
Mauro Carvalho Chehab | b6070f0 | 2008-12-22 06:20:32 -0300 | [diff] [blame] | 1009 | if (dev->board.has_msp34xx) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1010 | em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl); |
Hans Verkuil | 07f7db4 | 2009-01-21 17:06:42 -0300 | [diff] [blame] | 1011 | else { |
Mauro Carvalho Chehab | b6070f0 | 2008-12-22 06:20:32 -0300 | [diff] [blame] | 1012 | rc = em28xx_get_ctrl(dev, ctrl); |
Hans Verkuil | 07f7db4 | 2009-01-21 17:06:42 -0300 | [diff] [blame] | 1013 | if (rc < 0) { |
| 1014 | em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl); |
| 1015 | rc = 0; |
| 1016 | } |
| 1017 | } |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1018 | |
| 1019 | mutex_unlock(&dev->lock); |
| 1020 | return rc; |
| 1021 | } |
| 1022 | |
| 1023 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 1024 | struct v4l2_control *ctrl) |
| 1025 | { |
| 1026 | struct em28xx_fh *fh = priv; |
| 1027 | struct em28xx *dev = fh->dev; |
| 1028 | u8 i; |
| 1029 | int rc; |
| 1030 | |
| 1031 | rc = check_dev(dev); |
| 1032 | if (rc < 0) |
| 1033 | return rc; |
| 1034 | |
| 1035 | mutex_lock(&dev->lock); |
| 1036 | |
Mauro Carvalho Chehab | 505b6d0 | 2008-11-25 09:39:50 -0300 | [diff] [blame] | 1037 | if (dev->board.has_msp34xx) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1038 | em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl); |
| 1039 | else { |
| 1040 | rc = 1; |
| 1041 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { |
| 1042 | if (ctrl->id == em28xx_qctrl[i].id) { |
| 1043 | if (ctrl->value < em28xx_qctrl[i].minimum || |
| 1044 | ctrl->value > em28xx_qctrl[i].maximum) { |
| 1045 | rc = -ERANGE; |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | rc = em28xx_set_ctrl(dev, ctrl); |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | /* Control not found - try to send it to the attached devices */ |
| 1056 | if (rc == 1) { |
| 1057 | em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl); |
| 1058 | rc = 0; |
| 1059 | } |
| 1060 | |
| 1061 | mutex_unlock(&dev->lock); |
| 1062 | return rc; |
| 1063 | } |
| 1064 | |
| 1065 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 1066 | struct v4l2_tuner *t) |
| 1067 | { |
| 1068 | struct em28xx_fh *fh = priv; |
| 1069 | struct em28xx *dev = fh->dev; |
| 1070 | int rc; |
| 1071 | |
| 1072 | rc = check_dev(dev); |
| 1073 | if (rc < 0) |
| 1074 | return rc; |
| 1075 | |
| 1076 | if (0 != t->index) |
| 1077 | return -EINVAL; |
| 1078 | |
| 1079 | strcpy(t->name, "Tuner"); |
| 1080 | |
| 1081 | mutex_lock(&dev->lock); |
| 1082 | |
| 1083 | em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t); |
| 1084 | |
| 1085 | mutex_unlock(&dev->lock); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 1090 | struct v4l2_tuner *t) |
| 1091 | { |
| 1092 | struct em28xx_fh *fh = priv; |
| 1093 | struct em28xx *dev = fh->dev; |
| 1094 | int rc; |
| 1095 | |
| 1096 | rc = check_dev(dev); |
| 1097 | if (rc < 0) |
| 1098 | return rc; |
| 1099 | |
| 1100 | if (0 != t->index) |
| 1101 | return -EINVAL; |
| 1102 | |
| 1103 | mutex_lock(&dev->lock); |
| 1104 | |
| 1105 | em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t); |
| 1106 | |
| 1107 | mutex_unlock(&dev->lock); |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 1112 | struct v4l2_frequency *f) |
| 1113 | { |
| 1114 | struct em28xx_fh *fh = priv; |
| 1115 | struct em28xx *dev = fh->dev; |
| 1116 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1117 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1118 | f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1119 | f->frequency = dev->ctl_freq; |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1120 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1121 | |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 1126 | struct v4l2_frequency *f) |
| 1127 | { |
| 1128 | struct em28xx_fh *fh = priv; |
| 1129 | struct em28xx *dev = fh->dev; |
| 1130 | int rc; |
| 1131 | |
| 1132 | rc = check_dev(dev); |
| 1133 | if (rc < 0) |
| 1134 | return rc; |
| 1135 | |
| 1136 | if (0 != f->tuner) |
| 1137 | return -EINVAL; |
| 1138 | |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1139 | if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) |
| 1140 | return -EINVAL; |
| 1141 | if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1142 | return -EINVAL; |
| 1143 | |
| 1144 | mutex_lock(&dev->lock); |
| 1145 | |
| 1146 | dev->ctl_freq = f->frequency; |
| 1147 | em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f); |
| 1148 | |
| 1149 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1150 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1154 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1155 | static int em28xx_reg_len(int reg) |
| 1156 | { |
| 1157 | switch (reg) { |
Mauro Carvalho Chehab | 41facaa | 2008-04-17 21:44:58 -0300 | [diff] [blame] | 1158 | case EM28XX_R40_AC97LSB: |
| 1159 | case EM28XX_R30_HSCALELOW: |
| 1160 | case EM28XX_R32_VSCALELOW: |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1161 | return 2; |
| 1162 | default: |
| 1163 | return 1; |
| 1164 | } |
| 1165 | } |
| 1166 | |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1167 | static int vidioc_g_chip_ident(struct file *file, void *priv, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1168 | struct v4l2_dbg_chip_ident *chip) |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1169 | { |
| 1170 | struct em28xx_fh *fh = priv; |
| 1171 | struct em28xx *dev = fh->dev; |
| 1172 | |
| 1173 | chip->ident = V4L2_IDENT_NONE; |
| 1174 | chip->revision = 0; |
| 1175 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1176 | em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip); |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1182 | static int vidioc_g_register(struct file *file, void *priv, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1183 | struct v4l2_dbg_register *reg) |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1184 | { |
| 1185 | struct em28xx_fh *fh = priv; |
| 1186 | struct em28xx *dev = fh->dev; |
| 1187 | int ret; |
| 1188 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1189 | switch (reg->match.type) { |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1190 | case V4L2_CHIP_MATCH_AC97: |
Mauro Carvalho Chehab | 531c98e | 2008-12-22 13:18:27 -0300 | [diff] [blame] | 1191 | mutex_lock(&dev->lock); |
| 1192 | ret = em28xx_read_ac97(dev, reg->reg); |
| 1193 | mutex_unlock(&dev->lock); |
| 1194 | if (ret < 0) |
| 1195 | return ret; |
| 1196 | |
| 1197 | reg->val = ret; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1198 | reg->size = 1; |
Mauro Carvalho Chehab | 531c98e | 2008-12-22 13:18:27 -0300 | [diff] [blame] | 1199 | return 0; |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1200 | case V4L2_CHIP_MATCH_I2C_DRIVER: |
| 1201 | em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg); |
| 1202 | return 0; |
| 1203 | case V4L2_CHIP_MATCH_I2C_ADDR: |
| 1204 | /* Not supported yet */ |
| 1205 | return -EINVAL; |
| 1206 | default: |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1207 | if (!v4l2_chip_match_host(®->match)) |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1208 | return -EINVAL; |
Mauro Carvalho Chehab | 531c98e | 2008-12-22 13:18:27 -0300 | [diff] [blame] | 1209 | } |
| 1210 | |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1211 | /* Match host */ |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1212 | reg->size = em28xx_reg_len(reg->reg); |
| 1213 | if (reg->size == 1) { |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1214 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1215 | ret = em28xx_read_reg(dev, reg->reg); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1216 | mutex_unlock(&dev->lock); |
| 1217 | |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1218 | if (ret < 0) |
| 1219 | return ret; |
| 1220 | |
| 1221 | reg->val = ret; |
| 1222 | } else { |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1223 | __le16 val = 0; |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1224 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1225 | ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS, |
| 1226 | reg->reg, (char *)&val, 2); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1227 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1228 | if (ret < 0) |
| 1229 | return ret; |
| 1230 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1231 | reg->val = le16_to_cpu(val); |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static int vidioc_s_register(struct file *file, void *priv, |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1238 | struct v4l2_dbg_register *reg) |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1239 | { |
| 1240 | struct em28xx_fh *fh = priv; |
| 1241 | struct em28xx *dev = fh->dev; |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1242 | __le16 buf; |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1243 | int rc; |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1244 | |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1245 | switch (reg->match.type) { |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1246 | case V4L2_CHIP_MATCH_AC97: |
Mauro Carvalho Chehab | 531c98e | 2008-12-22 13:18:27 -0300 | [diff] [blame] | 1247 | mutex_lock(&dev->lock); |
| 1248 | rc = em28xx_write_ac97(dev, reg->reg, reg->val); |
| 1249 | mutex_unlock(&dev->lock); |
| 1250 | |
| 1251 | return rc; |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1252 | case V4L2_CHIP_MATCH_I2C_DRIVER: |
| 1253 | em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg); |
| 1254 | return 0; |
| 1255 | case V4L2_CHIP_MATCH_I2C_ADDR: |
| 1256 | /* Not supported yet */ |
| 1257 | return -EINVAL; |
| 1258 | default: |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1259 | if (!v4l2_chip_match_host(®->match)) |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1260 | return -EINVAL; |
Mauro Carvalho Chehab | 531c98e | 2008-12-22 13:18:27 -0300 | [diff] [blame] | 1261 | } |
| 1262 | |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1263 | /* Match host */ |
Hans Verkuil | aecde8b5 | 2008-12-30 07:14:19 -0300 | [diff] [blame] | 1264 | buf = cpu_to_le16(reg->val); |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1265 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1266 | mutex_lock(&dev->lock); |
| 1267 | rc = em28xx_write_regs(dev, reg->reg, (char *)&buf, |
| 1268 | em28xx_reg_len(reg->reg)); |
| 1269 | mutex_unlock(&dev->lock); |
| 1270 | |
| 1271 | return rc; |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1272 | } |
| 1273 | #endif |
| 1274 | |
| 1275 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1276 | static int vidioc_cropcap(struct file *file, void *priv, |
| 1277 | struct v4l2_cropcap *cc) |
| 1278 | { |
| 1279 | struct em28xx_fh *fh = priv; |
| 1280 | struct em28xx *dev = fh->dev; |
| 1281 | |
| 1282 | if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1283 | return -EINVAL; |
| 1284 | |
| 1285 | cc->bounds.left = 0; |
| 1286 | cc->bounds.top = 0; |
| 1287 | cc->bounds.width = dev->width; |
| 1288 | cc->bounds.height = dev->height; |
| 1289 | cc->defrect = cc->bounds; |
| 1290 | cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */ |
| 1291 | cc->pixelaspect.denominator = 59; |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | static int vidioc_streamon(struct file *file, void *priv, |
| 1297 | enum v4l2_buf_type type) |
| 1298 | { |
| 1299 | struct em28xx_fh *fh = priv; |
| 1300 | struct em28xx *dev = fh->dev; |
| 1301 | int rc; |
| 1302 | |
| 1303 | rc = check_dev(dev); |
| 1304 | if (rc < 0) |
| 1305 | return rc; |
| 1306 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1307 | |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 1308 | mutex_lock(&dev->lock); |
| 1309 | rc = res_get(fh); |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 1310 | |
Mauro Carvalho Chehab | 5db0b5e | 2008-12-22 06:14:31 -0300 | [diff] [blame] | 1311 | if (likely(rc >= 0)) |
| 1312 | rc = videobuf_streamon(&fh->vb_vidq); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1313 | |
| 1314 | mutex_unlock(&dev->lock); |
| 1315 | |
| 1316 | return rc; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | static int vidioc_streamoff(struct file *file, void *priv, |
| 1320 | enum v4l2_buf_type type) |
| 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 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1330 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1331 | return -EINVAL; |
| 1332 | if (type != fh->type) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1333 | return -EINVAL; |
| 1334 | |
Mauro Carvalho Chehab | 7831364 | 2008-12-16 20:00:49 -0300 | [diff] [blame] | 1335 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1336 | |
| 1337 | videobuf_streamoff(&fh->vb_vidq); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1338 | res_free(fh); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1339 | |
Mauro Carvalho Chehab | 7831364 | 2008-12-16 20:00:49 -0300 | [diff] [blame] | 1340 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1341 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | static int vidioc_querycap(struct file *file, void *priv, |
| 1346 | struct v4l2_capability *cap) |
| 1347 | { |
| 1348 | struct em28xx_fh *fh = priv; |
| 1349 | struct em28xx *dev = fh->dev; |
| 1350 | |
| 1351 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); |
| 1352 | strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 1353 | strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info)); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1354 | |
| 1355 | cap->version = EM28XX_VERSION_CODE; |
| 1356 | |
| 1357 | cap->capabilities = |
| 1358 | V4L2_CAP_SLICED_VBI_CAPTURE | |
| 1359 | V4L2_CAP_VIDEO_CAPTURE | |
| 1360 | V4L2_CAP_AUDIO | |
| 1361 | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; |
| 1362 | |
Mauro Carvalho Chehab | ed08631 | 2008-01-24 06:59:20 -0300 | [diff] [blame] | 1363 | if (dev->tuner_type != TUNER_ABSENT) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1364 | cap->capabilities |= V4L2_CAP_TUNER; |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1369 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 1370 | struct v4l2_fmtdesc *f) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1371 | { |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 1372 | if (unlikely(f->index >= ARRAY_SIZE(format))) |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1373 | return -EINVAL; |
| 1374 | |
Mauro Carvalho Chehab | bddcf63 | 2008-12-20 09:06:37 -0300 | [diff] [blame] | 1375 | strlcpy(f->description, format[f->index].name, sizeof(f->description)); |
| 1376 | f->pixelformat = format[f->index].fourcc; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | /* Sliced VBI ioctls */ |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1382 | static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1383 | struct v4l2_format *f) |
| 1384 | { |
| 1385 | struct em28xx_fh *fh = priv; |
| 1386 | struct em28xx *dev = fh->dev; |
| 1387 | int rc; |
| 1388 | |
| 1389 | rc = check_dev(dev); |
| 1390 | if (rc < 0) |
| 1391 | return rc; |
| 1392 | |
| 1393 | mutex_lock(&dev->lock); |
| 1394 | |
| 1395 | f->fmt.sliced.service_set = 0; |
| 1396 | |
| 1397 | em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f); |
| 1398 | |
| 1399 | if (f->fmt.sliced.service_set == 0) |
| 1400 | rc = -EINVAL; |
| 1401 | |
| 1402 | mutex_unlock(&dev->lock); |
| 1403 | return rc; |
| 1404 | } |
| 1405 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1406 | static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1407 | struct v4l2_format *f) |
| 1408 | { |
| 1409 | struct em28xx_fh *fh = priv; |
| 1410 | struct em28xx *dev = fh->dev; |
| 1411 | int rc; |
| 1412 | |
| 1413 | rc = check_dev(dev); |
| 1414 | if (rc < 0) |
| 1415 | return rc; |
| 1416 | |
| 1417 | mutex_lock(&dev->lock); |
| 1418 | em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f); |
| 1419 | mutex_unlock(&dev->lock); |
| 1420 | |
| 1421 | if (f->fmt.sliced.service_set == 0) |
| 1422 | return -EINVAL; |
| 1423 | |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1429 | struct v4l2_requestbuffers *rb) |
| 1430 | { |
| 1431 | struct em28xx_fh *fh = priv; |
| 1432 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1433 | int rc; |
| 1434 | |
| 1435 | rc = check_dev(dev); |
| 1436 | if (rc < 0) |
| 1437 | return rc; |
| 1438 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1439 | return (videobuf_reqbufs(&fh->vb_vidq, rb)); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1443 | struct v4l2_buffer *b) |
| 1444 | { |
| 1445 | struct em28xx_fh *fh = priv; |
| 1446 | struct em28xx *dev = fh->dev; |
| 1447 | int rc; |
| 1448 | |
| 1449 | rc = check_dev(dev); |
| 1450 | if (rc < 0) |
| 1451 | return rc; |
| 1452 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1453 | return (videobuf_querybuf(&fh->vb_vidq, b)); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) |
| 1457 | { |
| 1458 | struct em28xx_fh *fh = priv; |
| 1459 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1460 | int rc; |
| 1461 | |
| 1462 | rc = check_dev(dev); |
| 1463 | if (rc < 0) |
| 1464 | return rc; |
| 1465 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1466 | return (videobuf_qbuf(&fh->vb_vidq, b)); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) |
| 1470 | { |
| 1471 | struct em28xx_fh *fh = priv; |
| 1472 | struct em28xx *dev = fh->dev; |
| 1473 | int rc; |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1474 | |
| 1475 | rc = check_dev(dev); |
| 1476 | if (rc < 0) |
| 1477 | return rc; |
| 1478 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1479 | return (videobuf_dqbuf(&fh->vb_vidq, b, |
| 1480 | file->f_flags & O_NONBLOCK)); |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1481 | } |
| 1482 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1483 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 1484 | static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf) |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1485 | { |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 1486 | struct em28xx_fh *fh = priv; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1487 | |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 1488 | return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8); |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1489 | } |
| 1490 | #endif |
| 1491 | |
| 1492 | |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1493 | /* ----------------------------------------------------------- */ |
| 1494 | /* RADIO ESPECIFIC IOCTLS */ |
| 1495 | /* ----------------------------------------------------------- */ |
| 1496 | |
| 1497 | static int radio_querycap(struct file *file, void *priv, |
| 1498 | struct v4l2_capability *cap) |
| 1499 | { |
| 1500 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; |
| 1501 | |
| 1502 | strlcpy(cap->driver, "em28xx", sizeof(cap->driver)); |
| 1503 | strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card)); |
Kay Sievers | af128a1 | 2008-10-30 00:51:46 -0300 | [diff] [blame] | 1504 | strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info)); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1505 | |
| 1506 | cap->version = EM28XX_VERSION_CODE; |
| 1507 | cap->capabilities = V4L2_CAP_TUNER; |
| 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | static int radio_g_tuner(struct file *file, void *priv, |
| 1512 | struct v4l2_tuner *t) |
| 1513 | { |
| 1514 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; |
| 1515 | |
| 1516 | if (unlikely(t->index > 0)) |
| 1517 | return -EINVAL; |
| 1518 | |
| 1519 | strcpy(t->name, "Radio"); |
| 1520 | t->type = V4L2_TUNER_RADIO; |
| 1521 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1522 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1523 | em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1524 | mutex_unlock(&dev->lock); |
| 1525 | |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1526 | return 0; |
| 1527 | } |
| 1528 | |
| 1529 | static int radio_enum_input(struct file *file, void *priv, |
| 1530 | struct v4l2_input *i) |
| 1531 | { |
| 1532 | if (i->index != 0) |
| 1533 | return -EINVAL; |
| 1534 | strcpy(i->name, "Radio"); |
| 1535 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a) |
| 1541 | { |
| 1542 | if (unlikely(a->index)) |
| 1543 | return -EINVAL; |
| 1544 | |
| 1545 | strcpy(a->name, "Radio"); |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | static int radio_s_tuner(struct file *file, void *priv, |
| 1550 | struct v4l2_tuner *t) |
| 1551 | { |
| 1552 | struct em28xx *dev = ((struct em28xx_fh *)priv)->dev; |
| 1553 | |
| 1554 | if (0 != t->index) |
| 1555 | return -EINVAL; |
| 1556 | |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1557 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1558 | em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1559 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static int radio_s_audio(struct file *file, void *fh, |
| 1565 | struct v4l2_audio *a) |
| 1566 | { |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | static int radio_s_input(struct file *file, void *fh, unsigned int i) |
| 1571 | { |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | static int radio_queryctrl(struct file *file, void *priv, |
| 1576 | struct v4l2_queryctrl *qc) |
| 1577 | { |
| 1578 | int i; |
| 1579 | |
| 1580 | if (qc->id < V4L2_CID_BASE || |
| 1581 | qc->id >= V4L2_CID_LASTP1) |
| 1582 | return -EINVAL; |
| 1583 | |
| 1584 | for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) { |
| 1585 | if (qc->id && qc->id == em28xx_qctrl[i].id) { |
| 1586 | memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc)); |
| 1587 | return 0; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | return -EINVAL; |
| 1592 | } |
| 1593 | |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1594 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1595 | * em28xx_v4l2_open() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1596 | * inits the device and starts isoc transfer |
| 1597 | */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1598 | static int em28xx_v4l2_open(struct file *filp) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1599 | { |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1600 | int minor = video_devdata(filp)->minor; |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1601 | int errCode = 0, radio; |
| 1602 | struct em28xx *dev; |
| 1603 | enum v4l2_buf_type fh_type; |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1604 | struct em28xx_fh *fh; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 1605 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1606 | dev = em28xx_get_device(minor, &fh_type, &radio); |
Mauro Carvalho Chehab | efc52a9 | 2008-12-16 22:04:56 -0300 | [diff] [blame] | 1607 | |
Mauro Carvalho Chehab | 818a557 | 2008-11-20 10:30:26 -0300 | [diff] [blame] | 1608 | if (NULL == dev) |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1609 | return -ENODEV; |
Mauro Carvalho Chehab | 818a557 | 2008-11-20 10:30:26 -0300 | [diff] [blame] | 1610 | |
| 1611 | mutex_lock(&dev->lock); |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 1612 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1613 | em28xx_videodbg("open minor=%d type=%s users=%d\n", |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 1614 | minor, v4l2_type_names[fh_type], dev->users); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1615 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1616 | |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 1617 | fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL); |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1618 | if (!fh) { |
| 1619 | em28xx_errdev("em28xx-video.c: Out of memory?!\n"); |
Mauro Carvalho Chehab | 818a557 | 2008-11-20 10:30:26 -0300 | [diff] [blame] | 1620 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1621 | return -ENOMEM; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1622 | } |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1623 | fh->dev = dev; |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1624 | fh->radio = radio; |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 1625 | fh->type = fh_type; |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1626 | filp->private_data = fh; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1627 | |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 1628 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1629 | dev->width = norm_maxw(dev); |
| 1630 | dev->height = norm_maxh(dev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1631 | dev->hscale = 0; |
| 1632 | dev->vscale = 0; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1633 | |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 1634 | em28xx_set_mode(dev, EM28XX_ANALOG_MODE); |
Mauro Carvalho Chehab | 3687e1e | 2008-02-08 15:44:25 -0300 | [diff] [blame] | 1635 | em28xx_set_alternate(dev); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1636 | em28xx_resolution_set(dev); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1637 | |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 1638 | /* Needed, since GPIO might have disabled power of |
| 1639 | some i2c device |
| 1640 | */ |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1641 | em28xx_wake_i2c(dev); |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 1642 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1643 | } |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1644 | if (fh->radio) { |
| 1645 | em28xx_videodbg("video_open: setting radio device\n"); |
| 1646 | em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL); |
| 1647 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1648 | |
| 1649 | dev->users++; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1650 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1651 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops, |
| 1652 | NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED, |
| 1653 | sizeof(struct em28xx_buffer), fh); |
| 1654 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1655 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | c67ec53 | 2008-04-17 21:48:00 -0300 | [diff] [blame] | 1656 | |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1657 | return errCode; |
| 1658 | } |
| 1659 | |
| 1660 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1661 | * em28xx_realease_resources() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1662 | * unregisters the v4l2,i2c and usb devices |
| 1663 | * called when the device gets disconected or at module unload |
| 1664 | */ |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1665 | void em28xx_release_analog_resources(struct em28xx *dev) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1666 | { |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1667 | |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1668 | /*FIXME: I2C IR should be disconnected */ |
| 1669 | |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1670 | if (dev->radio_dev) { |
| 1671 | if (-1 != dev->radio_dev->minor) |
| 1672 | video_unregister_device(dev->radio_dev); |
| 1673 | else |
| 1674 | video_device_release(dev->radio_dev); |
| 1675 | dev->radio_dev = NULL; |
| 1676 | } |
| 1677 | if (dev->vbi_dev) { |
Devin Heitmueller | 4924044 | 2008-11-12 02:05:15 -0300 | [diff] [blame] | 1678 | em28xx_info("V4L2 device /dev/vbi%d deregistered\n", |
| 1679 | dev->vbi_dev->num); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1680 | if (-1 != dev->vbi_dev->minor) |
| 1681 | video_unregister_device(dev->vbi_dev); |
| 1682 | else |
| 1683 | video_device_release(dev->vbi_dev); |
| 1684 | dev->vbi_dev = NULL; |
| 1685 | } |
| 1686 | if (dev->vdev) { |
Devin Heitmueller | 4924044 | 2008-11-12 02:05:15 -0300 | [diff] [blame] | 1687 | em28xx_info("V4L2 device /dev/video%d deregistered\n", |
| 1688 | dev->vdev->num); |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1689 | if (-1 != dev->vdev->minor) |
| 1690 | video_unregister_device(dev->vdev); |
| 1691 | else |
| 1692 | video_device_release(dev->vdev); |
| 1693 | dev->vdev = NULL; |
| 1694 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1698 | * em28xx_v4l2_close() |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 1699 | * stops streaming and deallocates all resources allocated by the v4l2 |
| 1700 | * calls and ioctls |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1701 | */ |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1702 | static int em28xx_v4l2_close(struct file *filp) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1703 | { |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1704 | struct em28xx_fh *fh = filp->private_data; |
| 1705 | struct em28xx *dev = fh->dev; |
| 1706 | int errCode; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1707 | |
Mauro Carvalho Chehab | eac9435 | 2005-11-08 21:38:43 -0800 | [diff] [blame] | 1708 | em28xx_videodbg("users=%d\n", dev->users); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1709 | |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 1710 | |
Mauro Carvalho Chehab | 7831364 | 2008-12-16 20:00:49 -0300 | [diff] [blame] | 1711 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 1712 | if (res_check(fh)) |
| 1713 | res_free(fh); |
| 1714 | |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1715 | if (dev->users == 1) { |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1716 | videobuf_stop(&fh->vb_vidq); |
| 1717 | videobuf_mmap_free(&fh->vb_vidq); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1718 | |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1719 | /* the device is already disconnect, |
| 1720 | free the remaining resources */ |
| 1721 | if (dev->state & DEV_DISCONNECTED) { |
| 1722 | em28xx_release_resources(dev); |
| 1723 | mutex_unlock(&dev->lock); |
| 1724 | kfree(dev); |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
Mauro Carvalho Chehab | eb6c963 | 2008-12-05 10:39:12 -0300 | [diff] [blame] | 1728 | /* Save some power by putting tuner to sleep */ |
| 1729 | em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL); |
| 1730 | |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 1731 | /* do this before setting alternate! */ |
| 1732 | em28xx_uninit_isoc(dev); |
Mauro Carvalho Chehab | 2fe3e2e | 2008-11-27 09:10:40 -0300 | [diff] [blame] | 1733 | em28xx_set_mode(dev, EM28XX_SUSPEND); |
Aidan Thornton | d7aa802 | 2008-04-13 14:38:47 -0300 | [diff] [blame] | 1734 | |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1735 | /* set alternate 0 */ |
| 1736 | dev->alt = 0; |
| 1737 | em28xx_videodbg("setting alternate 0\n"); |
| 1738 | errCode = usb_set_interface(dev->udev, 0, 0); |
| 1739 | if (errCode < 0) { |
| 1740 | em28xx_errdev("cannot change alternate number to " |
| 1741 | "0 (error=%i)\n", errCode); |
| 1742 | } |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1743 | } |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1744 | kfree(fh); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1745 | dev->users--; |
| 1746 | wake_up_interruptible_nr(&dev->open, 1); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1747 | mutex_unlock(&dev->lock); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1752 | * em28xx_v4l2_read() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1753 | * will allocate buffers when called for the first time |
| 1754 | */ |
| 1755 | static ssize_t |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 1756 | em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count, |
Mauro Carvalho Chehab | f245e54 | 2008-04-13 14:41:23 -0300 | [diff] [blame] | 1757 | loff_t *pos) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1758 | { |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1759 | struct em28xx_fh *fh = filp->private_data; |
| 1760 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1761 | int rc; |
| 1762 | |
| 1763 | rc = check_dev(dev); |
| 1764 | if (rc < 0) |
| 1765 | return rc; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1766 | |
Mauro Carvalho Chehab | 9e31ced | 2007-11-11 01:13:49 -0300 | [diff] [blame] | 1767 | /* FIXME: read() is not prepared to allow changing the video |
| 1768 | resolution while streaming. Seems a bug at em28xx_set_fmt |
| 1769 | */ |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 1770 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1771 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 1772 | mutex_lock(&dev->lock); |
| 1773 | rc = res_get(fh); |
| 1774 | mutex_unlock(&dev->lock); |
| 1775 | |
| 1776 | if (unlikely(rc < 0)) |
| 1777 | return rc; |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 1778 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1779 | return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, |
| 1780 | filp->f_flags & O_NONBLOCK); |
Mauro Carvalho Chehab | e5589be | 2006-01-23 17:11:08 -0200 | [diff] [blame] | 1781 | } |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1782 | return 0; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1786 | * em28xx_v4l2_poll() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1787 | * will allocate buffers when called for the first time |
| 1788 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1789 | static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1790 | { |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1791 | struct em28xx_fh *fh = filp->private_data; |
| 1792 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1793 | int rc; |
| 1794 | |
| 1795 | rc = check_dev(dev); |
| 1796 | if (rc < 0) |
| 1797 | return rc; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1798 | |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 1799 | mutex_lock(&dev->lock); |
| 1800 | rc = res_get(fh); |
| 1801 | mutex_unlock(&dev->lock); |
| 1802 | |
| 1803 | if (unlikely(rc < 0)) |
Mauro Carvalho Chehab | a225452 | 2007-11-11 01:08:26 -0300 | [diff] [blame] | 1804 | return POLLERR; |
| 1805 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1806 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) |
| 1807 | return POLLERR; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1808 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1809 | return videobuf_poll_stream(filp, &fh->vb_vidq, wait); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /* |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1813 | * em28xx_v4l2_mmap() |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1814 | */ |
Mauro Carvalho Chehab | 3acf280 | 2005-11-08 21:38:27 -0800 | [diff] [blame] | 1815 | static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1816 | { |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1817 | struct em28xx_fh *fh = filp->private_data; |
| 1818 | struct em28xx *dev = fh->dev; |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1819 | int rc; |
Markus Rechberger | 9c75541 | 2005-11-08 21:37:52 -0800 | [diff] [blame] | 1820 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1821 | rc = check_dev(dev); |
| 1822 | if (rc < 0) |
| 1823 | return rc; |
Mauro Carvalho Chehab | a3a048c | 2007-11-10 22:21:01 -0300 | [diff] [blame] | 1824 | |
Mauro Carvalho Chehab | 29b5941 | 2008-12-16 20:19:24 -0300 | [diff] [blame] | 1825 | mutex_lock(&dev->lock); |
| 1826 | rc = res_get(fh); |
| 1827 | mutex_unlock(&dev->lock); |
| 1828 | |
| 1829 | if (unlikely(rc < 0)) |
| 1830 | return rc; |
| 1831 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1832 | rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1833 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1834 | em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n", |
| 1835 | (unsigned long)vma->vm_start, |
| 1836 | (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, |
| 1837 | rc); |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1838 | |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1839 | return rc; |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1840 | } |
| 1841 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1842 | static const struct v4l2_file_operations em28xx_v4l_fops = { |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1843 | .owner = THIS_MODULE, |
| 1844 | .open = em28xx_v4l2_open, |
| 1845 | .release = em28xx_v4l2_close, |
| 1846 | .read = em28xx_v4l2_read, |
| 1847 | .poll = em28xx_v4l2_poll, |
| 1848 | .mmap = em28xx_v4l2_mmap, |
| 1849 | .ioctl = video_ioctl2, |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1850 | }; |
| 1851 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1852 | static const struct v4l2_ioctl_ops video_ioctl_ops = { |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1853 | .vidioc_querycap = vidioc_querycap, |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1854 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1855 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1856 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1857 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1858 | .vidioc_g_audio = vidioc_g_audio, |
| 1859 | .vidioc_s_audio = vidioc_s_audio, |
| 1860 | .vidioc_cropcap = vidioc_cropcap, |
| 1861 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1862 | .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap, |
| 1863 | .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, |
| 1864 | .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1865 | |
| 1866 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1867 | .vidioc_querybuf = vidioc_querybuf, |
| 1868 | .vidioc_qbuf = vidioc_qbuf, |
| 1869 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1870 | .vidioc_s_std = vidioc_s_std, |
| 1871 | .vidioc_enum_input = vidioc_enum_input, |
| 1872 | .vidioc_g_input = vidioc_g_input, |
| 1873 | .vidioc_s_input = vidioc_s_input, |
| 1874 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1875 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1876 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1877 | .vidioc_streamon = vidioc_streamon, |
| 1878 | .vidioc_streamoff = vidioc_streamoff, |
| 1879 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1880 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1881 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1882 | .vidioc_s_frequency = vidioc_s_frequency, |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1883 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1884 | .vidioc_g_register = vidioc_g_register, |
| 1885 | .vidioc_s_register = vidioc_s_register, |
Mauro Carvalho Chehab | 14983d8 | 2008-12-22 20:58:41 -0300 | [diff] [blame] | 1886 | .vidioc_g_chip_ident = vidioc_g_chip_ident, |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1887 | #endif |
Mauro Carvalho Chehab | ad0ebb9 | 2008-04-13 14:37:52 -0300 | [diff] [blame] | 1888 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1889 | .vidiocgmbuf = vidiocgmbuf, |
| 1890 | #endif |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1891 | }; |
| 1892 | |
| 1893 | static const struct video_device em28xx_video_template = { |
| 1894 | .fops = &em28xx_v4l_fops, |
| 1895 | .release = video_device_release, |
| 1896 | .ioctl_ops = &video_ioctl_ops, |
| 1897 | |
| 1898 | .minor = -1, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1899 | |
| 1900 | .tvnorms = V4L2_STD_ALL, |
Mauro Carvalho Chehab | 7d497f8 | 2007-11-11 14:15:34 -0300 | [diff] [blame] | 1901 | .current_norm = V4L2_STD_PAL, |
Mauro Carvalho Chehab | 195a4ef | 2007-11-11 13:17:17 -0300 | [diff] [blame] | 1902 | }; |
| 1903 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1904 | static const struct v4l2_file_operations radio_fops = { |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1905 | .owner = THIS_MODULE, |
| 1906 | .open = em28xx_v4l2_open, |
| 1907 | .release = em28xx_v4l2_close, |
| 1908 | .ioctl = video_ioctl2, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1909 | }; |
| 1910 | |
| 1911 | static const struct v4l2_ioctl_ops radio_ioctl_ops = { |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1912 | .vidioc_querycap = radio_querycap, |
| 1913 | .vidioc_g_tuner = radio_g_tuner, |
| 1914 | .vidioc_enum_input = radio_enum_input, |
| 1915 | .vidioc_g_audio = radio_g_audio, |
| 1916 | .vidioc_s_tuner = radio_s_tuner, |
| 1917 | .vidioc_s_audio = radio_s_audio, |
| 1918 | .vidioc_s_input = radio_s_input, |
| 1919 | .vidioc_queryctrl = radio_queryctrl, |
| 1920 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1921 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1922 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1923 | .vidioc_s_frequency = vidioc_s_frequency, |
Mauro Carvalho Chehab | 1e7ad56 | 2008-02-06 09:00:41 -0300 | [diff] [blame] | 1924 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
| 1925 | .vidioc_g_register = vidioc_g_register, |
| 1926 | .vidioc_s_register = vidioc_s_register, |
| 1927 | #endif |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1928 | }; |
| 1929 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1930 | static struct video_device em28xx_radio_template = { |
| 1931 | .name = "em28xx-radio", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1932 | .fops = &radio_fops, |
| 1933 | .ioctl_ops = &radio_ioctl_ops, |
| 1934 | .minor = -1, |
| 1935 | }; |
| 1936 | |
Douglas Schilling Landgraf | 6ea54d9 | 2008-04-17 21:41:10 -0300 | [diff] [blame] | 1937 | /******************************** usb interface ******************************/ |
akpm@osdl.org | a6c2ba2 | 2005-11-08 21:37:07 -0800 | [diff] [blame] | 1938 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 1939 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 1940 | |
Adrian Bunk | 532fe65 | 2008-01-28 22:10:48 -0300 | [diff] [blame] | 1941 | static struct video_device *em28xx_vdev_init(struct em28xx *dev, |
| 1942 | const struct video_device *template, |
Adrian Bunk | 532fe65 | 2008-01-28 22:10:48 -0300 | [diff] [blame] | 1943 | const char *type_name) |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1944 | { |
| 1945 | struct video_device *vfd; |
| 1946 | |
| 1947 | vfd = video_device_alloc(); |
| 1948 | if (NULL == vfd) |
| 1949 | return NULL; |
| 1950 | *vfd = *template; |
| 1951 | vfd->minor = -1; |
Hans Verkuil | 5e85e73 | 2008-07-20 06:31:39 -0300 | [diff] [blame] | 1952 | vfd->parent = &dev->udev->dev; |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1953 | vfd->release = video_device_release; |
Mauro Carvalho Chehab | e9e6040 | 2008-04-13 15:05:47 -0300 | [diff] [blame] | 1954 | vfd->debug = video_debug; |
Mauro Carvalho Chehab | 0be4375 | 2008-01-05 17:22:01 -0300 | [diff] [blame] | 1955 | |
| 1956 | snprintf(vfd->name, sizeof(vfd->name), "%s %s", |
| 1957 | dev->name, type_name); |
| 1958 | |
| 1959 | return vfd; |
| 1960 | } |
| 1961 | |
Mauro Carvalho Chehab | 2e5ef2d | 2008-12-28 22:26:36 -0300 | [diff] [blame] | 1962 | int em28xx_register_analog_devices(struct em28xx *dev) |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1963 | { |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 1964 | u8 val; |
Mauro Carvalho Chehab | 2e5ef2d | 2008-12-28 22:26:36 -0300 | [diff] [blame] | 1965 | int ret; |
| 1966 | |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1967 | printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n", |
| 1968 | dev->name, |
| 1969 | (EM28XX_VERSION_CODE >> 16) & 0xff, |
| 1970 | (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff); |
| 1971 | |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1972 | /* set default norm */ |
| 1973 | dev->norm = em28xx_video_template.current_norm; |
| 1974 | dev->width = norm_maxw(dev); |
| 1975 | dev->height = norm_maxh(dev); |
| 1976 | dev->interlaced = EM28XX_INTERLACED_DEFAULT; |
| 1977 | dev->hscale = 0; |
| 1978 | dev->vscale = 0; |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 1979 | dev->ctl_input = 0; |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1980 | |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 1981 | /* Analog specific initialization */ |
| 1982 | dev->format = &format[0]; |
| 1983 | video_mux(dev, dev->ctl_input); |
| 1984 | |
| 1985 | /* Audio defaults */ |
| 1986 | dev->mute = 1; |
| 1987 | dev->volume = 0x1f; |
| 1988 | |
| 1989 | /* enable vbi capturing */ |
| 1990 | |
| 1991 | /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */ |
Robert Krakora | 6e7b9ea | 2009-01-18 21:59:34 -0300 | [diff] [blame] | 1992 | val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK); |
| 1993 | em28xx_write_reg(dev, EM28XX_R0F_XCLK, (EM28XX_XCLK_AUDIO_UNMUTE | val)); |
Mauro Carvalho Chehab | 24c3c41 | 2009-01-07 22:49:25 -0300 | [diff] [blame] | 1994 | em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51); |
| 1995 | |
| 1996 | em28xx_set_outfmt(dev); |
| 1997 | em28xx_colorlevels_set_default(dev); |
| 1998 | em28xx_compression_disable(dev); |
Mauro Carvalho Chehab | 1a23f81 | 2008-12-28 22:18:14 -0300 | [diff] [blame] | 1999 | |
Mauro Carvalho Chehab | 818a557 | 2008-11-20 10:30:26 -0300 | [diff] [blame] | 2000 | /* allocate and fill video video_device struct */ |
| 2001 | dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video"); |
| 2002 | if (!dev->vdev) { |
| 2003 | em28xx_errdev("cannot allocate video_device.\n"); |
| 2004 | return -ENODEV; |
| 2005 | } |
| 2006 | |
| 2007 | /* register v4l2 video video_device */ |
| 2008 | ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER, |
| 2009 | video_nr[dev->devno]); |
| 2010 | if (ret) { |
| 2011 | em28xx_errdev("unable to register video device (error=%i).\n", |
| 2012 | ret); |
| 2013 | return ret; |
| 2014 | } |
| 2015 | |
| 2016 | /* Allocate and fill vbi video_device struct */ |
| 2017 | dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi"); |
| 2018 | |
| 2019 | /* register v4l2 vbi video_device */ |
| 2020 | ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, |
| 2021 | vbi_nr[dev->devno]); |
| 2022 | if (ret < 0) { |
| 2023 | em28xx_errdev("unable to register vbi device\n"); |
| 2024 | return ret; |
| 2025 | } |
| 2026 | |
| 2027 | if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) { |
| 2028 | dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio"); |
| 2029 | if (!dev->radio_dev) { |
| 2030 | em28xx_errdev("cannot allocate video_device.\n"); |
| 2031 | return -ENODEV; |
| 2032 | } |
| 2033 | ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO, |
| 2034 | radio_nr[dev->devno]); |
| 2035 | if (ret < 0) { |
| 2036 | em28xx_errdev("can't register radio device\n"); |
| 2037 | return ret; |
| 2038 | } |
| 2039 | em28xx_info("Registered radio device as /dev/radio%d\n", |
| 2040 | dev->radio_dev->num); |
| 2041 | } |
| 2042 | |
| 2043 | em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n", |
| 2044 | dev->vdev->num, dev->vbi_dev->num); |
| 2045 | |
| 2046 | return 0; |
| 2047 | } |