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