Version:  2.0.40 2.2.26 2.4.37 2.6.39 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15

Linux/drivers/staging/media/msi3101/sdr-msi3101.c

  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.
 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>
 24  */
 25 
 26 #include <linux/module.h>
 27 #include <linux/slab.h>
 28 #include <asm/div64.h>
 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>
 34 #include <media/videobuf2-vmalloc.h>
 35 #include <linux/spi/spi.h>
 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)
 52 #define MAX_ISOC_ERRORS         20
 53 
 54 /* TODO: These should be moved to V4L2 API */
 55 #define V4L2_PIX_FMT_SDR_S8     v4l2_fourcc('D', 'S', '', '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 
 60 static const struct v4l2_frequency_band bands[] = {
 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 
 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);
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;
108 };
109 
110 struct msi3101_state {
111         struct video_device vdev;
112         struct v4l2_device v4l2_dev;
113         struct v4l2_subdev *v4l2_subdev;
114         struct spi_master *master;
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 
128         unsigned int f_adc;
129         u32 pixelformat;
130 
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];
135         int (*convert_stream)(struct msi3101_state *s, u8 *dst, u8 *src,
136                         unsigned int src_len);
137 
138         /* Controls */
139         struct v4l2_ctrl_handler hdl;
140 
141         u32 next_sample; /* for track lost packets */
142         u32 sample; /* for sample rate calc */
143         unsigned long jiffies_next;
144         unsigned int sample_ctrl_bit[4];
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 /*
166  * +===========================================================================
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  * +===========================================================================
288  * |   00-1023 | USB packet type '384'
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  * +---------------------------------------------------------------------------
318  * | 1000-1023 | garbage
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  */
330 static int msi3101_convert_stream_384(struct msi3101_state *s, u8 *dst,
331                 u8 *src, unsigned int src_len)
332 {
333         int i, i_max, dst_len = 0;
334         u32 sample_num[3];
335 
336         /* There could be 1-3 1024 bytes URB frames */
337         i_max = src_len / 1024;
338         for (i = 0; i < i_max; i++) {
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]) {
341                         dev_dbg_ratelimited(&s->udev->dev,
342                                         "%d samples lost, %d %08x:%08x\n",
343                                         sample_num[0] - s->next_sample,
344                                         src_len, s->next_sample, sample_num[0]);
345                 }
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]);
353 
354                 /* 384 x I+Q samples */
355                 src += 16;
356                 memcpy(dst, src, 984);
357                 src += 984 + 24;
358                 dst += 984;
359                 dst_len += 984;
360         }
361 
362         /* calculate samping rate and output it in 10 seconds intervals */
363         if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
364                 unsigned long jiffies_now = jiffies;
365                 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
366                 unsigned int samples = sample_num[i_max - 1] - s->sample;
367                 s->jiffies_next = jiffies_now;
368                 s->sample = sample_num[i_max - 1];
369                 dev_dbg(&s->udev->dev,
370                                 "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n",
371                                 src_len, samples, msecs,
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]);
375         }
376 
377         /* next sample (sample = sample + i * 384) */
378         s->next_sample = sample_num[i_max - 1] + 384;
379 
380         return dst_len;
381 }
382 
383 /*
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
394  */
395 static int msi3101_convert_stream_336(struct msi3101_state *s, u8 *dst,
396                 u8 *src, unsigned int src_len)
397 {
398         int i, i_max, dst_len = 0;
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 
419                 /* 336 x I+Q samples */
420                 src += 16;
421                 memcpy(dst, src, 1008);
422                 src += 1008;
423                 dst += 1008;
424                 dst_len += 1008;
425         }
426 
427         /* calculate samping rate and output it in 10 seconds intervals */
428         if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
429                 unsigned long jiffies_now = jiffies;
430                 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
431                 unsigned int samples = sample_num[i_max - 1] - s->sample;
432                 s->jiffies_next = jiffies_now;
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 /*
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
457  */
458 static int msi3101_convert_stream_252(struct msi3101_state *s, u8 *dst,
459                 u8 *src, unsigned int src_len)
460 {
461         int i, i_max, dst_len = 0;
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 
482                 /* 252 x I+Q samples */
483                 src += 16;
484                 memcpy(dst, src, 1008);
485                 src += 1008;
486                 dst += 1008;
487                 dst_len += 1008;
488         }
489 
490         /* calculate samping rate and output it in 10 seconds intervals */
491         if ((s->jiffies_next + msecs_to_jiffies(10000)) <= jiffies) {
492                 unsigned long jiffies_now = jiffies;
493                 unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies_next);
494                 unsigned int samples = sample_num[i_max - 1] - s->sample;
495                 s->jiffies_next = jiffies_now;
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 
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 
581 /*
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 
592         if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
593                         urb->status == -ESHUTDOWN)) {
594                 dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n",
595                                 urb, urb->status == -ENOENT ? "" : "a");
596                 return;
597         }
598 
599         if (unlikely(urb->status != 0)) {
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++) {
615                 void *ptr;
616 
617                 /* Check frame error */
618                 fstatus = urb->iso_frame_desc[i].status;
619                 if (unlikely(fstatus)) {
620                         dev_dbg_ratelimited(&s->udev->dev,
621                                         "frame=%d/%d has error %d skipping\n",
622                                         i, urb->number_of_packets, fstatus);
623                         continue;
624                 }
625 
626                 /* Check if that frame contains data */
627                 flen = urb->iso_frame_desc[i].actual_length;
628                 if (unlikely(flen == 0))
629                         continue;
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);
635                 if (unlikely(fbuf == NULL)) {
636                         s->vb_full++;
637                         dev_dbg_ratelimited(&s->udev->dev,
638                                         "videobuf is full, %d packets dropped\n",
639                                         s->vb_full);
640                         continue;
641                 }
642 
643                 /* fill framebuffer */
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);
647                 vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE);
648         }
649 
650 handler_end:
651         i = usb_submit_urb(urb, GFP_ATOMIC);
652         if (unlikely(i != 0))
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);
715         if (ret)
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;
801         v4l2_device_disconnect(&s->v4l2_dev);
802         video_unregister_device(&s->vdev);
803         spi_unregister_master(s->master);
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));
819         cap->device_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_STREAMING |
820                         V4L2_CAP_READWRITE | V4L2_CAP_TUNER;
821         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
822         return 0;
823 }
824 
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() */
834         *nbuffers = clamp_t(unsigned int, *nbuffers, 8, 32);
835         *nplanes = 1;
836         /*
837          *   3, wMaxPacketSize 3x 1024 bytes
838          * 504, max IQ sample pairs per 1024 frame
839          *   2, two samples, I and Q
840          *   2, 16-bit is enough for single sample
841          */
842         sizes[0] = PAGE_ALIGN(3 * 504 * 2 * 2);
843         dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n",
844                         __func__, *nbuffers, sizes[0]);
845         return 0;
846 }
847 
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 */
856         if (unlikely(!s->udev)) {
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 
893         ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0),
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 
903 #define F_REF 24000000
904 #define DIV_R_IN 2
905 static int msi3101_set_usb_adc(struct msi3101_state *s)
906 {
907         int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract;
908         u32 reg3, reg4, reg7;
909         struct v4l2_ctrl *bandwidth_auto;
910         struct v4l2_ctrl *bandwidth;
911 
912         f_sr = s->f_adc;
913 
914         /* set tuner, subdev, filters according to sampling rate */
915         bandwidth_auto = v4l2_ctrl_find(&s->hdl, V4L2_CID_RF_TUNER_BANDWIDTH_AUTO);
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         }
920 
921         /* select stream format */
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;
929                 reg7 = 0x00009407;
930                 break;
931         case V4L2_PIX_FMT_SDR_S8:
932                 s->convert_stream = msi3101_convert_stream_504;
933                 reg7 = 0x000c9407;
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;
951         }
952 
953         /*
954          * Synthesizer config is just a educated guess...
955          *
956          * [7:0]   0x03, register address
957          * [8]     1, power control
958          * [9]     ?, power control
959          * [12:10] output divider
960          * [13]    0 ?
961          * [14]    0 ?
962          * [15]    fractional MSB, bit 20
963          * [16:19] N
964          * [23:20] ?
965          * [24:31] 0x01
966          *
967          * output divider
968          * val   div
969          *   0     - (invalid)
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++
979          */
980         reg3 = 0x01000303;
981         reg4 = 0x00000004;
982 
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 
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)
998                         break;
999         }
1000 
1001         div_n = f_vco / (F_REF * DIV_R_IN);
1002         div_m = f_vco % (F_REF * DIV_R_IN);
1003         fract = 0x200000ul * div_m / (F_REF * DIV_R_IN);
1004 
1005         reg3 |= div_n << 16;
1006         reg3 |= (div_r_out / 2 - 1) << 10;
1007         reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */
1008         reg4 |= ((fract >>  0) & 0x0fffff) <<  8; /* [19:0] */
1009 
1010         dev_dbg(&s->udev->dev,
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);
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 
1034         ret = msi3101_ctrl_msg(s, CMD_WREG, reg7);
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 
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 
1061         /* wake-up tuner */
1062         v4l2_subdev_call(s->v4l2_subdev, core, s_power, 1);
1063 
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 
1077 static int msi3101_stop_streaming(struct vb2_queue *vq)
1078 {
1079         struct msi3101_state *s = vb2_get_drv_priv(vq);
1080         int ret;
1081         dev_dbg(&s->udev->dev, "%s:\n", __func__);
1082 
1083         if (mutex_lock_interruptible(&s->v4l2_lock))
1084                 return -ERESTARTSYS;
1085 
1086         if (s->udev)
1087                 msi3101_isoc_cleanup(s);
1088 
1089         msi3101_cleanup_queued_bufs(s);
1090 
1091         /* according to tests, at least 700us delay is required  */
1092         msleep(20);
1093         ret = msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0);
1094         if (ret)
1095                 goto err_sleep_tuner;
1096 
1097         /* sleep USB IF / ADC */
1098         ret = msi3101_ctrl_msg(s, CMD_WREG, 0x01000003);
1099         if (ret)
1100                 goto err_sleep_tuner;
1101 
1102 err_sleep_tuner:
1103         /* sleep tuner */
1104         ret = v4l2_subdev_call(s->v4l2_subdev, core, s_power, 0);
1105 
1106         mutex_unlock(&s->v4l2_lock);
1107 
1108         return ret;
1109 }
1110 
1111 static struct vb2_ops msi3101_vb2_ops = {
1112         .queue_setup            = msi3101_queue_setup,
1113         .buf_queue              = msi3101_buf_queue,
1114         .start_streaming        = msi3101_start_streaming,
1115         .stop_streaming         = msi3101_stop_streaming,
1116         .wait_prepare           = vb2_ops_wait_prepare,
1117         .wait_finish            = vb2_ops_wait_finish,
1118 };
1119 
1120 static int msi3101_enum_fmt_sdr_cap(struct file *file, void *priv,
1121                 struct v4l2_fmtdesc *f)
1122 {
1123         struct msi3101_state *s = video_drvdata(file);
1124         dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, f->index);
1125 
1126         if (f->index >= NUM_FORMATS)
1127                 return -EINVAL;
1128 
1129         strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1130         f->pixelformat = formats[f->index].pixelformat;
1131 
1132         return 0;
1133 }
1134 
1135 static int msi3101_g_fmt_sdr_cap(struct file *file, void *priv,
1136                 struct v4l2_format *f)
1137 {
1138         struct msi3101_state *s = video_drvdata(file);
1139         dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1140                         (char *)&s->pixelformat);
1141 
1142         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1143         f->fmt.sdr.pixelformat = s->pixelformat;
1144 
1145         return 0;
1146 }
1147 
1148 static int msi3101_s_fmt_sdr_cap(struct file *file, void *priv,
1149                 struct v4l2_format *f)
1150 {
1151         struct msi3101_state *s = video_drvdata(file);
1152         struct vb2_queue *q = &s->vb_queue;
1153         int i;
1154         dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1155                         (char *)&f->fmt.sdr.pixelformat);
1156 
1157         if (vb2_is_busy(q))
1158                 return -EBUSY;
1159 
1160         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1161         for (i = 0; i < NUM_FORMATS; i++) {
1162                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
1163                         s->pixelformat = f->fmt.sdr.pixelformat;
1164                         return 0;
1165                 }
1166         }
1167 
1168         f->fmt.sdr.pixelformat = formats[0].pixelformat;
1169         s->pixelformat = formats[0].pixelformat;
1170 
1171         return 0;
1172 }
1173 
1174 static int msi3101_try_fmt_sdr_cap(struct file *file, void *priv,
1175                 struct v4l2_format *f)
1176 {
1177         struct msi3101_state *s = video_drvdata(file);
1178         int i;
1179         dev_dbg(&s->udev->dev, "%s: pixelformat fourcc %4.4s\n", __func__,
1180                         (char *)&f->fmt.sdr.pixelformat);
1181 
1182         memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
1183         for (i = 0; i < NUM_FORMATS; i++) {
1184                 if (formats[i].pixelformat == f->fmt.sdr.pixelformat)
1185                         return 0;
1186         }
1187 
1188         f->fmt.sdr.pixelformat = formats[0].pixelformat;
1189 
1190         return 0;
1191 }
1192 
1193 static int msi3101_s_tuner(struct file *file, void *priv,
1194                 const struct v4l2_tuner *v)
1195 {
1196         struct msi3101_state *s = video_drvdata(file);
1197         int ret;
1198         dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
1199 
1200         if (v->index == 0)
1201                 ret = 0;
1202         else if (v->index == 1)
1203                 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_tuner, v);
1204         else
1205                 ret = -EINVAL;
1206 
1207         return ret;
1208 }
1209 
1210 static int msi3101_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v)
1211 {
1212         struct msi3101_state *s = video_drvdata(file);
1213         int ret;
1214         dev_dbg(&s->udev->dev, "%s: index=%d\n", __func__, v->index);
1215 
1216         if (v->index == 0) {
1217                 strlcpy(v->name, "Mirics MSi2500", sizeof(v->name));
1218                 v->type = V4L2_TUNER_ADC;
1219                 v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
1220                 v->rangelow =   1200000;
1221                 v->rangehigh = 15000000;
1222                 ret = 0;
1223         } else if (v->index == 1) {
1224                 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_tuner, v);
1225         } else {
1226                 ret = -EINVAL;
1227         }
1228 
1229         return ret;
1230 }
1231 
1232 static int msi3101_g_frequency(struct file *file, void *priv,
1233                 struct v4l2_frequency *f)
1234 {
1235         struct msi3101_state *s = video_drvdata(file);
1236         int ret  = 0;
1237         dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d\n",
1238                         __func__, f->tuner, f->type);
1239 
1240         if (f->tuner == 0) {
1241                 f->frequency = s->f_adc;
1242                 ret = 0;
1243         } else if (f->tuner == 1) {
1244                 f->type = V4L2_TUNER_RF;
1245                 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, g_frequency, f);
1246         } else {
1247                 ret = -EINVAL;
1248         }
1249 
1250         return ret;
1251 }
1252 
1253 static int msi3101_s_frequency(struct file *file, void *priv,
1254                 const struct v4l2_frequency *f)
1255 {
1256         struct msi3101_state *s = video_drvdata(file);
1257         int ret;
1258         dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d frequency=%u\n",
1259                         __func__, f->tuner, f->type, f->frequency);
1260 
1261         if (f->tuner == 0) {
1262                 s->f_adc = clamp_t(unsigned int, f->frequency,
1263                                 bands[0].rangelow,
1264                                 bands[0].rangehigh);
1265                 dev_dbg(&s->udev->dev, "%s: ADC frequency=%u Hz\n",
1266                                 __func__, s->f_adc);
1267                 ret = msi3101_set_usb_adc(s);
1268         } else if (f->tuner == 1) {
1269                 ret = v4l2_subdev_call(s->v4l2_subdev, tuner, s_frequency, f);
1270         } else {
1271                 ret = -EINVAL;
1272         }
1273 
1274         return ret;
1275 }
1276 
1277 static int msi3101_enum_freq_bands(struct file *file, void *priv,
1278                 struct v4l2_frequency_band *band)
1279 {
1280         struct msi3101_state *s = video_drvdata(file);
1281         int ret;
1282         dev_dbg(&s->udev->dev, "%s: tuner=%d type=%d index=%d\n",
1283                         __func__, band->tuner, band->type, band->index);
1284 
1285         if (band->tuner == 0) {
1286                 if (band->index >= ARRAY_SIZE(bands)) {
1287                         ret = -EINVAL;
1288                 } else {
1289                         *band = bands[band->index];
1290                         ret = 0;
1291                 }
1292         } else if (band->tuner == 1) {
1293                 ret = v4l2_subdev_call(s->v4l2_subdev, tuner,
1294                                 enum_freq_bands, band);
1295         } else {
1296                 ret = -EINVAL;
1297         }
1298 
1299         return ret;
1300 }
1301 
1302 static const struct v4l2_ioctl_ops msi3101_ioctl_ops = {
1303         .vidioc_querycap          = msi3101_querycap,
1304 
1305         .vidioc_enum_fmt_sdr_cap  = msi3101_enum_fmt_sdr_cap,
1306         .vidioc_g_fmt_sdr_cap     = msi3101_g_fmt_sdr_cap,
1307         .vidioc_s_fmt_sdr_cap     = msi3101_s_fmt_sdr_cap,
1308         .vidioc_try_fmt_sdr_cap   = msi3101_try_fmt_sdr_cap,
1309 
1310         .vidioc_reqbufs           = vb2_ioctl_reqbufs,
1311         .vidioc_create_bufs       = vb2_ioctl_create_bufs,
1312         .vidioc_prepare_buf       = vb2_ioctl_prepare_buf,
1313         .vidioc_querybuf          = vb2_ioctl_querybuf,
1314         .vidioc_qbuf              = vb2_ioctl_qbuf,
1315         .vidioc_dqbuf             = vb2_ioctl_dqbuf,
1316 
1317         .vidioc_streamon          = vb2_ioctl_streamon,
1318         .vidioc_streamoff         = vb2_ioctl_streamoff,
1319 
1320         .vidioc_g_tuner           = msi3101_g_tuner,
1321         .vidioc_s_tuner           = msi3101_s_tuner,
1322 
1323         .vidioc_g_frequency       = msi3101_g_frequency,
1324         .vidioc_s_frequency       = msi3101_s_frequency,
1325         .vidioc_enum_freq_bands   = msi3101_enum_freq_bands,
1326 
1327         .vidioc_subscribe_event   = v4l2_ctrl_subscribe_event,
1328         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1329         .vidioc_log_status        = v4l2_ctrl_log_status,
1330 };
1331 
1332 static const struct v4l2_file_operations msi3101_fops = {
1333         .owner                    = THIS_MODULE,
1334         .open                     = v4l2_fh_open,
1335         .release                  = vb2_fop_release,
1336         .read                     = vb2_fop_read,
1337         .poll                     = vb2_fop_poll,
1338         .mmap                     = vb2_fop_mmap,
1339         .unlocked_ioctl           = video_ioctl2,
1340 };
1341 
1342 static struct video_device msi3101_template = {
1343         .name                     = "Mirics MSi3101 SDR Dongle",
1344         .release                  = video_device_release_empty,
1345         .fops                     = &msi3101_fops,
1346         .ioctl_ops                = &msi3101_ioctl_ops,
1347 };
1348 
1349 static void msi3101_video_release(struct v4l2_device *v)
1350 {
1351         struct msi3101_state *s =
1352                         container_of(v, struct msi3101_state, v4l2_dev);
1353 
1354         v4l2_ctrl_handler_free(&s->hdl);
1355         v4l2_device_unregister(&s->v4l2_dev);
1356         kfree(s);
1357 }
1358 
1359 static int msi3101_transfer_one_message(struct spi_master *master,
1360                 struct spi_message *m)
1361 {
1362         struct msi3101_state *s = spi_master_get_devdata(master);
1363         struct spi_transfer *t;
1364         int ret = 0;
1365         u32 data;
1366 
1367         list_for_each_entry(t, &m->transfers, transfer_list) {
1368                 dev_dbg(&s->udev->dev, "%s: msg=%*ph\n",
1369                                 __func__, t->len, t->tx_buf);
1370                 data = 0x09; /* reg 9 is SPI adapter */
1371                 data |= ((u8 *)t->tx_buf)[0] << 8;
1372                 data |= ((u8 *)t->tx_buf)[1] << 16;
1373                 data |= ((u8 *)t->tx_buf)[2] << 24;
1374                 ret = msi3101_ctrl_msg(s, CMD_WREG, data);
1375         }
1376 
1377         m->status = ret;
1378         spi_finalize_current_message(master);
1379         return ret;
1380 }
1381 
1382 static int msi3101_probe(struct usb_interface *intf,
1383                 const struct usb_device_id *id)
1384 {
1385         struct usb_device *udev = interface_to_usbdev(intf);
1386         struct msi3101_state *s = NULL;
1387         struct v4l2_subdev *sd;
1388         struct spi_master *master;
1389         int ret;
1390         static struct spi_board_info board_info = {
1391                 .modalias               = "msi001",
1392                 .bus_num                = 0,
1393                 .chip_select            = 0,
1394                 .max_speed_hz           = 12000000,
1395         };
1396 
1397         s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL);
1398         if (s == NULL) {
1399                 pr_err("Could not allocate memory for msi3101_state\n");
1400                 return -ENOMEM;
1401         }
1402 
1403         mutex_init(&s->v4l2_lock);
1404         mutex_init(&s->vb_queue_lock);
1405         spin_lock_init(&s->queued_bufs_lock);
1406         INIT_LIST_HEAD(&s->queued_bufs);
1407         s->udev = udev;
1408         s->f_adc = bands[0].rangelow;
1409         s->pixelformat = V4L2_SDR_FMT_CU8;
1410 
1411         /* Init videobuf2 queue structure */
1412         s->vb_queue.type = V4L2_BUF_TYPE_SDR_CAPTURE;
1413         s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1414         s->vb_queue.drv_priv = s;
1415         s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf);
1416         s->vb_queue.ops = &msi3101_vb2_ops;
1417         s->vb_queue.mem_ops = &vb2_vmalloc_memops;
1418         s->vb_queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1419         ret = vb2_queue_init(&s->vb_queue);
1420         if (ret) {
1421                 dev_err(&s->udev->dev, "Could not initialize vb2 queue\n");
1422                 goto err_free_mem;
1423         }
1424 
1425         /* Init video_device structure */
1426         s->vdev = msi3101_template;
1427         s->vdev.queue = &s->vb_queue;
1428         s->vdev.queue->lock = &s->vb_queue_lock;
1429         set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags);
1430         video_set_drvdata(&s->vdev, s);
1431 
1432         /* Register the v4l2_device structure */
1433         s->v4l2_dev.release = msi3101_video_release;
1434         ret = v4l2_device_register(&intf->dev, &s->v4l2_dev);
1435         if (ret) {
1436                 dev_err(&s->udev->dev,
1437                                 "Failed to register v4l2-device (%d)\n", ret);
1438                 goto err_free_mem;
1439         }
1440 
1441         /* SPI master adapter */
1442         master = spi_alloc_master(&s->udev->dev, 0);
1443         if (master == NULL) {
1444                 ret = -ENOMEM;
1445                 goto err_unregister_v4l2_dev;
1446         }
1447 
1448         s->master = master;
1449         master->bus_num = 0;
1450         master->num_chipselect = 1;
1451         master->transfer_one_message = msi3101_transfer_one_message;
1452         spi_master_set_devdata(master, s);
1453         ret = spi_register_master(master);
1454         if (ret) {
1455                 spi_master_put(master);
1456                 goto err_unregister_v4l2_dev;
1457         }
1458 
1459         /* load v4l2 subdevice */
1460         sd = v4l2_spi_new_subdev(&s->v4l2_dev, master, &board_info);
1461         s->v4l2_subdev = sd;
1462         if (sd == NULL) {
1463                 dev_err(&s->udev->dev, "cannot get v4l2 subdevice\n");
1464                 ret = -ENODEV;
1465                 goto err_unregister_master;
1466         }
1467 
1468         /* Register controls */
1469         v4l2_ctrl_handler_init(&s->hdl, 0);
1470         if (s->hdl.error) {
1471                 ret = s->hdl.error;
1472                 dev_err(&s->udev->dev, "Could not initialize controls\n");
1473                 goto err_free_controls;
1474         }
1475 
1476         /* currently all controls are from subdev */
1477         v4l2_ctrl_add_handler(&s->hdl, sd->ctrl_handler, NULL);
1478 
1479         s->v4l2_dev.ctrl_handler = &s->hdl;
1480         s->vdev.v4l2_dev = &s->v4l2_dev;
1481         s->vdev.lock = &s->v4l2_lock;
1482 
1483         ret = video_register_device(&s->vdev, VFL_TYPE_SDR, -1);
1484         if (ret) {
1485                 dev_err(&s->udev->dev,
1486                                 "Failed to register as video device (%d)\n",
1487                                 ret);
1488                 goto err_unregister_v4l2_dev;
1489         }
1490         dev_info(&s->udev->dev, "Registered as %s\n",
1491                         video_device_node_name(&s->vdev));
1492 
1493         return 0;
1494 
1495 err_free_controls:
1496         v4l2_ctrl_handler_free(&s->hdl);
1497 err_unregister_master:
1498         spi_unregister_master(s->master);
1499 err_unregister_v4l2_dev:
1500         v4l2_device_unregister(&s->v4l2_dev);
1501 err_free_mem:
1502         kfree(s);
1503         return ret;
1504 }
1505 
1506 /* USB device ID list */
1507 static struct usb_device_id msi3101_id_table[] = {
1508         { USB_DEVICE(0x1df7, 0x2500) }, /* Mirics MSi3101 SDR Dongle */
1509         { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */
1510         { }
1511 };
1512 MODULE_DEVICE_TABLE(usb, msi3101_id_table);
1513 
1514 /* USB subsystem interface */
1515 static struct usb_driver msi3101_driver = {
1516         .name                     = KBUILD_MODNAME,
1517         .probe                    = msi3101_probe,
1518         .disconnect               = msi3101_disconnect,
1519         .id_table                 = msi3101_id_table,
1520 };
1521 
1522 module_usb_driver(msi3101_driver);
1523 
1524 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1525 MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle");
1526 MODULE_LICENSE("GPL");
1527 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us