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