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