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