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