Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1 | /* |
| 2 | * s2255drv.c - a driver for the Sensoray 2255 USB video capture device |
| 3 | * |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 4 | * Copyright (C) 2007-2010 by Sensoray Company Inc. |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 5 | * Dean Anderson |
| 6 | * |
| 7 | * Some video buffer code based on vivi driver: |
| 8 | * |
| 9 | * Sensoray 2255 device supports 4 simultaneous channels. |
| 10 | * The channels are not "crossbar" inputs, they are physically |
| 11 | * attached to separate video decoders. |
| 12 | * |
| 13 | * Because of USB2.0 bandwidth limitations. There is only a |
| 14 | * certain amount of data which may be transferred at one time. |
| 15 | * |
| 16 | * Example maximum bandwidth utilization: |
| 17 | * |
| 18 | * -full size, color mode YUYV or YUV422P: 2 channels at once |
| 19 | * |
| 20 | * -full or half size Grey scale: all 4 channels at once |
| 21 | * |
| 22 | * -half size, color mode YUYV or YUV422P: all 4 channels at once |
| 23 | * |
| 24 | * -full size, color mode YUYV or YUV422P 1/2 frame rate: all 4 channels |
| 25 | * at once. |
| 26 | * (TODO: Incorporate videodev2 frame rate(FR) enumeration, |
| 27 | * which is currently experimental.) |
| 28 | * |
| 29 | * This program is free software; you can redistribute it and/or modify |
| 30 | * it under the terms of the GNU General Public License as published by |
| 31 | * the Free Software Foundation; either version 2 of the License, or |
| 32 | * (at your option) any later version. |
| 33 | * |
| 34 | * This program is distributed in the hope that it will be useful, |
| 35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | * GNU General Public License for more details. |
| 38 | * |
| 39 | * You should have received a copy of the GNU General Public License |
| 40 | * along with this program; if not, write to the Free Software |
| 41 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 42 | */ |
| 43 | |
| 44 | #include <linux/module.h> |
| 45 | #include <linux/firmware.h> |
| 46 | #include <linux/kernel.h> |
| 47 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 48 | #include <linux/slab.h> |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 49 | #include <linux/videodev2.h> |
| 50 | #include <linux/version.h> |
Hans Verkuil | feb75f0 | 2008-07-27 06:30:21 -0300 | [diff] [blame] | 51 | #include <linux/mm.h> |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 52 | #include <media/videobuf-vmalloc.h> |
| 53 | #include <media/v4l2-common.h> |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 54 | #include <media/v4l2-device.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 55 | #include <media/v4l2-ioctl.h> |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 56 | #include <linux/vmalloc.h> |
| 57 | #include <linux/usb.h> |
| 58 | |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 59 | #define S2255_MAJOR_VERSION 1 |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 60 | #define S2255_MINOR_VERSION 20 |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 61 | #define S2255_RELEASE 0 |
| 62 | #define S2255_VERSION KERNEL_VERSION(S2255_MAJOR_VERSION, \ |
| 63 | S2255_MINOR_VERSION, \ |
| 64 | S2255_RELEASE) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 65 | #define FIRMWARE_FILE_NAME "f2255usb.bin" |
| 66 | |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 67 | /* default JPEG quality */ |
| 68 | #define S2255_DEF_JPEG_QUAL 50 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 69 | /* vendor request in */ |
| 70 | #define S2255_VR_IN 0 |
| 71 | /* vendor request out */ |
| 72 | #define S2255_VR_OUT 1 |
| 73 | /* firmware query */ |
| 74 | #define S2255_VR_FW 0x30 |
| 75 | /* USB endpoint number for configuring the device */ |
| 76 | #define S2255_CONFIG_EP 2 |
| 77 | /* maximum time for DSP to start responding after last FW word loaded(ms) */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 78 | #define S2255_DSP_BOOTTIME 800 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 79 | /* maximum time to wait for firmware to load (ms) */ |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 80 | #define S2255_LOAD_TIMEOUT (5000 + S2255_DSP_BOOTTIME) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 81 | #define S2255_DEF_BUFS 16 |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 82 | #define S2255_SETMODE_TIMEOUT 500 |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 83 | #define S2255_VIDSTATUS_TIMEOUT 350 |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 84 | #define S2255_MARKER_FRAME cpu_to_le32(0x2255DA4AL) |
| 85 | #define S2255_MARKER_RESPONSE cpu_to_le32(0x2255ACACL) |
| 86 | #define S2255_RESPONSE_SETMODE cpu_to_le32(0x01) |
| 87 | #define S2255_RESPONSE_FW cpu_to_le32(0x10) |
| 88 | #define S2255_RESPONSE_STATUS cpu_to_le32(0x20) |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 89 | #define S2255_USB_XFER_SIZE (16 * 1024) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 90 | #define MAX_CHANNELS 4 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 91 | #define SYS_FRAMES 4 |
| 92 | /* maximum size is PAL full size plus room for the marker header(s) */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 93 | #define SYS_FRAMES_MAXSIZE (720*288*2*2 + 4096) |
| 94 | #define DEF_USB_BLOCK S2255_USB_XFER_SIZE |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 95 | #define LINE_SZ_4CIFS_NTSC 640 |
| 96 | #define LINE_SZ_2CIFS_NTSC 640 |
| 97 | #define LINE_SZ_1CIFS_NTSC 320 |
| 98 | #define LINE_SZ_4CIFS_PAL 704 |
| 99 | #define LINE_SZ_2CIFS_PAL 704 |
| 100 | #define LINE_SZ_1CIFS_PAL 352 |
| 101 | #define NUM_LINES_4CIFS_NTSC 240 |
| 102 | #define NUM_LINES_2CIFS_NTSC 240 |
| 103 | #define NUM_LINES_1CIFS_NTSC 240 |
| 104 | #define NUM_LINES_4CIFS_PAL 288 |
| 105 | #define NUM_LINES_2CIFS_PAL 288 |
| 106 | #define NUM_LINES_1CIFS_PAL 288 |
| 107 | #define LINE_SZ_DEF 640 |
| 108 | #define NUM_LINES_DEF 240 |
| 109 | |
| 110 | |
| 111 | /* predefined settings */ |
| 112 | #define FORMAT_NTSC 1 |
| 113 | #define FORMAT_PAL 2 |
| 114 | |
| 115 | #define SCALE_4CIFS 1 /* 640x480(NTSC) or 704x576(PAL) */ |
| 116 | #define SCALE_2CIFS 2 /* 640x240(NTSC) or 704x288(PAL) */ |
| 117 | #define SCALE_1CIFS 3 /* 320x240(NTSC) or 352x288(PAL) */ |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 118 | /* SCALE_4CIFSI is the 2 fields interpolated into one */ |
| 119 | #define SCALE_4CIFSI 4 /* 640x480(NTSC) or 704x576(PAL) high quality */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 120 | |
| 121 | #define COLOR_YUVPL 1 /* YUV planar */ |
| 122 | #define COLOR_YUVPK 2 /* YUV packed */ |
| 123 | #define COLOR_Y8 4 /* monochrome */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 124 | #define COLOR_JPG 5 /* JPEG */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 125 | |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 126 | #define MASK_COLOR 0x000000ff |
| 127 | #define MASK_JPG_QUALITY 0x0000ff00 |
| 128 | #define MASK_INPUT_TYPE 0x000f0000 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 129 | /* frame decimation. Not implemented by V4L yet(experimental in V4L) */ |
| 130 | #define FDEC_1 1 /* capture every frame. default */ |
| 131 | #define FDEC_2 2 /* capture every 2nd frame */ |
| 132 | #define FDEC_3 3 /* capture every 3rd frame */ |
| 133 | #define FDEC_5 5 /* capture every 5th frame */ |
| 134 | |
| 135 | /*------------------------------------------------------- |
| 136 | * Default mode parameters. |
| 137 | *-------------------------------------------------------*/ |
| 138 | #define DEF_SCALE SCALE_4CIFS |
| 139 | #define DEF_COLOR COLOR_YUVPL |
| 140 | #define DEF_FDEC FDEC_1 |
| 141 | #define DEF_BRIGHT 0 |
| 142 | #define DEF_CONTRAST 0x5c |
| 143 | #define DEF_SATURATION 0x80 |
| 144 | #define DEF_HUE 0 |
| 145 | |
| 146 | /* usb config commands */ |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 147 | #define IN_DATA_TOKEN cpu_to_le32(0x2255c0de) |
| 148 | #define CMD_2255 cpu_to_le32(0xc2255000) |
| 149 | #define CMD_SET_MODE cpu_to_le32((CMD_2255 | 0x10)) |
| 150 | #define CMD_START cpu_to_le32((CMD_2255 | 0x20)) |
| 151 | #define CMD_STOP cpu_to_le32((CMD_2255 | 0x30)) |
| 152 | #define CMD_STATUS cpu_to_le32((CMD_2255 | 0x40)) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 153 | |
| 154 | struct s2255_mode { |
| 155 | u32 format; /* input video format (NTSC, PAL) */ |
| 156 | u32 scale; /* output video scale */ |
| 157 | u32 color; /* output video color format */ |
| 158 | u32 fdec; /* frame decimation */ |
| 159 | u32 bright; /* brightness */ |
| 160 | u32 contrast; /* contrast */ |
| 161 | u32 saturation; /* saturation */ |
| 162 | u32 hue; /* hue (NTSC only)*/ |
| 163 | u32 single; /* capture 1 frame at a time (!=0), continuously (==0)*/ |
| 164 | u32 usb_block; /* block size. should be 4096 of DEF_USB_BLOCK */ |
| 165 | u32 restart; /* if DSP requires restart */ |
| 166 | }; |
| 167 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 168 | |
| 169 | #define S2255_READ_IDLE 0 |
| 170 | #define S2255_READ_FRAME 1 |
| 171 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 172 | /* frame structure */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 173 | struct s2255_framei { |
| 174 | unsigned long size; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 175 | unsigned long ulState; /* ulState:S2255_READ_IDLE, S2255_READ_FRAME*/ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 176 | void *lpvbits; /* image data */ |
| 177 | unsigned long cur_size; /* current data copied to it */ |
| 178 | }; |
| 179 | |
| 180 | /* image buffer structure */ |
| 181 | struct s2255_bufferi { |
| 182 | unsigned long dwFrames; /* number of frames in buffer */ |
| 183 | struct s2255_framei frame[SYS_FRAMES]; /* array of FRAME structures */ |
| 184 | }; |
| 185 | |
| 186 | #define DEF_MODEI_NTSC_CONT {FORMAT_NTSC, DEF_SCALE, DEF_COLOR, \ |
| 187 | DEF_FDEC, DEF_BRIGHT, DEF_CONTRAST, DEF_SATURATION, \ |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 188 | DEF_HUE, 0, DEF_USB_BLOCK, 0} |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 189 | |
| 190 | struct s2255_dmaqueue { |
| 191 | struct list_head active; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 192 | struct s2255_dev *dev; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | /* for firmware loading, fw_state */ |
| 196 | #define S2255_FW_NOTLOADED 0 |
| 197 | #define S2255_FW_LOADED_DSPWAIT 1 |
| 198 | #define S2255_FW_SUCCESS 2 |
| 199 | #define S2255_FW_FAILED 3 |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 200 | #define S2255_FW_DISCONNECTING 4 |
Harvey Harrison | deaf53e | 2008-11-15 01:10:14 -0300 | [diff] [blame] | 201 | #define S2255_FW_MARKER cpu_to_le32(0x22552f2f) |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 202 | /* 2255 read states */ |
| 203 | #define S2255_READ_IDLE 0 |
| 204 | #define S2255_READ_FRAME 1 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 205 | struct s2255_fw { |
| 206 | int fw_loaded; |
| 207 | int fw_size; |
| 208 | struct urb *fw_urb; |
| 209 | atomic_t fw_state; |
| 210 | void *pfw_data; |
| 211 | wait_queue_head_t wait_fw; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 212 | const struct firmware *fw; |
| 213 | }; |
| 214 | |
| 215 | struct s2255_pipeinfo { |
| 216 | u32 max_transfer_size; |
| 217 | u32 cur_transfer_size; |
| 218 | u8 *transfer_buffer; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 219 | u32 state; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 220 | void *stream_urb; |
| 221 | void *dev; /* back pointer to s2255_dev struct*/ |
| 222 | u32 err_count; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 223 | u32 idx; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | struct s2255_fmt; /*forward declaration */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 227 | struct s2255_dev; |
| 228 | |
| 229 | struct s2255_channel { |
| 230 | struct video_device vdev; |
| 231 | int resources; |
| 232 | struct s2255_dmaqueue vidq; |
| 233 | struct s2255_bufferi buffer; |
| 234 | struct s2255_mode mode; |
| 235 | /* jpeg compression */ |
| 236 | struct v4l2_jpegcompression jc; |
| 237 | /* capture parameters (for high quality mode full size) */ |
| 238 | struct v4l2_captureparm cap_parm; |
| 239 | int cur_frame; |
| 240 | int last_frame; |
| 241 | |
| 242 | int b_acquire; |
| 243 | /* allocated image size */ |
| 244 | unsigned long req_image_size; |
| 245 | /* received packet size */ |
| 246 | unsigned long pkt_size; |
| 247 | int bad_payload; |
| 248 | unsigned long frame_count; |
| 249 | /* if JPEG image */ |
| 250 | int jpg_size; |
| 251 | /* if channel configured to default state */ |
| 252 | int configured; |
| 253 | wait_queue_head_t wait_setmode; |
| 254 | int setmode_ready; |
| 255 | /* video status items */ |
| 256 | int vidstatus; |
| 257 | wait_queue_head_t wait_vidstatus; |
| 258 | int vidstatus_ready; |
| 259 | unsigned int width; |
| 260 | unsigned int height; |
| 261 | const struct s2255_fmt *fmt; |
| 262 | int idx; /* channel number on device, 0-3 */ |
| 263 | }; |
| 264 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 265 | |
| 266 | struct s2255_dev { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 267 | struct s2255_channel channel[MAX_CHANNELS]; |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 268 | struct v4l2_device v4l2_dev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 269 | atomic_t num_channels; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 270 | int frames; |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 271 | struct mutex lock; /* channels[].vdev.lock */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 272 | struct mutex open_lock; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 273 | struct usb_device *udev; |
| 274 | struct usb_interface *interface; |
| 275 | u8 read_endpoint; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 276 | struct timer_list timer; |
| 277 | struct s2255_fw *fw_data; |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 278 | struct s2255_pipeinfo pipe; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 279 | u32 cc; /* current channel */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 280 | int frame_ready; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 281 | int chn_ready; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 282 | spinlock_t slock; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 283 | /* dsp firmware version (f2255usb.bin) */ |
| 284 | int dsp_fw_ver; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 285 | u16 pid; /* product id */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 286 | }; |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 287 | |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 288 | static inline struct s2255_dev *to_s2255_dev(struct v4l2_device *v4l2_dev) |
| 289 | { |
| 290 | return container_of(v4l2_dev, struct s2255_dev, v4l2_dev); |
| 291 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 292 | |
| 293 | struct s2255_fmt { |
| 294 | char *name; |
| 295 | u32 fourcc; |
| 296 | int depth; |
| 297 | }; |
| 298 | |
| 299 | /* buffer for one video frame */ |
| 300 | struct s2255_buffer { |
| 301 | /* common v4l buffer stuff -- must be first */ |
| 302 | struct videobuf_buffer vb; |
| 303 | const struct s2255_fmt *fmt; |
| 304 | }; |
| 305 | |
| 306 | struct s2255_fh { |
| 307 | struct s2255_dev *dev; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 308 | struct videobuf_queue vb_vidq; |
| 309 | enum v4l2_buf_type type; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 310 | struct s2255_channel *channel; |
| 311 | int resources; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 312 | }; |
| 313 | |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 314 | /* current cypress EEPROM firmware version */ |
| 315 | #define S2255_CUR_USB_FWVER ((3 << 8) | 6) |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 316 | /* current DSP FW version */ |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 317 | #define S2255_CUR_DSP_FWVER 8 |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 318 | /* Need DSP version 5+ for video status feature */ |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 319 | #define S2255_MIN_DSP_STATUS 5 |
| 320 | #define S2255_MIN_DSP_COLORFILTER 8 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 321 | #define S2255_NORMS (V4L2_STD_PAL | V4L2_STD_NTSC) |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 322 | |
| 323 | /* private V4L2 controls */ |
| 324 | |
| 325 | /* |
| 326 | * The following chart displays how COLORFILTER should be set |
| 327 | * ========================================================= |
| 328 | * = fourcc = COLORFILTER = |
| 329 | * = =============================== |
| 330 | * = = 0 = 1 = |
| 331 | * ========================================================= |
| 332 | * = V4L2_PIX_FMT_GREY(Y8) = monochrome from = monochrome= |
| 333 | * = = s-video or = composite = |
| 334 | * = = B/W camera = input = |
| 335 | * ========================================================= |
| 336 | * = other = color, svideo = color, = |
| 337 | * = = = composite = |
| 338 | * ========================================================= |
| 339 | * |
| 340 | * Notes: |
| 341 | * channels 0-3 on 2255 are composite |
| 342 | * channels 0-1 on 2257 are composite, 2-3 are s-video |
| 343 | * If COLORFILTER is 0 with a composite color camera connected, |
| 344 | * the output will appear monochrome but hatching |
| 345 | * will occur. |
| 346 | * COLORFILTER is different from "color killer" and "color effects" |
| 347 | * for reasons above. |
| 348 | */ |
| 349 | #define S2255_V4L2_YC_ON 1 |
| 350 | #define S2255_V4L2_YC_OFF 0 |
| 351 | #define V4L2_CID_PRIVATE_COLORFILTER (V4L2_CID_PRIVATE_BASE + 0) |
| 352 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 353 | /* frame prefix size (sent once every frame) */ |
| 354 | #define PREFIX_SIZE 512 |
| 355 | |
| 356 | /* Channels on box are in reverse order */ |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 357 | static unsigned long G_chnmap[MAX_CHANNELS] = {3, 2, 1, 0}; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 358 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 359 | static int debug; |
| 360 | static int *s2255_debug = &debug; |
| 361 | |
| 362 | static int s2255_start_readpipe(struct s2255_dev *dev); |
| 363 | static void s2255_stop_readpipe(struct s2255_dev *dev); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 364 | static int s2255_start_acquire(struct s2255_channel *channel); |
| 365 | static int s2255_stop_acquire(struct s2255_channel *channel); |
| 366 | static void s2255_fillbuff(struct s2255_channel *chn, struct s2255_buffer *buf, |
| 367 | int jpgsize); |
| 368 | static int s2255_set_mode(struct s2255_channel *chan, struct s2255_mode *mode); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 369 | static int s2255_board_shutdown(struct s2255_dev *dev); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 370 | static void s2255_fwload_start(struct s2255_dev *dev, int reset); |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 371 | static void s2255_destroy(struct s2255_dev *dev); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 372 | static long s2255_vendor_req(struct s2255_dev *dev, unsigned char req, |
| 373 | u16 index, u16 value, void *buf, |
| 374 | s32 buf_len, int bOut); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 375 | |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 376 | /* dev_err macro with driver name */ |
| 377 | #define S2255_DRIVER_NAME "s2255" |
| 378 | #define s2255_dev_err(dev, fmt, arg...) \ |
| 379 | dev_err(dev, S2255_DRIVER_NAME " - " fmt, ##arg) |
| 380 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 381 | #define dprintk(level, fmt, arg...) \ |
| 382 | do { \ |
| 383 | if (*s2255_debug >= (level)) { \ |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 384 | printk(KERN_DEBUG S2255_DRIVER_NAME \ |
| 385 | ": " fmt, ##arg); \ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 386 | } \ |
| 387 | } while (0) |
| 388 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 389 | static struct usb_driver s2255_driver; |
| 390 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 391 | /* Declare static vars that will be used as parameters */ |
| 392 | static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ |
| 393 | |
| 394 | /* start video number */ |
| 395 | static int video_nr = -1; /* /dev/videoN, -1 for autodetect */ |
| 396 | |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 397 | module_param(debug, int, 0644); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 398 | MODULE_PARM_DESC(debug, "Debug level(0-100) default 0"); |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 399 | module_param(vid_limit, int, 0644); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 400 | MODULE_PARM_DESC(vid_limit, "video memory limit(Mb)"); |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 401 | module_param(video_nr, int, 0644); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 402 | MODULE_PARM_DESC(video_nr, "start video minor(-1 default autodetect)"); |
| 403 | |
| 404 | /* USB device table */ |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 405 | #define USB_SENSORAY_VID 0x1943 |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 406 | static struct usb_device_id s2255_table[] = { |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 407 | {USB_DEVICE(USB_SENSORAY_VID, 0x2255)}, |
| 408 | {USB_DEVICE(USB_SENSORAY_VID, 0x2257)}, /*same family as 2255*/ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 409 | { } /* Terminating entry */ |
| 410 | }; |
| 411 | MODULE_DEVICE_TABLE(usb, s2255_table); |
| 412 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 413 | #define BUFFER_TIMEOUT msecs_to_jiffies(400) |
| 414 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 415 | /* image formats. */ |
| 416 | static const struct s2255_fmt formats[] = { |
| 417 | { |
| 418 | .name = "4:2:2, planar, YUV422P", |
| 419 | .fourcc = V4L2_PIX_FMT_YUV422P, |
| 420 | .depth = 16 |
| 421 | |
| 422 | }, { |
| 423 | .name = "4:2:2, packed, YUYV", |
| 424 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 425 | .depth = 16 |
| 426 | |
| 427 | }, { |
| 428 | .name = "4:2:2, packed, UYVY", |
| 429 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 430 | .depth = 16 |
| 431 | }, { |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 432 | .name = "JPG", |
| 433 | .fourcc = V4L2_PIX_FMT_JPEG, |
| 434 | .depth = 24 |
| 435 | }, { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 436 | .name = "8bpp GREY", |
| 437 | .fourcc = V4L2_PIX_FMT_GREY, |
| 438 | .depth = 8 |
| 439 | } |
| 440 | }; |
| 441 | |
| 442 | static int norm_maxw(struct video_device *vdev) |
| 443 | { |
| 444 | return (vdev->current_norm & V4L2_STD_NTSC) ? |
| 445 | LINE_SZ_4CIFS_NTSC : LINE_SZ_4CIFS_PAL; |
| 446 | } |
| 447 | |
| 448 | static int norm_maxh(struct video_device *vdev) |
| 449 | { |
| 450 | return (vdev->current_norm & V4L2_STD_NTSC) ? |
| 451 | (NUM_LINES_1CIFS_NTSC * 2) : (NUM_LINES_1CIFS_PAL * 2); |
| 452 | } |
| 453 | |
| 454 | static int norm_minw(struct video_device *vdev) |
| 455 | { |
| 456 | return (vdev->current_norm & V4L2_STD_NTSC) ? |
| 457 | LINE_SZ_1CIFS_NTSC : LINE_SZ_1CIFS_PAL; |
| 458 | } |
| 459 | |
| 460 | static int norm_minh(struct video_device *vdev) |
| 461 | { |
| 462 | return (vdev->current_norm & V4L2_STD_NTSC) ? |
| 463 | (NUM_LINES_1CIFS_NTSC) : (NUM_LINES_1CIFS_PAL); |
| 464 | } |
| 465 | |
| 466 | |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 467 | /* |
| 468 | * TODO: fixme: move YUV reordering to hardware |
| 469 | * converts 2255 planar format to yuyv or uyvy |
| 470 | */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 471 | static void planar422p_to_yuv_packed(const unsigned char *in, |
| 472 | unsigned char *out, |
| 473 | int width, int height, |
| 474 | int fmt) |
| 475 | { |
| 476 | unsigned char *pY; |
| 477 | unsigned char *pCb; |
| 478 | unsigned char *pCr; |
| 479 | unsigned long size = height * width; |
| 480 | unsigned int i; |
| 481 | pY = (unsigned char *)in; |
| 482 | pCr = (unsigned char *)in + height * width; |
| 483 | pCb = (unsigned char *)in + height * width + (height * width / 2); |
| 484 | for (i = 0; i < size * 2; i += 4) { |
| 485 | out[i] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCr++; |
| 486 | out[i + 1] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCr++ : *pY++; |
| 487 | out[i + 2] = (fmt == V4L2_PIX_FMT_YUYV) ? *pY++ : *pCb++; |
| 488 | out[i + 3] = (fmt == V4L2_PIX_FMT_YUYV) ? *pCb++ : *pY++; |
| 489 | } |
| 490 | return; |
| 491 | } |
| 492 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 493 | static void s2255_reset_dsppower(struct s2255_dev *dev) |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 494 | { |
| 495 | s2255_vendor_req(dev, 0x40, 0x0b0b, 0x0b0b, NULL, 0, 1); |
| 496 | msleep(10); |
| 497 | s2255_vendor_req(dev, 0x50, 0x0000, 0x0000, NULL, 0, 1); |
| 498 | return; |
| 499 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 500 | |
| 501 | /* kickstarts the firmware loading. from probe |
| 502 | */ |
| 503 | static void s2255_timer(unsigned long user_data) |
| 504 | { |
| 505 | struct s2255_fw *data = (struct s2255_fw *)user_data; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 506 | dprintk(100, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 507 | if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) { |
| 508 | printk(KERN_ERR "s2255: can't submit urb\n"); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 509 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
| 510 | /* wake up anything waiting for the firmware */ |
| 511 | wake_up(&data->wait_fw); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | } |
| 515 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 516 | |
| 517 | /* this loads the firmware asynchronously. |
| 518 | Originally this was done synchroously in probe. |
| 519 | But it is better to load it asynchronously here than block |
| 520 | inside the probe function. Blocking inside probe affects boot time. |
| 521 | FW loading is triggered by the timer in the probe function |
| 522 | */ |
| 523 | static void s2255_fwchunk_complete(struct urb *urb) |
| 524 | { |
| 525 | struct s2255_fw *data = urb->context; |
| 526 | struct usb_device *udev = urb->dev; |
| 527 | int len; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 528 | dprintk(100, "%s: udev %p urb %p", __func__, udev, urb); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 529 | if (urb->status) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 530 | dev_err(&udev->dev, "URB failed with status %d\n", urb->status); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 531 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
| 532 | /* wake up anything waiting for the firmware */ |
| 533 | wake_up(&data->wait_fw); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | if (data->fw_urb == NULL) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 537 | s2255_dev_err(&udev->dev, "disconnected\n"); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 538 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
| 539 | /* wake up anything waiting for the firmware */ |
| 540 | wake_up(&data->wait_fw); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 541 | return; |
| 542 | } |
| 543 | #define CHUNK_SIZE 512 |
| 544 | /* all USB transfers must be done with continuous kernel memory. |
| 545 | can't allocate more than 128k in current linux kernel, so |
| 546 | upload the firmware in chunks |
| 547 | */ |
| 548 | if (data->fw_loaded < data->fw_size) { |
| 549 | len = (data->fw_loaded + CHUNK_SIZE) > data->fw_size ? |
| 550 | data->fw_size % CHUNK_SIZE : CHUNK_SIZE; |
| 551 | |
| 552 | if (len < CHUNK_SIZE) |
| 553 | memset(data->pfw_data, 0, CHUNK_SIZE); |
| 554 | |
| 555 | dprintk(100, "completed len %d, loaded %d \n", len, |
| 556 | data->fw_loaded); |
| 557 | |
| 558 | memcpy(data->pfw_data, |
| 559 | (char *) data->fw->data + data->fw_loaded, len); |
| 560 | |
| 561 | usb_fill_bulk_urb(data->fw_urb, udev, usb_sndbulkpipe(udev, 2), |
| 562 | data->pfw_data, CHUNK_SIZE, |
| 563 | s2255_fwchunk_complete, data); |
| 564 | if (usb_submit_urb(data->fw_urb, GFP_ATOMIC) < 0) { |
| 565 | dev_err(&udev->dev, "failed submit URB\n"); |
| 566 | atomic_set(&data->fw_state, S2255_FW_FAILED); |
| 567 | /* wake up anything waiting for the firmware */ |
| 568 | wake_up(&data->wait_fw); |
| 569 | return; |
| 570 | } |
| 571 | data->fw_loaded += len; |
| 572 | } else { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 573 | atomic_set(&data->fw_state, S2255_FW_LOADED_DSPWAIT); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 574 | dprintk(100, "%s: firmware upload complete\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 575 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 576 | return; |
| 577 | |
| 578 | } |
| 579 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 580 | static int s2255_got_frame(struct s2255_channel *channel, int jpgsize) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 581 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 582 | struct s2255_dmaqueue *dma_q = &channel->vidq; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 583 | struct s2255_buffer *buf; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 584 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 585 | unsigned long flags = 0; |
| 586 | int rc = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 587 | spin_lock_irqsave(&dev->slock, flags); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 588 | if (list_empty(&dma_q->active)) { |
| 589 | dprintk(1, "No active queue to serve\n"); |
| 590 | rc = -1; |
| 591 | goto unlock; |
| 592 | } |
| 593 | buf = list_entry(dma_q->active.next, |
| 594 | struct s2255_buffer, vb.queue); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 595 | list_del(&buf->vb.queue); |
| 596 | do_gettimeofday(&buf->vb.ts); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 597 | s2255_fillbuff(channel, buf, jpgsize); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 598 | wake_up(&buf->vb.done); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 599 | dprintk(2, "%s: [buf/i] [%p/%d]\n", __func__, buf, buf->vb.i); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 600 | unlock: |
| 601 | spin_unlock_irqrestore(&dev->slock, flags); |
Julia Lawall | f277097 | 2010-08-16 13:26:13 -0300 | [diff] [blame] | 602 | return rc; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 603 | } |
| 604 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 605 | static const struct s2255_fmt *format_by_fourcc(int fourcc) |
| 606 | { |
| 607 | unsigned int i; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 608 | for (i = 0; i < ARRAY_SIZE(formats); i++) { |
| 609 | if (-1 == formats[i].fourcc) |
| 610 | continue; |
| 611 | if (formats[i].fourcc == fourcc) |
| 612 | return formats + i; |
| 613 | } |
| 614 | return NULL; |
| 615 | } |
| 616 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 617 | /* video buffer vmalloc implementation based partly on VIVI driver which is |
| 618 | * Copyright (c) 2006 by |
| 619 | * Mauro Carvalho Chehab <mchehab--a.t--infradead.org> |
| 620 | * Ted Walther <ted--a.t--enumera.com> |
| 621 | * John Sokol <sokol--a.t--videotechnology.com> |
| 622 | * http://v4l.videotechnology.com/ |
| 623 | * |
| 624 | */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 625 | static void s2255_fillbuff(struct s2255_channel *channel, |
| 626 | struct s2255_buffer *buf, int jpgsize) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 627 | { |
| 628 | int pos = 0; |
| 629 | struct timeval ts; |
| 630 | const char *tmpbuf; |
| 631 | char *vbuf = videobuf_to_vmalloc(&buf->vb); |
| 632 | unsigned long last_frame; |
| 633 | struct s2255_framei *frm; |
| 634 | |
| 635 | if (!vbuf) |
| 636 | return; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 637 | last_frame = channel->last_frame; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 638 | if (last_frame != -1) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 639 | frm = &channel->buffer.frame[last_frame]; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 640 | tmpbuf = |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 641 | (const char *)channel->buffer.frame[last_frame].lpvbits; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 642 | switch (buf->fmt->fourcc) { |
| 643 | case V4L2_PIX_FMT_YUYV: |
| 644 | case V4L2_PIX_FMT_UYVY: |
| 645 | planar422p_to_yuv_packed((const unsigned char *)tmpbuf, |
| 646 | vbuf, buf->vb.width, |
| 647 | buf->vb.height, |
| 648 | buf->fmt->fourcc); |
| 649 | break; |
| 650 | case V4L2_PIX_FMT_GREY: |
| 651 | memcpy(vbuf, tmpbuf, buf->vb.width * buf->vb.height); |
| 652 | break; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 653 | case V4L2_PIX_FMT_JPEG: |
| 654 | buf->vb.size = jpgsize; |
| 655 | memcpy(vbuf, tmpbuf, buf->vb.size); |
| 656 | break; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 657 | case V4L2_PIX_FMT_YUV422P: |
| 658 | memcpy(vbuf, tmpbuf, |
| 659 | buf->vb.width * buf->vb.height * 2); |
| 660 | break; |
| 661 | default: |
| 662 | printk(KERN_DEBUG "s2255: unknown format?\n"); |
| 663 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 664 | channel->last_frame = -1; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 665 | } else { |
| 666 | printk(KERN_ERR "s2255: =======no frame\n"); |
| 667 | return; |
| 668 | |
| 669 | } |
| 670 | dprintk(2, "s2255fill at : Buffer 0x%08lx size= %d\n", |
| 671 | (unsigned long)vbuf, pos); |
| 672 | /* tell v4l buffer was filled */ |
| 673 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 674 | buf->vb.field_count = channel->frame_count * 2; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 675 | do_gettimeofday(&ts); |
| 676 | buf->vb.ts = ts; |
| 677 | buf->vb.state = VIDEOBUF_DONE; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | /* ------------------------------------------------------------------ |
| 682 | Videobuf operations |
| 683 | ------------------------------------------------------------------*/ |
| 684 | |
| 685 | static int buffer_setup(struct videobuf_queue *vq, unsigned int *count, |
| 686 | unsigned int *size) |
| 687 | { |
| 688 | struct s2255_fh *fh = vq->priv_data; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 689 | struct s2255_channel *channel = fh->channel; |
| 690 | *size = channel->width * channel->height * (channel->fmt->depth >> 3); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 691 | |
| 692 | if (0 == *count) |
| 693 | *count = S2255_DEF_BUFS; |
| 694 | |
Andreas Bombe | dab7e31 | 2010-03-21 16:02:45 -0300 | [diff] [blame] | 695 | if (*size * *count > vid_limit * 1024 * 1024) |
| 696 | *count = (vid_limit * 1024 * 1024) / *size; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static void free_buffer(struct videobuf_queue *vq, struct s2255_buffer *buf) |
| 702 | { |
| 703 | dprintk(4, "%s\n", __func__); |
| 704 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 705 | videobuf_vmalloc_free(&buf->vb); |
| 706 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 707 | } |
| 708 | |
| 709 | static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, |
| 710 | enum v4l2_field field) |
| 711 | { |
| 712 | struct s2255_fh *fh = vq->priv_data; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 713 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 714 | struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb); |
| 715 | int rc; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 716 | int w = channel->width; |
| 717 | int h = channel->height; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 718 | dprintk(4, "%s, field=%d\n", __func__, field); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 719 | if (channel->fmt == NULL) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 720 | return -EINVAL; |
| 721 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 722 | if ((w < norm_minw(&channel->vdev)) || |
| 723 | (w > norm_maxw(&channel->vdev)) || |
| 724 | (h < norm_minh(&channel->vdev)) || |
| 725 | (h > norm_maxh(&channel->vdev))) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 726 | dprintk(4, "invalid buffer prepare\n"); |
| 727 | return -EINVAL; |
| 728 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 729 | buf->vb.size = w * h * (channel->fmt->depth >> 3); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 730 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) { |
| 731 | dprintk(4, "invalid buffer prepare\n"); |
| 732 | return -EINVAL; |
| 733 | } |
| 734 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 735 | buf->fmt = channel->fmt; |
| 736 | buf->vb.width = w; |
| 737 | buf->vb.height = h; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 738 | buf->vb.field = field; |
| 739 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 740 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
| 741 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
| 742 | if (rc < 0) |
| 743 | goto fail; |
| 744 | } |
| 745 | |
| 746 | buf->vb.state = VIDEOBUF_PREPARED; |
| 747 | return 0; |
| 748 | fail: |
| 749 | free_buffer(vq, buf); |
| 750 | return rc; |
| 751 | } |
| 752 | |
| 753 | static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 754 | { |
| 755 | struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb); |
| 756 | struct s2255_fh *fh = vq->priv_data; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 757 | struct s2255_channel *channel = fh->channel; |
| 758 | struct s2255_dmaqueue *vidq = &channel->vidq; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 759 | dprintk(1, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 760 | buf->vb.state = VIDEOBUF_QUEUED; |
| 761 | list_add_tail(&buf->vb.queue, &vidq->active); |
| 762 | } |
| 763 | |
| 764 | static void buffer_release(struct videobuf_queue *vq, |
| 765 | struct videobuf_buffer *vb) |
| 766 | { |
| 767 | struct s2255_buffer *buf = container_of(vb, struct s2255_buffer, vb); |
| 768 | struct s2255_fh *fh = vq->priv_data; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 769 | dprintk(4, "%s %d\n", __func__, fh->channel->idx); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 770 | free_buffer(vq, buf); |
| 771 | } |
| 772 | |
| 773 | static struct videobuf_queue_ops s2255_video_qops = { |
| 774 | .buf_setup = buffer_setup, |
| 775 | .buf_prepare = buffer_prepare, |
| 776 | .buf_queue = buffer_queue, |
| 777 | .buf_release = buffer_release, |
| 778 | }; |
| 779 | |
| 780 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 781 | static int res_get(struct s2255_fh *fh) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 782 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 783 | struct s2255_channel *channel = fh->channel; |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 784 | /* is it free? */ |
| 785 | if (channel->resources) |
| 786 | return 0; /* no, someone else uses it */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 787 | /* it's free, grab it */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 788 | channel->resources = 1; |
| 789 | fh->resources = 1; |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 790 | dprintk(1, "s2255: res: get\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 791 | return 1; |
| 792 | } |
| 793 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 794 | static int res_locked(struct s2255_fh *fh) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 795 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 796 | return fh->channel->resources; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 797 | } |
| 798 | |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 799 | static int res_check(struct s2255_fh *fh) |
| 800 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 801 | return fh->resources; |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 805 | static void res_free(struct s2255_fh *fh) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 806 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 807 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 808 | channel->resources = 0; |
| 809 | fh->resources = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 810 | dprintk(1, "res: put\n"); |
| 811 | } |
| 812 | |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 813 | static int vidioc_querymenu(struct file *file, void *priv, |
| 814 | struct v4l2_querymenu *qmenu) |
| 815 | { |
| 816 | static const char *colorfilter[] = { |
| 817 | "Off", |
| 818 | "On", |
| 819 | NULL |
| 820 | }; |
| 821 | if (qmenu->id == V4L2_CID_PRIVATE_COLORFILTER) { |
| 822 | int i; |
| 823 | const char **menu_items = colorfilter; |
| 824 | for (i = 0; i < qmenu->index && menu_items[i]; i++) |
| 825 | ; /* do nothing (from v4l2-common.c) */ |
| 826 | if (menu_items[i] == NULL || menu_items[i][0] == '\0') |
| 827 | return -EINVAL; |
| 828 | strlcpy(qmenu->name, menu_items[qmenu->index], |
| 829 | sizeof(qmenu->name)); |
| 830 | return 0; |
| 831 | } |
| 832 | return v4l2_ctrl_query_menu(qmenu, NULL, NULL); |
| 833 | } |
| 834 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 835 | static int vidioc_querycap(struct file *file, void *priv, |
| 836 | struct v4l2_capability *cap) |
| 837 | { |
| 838 | struct s2255_fh *fh = file->private_data; |
| 839 | struct s2255_dev *dev = fh->dev; |
| 840 | strlcpy(cap->driver, "s2255", sizeof(cap->driver)); |
| 841 | strlcpy(cap->card, "s2255", sizeof(cap->card)); |
Thierry MERLE | e22ed88 | 2009-01-20 18:19:25 -0300 | [diff] [blame] | 842 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 843 | cap->version = S2255_VERSION; |
| 844 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 849 | struct v4l2_fmtdesc *f) |
| 850 | { |
| 851 | int index = 0; |
| 852 | if (f) |
| 853 | index = f->index; |
| 854 | |
| 855 | if (index >= ARRAY_SIZE(formats)) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | dprintk(4, "name %s\n", formats[index].name); |
| 859 | strlcpy(f->description, formats[index].name, sizeof(f->description)); |
| 860 | f->pixelformat = formats[index].fourcc; |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 865 | struct v4l2_format *f) |
| 866 | { |
| 867 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 868 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 869 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 870 | f->fmt.pix.width = channel->width; |
| 871 | f->fmt.pix.height = channel->height; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 872 | f->fmt.pix.field = fh->vb_vidq.field; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 873 | f->fmt.pix.pixelformat = channel->fmt->fourcc; |
| 874 | f->fmt.pix.bytesperline = f->fmt.pix.width * (channel->fmt->depth >> 3); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 875 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 876 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 880 | struct v4l2_format *f) |
| 881 | { |
| 882 | const struct s2255_fmt *fmt; |
| 883 | enum v4l2_field field; |
| 884 | int b_any_field = 0; |
| 885 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 886 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 887 | int is_ntsc; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 888 | is_ntsc = |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 889 | (channel->vdev.current_norm & V4L2_STD_NTSC) ? 1 : 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 890 | |
| 891 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 892 | |
| 893 | if (fmt == NULL) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | field = f->fmt.pix.field; |
| 897 | if (field == V4L2_FIELD_ANY) |
| 898 | b_any_field = 1; |
| 899 | |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 900 | dprintk(50, "%s NTSC: %d suggested width: %d, height: %d\n", |
| 901 | __func__, is_ntsc, f->fmt.pix.width, f->fmt.pix.height); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 902 | if (is_ntsc) { |
| 903 | /* NTSC */ |
| 904 | if (f->fmt.pix.height >= NUM_LINES_1CIFS_NTSC * 2) { |
| 905 | f->fmt.pix.height = NUM_LINES_1CIFS_NTSC * 2; |
| 906 | if (b_any_field) { |
| 907 | field = V4L2_FIELD_SEQ_TB; |
| 908 | } else if (!((field == V4L2_FIELD_INTERLACED) || |
| 909 | (field == V4L2_FIELD_SEQ_TB) || |
| 910 | (field == V4L2_FIELD_INTERLACED_TB))) { |
| 911 | dprintk(1, "unsupported field setting\n"); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | } else { |
| 915 | f->fmt.pix.height = NUM_LINES_1CIFS_NTSC; |
| 916 | if (b_any_field) { |
| 917 | field = V4L2_FIELD_TOP; |
| 918 | } else if (!((field == V4L2_FIELD_TOP) || |
| 919 | (field == V4L2_FIELD_BOTTOM))) { |
| 920 | dprintk(1, "unsupported field setting\n"); |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | |
| 924 | } |
| 925 | if (f->fmt.pix.width >= LINE_SZ_4CIFS_NTSC) |
| 926 | f->fmt.pix.width = LINE_SZ_4CIFS_NTSC; |
| 927 | else if (f->fmt.pix.width >= LINE_SZ_2CIFS_NTSC) |
| 928 | f->fmt.pix.width = LINE_SZ_2CIFS_NTSC; |
| 929 | else if (f->fmt.pix.width >= LINE_SZ_1CIFS_NTSC) |
| 930 | f->fmt.pix.width = LINE_SZ_1CIFS_NTSC; |
| 931 | else |
| 932 | f->fmt.pix.width = LINE_SZ_1CIFS_NTSC; |
| 933 | } else { |
| 934 | /* PAL */ |
| 935 | if (f->fmt.pix.height >= NUM_LINES_1CIFS_PAL * 2) { |
| 936 | f->fmt.pix.height = NUM_LINES_1CIFS_PAL * 2; |
| 937 | if (b_any_field) { |
| 938 | field = V4L2_FIELD_SEQ_TB; |
| 939 | } else if (!((field == V4L2_FIELD_INTERLACED) || |
| 940 | (field == V4L2_FIELD_SEQ_TB) || |
| 941 | (field == V4L2_FIELD_INTERLACED_TB))) { |
| 942 | dprintk(1, "unsupported field setting\n"); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | } else { |
| 946 | f->fmt.pix.height = NUM_LINES_1CIFS_PAL; |
| 947 | if (b_any_field) { |
| 948 | field = V4L2_FIELD_TOP; |
| 949 | } else if (!((field == V4L2_FIELD_TOP) || |
| 950 | (field == V4L2_FIELD_BOTTOM))) { |
| 951 | dprintk(1, "unsupported field setting\n"); |
| 952 | return -EINVAL; |
| 953 | } |
| 954 | } |
| 955 | if (f->fmt.pix.width >= LINE_SZ_4CIFS_PAL) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 956 | f->fmt.pix.width = LINE_SZ_4CIFS_PAL; |
| 957 | field = V4L2_FIELD_SEQ_TB; |
| 958 | } else if (f->fmt.pix.width >= LINE_SZ_2CIFS_PAL) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 959 | f->fmt.pix.width = LINE_SZ_2CIFS_PAL; |
| 960 | field = V4L2_FIELD_TOP; |
| 961 | } else if (f->fmt.pix.width >= LINE_SZ_1CIFS_PAL) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 962 | f->fmt.pix.width = LINE_SZ_1CIFS_PAL; |
| 963 | field = V4L2_FIELD_TOP; |
| 964 | } else { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 965 | f->fmt.pix.width = LINE_SZ_1CIFS_PAL; |
| 966 | field = V4L2_FIELD_TOP; |
| 967 | } |
| 968 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 969 | f->fmt.pix.field = field; |
| 970 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; |
| 971 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 972 | dprintk(50, "%s: set width %d height %d field %d\n", __func__, |
| 973 | f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 978 | struct v4l2_format *f) |
| 979 | { |
| 980 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 981 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 982 | const struct s2255_fmt *fmt; |
| 983 | struct videobuf_queue *q = &fh->vb_vidq; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 984 | struct s2255_mode mode; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 985 | int ret; |
| 986 | int norm; |
| 987 | |
| 988 | ret = vidioc_try_fmt_vid_cap(file, fh, f); |
| 989 | |
| 990 | if (ret < 0) |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 991 | return ret; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 992 | |
| 993 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 994 | |
| 995 | if (fmt == NULL) |
| 996 | return -EINVAL; |
| 997 | |
| 998 | mutex_lock(&q->vb_lock); |
| 999 | |
| 1000 | if (videobuf_queue_is_busy(&fh->vb_vidq)) { |
| 1001 | dprintk(1, "queue busy\n"); |
| 1002 | ret = -EBUSY; |
| 1003 | goto out_s_fmt; |
| 1004 | } |
| 1005 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1006 | if (res_locked(fh)) { |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1007 | dprintk(1, "%s: channel busy\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1008 | ret = -EBUSY; |
| 1009 | goto out_s_fmt; |
| 1010 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1011 | mode = channel->mode; |
| 1012 | channel->fmt = fmt; |
| 1013 | channel->width = f->fmt.pix.width; |
| 1014 | channel->height = f->fmt.pix.height; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1015 | fh->vb_vidq.field = f->fmt.pix.field; |
| 1016 | fh->type = f->type; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1017 | norm = norm_minw(&channel->vdev); |
| 1018 | if (channel->width > norm_minw(&channel->vdev)) { |
| 1019 | if (channel->height > norm_minh(&channel->vdev)) { |
| 1020 | if (channel->cap_parm.capturemode & |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1021 | V4L2_MODE_HIGHQUALITY) |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1022 | mode.scale = SCALE_4CIFSI; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1023 | else |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1024 | mode.scale = SCALE_4CIFS; |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1025 | } else |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1026 | mode.scale = SCALE_2CIFS; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1027 | |
| 1028 | } else { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1029 | mode.scale = SCALE_1CIFS; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1030 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1031 | /* color mode */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1032 | switch (channel->fmt->fourcc) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1033 | case V4L2_PIX_FMT_GREY: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1034 | mode.color &= ~MASK_COLOR; |
| 1035 | mode.color |= COLOR_Y8; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1036 | break; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1037 | case V4L2_PIX_FMT_JPEG: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1038 | mode.color &= ~MASK_COLOR; |
| 1039 | mode.color |= COLOR_JPG; |
| 1040 | mode.color |= (channel->jc.quality << 8); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1041 | break; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1042 | case V4L2_PIX_FMT_YUV422P: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1043 | mode.color &= ~MASK_COLOR; |
| 1044 | mode.color |= COLOR_YUVPL; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1045 | break; |
| 1046 | case V4L2_PIX_FMT_YUYV: |
| 1047 | case V4L2_PIX_FMT_UYVY: |
| 1048 | default: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1049 | mode.color &= ~MASK_COLOR; |
| 1050 | mode.color |= COLOR_YUVPK; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1051 | break; |
| 1052 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1053 | if ((mode.color & MASK_COLOR) != (channel->mode.color & MASK_COLOR)) |
| 1054 | mode.restart = 1; |
| 1055 | else if (mode.scale != channel->mode.scale) |
| 1056 | mode.restart = 1; |
| 1057 | else if (mode.format != channel->mode.format) |
| 1058 | mode.restart = 1; |
| 1059 | channel->mode = mode; |
| 1060 | (void) s2255_set_mode(channel, &mode); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1061 | ret = 0; |
| 1062 | out_s_fmt: |
| 1063 | mutex_unlock(&q->vb_lock); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1068 | struct v4l2_requestbuffers *p) |
| 1069 | { |
| 1070 | int rc; |
| 1071 | struct s2255_fh *fh = priv; |
| 1072 | rc = videobuf_reqbufs(&fh->vb_vidq, p); |
| 1073 | return rc; |
| 1074 | } |
| 1075 | |
| 1076 | static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 1077 | { |
| 1078 | int rc; |
| 1079 | struct s2255_fh *fh = priv; |
| 1080 | rc = videobuf_querybuf(&fh->vb_vidq, p); |
| 1081 | return rc; |
| 1082 | } |
| 1083 | |
| 1084 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 1085 | { |
| 1086 | int rc; |
| 1087 | struct s2255_fh *fh = priv; |
| 1088 | rc = videobuf_qbuf(&fh->vb_vidq, p); |
| 1089 | return rc; |
| 1090 | } |
| 1091 | |
| 1092 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
| 1093 | { |
| 1094 | int rc; |
| 1095 | struct s2255_fh *fh = priv; |
| 1096 | rc = videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK); |
| 1097 | return rc; |
| 1098 | } |
| 1099 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1100 | /* write to the configuration pipe, synchronously */ |
| 1101 | static int s2255_write_config(struct usb_device *udev, unsigned char *pbuf, |
| 1102 | int size) |
| 1103 | { |
| 1104 | int pipe; |
| 1105 | int done; |
| 1106 | long retval = -1; |
| 1107 | if (udev) { |
| 1108 | pipe = usb_sndbulkpipe(udev, S2255_CONFIG_EP); |
| 1109 | retval = usb_bulk_msg(udev, pipe, pbuf, size, &done, 500); |
| 1110 | } |
| 1111 | return retval; |
| 1112 | } |
| 1113 | |
| 1114 | static u32 get_transfer_size(struct s2255_mode *mode) |
| 1115 | { |
| 1116 | int linesPerFrame = LINE_SZ_DEF; |
| 1117 | int pixelsPerLine = NUM_LINES_DEF; |
| 1118 | u32 outImageSize; |
| 1119 | u32 usbInSize; |
| 1120 | unsigned int mask_mult; |
| 1121 | |
| 1122 | if (mode == NULL) |
| 1123 | return 0; |
| 1124 | |
| 1125 | if (mode->format == FORMAT_NTSC) { |
| 1126 | switch (mode->scale) { |
| 1127 | case SCALE_4CIFS: |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1128 | case SCALE_4CIFSI: |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1129 | linesPerFrame = NUM_LINES_4CIFS_NTSC * 2; |
| 1130 | pixelsPerLine = LINE_SZ_4CIFS_NTSC; |
| 1131 | break; |
| 1132 | case SCALE_2CIFS: |
| 1133 | linesPerFrame = NUM_LINES_2CIFS_NTSC; |
| 1134 | pixelsPerLine = LINE_SZ_2CIFS_NTSC; |
| 1135 | break; |
| 1136 | case SCALE_1CIFS: |
| 1137 | linesPerFrame = NUM_LINES_1CIFS_NTSC; |
| 1138 | pixelsPerLine = LINE_SZ_1CIFS_NTSC; |
| 1139 | break; |
| 1140 | default: |
| 1141 | break; |
| 1142 | } |
| 1143 | } else if (mode->format == FORMAT_PAL) { |
| 1144 | switch (mode->scale) { |
| 1145 | case SCALE_4CIFS: |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1146 | case SCALE_4CIFSI: |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1147 | linesPerFrame = NUM_LINES_4CIFS_PAL * 2; |
| 1148 | pixelsPerLine = LINE_SZ_4CIFS_PAL; |
| 1149 | break; |
| 1150 | case SCALE_2CIFS: |
| 1151 | linesPerFrame = NUM_LINES_2CIFS_PAL; |
| 1152 | pixelsPerLine = LINE_SZ_2CIFS_PAL; |
| 1153 | break; |
| 1154 | case SCALE_1CIFS: |
| 1155 | linesPerFrame = NUM_LINES_1CIFS_PAL; |
| 1156 | pixelsPerLine = LINE_SZ_1CIFS_PAL; |
| 1157 | break; |
| 1158 | default: |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | outImageSize = linesPerFrame * pixelsPerLine; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1163 | if ((mode->color & MASK_COLOR) != COLOR_Y8) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1164 | /* 2 bytes/pixel if not monochrome */ |
| 1165 | outImageSize *= 2; |
| 1166 | } |
| 1167 | |
| 1168 | /* total bytes to send including prefix and 4K padding; |
| 1169 | must be a multiple of USB_READ_SIZE */ |
| 1170 | usbInSize = outImageSize + PREFIX_SIZE; /* always send prefix */ |
| 1171 | mask_mult = 0xffffffffUL - DEF_USB_BLOCK + 1; |
| 1172 | /* if size not a multiple of USB_READ_SIZE */ |
| 1173 | if (usbInSize & ~mask_mult) |
| 1174 | usbInSize = (usbInSize & mask_mult) + (DEF_USB_BLOCK); |
| 1175 | return usbInSize; |
| 1176 | } |
| 1177 | |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1178 | static void s2255_print_cfg(struct s2255_dev *sdev, struct s2255_mode *mode) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1179 | { |
| 1180 | struct device *dev = &sdev->udev->dev; |
| 1181 | dev_info(dev, "------------------------------------------------\n"); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1182 | dev_info(dev, "format: %d\nscale %d\n", mode->format, mode->scale); |
| 1183 | dev_info(dev, "fdec: %d\ncolor %d\n", mode->fdec, mode->color); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1184 | dev_info(dev, "bright: 0x%x\n", mode->bright); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1185 | dev_info(dev, "------------------------------------------------\n"); |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | * set mode is the function which controls the DSP. |
| 1190 | * the restart parameter in struct s2255_mode should be set whenever |
| 1191 | * the image size could change via color format, video system or image |
| 1192 | * size. |
| 1193 | * When the restart parameter is set, we sleep for ONE frame to allow the |
| 1194 | * DSP time to get the new frame |
| 1195 | */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1196 | static int s2255_set_mode(struct s2255_channel *channel, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1197 | struct s2255_mode *mode) |
| 1198 | { |
| 1199 | int res; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 1200 | __le32 *buffer; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1201 | unsigned long chn_rev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1202 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1203 | chn_rev = G_chnmap[channel->idx]; |
| 1204 | dprintk(3, "%s channel: %d\n", __func__, channel->idx); |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1205 | /* if JPEG, set the quality */ |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1206 | if ((mode->color & MASK_COLOR) == COLOR_JPG) { |
| 1207 | mode->color &= ~MASK_COLOR; |
| 1208 | mode->color |= COLOR_JPG; |
| 1209 | mode->color &= ~MASK_JPG_QUALITY; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1210 | mode->color |= (channel->jc.quality << 8); |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1211 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1212 | /* save the mode */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1213 | channel->mode = *mode; |
| 1214 | channel->req_image_size = get_transfer_size(mode); |
| 1215 | dprintk(1, "%s: reqsize %ld\n", __func__, channel->req_image_size); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1216 | buffer = kzalloc(512, GFP_KERNEL); |
| 1217 | if (buffer == NULL) { |
| 1218 | dev_err(&dev->udev->dev, "out of mem\n"); |
| 1219 | return -ENOMEM; |
| 1220 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1221 | /* set the mode */ |
| 1222 | buffer[0] = IN_DATA_TOKEN; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 1223 | buffer[1] = (__le32) cpu_to_le32(chn_rev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1224 | buffer[2] = CMD_SET_MODE; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1225 | memcpy(&buffer[3], &channel->mode, sizeof(struct s2255_mode)); |
| 1226 | channel->setmode_ready = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1227 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
| 1228 | if (debug) |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1229 | s2255_print_cfg(dev, mode); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1230 | kfree(buffer); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1231 | /* wait at least 3 frames before continuing */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1232 | if (mode->restart) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1233 | wait_event_timeout(channel->wait_setmode, |
| 1234 | (channel->setmode_ready != 0), |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1235 | msecs_to_jiffies(S2255_SETMODE_TIMEOUT)); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1236 | if (channel->setmode_ready != 1) { |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1237 | printk(KERN_DEBUG "s2255: no set mode response\n"); |
| 1238 | res = -EFAULT; |
| 1239 | } |
| 1240 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1241 | /* clear the restart flag */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1242 | channel->mode.restart = 0; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1243 | dprintk(1, "%s chn %d, result: %d\n", __func__, channel->idx, res); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1244 | return res; |
| 1245 | } |
| 1246 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1247 | static int s2255_cmd_status(struct s2255_channel *channel, u32 *pstatus) |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1248 | { |
| 1249 | int res; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 1250 | __le32 *buffer; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1251 | u32 chn_rev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1252 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1253 | chn_rev = G_chnmap[channel->idx]; |
| 1254 | dprintk(4, "%s chan %d\n", __func__, channel->idx); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1255 | buffer = kzalloc(512, GFP_KERNEL); |
| 1256 | if (buffer == NULL) { |
| 1257 | dev_err(&dev->udev->dev, "out of mem\n"); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1258 | return -ENOMEM; |
| 1259 | } |
| 1260 | /* form the get vid status command */ |
| 1261 | buffer[0] = IN_DATA_TOKEN; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 1262 | buffer[1] = (__le32) cpu_to_le32(chn_rev); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1263 | buffer[2] = CMD_STATUS; |
| 1264 | *pstatus = 0; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1265 | channel->vidstatus_ready = 0; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1266 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
| 1267 | kfree(buffer); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1268 | wait_event_timeout(channel->wait_vidstatus, |
| 1269 | (channel->vidstatus_ready != 0), |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1270 | msecs_to_jiffies(S2255_VIDSTATUS_TIMEOUT)); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1271 | if (channel->vidstatus_ready != 1) { |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1272 | printk(KERN_DEBUG "s2255: no vidstatus response\n"); |
| 1273 | res = -EFAULT; |
| 1274 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1275 | *pstatus = channel->vidstatus; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1276 | dprintk(4, "%s, vid status %d\n", __func__, *pstatus); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1277 | return res; |
| 1278 | } |
| 1279 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1280 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
| 1281 | { |
| 1282 | int res; |
| 1283 | struct s2255_fh *fh = priv; |
| 1284 | struct s2255_dev *dev = fh->dev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1285 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1286 | int j; |
| 1287 | dprintk(4, "%s\n", __func__); |
| 1288 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1289 | dev_err(&dev->udev->dev, "invalid fh type0\n"); |
| 1290 | return -EINVAL; |
| 1291 | } |
| 1292 | if (i != fh->type) { |
| 1293 | dev_err(&dev->udev->dev, "invalid fh type1\n"); |
| 1294 | return -EINVAL; |
| 1295 | } |
| 1296 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1297 | if (!res_get(fh)) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 1298 | s2255_dev_err(&dev->udev->dev, "stream busy\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1299 | return -EBUSY; |
| 1300 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1301 | channel->last_frame = -1; |
| 1302 | channel->bad_payload = 0; |
| 1303 | channel->cur_frame = 0; |
| 1304 | channel->frame_count = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1305 | for (j = 0; j < SYS_FRAMES; j++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1306 | channel->buffer.frame[j].ulState = S2255_READ_IDLE; |
| 1307 | channel->buffer.frame[j].cur_size = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1308 | } |
| 1309 | res = videobuf_streamon(&fh->vb_vidq); |
| 1310 | if (res == 0) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1311 | s2255_start_acquire(channel); |
| 1312 | channel->b_acquire = 1; |
| 1313 | } else |
| 1314 | res_free(fh); |
| 1315 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1316 | return res; |
| 1317 | } |
| 1318 | |
| 1319 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 1320 | { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1321 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1322 | dprintk(4, "%s\n, channel: %d", __func__, fh->channel->idx); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1323 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
| 1324 | printk(KERN_ERR "invalid fh type0\n"); |
| 1325 | return -EINVAL; |
| 1326 | } |
| 1327 | if (i != fh->type) { |
| 1328 | printk(KERN_ERR "invalid type i\n"); |
| 1329 | return -EINVAL; |
| 1330 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1331 | s2255_stop_acquire(fh->channel); |
Dean Anderson | b7732a3 | 2009-03-30 11:59:56 -0300 | [diff] [blame] | 1332 | videobuf_streamoff(&fh->vb_vidq); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1333 | res_free(fh); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1334 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i) |
| 1338 | { |
| 1339 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1340 | struct s2255_mode mode; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1341 | struct videobuf_queue *q = &fh->vb_vidq; |
| 1342 | int ret = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1343 | mutex_lock(&q->vb_lock); |
| 1344 | if (videobuf_queue_is_busy(q)) { |
| 1345 | dprintk(1, "queue busy\n"); |
| 1346 | ret = -EBUSY; |
| 1347 | goto out_s_std; |
| 1348 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1349 | if (res_locked(fh)) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1350 | dprintk(1, "can't change standard after started\n"); |
| 1351 | ret = -EBUSY; |
| 1352 | goto out_s_std; |
| 1353 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1354 | mode = fh->channel->mode; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1355 | if (*i & V4L2_STD_NTSC) { |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1356 | dprintk(4, "%s NTSC\n", __func__); |
| 1357 | /* if changing format, reset frame decimation/intervals */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1358 | if (mode.format != FORMAT_NTSC) { |
| 1359 | mode.restart = 1; |
| 1360 | mode.format = FORMAT_NTSC; |
| 1361 | mode.fdec = FDEC_1; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1362 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1363 | } else if (*i & V4L2_STD_PAL) { |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1364 | dprintk(4, "%s PAL\n", __func__); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1365 | if (mode.format != FORMAT_PAL) { |
| 1366 | mode.restart = 1; |
| 1367 | mode.format = FORMAT_PAL; |
| 1368 | mode.fdec = FDEC_1; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1369 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1370 | } else { |
| 1371 | ret = -EINVAL; |
| 1372 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1373 | if (mode.restart) |
| 1374 | s2255_set_mode(fh->channel, &mode); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1375 | out_s_std: |
| 1376 | mutex_unlock(&q->vb_lock); |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
| 1380 | /* Sensoray 2255 is a multiple channel capture device. |
| 1381 | It does not have a "crossbar" of inputs. |
| 1382 | We use one V4L device per channel. The user must |
| 1383 | be aware that certain combinations are not allowed. |
| 1384 | For instance, you cannot do full FPS on more than 2 channels(2 videodevs) |
| 1385 | at once in color(you can do full fps on 4 channels with greyscale. |
| 1386 | */ |
| 1387 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1388 | struct v4l2_input *inp) |
| 1389 | { |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1390 | struct s2255_fh *fh = priv; |
| 1391 | struct s2255_dev *dev = fh->dev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1392 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1393 | u32 status = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1394 | if (inp->index != 0) |
| 1395 | return -EINVAL; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1396 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 1397 | inp->std = S2255_NORMS; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1398 | inp->status = 0; |
| 1399 | if (dev->dsp_fw_ver >= S2255_MIN_DSP_STATUS) { |
| 1400 | int rc; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1401 | rc = s2255_cmd_status(fh->channel, &status); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 1402 | dprintk(4, "s2255_cmd_status rc: %d status %x\n", rc, status); |
| 1403 | if (rc == 0) |
| 1404 | inp->status = (status & 0x01) ? 0 |
| 1405 | : V4L2_IN_ST_NO_SIGNAL; |
| 1406 | } |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1407 | switch (dev->pid) { |
| 1408 | case 0x2255: |
| 1409 | default: |
| 1410 | strlcpy(inp->name, "Composite", sizeof(inp->name)); |
| 1411 | break; |
| 1412 | case 0x2257: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1413 | strlcpy(inp->name, (channel->idx < 2) ? "Composite" : "S-Video", |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1414 | sizeof(inp->name)); |
| 1415 | break; |
| 1416 | } |
Dean Anderson | 3f8d6f7 | 2008-06-30 21:28:34 -0300 | [diff] [blame] | 1417 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1421 | { |
| 1422 | *i = 0; |
| 1423 | return 0; |
| 1424 | } |
| 1425 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1426 | { |
| 1427 | if (i > 0) |
| 1428 | return -EINVAL; |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* --- controls ---------------------------------------------- */ |
| 1433 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 1434 | struct v4l2_queryctrl *qc) |
| 1435 | { |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1436 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1437 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1438 | struct s2255_dev *dev = fh->dev; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1439 | switch (qc->id) { |
| 1440 | case V4L2_CID_BRIGHTNESS: |
| 1441 | v4l2_ctrl_query_fill(qc, -127, 127, 1, DEF_BRIGHT); |
| 1442 | break; |
| 1443 | case V4L2_CID_CONTRAST: |
| 1444 | v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_CONTRAST); |
| 1445 | break; |
| 1446 | case V4L2_CID_SATURATION: |
| 1447 | v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_SATURATION); |
| 1448 | break; |
| 1449 | case V4L2_CID_HUE: |
| 1450 | v4l2_ctrl_query_fill(qc, 0, 255, 1, DEF_HUE); |
| 1451 | break; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1452 | case V4L2_CID_PRIVATE_COLORFILTER: |
| 1453 | if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER) |
| 1454 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1455 | if ((dev->pid == 0x2257) && (channel->idx > 1)) |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1456 | return -EINVAL; |
| 1457 | strlcpy(qc->name, "Color Filter", sizeof(qc->name)); |
| 1458 | qc->type = V4L2_CTRL_TYPE_MENU; |
| 1459 | qc->minimum = 0; |
| 1460 | qc->maximum = 1; |
| 1461 | qc->step = 1; |
| 1462 | qc->default_value = 1; |
| 1463 | qc->flags = 0; |
| 1464 | break; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1465 | default: |
| 1466 | return -EINVAL; |
| 1467 | } |
| 1468 | dprintk(4, "%s, id %d\n", __func__, qc->id); |
| 1469 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 1473 | struct v4l2_control *ctrl) |
| 1474 | { |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1475 | struct s2255_fh *fh = priv; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1476 | struct s2255_dev *dev = fh->dev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1477 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1478 | switch (ctrl->id) { |
| 1479 | case V4L2_CID_BRIGHTNESS: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1480 | ctrl->value = channel->mode.bright; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1481 | break; |
| 1482 | case V4L2_CID_CONTRAST: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1483 | ctrl->value = channel->mode.contrast; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1484 | break; |
| 1485 | case V4L2_CID_SATURATION: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1486 | ctrl->value = channel->mode.saturation; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1487 | break; |
| 1488 | case V4L2_CID_HUE: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1489 | ctrl->value = channel->mode.hue; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1490 | break; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1491 | case V4L2_CID_PRIVATE_COLORFILTER: |
| 1492 | if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER) |
| 1493 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1494 | if ((dev->pid == 0x2257) && (channel->idx > 1)) |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1495 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1496 | ctrl->value = !((channel->mode.color & MASK_INPUT_TYPE) >> 16); |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1497 | break; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1498 | default: |
| 1499 | return -EINVAL; |
| 1500 | } |
| 1501 | dprintk(4, "%s, id %d val %d\n", __func__, ctrl->id, ctrl->value); |
| 1502 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 1506 | struct v4l2_control *ctrl) |
| 1507 | { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1508 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1509 | struct s2255_channel *channel = fh->channel; |
| 1510 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
| 1511 | struct s2255_mode mode; |
| 1512 | mode = channel->mode; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1513 | dprintk(4, "%s\n", __func__); |
| 1514 | /* update the mode to the corresponding value */ |
| 1515 | switch (ctrl->id) { |
| 1516 | case V4L2_CID_BRIGHTNESS: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1517 | mode.bright = ctrl->value; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1518 | break; |
| 1519 | case V4L2_CID_CONTRAST: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1520 | mode.contrast = ctrl->value; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1521 | break; |
| 1522 | case V4L2_CID_HUE: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1523 | mode.hue = ctrl->value; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1524 | break; |
| 1525 | case V4L2_CID_SATURATION: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1526 | mode.saturation = ctrl->value; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1527 | break; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1528 | case V4L2_CID_PRIVATE_COLORFILTER: |
| 1529 | if (dev->dsp_fw_ver < S2255_MIN_DSP_COLORFILTER) |
| 1530 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1531 | if ((dev->pid == 0x2257) && (channel->idx > 1)) |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1532 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1533 | mode.color &= ~MASK_INPUT_TYPE; |
| 1534 | mode.color |= ((ctrl->value ? 0 : 1) << 16); |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1535 | break; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1536 | default: |
| 1537 | return -EINVAL; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1538 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1539 | mode.restart = 0; |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1540 | /* set mode here. Note: stream does not need restarted. |
| 1541 | some V4L programs restart stream unnecessarily |
| 1542 | after a s_crtl. |
| 1543 | */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1544 | s2255_set_mode(fh->channel, &mode); |
Dean Anderson | 2e70db9 | 2010-03-05 14:29:09 -0300 | [diff] [blame] | 1545 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1546 | } |
| 1547 | |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1548 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
| 1549 | struct v4l2_jpegcompression *jc) |
| 1550 | { |
| 1551 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1552 | struct s2255_channel *channel = fh->channel; |
| 1553 | *jc = channel->jc; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1554 | dprintk(2, "%s: quality %d\n", __func__, jc->quality); |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static int vidioc_s_jpegcomp(struct file *file, void *priv, |
| 1559 | struct v4l2_jpegcompression *jc) |
| 1560 | { |
| 1561 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1562 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1563 | if (jc->quality < 0 || jc->quality > 100) |
| 1564 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1565 | channel->jc.quality = jc->quality; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1566 | dprintk(2, "%s: quality %d\n", __func__, jc->quality); |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1567 | return 0; |
| 1568 | } |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1569 | |
| 1570 | static int vidioc_g_parm(struct file *file, void *priv, |
| 1571 | struct v4l2_streamparm *sp) |
| 1572 | { |
| 1573 | struct s2255_fh *fh = priv; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1574 | __u32 def_num, def_dem; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1575 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1576 | if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1577 | return -EINVAL; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1578 | memset(sp, 0, sizeof(struct v4l2_streamparm)); |
| 1579 | sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1580 | sp->parm.capture.capturemode = channel->cap_parm.capturemode; |
| 1581 | def_num = (channel->mode.format == FORMAT_NTSC) ? 1001 : 1000; |
| 1582 | def_dem = (channel->mode.format == FORMAT_NTSC) ? 30000 : 25000; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1583 | sp->parm.capture.timeperframe.denominator = def_dem; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1584 | switch (channel->mode.fdec) { |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1585 | default: |
| 1586 | case FDEC_1: |
| 1587 | sp->parm.capture.timeperframe.numerator = def_num; |
| 1588 | break; |
| 1589 | case FDEC_2: |
| 1590 | sp->parm.capture.timeperframe.numerator = def_num * 2; |
| 1591 | break; |
| 1592 | case FDEC_3: |
| 1593 | sp->parm.capture.timeperframe.numerator = def_num * 3; |
| 1594 | break; |
| 1595 | case FDEC_5: |
| 1596 | sp->parm.capture.timeperframe.numerator = def_num * 5; |
| 1597 | break; |
| 1598 | } |
| 1599 | dprintk(4, "%s capture mode, %d timeperframe %d/%d\n", __func__, |
| 1600 | sp->parm.capture.capturemode, |
| 1601 | sp->parm.capture.timeperframe.numerator, |
| 1602 | sp->parm.capture.timeperframe.denominator); |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | static int vidioc_s_parm(struct file *file, void *priv, |
| 1607 | struct v4l2_streamparm *sp) |
| 1608 | { |
| 1609 | struct s2255_fh *fh = priv; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1610 | struct s2255_channel *channel = fh->channel; |
| 1611 | struct s2255_mode mode; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1612 | int fdec = FDEC_1; |
| 1613 | __u32 def_num, def_dem; |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1614 | if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1615 | return -EINVAL; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1616 | mode = channel->mode; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1617 | /* high quality capture mode requires a stream restart */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1618 | if (channel->cap_parm.capturemode |
| 1619 | != sp->parm.capture.capturemode && res_locked(fh)) |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1620 | return -EBUSY; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1621 | def_num = (mode.format == FORMAT_NTSC) ? 1001 : 1000; |
| 1622 | def_dem = (mode.format == FORMAT_NTSC) ? 30000 : 25000; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1623 | if (def_dem != sp->parm.capture.timeperframe.denominator) |
| 1624 | sp->parm.capture.timeperframe.numerator = def_num; |
| 1625 | else if (sp->parm.capture.timeperframe.numerator <= def_num) |
| 1626 | sp->parm.capture.timeperframe.numerator = def_num; |
| 1627 | else if (sp->parm.capture.timeperframe.numerator <= (def_num * 2)) { |
| 1628 | sp->parm.capture.timeperframe.numerator = def_num * 2; |
| 1629 | fdec = FDEC_2; |
| 1630 | } else if (sp->parm.capture.timeperframe.numerator <= (def_num * 3)) { |
| 1631 | sp->parm.capture.timeperframe.numerator = def_num * 3; |
| 1632 | fdec = FDEC_3; |
| 1633 | } else { |
| 1634 | sp->parm.capture.timeperframe.numerator = def_num * 5; |
| 1635 | fdec = FDEC_5; |
| 1636 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1637 | mode.fdec = fdec; |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1638 | sp->parm.capture.timeperframe.denominator = def_dem; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1639 | s2255_set_mode(channel, &mode); |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1640 | dprintk(4, "%s capture mode, %d timeperframe %d/%d, fdec %d\n", |
| 1641 | __func__, |
| 1642 | sp->parm.capture.capturemode, |
| 1643 | sp->parm.capture.timeperframe.numerator, |
| 1644 | sp->parm.capture.timeperframe.denominator, fdec); |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1647 | |
| 1648 | static int vidioc_enum_frameintervals(struct file *file, void *priv, |
| 1649 | struct v4l2_frmivalenum *fe) |
| 1650 | { |
| 1651 | int is_ntsc = 0; |
| 1652 | #define NUM_FRAME_ENUMS 4 |
| 1653 | int frm_dec[NUM_FRAME_ENUMS] = {1, 2, 3, 5}; |
| 1654 | if (fe->index < 0 || fe->index >= NUM_FRAME_ENUMS) |
| 1655 | return -EINVAL; |
| 1656 | switch (fe->width) { |
| 1657 | case 640: |
| 1658 | if (fe->height != 240 && fe->height != 480) |
| 1659 | return -EINVAL; |
| 1660 | is_ntsc = 1; |
| 1661 | break; |
| 1662 | case 320: |
| 1663 | if (fe->height != 240) |
| 1664 | return -EINVAL; |
| 1665 | is_ntsc = 1; |
| 1666 | break; |
| 1667 | case 704: |
| 1668 | if (fe->height != 288 && fe->height != 576) |
| 1669 | return -EINVAL; |
| 1670 | break; |
| 1671 | case 352: |
| 1672 | if (fe->height != 288) |
| 1673 | return -EINVAL; |
| 1674 | break; |
| 1675 | default: |
| 1676 | return -EINVAL; |
| 1677 | } |
| 1678 | fe->type = V4L2_FRMIVAL_TYPE_DISCRETE; |
| 1679 | fe->discrete.denominator = is_ntsc ? 30000 : 25000; |
| 1680 | fe->discrete.numerator = (is_ntsc ? 1001 : 1000) * frm_dec[fe->index]; |
| 1681 | dprintk(4, "%s discrete %d/%d\n", __func__, fe->discrete.numerator, |
| 1682 | fe->discrete.denominator); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1686 | static int s2255_open(struct file *file) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1687 | { |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 1688 | struct video_device *vdev = video_devdata(file); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1689 | struct s2255_channel *channel = video_drvdata(file); |
| 1690 | struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1691 | struct s2255_fh *fh; |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 1692 | enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1693 | int state; |
Laurent Pinchart | 50462eb | 2009-12-10 11:47:13 -0200 | [diff] [blame] | 1694 | dprintk(1, "s2255: open called (dev=%s)\n", |
| 1695 | video_device_node_name(vdev)); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1696 | /* |
| 1697 | * open lock necessary to prevent multiple instances |
| 1698 | * of v4l-conf (or other programs) from simultaneously |
| 1699 | * reloading firmware. |
| 1700 | */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1701 | mutex_lock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1702 | state = atomic_read(&dev->fw_data->fw_state); |
| 1703 | switch (state) { |
| 1704 | case S2255_FW_DISCONNECTING: |
| 1705 | mutex_unlock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1706 | return -ENODEV; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1707 | case S2255_FW_FAILED: |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 1708 | s2255_dev_err(&dev->udev->dev, |
| 1709 | "firmware load failed. retrying.\n"); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1710 | s2255_fwload_start(dev, 1); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1711 | wait_event_timeout(dev->fw_data->wait_fw, |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1712 | ((atomic_read(&dev->fw_data->fw_state) |
| 1713 | == S2255_FW_SUCCESS) || |
| 1714 | (atomic_read(&dev->fw_data->fw_state) |
| 1715 | == S2255_FW_DISCONNECTING)), |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1716 | msecs_to_jiffies(S2255_LOAD_TIMEOUT)); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1717 | /* state may have changed, re-read */ |
| 1718 | state = atomic_read(&dev->fw_data->fw_state); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1719 | break; |
| 1720 | case S2255_FW_NOTLOADED: |
| 1721 | case S2255_FW_LOADED_DSPWAIT: |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1722 | /* give S2255_LOAD_TIMEOUT time for firmware to load in case |
| 1723 | driver loaded and then device immediately opened */ |
| 1724 | printk(KERN_INFO "%s waiting for firmware load\n", __func__); |
| 1725 | wait_event_timeout(dev->fw_data->wait_fw, |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1726 | ((atomic_read(&dev->fw_data->fw_state) |
| 1727 | == S2255_FW_SUCCESS) || |
| 1728 | (atomic_read(&dev->fw_data->fw_state) |
| 1729 | == S2255_FW_DISCONNECTING)), |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1730 | msecs_to_jiffies(S2255_LOAD_TIMEOUT)); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1731 | /* state may have changed, re-read */ |
| 1732 | state = atomic_read(&dev->fw_data->fw_state); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1733 | break; |
| 1734 | case S2255_FW_SUCCESS: |
| 1735 | default: |
| 1736 | break; |
| 1737 | } |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1738 | /* state may have changed in above switch statement */ |
| 1739 | switch (state) { |
| 1740 | case S2255_FW_SUCCESS: |
| 1741 | break; |
| 1742 | case S2255_FW_FAILED: |
| 1743 | printk(KERN_INFO "2255 firmware load failed.\n"); |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1744 | mutex_unlock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1745 | return -ENODEV; |
| 1746 | case S2255_FW_DISCONNECTING: |
| 1747 | printk(KERN_INFO "%s: disconnecting\n", __func__); |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1748 | mutex_unlock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1749 | return -ENODEV; |
| 1750 | case S2255_FW_LOADED_DSPWAIT: |
| 1751 | case S2255_FW_NOTLOADED: |
| 1752 | printk(KERN_INFO "%s: firmware not loaded yet" |
| 1753 | "please try again later\n", |
| 1754 | __func__); |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1755 | /* |
| 1756 | * Timeout on firmware load means device unusable. |
| 1757 | * Set firmware failure state. |
| 1758 | * On next s2255_open the firmware will be reloaded. |
| 1759 | */ |
| 1760 | atomic_set(&dev->fw_data->fw_state, |
| 1761 | S2255_FW_FAILED); |
| 1762 | mutex_unlock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1763 | return -EAGAIN; |
| 1764 | default: |
| 1765 | printk(KERN_INFO "%s: unknown state\n", __func__); |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1766 | mutex_unlock(&dev->open_lock); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1767 | return -EFAULT; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1768 | } |
Dean Anderson | eb78dee | 2010-04-12 15:05:37 -0300 | [diff] [blame] | 1769 | mutex_unlock(&dev->open_lock); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1770 | /* allocate + initialize per filehandle data */ |
| 1771 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); |
Dean Anderson | a5ef91c | 2010-04-08 23:46:08 -0300 | [diff] [blame] | 1772 | if (NULL == fh) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1773 | return -ENOMEM; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1774 | file->private_data = fh; |
| 1775 | fh->dev = dev; |
| 1776 | fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1777 | fh->channel = channel; |
| 1778 | if (!channel->configured) { |
| 1779 | /* configure channel to default state */ |
| 1780 | channel->fmt = &formats[0]; |
| 1781 | s2255_set_mode(channel, &channel->mode); |
| 1782 | channel->configured = 1; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1783 | } |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1784 | dprintk(1, "%s: dev=%s type=%s\n", __func__, |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1785 | video_device_node_name(vdev), v4l2_type_names[type]); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1786 | dprintk(2, "%s: fh=0x%08lx, dev=0x%08lx, vidq=0x%08lx\n", __func__, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1787 | (unsigned long)fh, (unsigned long)dev, |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1788 | (unsigned long)&channel->vidq); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1789 | dprintk(4, "%s: list_empty active=%d\n", __func__, |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1790 | list_empty(&channel->vidq.active)); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1791 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &s2255_video_qops, |
| 1792 | NULL, &dev->slock, |
| 1793 | fh->type, |
| 1794 | V4L2_FIELD_INTERLACED, |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 1795 | sizeof(struct s2255_buffer), |
| 1796 | fh, vdev->lock); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | |
| 1801 | static unsigned int s2255_poll(struct file *file, |
| 1802 | struct poll_table_struct *wait) |
| 1803 | { |
| 1804 | struct s2255_fh *fh = file->private_data; |
| 1805 | int rc; |
| 1806 | dprintk(100, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1807 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) |
| 1808 | return POLLERR; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1809 | rc = videobuf_poll_stream(file, &fh->vb_vidq, wait); |
| 1810 | return rc; |
| 1811 | } |
| 1812 | |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 1813 | static void s2255_destroy(struct s2255_dev *dev) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1814 | { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1815 | /* board shutdown stops the read pipe if it is running */ |
| 1816 | s2255_board_shutdown(dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1817 | /* make sure firmware still not trying to load */ |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1818 | del_timer(&dev->timer); /* only started in .probe and .open */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1819 | if (dev->fw_data->fw_urb) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1820 | usb_kill_urb(dev->fw_data->fw_urb); |
| 1821 | usb_free_urb(dev->fw_data->fw_urb); |
| 1822 | dev->fw_data->fw_urb = NULL; |
| 1823 | } |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1824 | if (dev->fw_data->fw) |
| 1825 | release_firmware(dev->fw_data->fw); |
| 1826 | kfree(dev->fw_data->pfw_data); |
| 1827 | kfree(dev->fw_data); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1828 | /* reset the DSP so firmware can be reloaded next time */ |
| 1829 | s2255_reset_dsppower(dev); |
| 1830 | mutex_destroy(&dev->open_lock); |
| 1831 | mutex_destroy(&dev->lock); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1832 | usb_put_dev(dev->udev); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1833 | v4l2_device_unregister(&dev->v4l2_dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1834 | dprintk(1, "%s", __func__); |
Dean Anderson | b7732a3 | 2009-03-30 11:59:56 -0300 | [diff] [blame] | 1835 | kfree(dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1836 | } |
| 1837 | |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1838 | static int s2255_release(struct file *file) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1839 | { |
| 1840 | struct s2255_fh *fh = file->private_data; |
| 1841 | struct s2255_dev *dev = fh->dev; |
Laurent Pinchart | 50462eb | 2009-12-10 11:47:13 -0200 | [diff] [blame] | 1842 | struct video_device *vdev = video_devdata(file); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1843 | struct s2255_channel *channel = fh->channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1844 | if (!dev) |
| 1845 | return -ENODEV; |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1846 | /* turn off stream */ |
| 1847 | if (res_check(fh)) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1848 | if (channel->b_acquire) |
| 1849 | s2255_stop_acquire(fh->channel); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1850 | videobuf_streamoff(&fh->vb_vidq); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1851 | res_free(fh); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1852 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1853 | videobuf_mmap_free(&fh->vb_vidq); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1854 | dprintk(1, "%s (dev=%s)\n", __func__, video_device_node_name(vdev)); |
Dean Anderson | f78d92c | 2008-07-22 14:43:27 -0300 | [diff] [blame] | 1855 | kfree(fh); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | static int s2255_mmap_v4l(struct file *file, struct vm_area_struct *vma) |
| 1860 | { |
| 1861 | struct s2255_fh *fh = file->private_data; |
| 1862 | int ret; |
| 1863 | |
| 1864 | if (!fh) |
| 1865 | return -ENODEV; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1866 | dprintk(4, "%s, vma=0x%08lx\n", __func__, (unsigned long)vma); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1867 | ret = videobuf_mmap_mapper(&fh->vb_vidq, vma); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 1868 | dprintk(4, "%s vma start=0x%08lx, size=%ld, ret=%d\n", __func__, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1869 | (unsigned long)vma->vm_start, |
| 1870 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1871 | return ret; |
| 1872 | } |
| 1873 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1874 | static const struct v4l2_file_operations s2255_fops_v4l = { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1875 | .owner = THIS_MODULE, |
| 1876 | .open = s2255_open, |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1877 | .release = s2255_release, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1878 | .poll = s2255_poll, |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 1879 | .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1880 | .mmap = s2255_mmap_v4l, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1881 | }; |
| 1882 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1883 | static const struct v4l2_ioctl_ops s2255_ioctl_ops = { |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 1884 | .vidioc_querymenu = vidioc_querymenu, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1885 | .vidioc_querycap = vidioc_querycap, |
| 1886 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1887 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1888 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1889 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 1890 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1891 | .vidioc_querybuf = vidioc_querybuf, |
| 1892 | .vidioc_qbuf = vidioc_qbuf, |
| 1893 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1894 | .vidioc_s_std = vidioc_s_std, |
| 1895 | .vidioc_enum_input = vidioc_enum_input, |
| 1896 | .vidioc_g_input = vidioc_g_input, |
| 1897 | .vidioc_s_input = vidioc_s_input, |
| 1898 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1899 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1900 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1901 | .vidioc_streamon = vidioc_streamon, |
| 1902 | .vidioc_streamoff = vidioc_streamoff, |
Dean Anderson | 22b88d4 | 2008-08-29 15:33:19 -0300 | [diff] [blame] | 1903 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
| 1904 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, |
Dean Anderson | 7d85353 | 2009-05-15 14:32:04 -0300 | [diff] [blame] | 1905 | .vidioc_s_parm = vidioc_s_parm, |
| 1906 | .vidioc_g_parm = vidioc_g_parm, |
Dean Anderson | e6b44bc | 2010-03-08 20:04:48 -0300 | [diff] [blame] | 1907 | .vidioc_enum_frameintervals = vidioc_enum_frameintervals, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1908 | }; |
| 1909 | |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1910 | static void s2255_video_device_release(struct video_device *vdev) |
| 1911 | { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1912 | struct s2255_dev *dev = to_s2255_dev(vdev->v4l2_dev); |
| 1913 | dprintk(4, "%s, chnls: %d \n", __func__, |
| 1914 | atomic_read(&dev->num_channels)); |
| 1915 | if (atomic_dec_and_test(&dev->num_channels)) |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 1916 | s2255_destroy(dev); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1917 | return; |
| 1918 | } |
| 1919 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1920 | static struct video_device template = { |
| 1921 | .name = "s2255v", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1922 | .fops = &s2255_fops_v4l, |
| 1923 | .ioctl_ops = &s2255_ioctl_ops, |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 1924 | .release = s2255_video_device_release, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1925 | .tvnorms = S2255_NORMS, |
| 1926 | .current_norm = V4L2_STD_NTSC_M, |
| 1927 | }; |
| 1928 | |
| 1929 | static int s2255_probe_v4l(struct s2255_dev *dev) |
| 1930 | { |
| 1931 | int ret; |
| 1932 | int i; |
| 1933 | int cur_nr = video_nr; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1934 | struct s2255_channel *channel; |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 1935 | ret = v4l2_device_register(&dev->interface->dev, &dev->v4l2_dev); |
| 1936 | if (ret) |
| 1937 | return ret; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1938 | /* initialize all video 4 linux */ |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1939 | /* register 4 video devices */ |
| 1940 | for (i = 0; i < MAX_CHANNELS; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1941 | channel = &dev->channel[i]; |
| 1942 | INIT_LIST_HEAD(&channel->vidq.active); |
| 1943 | channel->vidq.dev = dev; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1944 | /* register 4 video devices */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1945 | channel->vdev = template; |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 1946 | channel->vdev.lock = &dev->lock; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1947 | channel->vdev.v4l2_dev = &dev->v4l2_dev; |
| 1948 | video_set_drvdata(&channel->vdev, channel); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1949 | if (video_nr == -1) |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1950 | ret = video_register_device(&channel->vdev, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1951 | VFL_TYPE_GRABBER, |
| 1952 | video_nr); |
| 1953 | else |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1954 | ret = video_register_device(&channel->vdev, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1955 | VFL_TYPE_GRABBER, |
| 1956 | cur_nr + i); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1957 | |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1958 | if (ret) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1959 | dev_err(&dev->udev->dev, |
| 1960 | "failed to register video device!\n"); |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1961 | break; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1962 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1963 | atomic_inc(&dev->num_channels); |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 1964 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1965 | video_device_node_name(&channel->vdev)); |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1966 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1967 | } |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 1968 | printk(KERN_INFO "Sensoray 2255 V4L driver Revision: %d.%d\n", |
| 1969 | S2255_MAJOR_VERSION, |
| 1970 | S2255_MINOR_VERSION); |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1971 | /* if no channels registered, return error and probe will fail*/ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1972 | if (atomic_read(&dev->num_channels) == 0) { |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 1973 | v4l2_device_unregister(&dev->v4l2_dev); |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1974 | return ret; |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 1975 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 1976 | if (atomic_read(&dev->num_channels) != MAX_CHANNELS) |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 1977 | printk(KERN_WARNING "s2255: Not all channels available.\n"); |
| 1978 | return 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1979 | } |
| 1980 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1981 | /* this function moves the usb stream read pipe data |
| 1982 | * into the system buffers. |
| 1983 | * returns 0 on success, EAGAIN if more data to process( call this |
| 1984 | * function again). |
| 1985 | * |
| 1986 | * Received frame structure: |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1987 | * bytes 0-3: marker : 0x2255DA4AL (S2255_MARKER_FRAME) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1988 | * bytes 4-7: channel: 0-3 |
| 1989 | * bytes 8-11: payload size: size of the frame |
| 1990 | * bytes 12-payloadsize+12: frame data |
| 1991 | */ |
| 1992 | static int save_frame(struct s2255_dev *dev, struct s2255_pipeinfo *pipe_info) |
| 1993 | { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1994 | char *pdest; |
| 1995 | u32 offset = 0; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 1996 | int bframe = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 1997 | char *psrc; |
| 1998 | unsigned long copy_size; |
| 1999 | unsigned long size; |
| 2000 | s32 idx = -1; |
| 2001 | struct s2255_framei *frm; |
| 2002 | unsigned char *pdata; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2003 | struct s2255_channel *channel; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2004 | dprintk(100, "buffer to user\n"); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2005 | channel = &dev->channel[dev->cc]; |
| 2006 | idx = channel->cur_frame; |
| 2007 | frm = &channel->buffer.frame[idx]; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2008 | if (frm->ulState == S2255_READ_IDLE) { |
| 2009 | int jj; |
| 2010 | unsigned int cc; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 2011 | __le32 *pdword; /*data from dsp is little endian */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2012 | int payload; |
| 2013 | /* search for marker codes */ |
| 2014 | pdata = (unsigned char *)pipe_info->transfer_buffer; |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 2015 | pdword = (__le32 *)pdata; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2016 | for (jj = 0; jj < (pipe_info->cur_transfer_size - 12); jj++) { |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 2017 | switch (*pdword) { |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2018 | case S2255_MARKER_FRAME: |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2019 | dprintk(4, "found frame marker at offset:" |
| 2020 | " %d [%x %x]\n", jj, pdata[0], |
| 2021 | pdata[1]); |
| 2022 | offset = jj + PREFIX_SIZE; |
| 2023 | bframe = 1; |
| 2024 | cc = pdword[1]; |
| 2025 | if (cc >= MAX_CHANNELS) { |
| 2026 | printk(KERN_ERR |
| 2027 | "bad channel\n"); |
| 2028 | return -EINVAL; |
| 2029 | } |
| 2030 | /* reverse it */ |
| 2031 | dev->cc = G_chnmap[cc]; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2032 | channel = &dev->channel[dev->cc]; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2033 | payload = pdword[3]; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2034 | if (payload > channel->req_image_size) { |
| 2035 | channel->bad_payload++; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2036 | /* discard the bad frame */ |
| 2037 | return -EINVAL; |
| 2038 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2039 | channel->pkt_size = payload; |
| 2040 | channel->jpg_size = pdword[4]; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2041 | break; |
| 2042 | case S2255_MARKER_RESPONSE: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2043 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2044 | pdata += DEF_USB_BLOCK; |
| 2045 | jj += DEF_USB_BLOCK; |
| 2046 | if (pdword[1] >= MAX_CHANNELS) |
| 2047 | break; |
| 2048 | cc = G_chnmap[pdword[1]]; |
Roel Kluin | f14a297 | 2009-10-23 07:59:42 -0300 | [diff] [blame] | 2049 | if (cc >= MAX_CHANNELS) |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2050 | break; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2051 | channel = &dev->channel[cc]; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2052 | switch (pdword[2]) { |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 2053 | case S2255_RESPONSE_SETMODE: |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2054 | /* check if channel valid */ |
| 2055 | /* set mode ready */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2056 | channel->setmode_ready = 1; |
| 2057 | wake_up(&channel->wait_setmode); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2058 | dprintk(5, "setmode ready %d\n", cc); |
| 2059 | break; |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 2060 | case S2255_RESPONSE_FW: |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2061 | dev->chn_ready |= (1 << cc); |
| 2062 | if ((dev->chn_ready & 0x0f) != 0x0f) |
| 2063 | break; |
| 2064 | /* all channels ready */ |
| 2065 | printk(KERN_INFO "s2255: fw loaded\n"); |
| 2066 | atomic_set(&dev->fw_data->fw_state, |
| 2067 | S2255_FW_SUCCESS); |
| 2068 | wake_up(&dev->fw_data->wait_fw); |
| 2069 | break; |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 2070 | case S2255_RESPONSE_STATUS: |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2071 | channel->vidstatus = pdword[3]; |
| 2072 | channel->vidstatus_ready = 1; |
| 2073 | wake_up(&channel->wait_vidstatus); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 2074 | dprintk(5, "got vidstatus %x chan %d\n", |
| 2075 | pdword[3], cc); |
| 2076 | break; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2077 | default: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2078 | printk(KERN_INFO "s2255 unknown resp\n"); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2079 | } |
| 2080 | default: |
| 2081 | pdata++; |
| 2082 | break; |
| 2083 | } |
| 2084 | if (bframe) |
| 2085 | break; |
| 2086 | } /* for */ |
| 2087 | if (!bframe) |
| 2088 | return -EINVAL; |
| 2089 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2090 | channel = &dev->channel[dev->cc]; |
| 2091 | idx = channel->cur_frame; |
| 2092 | frm = &channel->buffer.frame[idx]; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2093 | /* search done. now find out if should be acquiring on this channel */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2094 | if (!channel->b_acquire) { |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2095 | /* we found a frame, but this channel is turned off */ |
| 2096 | frm->ulState = S2255_READ_IDLE; |
| 2097 | return -EINVAL; |
| 2098 | } |
| 2099 | |
| 2100 | if (frm->ulState == S2255_READ_IDLE) { |
| 2101 | frm->ulState = S2255_READ_FRAME; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2102 | frm->cur_size = 0; |
| 2103 | } |
| 2104 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2105 | /* skip the marker 512 bytes (and offset if out of sync) */ |
| 2106 | psrc = (u8 *)pipe_info->transfer_buffer + offset; |
| 2107 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2108 | |
| 2109 | if (frm->lpvbits == NULL) { |
| 2110 | dprintk(1, "s2255 frame buffer == NULL.%p %p %d %d", |
| 2111 | frm, dev, dev->cc, idx); |
| 2112 | return -ENOMEM; |
| 2113 | } |
| 2114 | |
| 2115 | pdest = frm->lpvbits + frm->cur_size; |
| 2116 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2117 | copy_size = (pipe_info->cur_transfer_size - offset); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2118 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2119 | size = channel->pkt_size - PREFIX_SIZE; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2120 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2121 | /* sanity check on pdest */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2122 | if ((copy_size + frm->cur_size) < channel->req_image_size) |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2123 | memcpy(pdest, psrc, copy_size); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2124 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2125 | frm->cur_size += copy_size; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2126 | dprintk(4, "cur_size size %lu size %lu \n", frm->cur_size, size); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2127 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2128 | if (frm->cur_size >= size) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2129 | dprintk(2, "****************[%d]Buffer[%d]full*************\n", |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2130 | dev->cc, idx); |
| 2131 | channel->last_frame = channel->cur_frame; |
| 2132 | channel->cur_frame++; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2133 | /* end of system frame ring buffer, start at zero */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2134 | if ((channel->cur_frame == SYS_FRAMES) || |
| 2135 | (channel->cur_frame == channel->buffer.dwFrames)) |
| 2136 | channel->cur_frame = 0; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2137 | /* frame ready */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2138 | if (channel->b_acquire) |
| 2139 | s2255_got_frame(channel, channel->jpg_size); |
| 2140 | channel->frame_count++; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2141 | frm->ulState = S2255_READ_IDLE; |
| 2142 | frm->cur_size = 0; |
| 2143 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2144 | } |
| 2145 | /* done successfully */ |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | static void s2255_read_video_callback(struct s2255_dev *dev, |
| 2150 | struct s2255_pipeinfo *pipe_info) |
| 2151 | { |
| 2152 | int res; |
| 2153 | dprintk(50, "callback read video \n"); |
| 2154 | |
| 2155 | if (dev->cc >= MAX_CHANNELS) { |
| 2156 | dev->cc = 0; |
| 2157 | dev_err(&dev->udev->dev, "invalid channel\n"); |
| 2158 | return; |
| 2159 | } |
| 2160 | /* otherwise copy to the system buffers */ |
| 2161 | res = save_frame(dev, pipe_info); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2162 | if (res != 0) |
| 2163 | dprintk(4, "s2255: read callback failed\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2164 | |
| 2165 | dprintk(50, "callback read video done\n"); |
| 2166 | return; |
| 2167 | } |
| 2168 | |
| 2169 | static long s2255_vendor_req(struct s2255_dev *dev, unsigned char Request, |
| 2170 | u16 Index, u16 Value, void *TransferBuffer, |
| 2171 | s32 TransferBufferLength, int bOut) |
| 2172 | { |
| 2173 | int r; |
| 2174 | if (!bOut) { |
| 2175 | r = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), |
| 2176 | Request, |
| 2177 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | |
| 2178 | USB_DIR_IN, |
| 2179 | Value, Index, TransferBuffer, |
| 2180 | TransferBufferLength, HZ * 5); |
| 2181 | } else { |
| 2182 | r = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), |
| 2183 | Request, USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 2184 | Value, Index, TransferBuffer, |
| 2185 | TransferBufferLength, HZ * 5); |
| 2186 | } |
| 2187 | return r; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * retrieve FX2 firmware version. future use. |
| 2192 | * @param dev pointer to device extension |
| 2193 | * @return -1 for fail, else returns firmware version as an int(16 bits) |
| 2194 | */ |
| 2195 | static int s2255_get_fx2fw(struct s2255_dev *dev) |
| 2196 | { |
| 2197 | int fw; |
| 2198 | int ret; |
| 2199 | unsigned char transBuffer[64]; |
| 2200 | ret = s2255_vendor_req(dev, S2255_VR_FW, 0, 0, transBuffer, 2, |
| 2201 | S2255_VR_IN); |
| 2202 | if (ret < 0) |
| 2203 | dprintk(2, "get fw error: %x\n", ret); |
| 2204 | fw = transBuffer[0] + (transBuffer[1] << 8); |
| 2205 | dprintk(2, "Get FW %x %x\n", transBuffer[0], transBuffer[1]); |
| 2206 | return fw; |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Create the system ring buffer to copy frames into from the |
| 2211 | * usb read pipe. |
| 2212 | */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2213 | static int s2255_create_sys_buffers(struct s2255_channel *channel) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2214 | { |
| 2215 | unsigned long i; |
| 2216 | unsigned long reqsize; |
| 2217 | dprintk(1, "create sys buffers\n"); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2218 | channel->buffer.dwFrames = SYS_FRAMES; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2219 | /* always allocate maximum size(PAL) for system buffers */ |
| 2220 | reqsize = SYS_FRAMES_MAXSIZE; |
| 2221 | |
| 2222 | if (reqsize > SYS_FRAMES_MAXSIZE) |
| 2223 | reqsize = SYS_FRAMES_MAXSIZE; |
| 2224 | |
| 2225 | for (i = 0; i < SYS_FRAMES; i++) { |
| 2226 | /* allocate the frames */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2227 | channel->buffer.frame[i].lpvbits = vmalloc(reqsize); |
| 2228 | dprintk(1, "valloc %p chan %d, idx %lu, pdata %p\n", |
| 2229 | &channel->buffer.frame[i], channel->idx, i, |
| 2230 | channel->buffer.frame[i].lpvbits); |
| 2231 | channel->buffer.frame[i].size = reqsize; |
| 2232 | if (channel->buffer.frame[i].lpvbits == NULL) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2233 | printk(KERN_INFO "out of memory. using less frames\n"); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2234 | channel->buffer.dwFrames = i; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | /* make sure internal states are set */ |
| 2240 | for (i = 0; i < SYS_FRAMES; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2241 | channel->buffer.frame[i].ulState = 0; |
| 2242 | channel->buffer.frame[i].cur_size = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2243 | } |
| 2244 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2245 | channel->cur_frame = 0; |
| 2246 | channel->last_frame = -1; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2247 | return 0; |
| 2248 | } |
| 2249 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2250 | static int s2255_release_sys_buffers(struct s2255_channel *channel) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2251 | { |
| 2252 | unsigned long i; |
| 2253 | dprintk(1, "release sys buffers\n"); |
| 2254 | for (i = 0; i < SYS_FRAMES; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2255 | if (channel->buffer.frame[i].lpvbits) { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2256 | dprintk(1, "vfree %p\n", |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2257 | channel->buffer.frame[i].lpvbits); |
| 2258 | vfree(channel->buffer.frame[i].lpvbits); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2259 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2260 | channel->buffer.frame[i].lpvbits = NULL; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2261 | } |
| 2262 | return 0; |
| 2263 | } |
| 2264 | |
| 2265 | static int s2255_board_init(struct s2255_dev *dev) |
| 2266 | { |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2267 | struct s2255_mode mode_def = DEF_MODEI_NTSC_CONT; |
| 2268 | int fw_ver; |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2269 | int j; |
| 2270 | struct s2255_pipeinfo *pipe = &dev->pipe; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2271 | dprintk(4, "board init: %p", dev); |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2272 | memset(pipe, 0, sizeof(*pipe)); |
| 2273 | pipe->dev = dev; |
| 2274 | pipe->cur_transfer_size = S2255_USB_XFER_SIZE; |
| 2275 | pipe->max_transfer_size = S2255_USB_XFER_SIZE; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2276 | |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2277 | pipe->transfer_buffer = kzalloc(pipe->max_transfer_size, |
| 2278 | GFP_KERNEL); |
| 2279 | if (pipe->transfer_buffer == NULL) { |
| 2280 | dprintk(1, "out of memory!\n"); |
| 2281 | return -ENOMEM; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2282 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2283 | /* query the firmware */ |
| 2284 | fw_ver = s2255_get_fx2fw(dev); |
| 2285 | |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 2286 | printk(KERN_INFO "2255 usb firmware version %d.%d\n", |
| 2287 | (fw_ver >> 8) & 0xff, |
| 2288 | fw_ver & 0xff); |
| 2289 | |
| 2290 | if (fw_ver < S2255_CUR_USB_FWVER) |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2291 | dev_err(&dev->udev->dev, |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 2292 | "usb firmware not up to date %d.%d\n", |
| 2293 | (fw_ver >> 8) & 0xff, |
| 2294 | fw_ver & 0xff); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2295 | |
| 2296 | for (j = 0; j < MAX_CHANNELS; j++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2297 | struct s2255_channel *channel = &dev->channel[j]; |
| 2298 | channel->b_acquire = 0; |
| 2299 | channel->mode = mode_def; |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 2300 | if (dev->pid == 0x2257 && j > 1) |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2301 | channel->mode.color |= (1 << 16); |
| 2302 | channel->jc.quality = S2255_DEF_JPEG_QUAL; |
| 2303 | channel->width = LINE_SZ_4CIFS_NTSC; |
| 2304 | channel->height = NUM_LINES_4CIFS_NTSC * 2; |
| 2305 | channel->fmt = &formats[0]; |
| 2306 | channel->mode.restart = 1; |
| 2307 | channel->req_image_size = get_transfer_size(&mode_def); |
| 2308 | channel->frame_count = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2309 | /* create the system buffers */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2310 | s2255_create_sys_buffers(channel); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2311 | } |
| 2312 | /* start read pipe */ |
| 2313 | s2255_start_readpipe(dev); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2314 | dprintk(1, "%s: success\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2315 | return 0; |
| 2316 | } |
| 2317 | |
| 2318 | static int s2255_board_shutdown(struct s2255_dev *dev) |
| 2319 | { |
| 2320 | u32 i; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2321 | dprintk(1, "%s: dev: %p", __func__, dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2322 | |
| 2323 | for (i = 0; i < MAX_CHANNELS; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2324 | if (dev->channel[i].b_acquire) |
| 2325 | s2255_stop_acquire(&dev->channel[i]); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2326 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2327 | s2255_stop_readpipe(dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2328 | for (i = 0; i < MAX_CHANNELS; i++) |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2329 | s2255_release_sys_buffers(&dev->channel[i]); |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2330 | /* release transfer buffer */ |
| 2331 | kfree(dev->pipe.transfer_buffer); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
| 2334 | |
| 2335 | static void read_pipe_completion(struct urb *purb) |
| 2336 | { |
| 2337 | struct s2255_pipeinfo *pipe_info; |
| 2338 | struct s2255_dev *dev; |
| 2339 | int status; |
| 2340 | int pipe; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2341 | pipe_info = purb->context; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2342 | dprintk(100, "%s: urb:%p, status %d\n", __func__, purb, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2343 | purb->status); |
| 2344 | if (pipe_info == NULL) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2345 | dev_err(&purb->dev->dev, "no context!\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2346 | return; |
| 2347 | } |
| 2348 | |
| 2349 | dev = pipe_info->dev; |
| 2350 | if (dev == NULL) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2351 | dev_err(&purb->dev->dev, "no context!\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2352 | return; |
| 2353 | } |
| 2354 | status = purb->status; |
Dean Anderson | b02064c | 2009-04-30 12:29:38 -0300 | [diff] [blame] | 2355 | /* if shutting down, do not resubmit, exit immediately */ |
| 2356 | if (status == -ESHUTDOWN) { |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2357 | dprintk(2, "%s: err shutdown\n", __func__); |
Dean Anderson | b02064c | 2009-04-30 12:29:38 -0300 | [diff] [blame] | 2358 | pipe_info->err_count++; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2359 | return; |
| 2360 | } |
| 2361 | |
| 2362 | if (pipe_info->state == 0) { |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2363 | dprintk(2, "%s: exiting USB pipe", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2364 | return; |
| 2365 | } |
| 2366 | |
Dean Anderson | b02064c | 2009-04-30 12:29:38 -0300 | [diff] [blame] | 2367 | if (status == 0) |
| 2368 | s2255_read_video_callback(dev, pipe_info); |
| 2369 | else { |
| 2370 | pipe_info->err_count++; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2371 | dprintk(1, "%s: failed URB %d\n", __func__, status); |
Dean Anderson | b02064c | 2009-04-30 12:29:38 -0300 | [diff] [blame] | 2372 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2373 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2374 | pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint); |
| 2375 | /* reuse urb */ |
| 2376 | usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev, |
| 2377 | pipe, |
| 2378 | pipe_info->transfer_buffer, |
| 2379 | pipe_info->cur_transfer_size, |
| 2380 | read_pipe_completion, pipe_info); |
| 2381 | |
| 2382 | if (pipe_info->state != 0) { |
| 2383 | if (usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL)) { |
| 2384 | dev_err(&dev->udev->dev, "error submitting urb\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2385 | } |
| 2386 | } else { |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2387 | dprintk(2, "%s :complete state 0\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2388 | } |
| 2389 | return; |
| 2390 | } |
| 2391 | |
| 2392 | static int s2255_start_readpipe(struct s2255_dev *dev) |
| 2393 | { |
| 2394 | int pipe; |
| 2395 | int retval; |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2396 | struct s2255_pipeinfo *pipe_info = &dev->pipe; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2397 | pipe = usb_rcvbulkpipe(dev->udev, dev->read_endpoint); |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2398 | dprintk(2, "%s: IN %d\n", __func__, dev->read_endpoint); |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2399 | pipe_info->state = 1; |
| 2400 | pipe_info->err_count = 0; |
| 2401 | pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2402 | if (!pipe_info->stream_urb) { |
| 2403 | dev_err(&dev->udev->dev, |
| 2404 | "ReadStream: Unable to alloc URB\n"); |
| 2405 | return -ENOMEM; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2406 | } |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2407 | /* transfer buffer allocated in board_init */ |
| 2408 | usb_fill_bulk_urb(pipe_info->stream_urb, dev->udev, |
| 2409 | pipe, |
| 2410 | pipe_info->transfer_buffer, |
| 2411 | pipe_info->cur_transfer_size, |
| 2412 | read_pipe_completion, pipe_info); |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2413 | retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL); |
| 2414 | if (retval) { |
| 2415 | printk(KERN_ERR "s2255: start read pipe failed\n"); |
| 2416 | return retval; |
| 2417 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | /* starts acquisition process */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2422 | static int s2255_start_acquire(struct s2255_channel *channel) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2423 | { |
| 2424 | unsigned char *buffer; |
| 2425 | int res; |
| 2426 | unsigned long chn_rev; |
| 2427 | int j; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2428 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
| 2429 | chn_rev = G_chnmap[channel->idx]; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2430 | buffer = kzalloc(512, GFP_KERNEL); |
| 2431 | if (buffer == NULL) { |
| 2432 | dev_err(&dev->udev->dev, "out of mem\n"); |
| 2433 | return -ENOMEM; |
| 2434 | } |
| 2435 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2436 | channel->last_frame = -1; |
| 2437 | channel->bad_payload = 0; |
| 2438 | channel->cur_frame = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2439 | for (j = 0; j < SYS_FRAMES; j++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2440 | channel->buffer.frame[j].ulState = 0; |
| 2441 | channel->buffer.frame[j].cur_size = 0; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /* send the start command */ |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 2445 | *(__le32 *) buffer = IN_DATA_TOKEN; |
| 2446 | *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev); |
| 2447 | *((__le32 *) buffer + 2) = CMD_START; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2448 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
| 2449 | if (res != 0) |
| 2450 | dev_err(&dev->udev->dev, "CMD_START error\n"); |
| 2451 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2452 | dprintk(2, "start acquire exit[%d] %d \n", channel->idx, res); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2453 | kfree(buffer); |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2457 | static int s2255_stop_acquire(struct s2255_channel *channel) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2458 | { |
| 2459 | unsigned char *buffer; |
| 2460 | int res; |
| 2461 | unsigned long chn_rev; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2462 | struct s2255_dev *dev = to_s2255_dev(channel->vdev.v4l2_dev); |
| 2463 | chn_rev = G_chnmap[channel->idx]; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2464 | buffer = kzalloc(512, GFP_KERNEL); |
| 2465 | if (buffer == NULL) { |
| 2466 | dev_err(&dev->udev->dev, "out of mem\n"); |
| 2467 | return -ENOMEM; |
| 2468 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2469 | /* send the stop command */ |
Dean Anderson | 3fa0060 | 2010-03-04 20:47:33 -0300 | [diff] [blame] | 2470 | *(__le32 *) buffer = IN_DATA_TOKEN; |
| 2471 | *((__le32 *) buffer + 1) = (__le32) cpu_to_le32(chn_rev); |
| 2472 | *((__le32 *) buffer + 2) = CMD_STOP; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2473 | res = s2255_write_config(dev->udev, (unsigned char *)buffer, 512); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2474 | if (res != 0) |
| 2475 | dev_err(&dev->udev->dev, "CMD_STOP error\n"); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2476 | kfree(buffer); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2477 | channel->b_acquire = 0; |
| 2478 | dprintk(4, "%s: chn %d, res %d\n", __func__, channel->idx, res); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2479 | return res; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | static void s2255_stop_readpipe(struct s2255_dev *dev) |
| 2483 | { |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2484 | struct s2255_pipeinfo *pipe = &dev->pipe; |
Dan Carpenter | 8b661b5 | 2010-05-05 03:00:47 -0300 | [diff] [blame] | 2485 | |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2486 | pipe->state = 0; |
| 2487 | if (pipe->stream_urb) { |
| 2488 | /* cancel urb */ |
| 2489 | usb_kill_urb(pipe->stream_urb); |
| 2490 | usb_free_urb(pipe->stream_urb); |
| 2491 | pipe->stream_urb = NULL; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2492 | } |
Dean Anderson | ab85c6a | 2010-04-08 23:39:12 -0300 | [diff] [blame] | 2493 | dprintk(4, "%s", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2497 | static void s2255_fwload_start(struct s2255_dev *dev, int reset) |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2498 | { |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2499 | if (reset) |
| 2500 | s2255_reset_dsppower(dev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2501 | dev->fw_data->fw_size = dev->fw_data->fw->size; |
| 2502 | atomic_set(&dev->fw_data->fw_state, S2255_FW_NOTLOADED); |
| 2503 | memcpy(dev->fw_data->pfw_data, |
| 2504 | dev->fw_data->fw->data, CHUNK_SIZE); |
| 2505 | dev->fw_data->fw_loaded = CHUNK_SIZE; |
| 2506 | usb_fill_bulk_urb(dev->fw_data->fw_urb, dev->udev, |
| 2507 | usb_sndbulkpipe(dev->udev, 2), |
| 2508 | dev->fw_data->pfw_data, |
| 2509 | CHUNK_SIZE, s2255_fwchunk_complete, |
| 2510 | dev->fw_data); |
| 2511 | mod_timer(&dev->timer, jiffies + HZ); |
| 2512 | } |
| 2513 | |
| 2514 | /* standard usb probe function */ |
| 2515 | static int s2255_probe(struct usb_interface *interface, |
| 2516 | const struct usb_device_id *id) |
| 2517 | { |
| 2518 | struct s2255_dev *dev = NULL; |
| 2519 | struct usb_host_interface *iface_desc; |
| 2520 | struct usb_endpoint_descriptor *endpoint; |
| 2521 | int i; |
| 2522 | int retval = -ENOMEM; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2523 | __le32 *pdata; |
| 2524 | int fw_size; |
Dean Anderson | 85b8548 | 2010-04-08 23:51:17 -0300 | [diff] [blame] | 2525 | dprintk(2, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2526 | /* allocate memory for our device state and initialize it to zero */ |
| 2527 | dev = kzalloc(sizeof(struct s2255_dev), GFP_KERNEL); |
| 2528 | if (dev == NULL) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2529 | s2255_dev_err(&interface->dev, "out of memory\n"); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2530 | return -ENOMEM; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2531 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2532 | atomic_set(&dev->num_channels, 0); |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 2533 | dev->pid = id->idProduct; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2534 | dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL); |
| 2535 | if (!dev->fw_data) |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2536 | goto errorFWDATA1; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2537 | mutex_init(&dev->lock); |
| 2538 | mutex_init(&dev->open_lock); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2539 | /* grab usb_device and save it */ |
| 2540 | dev->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 2541 | if (dev->udev == NULL) { |
| 2542 | dev_err(&interface->dev, "null usb device\n"); |
| 2543 | retval = -ENODEV; |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2544 | goto errorUDEV; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2545 | } |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 2546 | dprintk(1, "dev: %p, udev %p interface %p\n", dev, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2547 | dev->udev, interface); |
| 2548 | dev->interface = interface; |
| 2549 | /* set up the endpoint information */ |
| 2550 | iface_desc = interface->cur_altsetting; |
| 2551 | dprintk(1, "num endpoints %d\n", iface_desc->desc.bNumEndpoints); |
| 2552 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 2553 | endpoint = &iface_desc->endpoint[i].desc; |
| 2554 | if (!dev->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) { |
| 2555 | /* we found the bulk in endpoint */ |
| 2556 | dev->read_endpoint = endpoint->bEndpointAddress; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (!dev->read_endpoint) { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2561 | dev_err(&interface->dev, "Could not find bulk-in endpoint\n"); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2562 | goto errorEP; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2563 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2564 | init_timer(&dev->timer); |
| 2565 | dev->timer.function = s2255_timer; |
| 2566 | dev->timer.data = (unsigned long)dev->fw_data; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2567 | init_waitqueue_head(&dev->fw_data->wait_fw); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 2568 | for (i = 0; i < MAX_CHANNELS; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2569 | struct s2255_channel *channel = &dev->channel[i]; |
| 2570 | dev->channel[i].idx = i; |
| 2571 | init_waitqueue_head(&channel->wait_setmode); |
| 2572 | init_waitqueue_head(&channel->wait_vidstatus); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 2573 | } |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2574 | |
| 2575 | dev->fw_data->fw_urb = usb_alloc_urb(0, GFP_KERNEL); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2576 | if (!dev->fw_data->fw_urb) { |
| 2577 | dev_err(&interface->dev, "out of memory!\n"); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2578 | goto errorFWURB; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2579 | } |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2580 | |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2581 | dev->fw_data->pfw_data = kzalloc(CHUNK_SIZE, GFP_KERNEL); |
| 2582 | if (!dev->fw_data->pfw_data) { |
| 2583 | dev_err(&interface->dev, "out of memory!\n"); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2584 | goto errorFWDATA2; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2585 | } |
| 2586 | /* load the first chunk */ |
| 2587 | if (request_firmware(&dev->fw_data->fw, |
| 2588 | FIRMWARE_FILE_NAME, &dev->udev->dev)) { |
| 2589 | printk(KERN_ERR "sensoray 2255 failed to get firmware\n"); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2590 | goto errorREQFW; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2591 | } |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2592 | /* check the firmware is valid */ |
| 2593 | fw_size = dev->fw_data->fw->size; |
| 2594 | pdata = (__le32 *) &dev->fw_data->fw->data[fw_size - 8]; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2595 | |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2596 | if (*pdata != S2255_FW_MARKER) { |
| 2597 | printk(KERN_INFO "Firmware invalid.\n"); |
| 2598 | retval = -ENODEV; |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2599 | goto errorFWMARKER; |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2600 | } else { |
| 2601 | /* make sure firmware is the latest */ |
| 2602 | __le32 *pRel; |
| 2603 | pRel = (__le32 *) &dev->fw_data->fw->data[fw_size - 4]; |
| 2604 | printk(KERN_INFO "s2255 dsp fw version %x\n", *pRel); |
Dean Anderson | 4de39f5 | 2010-03-03 19:39:19 -0300 | [diff] [blame] | 2605 | dev->dsp_fw_ver = *pRel; |
| 2606 | if (*pRel < S2255_CUR_DSP_FWVER) |
| 2607 | printk(KERN_INFO "s2255: f2255usb.bin out of date.\n"); |
Dean Anderson | 5a34d9d | 2010-03-05 19:59:48 -0300 | [diff] [blame] | 2608 | if (dev->pid == 0x2257 && *pRel < S2255_MIN_DSP_COLORFILTER) |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2609 | printk(KERN_WARNING "s2255: 2257 requires firmware %d" |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2610 | " or above.\n", S2255_MIN_DSP_COLORFILTER); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2611 | } |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2612 | usb_reset_device(dev->udev); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2613 | /* load 2255 board specific */ |
Dean Anderson | abce21f | 2009-04-23 16:04:41 -0300 | [diff] [blame] | 2614 | retval = s2255_board_init(dev); |
| 2615 | if (retval) |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2616 | goto errorBOARDINIT; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2617 | spin_lock_init(&dev->slock); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2618 | s2255_fwload_start(dev, 0); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2619 | /* loads v4l specific */ |
| 2620 | retval = s2255_probe_v4l(dev); |
| 2621 | if (retval) |
Dean Anderson | 3a67b5cc | 2010-04-08 23:52:20 -0300 | [diff] [blame] | 2622 | goto errorBOARDINIT; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2623 | dev_info(&interface->dev, "Sensoray 2255 detected\n"); |
| 2624 | return 0; |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2625 | errorBOARDINIT: |
| 2626 | s2255_board_shutdown(dev); |
| 2627 | errorFWMARKER: |
| 2628 | release_firmware(dev->fw_data->fw); |
| 2629 | errorREQFW: |
| 2630 | kfree(dev->fw_data->pfw_data); |
| 2631 | errorFWDATA2: |
| 2632 | usb_free_urb(dev->fw_data->fw_urb); |
| 2633 | errorFWURB: |
| 2634 | del_timer(&dev->timer); |
| 2635 | errorEP: |
| 2636 | usb_put_dev(dev->udev); |
| 2637 | errorUDEV: |
| 2638 | kfree(dev->fw_data); |
| 2639 | mutex_destroy(&dev->open_lock); |
| 2640 | mutex_destroy(&dev->lock); |
| 2641 | errorFWDATA1: |
| 2642 | kfree(dev); |
| 2643 | printk(KERN_WARNING "Sensoray 2255 driver load failed: 0x%x\n", retval); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2644 | return retval; |
| 2645 | } |
| 2646 | |
| 2647 | /* disconnect routine. when board is removed physically or with rmmod */ |
| 2648 | static void s2255_disconnect(struct usb_interface *interface) |
| 2649 | { |
Dean Anderson | 65c6edb | 2010-04-20 17:21:32 -0300 | [diff] [blame] | 2650 | struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface)); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2651 | int i; |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2652 | int channels = atomic_read(&dev->num_channels); |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 2653 | mutex_lock(&dev->lock); |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2654 | v4l2_device_disconnect(&dev->v4l2_dev); |
Pete Eberlein | a19a5cd | 2010-12-20 19:18:59 -0300 | [diff] [blame] | 2655 | mutex_unlock(&dev->lock); |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 2656 | /*see comments in the uvc_driver.c usb disconnect function */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2657 | atomic_inc(&dev->num_channels); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2658 | /* unregister each video device. */ |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2659 | for (i = 0; i < channels; i++) |
| 2660 | video_unregister_device(&dev->channel[i].vdev); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2661 | /* wake up any of our timers */ |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2662 | atomic_set(&dev->fw_data->fw_state, S2255_FW_DISCONNECTING); |
| 2663 | wake_up(&dev->fw_data->wait_fw); |
| 2664 | for (i = 0; i < MAX_CHANNELS; i++) { |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2665 | dev->channel[i].setmode_ready = 1; |
| 2666 | wake_up(&dev->channel[i].wait_setmode); |
| 2667 | dev->channel[i].vidstatus_ready = 1; |
| 2668 | wake_up(&dev->channel[i].wait_vidstatus); |
Dean Anderson | 14d9626 | 2008-08-25 13:58:55 -0300 | [diff] [blame] | 2669 | } |
Dean Anderson | fe85ce9 | 2010-06-01 19:12:07 -0300 | [diff] [blame] | 2670 | if (atomic_dec_and_test(&dev->num_channels)) |
Dean Anderson | d62e85a | 2010-04-09 19:54:26 -0300 | [diff] [blame] | 2671 | s2255_destroy(dev); |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2672 | dev_info(&interface->dev, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | static struct usb_driver s2255_driver = { |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2676 | .name = S2255_DRIVER_NAME, |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2677 | .probe = s2255_probe, |
| 2678 | .disconnect = s2255_disconnect, |
| 2679 | .id_table = s2255_table, |
| 2680 | }; |
| 2681 | |
| 2682 | static int __init usb_s2255_init(void) |
| 2683 | { |
| 2684 | int result; |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2685 | /* register this driver with the USB subsystem */ |
| 2686 | result = usb_register(&s2255_driver); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2687 | if (result) |
Mauro Carvalho Chehab | be9ed51 | 2009-01-08 09:13:42 -0300 | [diff] [blame] | 2688 | pr_err(KBUILD_MODNAME |
Dean Anderson | ff7e22d | 2010-04-08 23:38:07 -0300 | [diff] [blame] | 2689 | ": usb_register failed. Error number %d\n", result); |
| 2690 | dprintk(2, "%s\n", __func__); |
Dean Anderson | 38f993a | 2008-06-26 23:15:51 -0300 | [diff] [blame] | 2691 | return result; |
| 2692 | } |
| 2693 | |
| 2694 | static void __exit usb_s2255_exit(void) |
| 2695 | { |
| 2696 | usb_deregister(&s2255_driver); |
| 2697 | } |
| 2698 | |
| 2699 | module_init(usb_s2255_init); |
| 2700 | module_exit(usb_s2255_exit); |
| 2701 | |
| 2702 | MODULE_DESCRIPTION("Sensoray 2255 Video for Linux driver"); |
| 2703 | MODULE_AUTHOR("Dean Anderson (Sensoray Company Inc.)"); |
| 2704 | MODULE_LICENSE("GPL"); |