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