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