-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharpzdc-v4l.c
515 lines (397 loc) · 12 KB
/
sharpzdc-v4l.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//#define DEBUG
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/interrupt.h>
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <media/v4l2-dev.h>
#include <media/v4l2-ioctl.h>
#include <media/videobuf-vmalloc.h>
#include "sharpzdc.h"
static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
module_param(vid_limit, int, 0644);
MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
static void sharpzdc_thread_tick(struct sharpzdc_info *info)
{
struct videobuf_buffer *vb;
void *vbuf;
unsigned long flags = 0;
pr_debug("%s\n", __func__);
spin_lock_irqsave(&info->lock, flags);
if (list_empty(&info->queued))
goto unlock;
vb = list_entry(info->queued.next,
struct videobuf_buffer, queue);
list_del(&vb->queue);
vbuf = videobuf_to_vmalloc(vb);
if (!vbuf) {
vb->state = VIDEOBUF_ERROR;
goto wake;
}
/* Fill buffer */
sharpzdc_get(info, vbuf);
/* Advice that buffer was filled */
vb->field_count += 1; /* two fields */
do_gettimeofday(&vb->ts);
vb->state = VIDEOBUF_DONE;
wake:
wake_up(&vb->done);
unlock:
spin_unlock_irqrestore(&info->lock, flags);
return;
}
int sharpzdc_kthread(void *data)
{
struct sharpzdc_info *info = data;
pr_debug("%s\n", __func__);
// set_user_nice(current, -20);
set_freezable();
for (;;) {
if (kthread_should_stop())
break;
try_to_freeze();
schedule_timeout_interruptible(HZ / (1000 * 30 / 1001));
// wait_event_freezable(&info->vb_vidq.wait, !list_empty(&info->queued) || kthread_should_stop());
if (kthread_should_stop())
break;
sharpzdc_thread_tick(info);
}
complete(&info->finish);
pr_debug("%s exiting\n", __func__);
return 0;
}
static void sharpzdc_buf_release(struct videobuf_queue *q,
struct videobuf_buffer *vb)
{
pr_debug("%s\n", __func__);
BUG_ON(in_interrupt());
videobuf_vmalloc_free(vb);
vb->state = VIDEOBUF_NEEDS_INIT;
}
static int sharpzdc_buf_setup(struct videobuf_queue *q,
unsigned int *count, unsigned int *size)
{
struct sharpzdc_info *info = q->priv_data;
pr_debug("%s\n", __func__);
*size = info->image_size;
if (*count == 0)
*count = 32;
while (*size * *count > vid_limit * 1024 * 1024)
(*count)--;
return 0;
}
static int sharpzdc_buf_prepare(struct videobuf_queue *q,
struct videobuf_buffer *vb,
enum v4l2_field field)
{
struct sharpzdc_info *info = q->priv_data;
int rc;
pr_debug("%s\n", __func__);
/* FIXME: width/height IRT rotation and zoom */
if (info->width < 32 || info->width > 640 ||
info->height < 32 || info->height > 480)
return -EINVAL;
vb->size = info->width*info->height*2;
if (0 != vb->baddr && vb->bsize < vb->size)
return -EINVAL;
/* These properties only change when queue is idle, see s_fmt */
vb->width = info->width;
vb->height = info->height;
vb->field = field;
if (vb->state == VIDEOBUF_NEEDS_INIT) {
rc = videobuf_iolock(q, vb, NULL);
if (rc < 0)
goto fail;
}
vb->state = VIDEOBUF_PREPARED;
return 0;
fail:
sharpzdc_buf_release(q, vb);
return rc;
}
static void sharpzdc_buf_queue(struct videobuf_queue *q,
struct videobuf_buffer *vb)
{
struct sharpzdc_info *info = q->priv_data;
pr_debug("%s\n", __func__);
vb->state = VIDEOBUF_QUEUED;
list_add_tail(&vb->queue, &info->queued);
}
static struct videobuf_queue_ops sharpzdc_video_qops = {
.buf_setup = sharpzdc_buf_setup,
.buf_prepare = sharpzdc_buf_prepare,
.buf_queue = sharpzdc_buf_queue,
.buf_release = sharpzdc_buf_release,
};
static void sharpzdc_vdev_release(struct video_device *vdev)
{
struct sharpzdc_info *info = video_get_drvdata(vdev);
pr_debug("%s\n", __func__);
video_device_release(vdev);
kref_put(&info->ref, sharpzdc_info_release); /* vdev->drvdata = info */
}
static ssize_t
sharpzdc_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
{
struct sharpzdc_info *info = file->private_data;
pr_debug("%s\n", __func__);
return videobuf_read_stream(&info->vb_vidq, data, count, ppos, 0,
file->f_flags & O_NONBLOCK);
}
static unsigned int
sharpzdc_poll(struct file *file, struct poll_table_struct *wait)
{
struct sharpzdc_info *info = file->private_data;
int ret;
pr_debug("%s\n", __func__);
ret = videobuf_poll_stream(file, &info->vb_vidq, wait);
return ret;
}
static int sharpzdc_mmap(struct file *file, struct vm_area_struct *vma)
{
struct sharpzdc_info *info = file->private_data;
pr_debug("%s\n", __func__);
return videobuf_mmap_mapper(&info->vb_vidq, vma);
}
static int sharpzdc_open(struct file *fp)
{
struct video_device *vdev = video_devdata(fp);
struct sharpzdc_info *info = video_get_drvdata(vdev);
pr_debug("%s\n", __func__);
kref_get(&info->ref); /* as we store info in fp */
fp->private_data = info;
videobuf_queue_vmalloc_init(&info->vb_vidq, &sharpzdc_video_qops,
NULL, &info->lock, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
sizeof(struct videobuf_buffer), info);
return 0;
}
static int sharpzdc_release(struct file *fp)
{
struct sharpzdc_info *info = fp->private_data;
pr_debug("%s\n", __func__);
videobuf_stop(&info->vb_vidq);
videobuf_mmap_free(&info->vb_vidq);
kref_put(&info->ref, sharpzdc_info_release); /* pair to kref_get in sharpzdc_open */
return 0;
}
static const struct v4l2_file_operations sharpzdc_fops = {
.owner = THIS_MODULE,
.open = sharpzdc_open,
.release = sharpzdc_release,
.read = sharpzdc_read,
.poll = sharpzdc_poll,
.mmap = sharpzdc_mmap,
.ioctl = video_ioctl2,
#ifdef CONFIG_COMPAT
.compat_ioctl = v4l_compat_ioctl32,
#endif
};
static int sharpzdc_querycap(struct file *file, void *private_data,
struct v4l2_capability *cap)
{
pr_debug("%s\n", __func__);
strcpy(cap->driver, "sharpzdc_cs");
strcpy(cap->card, "CE-AG06");
strncpy(cap->bus_info, "pcmcia:sharpzdc", sizeof(cap->bus_info));
cap->version = KERNEL_VERSION(0, 0, 1);
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_STREAMING |
V4L2_CAP_READWRITE;
return 0;
}
static int sharpzdc_enum_input(struct file *file, void *private_data,
struct v4l2_input *input)
{
pr_debug("%s\n", __func__);
if (input->index > 0)
return -EINVAL;
strcpy(input->name, "Camera");
input->type = V4L2_INPUT_TYPE_CAMERA;
input->std = V4L2_STD_UNKNOWN;
return 0;
}
static int sharpzdc_g_input(struct file *file, void *private_data,
unsigned int *index)
{
pr_debug("%s\n", __func__);
*index = 0;
return 0;
}
static int sharpzdc_s_input(struct file *file, void *private_data,
unsigned int index)
{
pr_debug("%s\n", __func__);
if (index != 0)
return -EINVAL;
return 0;
}
static int sharpzdc_enum_fmt_vid_cap(struct file *file, void *private_data,
struct v4l2_fmtdesc *f)
{
pr_debug("%s\n", __func__);
if (f->index > 0)
return -EINVAL;
f->pixelformat = V4L2_PIX_FMT_RGB565;
strcpy(f->description, "RGB565");
return 0;
}
static int sharpzdc_g_fmt_vid_cap(struct file *file, void *private_data,
struct v4l2_format *f)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
f->fmt.pix.width = info->width;
f->fmt.pix.height = info->height;
f->fmt.pix.bytesperline = info->line_stride;
f->fmt.pix.sizeimage = info->image_size;
f->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
f->fmt.pix.field = V4L2_FIELD_NONE;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
return 0;
}
static int sharpzdc_try_fmt_vid_cap(struct file *file, void *private_data,
struct v4l2_format *f)
{
pr_debug("%s\n", __func__);
/* FIXME: width, height, bytesperline, sizeimage limitation wrt rotating and zoom. */
if (f->fmt.pix.width < 32)
f->fmt.pix.width = 32;
if (f->fmt.pix.width > 640)
f->fmt.pix.width = 640;
f->fmt.pix.width = (f->fmt.pix.width + 1)&~1;
if (f->fmt.pix.height < 32)
f->fmt.pix.height = 32;
if (f->fmt.pix.height > 480)
f->fmt.pix.height = 480;
f->fmt.pix.height = (f->fmt.pix.height + 1)&~1;
if (f->fmt.pix.bytesperline < (f->fmt.pix.width * 2))
f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
f->fmt.pix.bytesperline = (f->fmt.pix.bytesperline + 3)&~3;
if (f->fmt.pix.sizeimage < (f->fmt.pix.bytesperline * f->fmt.pix.height))
f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * f->fmt.pix.height;
f->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
f->fmt.pix.field = V4L2_FIELD_NONE;
f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
return 0;
}
static int sharpzdc_s_fmt_vid_cap(struct file *file, void *private_data,
struct v4l2_format *f)
{
struct sharpzdc_info *info = private_data;
int ret = 0;
pr_debug("%s\n", __func__);
ret = sharpzdc_try_fmt_vid_cap(file, private_data, f);
if (ret < 0)
return ret;
mutex_lock(&info->vb_vidq.vb_lock);
if (videobuf_queue_is_busy(&info->vb_vidq)) {
pr_debug("%s queue busy\n", __func__);
ret = -EBUSY;
goto out;
}
/* FIXME: width, height, bytesperline, sizeimage limitation wrt rotating and zoom. */
info->width = f->fmt.pix.width;
info->height = f->fmt.pix.height;
info->line_stride = f->fmt.pix.bytesperline;
info->image_size = f->fmt.pix.sizeimage;
out:
mutex_unlock(&info->vb_vidq.vb_lock);
return ret;
}
static int sharpzdc_reqbufs(struct file *file, void *private_data,
struct v4l2_requestbuffers *p)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
return videobuf_reqbufs(&info->vb_vidq, p);
}
static int sharpzdc_querybuf(struct file *file, void *private_data,
struct v4l2_buffer *p)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
return videobuf_querybuf(&info->vb_vidq, p);
}
static int sharpzdc_qbuf(struct file *file, void *private_data, struct v4l2_buffer *p)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
return videobuf_qbuf(&info->vb_vidq, p);
}
static int sharpzdc_dqbuf(struct file *file, void *private_data, struct v4l2_buffer *p)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
return videobuf_dqbuf(&info->vb_vidq, p,
file->f_flags & O_NONBLOCK);
}
#ifdef CONFIG_VIDEO_V4L1_COMPAT
static int sharpzdc_cgmbuf(struct file *file, void *private_data, struct video_mbuf *mbuf)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
return videobuf_cgmbuf(&info->vb_vidq, mbuf, 8);
}
#endif
static int sharpzdc_streamon(struct file *file, void *private_data, enum v4l2_buf_type i)
{
struct sharpzdc_info *info = private_data;
if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
pr_debug("%s\n", __func__);
return videobuf_streamon(&info->vb_vidq);
}
static int sharpzdc_streamoff(struct file *file, void *private_data, enum v4l2_buf_type i)
{
struct sharpzdc_info *info = private_data;
pr_debug("%s\n", __func__);
if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return -EINVAL;
return videobuf_streamoff(&info->vb_vidq);
}
static const struct v4l2_ioctl_ops sharpzdc_ioctl_ops = {
.vidioc_querycap = sharpzdc_querycap,
.vidioc_enum_input = sharpzdc_enum_input,
.vidioc_g_input = sharpzdc_g_input,
.vidioc_s_input = sharpzdc_s_input,
.vidioc_enum_fmt_vid_cap = sharpzdc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = sharpzdc_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = sharpzdc_s_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = sharpzdc_try_fmt_vid_cap,
.vidioc_reqbufs = sharpzdc_reqbufs,
.vidioc_querybuf = sharpzdc_querybuf,
.vidioc_qbuf = sharpzdc_qbuf,
.vidioc_dqbuf = sharpzdc_dqbuf,
.vidioc_streamon = sharpzdc_streamon,
.vidioc_streamoff = sharpzdc_streamoff,
#ifdef CONFIG_VIDEO_V4L1_COMPAT
.vidiocgmbuf = sharpzdc_cgmbuf,
#endif
};
int sharpzdc_vdev_init(struct device *parent, struct sharpzdc_info *info)
{
int rc;
info->vdev = video_device_alloc();
if (info->vdev == NULL)
return -ENOMEM;
kref_get(&info->ref); /* vdev->drvdata = info */
video_set_drvdata(info->vdev, info);
info->vdev->fops = &sharpzdc_fops;
info->vdev->fops = &sharpzdc_fops;
info->vdev->release = sharpzdc_vdev_release;
info->vdev->ioctl_ops = &sharpzdc_ioctl_ops;
info->vdev->tvnorms = V4L2_STD_UNKNOWN;
info->vdev->current_norm = V4L2_STD_UNKNOWN;
info->vdev->parent = parent;
strncpy(info->vdev->name, "sharpzdc", sizeof(info->vdev->name));
rc = video_register_device(info->vdev, VFL_TYPE_GRABBER, -1);
if (rc) {
sharpzdc_vdev_release(info->vdev);
info->vdev = NULL;
}
return rc;
}
void sharpzdc_vdev_exit(struct sharpzdc_info *info)
{
video_unregister_device(info->vdev);
}