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