Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Mirics MSi3101 SDR Dongle driver |
| 3 | * |
| 4 | * Copyright (C) 2013 Antti Palosaari <crope@iki.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 19 | * |
| 20 | * That driver is somehow based of pwc driver: |
| 21 | * (C) 1999-2004 Nemosoft Unv. |
| 22 | * (C) 2004-2006 Luc Saillard (luc@saillard.org) |
| 23 | * (C) 2011 Hans de Goede <hdegoede@redhat.com> |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 24 | */ |
| 25 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 26 | #include <linux/module.h> |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 27 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 04e40bd | 2013-08-24 13:35:14 -0300 | [diff] [blame] | 28 | #include <asm/div64.h> |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 29 | #include <media/v4l2-device.h> |
| 30 | #include <media/v4l2-ioctl.h> |
| 31 | #include <media/v4l2-ctrls.h> |
| 32 | #include <media/v4l2-event.h> |
| 33 | #include <linux/usb.h> |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 34 | #include <media/videobuf2-vmalloc.h> |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 35 | #include <linux/spi/spi.h> |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 36 | |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 37 | static bool msi3101_emulated_fmt; |
| 38 | module_param_named(emulated_formats, msi3101_emulated_fmt, bool, 0644); |
| 39 | MODULE_PARM_DESC(emulated_formats, "enable emulated formats (disappears in future)"); |
| 40 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 41 | /* |
| 42 | * iConfiguration 0 |
| 43 | * bInterfaceNumber 0 |
| 44 | * bAlternateSetting 1 |
| 45 | * bNumEndpoints 1 |
| 46 | * bEndpointAddress 0x81 EP 1 IN |
| 47 | * bmAttributes 1 |
| 48 | * Transfer Type Isochronous |
| 49 | * wMaxPacketSize 0x1400 3x 1024 bytes |
| 50 | * bInterval 1 |
| 51 | */ |
| 52 | #define MAX_ISO_BUFS (8) |
| 53 | #define ISO_FRAMES_PER_DESC (8) |
| 54 | #define ISO_MAX_FRAME_SIZE (3 * 1024) |
| 55 | #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 56 | #define MAX_ISOC_ERRORS 20 |
| 57 | |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 58 | /* TODO: These should be moved to V4L2 API */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 59 | #define V4L2_PIX_FMT_SDR_S12 v4l2_fourcc('D', 'S', '1', '2') /* signed 12-bit */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 60 | #define V4L2_PIX_FMT_SDR_MSI2500_384 v4l2_fourcc('M', '3', '8', '4') /* Mirics MSi2500 format 384 */ |
| 61 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 62 | static const struct v4l2_frequency_band bands[] = { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 63 | { |
| 64 | .tuner = 0, |
| 65 | .type = V4L2_TUNER_ADC, |
| 66 | .index = 0, |
| 67 | .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, |
| 68 | .rangelow = 1200000, |
| 69 | .rangehigh = 15000000, |
| 70 | }, |
| 71 | }; |
| 72 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 73 | /* stream formats */ |
| 74 | struct msi3101_format { |
| 75 | char *name; |
| 76 | u32 pixelformat; |
| 77 | }; |
| 78 | |
| 79 | /* format descriptions for capture and preview */ |
| 80 | static struct msi3101_format formats[] = { |
| 81 | { |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 82 | .name = "Complex S8", |
| 83 | .pixelformat = V4L2_SDR_FMT_CS8, |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 84 | #if 0 |
| 85 | }, { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 86 | .name = "10+2-bit signed", |
| 87 | .pixelformat = V4L2_PIX_FMT_SDR_MSI2500_384, |
| 88 | }, { |
| 89 | .name = "12-bit signed", |
| 90 | .pixelformat = V4L2_PIX_FMT_SDR_S12, |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 91 | #endif |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 92 | }, { |
| 93 | .name = "Complex S14LE", |
| 94 | .pixelformat = V4L2_SDR_FMT_CS14LE, |
| 95 | }, { |
| 96 | .name = "Complex U8 (emulated)", |
| 97 | .pixelformat = V4L2_SDR_FMT_CU8, |
| 98 | }, { |
| 99 | .name = "Complex U16LE (emulated)", |
| 100 | .pixelformat = V4L2_SDR_FMT_CU16LE, |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 101 | }, |
| 102 | }; |
| 103 | |
| 104 | static const unsigned int NUM_FORMATS = ARRAY_SIZE(formats); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 105 | |
| 106 | /* intermediate buffers with raw data from the USB device */ |
| 107 | struct msi3101_frame_buf { |
| 108 | struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ |
| 109 | struct list_head list; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct msi3101_state { |
| 113 | struct video_device vdev; |
| 114 | struct v4l2_device v4l2_dev; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 115 | struct v4l2_subdev *v4l2_subdev; |
| 116 | struct spi_master *master; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 117 | |
| 118 | /* videobuf2 queue and queued buffers list */ |
| 119 | struct vb2_queue vb_queue; |
| 120 | struct list_head queued_bufs; |
| 121 | spinlock_t queued_bufs_lock; /* Protects queued_bufs */ |
| 122 | |
| 123 | /* Note if taking both locks v4l2_lock must always be locked first! */ |
| 124 | struct mutex v4l2_lock; /* Protects everything else */ |
| 125 | struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ |
| 126 | |
| 127 | /* Pointer to our usb_device, will be NULL after unplug */ |
| 128 | struct usb_device *udev; /* Both mutexes most be hold when setting! */ |
| 129 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 130 | unsigned int f_adc; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 131 | u32 pixelformat; |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 132 | unsigned int num_formats; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 133 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 134 | unsigned int isoc_errors; /* number of contiguous ISOC errors */ |
| 135 | unsigned int vb_full; /* vb is full and packets dropped */ |
| 136 | |
| 137 | struct urb *urbs[MAX_ISO_BUFS]; |
Linus Torvalds | 3c83e61 | 2014-04-04 09:50:07 -0700 | [diff] [blame] | 138 | int (*convert_stream)(struct msi3101_state *s, u8 *dst, u8 *src, |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 139 | unsigned int src_len); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 140 | |
| 141 | /* Controls */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 142 | struct v4l2_ctrl_handler hdl; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 143 | |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 144 | u32 next_sample; /* for track lost packets */ |
| 145 | u32 sample; /* for sample rate calc */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 146 | unsigned long jiffies_next; |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 147 | unsigned int sample_ctrl_bit[4]; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /* Private functions */ |
| 151 | static struct msi3101_frame_buf *msi3101_get_next_fill_buf( |
| 152 | struct msi3101_state *s) |
| 153 | { |
| 154 | unsigned long flags = 0; |
| 155 | struct msi3101_frame_buf *buf = NULL; |
| 156 | |
| 157 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 158 | if (list_empty(&s->queued_bufs)) |
| 159 | goto leave; |
| 160 | |
| 161 | buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list); |
| 162 | list_del(&buf->list); |
| 163 | leave: |
| 164 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 165 | return buf; |
| 166 | } |
| 167 | |
| 168 | /* |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 169 | * +=========================================================================== |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 170 | * | 00-1023 | USB packet type '504' |
| 171 | * +=========================================================================== |
| 172 | * | 00- 03 | sequence number of first sample in that USB packet |
| 173 | * +--------------------------------------------------------------------------- |
| 174 | * | 04- 15 | garbage |
| 175 | * +--------------------------------------------------------------------------- |
| 176 | * | 16-1023 | samples |
| 177 | * +--------------------------------------------------------------------------- |
| 178 | * signed 8-bit sample |
| 179 | * 504 * 2 = 1008 samples |
| 180 | */ |
| 181 | static int msi3101_convert_stream_504(struct msi3101_state *s, u8 *dst, |
| 182 | u8 *src, unsigned int src_len) |
| 183 | { |
| 184 | int i, i_max, dst_len = 0; |
| 185 | u32 sample_num[3]; |
| 186 | |
| 187 | /* There could be 1-3 1024 bytes URB frames */ |
| 188 | i_max = src_len / 1024; |
| 189 | |
| 190 | for (i = 0; i < i_max; i++) { |
| 191 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 192 | if (i == 0 && s->next_sample != sample_num[0]) { |
| 193 | dev_dbg_ratelimited(&s->udev->dev, |
| 194 | "%d samples lost, %d %08x:%08x\n", |
| 195 | sample_num[0] - s->next_sample, |
| 196 | src_len, s->next_sample, sample_num[0]); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Dump all unknown 'garbage' data - maybe we will discover |
| 201 | * someday if there is something rational... |
| 202 | */ |
| 203 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); |
| 204 | |
| 205 | /* 504 x I+Q samples */ |
| 206 | src += 16; |
| 207 | memcpy(dst, src, 1008); |
| 208 | src += 1008; |
| 209 | dst += 1008; |
| 210 | dst_len += 1008; |
| 211 | } |
| 212 | |
| 213 | /* calculate samping rate and output it in 10 seconds intervals */ |
| 214 | if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) { |
| 215 | unsigned long jiffies_now = jiffies; |
| 216 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next); |
| 217 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
| 218 | s->jiffies_next = jiffies_now; |
| 219 | s->sample = sample_num[i_max - 1]; |
| 220 | dev_dbg(&s->udev->dev, |
| 221 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 222 | src_len, samples, msecs, |
| 223 | samples * 1000UL / msecs); |
| 224 | } |
| 225 | |
| 226 | /* next sample (sample = sample + i * 504) */ |
| 227 | s->next_sample = sample_num[i_max - 1] + 504; |
| 228 | |
| 229 | return dst_len; |
| 230 | } |
| 231 | |
| 232 | static int msi3101_convert_stream_504_u8(struct msi3101_state *s, u8 *dst, |
| 233 | u8 *src, unsigned int src_len) |
| 234 | { |
| 235 | int i, j, i_max, dst_len = 0; |
| 236 | u32 sample_num[3]; |
| 237 | s8 *s8src; |
| 238 | u8 *u8dst; |
| 239 | |
| 240 | /* There could be 1-3 1024 bytes URB frames */ |
| 241 | i_max = src_len / 1024; |
| 242 | u8dst = (u8 *) dst; |
| 243 | |
| 244 | for (i = 0; i < i_max; i++) { |
| 245 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 246 | if (i == 0 && s->next_sample != sample_num[0]) { |
| 247 | dev_dbg_ratelimited(&s->udev->dev, |
| 248 | "%d samples lost, %d %08x:%08x\n", |
| 249 | sample_num[0] - s->next_sample, |
| 250 | src_len, s->next_sample, sample_num[0]); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Dump all unknown 'garbage' data - maybe we will discover |
| 255 | * someday if there is something rational... |
| 256 | */ |
| 257 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); |
| 258 | |
| 259 | /* 504 x I+Q samples */ |
| 260 | src += 16; |
| 261 | |
| 262 | s8src = (s8 *) src; |
| 263 | for (j = 0; j < 1008; j++) |
| 264 | *u8dst++ = *s8src++ + 128; |
| 265 | |
| 266 | src += 1008; |
| 267 | dst += 1008; |
| 268 | dst_len += 1008; |
| 269 | } |
| 270 | |
| 271 | /* calculate samping rate and output it in 10 seconds intervals */ |
| 272 | if (unlikely(time_is_before_jiffies(s->jiffies_next))) { |
| 273 | #define MSECS 10000UL |
| 274 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
| 275 | s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); |
| 276 | s->sample = sample_num[i_max - 1]; |
| 277 | dev_dbg(&s->udev->dev, |
| 278 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 279 | src_len, samples, MSECS, |
| 280 | samples * 1000UL / MSECS); |
| 281 | } |
| 282 | |
| 283 | /* next sample (sample = sample + i * 504) */ |
| 284 | s->next_sample = sample_num[i_max - 1] + 504; |
| 285 | |
| 286 | return dst_len; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * +=========================================================================== |
Antti Palosaari | 61198df | 2013-08-08 15:39:33 -0300 | [diff] [blame] | 291 | * | 00-1023 | USB packet type '384' |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 292 | * +=========================================================================== |
| 293 | * | 00- 03 | sequence number of first sample in that USB packet |
| 294 | * +--------------------------------------------------------------------------- |
| 295 | * | 04- 15 | garbage |
| 296 | * +--------------------------------------------------------------------------- |
| 297 | * | 16- 175 | samples |
| 298 | * +--------------------------------------------------------------------------- |
| 299 | * | 176- 179 | control bits for previous samples |
| 300 | * +--------------------------------------------------------------------------- |
| 301 | * | 180- 339 | samples |
| 302 | * +--------------------------------------------------------------------------- |
| 303 | * | 340- 343 | control bits for previous samples |
| 304 | * +--------------------------------------------------------------------------- |
| 305 | * | 344- 503 | samples |
| 306 | * +--------------------------------------------------------------------------- |
| 307 | * | 504- 507 | control bits for previous samples |
| 308 | * +--------------------------------------------------------------------------- |
| 309 | * | 508- 667 | samples |
| 310 | * +--------------------------------------------------------------------------- |
| 311 | * | 668- 671 | control bits for previous samples |
| 312 | * +--------------------------------------------------------------------------- |
| 313 | * | 672- 831 | samples |
| 314 | * +--------------------------------------------------------------------------- |
| 315 | * | 832- 835 | control bits for previous samples |
| 316 | * +--------------------------------------------------------------------------- |
| 317 | * | 836- 995 | samples |
| 318 | * +--------------------------------------------------------------------------- |
| 319 | * | 996- 999 | control bits for previous samples |
| 320 | * +--------------------------------------------------------------------------- |
Antti Palosaari | 6450e50 | 2013-07-31 19:58:12 -0300 | [diff] [blame] | 321 | * | 1000-1023 | garbage |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 322 | * +--------------------------------------------------------------------------- |
| 323 | * |
| 324 | * Bytes 4 - 7 could have some meaning? |
| 325 | * |
| 326 | * Control bits for previous samples is 32-bit field, containing 16 x 2-bit |
| 327 | * numbers. This results one 2-bit number for 8 samples. It is likely used for |
| 328 | * for bit shifting sample by given bits, increasing actual sampling resolution. |
| 329 | * Number 2 (0b10) was never seen. |
| 330 | * |
| 331 | * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes |
| 332 | */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 333 | static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst, |
Antti Palosaari | 61198df | 2013-08-08 15:39:33 -0300 | [diff] [blame] | 334 | u8 *src, unsigned int src_len) |
| 335 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 336 | int i, i_max, dst_len = 0; |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 337 | u32 sample_num[3]; |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 338 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 339 | /* There could be 1-3 1024 bytes URB frames */ |
| 340 | i_max = src_len / 1024; |
| 341 | for (i = 0; i < i_max; i++) { |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 342 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 343 | if (i == 0 && s->next_sample != sample_num[0]) { |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 344 | dev_dbg_ratelimited(&s->udev->dev, |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 345 | "%d samples lost, %d %08x:%08x\n", |
| 346 | sample_num[0] - s->next_sample, |
| 347 | src_len, s->next_sample, sample_num[0]); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 348 | } |
Antti Palosaari | 6450e50 | 2013-07-31 19:58:12 -0300 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * Dump all unknown 'garbage' data - maybe we will discover |
| 352 | * someday if there is something rational... |
| 353 | */ |
| 354 | dev_dbg_ratelimited(&s->udev->dev, |
| 355 | "%*ph %*ph\n", 12, &src[4], 24, &src[1000]); |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 356 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 357 | /* 384 x I+Q samples */ |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 358 | src += 16; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 359 | memcpy(dst, src, 984); |
| 360 | src += 984 + 24; |
| 361 | dst += 984; |
| 362 | dst_len += 984; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 363 | } |
| 364 | |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 365 | /* calculate samping rate and output it in 10 seconds intervals */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 366 | if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 367 | unsigned long jiffies_now = jiffies; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 368 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next); |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 369 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 370 | s->jiffies_next = jiffies_now; |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 371 | s->sample = sample_num[i_max - 1]; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 372 | dev_dbg(&s->udev->dev, |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 373 | "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n", |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 374 | src_len, samples, msecs, |
Antti Palosaari | 0200468 | 2013-07-10 21:59:58 -0300 | [diff] [blame] | 375 | samples * 1000UL / msecs, |
| 376 | s->sample_ctrl_bit[0], s->sample_ctrl_bit[1], |
| 377 | s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 378 | } |
| 379 | |
Antti Palosaari | 34599b9 | 2013-07-08 22:31:47 -0300 | [diff] [blame] | 380 | /* next sample (sample = sample + i * 384) */ |
| 381 | s->next_sample = sample_num[i_max - 1] + 384; |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 382 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 383 | return dst_len; |
| 384 | } |
| 385 | |
| 386 | /* |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 387 | * +=========================================================================== |
| 388 | * | 00-1023 | USB packet type '336' |
| 389 | * +=========================================================================== |
| 390 | * | 00- 03 | sequence number of first sample in that USB packet |
| 391 | * +--------------------------------------------------------------------------- |
| 392 | * | 04- 15 | garbage |
| 393 | * +--------------------------------------------------------------------------- |
| 394 | * | 16-1023 | samples |
| 395 | * +--------------------------------------------------------------------------- |
| 396 | * signed 12-bit sample |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 397 | */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 398 | static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst, |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 399 | u8 *src, unsigned int src_len) |
| 400 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 401 | int i, i_max, dst_len = 0; |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 402 | u32 sample_num[3]; |
| 403 | |
| 404 | /* There could be 1-3 1024 bytes URB frames */ |
| 405 | i_max = src_len / 1024; |
| 406 | |
| 407 | for (i = 0; i < i_max; i++) { |
| 408 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 409 | if (i == 0 && s->next_sample != sample_num[0]) { |
| 410 | dev_dbg_ratelimited(&s->udev->dev, |
| 411 | "%d samples lost, %d %08x:%08x\n", |
| 412 | sample_num[0] - s->next_sample, |
| 413 | src_len, s->next_sample, sample_num[0]); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Dump all unknown 'garbage' data - maybe we will discover |
| 418 | * someday if there is something rational... |
| 419 | */ |
| 420 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); |
| 421 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 422 | /* 336 x I+Q samples */ |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 423 | src += 16; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 424 | memcpy(dst, src, 1008); |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 425 | src += 1008; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 426 | dst += 1008; |
| 427 | dst_len += 1008; |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* calculate samping rate and output it in 10 seconds intervals */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 431 | if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) { |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 432 | unsigned long jiffies_now = jiffies; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 433 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next); |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 434 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 435 | s->jiffies_next = jiffies_now; |
Antti Palosaari | 8e2efac | 2013-08-07 11:27:21 -0300 | [diff] [blame] | 436 | s->sample = sample_num[i_max - 1]; |
| 437 | dev_dbg(&s->udev->dev, |
| 438 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 439 | src_len, samples, msecs, |
| 440 | samples * 1000UL / msecs); |
| 441 | } |
| 442 | |
| 443 | /* next sample (sample = sample + i * 336) */ |
| 444 | s->next_sample = sample_num[i_max - 1] + 336; |
| 445 | |
| 446 | return dst_len; |
| 447 | } |
| 448 | |
| 449 | /* |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 450 | * +=========================================================================== |
| 451 | * | 00-1023 | USB packet type '252' |
| 452 | * +=========================================================================== |
| 453 | * | 00- 03 | sequence number of first sample in that USB packet |
| 454 | * +--------------------------------------------------------------------------- |
| 455 | * | 04- 15 | garbage |
| 456 | * +--------------------------------------------------------------------------- |
| 457 | * | 16-1023 | samples |
| 458 | * +--------------------------------------------------------------------------- |
| 459 | * signed 14-bit sample |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 460 | */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 461 | static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst, |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 462 | u8 *src, unsigned int src_len) |
| 463 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 464 | int i, i_max, dst_len = 0; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 465 | u32 sample_num[3]; |
| 466 | |
| 467 | /* There could be 1-3 1024 bytes URB frames */ |
| 468 | i_max = src_len / 1024; |
| 469 | |
| 470 | for (i = 0; i < i_max; i++) { |
| 471 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 472 | if (i == 0 && s->next_sample != sample_num[0]) { |
| 473 | dev_dbg_ratelimited(&s->udev->dev, |
| 474 | "%d samples lost, %d %08x:%08x\n", |
| 475 | sample_num[0] - s->next_sample, |
| 476 | src_len, s->next_sample, sample_num[0]); |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Dump all unknown 'garbage' data - maybe we will discover |
| 481 | * someday if there is something rational... |
| 482 | */ |
| 483 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); |
| 484 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 485 | /* 252 x I+Q samples */ |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 486 | src += 16; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 487 | memcpy(dst, src, 1008); |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 488 | src += 1008; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 489 | dst += 1008; |
| 490 | dst_len += 1008; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /* calculate samping rate and output it in 10 seconds intervals */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 494 | if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) { |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 495 | unsigned long jiffies_now = jiffies; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 496 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next); |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 497 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 498 | s->jiffies_next = jiffies_now; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 499 | s->sample = sample_num[i_max - 1]; |
| 500 | dev_dbg(&s->udev->dev, |
| 501 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 502 | src_len, samples, msecs, |
| 503 | samples * 1000UL / msecs); |
| 504 | } |
| 505 | |
| 506 | /* next sample (sample = sample + i * 252) */ |
| 507 | s->next_sample = sample_num[i_max - 1] + 252; |
| 508 | |
| 509 | return dst_len; |
| 510 | } |
| 511 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 512 | static int msi3101_convert_stream_252_u16(struct msi3101_state *s, u8 *dst, |
| 513 | u8 *src, unsigned int src_len) |
| 514 | { |
| 515 | int i, j, i_max, dst_len = 0; |
| 516 | u32 sample_num[3]; |
| 517 | u16 *u16dst = (u16 *) dst; |
| 518 | struct {signed int x:14;} se; |
| 519 | |
| 520 | /* There could be 1-3 1024 bytes URB frames */ |
| 521 | i_max = src_len / 1024; |
| 522 | |
| 523 | for (i = 0; i < i_max; i++) { |
| 524 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
| 525 | if (i == 0 && s->next_sample != sample_num[0]) { |
| 526 | dev_dbg_ratelimited(&s->udev->dev, |
| 527 | "%d samples lost, %d %08x:%08x\n", |
| 528 | sample_num[0] - s->next_sample, |
| 529 | src_len, s->next_sample, sample_num[0]); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Dump all unknown 'garbage' data - maybe we will discover |
| 534 | * someday if there is something rational... |
| 535 | */ |
| 536 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); |
| 537 | |
| 538 | /* 252 x I+Q samples */ |
| 539 | src += 16; |
| 540 | |
| 541 | for (j = 0; j < 1008; j += 4) { |
| 542 | unsigned int usample[2]; |
| 543 | int ssample[2]; |
| 544 | |
| 545 | usample[0] = src[j + 0] >> 0 | src[j + 1] << 8; |
| 546 | usample[1] = src[j + 2] >> 0 | src[j + 3] << 8; |
| 547 | |
| 548 | /* sign extension from 14-bit to signed int */ |
| 549 | ssample[0] = se.x = usample[0]; |
| 550 | ssample[1] = se.x = usample[1]; |
| 551 | |
| 552 | /* from signed to unsigned */ |
| 553 | usample[0] = ssample[0] + 8192; |
| 554 | usample[1] = ssample[1] + 8192; |
| 555 | |
| 556 | /* from 14-bit to 16-bit */ |
| 557 | *u16dst++ = (usample[0] << 2) | (usample[0] >> 12); |
| 558 | *u16dst++ = (usample[1] << 2) | (usample[1] >> 12); |
| 559 | } |
| 560 | |
| 561 | src += 1008; |
| 562 | dst += 1008; |
| 563 | dst_len += 1008; |
| 564 | } |
| 565 | |
| 566 | /* calculate samping rate and output it in 10 seconds intervals */ |
| 567 | if (unlikely(time_is_before_jiffies(s->jiffies_next))) { |
| 568 | #define MSECS 10000UL |
| 569 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
| 570 | s->jiffies_next = jiffies + msecs_to_jiffies(MSECS); |
| 571 | s->sample = sample_num[i_max - 1]; |
| 572 | dev_dbg(&s->udev->dev, |
| 573 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", |
| 574 | src_len, samples, MSECS, |
| 575 | samples * 1000UL / MSECS); |
| 576 | } |
| 577 | |
| 578 | /* next sample (sample = sample + i * 252) */ |
| 579 | s->next_sample = sample_num[i_max - 1] + 252; |
| 580 | |
| 581 | return dst_len; |
| 582 | } |
| 583 | |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 584 | /* |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 585 | * This gets called for the Isochronous pipe (stream). This is done in interrupt |
| 586 | * time, so it has to be fast, not crash, and not stall. Neat. |
| 587 | */ |
| 588 | static void msi3101_isoc_handler(struct urb *urb) |
| 589 | { |
| 590 | struct msi3101_state *s = (struct msi3101_state *)urb->context; |
| 591 | int i, flen, fstatus; |
| 592 | unsigned char *iso_buf = NULL; |
| 593 | struct msi3101_frame_buf *fbuf; |
| 594 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 595 | if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET || |
| 596 | urb->status == -ESHUTDOWN)) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 597 | dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n", |
| 598 | urb, urb->status == -ENOENT ? "" : "a"); |
| 599 | return; |
| 600 | } |
| 601 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 602 | if (unlikely(urb->status != 0)) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 603 | dev_dbg(&s->udev->dev, |
| 604 | "msi3101_isoc_handler() called with status %d\n", |
| 605 | urb->status); |
| 606 | /* Give up after a number of contiguous errors */ |
| 607 | if (++s->isoc_errors > MAX_ISOC_ERRORS) |
| 608 | dev_dbg(&s->udev->dev, |
| 609 | "Too many ISOC errors, bailing out\n"); |
| 610 | goto handler_end; |
| 611 | } else { |
| 612 | /* Reset ISOC error counter. We did get here, after all. */ |
| 613 | s->isoc_errors = 0; |
| 614 | } |
| 615 | |
| 616 | /* Compact data */ |
| 617 | for (i = 0; i < urb->number_of_packets; i++) { |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 618 | void *ptr; |
| 619 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 620 | /* Check frame error */ |
| 621 | fstatus = urb->iso_frame_desc[i].status; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 622 | if (unlikely(fstatus)) { |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 623 | dev_dbg_ratelimited(&s->udev->dev, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 624 | "frame=%d/%d has error %d skipping\n", |
| 625 | i, urb->number_of_packets, fstatus); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 626 | continue; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* Check if that frame contains data */ |
| 630 | flen = urb->iso_frame_desc[i].actual_length; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 631 | if (unlikely(flen == 0)) |
| 632 | continue; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 633 | |
| 634 | iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 635 | |
| 636 | /* Get free framebuffer */ |
| 637 | fbuf = msi3101_get_next_fill_buf(s); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 638 | if (unlikely(fbuf == NULL)) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 639 | s->vb_full++; |
| 640 | dev_dbg_ratelimited(&s->udev->dev, |
| 641 | "videobuf is full, %d packets dropped\n", |
| 642 | s->vb_full); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 643 | continue; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* fill framebuffer */ |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 647 | ptr = vb2_plane_vaddr(&fbuf->vb, 0); |
| 648 | flen = s->convert_stream(s, ptr, iso_buf, flen); |
| 649 | vb2_set_plane_payload(&fbuf->vb, 0, flen); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 650 | vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | handler_end: |
| 654 | i = usb_submit_urb(urb, GFP_ATOMIC); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 655 | if (unlikely(i != 0)) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 656 | dev_dbg(&s->udev->dev, |
| 657 | "Error (%d) re-submitting urb in msi3101_isoc_handler\n", |
| 658 | i); |
| 659 | } |
| 660 | |
| 661 | static void msi3101_iso_stop(struct msi3101_state *s) |
| 662 | { |
| 663 | int i; |
| 664 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 665 | |
| 666 | /* Unlinking ISOC buffers one by one */ |
| 667 | for (i = 0; i < MAX_ISO_BUFS; i++) { |
| 668 | if (s->urbs[i]) { |
| 669 | dev_dbg(&s->udev->dev, "Unlinking URB %p\n", |
| 670 | s->urbs[i]); |
| 671 | usb_kill_urb(s->urbs[i]); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static void msi3101_iso_free(struct msi3101_state *s) |
| 677 | { |
| 678 | int i; |
| 679 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 680 | |
| 681 | /* Freeing ISOC buffers one by one */ |
| 682 | for (i = 0; i < MAX_ISO_BUFS; i++) { |
| 683 | if (s->urbs[i]) { |
| 684 | dev_dbg(&s->udev->dev, "Freeing URB\n"); |
| 685 | if (s->urbs[i]->transfer_buffer) { |
| 686 | usb_free_coherent(s->udev, |
| 687 | s->urbs[i]->transfer_buffer_length, |
| 688 | s->urbs[i]->transfer_buffer, |
| 689 | s->urbs[i]->transfer_dma); |
| 690 | } |
| 691 | usb_free_urb(s->urbs[i]); |
| 692 | s->urbs[i] = NULL; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ |
| 698 | static void msi3101_isoc_cleanup(struct msi3101_state *s) |
| 699 | { |
| 700 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 701 | |
| 702 | msi3101_iso_stop(s); |
| 703 | msi3101_iso_free(s); |
| 704 | } |
| 705 | |
| 706 | /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ |
| 707 | static int msi3101_isoc_init(struct msi3101_state *s) |
| 708 | { |
| 709 | struct usb_device *udev; |
| 710 | struct urb *urb; |
| 711 | int i, j, ret; |
| 712 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 713 | |
| 714 | s->isoc_errors = 0; |
| 715 | udev = s->udev; |
| 716 | |
| 717 | ret = usb_set_interface(s->udev, 0, 1); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 718 | if (ret) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 719 | return ret; |
| 720 | |
| 721 | /* Allocate and init Isochronuous urbs */ |
| 722 | for (i = 0; i < MAX_ISO_BUFS; i++) { |
| 723 | urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL); |
| 724 | if (urb == NULL) { |
| 725 | dev_err(&s->udev->dev, |
| 726 | "Failed to allocate urb %d\n", i); |
| 727 | msi3101_isoc_cleanup(s); |
| 728 | return -ENOMEM; |
| 729 | } |
| 730 | s->urbs[i] = urb; |
| 731 | dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb); |
| 732 | |
| 733 | urb->interval = 1; |
| 734 | urb->dev = udev; |
| 735 | urb->pipe = usb_rcvisocpipe(udev, 0x81); |
| 736 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 737 | urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE, |
| 738 | GFP_KERNEL, &urb->transfer_dma); |
| 739 | if (urb->transfer_buffer == NULL) { |
| 740 | dev_err(&s->udev->dev, |
| 741 | "Failed to allocate urb buffer %d\n", |
| 742 | i); |
| 743 | msi3101_isoc_cleanup(s); |
| 744 | return -ENOMEM; |
| 745 | } |
| 746 | urb->transfer_buffer_length = ISO_BUFFER_SIZE; |
| 747 | urb->complete = msi3101_isoc_handler; |
| 748 | urb->context = s; |
| 749 | urb->start_frame = 0; |
| 750 | urb->number_of_packets = ISO_FRAMES_PER_DESC; |
| 751 | for (j = 0; j < ISO_FRAMES_PER_DESC; j++) { |
| 752 | urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE; |
| 753 | urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* link */ |
| 758 | for (i = 0; i < MAX_ISO_BUFS; i++) { |
| 759 | ret = usb_submit_urb(s->urbs[i], GFP_KERNEL); |
| 760 | if (ret) { |
| 761 | dev_err(&s->udev->dev, |
| 762 | "isoc_init() submit_urb %d failed with error %d\n", |
| 763 | i, ret); |
| 764 | msi3101_isoc_cleanup(s); |
| 765 | return ret; |
| 766 | } |
| 767 | dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]); |
| 768 | } |
| 769 | |
| 770 | /* All is done... */ |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* Must be called with vb_queue_lock hold */ |
| 775 | static void msi3101_cleanup_queued_bufs(struct msi3101_state *s) |
| 776 | { |
| 777 | unsigned long flags = 0; |
| 778 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 779 | |
| 780 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 781 | while (!list_empty(&s->queued_bufs)) { |
| 782 | struct msi3101_frame_buf *buf; |
| 783 | |
| 784 | buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, |
| 785 | list); |
| 786 | list_del(&buf->list); |
| 787 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 788 | } |
| 789 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 790 | } |
| 791 | |
| 792 | /* The user yanked out the cable... */ |
| 793 | static void msi3101_disconnect(struct usb_interface *intf) |
| 794 | { |
| 795 | struct v4l2_device *v = usb_get_intfdata(intf); |
| 796 | struct msi3101_state *s = |
| 797 | container_of(v, struct msi3101_state, v4l2_dev); |
| 798 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 799 | |
| 800 | mutex_lock(&s->vb_queue_lock); |
| 801 | mutex_lock(&s->v4l2_lock); |
| 802 | /* No need to keep the urbs around after disconnection */ |
| 803 | s->udev = NULL; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 804 | v4l2_device_disconnect(&s->v4l2_dev); |
| 805 | video_unregister_device(&s->vdev); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 806 | spi_unregister_master(s->master); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 807 | mutex_unlock(&s->v4l2_lock); |
| 808 | mutex_unlock(&s->vb_queue_lock); |
| 809 | |
| 810 | v4l2_device_put(&s->v4l2_dev); |
| 811 | } |
| 812 | |
| 813 | static int msi3101_querycap(struct file *file, void *fh, |
| 814 | struct v4l2_capability *cap) |
| 815 | { |
| 816 | struct msi3101_state *s = video_drvdata(file); |
| 817 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 818 | |
| 819 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); |
| 820 | strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); |
| 821 | usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 822 | cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING | |
| 823 | V4L2_CAP_READWRITE | V4L2_CAP_TUNER; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 824 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
| 825 | return 0; |
| 826 | } |
| 827 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 828 | /* Videobuf2 operations */ |
| 829 | static int msi3101_queue_setup(struct vb2_queue *vq, |
| 830 | const struct v4l2_format *fmt, unsigned int *nbuffers, |
| 831 | unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) |
| 832 | { |
| 833 | struct msi3101_state *s = vb2_get_drv_priv(vq); |
| 834 | dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers); |
| 835 | |
| 836 | /* Absolute min and max number of buffers available for mmap() */ |
Antti Palosaari | d287e4e | 2014-02-26 20:48:23 -0300 | [diff] [blame] | 837 | *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 838 | *nplanes = 1; |
Antti Palosaari | c0c7203 | 2013-09-15 13:21:20 -0300 | [diff] [blame] | 839 | /* |
| 840 | * 3, wMaxPacketSize 3x 1024 bytes |
| 841 | * 504, max IQ sample pairs per 1024 frame |
| 842 | * 2, two samples, I and Q |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 843 | * 2, 16-bit is enough for single sample |
Antti Palosaari | c0c7203 | 2013-09-15 13:21:20 -0300 | [diff] [blame] | 844 | */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 845 | sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 846 | dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", |
| 847 | __func__, *nbuffers, sizes[0]); |
| 848 | return 0; |
| 849 | } |
| 850 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 851 | static void msi3101_buf_queue(struct vb2_buffer *vb) |
| 852 | { |
| 853 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); |
| 854 | struct msi3101_frame_buf *buf = |
| 855 | container_of(vb, struct msi3101_frame_buf, vb); |
| 856 | unsigned long flags = 0; |
| 857 | |
| 858 | /* Check the device has not disconnected between prep and queuing */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 859 | if (unlikely(!s->udev)) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 860 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | spin_lock_irqsave(&s->queued_bufs_lock, flags); |
| 865 | list_add_tail(&buf->list, &s->queued_bufs); |
| 866 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); |
| 867 | } |
| 868 | |
| 869 | #define CMD_WREG 0x41 |
| 870 | #define CMD_START_STREAMING 0x43 |
| 871 | #define CMD_STOP_STREAMING 0x45 |
| 872 | #define CMD_READ_UNKNOW 0x48 |
| 873 | |
| 874 | #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \ |
| 875 | char *direction; \ |
| 876 | if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \ |
| 877 | direction = ">>>"; \ |
| 878 | else \ |
| 879 | direction = "<<<"; \ |
| 880 | dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \ |
| 881 | "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \ |
| 882 | _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \ |
| 883 | } |
| 884 | |
| 885 | static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data) |
| 886 | { |
| 887 | int ret; |
| 888 | u8 request = cmd; |
| 889 | u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR; |
| 890 | u16 value = (data >> 0) & 0xffff; |
| 891 | u16 index = (data >> 16) & 0xffff; |
| 892 | |
| 893 | msi3101_dbg_usb_control_msg(s->udev, |
| 894 | request, requesttype, value, index, NULL, 0); |
| 895 | |
Antti Palosaari | d0fadf4 | 2013-07-11 12:19:32 -0300 | [diff] [blame] | 896 | ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0), |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 897 | request, requesttype, value, index, NULL, 0, 2000); |
| 898 | |
| 899 | if (ret) |
| 900 | dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n", |
| 901 | __func__, ret, cmd, data); |
| 902 | |
| 903 | return ret; |
| 904 | }; |
| 905 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 906 | #define F_REF 24000000 |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 907 | #define DIV_R_IN 2 |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 908 | static int msi3101_set_usb_adc(struct msi3101_state *s) |
| 909 | { |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 910 | int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 911 | u32 reg3, reg4, reg7; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 912 | struct v4l2_ctrl *bandwidth_auto; |
| 913 | struct v4l2_ctrl *bandwidth; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 914 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 915 | f_sr = s->f_adc; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 916 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 917 | /* set tuner, subdev, filters according to sampling rate */ |
| 918 | bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 919 | if (v4l2_ctrl_g_ctrl(bandwidth_auto)) { |
| 920 | bandwidth = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH); |
| 921 | v4l2_ctrl_s_ctrl(bandwidth, s->f_adc); |
| 922 | } |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 923 | |
| 924 | /* select stream format */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 925 | switch (s->pixelformat) { |
| 926 | case V4L2_SDR_FMT_CU8: |
| 927 | s->convert_stream = msi3101_convert_stream_504_u8; |
| 928 | reg7 = 0x000c9407; |
| 929 | break; |
| 930 | case V4L2_SDR_FMT_CU16LE: |
| 931 | s->convert_stream = msi3101_convert_stream_252_u16; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 932 | reg7 = 0x00009407; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 933 | break; |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 934 | case V4L2_SDR_FMT_CS8: |
Antti Palosaari | 61198df | 2013-08-08 15:39:33 -0300 | [diff] [blame] | 935 | s->convert_stream = msi3101_convert_stream_504; |
| 936 | reg7 = 0x000c9407; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 937 | break; |
| 938 | case V4L2_PIX_FMT_SDR_MSI2500_384: |
| 939 | s->convert_stream = msi3101_convert_stream_384; |
| 940 | reg7 = 0x0000a507; |
| 941 | break; |
| 942 | case V4L2_PIX_FMT_SDR_S12: |
| 943 | s->convert_stream = msi3101_convert_stream_336; |
| 944 | reg7 = 0x00008507; |
| 945 | break; |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 946 | case V4L2_SDR_FMT_CS14LE: |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 947 | s->convert_stream = msi3101_convert_stream_252; |
| 948 | reg7 = 0x00009407; |
| 949 | break; |
| 950 | default: |
| 951 | s->convert_stream = msi3101_convert_stream_504_u8; |
| 952 | reg7 = 0x000c9407; |
| 953 | break; |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 954 | } |
| 955 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 956 | /* |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 957 | * Synthesizer config is just a educated guess... |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 958 | * |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 959 | * [7:0] 0x03, register address |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 960 | * [8] 1, power control |
| 961 | * [9] ?, power control |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 962 | * [12:10] output divider |
| 963 | * [13] 0 ? |
| 964 | * [14] 0 ? |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 965 | * [15] fractional MSB, bit 20 |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 966 | * [16:19] N |
| 967 | * [23:20] ? |
| 968 | * [24:31] 0x01 |
| 969 | * |
| 970 | * output divider |
| 971 | * val div |
| 972 | * 0 - (invalid) |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 973 | * 1 4 |
| 974 | * 2 6 |
| 975 | * 3 8 |
| 976 | * 4 10 |
| 977 | * 5 12 |
| 978 | * 6 14 |
| 979 | * 7 16 |
| 980 | * |
| 981 | * VCO 202000000 - 720000000++ |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 982 | */ |
Antti Palosaari | c5a431d | 2013-08-05 21:33:31 -0300 | [diff] [blame] | 983 | reg3 = 0x01000303; |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 984 | reg4 = 0x00000004; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 985 | |
Antti Palosaari | c5a431d | 2013-08-05 21:33:31 -0300 | [diff] [blame] | 986 | /* XXX: Filters? AGC? */ |
| 987 | if (f_sr < 6000000) |
| 988 | reg3 |= 0x1 << 20; |
| 989 | else if (f_sr < 7000000) |
| 990 | reg3 |= 0x5 << 20; |
| 991 | else if (f_sr < 8500000) |
| 992 | reg3 |= 0x9 << 20; |
| 993 | else |
| 994 | reg3 |= 0xd << 20; |
| 995 | |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 996 | for (div_r_out = 4; div_r_out < 16; div_r_out += 2) { |
| 997 | f_vco = f_sr * div_r_out * 12; |
| 998 | dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n", |
| 999 | __func__, div_r_out, f_vco); |
| 1000 | if (f_vco >= 202000000) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1001 | break; |
| 1002 | } |
| 1003 | |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 1004 | div_n = f_vco / (F_REF * DIV_R_IN); |
| 1005 | div_m = f_vco % (F_REF * DIV_R_IN); |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 1006 | fract = 0x200000ul * div_m / (F_REF * DIV_R_IN); |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 1007 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1008 | reg3 |= div_n << 16; |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 1009 | reg3 |= (div_r_out / 2 - 1) << 10; |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 1010 | reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */ |
| 1011 | reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */ |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1012 | |
Antti Palosaari | b152085 | 2013-07-09 02:44:56 -0300 | [diff] [blame] | 1013 | dev_dbg(&s->udev->dev, |
Antti Palosaari | 22ca680 | 2013-07-31 21:48:51 -0300 | [diff] [blame] | 1014 | "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n", |
| 1015 | __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1016 | |
| 1017 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008); |
| 1018 | if (ret) |
| 1019 | goto err; |
| 1020 | |
| 1021 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05); |
| 1022 | if (ret) |
| 1023 | goto err; |
| 1024 | |
| 1025 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000); |
| 1026 | if (ret) |
| 1027 | goto err; |
| 1028 | |
| 1029 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102); |
| 1030 | if (ret) |
| 1031 | goto err; |
| 1032 | |
| 1033 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008); |
| 1034 | if (ret) |
| 1035 | goto err; |
| 1036 | |
Antti Palosaari | 554cbfb | 2013-08-05 20:43:36 -0300 | [diff] [blame] | 1037 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg7); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1038 | if (ret) |
| 1039 | goto err; |
| 1040 | |
| 1041 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg4); |
| 1042 | if (ret) |
| 1043 | goto err; |
| 1044 | |
| 1045 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg3); |
| 1046 | if (ret) |
| 1047 | goto err; |
| 1048 | err: |
| 1049 | return ret; |
| 1050 | }; |
| 1051 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1052 | static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count) |
| 1053 | { |
| 1054 | struct msi3101_state *s = vb2_get_drv_priv(vq); |
| 1055 | int ret; |
| 1056 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1057 | |
| 1058 | if (!s->udev) |
| 1059 | return -ENODEV; |
| 1060 | |
| 1061 | if (mutex_lock_interruptible(&s->v4l2_lock)) |
| 1062 | return -ERESTARTSYS; |
| 1063 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1064 | /* wake-up tuner */ |
| 1065 | v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1); |
| 1066 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1067 | ret = msi3101_set_usb_adc(s); |
| 1068 | |
| 1069 | ret = msi3101_isoc_init(s); |
| 1070 | if (ret) |
| 1071 | msi3101_cleanup_queued_bufs(s); |
| 1072 | |
| 1073 | ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0); |
| 1074 | |
| 1075 | mutex_unlock(&s->v4l2_lock); |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1080 | static void msi3101_stop_streaming(struct vb2_queue *vq) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1081 | { |
| 1082 | struct msi3101_state *s = vb2_get_drv_priv(vq); |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1083 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1084 | dev_dbg(&s->udev->dev, "%s:\n", __func__); |
| 1085 | |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1086 | mutex_lock(&s->v4l2_lock); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1087 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1088 | if (s->udev) |
| 1089 | msi3101_isoc_cleanup(s); |
| 1090 | |
| 1091 | msi3101_cleanup_queued_bufs(s); |
Antti Palosaari | d0fadf4 | 2013-07-11 12:19:32 -0300 | [diff] [blame] | 1092 | |
| 1093 | /* according to tests, at least 700us delay is required */ |
| 1094 | msleep(20); |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1095 | if (!msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0)) { |
| 1096 | /* sleep USB IF / ADC */ |
| 1097 | msi3101_ctrl_msg(s, CMD_WREG, 0x01000003); |
| 1098 | } |
Antti Palosaari | d0fadf4 | 2013-07-11 12:19:32 -0300 | [diff] [blame] | 1099 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1100 | /* sleep tuner */ |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1101 | v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1102 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1103 | mutex_unlock(&s->v4l2_lock); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | static struct vb2_ops msi3101_vb2_ops = { |
| 1107 | .queue_setup = msi3101_queue_setup, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1108 | .buf_queue = msi3101_buf_queue, |
| 1109 | .start_streaming = msi3101_start_streaming, |
| 1110 | .stop_streaming = msi3101_stop_streaming, |
| 1111 | .wait_prepare = vb2_ops_wait_prepare, |
| 1112 | .wait_finish = vb2_ops_wait_finish, |
| 1113 | }; |
| 1114 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1115 | static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv, |
| 1116 | struct v4l2_fmtdesc *f) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1117 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1118 | struct msi3101_state *s = video_drvdata(file); |
| 1119 | dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index); |
| 1120 | |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 1121 | if (f->index >= s->num_formats) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1122 | return -EINVAL; |
| 1123 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1124 | strlcpy(f->description, formats[f->index].name, sizeof(f->description)); |
| 1125 | f->pixelformat = formats[f->index].pixelformat; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1130 | static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv, |
| 1131 | struct v4l2_format *f) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1132 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1133 | struct msi3101_state *s = video_drvdata(file); |
| 1134 | dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, |
| 1135 | (char *)&s->pixelformat); |
| 1136 | |
Antti Palosaari | c350912 | 2014-03-10 16:30:44 -0300 | [diff] [blame] | 1137 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1138 | f->fmt.sdr.pixelformat = s->pixelformat; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1139 | |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1143 | static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv, |
| 1144 | struct v4l2_format *f) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1145 | { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1146 | struct msi3101_state *s = video_drvdata(file); |
| 1147 | struct vb2_queue *q = &s->vb_queue; |
| 1148 | int i; |
| 1149 | dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, |
| 1150 | (char *)&f->fmt.sdr.pixelformat); |
| 1151 | |
| 1152 | if (vb2_is_busy(q)) |
| 1153 | return -EBUSY; |
| 1154 | |
Antti Palosaari | c350912 | 2014-03-10 16:30:44 -0300 | [diff] [blame] | 1155 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 1156 | for (i = 0; i < s->num_formats; i++) { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1157 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { |
| 1158 | s->pixelformat = f->fmt.sdr.pixelformat; |
| 1159 | return 0; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 1164 | s->pixelformat = formats[0].pixelformat; |
| 1165 | |
| 1166 | return 0; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1167 | } |
| 1168 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1169 | static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv, |
| 1170 | struct v4l2_format *f) |
| 1171 | { |
| 1172 | struct msi3101_state *s = video_drvdata(file); |
| 1173 | int i; |
| 1174 | dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__, |
| 1175 | (char *)&f->fmt.sdr.pixelformat); |
| 1176 | |
Antti Palosaari | c350912 | 2014-03-10 16:30:44 -0300 | [diff] [blame] | 1177 | memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved)); |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 1178 | for (i = 0; i < s->num_formats; i++) { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1179 | if (formats[i].pixelformat == f->fmt.sdr.pixelformat) |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | f->fmt.sdr.pixelformat = formats[0].pixelformat; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int msi3101_s_tuner(struct file *file, void *priv, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1189 | const struct v4l2_tuner *v) |
| 1190 | { |
| 1191 | struct msi3101_state *s = video_drvdata(file); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1192 | int ret; |
| 1193 | dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1194 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1195 | if (v->index == 0) |
| 1196 | ret = 0; |
| 1197 | else if (v->index == 1) |
| 1198 | ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v); |
| 1199 | else |
| 1200 | ret = -EINVAL; |
| 1201 | |
| 1202 | return ret; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1203 | } |
| 1204 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1205 | static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1206 | { |
| 1207 | struct msi3101_state *s = video_drvdata(file); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1208 | int ret; |
| 1209 | dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1210 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1211 | if (v->index == 0) { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1212 | strlcpy(v->name, "Mirics MSi2500", sizeof(v->name)); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1213 | v->type = V4L2_TUNER_ADC; |
| 1214 | v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; |
| 1215 | v->rangelow = 1200000; |
| 1216 | v->rangehigh = 15000000; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1217 | ret = 0; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1218 | } else if (v->index == 1) { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1219 | ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1220 | } else { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1221 | ret = -EINVAL; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1222 | } |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1223 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1224 | return ret; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1225 | } |
| 1226 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1227 | static int msi3101_g_frequency(struct file *file, void *priv, |
| 1228 | struct v4l2_frequency *f) |
| 1229 | { |
| 1230 | struct msi3101_state *s = video_drvdata(file); |
| 1231 | int ret = 0; |
| 1232 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n", |
| 1233 | __func__, f->tuner, f->type); |
| 1234 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1235 | if (f->tuner == 0) { |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1236 | f->frequency = s->f_adc; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1237 | ret = 0; |
| 1238 | } else if (f->tuner == 1) { |
Antti Palosaari | c350912 | 2014-03-10 16:30:44 -0300 | [diff] [blame] | 1239 | f->type = V4L2_TUNER_RF; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1240 | ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f); |
| 1241 | } else { |
| 1242 | ret = -EINVAL; |
| 1243 | } |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | static int msi3101_s_frequency(struct file *file, void *priv, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1249 | const struct v4l2_frequency *f) |
| 1250 | { |
| 1251 | struct msi3101_state *s = video_drvdata(file); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1252 | int ret; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1253 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n", |
| 1254 | __func__, f->tuner, f->type, f->frequency); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1255 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1256 | if (f->tuner == 0) { |
| 1257 | s->f_adc = clamp_t(unsigned int, f->frequency, |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1258 | bands[0].rangelow, |
| 1259 | bands[0].rangehigh); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1260 | dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n", |
| 1261 | __func__, s->f_adc); |
| 1262 | ret = msi3101_set_usb_adc(s); |
| 1263 | } else if (f->tuner == 1) { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1264 | ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1265 | } else { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1266 | ret = -EINVAL; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
| 1272 | static int msi3101_enum_freq_bands(struct file *file, void *priv, |
| 1273 | struct v4l2_frequency_band *band) |
| 1274 | { |
| 1275 | struct msi3101_state *s = video_drvdata(file); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1276 | int ret; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1277 | dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n", |
| 1278 | __func__, band->tuner, band->type, band->index); |
| 1279 | |
| 1280 | if (band->tuner == 0) { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1281 | if (band->index >= ARRAY_SIZE(bands)) { |
| 1282 | ret = -EINVAL; |
| 1283 | } else { |
| 1284 | *band = bands[band->index]; |
| 1285 | ret = 0; |
| 1286 | } |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1287 | } else if (band->tuner == 1) { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1288 | ret = v4l2_subdev_call(s->v4l2_subdev, tuner, |
| 1289 | enum_freq_bands, band); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1290 | } else { |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1291 | ret = -EINVAL; |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1292 | } |
| 1293 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1294 | return ret; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1295 | } |
| 1296 | |
Fengguang Wu | 6648ef1 | 2013-09-02 19:09:53 -0300 | [diff] [blame] | 1297 | static const struct v4l2_ioctl_ops msi3101_ioctl_ops = { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1298 | .vidioc_querycap = msi3101_querycap, |
| 1299 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1300 | .vidioc_enum_fmt_sdr_cap = msi3101_enum_fmt_sdr_cap, |
| 1301 | .vidioc_g_fmt_sdr_cap = msi3101_g_fmt_sdr_cap, |
| 1302 | .vidioc_s_fmt_sdr_cap = msi3101_s_fmt_sdr_cap, |
| 1303 | .vidioc_try_fmt_sdr_cap = msi3101_try_fmt_sdr_cap, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1304 | |
| 1305 | .vidioc_reqbufs = vb2_ioctl_reqbufs, |
| 1306 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
| 1307 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, |
| 1308 | .vidioc_querybuf = vb2_ioctl_querybuf, |
| 1309 | .vidioc_qbuf = vb2_ioctl_qbuf, |
| 1310 | .vidioc_dqbuf = vb2_ioctl_dqbuf, |
| 1311 | |
| 1312 | .vidioc_streamon = vb2_ioctl_streamon, |
| 1313 | .vidioc_streamoff = vb2_ioctl_streamoff, |
| 1314 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1315 | .vidioc_g_tuner = msi3101_g_tuner, |
| 1316 | .vidioc_s_tuner = msi3101_s_tuner, |
| 1317 | |
| 1318 | .vidioc_g_frequency = msi3101_g_frequency, |
| 1319 | .vidioc_s_frequency = msi3101_s_frequency, |
| 1320 | .vidioc_enum_freq_bands = msi3101_enum_freq_bands, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1321 | |
| 1322 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1323 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
| 1324 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 1325 | }; |
| 1326 | |
| 1327 | static const struct v4l2_file_operations msi3101_fops = { |
| 1328 | .owner = THIS_MODULE, |
| 1329 | .open = v4l2_fh_open, |
| 1330 | .release = vb2_fop_release, |
| 1331 | .read = vb2_fop_read, |
| 1332 | .poll = vb2_fop_poll, |
| 1333 | .mmap = vb2_fop_mmap, |
| 1334 | .unlocked_ioctl = video_ioctl2, |
| 1335 | }; |
| 1336 | |
| 1337 | static struct video_device msi3101_template = { |
| 1338 | .name = "Mirics MSi3101 SDR Dongle", |
| 1339 | .release = video_device_release_empty, |
| 1340 | .fops = &msi3101_fops, |
| 1341 | .ioctl_ops = &msi3101_ioctl_ops, |
| 1342 | }; |
| 1343 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1344 | static void msi3101_video_release(struct v4l2_device *v) |
| 1345 | { |
| 1346 | struct msi3101_state *s = |
| 1347 | container_of(v, struct msi3101_state, v4l2_dev); |
| 1348 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1349 | v4l2_ctrl_handler_free(&s->hdl); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1350 | v4l2_device_unregister(&s->v4l2_dev); |
| 1351 | kfree(s); |
| 1352 | } |
| 1353 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1354 | static int msi3101_transfer_one_message(struct spi_master *master, |
| 1355 | struct spi_message *m) |
| 1356 | { |
| 1357 | struct msi3101_state *s = spi_master_get_devdata(master); |
| 1358 | struct spi_transfer *t; |
| 1359 | int ret = 0; |
| 1360 | u32 data; |
| 1361 | |
| 1362 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 1363 | dev_dbg(&s->udev->dev, "%s: msg=%*ph\n", |
| 1364 | __func__, t->len, t->tx_buf); |
| 1365 | data = 0x09; /* reg 9 is SPI adapter */ |
| 1366 | data |= ((u8 *)t->tx_buf)[0] << 8; |
| 1367 | data |= ((u8 *)t->tx_buf)[1] << 16; |
| 1368 | data |= ((u8 *)t->tx_buf)[2] << 24; |
| 1369 | ret = msi3101_ctrl_msg(s, CMD_WREG, data); |
| 1370 | } |
| 1371 | |
| 1372 | m->status = ret; |
| 1373 | spi_finalize_current_message(master); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1377 | static int msi3101_probe(struct usb_interface *intf, |
| 1378 | const struct usb_device_id *id) |
| 1379 | { |
| 1380 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1381 | struct msi3101_state *s = NULL; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1382 | struct v4l2_subdev *sd; |
| 1383 | struct spi_master *master; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1384 | int ret; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1385 | static struct spi_board_info board_info = { |
| 1386 | .modalias = "msi001", |
| 1387 | .bus_num = 0, |
| 1388 | .chip_select = 0, |
| 1389 | .max_speed_hz = 12000000, |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1390 | }; |
| 1391 | |
| 1392 | s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL); |
| 1393 | if (s == NULL) { |
| 1394 | pr_err("Could not allocate memory for msi3101_state\n"); |
| 1395 | return -ENOMEM; |
| 1396 | } |
| 1397 | |
| 1398 | mutex_init(&s->v4l2_lock); |
| 1399 | mutex_init(&s->vb_queue_lock); |
| 1400 | spin_lock_init(&s->queued_bufs_lock); |
| 1401 | INIT_LIST_HEAD(&s->queued_bufs); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1402 | s->udev = udev; |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1403 | s->f_adc = bands[0].rangelow; |
Antti Palosaari | 3912eb6 | 2014-07-13 22:27:21 -0300 | [diff] [blame^] | 1404 | s->pixelformat = formats[0].pixelformat; |
| 1405 | s->num_formats = NUM_FORMATS; |
| 1406 | if (msi3101_emulated_fmt == false) |
| 1407 | s->num_formats -= 2; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1408 | |
| 1409 | /* Init videobuf2 queue structure */ |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1410 | s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1411 | s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; |
| 1412 | s->vb_queue.drv_priv = s; |
| 1413 | s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf); |
| 1414 | s->vb_queue.ops = &msi3101_vb2_ops; |
| 1415 | s->vb_queue.mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 1416 | s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1417 | ret = vb2_queue_init(&s->vb_queue); |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1418 | if (ret) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1419 | dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); |
| 1420 | goto err_free_mem; |
| 1421 | } |
| 1422 | |
| 1423 | /* Init video_device structure */ |
| 1424 | s->vdev = msi3101_template; |
| 1425 | s->vdev.queue = &s->vb_queue; |
| 1426 | s->vdev.queue->lock = &s->vb_queue_lock; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1427 | video_set_drvdata(&s->vdev, s); |
| 1428 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1429 | /* Register the v4l2_device structure */ |
| 1430 | s->v4l2_dev.release = msi3101_video_release; |
| 1431 | ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); |
| 1432 | if (ret) { |
| 1433 | dev_err(&s->udev->dev, |
| 1434 | "Failed to register v4l2-device (%d)\n", ret); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1435 | goto err_free_mem; |
| 1436 | } |
| 1437 | |
| 1438 | /* SPI master adapter */ |
| 1439 | master = spi_alloc_master(&s->udev->dev, 0); |
| 1440 | if (master == NULL) { |
| 1441 | ret = -ENOMEM; |
| 1442 | goto err_unregister_v4l2_dev; |
| 1443 | } |
| 1444 | |
| 1445 | s->master = master; |
| 1446 | master->bus_num = 0; |
| 1447 | master->num_chipselect = 1; |
| 1448 | master->transfer_one_message = msi3101_transfer_one_message; |
| 1449 | spi_master_set_devdata(master, s); |
| 1450 | ret = spi_register_master(master); |
| 1451 | if (ret) { |
| 1452 | spi_master_put(master); |
| 1453 | goto err_unregister_v4l2_dev; |
| 1454 | } |
| 1455 | |
| 1456 | /* load v4l2 subdevice */ |
| 1457 | sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info); |
| 1458 | s->v4l2_subdev = sd; |
| 1459 | if (sd == NULL) { |
| 1460 | dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n"); |
| 1461 | ret = -ENODEV; |
| 1462 | goto err_unregister_master; |
| 1463 | } |
| 1464 | |
| 1465 | /* Register controls */ |
| 1466 | v4l2_ctrl_handler_init(&s->hdl, 0); |
| 1467 | if (s->hdl.error) { |
| 1468 | ret = s->hdl.error; |
| 1469 | dev_err(&s->udev->dev, "Could not initialize controls\n"); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1470 | goto err_free_controls; |
| 1471 | } |
| 1472 | |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1473 | /* currently all controls are from subdev */ |
| 1474 | v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL); |
| 1475 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1476 | s->v4l2_dev.ctrl_handler = &s->hdl; |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1477 | s->vdev.v4l2_dev = &s->v4l2_dev; |
| 1478 | s->vdev.lock = &s->v4l2_lock; |
| 1479 | |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1480 | ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1); |
| 1481 | if (ret) { |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1482 | dev_err(&s->udev->dev, |
| 1483 | "Failed to register as video device (%d)\n", |
| 1484 | ret); |
| 1485 | goto err_unregister_v4l2_dev; |
| 1486 | } |
| 1487 | dev_info(&s->udev->dev, "Registered as %s\n", |
| 1488 | video_device_node_name(&s->vdev)); |
| 1489 | |
| 1490 | return 0; |
| 1491 | |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1492 | err_free_controls: |
Antti Palosaari | 3d0c8fa | 2013-11-17 03:04:51 -0300 | [diff] [blame] | 1493 | v4l2_ctrl_handler_free(&s->hdl); |
Antti Palosaari | 2e68f84 | 2014-02-02 23:42:00 -0300 | [diff] [blame] | 1494 | err_unregister_master: |
| 1495 | spi_unregister_master(s->master); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1496 | err_unregister_v4l2_dev: |
| 1497 | v4l2_device_unregister(&s->v4l2_dev); |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1498 | err_free_mem: |
| 1499 | kfree(s); |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | /* USB device ID list */ |
| 1504 | static struct usb_device_id msi3101_id_table[] = { |
Antti Palosaari | 00e049b | 2013-08-07 15:06:29 -0300 | [diff] [blame] | 1505 | { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */ |
Antti Palosaari | 42fc5b4 | 2013-07-17 15:05:27 -0300 | [diff] [blame] | 1506 | { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */ |
Antti Palosaari | 977e444 | 2013-07-05 00:24:08 -0300 | [diff] [blame] | 1507 | { } |
| 1508 | }; |
| 1509 | MODULE_DEVICE_TABLE(usb, msi3101_id_table); |
| 1510 | |
| 1511 | /* USB subsystem interface */ |
| 1512 | static struct usb_driver msi3101_driver = { |
| 1513 | .name = KBUILD_MODNAME, |
| 1514 | .probe = msi3101_probe, |
| 1515 | .disconnect = msi3101_disconnect, |
| 1516 | .id_table = msi3101_id_table, |
| 1517 | }; |
| 1518 | |
| 1519 | module_usb_driver(msi3101_driver); |
| 1520 | |
| 1521 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); |
| 1522 | MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle"); |
| 1523 | MODULE_LICENSE("GPL"); |