]>
Commit | Line | Data |
---|---|---|
f3d27f34 LR |
1 | /* |
2 | * Fushicai USBTV007 Video Grabber Driver | |
3 | * | |
4 | * Product web site: | |
5 | * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html | |
6 | * | |
7 | * Following LWN articles were very useful in construction of this driver: | |
8 | * Video4Linux2 API series: http://lwn.net/Articles/203924/ | |
9 | * videobuf2 API explanation: http://lwn.net/Articles/447435/ | |
10 | * Thanks go to Jonathan Corbet for providing this quality documentation. | |
11 | * He is awesome. | |
12 | * | |
13 | * Copyright (c) 2013 Lubomir Rintel | |
14 | * All rights reserved. | |
15 | * No physical hardware was harmed running Windows during the | |
16 | * reverse-engineering activity | |
17 | * | |
18 | * Redistribution and use in source and binary forms, with or without | |
19 | * modification, are permitted provided that the following conditions | |
20 | * are met: | |
21 | * 1. Redistributions of source code must retain the above copyright | |
22 | * notice, this list of conditions, and the following disclaimer, | |
23 | * without modification. | |
24 | * 2. The name of the author may not be used to endorse or promote products | |
25 | * derived from this software without specific prior written permission. | |
26 | * | |
27 | * Alternatively, this software may be distributed under the terms of the | |
28 | * GNU General Public License ("GPL"). | |
29 | */ | |
30 | ||
31 | #include <linux/init.h> | |
32 | #include <linux/list.h> | |
33 | #include <linux/module.h> | |
34 | #include <linux/slab.h> | |
35 | #include <linux/usb.h> | |
f3d27f34 LR |
36 | #include <linux/videodev2.h> |
37 | ||
38 | #include <media/v4l2-device.h> | |
39 | #include <media/v4l2-ioctl.h> | |
40 | #include <media/videobuf2-core.h> | |
41 | #include <media/videobuf2-vmalloc.h> | |
42 | ||
43 | /* Hardware. */ | |
44 | #define USBTV_VIDEO_ENDP 0x81 | |
45 | #define USBTV_BASE 0xc000 | |
46 | #define USBTV_REQUEST_REG 12 | |
47 | ||
48 | /* Number of concurrent isochronous urbs submitted. | |
49 | * Higher numbers was seen to overly saturate the USB bus. */ | |
50 | #define USBTV_ISOC_TRANSFERS 16 | |
51 | #define USBTV_ISOC_PACKETS 8 | |
52 | ||
f3d27f34 LR |
53 | #define USBTV_CHUNK_SIZE 256 |
54 | #define USBTV_CHUNK 240 | |
f3d27f34 LR |
55 | |
56 | /* Chunk header. */ | |
57 | #define USBTV_MAGIC_OK(chunk) ((be32_to_cpu(chunk[0]) & 0xff000000) \ | |
58 | == 0x88000000) | |
59 | #define USBTV_FRAME_ID(chunk) ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16) | |
60 | #define USBTV_ODD(chunk) ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15) | |
61 | #define USBTV_CHUNK_NO(chunk) (be32_to_cpu(chunk[0]) & 0x00000fff) | |
62 | ||
0e0fe395 GK |
63 | #define USBTV_TV_STD (V4L2_STD_525_60 | V4L2_STD_PAL) |
64 | ||
65 | /* parameters for supported TV norms */ | |
66 | struct usbtv_norm_params { | |
67 | v4l2_std_id norm; | |
68 | int cap_width, cap_height; | |
69 | }; | |
70 | ||
71 | static struct usbtv_norm_params norm_params[] = { | |
72 | { | |
73 | .norm = V4L2_STD_525_60, | |
74 | .cap_width = 720, | |
75 | .cap_height = 480, | |
76 | }, | |
77 | { | |
78 | .norm = V4L2_STD_PAL, | |
79 | .cap_width = 720, | |
80 | .cap_height = 576, | |
81 | } | |
82 | }; | |
83 | ||
f3d27f34 LR |
84 | /* A single videobuf2 frame buffer. */ |
85 | struct usbtv_buf { | |
86 | struct vb2_buffer vb; | |
87 | struct list_head list; | |
88 | }; | |
89 | ||
90 | /* Per-device structure. */ | |
91 | struct usbtv { | |
92 | struct device *dev; | |
93 | struct usb_device *udev; | |
94 | struct v4l2_device v4l2_dev; | |
95 | struct video_device vdev; | |
96 | struct vb2_queue vb2q; | |
97 | struct mutex v4l2_lock; | |
98 | struct mutex vb2q_lock; | |
99 | ||
100 | /* List of videobuf2 buffers protected by a lock. */ | |
101 | spinlock_t buflock; | |
102 | struct list_head bufs; | |
103 | ||
104 | /* Number of currently processed frame, useful find | |
105 | * out when a new one begins. */ | |
106 | u32 frame_id; | |
6ee0faa5 | 107 | int chunks_done; |
f3d27f34 | 108 | |
2206112b LR |
109 | enum { |
110 | USBTV_COMPOSITE_INPUT, | |
111 | USBTV_SVIDEO_INPUT, | |
112 | } input; | |
0e0fe395 GK |
113 | v4l2_std_id norm; |
114 | int width, height; | |
115 | int n_chunks; | |
f3d27f34 LR |
116 | int iso_size; |
117 | unsigned int sequence; | |
118 | struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS]; | |
119 | }; | |
120 | ||
0e0fe395 GK |
121 | static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm) |
122 | { | |
123 | int i, ret = 0; | |
124 | struct usbtv_norm_params *params = NULL; | |
125 | ||
126 | for (i = 0; i < ARRAY_SIZE(norm_params); i++) { | |
127 | if (norm_params[i].norm & norm) { | |
128 | params = &norm_params[i]; | |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | if (params) { | |
134 | usbtv->width = params->cap_width; | |
135 | usbtv->height = params->cap_height; | |
136 | usbtv->n_chunks = usbtv->width * usbtv->height | |
137 | / 4 / USBTV_CHUNK; | |
138 | usbtv->norm = params->norm; | |
139 | } else | |
140 | ret = -EINVAL; | |
141 | ||
142 | return ret; | |
143 | } | |
144 | ||
2206112b | 145 | static int usbtv_set_regs(struct usbtv *usbtv, const u16 regs[][2], int size) |
f3d27f34 LR |
146 | { |
147 | int ret; | |
148 | int pipe = usb_rcvctrlpipe(usbtv->udev, 0); | |
149 | int i; | |
2206112b LR |
150 | |
151 | for (i = 0; i < size; i++) { | |
152 | u16 index = regs[i][0]; | |
153 | u16 value = regs[i][1]; | |
154 | ||
155 | ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG, | |
156 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
157 | value, index, NULL, 0, 0); | |
158 | if (ret < 0) | |
159 | return ret; | |
160 | } | |
161 | ||
162 | return 0; | |
163 | } | |
164 | ||
165 | static int usbtv_select_input(struct usbtv *usbtv, int input) | |
166 | { | |
167 | int ret; | |
168 | ||
169 | static const u16 composite[][2] = { | |
170 | { USBTV_BASE + 0x0105, 0x0060 }, | |
171 | { USBTV_BASE + 0x011f, 0x00f2 }, | |
172 | { USBTV_BASE + 0x0127, 0x0060 }, | |
173 | { USBTV_BASE + 0x00ae, 0x0010 }, | |
174 | { USBTV_BASE + 0x0284, 0x00aa }, | |
175 | { USBTV_BASE + 0x0239, 0x0060 }, | |
176 | }; | |
177 | ||
178 | static const u16 svideo[][2] = { | |
179 | { USBTV_BASE + 0x0105, 0x0010 }, | |
180 | { USBTV_BASE + 0x011f, 0x00ff }, | |
181 | { USBTV_BASE + 0x0127, 0x0060 }, | |
182 | { USBTV_BASE + 0x00ae, 0x0030 }, | |
183 | { USBTV_BASE + 0x0284, 0x0088 }, | |
184 | { USBTV_BASE + 0x0239, 0x0060 }, | |
185 | }; | |
186 | ||
187 | switch (input) { | |
188 | case USBTV_COMPOSITE_INPUT: | |
189 | ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite)); | |
190 | break; | |
191 | case USBTV_SVIDEO_INPUT: | |
192 | ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo)); | |
193 | break; | |
194 | default: | |
195 | ret = -EINVAL; | |
196 | } | |
197 | ||
198 | if (!ret) | |
199 | usbtv->input = input; | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
0e0fe395 GK |
204 | static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm) |
205 | { | |
206 | int ret; | |
207 | static const u16 pal[][2] = { | |
208 | { USBTV_BASE + 0x001a, 0x0068 }, | |
209 | { USBTV_BASE + 0x010e, 0x0072 }, | |
210 | { USBTV_BASE + 0x010f, 0x00a2 }, | |
211 | { USBTV_BASE + 0x0112, 0x00b0 }, | |
212 | { USBTV_BASE + 0x0117, 0x0001 }, | |
213 | { USBTV_BASE + 0x0118, 0x002c }, | |
214 | { USBTV_BASE + 0x012d, 0x0010 }, | |
215 | { USBTV_BASE + 0x012f, 0x0020 }, | |
216 | { USBTV_BASE + 0x024f, 0x0002 }, | |
217 | { USBTV_BASE + 0x0254, 0x0059 }, | |
218 | { USBTV_BASE + 0x025a, 0x0016 }, | |
219 | { USBTV_BASE + 0x025b, 0x0035 }, | |
220 | { USBTV_BASE + 0x0263, 0x0017 }, | |
221 | { USBTV_BASE + 0x0266, 0x0016 }, | |
222 | { USBTV_BASE + 0x0267, 0x0036 } | |
223 | }; | |
224 | ||
225 | static const u16 ntsc[][2] = { | |
226 | { USBTV_BASE + 0x001a, 0x0079 }, | |
227 | { USBTV_BASE + 0x010e, 0x0068 }, | |
228 | { USBTV_BASE + 0x010f, 0x009c }, | |
229 | { USBTV_BASE + 0x0112, 0x00f0 }, | |
230 | { USBTV_BASE + 0x0117, 0x0000 }, | |
231 | { USBTV_BASE + 0x0118, 0x00fc }, | |
232 | { USBTV_BASE + 0x012d, 0x0004 }, | |
233 | { USBTV_BASE + 0x012f, 0x0008 }, | |
234 | { USBTV_BASE + 0x024f, 0x0001 }, | |
235 | { USBTV_BASE + 0x0254, 0x005f }, | |
236 | { USBTV_BASE + 0x025a, 0x0012 }, | |
237 | { USBTV_BASE + 0x025b, 0x0001 }, | |
238 | { USBTV_BASE + 0x0263, 0x001c }, | |
239 | { USBTV_BASE + 0x0266, 0x0011 }, | |
240 | { USBTV_BASE + 0x0267, 0x0005 } | |
241 | }; | |
242 | ||
243 | ret = usbtv_configure_for_norm(usbtv, norm); | |
244 | ||
245 | if (!ret) { | |
246 | if (norm & V4L2_STD_525_60) | |
247 | ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc)); | |
248 | else if (norm & V4L2_STD_PAL) | |
249 | ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal)); | |
250 | } | |
251 | ||
252 | return ret; | |
253 | } | |
254 | ||
2206112b LR |
255 | static int usbtv_setup_capture(struct usbtv *usbtv) |
256 | { | |
257 | int ret; | |
258 | static const u16 setup[][2] = { | |
f3d27f34 LR |
259 | /* These seem to enable the device. */ |
260 | { USBTV_BASE + 0x0008, 0x0001 }, | |
261 | { USBTV_BASE + 0x01d0, 0x00ff }, | |
262 | { USBTV_BASE + 0x01d9, 0x0002 }, | |
263 | ||
264 | /* These seem to influence color parameters, such as | |
265 | * brightness, etc. */ | |
266 | { USBTV_BASE + 0x0239, 0x0040 }, | |
267 | { USBTV_BASE + 0x0240, 0x0000 }, | |
268 | { USBTV_BASE + 0x0241, 0x0000 }, | |
269 | { USBTV_BASE + 0x0242, 0x0002 }, | |
270 | { USBTV_BASE + 0x0243, 0x0080 }, | |
271 | { USBTV_BASE + 0x0244, 0x0012 }, | |
272 | { USBTV_BASE + 0x0245, 0x0090 }, | |
273 | { USBTV_BASE + 0x0246, 0x0000 }, | |
274 | ||
275 | { USBTV_BASE + 0x0278, 0x002d }, | |
276 | { USBTV_BASE + 0x0279, 0x000a }, | |
277 | { USBTV_BASE + 0x027a, 0x0032 }, | |
278 | { 0xf890, 0x000c }, | |
279 | { 0xf894, 0x0086 }, | |
280 | ||
281 | { USBTV_BASE + 0x00ac, 0x00c0 }, | |
282 | { USBTV_BASE + 0x00ad, 0x0000 }, | |
283 | { USBTV_BASE + 0x00a2, 0x0012 }, | |
284 | { USBTV_BASE + 0x00a3, 0x00e0 }, | |
285 | { USBTV_BASE + 0x00a4, 0x0028 }, | |
286 | { USBTV_BASE + 0x00a5, 0x0082 }, | |
287 | { USBTV_BASE + 0x00a7, 0x0080 }, | |
288 | { USBTV_BASE + 0x0000, 0x0014 }, | |
289 | { USBTV_BASE + 0x0006, 0x0003 }, | |
290 | { USBTV_BASE + 0x0090, 0x0099 }, | |
291 | { USBTV_BASE + 0x0091, 0x0090 }, | |
292 | { USBTV_BASE + 0x0094, 0x0068 }, | |
293 | { USBTV_BASE + 0x0095, 0x0070 }, | |
294 | { USBTV_BASE + 0x009c, 0x0030 }, | |
295 | { USBTV_BASE + 0x009d, 0x00c0 }, | |
296 | { USBTV_BASE + 0x009e, 0x00e0 }, | |
297 | { USBTV_BASE + 0x0019, 0x0006 }, | |
298 | { USBTV_BASE + 0x008c, 0x00ba }, | |
299 | { USBTV_BASE + 0x0101, 0x00ff }, | |
300 | { USBTV_BASE + 0x010c, 0x00b3 }, | |
301 | { USBTV_BASE + 0x01b2, 0x0080 }, | |
302 | { USBTV_BASE + 0x01b4, 0x00a0 }, | |
303 | { USBTV_BASE + 0x014c, 0x00ff }, | |
304 | { USBTV_BASE + 0x014d, 0x00ca }, | |
305 | { USBTV_BASE + 0x0113, 0x0053 }, | |
306 | { USBTV_BASE + 0x0119, 0x008a }, | |
307 | { USBTV_BASE + 0x013c, 0x0003 }, | |
308 | { USBTV_BASE + 0x0150, 0x009c }, | |
309 | { USBTV_BASE + 0x0151, 0x0071 }, | |
310 | { USBTV_BASE + 0x0152, 0x00c6 }, | |
311 | { USBTV_BASE + 0x0153, 0x0084 }, | |
312 | { USBTV_BASE + 0x0154, 0x00bc }, | |
313 | { USBTV_BASE + 0x0155, 0x00a0 }, | |
314 | { USBTV_BASE + 0x0156, 0x00a0 }, | |
315 | { USBTV_BASE + 0x0157, 0x009c }, | |
316 | { USBTV_BASE + 0x0158, 0x001f }, | |
317 | { USBTV_BASE + 0x0159, 0x0006 }, | |
318 | { USBTV_BASE + 0x015d, 0x0000 }, | |
319 | ||
320 | { USBTV_BASE + 0x0284, 0x0088 }, | |
321 | { USBTV_BASE + 0x0003, 0x0004 }, | |
f3d27f34 | 322 | { USBTV_BASE + 0x0100, 0x00d3 }, |
f3d27f34 | 323 | { USBTV_BASE + 0x0115, 0x0015 }, |
f3d27f34 LR |
324 | { USBTV_BASE + 0x0220, 0x002e }, |
325 | { USBTV_BASE + 0x0225, 0x0008 }, | |
326 | { USBTV_BASE + 0x024e, 0x0002 }, | |
f3d27f34 LR |
327 | { USBTV_BASE + 0x024e, 0x0002 }, |
328 | { USBTV_BASE + 0x024f, 0x0002 }, | |
329 | }; | |
330 | ||
2206112b LR |
331 | ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup)); |
332 | if (ret) | |
333 | return ret; | |
f3d27f34 | 334 | |
0e0fe395 GK |
335 | ret = usbtv_select_norm(usbtv, usbtv->norm); |
336 | if (ret) | |
337 | return ret; | |
338 | ||
2206112b LR |
339 | ret = usbtv_select_input(usbtv, usbtv->input); |
340 | if (ret) | |
341 | return ret; | |
f3d27f34 LR |
342 | |
343 | return 0; | |
344 | } | |
345 | ||
30800724 LR |
346 | /* Copy data from chunk into a frame buffer, deinterlacing the data |
347 | * into every second line. Unfortunately, they don't align nicely into | |
348 | * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels. | |
349 | * Therefore, we break down the chunk into two halves before copyting, | |
350 | * so that we can interleave a line if needed. */ | |
351 | static void usbtv_chunk_to_vbuf(u32 *frame, u32 *src, int chunk_no, int odd) | |
352 | { | |
353 | int half; | |
354 | ||
355 | for (half = 0; half < 2; half++) { | |
356 | int part_no = chunk_no * 2 + half; | |
357 | int line = part_no / 3; | |
358 | int part_index = (line * 2 + !odd) * 3 + (part_no % 3); | |
359 | ||
360 | u32 *dst = &frame[part_index * USBTV_CHUNK/2]; | |
361 | memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src)); | |
362 | src += USBTV_CHUNK/2; | |
363 | } | |
364 | } | |
365 | ||
f3d27f34 LR |
366 | /* Called for each 256-byte image chunk. |
367 | * First word identifies the chunk, followed by 240 words of image | |
368 | * data and padding. */ | |
369 | static void usbtv_image_chunk(struct usbtv *usbtv, u32 *chunk) | |
370 | { | |
371 | int frame_id, odd, chunk_no; | |
372 | u32 *frame; | |
373 | struct usbtv_buf *buf; | |
374 | unsigned long flags; | |
375 | ||
376 | /* Ignore corrupted lines. */ | |
377 | if (!USBTV_MAGIC_OK(chunk)) | |
378 | return; | |
379 | frame_id = USBTV_FRAME_ID(chunk); | |
380 | odd = USBTV_ODD(chunk); | |
381 | chunk_no = USBTV_CHUNK_NO(chunk); | |
0e0fe395 | 382 | if (chunk_no >= usbtv->n_chunks) |
f3d27f34 LR |
383 | return; |
384 | ||
385 | /* Beginning of a frame. */ | |
6ee0faa5 | 386 | if (chunk_no == 0) { |
f3d27f34 | 387 | usbtv->frame_id = frame_id; |
6ee0faa5 LR |
388 | usbtv->chunks_done = 0; |
389 | } | |
390 | ||
391 | if (usbtv->frame_id != frame_id) | |
392 | return; | |
f3d27f34 LR |
393 | |
394 | spin_lock_irqsave(&usbtv->buflock, flags); | |
395 | if (list_empty(&usbtv->bufs)) { | |
396 | /* No free buffers. Userspace likely too slow. */ | |
397 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
398 | return; | |
399 | } | |
400 | ||
401 | /* First available buffer. */ | |
402 | buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list); | |
403 | frame = vb2_plane_vaddr(&buf->vb, 0); | |
404 | ||
30800724 LR |
405 | /* Copy the chunk data. */ |
406 | usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd); | |
6ee0faa5 | 407 | usbtv->chunks_done++; |
f3d27f34 LR |
408 | |
409 | /* Last chunk in a frame, signalling an end */ | |
0e0fe395 | 410 | if (odd && chunk_no == usbtv->n_chunks-1) { |
f3d27f34 | 411 | int size = vb2_plane_size(&buf->vb, 0); |
6ee0faa5 | 412 | enum vb2_buffer_state state = usbtv->chunks_done == |
0e0fe395 | 413 | usbtv->n_chunks ? |
6ee0faa5 LR |
414 | VB2_BUF_STATE_DONE : |
415 | VB2_BUF_STATE_ERROR; | |
f3d27f34 LR |
416 | |
417 | buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED; | |
418 | buf->vb.v4l2_buf.sequence = usbtv->sequence++; | |
419 | v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp); | |
420 | vb2_set_plane_payload(&buf->vb, 0, size); | |
6ee0faa5 | 421 | vb2_buffer_done(&buf->vb, state); |
f3d27f34 LR |
422 | list_del(&buf->list); |
423 | } | |
424 | ||
425 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
426 | } | |
427 | ||
428 | /* Got image data. Each packet contains a number of 256-word chunks we | |
429 | * compose the image from. */ | |
430 | static void usbtv_iso_cb(struct urb *ip) | |
431 | { | |
432 | int ret; | |
433 | int i; | |
434 | struct usbtv *usbtv = (struct usbtv *)ip->context; | |
435 | ||
436 | switch (ip->status) { | |
437 | /* All fine. */ | |
438 | case 0: | |
439 | break; | |
440 | /* Device disconnected or capture stopped? */ | |
441 | case -ENODEV: | |
442 | case -ENOENT: | |
443 | case -ECONNRESET: | |
444 | case -ESHUTDOWN: | |
445 | return; | |
446 | /* Unknown error. Retry. */ | |
447 | default: | |
448 | dev_warn(usbtv->dev, "Bad response for ISO request.\n"); | |
449 | goto resubmit; | |
450 | } | |
451 | ||
452 | for (i = 0; i < ip->number_of_packets; i++) { | |
453 | int size = ip->iso_frame_desc[i].actual_length; | |
454 | unsigned char *data = ip->transfer_buffer + | |
455 | ip->iso_frame_desc[i].offset; | |
456 | int offset; | |
457 | ||
458 | for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++) | |
459 | usbtv_image_chunk(usbtv, | |
460 | (u32 *)&data[USBTV_CHUNK_SIZE * offset]); | |
461 | } | |
462 | ||
463 | resubmit: | |
464 | ret = usb_submit_urb(ip, GFP_ATOMIC); | |
465 | if (ret < 0) | |
466 | dev_warn(usbtv->dev, "Could not resubmit ISO URB\n"); | |
467 | } | |
468 | ||
469 | static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv) | |
470 | { | |
471 | struct urb *ip; | |
472 | int size = usbtv->iso_size; | |
473 | int i; | |
474 | ||
475 | ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL); | |
476 | if (ip == NULL) | |
477 | return NULL; | |
478 | ||
479 | ip->dev = usbtv->udev; | |
480 | ip->context = usbtv; | |
481 | ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP); | |
482 | ip->interval = 1; | |
483 | ip->transfer_flags = URB_ISO_ASAP; | |
484 | ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS, | |
485 | GFP_KERNEL); | |
486 | ip->complete = usbtv_iso_cb; | |
487 | ip->number_of_packets = USBTV_ISOC_PACKETS; | |
488 | ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS; | |
489 | for (i = 0; i < USBTV_ISOC_PACKETS; i++) { | |
490 | ip->iso_frame_desc[i].offset = size * i; | |
491 | ip->iso_frame_desc[i].length = size; | |
492 | } | |
493 | ||
494 | return ip; | |
495 | } | |
496 | ||
497 | static void usbtv_stop(struct usbtv *usbtv) | |
498 | { | |
499 | int i; | |
500 | unsigned long flags; | |
501 | ||
502 | /* Cancel running transfers. */ | |
503 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { | |
504 | struct urb *ip = usbtv->isoc_urbs[i]; | |
505 | if (ip == NULL) | |
506 | continue; | |
507 | usb_kill_urb(ip); | |
508 | kfree(ip->transfer_buffer); | |
509 | usb_free_urb(ip); | |
510 | usbtv->isoc_urbs[i] = NULL; | |
511 | } | |
512 | ||
513 | /* Return buffers to userspace. */ | |
514 | spin_lock_irqsave(&usbtv->buflock, flags); | |
515 | while (!list_empty(&usbtv->bufs)) { | |
516 | struct usbtv_buf *buf = list_first_entry(&usbtv->bufs, | |
517 | struct usbtv_buf, list); | |
518 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
519 | list_del(&buf->list); | |
520 | } | |
521 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
522 | } | |
523 | ||
524 | static int usbtv_start(struct usbtv *usbtv) | |
525 | { | |
526 | int i; | |
527 | int ret; | |
528 | ||
529 | ret = usb_set_interface(usbtv->udev, 0, 0); | |
530 | if (ret < 0) | |
531 | return ret; | |
532 | ||
533 | ret = usbtv_setup_capture(usbtv); | |
534 | if (ret < 0) | |
535 | return ret; | |
536 | ||
537 | ret = usb_set_interface(usbtv->udev, 0, 1); | |
538 | if (ret < 0) | |
539 | return ret; | |
540 | ||
541 | for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) { | |
542 | struct urb *ip; | |
543 | ||
544 | ip = usbtv_setup_iso_transfer(usbtv); | |
545 | if (ip == NULL) { | |
546 | ret = -ENOMEM; | |
547 | goto start_fail; | |
548 | } | |
549 | usbtv->isoc_urbs[i] = ip; | |
550 | ||
551 | ret = usb_submit_urb(ip, GFP_KERNEL); | |
552 | if (ret < 0) | |
553 | goto start_fail; | |
554 | } | |
555 | ||
556 | return 0; | |
557 | ||
558 | start_fail: | |
559 | usbtv_stop(usbtv); | |
560 | return ret; | |
561 | } | |
562 | ||
563 | struct usb_device_id usbtv_id_table[] = { | |
564 | { USB_DEVICE(0x1b71, 0x3002) }, | |
565 | {} | |
566 | }; | |
567 | MODULE_DEVICE_TABLE(usb, usbtv_id_table); | |
568 | ||
569 | static int usbtv_querycap(struct file *file, void *priv, | |
570 | struct v4l2_capability *cap) | |
571 | { | |
572 | struct usbtv *dev = video_drvdata(file); | |
573 | ||
574 | strlcpy(cap->driver, "usbtv", sizeof(cap->driver)); | |
575 | strlcpy(cap->card, "usbtv", sizeof(cap->card)); | |
576 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); | |
577 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE; | |
578 | cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; | |
579 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; | |
580 | return 0; | |
581 | } | |
582 | ||
583 | static int usbtv_enum_input(struct file *file, void *priv, | |
584 | struct v4l2_input *i) | |
585 | { | |
0e0fe395 GK |
586 | struct usbtv *dev = video_drvdata(file); |
587 | ||
2206112b LR |
588 | switch (i->index) { |
589 | case USBTV_COMPOSITE_INPUT: | |
590 | strlcpy(i->name, "Composite", sizeof(i->name)); | |
591 | break; | |
592 | case USBTV_SVIDEO_INPUT: | |
593 | strlcpy(i->name, "S-Video", sizeof(i->name)); | |
594 | break; | |
595 | default: | |
f3d27f34 | 596 | return -EINVAL; |
2206112b | 597 | } |
f3d27f34 | 598 | |
f3d27f34 | 599 | i->type = V4L2_INPUT_TYPE_CAMERA; |
0e0fe395 | 600 | i->std = dev->vdev.tvnorms; |
f3d27f34 LR |
601 | return 0; |
602 | } | |
603 | ||
604 | static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv, | |
605 | struct v4l2_fmtdesc *f) | |
606 | { | |
607 | if (f->index > 0) | |
608 | return -EINVAL; | |
609 | ||
610 | strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed", | |
611 | sizeof(f->description)); | |
612 | f->pixelformat = V4L2_PIX_FMT_YUYV; | |
613 | return 0; | |
614 | } | |
615 | ||
616 | static int usbtv_fmt_vid_cap(struct file *file, void *priv, | |
617 | struct v4l2_format *f) | |
618 | { | |
0e0fe395 GK |
619 | struct usbtv *usbtv = video_drvdata(file); |
620 | ||
621 | f->fmt.pix.width = usbtv->width; | |
622 | f->fmt.pix.height = usbtv->height; | |
f3d27f34 LR |
623 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
624 | f->fmt.pix.field = V4L2_FIELD_INTERLACED; | |
0e0fe395 | 625 | f->fmt.pix.bytesperline = usbtv->width * 2; |
f3d27f34 LR |
626 | f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height); |
627 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; | |
0e0fe395 | 628 | |
f3d27f34 LR |
629 | return 0; |
630 | } | |
631 | ||
632 | static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm) | |
633 | { | |
0e0fe395 GK |
634 | struct usbtv *usbtv = video_drvdata(file); |
635 | *norm = usbtv->norm; | |
f3d27f34 LR |
636 | return 0; |
637 | } | |
638 | ||
0e0fe395 GK |
639 | static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm) |
640 | { | |
641 | int ret = -EINVAL; | |
642 | struct usbtv *usbtv = video_drvdata(file); | |
643 | ||
644 | if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL)) | |
645 | ret = usbtv_select_norm(usbtv, norm); | |
646 | ||
647 | return ret; | |
648 | } | |
649 | ||
f3d27f34 LR |
650 | static int usbtv_g_input(struct file *file, void *priv, unsigned int *i) |
651 | { | |
2206112b LR |
652 | struct usbtv *usbtv = video_drvdata(file); |
653 | *i = usbtv->input; | |
f3d27f34 LR |
654 | return 0; |
655 | } | |
656 | ||
657 | static int usbtv_s_input(struct file *file, void *priv, unsigned int i) | |
658 | { | |
2206112b LR |
659 | struct usbtv *usbtv = video_drvdata(file); |
660 | return usbtv_select_input(usbtv, i); | |
f3d27f34 LR |
661 | } |
662 | ||
f3d27f34 LR |
663 | struct v4l2_ioctl_ops usbtv_ioctl_ops = { |
664 | .vidioc_querycap = usbtv_querycap, | |
665 | .vidioc_enum_input = usbtv_enum_input, | |
666 | .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap, | |
667 | .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap, | |
668 | .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap, | |
669 | .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap, | |
670 | .vidioc_g_std = usbtv_g_std, | |
671 | .vidioc_s_std = usbtv_s_std, | |
672 | .vidioc_g_input = usbtv_g_input, | |
673 | .vidioc_s_input = usbtv_s_input, | |
674 | ||
675 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
676 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
677 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
678 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
679 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
680 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
681 | .vidioc_streamon = vb2_ioctl_streamon, | |
682 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
683 | }; | |
684 | ||
685 | struct v4l2_file_operations usbtv_fops = { | |
686 | .owner = THIS_MODULE, | |
687 | .unlocked_ioctl = video_ioctl2, | |
688 | .mmap = vb2_fop_mmap, | |
689 | .open = v4l2_fh_open, | |
690 | .release = vb2_fop_release, | |
691 | .read = vb2_fop_read, | |
692 | .poll = vb2_fop_poll, | |
693 | }; | |
694 | ||
695 | static int usbtv_queue_setup(struct vb2_queue *vq, | |
696 | const struct v4l2_format *v4l_fmt, unsigned int *nbuffers, | |
697 | unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[]) | |
698 | { | |
0e0fe395 GK |
699 | struct usbtv *usbtv = vb2_get_drv_priv(vq); |
700 | ||
f3d27f34 LR |
701 | if (*nbuffers < 2) |
702 | *nbuffers = 2; | |
703 | *nplanes = 1; | |
0e0fe395 | 704 | sizes[0] = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32); |
f3d27f34 LR |
705 | |
706 | return 0; | |
707 | } | |
708 | ||
709 | static void usbtv_buf_queue(struct vb2_buffer *vb) | |
710 | { | |
711 | struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue); | |
712 | struct usbtv_buf *buf = container_of(vb, struct usbtv_buf, vb); | |
713 | unsigned long flags; | |
714 | ||
715 | if (usbtv->udev == NULL) { | |
716 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
717 | return; | |
718 | } | |
719 | ||
720 | spin_lock_irqsave(&usbtv->buflock, flags); | |
721 | list_add_tail(&buf->list, &usbtv->bufs); | |
722 | spin_unlock_irqrestore(&usbtv->buflock, flags); | |
723 | } | |
724 | ||
725 | static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count) | |
726 | { | |
727 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
728 | ||
729 | if (usbtv->udev == NULL) | |
730 | return -ENODEV; | |
731 | ||
732 | return usbtv_start(usbtv); | |
733 | } | |
734 | ||
735 | static int usbtv_stop_streaming(struct vb2_queue *vq) | |
736 | { | |
737 | struct usbtv *usbtv = vb2_get_drv_priv(vq); | |
738 | ||
739 | if (usbtv->udev == NULL) | |
740 | return -ENODEV; | |
741 | ||
742 | usbtv_stop(usbtv); | |
743 | return 0; | |
744 | } | |
745 | ||
746 | struct vb2_ops usbtv_vb2_ops = { | |
747 | .queue_setup = usbtv_queue_setup, | |
748 | .buf_queue = usbtv_buf_queue, | |
749 | .start_streaming = usbtv_start_streaming, | |
750 | .stop_streaming = usbtv_stop_streaming, | |
751 | }; | |
752 | ||
753 | static void usbtv_release(struct v4l2_device *v4l2_dev) | |
754 | { | |
755 | struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev); | |
756 | ||
757 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
758 | vb2_queue_release(&usbtv->vb2q); | |
759 | kfree(usbtv); | |
760 | } | |
761 | ||
762 | static int usbtv_probe(struct usb_interface *intf, | |
763 | const struct usb_device_id *id) | |
764 | { | |
765 | int ret; | |
766 | int size; | |
767 | struct device *dev = &intf->dev; | |
768 | struct usbtv *usbtv; | |
769 | ||
770 | /* Checks that the device is what we think it is. */ | |
771 | if (intf->num_altsetting != 2) | |
772 | return -ENODEV; | |
773 | if (intf->altsetting[1].desc.bNumEndpoints != 4) | |
774 | return -ENODEV; | |
775 | ||
776 | /* Packet size is split into 11 bits of base size and count of | |
777 | * extra multiplies of it.*/ | |
778 | size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc); | |
779 | size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1); | |
780 | ||
781 | /* Device structure */ | |
782 | usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL); | |
783 | if (usbtv == NULL) | |
784 | return -ENOMEM; | |
785 | usbtv->dev = dev; | |
786 | usbtv->udev = usb_get_dev(interface_to_usbdev(intf)); | |
0e0fe395 | 787 | |
f3d27f34 | 788 | usbtv->iso_size = size; |
0e0fe395 GK |
789 | |
790 | (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60); | |
791 | ||
f3d27f34 LR |
792 | spin_lock_init(&usbtv->buflock); |
793 | mutex_init(&usbtv->v4l2_lock); | |
794 | mutex_init(&usbtv->vb2q_lock); | |
795 | INIT_LIST_HEAD(&usbtv->bufs); | |
796 | ||
797 | /* videobuf2 structure */ | |
798 | usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
799 | usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; | |
800 | usbtv->vb2q.drv_priv = usbtv; | |
801 | usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf); | |
802 | usbtv->vb2q.ops = &usbtv_vb2_ops; | |
803 | usbtv->vb2q.mem_ops = &vb2_vmalloc_memops; | |
804 | usbtv->vb2q.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
805 | usbtv->vb2q.lock = &usbtv->vb2q_lock; | |
806 | ret = vb2_queue_init(&usbtv->vb2q); | |
807 | if (ret < 0) { | |
808 | dev_warn(dev, "Could not initialize videobuf2 queue\n"); | |
809 | goto usbtv_fail; | |
810 | } | |
811 | ||
812 | /* v4l2 structure */ | |
813 | usbtv->v4l2_dev.release = usbtv_release; | |
814 | ret = v4l2_device_register(dev, &usbtv->v4l2_dev); | |
815 | if (ret < 0) { | |
816 | dev_warn(dev, "Could not register v4l2 device\n"); | |
817 | goto v4l2_fail; | |
818 | } | |
819 | ||
820 | usb_set_intfdata(intf, usbtv); | |
821 | ||
822 | /* Video structure */ | |
823 | strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name)); | |
824 | usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev; | |
825 | usbtv->vdev.release = video_device_release_empty; | |
826 | usbtv->vdev.fops = &usbtv_fops; | |
827 | usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops; | |
0e0fe395 | 828 | usbtv->vdev.tvnorms = USBTV_TV_STD; |
f3d27f34 LR |
829 | usbtv->vdev.queue = &usbtv->vb2q; |
830 | usbtv->vdev.lock = &usbtv->v4l2_lock; | |
831 | set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags); | |
832 | video_set_drvdata(&usbtv->vdev, usbtv); | |
833 | ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1); | |
834 | if (ret < 0) { | |
835 | dev_warn(dev, "Could not register video device\n"); | |
836 | goto vdev_fail; | |
837 | } | |
838 | ||
839 | dev_info(dev, "Fushicai USBTV007 Video Grabber\n"); | |
840 | return 0; | |
841 | ||
842 | vdev_fail: | |
843 | v4l2_device_unregister(&usbtv->v4l2_dev); | |
844 | v4l2_fail: | |
845 | vb2_queue_release(&usbtv->vb2q); | |
846 | usbtv_fail: | |
847 | kfree(usbtv); | |
848 | ||
849 | return ret; | |
850 | } | |
851 | ||
852 | static void usbtv_disconnect(struct usb_interface *intf) | |
853 | { | |
854 | struct usbtv *usbtv = usb_get_intfdata(intf); | |
855 | ||
856 | mutex_lock(&usbtv->vb2q_lock); | |
857 | mutex_lock(&usbtv->v4l2_lock); | |
858 | ||
859 | usbtv_stop(usbtv); | |
860 | usb_set_intfdata(intf, NULL); | |
861 | video_unregister_device(&usbtv->vdev); | |
862 | v4l2_device_disconnect(&usbtv->v4l2_dev); | |
863 | usb_put_dev(usbtv->udev); | |
864 | usbtv->udev = NULL; | |
865 | ||
866 | mutex_unlock(&usbtv->v4l2_lock); | |
867 | mutex_unlock(&usbtv->vb2q_lock); | |
868 | ||
869 | v4l2_device_put(&usbtv->v4l2_dev); | |
870 | } | |
871 | ||
872 | MODULE_AUTHOR("Lubomir Rintel"); | |
873 | MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver"); | |
874 | MODULE_LICENSE("Dual BSD/GPL"); | |
875 | ||
876 | struct usb_driver usbtv_usb_driver = { | |
877 | .name = "usbtv", | |
878 | .id_table = usbtv_id_table, | |
879 | .probe = usbtv_probe, | |
880 | .disconnect = usbtv_disconnect, | |
881 | }; | |
882 | ||
883 | module_usb_driver(usbtv_usb_driver); |