Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / input / touchscreen / sur40.c
blobf16f8358c70aab09da0cda18a4fa623f7d8a027a
1 /*
2 * Surface2.0/SUR40/PixelSense input driver
4 * Copyright (c) 2014 by Florian 'floe' Echtler <floe@butterbrot.org>
6 * Derived from the USB Skeleton driver 1.1,
7 * Copyright (c) 2003 Greg Kroah-Hartman (greg@kroah.com)
9 * and from the Apple USB BCM5974 multitouch driver,
10 * Copyright (c) 2008 Henrik Rydberg (rydberg@euromail.se)
12 * and from the generic hid-multitouch driver,
13 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
15 * and from the v4l2-pci-skeleton driver,
16 * Copyright (c) Copyright 2014 Cisco Systems, Inc.
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
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>
44 /* read 512 bytes from endpoint 0x86 -> get header + blobs */
45 struct sur40_header {
47 __le16 type; /* always 0x0001 */
48 __le16 count; /* count of blobs (if 0: continue prev. packet) */
50 __le32 packet_id; /* unique ID for all packets in one frame */
52 __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
53 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
55 } __packed;
57 struct sur40_blob {
59 __le16 blob_id;
61 u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
62 u8 type; /* bitmask (0x01 blob, 0x02 touch, 0x04 tag) */
64 __le16 bb_pos_x; /* upper left corner of bounding box */
65 __le16 bb_pos_y;
67 __le16 bb_size_x; /* size of bounding box */
68 __le16 bb_size_y;
70 __le16 pos_x; /* finger tip position */
71 __le16 pos_y;
73 __le16 ctr_x; /* centroid position */
74 __le16 ctr_y;
76 __le16 axis_x; /* somehow related to major/minor axis, mostly: */
77 __le16 axis_y; /* axis_x == bb_size_y && axis_y == bb_size_x */
79 __le32 angle; /* orientation in radians relative to x axis -
80 actually an IEEE754 float, don't use in kernel */
82 __le32 area; /* size in pixels/pressure (?) */
84 u8 padding[32];
86 } __packed;
88 /* combined header/blob data */
89 struct sur40_data {
90 struct sur40_header header;
91 struct sur40_blob blobs[];
92 } __packed;
94 /* read 512 bytes from endpoint 0x82 -> get header below
95 * continue reading 16k blocks until header.size bytes read */
96 struct sur40_image_header {
97 __le32 magic; /* "SUBF" */
98 __le32 packet_id;
99 __le32 size; /* always 0x0007e900 = 960x540 */
100 __le32 timestamp; /* milliseconds (increases by 16 or 17 each frame) */
101 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
102 } __packed;
104 /* version information */
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"
110 /* vendor and device IDs */
111 #define ID_MICROSOFT 0x045e
112 #define ID_SUR40 0x0775
114 /* sensor resolution */
115 #define SENSOR_RES_X 1920
116 #define SENSOR_RES_Y 1080
118 /* touch data endpoint */
119 #define TOUCH_ENDPOINT 0x86
121 /* video data endpoint */
122 #define VIDEO_ENDPOINT 0x82
124 /* video header fields */
125 #define VIDEO_HEADER_MAGIC 0x46425553
126 #define VIDEO_PACKET_SIZE 16384
128 /* polling interval (ms) */
129 #define POLL_INTERVAL 1
131 /* maximum number of contacts FIXME: this is a guess? */
132 #define MAX_CONTACTS 64
134 /* control commands */
135 #define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
136 #define SUR40_ACCEL_CAPS 0xb3 /* 5 bytes */
137 #define SUR40_SENSOR_CAPS 0xc1 /* 24 bytes */
139 #define SUR40_POKE 0xc5 /* poke register byte */
140 #define SUR40_PEEK 0xc4 /* 48 bytes registers */
142 #define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
143 #define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
145 #define SUR40_BLOB 0x01
146 #define SUR40_TOUCH 0x02
147 #define SUR40_TAG 0x04
149 static const struct v4l2_pix_format sur40_pix_format[] = {
151 .pixelformat = V4L2_TCH_FMT_TU08,
152 .width = SENSOR_RES_X / 2,
153 .height = SENSOR_RES_Y / 2,
154 .field = V4L2_FIELD_NONE,
155 .colorspace = V4L2_COLORSPACE_SRGB,
156 .bytesperline = SENSOR_RES_X / 2,
157 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
160 .pixelformat = V4L2_PIX_FMT_GREY,
161 .width = SENSOR_RES_X / 2,
162 .height = SENSOR_RES_Y / 2,
163 .field = V4L2_FIELD_NONE,
164 .colorspace = V4L2_COLORSPACE_SRGB,
165 .bytesperline = SENSOR_RES_X / 2,
166 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
170 /* master device state */
171 struct sur40_state {
173 struct usb_device *usbdev;
174 struct device *dev;
175 struct input_polled_dev *input;
177 struct v4l2_device v4l2;
178 struct video_device vdev;
179 struct mutex lock;
180 struct v4l2_pix_format pix_fmt;
182 struct vb2_queue queue;
183 struct list_head buf_list;
184 spinlock_t qlock;
185 int sequence;
187 struct sur40_data *bulk_in_buffer;
188 size_t bulk_in_size;
189 u8 bulk_in_epaddr;
191 char phys[64];
194 struct sur40_buffer {
195 struct vb2_v4l2_buffer vb;
196 struct list_head list;
199 /* forward declarations */
200 static const struct video_device sur40_video_device;
201 static const struct vb2_queue sur40_queue;
202 static void sur40_process_video(struct sur40_state *sur40);
205 * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
206 * here by mistake which is very likely to have corrupted the firmware EEPROM
207 * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
208 * Should you ever run into a similar problem, the background story to this
209 * incident and instructions on how to fix the corrupted EEPROM are available
210 * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
213 /* command wrapper */
214 static int sur40_command(struct sur40_state *dev,
215 u8 command, u16 index, void *buffer, u16 size)
217 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
218 command,
219 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
220 0x00, index, buffer, size, 1000);
223 /* Initialization routine, called from sur40_open */
224 static int sur40_init(struct sur40_state *dev)
226 int result;
227 u8 *buffer;
229 buffer = kmalloc(24, GFP_KERNEL);
230 if (!buffer) {
231 result = -ENOMEM;
232 goto error;
235 /* stupidly replay the original MS driver init sequence */
236 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
237 if (result < 0)
238 goto error;
240 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
241 if (result < 0)
242 goto error;
244 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
245 if (result < 0)
246 goto error;
248 result = sur40_command(dev, SUR40_SENSOR_CAPS, 0x00, buffer, 24);
249 if (result < 0)
250 goto error;
252 result = sur40_command(dev, SUR40_ACCEL_CAPS, 0x00, buffer, 5);
253 if (result < 0)
254 goto error;
256 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
259 * Discard the result buffer - no known data inside except
260 * some version strings, maybe extract these sometime...
262 error:
263 kfree(buffer);
264 return result;
268 * Callback routines from input_polled_dev
271 /* Enable the device, polling will now start. */
272 static void sur40_open(struct input_polled_dev *polldev)
274 struct sur40_state *sur40 = polldev->private;
276 dev_dbg(sur40->dev, "open\n");
277 sur40_init(sur40);
280 /* Disable device, polling has stopped. */
281 static void sur40_close(struct input_polled_dev *polldev)
283 struct sur40_state *sur40 = polldev->private;
285 dev_dbg(sur40->dev, "close\n");
287 * There is no known way to stop the device, so we simply
288 * stop polling.
293 * This function is called when a whole contact has been processed,
294 * so that it can assign it to a slot and store the data there.
296 static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
298 int wide, major, minor;
299 int bb_size_x, bb_size_y, pos_x, pos_y, ctr_x, ctr_y, slotnum;
301 if (blob->type != SUR40_TOUCH)
302 return;
304 slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
305 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
306 return;
308 bb_size_x = le16_to_cpu(blob->bb_size_x);
309 bb_size_y = le16_to_cpu(blob->bb_size_y);
311 pos_x = le16_to_cpu(blob->pos_x);
312 pos_y = le16_to_cpu(blob->pos_y);
314 ctr_x = le16_to_cpu(blob->ctr_x);
315 ctr_y = le16_to_cpu(blob->ctr_y);
317 input_mt_slot(input, slotnum);
318 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
319 wide = (bb_size_x > bb_size_y);
320 major = max(bb_size_x, bb_size_y);
321 minor = min(bb_size_x, bb_size_y);
323 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
324 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
325 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
326 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
328 /* TODO: use a better orientation measure */
329 input_report_abs(input, ABS_MT_ORIENTATION, wide);
330 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
331 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
334 /* core function: poll for new input data */
335 static void sur40_poll(struct input_polled_dev *polldev)
337 struct sur40_state *sur40 = polldev->private;
338 struct input_dev *input = polldev->input;
339 int result, bulk_read, need_blobs, packet_blobs, i;
340 u32 uninitialized_var(packet_id);
342 struct sur40_header *header = &sur40->bulk_in_buffer->header;
343 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
345 dev_dbg(sur40->dev, "poll\n");
347 need_blobs = -1;
349 do {
351 /* perform a blocking bulk read to get data from the device */
352 result = usb_bulk_msg(sur40->usbdev,
353 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
354 sur40->bulk_in_buffer, sur40->bulk_in_size,
355 &bulk_read, 1000);
357 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
359 if (result < 0) {
360 dev_err(sur40->dev, "error in usb_bulk_read\n");
361 return;
364 result = bulk_read - sizeof(struct sur40_header);
366 if (result % sizeof(struct sur40_blob) != 0) {
367 dev_err(sur40->dev, "transfer size mismatch\n");
368 return;
371 /* first packet? */
372 if (need_blobs == -1) {
373 need_blobs = le16_to_cpu(header->count);
374 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
375 packet_id = le32_to_cpu(header->packet_id);
379 * Sanity check. when video data is also being retrieved, the
380 * packet ID will usually increase in the middle of a series
381 * instead of at the end. However, the data is still consistent,
382 * so the packet ID is probably just valid for the first packet
383 * in a series.
385 if (packet_id != le32_to_cpu(header->packet_id))
386 dev_dbg(sur40->dev, "packet ID mismatch\n");
389 packet_blobs = result / sizeof(struct sur40_blob);
390 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
392 /* packets always contain at least 4 blobs, even if empty */
393 if (packet_blobs > need_blobs)
394 packet_blobs = need_blobs;
396 for (i = 0; i < packet_blobs; i++) {
397 need_blobs--;
398 dev_dbg(sur40->dev, "processing blob\n");
399 sur40_report_blob(&(inblob[i]), input);
402 } while (need_blobs > 0);
404 input_mt_sync_frame(input);
405 input_sync(input);
407 sur40_process_video(sur40);
410 /* deal with video data */
411 static void sur40_process_video(struct sur40_state *sur40)
414 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
415 struct sur40_buffer *new_buf;
416 struct usb_sg_request sgr;
417 struct sg_table *sgt;
418 int result, bulk_read;
420 if (!vb2_start_streaming_called(&sur40->queue))
421 return;
423 /* get a new buffer from the list */
424 spin_lock(&sur40->qlock);
425 if (list_empty(&sur40->buf_list)) {
426 dev_dbg(sur40->dev, "buffer queue empty\n");
427 spin_unlock(&sur40->qlock);
428 return;
430 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
431 list_del(&new_buf->list);
432 spin_unlock(&sur40->qlock);
434 dev_dbg(sur40->dev, "buffer acquired\n");
436 /* retrieve data via bulk read */
437 result = usb_bulk_msg(sur40->usbdev,
438 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
439 sur40->bulk_in_buffer, sur40->bulk_in_size,
440 &bulk_read, 1000);
442 if (result < 0) {
443 dev_err(sur40->dev, "error in usb_bulk_read\n");
444 goto err_poll;
447 if (bulk_read != sizeof(struct sur40_image_header)) {
448 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
449 bulk_read, sizeof(struct sur40_image_header));
450 goto err_poll;
453 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
454 dev_err(sur40->dev, "image magic mismatch\n");
455 goto err_poll;
458 if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
459 dev_err(sur40->dev, "image size mismatch\n");
460 goto err_poll;
463 dev_dbg(sur40->dev, "header acquired\n");
465 sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
467 result = usb_sg_init(&sgr, sur40->usbdev,
468 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
469 sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
470 if (result < 0) {
471 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
472 goto err_poll;
475 usb_sg_wait(&sgr);
476 if (sgr.status < 0) {
477 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
478 goto err_poll;
481 dev_dbg(sur40->dev, "image acquired\n");
483 /* return error if streaming was stopped in the meantime */
484 if (sur40->sequence == -1)
485 return;
487 /* mark as finished */
488 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
489 new_buf->vb.sequence = sur40->sequence++;
490 new_buf->vb.field = V4L2_FIELD_NONE;
491 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
492 dev_dbg(sur40->dev, "buffer marked done\n");
493 return;
495 err_poll:
496 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
499 /* Initialize input device parameters. */
500 static void sur40_input_setup(struct input_dev *input_dev)
502 __set_bit(EV_KEY, input_dev->evbit);
503 __set_bit(EV_ABS, input_dev->evbit);
505 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
506 0, SENSOR_RES_X, 0, 0);
507 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
508 0, SENSOR_RES_Y, 0, 0);
510 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
511 0, SENSOR_RES_X, 0, 0);
512 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
513 0, SENSOR_RES_Y, 0, 0);
515 /* max value unknown, but major/minor axis
516 * can never be larger than screen */
517 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
518 0, SENSOR_RES_X, 0, 0);
519 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
520 0, SENSOR_RES_Y, 0, 0);
522 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
524 input_mt_init_slots(input_dev, MAX_CONTACTS,
525 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
528 /* Check candidate USB interface. */
529 static int sur40_probe(struct usb_interface *interface,
530 const struct usb_device_id *id)
532 struct usb_device *usbdev = interface_to_usbdev(interface);
533 struct sur40_state *sur40;
534 struct usb_host_interface *iface_desc;
535 struct usb_endpoint_descriptor *endpoint;
536 struct input_polled_dev *poll_dev;
537 int error;
539 /* Check if we really have the right interface. */
540 iface_desc = &interface->altsetting[0];
541 if (iface_desc->desc.bInterfaceClass != 0xFF)
542 return -ENODEV;
544 if (iface_desc->desc.bNumEndpoints < 5)
545 return -ENODEV;
547 /* Use endpoint #4 (0x86). */
548 endpoint = &iface_desc->endpoint[4].desc;
549 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
550 return -ENODEV;
552 /* Allocate memory for our device state and initialize it. */
553 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
554 if (!sur40)
555 return -ENOMEM;
557 poll_dev = input_allocate_polled_device();
558 if (!poll_dev) {
559 error = -ENOMEM;
560 goto err_free_dev;
563 /* initialize locks/lists */
564 INIT_LIST_HEAD(&sur40->buf_list);
565 spin_lock_init(&sur40->qlock);
566 mutex_init(&sur40->lock);
568 /* Set up polled input device control structure */
569 poll_dev->private = sur40;
570 poll_dev->poll_interval = POLL_INTERVAL;
571 poll_dev->open = sur40_open;
572 poll_dev->poll = sur40_poll;
573 poll_dev->close = sur40_close;
575 /* Set up regular input device structure */
576 sur40_input_setup(poll_dev->input);
578 poll_dev->input->name = DRIVER_LONG;
579 usb_to_input_id(usbdev, &poll_dev->input->id);
580 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
581 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
582 poll_dev->input->phys = sur40->phys;
583 poll_dev->input->dev.parent = &interface->dev;
585 sur40->usbdev = usbdev;
586 sur40->dev = &interface->dev;
587 sur40->input = poll_dev;
589 /* use the bulk-in endpoint tested above */
590 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
591 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
592 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
593 if (!sur40->bulk_in_buffer) {
594 dev_err(&interface->dev, "Unable to allocate input buffer.");
595 error = -ENOMEM;
596 goto err_free_polldev;
599 /* register the polled input device */
600 error = input_register_polled_device(poll_dev);
601 if (error) {
602 dev_err(&interface->dev,
603 "Unable to register polled input device.");
604 goto err_free_buffer;
607 /* register the video master device */
608 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
609 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
610 if (error) {
611 dev_err(&interface->dev,
612 "Unable to register video master device.");
613 goto err_unreg_v4l2;
616 /* initialize the lock and subdevice */
617 sur40->queue = sur40_queue;
618 sur40->queue.drv_priv = sur40;
619 sur40->queue.lock = &sur40->lock;
620 sur40->queue.dev = sur40->dev;
622 /* initialize the queue */
623 error = vb2_queue_init(&sur40->queue);
624 if (error)
625 goto err_unreg_v4l2;
627 sur40->pix_fmt = sur40_pix_format[0];
628 sur40->vdev = sur40_video_device;
629 sur40->vdev.v4l2_dev = &sur40->v4l2;
630 sur40->vdev.lock = &sur40->lock;
631 sur40->vdev.queue = &sur40->queue;
632 video_set_drvdata(&sur40->vdev, sur40);
634 error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
635 if (error) {
636 dev_err(&interface->dev,
637 "Unable to register video subdevice.");
638 goto err_unreg_video;
641 /* we can register the device now, as it is ready */
642 usb_set_intfdata(interface, sur40);
643 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
645 return 0;
647 err_unreg_video:
648 video_unregister_device(&sur40->vdev);
649 err_unreg_v4l2:
650 v4l2_device_unregister(&sur40->v4l2);
651 err_free_buffer:
652 kfree(sur40->bulk_in_buffer);
653 err_free_polldev:
654 input_free_polled_device(sur40->input);
655 err_free_dev:
656 kfree(sur40);
658 return error;
661 /* Unregister device & clean up. */
662 static void sur40_disconnect(struct usb_interface *interface)
664 struct sur40_state *sur40 = usb_get_intfdata(interface);
666 video_unregister_device(&sur40->vdev);
667 v4l2_device_unregister(&sur40->v4l2);
669 input_unregister_polled_device(sur40->input);
670 input_free_polled_device(sur40->input);
671 kfree(sur40->bulk_in_buffer);
672 kfree(sur40);
674 usb_set_intfdata(interface, NULL);
675 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
679 * Setup the constraints of the queue: besides setting the number of planes
680 * per buffer and the size and allocation context of each plane, it also
681 * checks if sufficient buffers have been allocated. Usually 3 is a good
682 * minimum number: many DMA engines need a minimum of 2 buffers in the
683 * queue and you need to have another available for userspace processing.
685 static int sur40_queue_setup(struct vb2_queue *q,
686 unsigned int *nbuffers, unsigned int *nplanes,
687 unsigned int sizes[], struct device *alloc_devs[])
689 struct sur40_state *sur40 = vb2_get_drv_priv(q);
691 if (q->num_buffers + *nbuffers < 3)
692 *nbuffers = 3 - q->num_buffers;
694 if (*nplanes)
695 return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
697 *nplanes = 1;
698 sizes[0] = sur40->pix_fmt.sizeimage;
700 return 0;
704 * Prepare the buffer for queueing to the DMA engine: check and set the
705 * payload size.
707 static int sur40_buffer_prepare(struct vb2_buffer *vb)
709 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
710 unsigned long size = sur40->pix_fmt.sizeimage;
712 if (vb2_plane_size(vb, 0) < size) {
713 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
714 vb2_plane_size(vb, 0), size);
715 return -EINVAL;
718 vb2_set_plane_payload(vb, 0, size);
719 return 0;
723 * Queue this buffer to the DMA engine.
725 static void sur40_buffer_queue(struct vb2_buffer *vb)
727 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
728 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
730 spin_lock(&sur40->qlock);
731 list_add_tail(&buf->list, &sur40->buf_list);
732 spin_unlock(&sur40->qlock);
735 static void return_all_buffers(struct sur40_state *sur40,
736 enum vb2_buffer_state state)
738 struct sur40_buffer *buf, *node;
740 spin_lock(&sur40->qlock);
741 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
742 vb2_buffer_done(&buf->vb.vb2_buf, state);
743 list_del(&buf->list);
745 spin_unlock(&sur40->qlock);
749 * Start streaming. First check if the minimum number of buffers have been
750 * queued. If not, then return -ENOBUFS and the vb2 framework will call
751 * this function again the next time a buffer has been queued until enough
752 * buffers are available to actually start the DMA engine.
754 static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
756 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
758 sur40->sequence = 0;
759 return 0;
763 * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
764 * and passed on to the vb2 framework marked as STATE_ERROR.
766 static void sur40_stop_streaming(struct vb2_queue *vq)
768 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
769 vb2_wait_for_all_buffers(vq);
770 sur40->sequence = -1;
772 /* Release all active buffers */
773 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
776 /* V4L ioctl */
777 static int sur40_vidioc_querycap(struct file *file, void *priv,
778 struct v4l2_capability *cap)
780 struct sur40_state *sur40 = video_drvdata(file);
782 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
783 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
784 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
785 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
786 V4L2_CAP_READWRITE |
787 V4L2_CAP_STREAMING;
788 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
789 return 0;
792 static int sur40_vidioc_enum_input(struct file *file, void *priv,
793 struct v4l2_input *i)
795 if (i->index != 0)
796 return -EINVAL;
797 i->type = V4L2_INPUT_TYPE_TOUCH;
798 i->std = V4L2_STD_UNKNOWN;
799 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
800 i->capabilities = 0;
801 return 0;
804 static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
806 return (i == 0) ? 0 : -EINVAL;
809 static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
811 *i = 0;
812 return 0;
815 static int sur40_vidioc_try_fmt(struct file *file, void *priv,
816 struct v4l2_format *f)
818 switch (f->fmt.pix.pixelformat) {
819 case V4L2_PIX_FMT_GREY:
820 f->fmt.pix = sur40_pix_format[1];
821 break;
823 default:
824 f->fmt.pix = sur40_pix_format[0];
825 break;
828 return 0;
831 static int sur40_vidioc_s_fmt(struct file *file, void *priv,
832 struct v4l2_format *f)
834 struct sur40_state *sur40 = video_drvdata(file);
836 switch (f->fmt.pix.pixelformat) {
837 case V4L2_PIX_FMT_GREY:
838 sur40->pix_fmt = sur40_pix_format[1];
839 break;
841 default:
842 sur40->pix_fmt = sur40_pix_format[0];
843 break;
846 f->fmt.pix = sur40->pix_fmt;
847 return 0;
850 static int sur40_vidioc_g_fmt(struct file *file, void *priv,
851 struct v4l2_format *f)
853 struct sur40_state *sur40 = video_drvdata(file);
855 f->fmt.pix = sur40->pix_fmt;
856 return 0;
859 static int sur40_ioctl_parm(struct file *file, void *priv,
860 struct v4l2_streamparm *p)
862 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
863 return -EINVAL;
865 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
866 p->parm.capture.timeperframe.numerator = 1;
867 p->parm.capture.timeperframe.denominator = 60;
868 p->parm.capture.readbuffers = 3;
869 return 0;
872 static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
873 struct v4l2_fmtdesc *f)
875 if (f->index >= ARRAY_SIZE(sur40_pix_format))
876 return -EINVAL;
878 f->pixelformat = sur40_pix_format[f->index].pixelformat;
879 f->flags = 0;
880 return 0;
883 static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
884 struct v4l2_frmsizeenum *f)
886 struct sur40_state *sur40 = video_drvdata(file);
888 if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
889 && (f->pixel_format != V4L2_PIX_FMT_GREY)))
890 return -EINVAL;
892 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
893 f->discrete.width = sur40->pix_fmt.width;
894 f->discrete.height = sur40->pix_fmt.height;
895 return 0;
898 static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
899 struct v4l2_frmivalenum *f)
901 struct sur40_state *sur40 = video_drvdata(file);
903 if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
904 && (f->pixel_format != V4L2_PIX_FMT_GREY))
905 || (f->width != sur40->pix_fmt.width)
906 || (f->height != sur40->pix_fmt.height))
907 return -EINVAL;
909 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
910 f->discrete.denominator = 60;
911 f->discrete.numerator = 1;
912 return 0;
916 static const struct usb_device_id sur40_table[] = {
917 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
918 { } /* terminating null entry */
920 MODULE_DEVICE_TABLE(usb, sur40_table);
922 /* V4L2 structures */
923 static const struct vb2_ops sur40_queue_ops = {
924 .queue_setup = sur40_queue_setup,
925 .buf_prepare = sur40_buffer_prepare,
926 .buf_queue = sur40_buffer_queue,
927 .start_streaming = sur40_start_streaming,
928 .stop_streaming = sur40_stop_streaming,
929 .wait_prepare = vb2_ops_wait_prepare,
930 .wait_finish = vb2_ops_wait_finish,
933 static const struct vb2_queue sur40_queue = {
934 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
936 * VB2_USERPTR in currently not enabled: passing a user pointer to
937 * dma-sg will result in segment sizes that are not a multiple of
938 * 512 bytes, which is required by the host controller.
940 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
941 .buf_struct_size = sizeof(struct sur40_buffer),
942 .ops = &sur40_queue_ops,
943 .mem_ops = &vb2_dma_sg_memops,
944 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
945 .min_buffers_needed = 3,
948 static const struct v4l2_file_operations sur40_video_fops = {
949 .owner = THIS_MODULE,
950 .open = v4l2_fh_open,
951 .release = vb2_fop_release,
952 .unlocked_ioctl = video_ioctl2,
953 .read = vb2_fop_read,
954 .mmap = vb2_fop_mmap,
955 .poll = vb2_fop_poll,
958 static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
960 .vidioc_querycap = sur40_vidioc_querycap,
962 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
963 .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
964 .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
965 .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
967 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
968 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
970 .vidioc_g_parm = sur40_ioctl_parm,
971 .vidioc_s_parm = sur40_ioctl_parm,
973 .vidioc_enum_input = sur40_vidioc_enum_input,
974 .vidioc_g_input = sur40_vidioc_g_input,
975 .vidioc_s_input = sur40_vidioc_s_input,
977 .vidioc_reqbufs = vb2_ioctl_reqbufs,
978 .vidioc_create_bufs = vb2_ioctl_create_bufs,
979 .vidioc_querybuf = vb2_ioctl_querybuf,
980 .vidioc_qbuf = vb2_ioctl_qbuf,
981 .vidioc_dqbuf = vb2_ioctl_dqbuf,
982 .vidioc_expbuf = vb2_ioctl_expbuf,
984 .vidioc_streamon = vb2_ioctl_streamon,
985 .vidioc_streamoff = vb2_ioctl_streamoff,
988 static const struct video_device sur40_video_device = {
989 .name = DRIVER_LONG,
990 .fops = &sur40_video_fops,
991 .ioctl_ops = &sur40_video_ioctl_ops,
992 .release = video_device_release_empty,
995 /* USB-specific object needed to register this driver with the USB subsystem. */
996 static struct usb_driver sur40_driver = {
997 .name = DRIVER_SHORT,
998 .probe = sur40_probe,
999 .disconnect = sur40_disconnect,
1000 .id_table = sur40_table,
1003 module_usb_driver(sur40_driver);
1005 MODULE_AUTHOR(DRIVER_AUTHOR);
1006 MODULE_DESCRIPTION(DRIVER_DESC);
1007 MODULE_LICENSE("GPL");