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