1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/completion.h>
31#include <linux/uaccess.h>
32#include <linux/usb.h>
33#include <linux/printk.h>
34#include <linux/input-polldev.h>
35#include <linux/input/mt.h>
36#include <linux/usb/input.h>
37#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-dev.h>
40#include <media/v4l2-ioctl.h>
41#include <media/videobuf2-v4l2.h>
42#include <media/videobuf2-dma-sg.h>
43
44
45struct sur40_header {
46
47 __le16 type;
48 __le16 count;
49
50 __le32 packet_id;
51
52 __le32 timestamp;
53 __le32 unknown;
54
55} __packed;
56
57struct sur40_blob {
58
59 __le16 blob_id;
60
61 u8 action;
62 u8 unknown;
63
64 __le16 bb_pos_x;
65 __le16 bb_pos_y;
66
67 __le16 bb_size_x;
68 __le16 bb_size_y;
69
70 __le16 pos_x;
71 __le16 pos_y;
72
73 __le16 ctr_x;
74 __le16 ctr_y;
75
76 __le16 axis_x;
77 __le16 axis_y;
78
79 __le32 angle;
80
81
82 __le32 area;
83
84 u8 padding[32];
85
86} __packed;
87
88
89struct sur40_data {
90 struct sur40_header header;
91 struct sur40_blob blobs[];
92} __packed;
93
94
95
96struct sur40_image_header {
97 __le32 magic;
98 __le32 packet_id;
99 __le32 size;
100 __le32 timestamp;
101 __le32 unknown;
102} __packed;
103
104
105#define DRIVER_SHORT "sur40"
106#define DRIVER_LONG "Samsung SUR40"
107#define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
108#define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
109
110
111#define ID_MICROSOFT 0x045e
112#define ID_SUR40 0x0775
113
114
115#define SENSOR_RES_X 1920
116#define SENSOR_RES_Y 1080
117
118
119#define TOUCH_ENDPOINT 0x86
120
121
122#define VIDEO_ENDPOINT 0x82
123
124
125#define VIDEO_HEADER_MAGIC 0x46425553
126#define VIDEO_PACKET_SIZE 16384
127
128
129#define POLL_INTERVAL 1
130
131
132#define MAX_CONTACTS 64
133
134
135#define SUR40_GET_VERSION 0xb0
136#define SUR40_UNKNOWN1 0xb3
137#define SUR40_UNKNOWN2 0xc1
138
139#define SUR40_GET_STATE 0xc5
140#define SUR40_GET_SENSORS 0xb1
141
142static const struct v4l2_pix_format sur40_pix_format[] = {
143 {
144 .pixelformat = V4L2_TCH_FMT_TU08,
145 .width = SENSOR_RES_X / 2,
146 .height = SENSOR_RES_Y / 2,
147 .field = V4L2_FIELD_NONE,
148 .colorspace = V4L2_COLORSPACE_SRGB,
149 .bytesperline = SENSOR_RES_X / 2,
150 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
151 },
152 {
153 .pixelformat = V4L2_PIX_FMT_GREY,
154 .width = SENSOR_RES_X / 2,
155 .height = SENSOR_RES_Y / 2,
156 .field = V4L2_FIELD_NONE,
157 .colorspace = V4L2_COLORSPACE_SRGB,
158 .bytesperline = SENSOR_RES_X / 2,
159 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
160 }
161};
162
163
164struct sur40_state {
165
166 struct usb_device *usbdev;
167 struct device *dev;
168 struct input_polled_dev *input;
169
170 struct v4l2_device v4l2;
171 struct video_device vdev;
172 struct mutex lock;
173 struct v4l2_pix_format pix_fmt;
174
175 struct vb2_queue queue;
176 struct list_head buf_list;
177 spinlock_t qlock;
178 int sequence;
179
180 struct sur40_data *bulk_in_buffer;
181 size_t bulk_in_size;
182 u8 bulk_in_epaddr;
183
184 char phys[64];
185};
186
187struct sur40_buffer {
188 struct vb2_v4l2_buffer vb;
189 struct list_head list;
190};
191
192
193static const struct video_device sur40_video_device;
194static const struct vb2_queue sur40_queue;
195static void sur40_process_video(struct sur40_state *sur40);
196
197
198
199
200
201
202
203
204
205
206
207static int sur40_command(struct sur40_state *dev,
208 u8 command, u16 index, void *buffer, u16 size)
209{
210 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
211 command,
212 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
213 0x00, index, buffer, size, 1000);
214}
215
216
217static int sur40_init(struct sur40_state *dev)
218{
219 int result;
220 u8 *buffer;
221
222 buffer = kmalloc(24, GFP_KERNEL);
223 if (!buffer) {
224 result = -ENOMEM;
225 goto error;
226 }
227
228
229 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
230 if (result < 0)
231 goto error;
232
233 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
234 if (result < 0)
235 goto error;
236
237 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
238 if (result < 0)
239 goto error;
240
241 result = sur40_command(dev, SUR40_UNKNOWN2, 0x00, buffer, 24);
242 if (result < 0)
243 goto error;
244
245 result = sur40_command(dev, SUR40_UNKNOWN1, 0x00, buffer, 5);
246 if (result < 0)
247 goto error;
248
249 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
250
251
252
253
254
255error:
256 kfree(buffer);
257 return result;
258}
259
260
261
262
263
264
265static void sur40_open(struct input_polled_dev *polldev)
266{
267 struct sur40_state *sur40 = polldev->private;
268
269 dev_dbg(sur40->dev, "open\n");
270 sur40_init(sur40);
271}
272
273
274static void sur40_close(struct input_polled_dev *polldev)
275{
276 struct sur40_state *sur40 = polldev->private;
277
278 dev_dbg(sur40->dev, "close\n");
279
280
281
282
283}
284
285
286
287
288
289static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
290{
291 int wide, major, minor;
292
293 int bb_size_x = le16_to_cpu(blob->bb_size_x);
294 int bb_size_y = le16_to_cpu(blob->bb_size_y);
295
296 int pos_x = le16_to_cpu(blob->pos_x);
297 int pos_y = le16_to_cpu(blob->pos_y);
298
299 int ctr_x = le16_to_cpu(blob->ctr_x);
300 int ctr_y = le16_to_cpu(blob->ctr_y);
301
302 int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
303 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
304 return;
305
306 input_mt_slot(input, slotnum);
307 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
308 wide = (bb_size_x > bb_size_y);
309 major = max(bb_size_x, bb_size_y);
310 minor = min(bb_size_x, bb_size_y);
311
312 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
313 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
314 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
315 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
316
317
318 input_report_abs(input, ABS_MT_ORIENTATION, wide);
319 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
320 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
321}
322
323
324static void sur40_poll(struct input_polled_dev *polldev)
325{
326 struct sur40_state *sur40 = polldev->private;
327 struct input_dev *input = polldev->input;
328 int result, bulk_read, need_blobs, packet_blobs, i;
329 u32 uninitialized_var(packet_id);
330
331 struct sur40_header *header = &sur40->bulk_in_buffer->header;
332 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
333
334 dev_dbg(sur40->dev, "poll\n");
335
336 need_blobs = -1;
337
338 do {
339
340
341 result = usb_bulk_msg(sur40->usbdev,
342 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
343 sur40->bulk_in_buffer, sur40->bulk_in_size,
344 &bulk_read, 1000);
345
346 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
347
348 if (result < 0) {
349 dev_err(sur40->dev, "error in usb_bulk_read\n");
350 return;
351 }
352
353 result = bulk_read - sizeof(struct sur40_header);
354
355 if (result % sizeof(struct sur40_blob) != 0) {
356 dev_err(sur40->dev, "transfer size mismatch\n");
357 return;
358 }
359
360
361 if (need_blobs == -1) {
362 need_blobs = le16_to_cpu(header->count);
363 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
364 packet_id = le32_to_cpu(header->packet_id);
365 }
366
367
368
369
370
371
372 if (packet_id != header->packet_id)
373 dev_dbg(sur40->dev, "packet ID mismatch\n");
374
375 packet_blobs = result / sizeof(struct sur40_blob);
376 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
377
378
379 if (packet_blobs > need_blobs)
380 packet_blobs = need_blobs;
381
382 for (i = 0; i < packet_blobs; i++) {
383 need_blobs--;
384 dev_dbg(sur40->dev, "processing blob\n");
385 sur40_report_blob(&(inblob[i]), input);
386 }
387
388 } while (need_blobs > 0);
389
390 input_mt_sync_frame(input);
391 input_sync(input);
392
393 sur40_process_video(sur40);
394}
395
396
397static void sur40_process_video(struct sur40_state *sur40)
398{
399
400 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
401 struct sur40_buffer *new_buf;
402 struct usb_sg_request sgr;
403 struct sg_table *sgt;
404 int result, bulk_read;
405
406 if (!vb2_start_streaming_called(&sur40->queue))
407 return;
408
409
410 spin_lock(&sur40->qlock);
411 if (list_empty(&sur40->buf_list)) {
412 dev_dbg(sur40->dev, "buffer queue empty\n");
413 spin_unlock(&sur40->qlock);
414 return;
415 }
416 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
417 list_del(&new_buf->list);
418 spin_unlock(&sur40->qlock);
419
420 dev_dbg(sur40->dev, "buffer acquired\n");
421
422
423 result = usb_bulk_msg(sur40->usbdev,
424 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
425 sur40->bulk_in_buffer, sur40->bulk_in_size,
426 &bulk_read, 1000);
427
428 if (result < 0) {
429 dev_err(sur40->dev, "error in usb_bulk_read\n");
430 goto err_poll;
431 }
432
433 if (bulk_read != sizeof(struct sur40_image_header)) {
434 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
435 bulk_read, sizeof(struct sur40_image_header));
436 goto err_poll;
437 }
438
439 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
440 dev_err(sur40->dev, "image magic mismatch\n");
441 goto err_poll;
442 }
443
444 if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
445 dev_err(sur40->dev, "image size mismatch\n");
446 goto err_poll;
447 }
448
449 dev_dbg(sur40->dev, "header acquired\n");
450
451 sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
452
453 result = usb_sg_init(&sgr, sur40->usbdev,
454 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
455 sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
456 if (result < 0) {
457 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
458 goto err_poll;
459 }
460
461 usb_sg_wait(&sgr);
462 if (sgr.status < 0) {
463 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
464 goto err_poll;
465 }
466
467 dev_dbg(sur40->dev, "image acquired\n");
468
469
470 if (sur40->sequence == -1)
471 return;
472
473
474 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
475 new_buf->vb.sequence = sur40->sequence++;
476 new_buf->vb.field = V4L2_FIELD_NONE;
477 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
478 dev_dbg(sur40->dev, "buffer marked done\n");
479 return;
480
481err_poll:
482 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
483}
484
485
486static void sur40_input_setup(struct input_dev *input_dev)
487{
488 __set_bit(EV_KEY, input_dev->evbit);
489 __set_bit(EV_ABS, input_dev->evbit);
490
491 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
492 0, SENSOR_RES_X, 0, 0);
493 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
494 0, SENSOR_RES_Y, 0, 0);
495
496 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
497 0, SENSOR_RES_X, 0, 0);
498 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
499 0, SENSOR_RES_Y, 0, 0);
500
501
502
503 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
504 0, SENSOR_RES_X, 0, 0);
505 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
506 0, SENSOR_RES_Y, 0, 0);
507
508 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
509
510 input_mt_init_slots(input_dev, MAX_CONTACTS,
511 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
512}
513
514
515static int sur40_probe(struct usb_interface *interface,
516 const struct usb_device_id *id)
517{
518 struct usb_device *usbdev = interface_to_usbdev(interface);
519 struct sur40_state *sur40;
520 struct usb_host_interface *iface_desc;
521 struct usb_endpoint_descriptor *endpoint;
522 struct input_polled_dev *poll_dev;
523 int error;
524
525
526 iface_desc = &interface->altsetting[0];
527 if (iface_desc->desc.bInterfaceClass != 0xFF)
528 return -ENODEV;
529
530
531 endpoint = &iface_desc->endpoint[4].desc;
532 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
533 return -ENODEV;
534
535
536 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
537 if (!sur40)
538 return -ENOMEM;
539
540 poll_dev = input_allocate_polled_device();
541 if (!poll_dev) {
542 error = -ENOMEM;
543 goto err_free_dev;
544 }
545
546
547 INIT_LIST_HEAD(&sur40->buf_list);
548 spin_lock_init(&sur40->qlock);
549 mutex_init(&sur40->lock);
550
551
552 poll_dev->private = sur40;
553 poll_dev->poll_interval = POLL_INTERVAL;
554 poll_dev->open = sur40_open;
555 poll_dev->poll = sur40_poll;
556 poll_dev->close = sur40_close;
557
558
559 sur40_input_setup(poll_dev->input);
560
561 poll_dev->input->name = DRIVER_LONG;
562 usb_to_input_id(usbdev, &poll_dev->input->id);
563 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
564 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
565 poll_dev->input->phys = sur40->phys;
566 poll_dev->input->dev.parent = &interface->dev;
567
568 sur40->usbdev = usbdev;
569 sur40->dev = &interface->dev;
570 sur40->input = poll_dev;
571
572
573 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
574 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
575 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
576 if (!sur40->bulk_in_buffer) {
577 dev_err(&interface->dev, "Unable to allocate input buffer.");
578 error = -ENOMEM;
579 goto err_free_polldev;
580 }
581
582
583 error = input_register_polled_device(poll_dev);
584 if (error) {
585 dev_err(&interface->dev,
586 "Unable to register polled input device.");
587 goto err_free_buffer;
588 }
589
590
591 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
592 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
593 if (error) {
594 dev_err(&interface->dev,
595 "Unable to register video master device.");
596 goto err_unreg_v4l2;
597 }
598
599
600 sur40->queue = sur40_queue;
601 sur40->queue.drv_priv = sur40;
602 sur40->queue.lock = &sur40->lock;
603 sur40->queue.dev = sur40->dev;
604
605
606 error = vb2_queue_init(&sur40->queue);
607 if (error)
608 goto err_unreg_v4l2;
609
610 sur40->pix_fmt = sur40_pix_format[0];
611 sur40->vdev = sur40_video_device;
612 sur40->vdev.v4l2_dev = &sur40->v4l2;
613 sur40->vdev.lock = &sur40->lock;
614 sur40->vdev.queue = &sur40->queue;
615 video_set_drvdata(&sur40->vdev, sur40);
616
617 error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
618 if (error) {
619 dev_err(&interface->dev,
620 "Unable to register video subdevice.");
621 goto err_unreg_video;
622 }
623
624
625 usb_set_intfdata(interface, sur40);
626 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
627
628 return 0;
629
630err_unreg_video:
631 video_unregister_device(&sur40->vdev);
632err_unreg_v4l2:
633 v4l2_device_unregister(&sur40->v4l2);
634err_free_buffer:
635 kfree(sur40->bulk_in_buffer);
636err_free_polldev:
637 input_free_polled_device(sur40->input);
638err_free_dev:
639 kfree(sur40);
640
641 return error;
642}
643
644
645static void sur40_disconnect(struct usb_interface *interface)
646{
647 struct sur40_state *sur40 = usb_get_intfdata(interface);
648
649 video_unregister_device(&sur40->vdev);
650 v4l2_device_unregister(&sur40->v4l2);
651
652 input_unregister_polled_device(sur40->input);
653 input_free_polled_device(sur40->input);
654 kfree(sur40->bulk_in_buffer);
655 kfree(sur40);
656
657 usb_set_intfdata(interface, NULL);
658 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
659}
660
661
662
663
664
665
666
667
668static int sur40_queue_setup(struct vb2_queue *q,
669 unsigned int *nbuffers, unsigned int *nplanes,
670 unsigned int sizes[], struct device *alloc_devs[])
671{
672 struct sur40_state *sur40 = vb2_get_drv_priv(q);
673
674 if (q->num_buffers + *nbuffers < 3)
675 *nbuffers = 3 - q->num_buffers;
676
677 if (*nplanes)
678 return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
679
680 *nplanes = 1;
681 sizes[0] = sur40->pix_fmt.sizeimage;
682
683 return 0;
684}
685
686
687
688
689
690static int sur40_buffer_prepare(struct vb2_buffer *vb)
691{
692 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
693 unsigned long size = sur40->pix_fmt.sizeimage;
694
695 if (vb2_plane_size(vb, 0) < size) {
696 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
697 vb2_plane_size(vb, 0), size);
698 return -EINVAL;
699 }
700
701 vb2_set_plane_payload(vb, 0, size);
702 return 0;
703}
704
705
706
707
708static void sur40_buffer_queue(struct vb2_buffer *vb)
709{
710 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
711 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
712
713 spin_lock(&sur40->qlock);
714 list_add_tail(&buf->list, &sur40->buf_list);
715 spin_unlock(&sur40->qlock);
716}
717
718static void return_all_buffers(struct sur40_state *sur40,
719 enum vb2_buffer_state state)
720{
721 struct sur40_buffer *buf, *node;
722
723 spin_lock(&sur40->qlock);
724 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
725 vb2_buffer_done(&buf->vb.vb2_buf, state);
726 list_del(&buf->list);
727 }
728 spin_unlock(&sur40->qlock);
729}
730
731
732
733
734
735
736
737static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
738{
739 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
740
741 sur40->sequence = 0;
742 return 0;
743}
744
745
746
747
748
749static void sur40_stop_streaming(struct vb2_queue *vq)
750{
751 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
752 vb2_wait_for_all_buffers(vq);
753 sur40->sequence = -1;
754
755
756 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
757}
758
759
760static int sur40_vidioc_querycap(struct file *file, void *priv,
761 struct v4l2_capability *cap)
762{
763 struct sur40_state *sur40 = video_drvdata(file);
764
765 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
766 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
767 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
768 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
769 V4L2_CAP_READWRITE |
770 V4L2_CAP_STREAMING;
771 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
772 return 0;
773}
774
775static int sur40_vidioc_enum_input(struct file *file, void *priv,
776 struct v4l2_input *i)
777{
778 if (i->index != 0)
779 return -EINVAL;
780 i->type = V4L2_INPUT_TYPE_TOUCH;
781 i->std = V4L2_STD_UNKNOWN;
782 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
783 i->capabilities = 0;
784 return 0;
785}
786
787static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
788{
789 return (i == 0) ? 0 : -EINVAL;
790}
791
792static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
793{
794 *i = 0;
795 return 0;
796}
797
798static int sur40_vidioc_try_fmt(struct file *file, void *priv,
799 struct v4l2_format *f)
800{
801 switch (f->fmt.pix.pixelformat) {
802 case V4L2_PIX_FMT_GREY:
803 f->fmt.pix = sur40_pix_format[1];
804 break;
805
806 default:
807 f->fmt.pix = sur40_pix_format[0];
808 break;
809 }
810
811 return 0;
812}
813
814static int sur40_vidioc_s_fmt(struct file *file, void *priv,
815 struct v4l2_format *f)
816{
817 struct sur40_state *sur40 = video_drvdata(file);
818
819 switch (f->fmt.pix.pixelformat) {
820 case V4L2_PIX_FMT_GREY:
821 sur40->pix_fmt = sur40_pix_format[1];
822 break;
823
824 default:
825 sur40->pix_fmt = sur40_pix_format[0];
826 break;
827 }
828
829 f->fmt.pix = sur40->pix_fmt;
830 return 0;
831}
832
833static int sur40_vidioc_g_fmt(struct file *file, void *priv,
834 struct v4l2_format *f)
835{
836 struct sur40_state *sur40 = video_drvdata(file);
837
838 f->fmt.pix = sur40->pix_fmt;
839 return 0;
840}
841
842static int sur40_ioctl_parm(struct file *file, void *priv,
843 struct v4l2_streamparm *p)
844{
845 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
846 return -EINVAL;
847
848 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
849 p->parm.capture.timeperframe.numerator = 1;
850 p->parm.capture.timeperframe.denominator = 60;
851 p->parm.capture.readbuffers = 3;
852 return 0;
853}
854
855static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
856 struct v4l2_fmtdesc *f)
857{
858 if (f->index >= ARRAY_SIZE(sur40_pix_format))
859 return -EINVAL;
860
861 f->pixelformat = sur40_pix_format[f->index].pixelformat;
862 f->flags = 0;
863 return 0;
864}
865
866static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
867 struct v4l2_frmsizeenum *f)
868{
869 struct sur40_state *sur40 = video_drvdata(file);
870
871 if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
872 && (f->pixel_format != V4L2_PIX_FMT_GREY)))
873 return -EINVAL;
874
875 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
876 f->discrete.width = sur40->pix_fmt.width;
877 f->discrete.height = sur40->pix_fmt.height;
878 return 0;
879}
880
881static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
882 struct v4l2_frmivalenum *f)
883{
884 struct sur40_state *sur40 = video_drvdata(file);
885
886 if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
887 && (f->pixel_format != V4L2_PIX_FMT_GREY))
888 || (f->width != sur40->pix_fmt.width)
889 || (f->height != sur40->pix_fmt.height))
890 return -EINVAL;
891
892 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
893 f->discrete.denominator = 60;
894 f->discrete.numerator = 1;
895 return 0;
896}
897
898
899static const struct usb_device_id sur40_table[] = {
900 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) },
901 { }
902};
903MODULE_DEVICE_TABLE(usb, sur40_table);
904
905
906static const struct vb2_ops sur40_queue_ops = {
907 .queue_setup = sur40_queue_setup,
908 .buf_prepare = sur40_buffer_prepare,
909 .buf_queue = sur40_buffer_queue,
910 .start_streaming = sur40_start_streaming,
911 .stop_streaming = sur40_stop_streaming,
912 .wait_prepare = vb2_ops_wait_prepare,
913 .wait_finish = vb2_ops_wait_finish,
914};
915
916static const struct vb2_queue sur40_queue = {
917 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
918
919
920
921
922
923 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
924 .buf_struct_size = sizeof(struct sur40_buffer),
925 .ops = &sur40_queue_ops,
926 .mem_ops = &vb2_dma_sg_memops,
927 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
928 .min_buffers_needed = 3,
929};
930
931static const struct v4l2_file_operations sur40_video_fops = {
932 .owner = THIS_MODULE,
933 .open = v4l2_fh_open,
934 .release = vb2_fop_release,
935 .unlocked_ioctl = video_ioctl2,
936 .read = vb2_fop_read,
937 .mmap = vb2_fop_mmap,
938 .poll = vb2_fop_poll,
939};
940
941static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
942
943 .vidioc_querycap = sur40_vidioc_querycap,
944
945 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
946 .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
947 .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
948 .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
949
950 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
951 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
952
953 .vidioc_g_parm = sur40_ioctl_parm,
954 .vidioc_s_parm = sur40_ioctl_parm,
955
956 .vidioc_enum_input = sur40_vidioc_enum_input,
957 .vidioc_g_input = sur40_vidioc_g_input,
958 .vidioc_s_input = sur40_vidioc_s_input,
959
960 .vidioc_reqbufs = vb2_ioctl_reqbufs,
961 .vidioc_create_bufs = vb2_ioctl_create_bufs,
962 .vidioc_querybuf = vb2_ioctl_querybuf,
963 .vidioc_qbuf = vb2_ioctl_qbuf,
964 .vidioc_dqbuf = vb2_ioctl_dqbuf,
965 .vidioc_expbuf = vb2_ioctl_expbuf,
966
967 .vidioc_streamon = vb2_ioctl_streamon,
968 .vidioc_streamoff = vb2_ioctl_streamoff,
969};
970
971static const struct video_device sur40_video_device = {
972 .name = DRIVER_LONG,
973 .fops = &sur40_video_fops,
974 .ioctl_ops = &sur40_video_ioctl_ops,
975 .release = video_device_release_empty,
976};
977
978
979static struct usb_driver sur40_driver = {
980 .name = DRIVER_SHORT,
981 .probe = sur40_probe,
982 .disconnect = sur40_disconnect,
983 .id_table = sur40_table,
984};
985
986module_usb_driver(sur40_driver);
987
988MODULE_AUTHOR(DRIVER_AUTHOR);
989MODULE_DESCRIPTION(DRIVER_DESC);
990MODULE_LICENSE("GPL");
991