]>
Commit | Line | Data |
---|---|---|
977e444f AP |
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 | ||
21 | #include <linux/kernel.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/input.h> | |
26 | #include <linux/videodev2.h> | |
27 | #include <media/v4l2-device.h> | |
28 | #include <media/v4l2-ioctl.h> | |
29 | #include <media/v4l2-ctrls.h> | |
30 | #include <media/v4l2-event.h> | |
31 | #include <linux/usb.h> | |
32 | #include <linux/mutex.h> | |
33 | #include <media/videobuf2-vmalloc.h> | |
34 | ||
35 | struct msi3101_gain { | |
36 | u8 tot:7; | |
37 | u8 baseband:6; | |
38 | bool lna:1; | |
39 | bool mixer:1; | |
40 | }; | |
41 | ||
42 | /* 60 – 120 MHz band, lna 24dB, mixer 19dB */ | |
43 | static const struct msi3101_gain msi3101_gain_lut_120[] = { | |
44 | { 0, 0, 0, 0}, | |
45 | { 1, 1, 0, 0}, | |
46 | { 2, 2, 0, 0}, | |
47 | { 3, 3, 0, 0}, | |
48 | { 4, 4, 0, 0}, | |
49 | { 5, 5, 0, 0}, | |
50 | { 6, 6, 0, 0}, | |
51 | { 7, 7, 0, 0}, | |
52 | { 8, 8, 0, 0}, | |
53 | { 9, 9, 0, 0}, | |
54 | { 10, 10, 0, 0}, | |
55 | { 11, 11, 0, 0}, | |
56 | { 12, 12, 0, 0}, | |
57 | { 13, 13, 0, 0}, | |
58 | { 14, 14, 0, 0}, | |
59 | { 15, 15, 0, 0}, | |
60 | { 16, 16, 0, 0}, | |
61 | { 17, 17, 0, 0}, | |
62 | { 18, 18, 0, 0}, | |
63 | { 19, 19, 0, 0}, | |
64 | { 20, 20, 0, 0}, | |
65 | { 21, 21, 0, 0}, | |
66 | { 22, 22, 0, 0}, | |
67 | { 23, 23, 0, 0}, | |
68 | { 24, 24, 0, 0}, | |
69 | { 25, 25, 0, 0}, | |
70 | { 26, 26, 0, 0}, | |
71 | { 27, 27, 0, 0}, | |
72 | { 28, 28, 0, 0}, | |
73 | { 29, 5, 1, 0}, | |
74 | { 30, 6, 1, 0}, | |
75 | { 31, 7, 1, 0}, | |
76 | { 32, 8, 1, 0}, | |
77 | { 33, 9, 1, 0}, | |
78 | { 34, 10, 1, 0}, | |
79 | { 35, 11, 1, 0}, | |
80 | { 36, 12, 1, 0}, | |
81 | { 37, 13, 1, 0}, | |
82 | { 38, 14, 1, 0}, | |
83 | { 39, 15, 1, 0}, | |
84 | { 40, 16, 1, 0}, | |
85 | { 41, 17, 1, 0}, | |
86 | { 42, 18, 1, 0}, | |
87 | { 43, 19, 1, 0}, | |
88 | { 44, 20, 1, 0}, | |
89 | { 45, 21, 1, 0}, | |
90 | { 46, 22, 1, 0}, | |
91 | { 47, 23, 1, 0}, | |
92 | { 48, 24, 1, 0}, | |
93 | { 49, 25, 1, 0}, | |
94 | { 50, 26, 1, 0}, | |
95 | { 51, 27, 1, 0}, | |
96 | { 52, 28, 1, 0}, | |
97 | { 53, 29, 1, 0}, | |
98 | { 54, 30, 1, 0}, | |
99 | { 55, 31, 1, 0}, | |
100 | { 56, 32, 1, 0}, | |
101 | { 57, 33, 1, 0}, | |
102 | { 58, 34, 1, 0}, | |
103 | { 59, 35, 1, 0}, | |
104 | { 60, 36, 1, 0}, | |
105 | { 61, 37, 1, 0}, | |
106 | { 62, 38, 1, 0}, | |
107 | { 63, 39, 1, 0}, | |
108 | { 64, 40, 1, 0}, | |
109 | { 65, 41, 1, 0}, | |
110 | { 66, 42, 1, 0}, | |
111 | { 67, 43, 1, 0}, | |
112 | { 68, 44, 1, 0}, | |
113 | { 69, 45, 1, 0}, | |
114 | { 70, 46, 1, 0}, | |
115 | { 71, 47, 1, 0}, | |
116 | { 72, 48, 1, 0}, | |
117 | { 73, 49, 1, 0}, | |
118 | { 74, 50, 1, 0}, | |
119 | { 75, 51, 1, 0}, | |
120 | { 76, 52, 1, 0}, | |
121 | { 77, 53, 1, 0}, | |
122 | { 78, 54, 1, 0}, | |
123 | { 79, 55, 1, 0}, | |
124 | { 80, 56, 1, 0}, | |
125 | { 81, 57, 1, 0}, | |
126 | { 82, 58, 1, 0}, | |
127 | { 83, 40, 1, 1}, | |
128 | { 84, 41, 1, 1}, | |
129 | { 85, 42, 1, 1}, | |
130 | { 86, 43, 1, 1}, | |
131 | { 87, 44, 1, 1}, | |
132 | { 88, 45, 1, 1}, | |
133 | { 89, 46, 1, 1}, | |
134 | { 90, 47, 1, 1}, | |
135 | { 91, 48, 1, 1}, | |
136 | { 92, 49, 1, 1}, | |
137 | { 93, 50, 1, 1}, | |
138 | { 94, 51, 1, 1}, | |
139 | { 95, 52, 1, 1}, | |
140 | { 96, 53, 1, 1}, | |
141 | { 97, 54, 1, 1}, | |
142 | { 98, 55, 1, 1}, | |
143 | { 99, 56, 1, 1}, | |
144 | {100, 57, 1, 1}, | |
145 | {101, 58, 1, 1}, | |
146 | {102, 59, 1, 1}, | |
147 | }; | |
148 | ||
149 | /* 120 – 245 MHz band, lna 24dB, mixer 19dB */ | |
150 | static const struct msi3101_gain msi3101_gain_lut_245[] = { | |
151 | { 0, 0, 0, 0}, | |
152 | { 1, 1, 0, 0}, | |
153 | { 2, 2, 0, 0}, | |
154 | { 3, 3, 0, 0}, | |
155 | { 4, 4, 0, 0}, | |
156 | { 5, 5, 0, 0}, | |
157 | { 6, 6, 0, 0}, | |
158 | { 7, 7, 0, 0}, | |
159 | { 8, 8, 0, 0}, | |
160 | { 9, 9, 0, 0}, | |
161 | { 10, 10, 0, 0}, | |
162 | { 11, 11, 0, 0}, | |
163 | { 12, 12, 0, 0}, | |
164 | { 13, 13, 0, 0}, | |
165 | { 14, 14, 0, 0}, | |
166 | { 15, 15, 0, 0}, | |
167 | { 16, 16, 0, 0}, | |
168 | { 17, 17, 0, 0}, | |
169 | { 18, 18, 0, 0}, | |
170 | { 19, 19, 0, 0}, | |
171 | { 20, 20, 0, 0}, | |
172 | { 21, 21, 0, 0}, | |
173 | { 22, 22, 0, 0}, | |
174 | { 23, 23, 0, 0}, | |
175 | { 24, 24, 0, 0}, | |
176 | { 25, 25, 0, 0}, | |
177 | { 26, 26, 0, 0}, | |
178 | { 27, 27, 0, 0}, | |
179 | { 28, 28, 0, 0}, | |
180 | { 29, 5, 1, 0}, | |
181 | { 30, 6, 1, 0}, | |
182 | { 31, 7, 1, 0}, | |
183 | { 32, 8, 1, 0}, | |
184 | { 33, 9, 1, 0}, | |
185 | { 34, 10, 1, 0}, | |
186 | { 35, 11, 1, 0}, | |
187 | { 36, 12, 1, 0}, | |
188 | { 37, 13, 1, 0}, | |
189 | { 38, 14, 1, 0}, | |
190 | { 39, 15, 1, 0}, | |
191 | { 40, 16, 1, 0}, | |
192 | { 41, 17, 1, 0}, | |
193 | { 42, 18, 1, 0}, | |
194 | { 43, 19, 1, 0}, | |
195 | { 44, 20, 1, 0}, | |
196 | { 45, 21, 1, 0}, | |
197 | { 46, 22, 1, 0}, | |
198 | { 47, 23, 1, 0}, | |
199 | { 48, 24, 1, 0}, | |
200 | { 49, 25, 1, 0}, | |
201 | { 50, 26, 1, 0}, | |
202 | { 51, 27, 1, 0}, | |
203 | { 52, 28, 1, 0}, | |
204 | { 53, 29, 1, 0}, | |
205 | { 54, 30, 1, 0}, | |
206 | { 55, 31, 1, 0}, | |
207 | { 56, 32, 1, 0}, | |
208 | { 57, 33, 1, 0}, | |
209 | { 58, 34, 1, 0}, | |
210 | { 59, 35, 1, 0}, | |
211 | { 60, 36, 1, 0}, | |
212 | { 61, 37, 1, 0}, | |
213 | { 62, 38, 1, 0}, | |
214 | { 63, 39, 1, 0}, | |
215 | { 64, 40, 1, 0}, | |
216 | { 65, 41, 1, 0}, | |
217 | { 66, 42, 1, 0}, | |
218 | { 67, 43, 1, 0}, | |
219 | { 68, 44, 1, 0}, | |
220 | { 69, 45, 1, 0}, | |
221 | { 70, 46, 1, 0}, | |
222 | { 71, 47, 1, 0}, | |
223 | { 72, 48, 1, 0}, | |
224 | { 73, 49, 1, 0}, | |
225 | { 74, 50, 1, 0}, | |
226 | { 75, 51, 1, 0}, | |
227 | { 76, 52, 1, 0}, | |
228 | { 77, 53, 1, 0}, | |
229 | { 78, 54, 1, 0}, | |
230 | { 79, 55, 1, 0}, | |
231 | { 80, 56, 1, 0}, | |
232 | { 81, 57, 1, 0}, | |
233 | { 82, 58, 1, 0}, | |
234 | { 83, 40, 1, 1}, | |
235 | { 84, 41, 1, 1}, | |
236 | { 85, 42, 1, 1}, | |
237 | { 86, 43, 1, 1}, | |
238 | { 87, 44, 1, 1}, | |
239 | { 88, 45, 1, 1}, | |
240 | { 89, 46, 1, 1}, | |
241 | { 90, 47, 1, 1}, | |
242 | { 91, 48, 1, 1}, | |
243 | { 92, 49, 1, 1}, | |
244 | { 93, 50, 1, 1}, | |
245 | { 94, 51, 1, 1}, | |
246 | { 95, 52, 1, 1}, | |
247 | { 96, 53, 1, 1}, | |
248 | { 97, 54, 1, 1}, | |
249 | { 98, 55, 1, 1}, | |
250 | { 99, 56, 1, 1}, | |
251 | {100, 57, 1, 1}, | |
252 | {101, 58, 1, 1}, | |
253 | {102, 59, 1, 1}, | |
254 | }; | |
255 | ||
256 | /* 420 – 1000 MHz band, lna 7dB, mixer 19dB */ | |
257 | static const struct msi3101_gain msi3101_gain_lut_1000[] = { | |
258 | { 0, 0, 0, 0}, | |
259 | { 1, 1, 0, 0}, | |
260 | { 2, 2, 0, 0}, | |
261 | { 3, 3, 0, 0}, | |
262 | { 4, 4, 0, 0}, | |
263 | { 5, 5, 0, 0}, | |
264 | { 6, 6, 0, 0}, | |
265 | { 7, 7, 0, 0}, | |
266 | { 8, 8, 0, 0}, | |
267 | { 9, 9, 0, 0}, | |
268 | { 10, 10, 0, 0}, | |
269 | { 11, 11, 0, 0}, | |
270 | { 12, 5, 1, 0}, | |
271 | { 13, 6, 1, 0}, | |
272 | { 14, 7, 1, 0}, | |
273 | { 15, 8, 1, 0}, | |
274 | { 16, 9, 1, 0}, | |
275 | { 17, 10, 1, 0}, | |
276 | { 18, 11, 1, 0}, | |
277 | { 19, 12, 1, 0}, | |
278 | { 20, 13, 1, 0}, | |
279 | { 21, 14, 1, 0}, | |
280 | { 22, 15, 1, 0}, | |
281 | { 23, 16, 1, 0}, | |
282 | { 24, 17, 1, 0}, | |
283 | { 25, 18, 1, 0}, | |
284 | { 26, 19, 1, 0}, | |
285 | { 27, 20, 1, 0}, | |
286 | { 28, 21, 1, 0}, | |
287 | { 29, 22, 1, 0}, | |
288 | { 30, 23, 1, 0}, | |
289 | { 31, 24, 1, 0}, | |
290 | { 32, 25, 1, 0}, | |
291 | { 33, 26, 1, 0}, | |
292 | { 34, 27, 1, 0}, | |
293 | { 35, 28, 1, 0}, | |
294 | { 36, 29, 1, 0}, | |
295 | { 37, 30, 1, 0}, | |
296 | { 38, 31, 1, 0}, | |
297 | { 39, 32, 1, 0}, | |
298 | { 40, 33, 1, 0}, | |
299 | { 41, 34, 1, 0}, | |
300 | { 42, 35, 1, 0}, | |
301 | { 43, 36, 1, 0}, | |
302 | { 44, 37, 1, 0}, | |
303 | { 45, 38, 1, 0}, | |
304 | { 46, 39, 1, 0}, | |
305 | { 47, 40, 1, 0}, | |
306 | { 48, 41, 1, 0}, | |
307 | { 49, 42, 1, 0}, | |
308 | { 50, 43, 1, 0}, | |
309 | { 51, 44, 1, 0}, | |
310 | { 52, 45, 1, 0}, | |
311 | { 53, 46, 1, 0}, | |
312 | { 54, 47, 1, 0}, | |
313 | { 55, 48, 1, 0}, | |
314 | { 56, 49, 1, 0}, | |
315 | { 57, 50, 1, 0}, | |
316 | { 58, 51, 1, 0}, | |
317 | { 59, 52, 1, 0}, | |
318 | { 60, 53, 1, 0}, | |
319 | { 61, 54, 1, 0}, | |
320 | { 62, 55, 1, 0}, | |
321 | { 63, 56, 1, 0}, | |
322 | { 64, 57, 1, 0}, | |
323 | { 65, 58, 1, 0}, | |
324 | { 66, 40, 1, 1}, | |
325 | { 67, 41, 1, 1}, | |
326 | { 68, 42, 1, 1}, | |
327 | { 69, 43, 1, 1}, | |
328 | { 70, 44, 1, 1}, | |
329 | { 71, 45, 1, 1}, | |
330 | { 72, 46, 1, 1}, | |
331 | { 73, 47, 1, 1}, | |
332 | { 74, 48, 1, 1}, | |
333 | { 75, 49, 1, 1}, | |
334 | { 76, 50, 1, 1}, | |
335 | { 77, 51, 1, 1}, | |
336 | { 78, 52, 1, 1}, | |
337 | { 79, 53, 1, 1}, | |
338 | { 80, 54, 1, 1}, | |
339 | { 81, 55, 1, 1}, | |
340 | { 82, 56, 1, 1}, | |
341 | { 83, 57, 1, 1}, | |
342 | { 84, 58, 1, 1}, | |
343 | { 85, 59, 1, 1}, | |
344 | }; | |
345 | ||
346 | /* | |
347 | * iConfiguration 0 | |
348 | * bInterfaceNumber 0 | |
349 | * bAlternateSetting 1 | |
350 | * bNumEndpoints 1 | |
351 | * bEndpointAddress 0x81 EP 1 IN | |
352 | * bmAttributes 1 | |
353 | * Transfer Type Isochronous | |
354 | * wMaxPacketSize 0x1400 3x 1024 bytes | |
355 | * bInterval 1 | |
356 | */ | |
357 | #define MAX_ISO_BUFS (8) | |
358 | #define ISO_FRAMES_PER_DESC (8) | |
359 | #define ISO_MAX_FRAME_SIZE (3 * 1024) | |
360 | #define ISO_BUFFER_SIZE (ISO_FRAMES_PER_DESC * ISO_MAX_FRAME_SIZE) | |
361 | ||
362 | #define MAX_ISOC_ERRORS 20 | |
363 | ||
3673b35c AP |
364 | #define MSI3101_CID_SAMPLING_MODE ((V4L2_CID_USER_BASE | 0xf000) + 0) |
365 | #define MSI3101_CID_SAMPLING_RATE ((V4L2_CID_USER_BASE | 0xf000) + 1) | |
366 | #define MSI3101_CID_SAMPLING_RESOLUTION ((V4L2_CID_USER_BASE | 0xf000) + 2) | |
977e444f AP |
367 | #define MSI3101_CID_TUNER_RF ((V4L2_CID_USER_BASE | 0xf000) + 10) |
368 | #define MSI3101_CID_TUNER_BW ((V4L2_CID_USER_BASE | 0xf000) + 11) | |
369 | #define MSI3101_CID_TUNER_IF ((V4L2_CID_USER_BASE | 0xf000) + 12) | |
370 | #define MSI3101_CID_TUNER_GAIN ((V4L2_CID_USER_BASE | 0xf000) + 13) | |
371 | ||
372 | /* intermediate buffers with raw data from the USB device */ | |
373 | struct msi3101_frame_buf { | |
374 | struct vb2_buffer vb; /* common v4l buffer stuff -- must be first */ | |
375 | struct list_head list; | |
376 | void *data; /* raw data from USB device */ | |
377 | int filled; /* number of bytes filled to *data */ | |
378 | }; | |
379 | ||
380 | struct msi3101_state { | |
381 | struct video_device vdev; | |
382 | struct v4l2_device v4l2_dev; | |
383 | ||
384 | /* videobuf2 queue and queued buffers list */ | |
385 | struct vb2_queue vb_queue; | |
386 | struct list_head queued_bufs; | |
387 | spinlock_t queued_bufs_lock; /* Protects queued_bufs */ | |
388 | ||
389 | /* Note if taking both locks v4l2_lock must always be locked first! */ | |
390 | struct mutex v4l2_lock; /* Protects everything else */ | |
391 | struct mutex vb_queue_lock; /* Protects vb_queue and capt_file */ | |
392 | ||
393 | /* Pointer to our usb_device, will be NULL after unplug */ | |
394 | struct usb_device *udev; /* Both mutexes most be hold when setting! */ | |
395 | ||
396 | unsigned int isoc_errors; /* number of contiguous ISOC errors */ | |
397 | unsigned int vb_full; /* vb is full and packets dropped */ | |
398 | ||
399 | struct urb *urbs[MAX_ISO_BUFS]; | |
554cbfbe AP |
400 | int (*convert_stream) (struct msi3101_state *s, u32 *dst, u8 *src, |
401 | unsigned int src_len); | |
977e444f AP |
402 | |
403 | /* Controls */ | |
404 | struct v4l2_ctrl_handler ctrl_handler; | |
405 | struct v4l2_ctrl *ctrl_sampling_rate; | |
406 | struct v4l2_ctrl *ctrl_tuner_rf; | |
407 | struct v4l2_ctrl *ctrl_tuner_bw; | |
408 | struct v4l2_ctrl *ctrl_tuner_if; | |
409 | struct v4l2_ctrl *ctrl_tuner_gain; | |
410 | ||
34599b9b AP |
411 | u32 next_sample; /* for track lost packets */ |
412 | u32 sample; /* for sample rate calc */ | |
977e444f | 413 | unsigned long jiffies; |
02004681 | 414 | unsigned int sample_ctrl_bit[4]; |
977e444f AP |
415 | }; |
416 | ||
417 | /* Private functions */ | |
418 | static struct msi3101_frame_buf *msi3101_get_next_fill_buf( | |
419 | struct msi3101_state *s) | |
420 | { | |
421 | unsigned long flags = 0; | |
422 | struct msi3101_frame_buf *buf = NULL; | |
423 | ||
424 | spin_lock_irqsave(&s->queued_bufs_lock, flags); | |
425 | if (list_empty(&s->queued_bufs)) | |
426 | goto leave; | |
427 | ||
428 | buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, list); | |
429 | list_del(&buf->list); | |
430 | leave: | |
431 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); | |
432 | return buf; | |
433 | } | |
434 | ||
02004681 AP |
435 | /* |
436 | * +=========================================================================== | |
6450e509 | 437 | * | 00-1023 | USB packet |
02004681 AP |
438 | * +=========================================================================== |
439 | * | 00- 03 | sequence number of first sample in that USB packet | |
440 | * +--------------------------------------------------------------------------- | |
441 | * | 04- 15 | garbage | |
442 | * +--------------------------------------------------------------------------- | |
443 | * | 16- 175 | samples | |
444 | * +--------------------------------------------------------------------------- | |
445 | * | 176- 179 | control bits for previous samples | |
446 | * +--------------------------------------------------------------------------- | |
447 | * | 180- 339 | samples | |
448 | * +--------------------------------------------------------------------------- | |
449 | * | 340- 343 | control bits for previous samples | |
450 | * +--------------------------------------------------------------------------- | |
451 | * | 344- 503 | samples | |
452 | * +--------------------------------------------------------------------------- | |
453 | * | 504- 507 | control bits for previous samples | |
454 | * +--------------------------------------------------------------------------- | |
455 | * | 508- 667 | samples | |
456 | * +--------------------------------------------------------------------------- | |
457 | * | 668- 671 | control bits for previous samples | |
458 | * +--------------------------------------------------------------------------- | |
459 | * | 672- 831 | samples | |
460 | * +--------------------------------------------------------------------------- | |
461 | * | 832- 835 | control bits for previous samples | |
462 | * +--------------------------------------------------------------------------- | |
463 | * | 836- 995 | samples | |
464 | * +--------------------------------------------------------------------------- | |
465 | * | 996- 999 | control bits for previous samples | |
466 | * +--------------------------------------------------------------------------- | |
6450e509 | 467 | * | 1000-1023 | garbage |
02004681 AP |
468 | * +--------------------------------------------------------------------------- |
469 | * | |
470 | * Bytes 4 - 7 could have some meaning? | |
471 | * | |
472 | * Control bits for previous samples is 32-bit field, containing 16 x 2-bit | |
473 | * numbers. This results one 2-bit number for 8 samples. It is likely used for | |
474 | * for bit shifting sample by given bits, increasing actual sampling resolution. | |
475 | * Number 2 (0b10) was never seen. | |
476 | * | |
477 | * 6 * 16 * 2 * 4 = 768 samples. 768 * 4 = 3072 bytes | |
478 | */ | |
479 | ||
977e444f AP |
480 | /* |
481 | * Converts signed 10-bit integer into 32-bit IEEE floating point | |
482 | * representation. | |
483 | * Will be exact from 0 to 2^24. Above that, we round towards zero | |
484 | * as the fractional bits will not fit in a float. (It would be better to | |
485 | * round towards even as the fpu does, but that is slower.) | |
486 | */ | |
487 | #define I2F_FRAC_BITS 23 | |
488 | #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) | |
554cbfbe | 489 | static u32 msi3101_convert_sample_384(struct msi3101_state *s, u16 x, int shift) |
977e444f | 490 | { |
02004681 AP |
491 | u32 msb, exponent, fraction, sign; |
492 | s->sample_ctrl_bit[shift]++; | |
977e444f AP |
493 | |
494 | /* Zero is special */ | |
495 | if (!x) | |
496 | return 0; | |
497 | ||
02004681 AP |
498 | /* Convert 10-bit two's complement to 13-bit */ |
499 | if (x & (1 << 9)) { | |
500 | x |= ~0U << 10; /* set all the rest bits to one */ | |
501 | x <<= shift; | |
977e444f | 502 | x = -x; |
02004681 | 503 | x &= 0xfff; /* result is 12 bit ... + sign */ |
977e444f AP |
504 | sign = 1 << 31; |
505 | } else { | |
02004681 | 506 | x <<= shift; |
977e444f AP |
507 | sign = 0 << 31; |
508 | } | |
509 | ||
510 | /* Get location of the most significant bit */ | |
511 | msb = __fls(x); | |
512 | ||
513 | /* | |
514 | * Use a rotate instead of a shift because that works both leftwards | |
515 | * and rightwards due to the mod(32) behaviour. This means we don't | |
516 | * need to check to see if we are above 2^24 or not. | |
517 | */ | |
518 | fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; | |
519 | exponent = (127 + msb) << I2F_FRAC_BITS; | |
520 | ||
521 | return (fraction + exponent) | sign; | |
522 | } | |
523 | ||
524 | #define MSI3101_CONVERT_IN_URB_HANDLER | |
525 | #define MSI3101_EXTENSIVE_DEBUG | |
554cbfbe | 526 | static int msi3101_convert_stream_384(struct msi3101_state *s, u32 *dst, |
6450e509 | 527 | u8 *src, unsigned int src_len) |
977e444f AP |
528 | { |
529 | int i, j, k, l, i_max, dst_len = 0; | |
530 | u16 sample[4]; | |
02004681 | 531 | u32 bits; |
977e444f | 532 | #ifdef MSI3101_EXTENSIVE_DEBUG |
34599b9b | 533 | u32 sample_num[3]; |
977e444f AP |
534 | #endif |
535 | /* There could be 1-3 1024 bytes URB frames */ | |
536 | i_max = src_len / 1024; | |
537 | for (i = 0; i < i_max; i++) { | |
538 | #ifdef MSI3101_EXTENSIVE_DEBUG | |
34599b9b AP |
539 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; |
540 | if (i == 0 && s->next_sample != sample_num[0]) { | |
977e444f | 541 | dev_dbg(&s->udev->dev, |
34599b9b AP |
542 | "%d samples lost, %d %08x:%08x\n", |
543 | sample_num[0] - s->next_sample, | |
544 | src_len, s->next_sample, sample_num[0]); | |
977e444f | 545 | } |
6450e509 AP |
546 | |
547 | /* | |
548 | * Dump all unknown 'garbage' data - maybe we will discover | |
549 | * someday if there is something rational... | |
550 | */ | |
551 | dev_dbg_ratelimited(&s->udev->dev, | |
552 | "%*ph %*ph\n", 12, &src[4], 24, &src[1000]); | |
553 | memset(&src[4], 0, 12); | |
554 | memset(&src[1000], 0, 24); | |
977e444f AP |
555 | #endif |
556 | src += 16; | |
557 | for (j = 0; j < 6; j++) { | |
02004681 | 558 | bits = src[160 + 3] << 24 | src[160 + 2] << 16 | src[160 + 1] << 8 | src[160 + 0] << 0; |
977e444f AP |
559 | for (k = 0; k < 16; k++) { |
560 | for (l = 0; l < 10; l += 5) { | |
561 | sample[0] = (src[l + 0] & 0xff) >> 0 | (src[l + 1] & 0x03) << 8; | |
562 | sample[1] = (src[l + 1] & 0xfc) >> 2 | (src[l + 2] & 0x0f) << 6; | |
563 | sample[2] = (src[l + 2] & 0xf0) >> 4 | (src[l + 3] & 0x3f) << 4; | |
564 | sample[3] = (src[l + 3] & 0xc0) >> 6 | (src[l + 4] & 0xff) << 2; | |
565 | ||
554cbfbe AP |
566 | *dst++ = msi3101_convert_sample_384(s, sample[0], (bits >> (2 * k)) & 0x3); |
567 | *dst++ = msi3101_convert_sample_384(s, sample[1], (bits >> (2 * k)) & 0x3); | |
568 | *dst++ = msi3101_convert_sample_384(s, sample[2], (bits >> (2 * k)) & 0x3); | |
569 | *dst++ = msi3101_convert_sample_384(s, sample[3], (bits >> (2 * k)) & 0x3); | |
977e444f AP |
570 | |
571 | /* 4 x 32bit float samples */ | |
572 | dst_len += 4 * 4; | |
573 | } | |
574 | src += 10; | |
575 | } | |
576 | #ifdef MSI3101_EXTENSIVE_DEBUG | |
02004681 AP |
577 | dev_dbg_ratelimited(&s->udev->dev, |
578 | "sample control bits %08x\n", bits); | |
977e444f AP |
579 | #endif |
580 | src += 4; | |
581 | } | |
582 | src += 24; | |
583 | } | |
584 | ||
585 | #ifdef MSI3101_EXTENSIVE_DEBUG | |
34599b9b | 586 | /* calculate samping rate and output it in 10 seconds intervals */ |
977e444f AP |
587 | if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { |
588 | unsigned long jiffies_now = jiffies; | |
589 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); | |
34599b9b | 590 | unsigned int samples = sample_num[i_max - 1] - s->sample; |
977e444f | 591 | s->jiffies = jiffies_now; |
34599b9b | 592 | s->sample = sample_num[i_max - 1]; |
977e444f | 593 | dev_dbg(&s->udev->dev, |
02004681 | 594 | "slen=%d samples=%u msecs=%lu sampling rate=%lu bits=%d.%d.%d.%d\n", |
34599b9b | 595 | src_len, samples, msecs, |
02004681 AP |
596 | samples * 1000UL / msecs, |
597 | s->sample_ctrl_bit[0], s->sample_ctrl_bit[1], | |
598 | s->sample_ctrl_bit[2], s->sample_ctrl_bit[3]); | |
977e444f AP |
599 | } |
600 | ||
34599b9b AP |
601 | /* next sample (sample = sample + i * 384) */ |
602 | s->next_sample = sample_num[i_max - 1] + 384; | |
977e444f AP |
603 | #endif |
604 | return dst_len; | |
605 | } | |
606 | ||
8e2efac7 AP |
607 | /* |
608 | * Converts signed 12-bit integer into 32-bit IEEE floating point | |
609 | * representation. | |
610 | */ | |
611 | static u32 msi3101_convert_sample_336(struct msi3101_state *s, u16 x) | |
612 | { | |
613 | u32 msb, exponent, fraction, sign; | |
614 | ||
615 | /* Zero is special */ | |
616 | if (!x) | |
617 | return 0; | |
618 | ||
619 | /* Negative / positive value */ | |
620 | if (x & (1 << 11)) { | |
621 | x = -x; | |
622 | x &= 0x7ff; /* result is 11 bit ... + sign */ | |
623 | sign = 1 << 31; | |
624 | } else { | |
625 | sign = 0 << 31; | |
626 | } | |
627 | ||
628 | /* Get location of the most significant bit */ | |
629 | msb = __fls(x); | |
630 | ||
631 | /* | |
632 | * Use a rotate instead of a shift because that works both leftwards | |
633 | * and rightwards due to the mod(32) behaviour. This means we don't | |
634 | * need to check to see if we are above 2^24 or not. | |
635 | */ | |
636 | fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; | |
637 | exponent = (127 + msb) << I2F_FRAC_BITS; | |
638 | ||
639 | return (fraction + exponent) | sign; | |
640 | } | |
641 | ||
642 | static int msi3101_convert_stream_336(struct msi3101_state *s, u32 *dst, | |
643 | u8 *src, unsigned int src_len) | |
644 | { | |
645 | int i, j, i_max, dst_len = 0; | |
646 | u16 sample[2]; | |
647 | u32 sample_num[3]; | |
648 | ||
649 | /* There could be 1-3 1024 bytes URB frames */ | |
650 | i_max = src_len / 1024; | |
651 | ||
652 | for (i = 0; i < i_max; i++) { | |
653 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; | |
654 | if (i == 0 && s->next_sample != sample_num[0]) { | |
655 | dev_dbg_ratelimited(&s->udev->dev, | |
656 | "%d samples lost, %d %08x:%08x\n", | |
657 | sample_num[0] - s->next_sample, | |
658 | src_len, s->next_sample, sample_num[0]); | |
659 | } | |
660 | ||
661 | /* | |
662 | * Dump all unknown 'garbage' data - maybe we will discover | |
663 | * someday if there is something rational... | |
664 | */ | |
665 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); | |
666 | ||
667 | src += 16; | |
668 | for (j = 0; j < 1008; j += 3) { | |
669 | sample[0] = (src[j + 0] & 0xff) >> 0 | (src[j + 1] & 0x0f) << 8; | |
670 | sample[1] = (src[j + 1] & 0xf0) >> 4 | (src[j + 2] & 0xff) << 4; | |
671 | ||
672 | *dst++ = msi3101_convert_sample_336(s, sample[0]); | |
673 | *dst++ = msi3101_convert_sample_336(s, sample[1]); | |
674 | } | |
675 | /* 336 x I+Q 32bit float samples */ | |
676 | dst_len += 336 * 2 * 4; | |
677 | src += 1008; | |
678 | } | |
679 | ||
680 | /* calculate samping rate and output it in 10 seconds intervals */ | |
681 | if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { | |
682 | unsigned long jiffies_now = jiffies; | |
683 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); | |
684 | unsigned int samples = sample_num[i_max - 1] - s->sample; | |
685 | s->jiffies = jiffies_now; | |
686 | s->sample = sample_num[i_max - 1]; | |
687 | dev_dbg(&s->udev->dev, | |
688 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", | |
689 | src_len, samples, msecs, | |
690 | samples * 1000UL / msecs); | |
691 | } | |
692 | ||
693 | /* next sample (sample = sample + i * 336) */ | |
694 | s->next_sample = sample_num[i_max - 1] + 336; | |
695 | ||
696 | return dst_len; | |
697 | } | |
698 | ||
554cbfbe AP |
699 | /* |
700 | * Converts signed 14-bit integer into 32-bit IEEE floating point | |
701 | * representation. | |
702 | * Will be exact from 0 to 2^24. Above that, we round towards zero | |
703 | * as the fractional bits will not fit in a float. (It would be better to | |
704 | * round towards even as the fpu does, but that is slower.) | |
705 | */ | |
706 | #define I2F_FRAC_BITS 23 | |
707 | #define I2F_MASK ((1 << I2F_FRAC_BITS) - 1) | |
708 | static u32 msi3101_convert_sample_252(struct msi3101_state *s, u16 x) | |
709 | { | |
710 | u32 msb, exponent, fraction, sign; | |
711 | ||
712 | /* Zero is special */ | |
713 | if (!x) | |
714 | return 0; | |
715 | ||
716 | /* Negative / positive value */ | |
717 | if (x & (1 << 13)) { | |
718 | x = -x; | |
719 | x &= 0x1fff; /* result is 13 bit ... + sign */ | |
720 | sign = 1 << 31; | |
721 | } else { | |
722 | sign = 0 << 31; | |
723 | } | |
724 | ||
725 | /* Get location of the most significant bit */ | |
726 | msb = __fls(x); | |
727 | ||
728 | /* | |
729 | * Use a rotate instead of a shift because that works both leftwards | |
730 | * and rightwards due to the mod(32) behaviour. This means we don't | |
731 | * need to check to see if we are above 2^24 or not. | |
732 | */ | |
733 | fraction = ror32(x, (msb - I2F_FRAC_BITS) & 0x1f) & I2F_MASK; | |
734 | exponent = (127 + msb) << I2F_FRAC_BITS; | |
735 | ||
736 | return (fraction + exponent) | sign; | |
737 | } | |
738 | ||
739 | static int msi3101_convert_stream_252(struct msi3101_state *s, u32 *dst, | |
740 | u8 *src, unsigned int src_len) | |
741 | { | |
742 | int i, j, i_max, dst_len = 0; | |
743 | u16 sample[2]; | |
744 | u32 sample_num[3]; | |
745 | ||
746 | /* There could be 1-3 1024 bytes URB frames */ | |
747 | i_max = src_len / 1024; | |
748 | ||
749 | for (i = 0; i < i_max; i++) { | |
750 | sample_num[i] = src[3] << 24 | src[2] << 16 | src[1] << 8 | src[0] << 0; | |
751 | if (i == 0 && s->next_sample != sample_num[0]) { | |
752 | dev_dbg_ratelimited(&s->udev->dev, | |
753 | "%d samples lost, %d %08x:%08x\n", | |
754 | sample_num[0] - s->next_sample, | |
755 | src_len, s->next_sample, sample_num[0]); | |
756 | } | |
757 | ||
758 | /* | |
759 | * Dump all unknown 'garbage' data - maybe we will discover | |
760 | * someday if there is something rational... | |
761 | */ | |
762 | dev_dbg_ratelimited(&s->udev->dev, "%*ph\n", 12, &src[4]); | |
763 | ||
764 | src += 16; | |
765 | for (j = 0; j < 1008; j += 4) { | |
766 | sample[0] = src[j + 0] >> 0 | src[j + 1] << 8; | |
767 | sample[1] = src[j + 2] >> 0 | src[j + 3] << 8; | |
768 | ||
769 | *dst++ = msi3101_convert_sample_252(s, sample[0]); | |
770 | *dst++ = msi3101_convert_sample_252(s, sample[1]); | |
771 | } | |
772 | /* 252 x I+Q 32bit float samples */ | |
773 | dst_len += 252 * 2 * 4; | |
774 | src += 1008; | |
775 | } | |
776 | ||
777 | /* calculate samping rate and output it in 10 seconds intervals */ | |
778 | if ((s->jiffies + msecs_to_jiffies(10000)) <= jiffies) { | |
779 | unsigned long jiffies_now = jiffies; | |
780 | unsigned long msecs = jiffies_to_msecs(jiffies_now) - jiffies_to_msecs(s->jiffies); | |
781 | unsigned int samples = sample_num[i_max - 1] - s->sample; | |
782 | s->jiffies = jiffies_now; | |
783 | s->sample = sample_num[i_max - 1]; | |
784 | dev_dbg(&s->udev->dev, | |
785 | "slen=%d samples=%u msecs=%lu sampling rate=%lu\n", | |
786 | src_len, samples, msecs, | |
787 | samples * 1000UL / msecs); | |
788 | } | |
789 | ||
790 | /* next sample (sample = sample + i * 252) */ | |
791 | s->next_sample = sample_num[i_max - 1] + 252; | |
792 | ||
793 | return dst_len; | |
794 | } | |
795 | ||
977e444f AP |
796 | /* |
797 | * This gets called for the Isochronous pipe (stream). This is done in interrupt | |
798 | * time, so it has to be fast, not crash, and not stall. Neat. | |
799 | */ | |
800 | static void msi3101_isoc_handler(struct urb *urb) | |
801 | { | |
802 | struct msi3101_state *s = (struct msi3101_state *)urb->context; | |
803 | int i, flen, fstatus; | |
804 | unsigned char *iso_buf = NULL; | |
805 | struct msi3101_frame_buf *fbuf; | |
806 | ||
807 | if (urb->status == -ENOENT || urb->status == -ECONNRESET || | |
808 | urb->status == -ESHUTDOWN) { | |
809 | dev_dbg(&s->udev->dev, "URB (%p) unlinked %ssynchronuously\n", | |
810 | urb, urb->status == -ENOENT ? "" : "a"); | |
811 | return; | |
812 | } | |
813 | ||
814 | if (urb->status != 0) { | |
815 | dev_dbg(&s->udev->dev, | |
816 | "msi3101_isoc_handler() called with status %d\n", | |
817 | urb->status); | |
818 | /* Give up after a number of contiguous errors */ | |
819 | if (++s->isoc_errors > MAX_ISOC_ERRORS) | |
820 | dev_dbg(&s->udev->dev, | |
821 | "Too many ISOC errors, bailing out\n"); | |
822 | goto handler_end; | |
823 | } else { | |
824 | /* Reset ISOC error counter. We did get here, after all. */ | |
825 | s->isoc_errors = 0; | |
826 | } | |
827 | ||
828 | /* Compact data */ | |
829 | for (i = 0; i < urb->number_of_packets; i++) { | |
554cbfbe AP |
830 | void *ptr; |
831 | ||
977e444f AP |
832 | /* Check frame error */ |
833 | fstatus = urb->iso_frame_desc[i].status; | |
834 | if (fstatus) { | |
835 | dev_dbg(&s->udev->dev, | |
836 | "frame=%d/%d has error %d skipping\n", | |
837 | i, urb->number_of_packets, fstatus); | |
838 | goto skip; | |
839 | } | |
840 | ||
841 | /* Check if that frame contains data */ | |
842 | flen = urb->iso_frame_desc[i].actual_length; | |
843 | if (flen == 0) | |
844 | goto skip; | |
845 | ||
846 | iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
847 | ||
848 | /* Get free framebuffer */ | |
849 | fbuf = msi3101_get_next_fill_buf(s); | |
850 | if (fbuf == NULL) { | |
851 | s->vb_full++; | |
852 | dev_dbg_ratelimited(&s->udev->dev, | |
853 | "videobuf is full, %d packets dropped\n", | |
854 | s->vb_full); | |
855 | goto skip; | |
856 | } | |
857 | ||
858 | /* fill framebuffer */ | |
859 | #ifdef MSI3101_CONVERT_IN_URB_HANDLER | |
554cbfbe AP |
860 | ptr = vb2_plane_vaddr(&fbuf->vb, 0); |
861 | flen = s->convert_stream(s, ptr, iso_buf, flen); | |
862 | vb2_set_plane_payload(&fbuf->vb, 0, flen); | |
977e444f AP |
863 | #else |
864 | memcpy(fbuf->data, iso_buf, flen); | |
865 | fbuf->filled = flen; | |
866 | #endif | |
867 | vb2_buffer_done(&fbuf->vb, VB2_BUF_STATE_DONE); | |
868 | skip: | |
869 | ; | |
870 | } | |
871 | ||
872 | handler_end: | |
873 | i = usb_submit_urb(urb, GFP_ATOMIC); | |
874 | if (i != 0) | |
875 | dev_dbg(&s->udev->dev, | |
876 | "Error (%d) re-submitting urb in msi3101_isoc_handler\n", | |
877 | i); | |
878 | } | |
879 | ||
880 | static void msi3101_iso_stop(struct msi3101_state *s) | |
881 | { | |
882 | int i; | |
883 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
884 | ||
885 | /* Unlinking ISOC buffers one by one */ | |
886 | for (i = 0; i < MAX_ISO_BUFS; i++) { | |
887 | if (s->urbs[i]) { | |
888 | dev_dbg(&s->udev->dev, "Unlinking URB %p\n", | |
889 | s->urbs[i]); | |
890 | usb_kill_urb(s->urbs[i]); | |
891 | } | |
892 | } | |
893 | } | |
894 | ||
895 | static void msi3101_iso_free(struct msi3101_state *s) | |
896 | { | |
897 | int i; | |
898 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
899 | ||
900 | /* Freeing ISOC buffers one by one */ | |
901 | for (i = 0; i < MAX_ISO_BUFS; i++) { | |
902 | if (s->urbs[i]) { | |
903 | dev_dbg(&s->udev->dev, "Freeing URB\n"); | |
904 | if (s->urbs[i]->transfer_buffer) { | |
905 | usb_free_coherent(s->udev, | |
906 | s->urbs[i]->transfer_buffer_length, | |
907 | s->urbs[i]->transfer_buffer, | |
908 | s->urbs[i]->transfer_dma); | |
909 | } | |
910 | usb_free_urb(s->urbs[i]); | |
911 | s->urbs[i] = NULL; | |
912 | } | |
913 | } | |
914 | } | |
915 | ||
916 | /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ | |
917 | static void msi3101_isoc_cleanup(struct msi3101_state *s) | |
918 | { | |
919 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
920 | ||
921 | msi3101_iso_stop(s); | |
922 | msi3101_iso_free(s); | |
923 | } | |
924 | ||
925 | /* Both v4l2_lock and vb_queue_lock should be locked when calling this */ | |
926 | static int msi3101_isoc_init(struct msi3101_state *s) | |
927 | { | |
928 | struct usb_device *udev; | |
929 | struct urb *urb; | |
930 | int i, j, ret; | |
931 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
932 | ||
933 | s->isoc_errors = 0; | |
934 | udev = s->udev; | |
935 | ||
936 | ret = usb_set_interface(s->udev, 0, 1); | |
937 | if (ret < 0) | |
938 | return ret; | |
939 | ||
940 | /* Allocate and init Isochronuous urbs */ | |
941 | for (i = 0; i < MAX_ISO_BUFS; i++) { | |
942 | urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL); | |
943 | if (urb == NULL) { | |
944 | dev_err(&s->udev->dev, | |
945 | "Failed to allocate urb %d\n", i); | |
946 | msi3101_isoc_cleanup(s); | |
947 | return -ENOMEM; | |
948 | } | |
949 | s->urbs[i] = urb; | |
950 | dev_dbg(&s->udev->dev, "Allocated URB at 0x%p\n", urb); | |
951 | ||
952 | urb->interval = 1; | |
953 | urb->dev = udev; | |
954 | urb->pipe = usb_rcvisocpipe(udev, 0x81); | |
955 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | |
956 | urb->transfer_buffer = usb_alloc_coherent(udev, ISO_BUFFER_SIZE, | |
957 | GFP_KERNEL, &urb->transfer_dma); | |
958 | if (urb->transfer_buffer == NULL) { | |
959 | dev_err(&s->udev->dev, | |
960 | "Failed to allocate urb buffer %d\n", | |
961 | i); | |
962 | msi3101_isoc_cleanup(s); | |
963 | return -ENOMEM; | |
964 | } | |
965 | urb->transfer_buffer_length = ISO_BUFFER_SIZE; | |
966 | urb->complete = msi3101_isoc_handler; | |
967 | urb->context = s; | |
968 | urb->start_frame = 0; | |
969 | urb->number_of_packets = ISO_FRAMES_PER_DESC; | |
970 | for (j = 0; j < ISO_FRAMES_PER_DESC; j++) { | |
971 | urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE; | |
972 | urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE; | |
973 | } | |
974 | } | |
975 | ||
976 | /* link */ | |
977 | for (i = 0; i < MAX_ISO_BUFS; i++) { | |
978 | ret = usb_submit_urb(s->urbs[i], GFP_KERNEL); | |
979 | if (ret) { | |
980 | dev_err(&s->udev->dev, | |
981 | "isoc_init() submit_urb %d failed with error %d\n", | |
982 | i, ret); | |
983 | msi3101_isoc_cleanup(s); | |
984 | return ret; | |
985 | } | |
986 | dev_dbg(&s->udev->dev, "URB 0x%p submitted.\n", s->urbs[i]); | |
987 | } | |
988 | ||
989 | /* All is done... */ | |
990 | return 0; | |
991 | } | |
992 | ||
993 | /* Must be called with vb_queue_lock hold */ | |
994 | static void msi3101_cleanup_queued_bufs(struct msi3101_state *s) | |
995 | { | |
996 | unsigned long flags = 0; | |
997 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
998 | ||
999 | spin_lock_irqsave(&s->queued_bufs_lock, flags); | |
1000 | while (!list_empty(&s->queued_bufs)) { | |
1001 | struct msi3101_frame_buf *buf; | |
1002 | ||
1003 | buf = list_entry(s->queued_bufs.next, struct msi3101_frame_buf, | |
1004 | list); | |
1005 | list_del(&buf->list); | |
1006 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
1007 | } | |
1008 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); | |
1009 | } | |
1010 | ||
1011 | /* The user yanked out the cable... */ | |
1012 | static void msi3101_disconnect(struct usb_interface *intf) | |
1013 | { | |
1014 | struct v4l2_device *v = usb_get_intfdata(intf); | |
1015 | struct msi3101_state *s = | |
1016 | container_of(v, struct msi3101_state, v4l2_dev); | |
1017 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1018 | ||
1019 | mutex_lock(&s->vb_queue_lock); | |
1020 | mutex_lock(&s->v4l2_lock); | |
1021 | /* No need to keep the urbs around after disconnection */ | |
1022 | s->udev = NULL; | |
1023 | ||
1024 | v4l2_device_disconnect(&s->v4l2_dev); | |
1025 | video_unregister_device(&s->vdev); | |
1026 | mutex_unlock(&s->v4l2_lock); | |
1027 | mutex_unlock(&s->vb_queue_lock); | |
1028 | ||
1029 | v4l2_device_put(&s->v4l2_dev); | |
1030 | } | |
1031 | ||
1032 | static int msi3101_querycap(struct file *file, void *fh, | |
1033 | struct v4l2_capability *cap) | |
1034 | { | |
1035 | struct msi3101_state *s = video_drvdata(file); | |
1036 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1037 | ||
1038 | strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); | |
1039 | strlcpy(cap->card, s->vdev.name, sizeof(cap->card)); | |
1040 | usb_make_path(s->udev, cap->bus_info, sizeof(cap->bus_info)); | |
1041 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | | |
1042 | V4L2_CAP_READWRITE; | |
1043 | cap->device_caps = V4L2_CAP_TUNER; | |
1044 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | ||
1049 | /* Videobuf2 operations */ | |
1050 | static int msi3101_queue_setup(struct vb2_queue *vq, | |
1051 | const struct v4l2_format *fmt, unsigned int *nbuffers, | |
1052 | unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) | |
1053 | { | |
1054 | struct msi3101_state *s = vb2_get_drv_priv(vq); | |
1055 | dev_dbg(&s->udev->dev, "%s: *nbuffers=%d\n", __func__, *nbuffers); | |
1056 | ||
1057 | /* Absolute min and max number of buffers available for mmap() */ | |
1058 | *nbuffers = 32; | |
1059 | *nplanes = 1; | |
1060 | sizes[0] = PAGE_ALIGN(3 * 3072); /* 3 * 768 * 4 */ | |
1061 | dev_dbg(&s->udev->dev, "%s: nbuffers=%d sizes[0]=%d\n", | |
1062 | __func__, *nbuffers, sizes[0]); | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | static int msi3101_buf_init(struct vb2_buffer *vb) | |
1067 | { | |
1068 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); | |
1069 | struct msi3101_frame_buf *fbuf = | |
1070 | container_of(vb, struct msi3101_frame_buf, vb); | |
1071 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1072 | ||
1073 | fbuf->data = vzalloc(ISO_MAX_FRAME_SIZE); | |
1074 | if (fbuf->data == NULL) | |
1075 | return -ENOMEM; | |
1076 | ||
1077 | return 0; | |
1078 | } | |
1079 | ||
1080 | static int msi3101_buf_prepare(struct vb2_buffer *vb) | |
1081 | { | |
1082 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); | |
1083 | ||
1084 | /* Don't allow queing new buffers after device disconnection */ | |
1085 | if (!s->udev) | |
1086 | return -ENODEV; | |
1087 | ||
1088 | return 0; | |
1089 | } | |
1090 | ||
977e444f AP |
1091 | #ifdef MSI3101_CONVERT_IN_URB_HANDLER |
1092 | static int msi3101_buf_finish(struct vb2_buffer *vb) | |
1093 | { | |
1094 | return 0; | |
1095 | } | |
1096 | #else | |
1097 | static int msi3101_buf_finish(struct vb2_buffer *vb) | |
1098 | { | |
1099 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); | |
1100 | struct msi3101_frame_buf *fbuf = | |
1101 | container_of(vb, struct msi3101_frame_buf, vb); | |
1102 | int ret; | |
1103 | u32 *dst = vb2_plane_vaddr(&fbuf->vb, 0); | |
554cbfbe | 1104 | ret = msi3101_convert_stream_384(s, dst, fbuf->data, fbuf->filled); |
977e444f AP |
1105 | vb2_set_plane_payload(&fbuf->vb, 0, ret); |
1106 | return 0; | |
1107 | } | |
1108 | #endif | |
1109 | ||
1110 | static void msi3101_buf_cleanup(struct vb2_buffer *vb) | |
1111 | { | |
1112 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); | |
1113 | struct msi3101_frame_buf *buf = | |
1114 | container_of(vb, struct msi3101_frame_buf, vb); | |
1115 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1116 | ||
1117 | vfree(buf->data); | |
1118 | } | |
1119 | static void msi3101_buf_queue(struct vb2_buffer *vb) | |
1120 | { | |
1121 | struct msi3101_state *s = vb2_get_drv_priv(vb->vb2_queue); | |
1122 | struct msi3101_frame_buf *buf = | |
1123 | container_of(vb, struct msi3101_frame_buf, vb); | |
1124 | unsigned long flags = 0; | |
1125 | ||
1126 | /* Check the device has not disconnected between prep and queuing */ | |
1127 | if (!s->udev) { | |
1128 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
1129 | return; | |
1130 | } | |
1131 | ||
1132 | spin_lock_irqsave(&s->queued_bufs_lock, flags); | |
1133 | list_add_tail(&buf->list, &s->queued_bufs); | |
1134 | spin_unlock_irqrestore(&s->queued_bufs_lock, flags); | |
1135 | } | |
1136 | ||
1137 | #define CMD_WREG 0x41 | |
1138 | #define CMD_START_STREAMING 0x43 | |
1139 | #define CMD_STOP_STREAMING 0x45 | |
1140 | #define CMD_READ_UNKNOW 0x48 | |
1141 | ||
1142 | #define msi3101_dbg_usb_control_msg(udev, r, t, v, _i, b, l) { \ | |
1143 | char *direction; \ | |
1144 | if (t == (USB_TYPE_VENDOR | USB_DIR_OUT)) \ | |
1145 | direction = ">>>"; \ | |
1146 | else \ | |
1147 | direction = "<<<"; \ | |
1148 | dev_dbg(&udev->dev, "%s: %02x %02x %02x %02x %02x %02x %02x %02x " \ | |
1149 | "%s %*ph\n", __func__, t, r, v & 0xff, v >> 8, \ | |
1150 | _i & 0xff, _i >> 8, l & 0xff, l >> 8, direction, l, b); \ | |
1151 | } | |
1152 | ||
1153 | static int msi3101_ctrl_msg(struct msi3101_state *s, u8 cmd, u32 data) | |
1154 | { | |
1155 | int ret; | |
1156 | u8 request = cmd; | |
1157 | u8 requesttype = USB_DIR_OUT | USB_TYPE_VENDOR; | |
1158 | u16 value = (data >> 0) & 0xffff; | |
1159 | u16 index = (data >> 16) & 0xffff; | |
1160 | ||
1161 | msi3101_dbg_usb_control_msg(s->udev, | |
1162 | request, requesttype, value, index, NULL, 0); | |
1163 | ||
d0fadf40 | 1164 | ret = usb_control_msg(s->udev, usb_sndctrlpipe(s->udev, 0), |
977e444f AP |
1165 | request, requesttype, value, index, NULL, 0, 2000); |
1166 | ||
1167 | if (ret) | |
1168 | dev_err(&s->udev->dev, "%s: failed %d, cmd %02x, data %04x\n", | |
1169 | __func__, ret, cmd, data); | |
1170 | ||
1171 | return ret; | |
1172 | }; | |
1173 | ||
1174 | static int msi3101_tuner_write(struct msi3101_state *s, u32 data) | |
1175 | { | |
1176 | return msi3101_ctrl_msg(s, CMD_WREG, data << 8 | 0x09); | |
1177 | }; | |
1178 | ||
1179 | #define F_REF 24000000 | |
b1520857 | 1180 | #define DIV_R_IN 2 |
977e444f AP |
1181 | static int msi3101_set_usb_adc(struct msi3101_state *s) |
1182 | { | |
22ca680e | 1183 | int ret, div_n, div_m, div_r_out, f_sr, f_vco, fract; |
554cbfbe AP |
1184 | u32 reg3, reg4, reg7; |
1185 | ||
1186 | f_sr = s->ctrl_sampling_rate->val64; | |
1187 | ||
1188 | /* select stream format */ | |
1189 | if (f_sr < 6000000) { | |
1190 | s->convert_stream = msi3101_convert_stream_252; | |
1191 | reg7 = 0x00009407; | |
8e2efac7 AP |
1192 | } else if (f_sr < 8000000) { |
1193 | s->convert_stream = msi3101_convert_stream_336; | |
1194 | reg7 = 0x00008507; | |
554cbfbe AP |
1195 | } else { |
1196 | s->convert_stream = msi3101_convert_stream_384; | |
1197 | reg7 = 0x0000a507; | |
1198 | } | |
1199 | ||
977e444f | 1200 | /* |
b1520857 | 1201 | * Synthesizer config is just a educated guess... |
977e444f | 1202 | * |
977e444f AP |
1203 | * [7:0] 0x03, register address |
1204 | * [8] 1, always | |
1205 | * [9] ? | |
1206 | * [12:10] output divider | |
1207 | * [13] 0 ? | |
1208 | * [14] 0 ? | |
22ca680e | 1209 | * [15] fractional MSB, bit 20 |
977e444f AP |
1210 | * [16:19] N |
1211 | * [23:20] ? | |
1212 | * [24:31] 0x01 | |
1213 | * | |
1214 | * output divider | |
1215 | * val div | |
1216 | * 0 - (invalid) | |
b1520857 AP |
1217 | * 1 4 |
1218 | * 2 6 | |
1219 | * 3 8 | |
1220 | * 4 10 | |
1221 | * 5 12 | |
1222 | * 6 14 | |
1223 | * 7 16 | |
1224 | * | |
1225 | * VCO 202000000 - 720000000++ | |
977e444f | 1226 | */ |
c5a431d0 | 1227 | reg3 = 0x01000303; |
22ca680e | 1228 | reg4 = 0x00000004; |
977e444f | 1229 | |
c5a431d0 AP |
1230 | /* XXX: Filters? AGC? */ |
1231 | if (f_sr < 6000000) | |
1232 | reg3 |= 0x1 << 20; | |
1233 | else if (f_sr < 7000000) | |
1234 | reg3 |= 0x5 << 20; | |
1235 | else if (f_sr < 8500000) | |
1236 | reg3 |= 0x9 << 20; | |
1237 | else | |
1238 | reg3 |= 0xd << 20; | |
1239 | ||
b1520857 AP |
1240 | for (div_r_out = 4; div_r_out < 16; div_r_out += 2) { |
1241 | f_vco = f_sr * div_r_out * 12; | |
1242 | dev_dbg(&s->udev->dev, "%s: div_r_out=%d f_vco=%d\n", | |
1243 | __func__, div_r_out, f_vco); | |
1244 | if (f_vco >= 202000000) | |
977e444f AP |
1245 | break; |
1246 | } | |
1247 | ||
b1520857 AP |
1248 | div_n = f_vco / (F_REF * DIV_R_IN); |
1249 | div_m = f_vco % (F_REF * DIV_R_IN); | |
22ca680e | 1250 | fract = 0x200000ul * div_m / (F_REF * DIV_R_IN); |
977e444f | 1251 | |
b1520857 AP |
1252 | reg3 |= div_n << 16; |
1253 | reg3 |= (div_r_out / 2 - 1) << 10; | |
22ca680e AP |
1254 | reg3 |= ((fract >> 20) & 0x000001) << 15; /* [20] */ |
1255 | reg4 |= ((fract >> 0) & 0x0fffff) << 8; /* [19:0] */ | |
977e444f | 1256 | |
b1520857 | 1257 | dev_dbg(&s->udev->dev, |
22ca680e AP |
1258 | "%s: f_sr=%d f_vco=%d div_n=%d div_m=%d div_r_out=%d reg3=%08x reg4=%08x\n", |
1259 | __func__, f_sr, f_vco, div_n, div_m, div_r_out, reg3, reg4); | |
977e444f AP |
1260 | |
1261 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00608008); | |
1262 | if (ret) | |
1263 | goto err; | |
1264 | ||
1265 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00000c05); | |
1266 | if (ret) | |
1267 | goto err; | |
1268 | ||
1269 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00020000); | |
1270 | if (ret) | |
1271 | goto err; | |
1272 | ||
1273 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00480102); | |
1274 | if (ret) | |
1275 | goto err; | |
1276 | ||
1277 | ret = msi3101_ctrl_msg(s, CMD_WREG, 0x00f38008); | |
1278 | if (ret) | |
1279 | goto err; | |
1280 | ||
554cbfbe | 1281 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg7); |
977e444f AP |
1282 | if (ret) |
1283 | goto err; | |
1284 | ||
1285 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg4); | |
1286 | if (ret) | |
1287 | goto err; | |
1288 | ||
1289 | ret = msi3101_ctrl_msg(s, CMD_WREG, reg3); | |
1290 | if (ret) | |
1291 | goto err; | |
1292 | err: | |
1293 | return ret; | |
1294 | }; | |
1295 | ||
1296 | static int msi3101_set_tuner(struct msi3101_state *s) | |
1297 | { | |
23df427e AP |
1298 | int ret, i, len; |
1299 | unsigned int n, m, thresh, frac, vco_step, tmp; | |
1300 | u32 reg; | |
1301 | u64 f_vco; | |
977e444f AP |
1302 | u8 mode, lo_div; |
1303 | const struct msi3101_gain *gain_lut; | |
1304 | static const struct { | |
1305 | u32 rf; | |
1306 | u8 mode; | |
1307 | u8 lo_div; | |
1308 | } band_lut[] = { | |
093cdc79 | 1309 | { 47000000, 0x01, 16}, /* AM_MODE1 */ |
977e444f | 1310 | {108000000, 0x02, 32}, /* VHF_MODE */ |
093cdc79 | 1311 | {330000000, 0x04, 16}, /* B3_MODE */ |
977e444f | 1312 | {960000000, 0x08, 4}, /* B45_MODE */ |
093cdc79 | 1313 | { ~0U, 0x10, 2}, /* BL_MODE */ |
977e444f AP |
1314 | }; |
1315 | static const struct { | |
1316 | u32 freq; | |
1317 | u8 val; | |
1318 | } if_freq_lut[] = { | |
1319 | { 0, 0x03}, /* Zero IF */ | |
1320 | { 450000, 0x02}, /* 450 kHz IF */ | |
1321 | {1620000, 0x01}, /* 1.62 MHz IF */ | |
1322 | {2048000, 0x00}, /* 2.048 MHz IF */ | |
1323 | }; | |
1324 | static const struct { | |
1325 | u32 freq; | |
1326 | u8 val; | |
1327 | } bandwidth_lut[] = { | |
1328 | { 200000, 0x00}, /* 200 kHz */ | |
1329 | { 300000, 0x01}, /* 300 kHz */ | |
1330 | { 600000, 0x02}, /* 600 kHz */ | |
1331 | {1536000, 0x03}, /* 1.536 MHz */ | |
1332 | {5000000, 0x04}, /* 5 MHz */ | |
1333 | {6000000, 0x05}, /* 6 MHz */ | |
1334 | {7000000, 0x06}, /* 7 MHz */ | |
1335 | {8000000, 0x07}, /* 8 MHz */ | |
1336 | }; | |
1337 | ||
1338 | unsigned int rf_freq = s->ctrl_tuner_rf->val64; | |
1339 | ||
1340 | /* | |
1341 | * bandwidth (Hz) | |
1342 | * 200000, 300000, 600000, 1536000, 5000000, 6000000, 7000000, 8000000 | |
1343 | */ | |
1344 | int bandwidth = s->ctrl_tuner_bw->val; | |
1345 | ||
1346 | /* | |
1347 | * intermediate frequency (Hz) | |
1348 | * 0, 450000, 1620000, 2048000 | |
1349 | */ | |
1350 | int if_freq = s->ctrl_tuner_if->val; | |
1351 | ||
1352 | /* | |
1353 | * gain reduction (dB) | |
1354 | * 0 - 102 below 420 MHz | |
1355 | * 0 - 85 above 420 MHz | |
1356 | */ | |
1357 | int gain = s->ctrl_tuner_gain->val; | |
1358 | ||
1359 | dev_dbg(&s->udev->dev, | |
1360 | "%s: rf_freq=%d bandwidth=%d if_freq=%d gain=%d\n", | |
1361 | __func__, rf_freq, bandwidth, if_freq, gain); | |
1362 | ||
1363 | ret = -EINVAL; | |
1364 | ||
1365 | for (i = 0; i < ARRAY_SIZE(band_lut); i++) { | |
1366 | if (rf_freq <= band_lut[i].rf) { | |
1367 | mode = band_lut[i].mode; | |
1368 | lo_div = band_lut[i].lo_div; | |
1369 | break; | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | if (i == ARRAY_SIZE(band_lut)) | |
1374 | goto err; | |
1375 | ||
1376 | for (i = 0; i < ARRAY_SIZE(if_freq_lut); i++) { | |
1377 | if (if_freq == if_freq_lut[i].freq) { | |
1378 | if_freq = if_freq_lut[i].val; | |
1379 | break; | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | if (i == ARRAY_SIZE(if_freq_lut)) | |
1384 | goto err; | |
1385 | ||
1386 | for (i = 0; i < ARRAY_SIZE(bandwidth_lut); i++) { | |
1387 | if (bandwidth == bandwidth_lut[i].freq) { | |
1388 | bandwidth = bandwidth_lut[i].val; | |
1389 | break; | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | if (i == ARRAY_SIZE(bandwidth_lut)) | |
1394 | goto err; | |
1395 | ||
23df427e AP |
1396 | #define F_OUT_STEP 1 |
1397 | #define R_REF 4 | |
1398 | #define F_IF 0 | |
1399 | f_vco = (rf_freq + F_IF) * lo_div; | |
1400 | n = f_vco / (F_REF * R_REF); | |
1401 | m = f_vco % (F_REF * R_REF); | |
977e444f | 1402 | |
23df427e AP |
1403 | vco_step = F_OUT_STEP * lo_div; |
1404 | thresh = (F_REF * R_REF) / vco_step; | |
1405 | frac = 1ul * thresh * m / (F_REF * R_REF); | |
1406 | ||
1407 | /* Divide to reg max. After that RF resolution will be +-500Hz. */ | |
1408 | tmp = DIV_ROUND_UP(thresh, 4095); | |
1409 | thresh = DIV_ROUND_CLOSEST(thresh, tmp); | |
1410 | frac = DIV_ROUND_CLOSEST(frac, tmp); | |
1411 | ||
1412 | /* calc real RF set */ | |
1413 | tmp = 1ul * F_REF * R_REF * n; | |
1414 | tmp += 1ul * F_REF * R_REF * frac / thresh; | |
1415 | tmp /= lo_div; | |
1416 | ||
1417 | dev_dbg(&s->udev->dev, | |
1418 | "%s: rf=%u:%u n=%d thresh=%d frac=%d\n", | |
1419 | __func__, rf_freq, tmp, n, thresh, frac); | |
977e444f AP |
1420 | |
1421 | ret = msi3101_tuner_write(s, 0x00000e); | |
1422 | ret = msi3101_tuner_write(s, 0x000003); | |
1423 | ||
1424 | reg = 0 << 0; | |
1425 | reg |= mode << 4; | |
1426 | reg |= 1 << 10; | |
1427 | reg |= if_freq << 12; | |
1428 | reg |= bandwidth << 14; | |
1429 | reg |= 0x02 << 17; | |
1430 | reg |= 0x00 << 20; | |
1431 | ret = msi3101_tuner_write(s, reg); | |
1432 | if (ret) | |
1433 | goto err; | |
1434 | ||
1435 | reg = 5 << 0; | |
1436 | reg |= thresh << 4; | |
1437 | reg |= 1 << 19; | |
1438 | reg |= 1 << 21; | |
1439 | ret = msi3101_tuner_write(s, reg); | |
1440 | if (ret) | |
1441 | goto err; | |
1442 | ||
1443 | reg = 2 << 0; | |
1444 | reg |= frac << 4; | |
1445 | reg |= n << 16; | |
1446 | ret = msi3101_tuner_write(s, reg); | |
1447 | if (ret) | |
1448 | goto err; | |
1449 | ||
1450 | if (rf_freq < 120000000) { | |
1451 | gain_lut = msi3101_gain_lut_120; | |
1452 | len = ARRAY_SIZE(msi3101_gain_lut_120); | |
1453 | } else if (rf_freq < 245000000) { | |
1454 | gain_lut = msi3101_gain_lut_245; | |
1455 | len = ARRAY_SIZE(msi3101_gain_lut_120); | |
1456 | } else { | |
1457 | gain_lut = msi3101_gain_lut_1000; | |
1458 | len = ARRAY_SIZE(msi3101_gain_lut_1000); | |
1459 | } | |
1460 | ||
1461 | for (i = 0; i < len; i++) { | |
1462 | if (gain_lut[i].tot >= gain) | |
1463 | break; | |
1464 | } | |
1465 | ||
1466 | if (i == len) | |
1467 | goto err; | |
1468 | ||
1469 | dev_dbg(&s->udev->dev, | |
1470 | "%s: gain tot=%d baseband=%d lna=%d mixer=%d\n", | |
1471 | __func__, gain_lut[i].tot, gain_lut[i].baseband, | |
1472 | gain_lut[i].lna, gain_lut[i].mixer); | |
1473 | ||
1474 | reg = 1 << 0; | |
1475 | reg |= gain_lut[i].baseband << 4; | |
1476 | reg |= 0 << 10; | |
1477 | reg |= gain_lut[i].mixer << 12; | |
1478 | reg |= gain_lut[i].lna << 13; | |
1479 | reg |= 4 << 14; | |
1480 | reg |= 0 << 17; | |
1481 | ret = msi3101_tuner_write(s, reg); | |
1482 | if (ret) | |
1483 | goto err; | |
1484 | ||
1485 | reg = 6 << 0; | |
1486 | reg |= 63 << 4; | |
1487 | reg |= 4095 << 10; | |
1488 | ret = msi3101_tuner_write(s, reg); | |
1489 | if (ret) | |
1490 | goto err; | |
1491 | ||
1492 | return 0; | |
1493 | err: | |
1494 | dev_dbg(&s->udev->dev, "%s: failed %d\n", __func__, ret); | |
1495 | return ret; | |
1496 | }; | |
1497 | ||
1498 | static int msi3101_start_streaming(struct vb2_queue *vq, unsigned int count) | |
1499 | { | |
1500 | struct msi3101_state *s = vb2_get_drv_priv(vq); | |
1501 | int ret; | |
1502 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1503 | ||
1504 | if (!s->udev) | |
1505 | return -ENODEV; | |
1506 | ||
1507 | if (mutex_lock_interruptible(&s->v4l2_lock)) | |
1508 | return -ERESTARTSYS; | |
1509 | ||
1510 | ret = msi3101_set_usb_adc(s); | |
1511 | ||
1512 | ret = msi3101_isoc_init(s); | |
1513 | if (ret) | |
1514 | msi3101_cleanup_queued_bufs(s); | |
1515 | ||
1516 | ret = msi3101_ctrl_msg(s, CMD_START_STREAMING, 0); | |
1517 | ||
1518 | mutex_unlock(&s->v4l2_lock); | |
1519 | ||
1520 | return ret; | |
1521 | } | |
1522 | ||
1523 | static int msi3101_stop_streaming(struct vb2_queue *vq) | |
1524 | { | |
1525 | struct msi3101_state *s = vb2_get_drv_priv(vq); | |
1526 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1527 | ||
1528 | if (mutex_lock_interruptible(&s->v4l2_lock)) | |
1529 | return -ERESTARTSYS; | |
1530 | ||
977e444f AP |
1531 | if (s->udev) |
1532 | msi3101_isoc_cleanup(s); | |
1533 | ||
1534 | msi3101_cleanup_queued_bufs(s); | |
d0fadf40 AP |
1535 | |
1536 | /* according to tests, at least 700us delay is required */ | |
1537 | msleep(20); | |
1538 | msi3101_ctrl_msg(s, CMD_STOP_STREAMING, 0); | |
1539 | ||
977e444f AP |
1540 | mutex_unlock(&s->v4l2_lock); |
1541 | ||
1542 | return 0; | |
1543 | } | |
1544 | ||
1545 | static struct vb2_ops msi3101_vb2_ops = { | |
1546 | .queue_setup = msi3101_queue_setup, | |
1547 | .buf_init = msi3101_buf_init, | |
1548 | .buf_prepare = msi3101_buf_prepare, | |
1549 | .buf_finish = msi3101_buf_finish, | |
1550 | .buf_cleanup = msi3101_buf_cleanup, | |
1551 | .buf_queue = msi3101_buf_queue, | |
1552 | .start_streaming = msi3101_start_streaming, | |
1553 | .stop_streaming = msi3101_stop_streaming, | |
1554 | .wait_prepare = vb2_ops_wait_prepare, | |
1555 | .wait_finish = vb2_ops_wait_finish, | |
1556 | }; | |
1557 | ||
1558 | static int msi3101_enum_input(struct file *file, void *fh, struct v4l2_input *i) | |
1559 | { | |
1560 | if (i->index != 0) | |
1561 | return -EINVAL; | |
1562 | ||
1563 | strlcpy(i->name, "SDR data", sizeof(i->name)); | |
1564 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
1565 | ||
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | static int msi3101_g_input(struct file *file, void *fh, unsigned int *i) | |
1570 | { | |
1571 | *i = 0; | |
1572 | ||
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | static int msi3101_s_input(struct file *file, void *fh, unsigned int i) | |
1577 | { | |
1578 | return i ? -EINVAL : 0; | |
1579 | } | |
1580 | ||
1581 | static int vidioc_s_tuner(struct file *file, void *priv, | |
1582 | const struct v4l2_tuner *v) | |
1583 | { | |
1584 | struct msi3101_state *s = video_drvdata(file); | |
1585 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1586 | ||
1587 | return 0; | |
1588 | } | |
1589 | ||
1590 | static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *v) | |
1591 | { | |
1592 | struct msi3101_state *s = video_drvdata(file); | |
1593 | dev_dbg(&s->udev->dev, "%s:\n", __func__); | |
1594 | ||
1595 | strcpy(v->name, "SDR RX"); | |
1596 | v->capability = V4L2_TUNER_CAP_LOW; | |
1597 | ||
1598 | return 0; | |
1599 | } | |
1600 | ||
1601 | static int vidioc_s_frequency(struct file *file, void *priv, | |
1602 | const struct v4l2_frequency *f) | |
1603 | { | |
1604 | struct msi3101_state *s = video_drvdata(file); | |
35111f5b AP |
1605 | dev_dbg(&s->udev->dev, "%s: frequency=%lu Hz (%u)\n", |
1606 | __func__, f->frequency * 625UL / 10UL, f->frequency); | |
977e444f AP |
1607 | |
1608 | return v4l2_ctrl_s_ctrl_int64(s->ctrl_tuner_rf, | |
35111f5b | 1609 | f->frequency * 625UL / 10UL); |
977e444f AP |
1610 | } |
1611 | ||
1612 | const struct v4l2_ioctl_ops msi3101_ioctl_ops = { | |
1613 | .vidioc_querycap = msi3101_querycap, | |
1614 | ||
1615 | .vidioc_enum_input = msi3101_enum_input, | |
1616 | .vidioc_g_input = msi3101_g_input, | |
1617 | .vidioc_s_input = msi3101_s_input, | |
1618 | ||
1619 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1620 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1621 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
1622 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1623 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1624 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1625 | ||
1626 | .vidioc_streamon = vb2_ioctl_streamon, | |
1627 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1628 | ||
1629 | .vidioc_g_tuner = vidioc_g_tuner, | |
1630 | .vidioc_s_tuner = vidioc_s_tuner, | |
1631 | .vidioc_s_frequency = vidioc_s_frequency, | |
1632 | ||
1633 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
1634 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
1635 | .vidioc_log_status = v4l2_ctrl_log_status, | |
1636 | }; | |
1637 | ||
1638 | static const struct v4l2_file_operations msi3101_fops = { | |
1639 | .owner = THIS_MODULE, | |
1640 | .open = v4l2_fh_open, | |
1641 | .release = vb2_fop_release, | |
1642 | .read = vb2_fop_read, | |
1643 | .poll = vb2_fop_poll, | |
1644 | .mmap = vb2_fop_mmap, | |
1645 | .unlocked_ioctl = video_ioctl2, | |
1646 | }; | |
1647 | ||
1648 | static struct video_device msi3101_template = { | |
1649 | .name = "Mirics MSi3101 SDR Dongle", | |
1650 | .release = video_device_release_empty, | |
1651 | .fops = &msi3101_fops, | |
1652 | .ioctl_ops = &msi3101_ioctl_ops, | |
1653 | }; | |
1654 | ||
1655 | static int msi3101_s_ctrl(struct v4l2_ctrl *ctrl) | |
1656 | { | |
1657 | struct msi3101_state *s = | |
1658 | container_of(ctrl->handler, struct msi3101_state, | |
1659 | ctrl_handler); | |
1660 | int ret; | |
1661 | dev_dbg(&s->udev->dev, | |
1662 | "%s: id=%d name=%s val=%d min=%d max=%d step=%d\n", | |
1663 | __func__, ctrl->id, ctrl->name, ctrl->val, | |
1664 | ctrl->minimum, ctrl->maximum, ctrl->step); | |
1665 | ||
1666 | switch (ctrl->id) { | |
3673b35c | 1667 | case MSI3101_CID_SAMPLING_MODE: |
977e444f AP |
1668 | case MSI3101_CID_SAMPLING_RATE: |
1669 | case MSI3101_CID_SAMPLING_RESOLUTION: | |
1670 | ret = 0; | |
1671 | break; | |
1672 | case MSI3101_CID_TUNER_RF: | |
1673 | case MSI3101_CID_TUNER_BW: | |
1674 | case MSI3101_CID_TUNER_IF: | |
1675 | case MSI3101_CID_TUNER_GAIN: | |
1676 | ret = msi3101_set_tuner(s); | |
1677 | break; | |
1678 | default: | |
1679 | ret = -EINVAL; | |
1680 | } | |
1681 | ||
1682 | return ret; | |
1683 | } | |
1684 | ||
1685 | static const struct v4l2_ctrl_ops msi3101_ctrl_ops = { | |
1686 | .s_ctrl = msi3101_s_ctrl, | |
1687 | }; | |
1688 | ||
1689 | static void msi3101_video_release(struct v4l2_device *v) | |
1690 | { | |
1691 | struct msi3101_state *s = | |
1692 | container_of(v, struct msi3101_state, v4l2_dev); | |
1693 | ||
1694 | v4l2_ctrl_handler_free(&s->ctrl_handler); | |
1695 | v4l2_device_unregister(&s->v4l2_dev); | |
1696 | kfree(s); | |
1697 | } | |
1698 | ||
1699 | static int msi3101_probe(struct usb_interface *intf, | |
1700 | const struct usb_device_id *id) | |
1701 | { | |
1702 | struct usb_device *udev = interface_to_usbdev(intf); | |
1703 | struct msi3101_state *s = NULL; | |
1704 | int ret; | |
3673b35c AP |
1705 | static const char * const ctrl_sampling_mode_qmenu_strings[] = { |
1706 | "Quadrature Sampling", | |
1707 | NULL, | |
1708 | }; | |
1709 | static const struct v4l2_ctrl_config ctrl_sampling_mode = { | |
1710 | .ops = &msi3101_ctrl_ops, | |
1711 | .id = MSI3101_CID_SAMPLING_MODE, | |
1712 | .type = V4L2_CTRL_TYPE_MENU, | |
1713 | .flags = V4L2_CTRL_FLAG_INACTIVE, | |
1714 | .name = "Sampling Mode", | |
1715 | .qmenu = ctrl_sampling_mode_qmenu_strings, | |
1716 | }; | |
977e444f AP |
1717 | static const struct v4l2_ctrl_config ctrl_sampling_rate = { |
1718 | .ops = &msi3101_ctrl_ops, | |
1719 | .id = MSI3101_CID_SAMPLING_RATE, | |
1720 | .type = V4L2_CTRL_TYPE_INTEGER64, | |
1721 | .name = "Sampling Rate", | |
1722 | .min = 500000, | |
1723 | .max = 12000000, | |
1724 | .def = 2048000, | |
1725 | .step = 1, | |
1726 | }; | |
1727 | static const struct v4l2_ctrl_config ctrl_sampling_resolution = { | |
1728 | .ops = &msi3101_ctrl_ops, | |
1729 | .id = MSI3101_CID_SAMPLING_RESOLUTION, | |
1730 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1731 | .flags = V4L2_CTRL_FLAG_INACTIVE, | |
1732 | .name = "Sampling Resolution", | |
1733 | .min = 10, | |
1734 | .max = 10, | |
1735 | .def = 10, | |
1736 | .step = 1, | |
1737 | }; | |
1738 | static const struct v4l2_ctrl_config ctrl_tuner_rf = { | |
1739 | .ops = &msi3101_ctrl_ops, | |
1740 | .id = MSI3101_CID_TUNER_RF, | |
1741 | .type = V4L2_CTRL_TYPE_INTEGER64, | |
1742 | .name = "Tuner RF", | |
1743 | .min = 40000000, | |
1744 | .max = 2000000000, | |
1745 | .def = 100000000, | |
1746 | .step = 1, | |
1747 | }; | |
1748 | static const struct v4l2_ctrl_config ctrl_tuner_bw = { | |
1749 | .ops = &msi3101_ctrl_ops, | |
1750 | .id = MSI3101_CID_TUNER_BW, | |
1751 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1752 | .name = "Tuner BW", | |
1753 | .min = 200000, | |
1754 | .max = 8000000, | |
1755 | .def = 600000, | |
1756 | .step = 1, | |
1757 | }; | |
1758 | static const struct v4l2_ctrl_config ctrl_tuner_if = { | |
1759 | .ops = &msi3101_ctrl_ops, | |
1760 | .id = MSI3101_CID_TUNER_IF, | |
1761 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1762 | .flags = V4L2_CTRL_FLAG_INACTIVE, | |
1763 | .name = "Tuner IF", | |
1764 | .min = 0, | |
1765 | .max = 2048000, | |
1766 | .def = 0, | |
1767 | .step = 1, | |
1768 | }; | |
1769 | static const struct v4l2_ctrl_config ctrl_tuner_gain = { | |
1770 | .ops = &msi3101_ctrl_ops, | |
1771 | .id = MSI3101_CID_TUNER_GAIN, | |
1772 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1773 | .name = "Tuner Gain", | |
1774 | .min = 0, | |
1775 | .max = 102, | |
1776 | .def = 0, | |
1777 | .step = 1, | |
1778 | }; | |
1779 | ||
1780 | s = kzalloc(sizeof(struct msi3101_state), GFP_KERNEL); | |
1781 | if (s == NULL) { | |
1782 | pr_err("Could not allocate memory for msi3101_state\n"); | |
1783 | return -ENOMEM; | |
1784 | } | |
1785 | ||
1786 | mutex_init(&s->v4l2_lock); | |
1787 | mutex_init(&s->vb_queue_lock); | |
1788 | spin_lock_init(&s->queued_bufs_lock); | |
1789 | INIT_LIST_HEAD(&s->queued_bufs); | |
1790 | ||
1791 | s->udev = udev; | |
1792 | ||
1793 | /* Init videobuf2 queue structure */ | |
1794 | s->vb_queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1795 | s->vb_queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; | |
1796 | s->vb_queue.drv_priv = s; | |
1797 | s->vb_queue.buf_struct_size = sizeof(struct msi3101_frame_buf); | |
1798 | s->vb_queue.ops = &msi3101_vb2_ops; | |
1799 | s->vb_queue.mem_ops = &vb2_vmalloc_memops; | |
1800 | s->vb_queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1801 | ret = vb2_queue_init(&s->vb_queue); | |
1802 | if (ret < 0) { | |
1803 | dev_err(&s->udev->dev, "Could not initialize vb2 queue\n"); | |
1804 | goto err_free_mem; | |
1805 | } | |
1806 | ||
1807 | /* Init video_device structure */ | |
1808 | s->vdev = msi3101_template; | |
1809 | s->vdev.queue = &s->vb_queue; | |
1810 | s->vdev.queue->lock = &s->vb_queue_lock; | |
1811 | set_bit(V4L2_FL_USE_FH_PRIO, &s->vdev.flags); | |
1812 | video_set_drvdata(&s->vdev, s); | |
1813 | ||
1814 | /* Register controls */ | |
3673b35c AP |
1815 | v4l2_ctrl_handler_init(&s->ctrl_handler, 7); |
1816 | v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_mode, NULL); | |
977e444f AP |
1817 | s->ctrl_sampling_rate = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_rate, NULL); |
1818 | v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_sampling_resolution, NULL); | |
1819 | s->ctrl_tuner_rf = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_rf, NULL); | |
1820 | s->ctrl_tuner_bw = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_bw, NULL); | |
1821 | s->ctrl_tuner_if = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_if, NULL); | |
1822 | s->ctrl_tuner_gain = v4l2_ctrl_new_custom(&s->ctrl_handler, &ctrl_tuner_gain, NULL); | |
1823 | if (s->ctrl_handler.error) { | |
1824 | ret = s->ctrl_handler.error; | |
1825 | dev_err(&s->udev->dev, "Could not initialize controls\n"); | |
1826 | goto err_free_controls; | |
1827 | } | |
1828 | ||
1829 | /* Register the v4l2_device structure */ | |
1830 | s->v4l2_dev.release = msi3101_video_release; | |
1831 | ret = v4l2_device_register(&intf->dev, &s->v4l2_dev); | |
1832 | if (ret) { | |
1833 | dev_err(&s->udev->dev, | |
1834 | "Failed to register v4l2-device (%d)\n", ret); | |
1835 | goto err_free_controls; | |
1836 | } | |
1837 | ||
1838 | s->v4l2_dev.ctrl_handler = &s->ctrl_handler; | |
1839 | s->vdev.v4l2_dev = &s->v4l2_dev; | |
1840 | s->vdev.lock = &s->v4l2_lock; | |
1841 | ||
1842 | ret = video_register_device(&s->vdev, VFL_TYPE_GRABBER, -1); | |
1843 | if (ret < 0) { | |
1844 | dev_err(&s->udev->dev, | |
1845 | "Failed to register as video device (%d)\n", | |
1846 | ret); | |
1847 | goto err_unregister_v4l2_dev; | |
1848 | } | |
1849 | dev_info(&s->udev->dev, "Registered as %s\n", | |
1850 | video_device_node_name(&s->vdev)); | |
1851 | ||
1852 | return 0; | |
1853 | ||
1854 | err_unregister_v4l2_dev: | |
1855 | v4l2_device_unregister(&s->v4l2_dev); | |
1856 | err_free_controls: | |
1857 | v4l2_ctrl_handler_free(&s->ctrl_handler); | |
1858 | err_free_mem: | |
1859 | kfree(s); | |
1860 | return ret; | |
1861 | } | |
1862 | ||
1863 | /* USB device ID list */ | |
1864 | static struct usb_device_id msi3101_id_table[] = { | |
1865 | { USB_DEVICE(0x1df7, 0x2500) }, | |
42fc5b42 | 1866 | { USB_DEVICE(0x2040, 0xd300) }, /* Hauppauge WinTV 133559 LF */ |
977e444f AP |
1867 | { } |
1868 | }; | |
1869 | MODULE_DEVICE_TABLE(usb, msi3101_id_table); | |
1870 | ||
1871 | /* USB subsystem interface */ | |
1872 | static struct usb_driver msi3101_driver = { | |
1873 | .name = KBUILD_MODNAME, | |
1874 | .probe = msi3101_probe, | |
1875 | .disconnect = msi3101_disconnect, | |
1876 | .id_table = msi3101_id_table, | |
1877 | }; | |
1878 | ||
1879 | module_usb_driver(msi3101_driver); | |
1880 | ||
1881 | MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>"); | |
1882 | MODULE_DESCRIPTION("Mirics MSi3101 SDR Dongle"); | |
1883 | MODULE_LICENSE("GPL"); |