1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Surface2.0/SUR40/PixelSense input driver
4  *
5  * Copyright (c) 2014 by Florian 'floe' Echtler <floe@butterbrot.org>
6  *
7  * Derived from the USB Skeleton driver 1.1,
8  * Copyright (c) 2003 Greg Kroah-Hartman (greg@kroah.com)
9  *
10  * and from the Apple USB BCM5974 multitouch driver,
11  * Copyright (c) 2008 Henrik Rydberg (rydberg@euromail.se)
12  *
13  * and from the generic hid-multitouch driver,
14  * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
15  *
16  * and from the v4l2-pci-skeleton driver,
17  * Copyright (c) Copyright 2014 Cisco Systems, Inc.
18  */
19 
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/completion.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/printk.h>
30 #include <linux/input-polldev.h>
31 #include <linux/input/mt.h>
32 #include <linux/usb/input.h>
33 #include <linux/videodev2.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-dev.h>
36 #include <media/v4l2-ioctl.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/videobuf2-v4l2.h>
39 #include <media/videobuf2-dma-sg.h>
40 
41 /* read 512 bytes from endpoint 0x86 -> get header + blobs */
42 struct sur40_header {
43 
44 	__le16 type;       /* always 0x0001 */
45 	__le16 count;      /* count of blobs (if 0: continue prev. packet) */
46 
47 	__le32 packet_id;  /* unique ID for all packets in one frame */
48 
49 	__le32 timestamp;  /* milliseconds (inc. by 16 or 17 each frame) */
50 	__le32 unknown;    /* "epoch?" always 02/03 00 00 00 */
51 
52 } __packed;
53 
54 struct sur40_blob {
55 
56 	__le16 blob_id;
57 
58 	u8 action;         /* 0x02 = enter/exit, 0x03 = update (?) */
59 	u8 type;           /* bitmask (0x01 blob,  0x02 touch, 0x04 tag) */
60 
61 	__le16 bb_pos_x;   /* upper left corner of bounding box */
62 	__le16 bb_pos_y;
63 
64 	__le16 bb_size_x;  /* size of bounding box */
65 	__le16 bb_size_y;
66 
67 	__le16 pos_x;      /* finger tip position */
68 	__le16 pos_y;
69 
70 	__le16 ctr_x;      /* centroid position */
71 	__le16 ctr_y;
72 
73 	__le16 axis_x;     /* somehow related to major/minor axis, mostly: */
74 	__le16 axis_y;     /* axis_x == bb_size_y && axis_y == bb_size_x */
75 
76 	__le32 angle;      /* orientation in radians relative to x axis -
77 	                      actually an IEEE754 float, don't use in kernel */
78 
79 	__le32 area;       /* size in pixels/pressure (?) */
80 
81 	u8 padding[24];
82 
83 	__le32 tag_id;     /* valid when type == 0x04 (SUR40_TAG) */
84 	__le32 unknown;
85 
86 } __packed;
87 
88 /* combined header/blob data */
89 struct sur40_data {
90 	struct sur40_header header;
91 	struct sur40_blob   blobs[];
92 } __packed;
93 
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;
103 
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"
109 
110 /* vendor and device IDs */
111 #define ID_MICROSOFT 0x045e
112 #define ID_SUR40     0x0775
113 
114 /* sensor resolution */
115 #define SENSOR_RES_X 1920
116 #define SENSOR_RES_Y 1080
117 
118 /* touch data endpoint */
119 #define TOUCH_ENDPOINT 0x86
120 
121 /* video data endpoint */
122 #define VIDEO_ENDPOINT 0x82
123 
124 /* video header fields */
125 #define VIDEO_HEADER_MAGIC 0x46425553
126 #define VIDEO_PACKET_SIZE  16384
127 
128 /* polling interval (ms) */
129 #define POLL_INTERVAL 1
130 
131 /* maximum number of contacts FIXME: this is a guess? */
132 #define MAX_CONTACTS 64
133 
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           */
138 
139 #define SUR40_POKE        0xc5 /* poke register byte */
140 #define SUR40_PEEK        0xc4 /* 48 bytes registers */
141 
142 #define SUR40_GET_STATE   0xc5 /*  4 bytes state (?) */
143 #define SUR40_GET_SENSORS 0xb1 /*  8 bytes sensors   */
144 
145 #define SUR40_BLOB	0x01
146 #define SUR40_TOUCH	0x02
147 #define SUR40_TAG	0x04
148 
149 /* video controls */
150 #define SUR40_BRIGHTNESS_MAX 0xff
151 #define SUR40_BRIGHTNESS_MIN 0x00
152 #define SUR40_BRIGHTNESS_DEF 0xff
153 
154 #define SUR40_CONTRAST_MAX 0x0f
155 #define SUR40_CONTRAST_MIN 0x00
156 #define SUR40_CONTRAST_DEF 0x0a
157 
158 #define SUR40_GAIN_MAX 0x09
159 #define SUR40_GAIN_MIN 0x00
160 #define SUR40_GAIN_DEF 0x08
161 
162 #define SUR40_BACKLIGHT_MAX 0x01
163 #define SUR40_BACKLIGHT_MIN 0x00
164 #define SUR40_BACKLIGHT_DEF 0x01
165 
166 #define sur40_str(s) #s
167 #define SUR40_PARAM_RANGE(lo, hi) " (range " sur40_str(lo) "-" sur40_str(hi) ")"
168 
169 /* module parameters */
170 static uint brightness = SUR40_BRIGHTNESS_DEF;
171 module_param(brightness, uint, 0644);
172 MODULE_PARM_DESC(brightness, "set initial brightness"
173 	SUR40_PARAM_RANGE(SUR40_BRIGHTNESS_MIN, SUR40_BRIGHTNESS_MAX));
174 static uint contrast = SUR40_CONTRAST_DEF;
175 module_param(contrast, uint, 0644);
176 MODULE_PARM_DESC(contrast, "set initial contrast"
177 	SUR40_PARAM_RANGE(SUR40_CONTRAST_MIN, SUR40_CONTRAST_MAX));
178 static uint gain = SUR40_GAIN_DEF;
179 module_param(gain, uint, 0644);
180 MODULE_PARM_DESC(gain, "set initial gain"
181 	SUR40_PARAM_RANGE(SUR40_GAIN_MIN, SUR40_GAIN_MAX));
182 
183 static const struct v4l2_pix_format sur40_pix_format[] = {
184 	{
185 		.pixelformat = V4L2_TCH_FMT_TU08,
186 		.width  = SENSOR_RES_X / 2,
187 		.height = SENSOR_RES_Y / 2,
188 		.field = V4L2_FIELD_NONE,
189 		.colorspace = V4L2_COLORSPACE_SRGB,
190 		.bytesperline = SENSOR_RES_X / 2,
191 		.sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
192 	},
193 	{
194 		.pixelformat = V4L2_PIX_FMT_GREY,
195 		.width  = SENSOR_RES_X / 2,
196 		.height = SENSOR_RES_Y / 2,
197 		.field = V4L2_FIELD_NONE,
198 		.colorspace = V4L2_COLORSPACE_SRGB,
199 		.bytesperline = SENSOR_RES_X / 2,
200 		.sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
201 	}
202 };
203 
204 /* master device state */
205 struct sur40_state {
206 
207 	struct usb_device *usbdev;
208 	struct device *dev;
209 	struct input_polled_dev *input;
210 
211 	struct v4l2_device v4l2;
212 	struct video_device vdev;
213 	struct mutex lock;
214 	struct v4l2_pix_format pix_fmt;
215 	struct v4l2_ctrl_handler hdl;
216 
217 	struct vb2_queue queue;
218 	struct list_head buf_list;
219 	spinlock_t qlock;
220 	int sequence;
221 
222 	struct sur40_data *bulk_in_buffer;
223 	size_t bulk_in_size;
224 	u8 bulk_in_epaddr;
225 	u8 vsvideo;
226 
227 	char phys[64];
228 };
229 
230 struct sur40_buffer {
231 	struct vb2_v4l2_buffer vb;
232 	struct list_head list;
233 };
234 
235 /* forward declarations */
236 static const struct video_device sur40_video_device;
237 static const struct vb2_queue sur40_queue;
238 static void sur40_process_video(struct sur40_state *sur40);
239 static int sur40_s_ctrl(struct v4l2_ctrl *ctrl);
240 
241 static const struct v4l2_ctrl_ops sur40_ctrl_ops = {
242 	.s_ctrl = sur40_s_ctrl,
243 };
244 
245 /*
246  * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
247  * here by mistake which is very likely to have corrupted the firmware EEPROM
248  * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
249  * Should you ever run into a similar problem, the background story to this
250  * incident and instructions on how to fix the corrupted EEPROM are available
251  * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
252 */
253 
254 /* command wrapper */
255 static int sur40_command(struct sur40_state *dev,
256 			 u8 command, u16 index, void *buffer, u16 size)
257 {
258 	return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
259 			       command,
260 			       USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
261 			       0x00, index, buffer, size, 1000);
262 }
263 
264 /* poke a byte in the panel register space */
265 static int sur40_poke(struct sur40_state *dev, u8 offset, u8 value)
266 {
267 	int result;
268 	u8 index = 0x96; // 0xae for permanent write
269 
270 	result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
271 		SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
272 		0x32, index, NULL, 0, 1000);
273 	if (result < 0)
274 		goto error;
275 	msleep(5);
276 
277 	result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
278 		SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
279 		0x72, offset, NULL, 0, 1000);
280 	if (result < 0)
281 		goto error;
282 	msleep(5);
283 
284 	result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
285 		SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
286 		0xb2, value, NULL, 0, 1000);
287 	if (result < 0)
288 		goto error;
289 	msleep(5);
290 
291 error:
292 	return result;
293 }
294 
295 static int sur40_set_preprocessor(struct sur40_state *dev, u8 value)
296 {
297 	u8 setting_07[2] = { 0x01, 0x00 };
298 	u8 setting_17[2] = { 0x85, 0x80 };
299 	int result;
300 
301 	if (value > 1)
302 		return -ERANGE;
303 
304 	result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
305 		SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
306 		0x07, setting_07[value], NULL, 0, 1000);
307 	if (result < 0)
308 		goto error;
309 	msleep(5);
310 
311 	result = usb_control_msg(dev->usbdev, usb_sndctrlpipe(dev->usbdev, 0),
312 		SUR40_POKE, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
313 		0x17, setting_17[value], NULL, 0, 1000);
314 	if (result < 0)
315 		goto error;
316 	msleep(5);
317 
318 error:
319 	return result;
320 }
321 
322 static void sur40_set_vsvideo(struct sur40_state *handle, u8 value)
323 {
324 	int i;
325 
326 	for (i = 0; i < 4; i++)
327 		sur40_poke(handle, 0x1c+i, value);
328 	handle->vsvideo = value;
329 }
330 
331 static void sur40_set_irlevel(struct sur40_state *handle, u8 value)
332 {
333 	int i;
334 
335 	for (i = 0; i < 8; i++)
336 		sur40_poke(handle, 0x08+(2*i), value);
337 }
338 
339 /* Initialization routine, called from sur40_open */
340 static int sur40_init(struct sur40_state *dev)
341 {
342 	int result;
343 	u8 *buffer;
344 
345 	buffer = kmalloc(24, GFP_KERNEL);
346 	if (!buffer) {
347 		result = -ENOMEM;
348 		goto error;
349 	}
350 
351 	/* stupidly replay the original MS driver init sequence */
352 	result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
353 	if (result < 0)
354 		goto error;
355 
356 	result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
357 	if (result < 0)
358 		goto error;
359 
360 	result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
361 	if (result < 0)
362 		goto error;
363 
364 	result = sur40_command(dev, SUR40_SENSOR_CAPS, 0x00, buffer, 24);
365 	if (result < 0)
366 		goto error;
367 
368 	result = sur40_command(dev, SUR40_ACCEL_CAPS, 0x00, buffer, 5);
369 	if (result < 0)
370 		goto error;
371 
372 	result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
373 
374 	/*
375 	 * Discard the result buffer - no known data inside except
376 	 * some version strings, maybe extract these sometime...
377 	 */
378 error:
379 	kfree(buffer);
380 	return result;
381 }
382 
383 /*
384  * Callback routines from input_polled_dev
385  */
386 
387 /* Enable the device, polling will now start. */
388 static void sur40_open(struct input_polled_dev *polldev)
389 {
390 	struct sur40_state *sur40 = polldev->private;
391 
392 	dev_dbg(sur40->dev, "open\n");
393 	sur40_init(sur40);
394 }
395 
396 /* Disable device, polling has stopped. */
397 static void sur40_close(struct input_polled_dev *polldev)
398 {
399 	struct sur40_state *sur40 = polldev->private;
400 
401 	dev_dbg(sur40->dev, "close\n");
402 	/*
403 	 * There is no known way to stop the device, so we simply
404 	 * stop polling.
405 	 */
406 }
407 
408 /*
409  * This function is called when a whole contact has been processed,
410  * so that it can assign it to a slot and store the data there.
411  */
412 static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
413 {
414 	int wide, major, minor;
415 	int bb_size_x, bb_size_y, pos_x, pos_y, ctr_x, ctr_y, slotnum;
416 
417 	if (blob->type != SUR40_TOUCH)
418 		return;
419 
420 	slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
421 	if (slotnum < 0 || slotnum >= MAX_CONTACTS)
422 		return;
423 
424 	bb_size_x = le16_to_cpu(blob->bb_size_x);
425 	bb_size_y = le16_to_cpu(blob->bb_size_y);
426 
427 	pos_x = le16_to_cpu(blob->pos_x);
428 	pos_y = le16_to_cpu(blob->pos_y);
429 
430 	ctr_x = le16_to_cpu(blob->ctr_x);
431 	ctr_y = le16_to_cpu(blob->ctr_y);
432 
433 	input_mt_slot(input, slotnum);
434 	input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
435 	wide = (bb_size_x > bb_size_y);
436 	major = max(bb_size_x, bb_size_y);
437 	minor = min(bb_size_x, bb_size_y);
438 
439 	input_report_abs(input, ABS_MT_POSITION_X, pos_x);
440 	input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
441 	input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
442 	input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
443 
444 	/* TODO: use a better orientation measure */
445 	input_report_abs(input, ABS_MT_ORIENTATION, wide);
446 	input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
447 	input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
448 }
449 
450 /* core function: poll for new input data */
451 static void sur40_poll(struct input_polled_dev *polldev)
452 {
453 	struct sur40_state *sur40 = polldev->private;
454 	struct input_dev *input = polldev->input;
455 	int result, bulk_read, need_blobs, packet_blobs, i;
456 	u32 uninitialized_var(packet_id);
457 
458 	struct sur40_header *header = &sur40->bulk_in_buffer->header;
459 	struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
460 
461 	dev_dbg(sur40->dev, "poll\n");
462 
463 	need_blobs = -1;
464 
465 	do {
466 
467 		/* perform a blocking bulk read to get data from the device */
468 		result = usb_bulk_msg(sur40->usbdev,
469 			usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
470 			sur40->bulk_in_buffer, sur40->bulk_in_size,
471 			&bulk_read, 1000);
472 
473 		dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
474 
475 		if (result < 0) {
476 			dev_err(sur40->dev, "error in usb_bulk_read\n");
477 			return;
478 		}
479 
480 		result = bulk_read - sizeof(struct sur40_header);
481 
482 		if (result % sizeof(struct sur40_blob) != 0) {
483 			dev_err(sur40->dev, "transfer size mismatch\n");
484 			return;
485 		}
486 
487 		/* first packet? */
488 		if (need_blobs == -1) {
489 			need_blobs = le16_to_cpu(header->count);
490 			dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
491 			packet_id = le32_to_cpu(header->packet_id);
492 		}
493 
494 		/*
495 		 * Sanity check. when video data is also being retrieved, the
496 		 * packet ID will usually increase in the middle of a series
497 		 * instead of at the end. However, the data is still consistent,
498 		 * so the packet ID is probably just valid for the first packet
499 		 * in a series.
500 
501 		if (packet_id != le32_to_cpu(header->packet_id))
502 			dev_dbg(sur40->dev, "packet ID mismatch\n");
503 		 */
504 
505 		packet_blobs = result / sizeof(struct sur40_blob);
506 		dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
507 
508 		/* packets always contain at least 4 blobs, even if empty */
509 		if (packet_blobs > need_blobs)
510 			packet_blobs = need_blobs;
511 
512 		for (i = 0; i < packet_blobs; i++) {
513 			need_blobs--;
514 			dev_dbg(sur40->dev, "processing blob\n");
515 			sur40_report_blob(&(inblob[i]), input);
516 		}
517 
518 	} while (need_blobs > 0);
519 
520 	input_mt_sync_frame(input);
521 	input_sync(input);
522 
523 	sur40_process_video(sur40);
524 }
525 
526 /* deal with video data */
527 static void sur40_process_video(struct sur40_state *sur40)
528 {
529 
530 	struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
531 	struct sur40_buffer *new_buf;
532 	struct usb_sg_request sgr;
533 	struct sg_table *sgt;
534 	int result, bulk_read;
535 
536 	if (!vb2_start_streaming_called(&sur40->queue))
537 		return;
538 
539 	/* get a new buffer from the list */
540 	spin_lock(&sur40->qlock);
541 	if (list_empty(&sur40->buf_list)) {
542 		dev_dbg(sur40->dev, "buffer queue empty\n");
543 		spin_unlock(&sur40->qlock);
544 		return;
545 	}
546 	new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
547 	list_del(&new_buf->list);
548 	spin_unlock(&sur40->qlock);
549 
550 	dev_dbg(sur40->dev, "buffer acquired\n");
551 
552 	/* retrieve data via bulk read */
553 	result = usb_bulk_msg(sur40->usbdev,
554 			usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
555 			sur40->bulk_in_buffer, sur40->bulk_in_size,
556 			&bulk_read, 1000);
557 
558 	if (result < 0) {
559 		dev_err(sur40->dev, "error in usb_bulk_read\n");
560 		goto err_poll;
561 	}
562 
563 	if (bulk_read != sizeof(struct sur40_image_header)) {
564 		dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
565 			bulk_read, sizeof(struct sur40_image_header));
566 		goto err_poll;
567 	}
568 
569 	if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
570 		dev_err(sur40->dev, "image magic mismatch\n");
571 		goto err_poll;
572 	}
573 
574 	if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
575 		dev_err(sur40->dev, "image size mismatch\n");
576 		goto err_poll;
577 	}
578 
579 	dev_dbg(sur40->dev, "header acquired\n");
580 
581 	sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
582 
583 	result = usb_sg_init(&sgr, sur40->usbdev,
584 		usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
585 		sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
586 	if (result < 0) {
587 		dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
588 		goto err_poll;
589 	}
590 
591 	usb_sg_wait(&sgr);
592 	if (sgr.status < 0) {
593 		dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
594 		goto err_poll;
595 	}
596 
597 	dev_dbg(sur40->dev, "image acquired\n");
598 
599 	/* return error if streaming was stopped in the meantime */
600 	if (sur40->sequence == -1)
601 		return;
602 
603 	/* mark as finished */
604 	new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
605 	new_buf->vb.sequence = sur40->sequence++;
606 	new_buf->vb.field = V4L2_FIELD_NONE;
607 	vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
608 	dev_dbg(sur40->dev, "buffer marked done\n");
609 	return;
610 
611 err_poll:
612 	vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
613 }
614 
615 /* Initialize input device parameters. */
616 static void sur40_input_setup(struct input_dev *input_dev)
617 {
618 	__set_bit(EV_KEY, input_dev->evbit);
619 	__set_bit(EV_ABS, input_dev->evbit);
620 
621 	input_set_abs_params(input_dev, ABS_MT_POSITION_X,
622 			     0, SENSOR_RES_X, 0, 0);
623 	input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
624 			     0, SENSOR_RES_Y, 0, 0);
625 
626 	input_set_abs_params(input_dev, ABS_MT_TOOL_X,
627 			     0, SENSOR_RES_X, 0, 0);
628 	input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
629 			     0, SENSOR_RES_Y, 0, 0);
630 
631 	/* max value unknown, but major/minor axis
632 	 * can never be larger than screen */
633 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
634 			     0, SENSOR_RES_X, 0, 0);
635 	input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
636 			     0, SENSOR_RES_Y, 0, 0);
637 
638 	input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
639 
640 	input_mt_init_slots(input_dev, MAX_CONTACTS,
641 			    INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
642 }
643 
644 /* Check candidate USB interface. */
645 static int sur40_probe(struct usb_interface *interface,
646 		       const struct usb_device_id *id)
647 {
648 	struct usb_device *usbdev = interface_to_usbdev(interface);
649 	struct sur40_state *sur40;
650 	struct usb_host_interface *iface_desc;
651 	struct usb_endpoint_descriptor *endpoint;
652 	struct input_polled_dev *poll_dev;
653 	int error;
654 
655 	/* Check if we really have the right interface. */
656 	iface_desc = &interface->altsetting[0];
657 	if (iface_desc->desc.bInterfaceClass != 0xFF)
658 		return -ENODEV;
659 
660 	if (iface_desc->desc.bNumEndpoints < 5)
661 		return -ENODEV;
662 
663 	/* Use endpoint #4 (0x86). */
664 	endpoint = &iface_desc->endpoint[4].desc;
665 	if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
666 		return -ENODEV;
667 
668 	/* Allocate memory for our device state and initialize it. */
669 	sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
670 	if (!sur40)
671 		return -ENOMEM;
672 
673 	poll_dev = input_allocate_polled_device();
674 	if (!poll_dev) {
675 		error = -ENOMEM;
676 		goto err_free_dev;
677 	}
678 
679 	/* initialize locks/lists */
680 	INIT_LIST_HEAD(&sur40->buf_list);
681 	spin_lock_init(&sur40->qlock);
682 	mutex_init(&sur40->lock);
683 
684 	/* Set up polled input device control structure */
685 	poll_dev->private = sur40;
686 	poll_dev->poll_interval = POLL_INTERVAL;
687 	poll_dev->open = sur40_open;
688 	poll_dev->poll = sur40_poll;
689 	poll_dev->close = sur40_close;
690 
691 	/* Set up regular input device structure */
692 	sur40_input_setup(poll_dev->input);
693 
694 	poll_dev->input->name = DRIVER_LONG;
695 	usb_to_input_id(usbdev, &poll_dev->input->id);
696 	usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
697 	strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
698 	poll_dev->input->phys = sur40->phys;
699 	poll_dev->input->dev.parent = &interface->dev;
700 
701 	sur40->usbdev = usbdev;
702 	sur40->dev = &interface->dev;
703 	sur40->input = poll_dev;
704 
705 	/* use the bulk-in endpoint tested above */
706 	sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
707 	sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
708 	sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
709 	if (!sur40->bulk_in_buffer) {
710 		dev_err(&interface->dev, "Unable to allocate input buffer.");
711 		error = -ENOMEM;
712 		goto err_free_polldev;
713 	}
714 
715 	/* register the polled input device */
716 	error = input_register_polled_device(poll_dev);
717 	if (error) {
718 		dev_err(&interface->dev,
719 			"Unable to register polled input device.");
720 		goto err_free_buffer;
721 	}
722 
723 	/* register the video master device */
724 	snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
725 	error = v4l2_device_register(sur40->dev, &sur40->v4l2);
726 	if (error) {
727 		dev_err(&interface->dev,
728 			"Unable to register video master device.");
729 		goto err_unreg_v4l2;
730 	}
731 
732 	/* initialize the lock and subdevice */
733 	sur40->queue = sur40_queue;
734 	sur40->queue.drv_priv = sur40;
735 	sur40->queue.lock = &sur40->lock;
736 	sur40->queue.dev = sur40->dev;
737 
738 	/* initialize the queue */
739 	error = vb2_queue_init(&sur40->queue);
740 	if (error)
741 		goto err_unreg_v4l2;
742 
743 	sur40->pix_fmt = sur40_pix_format[0];
744 	sur40->vdev = sur40_video_device;
745 	sur40->vdev.v4l2_dev = &sur40->v4l2;
746 	sur40->vdev.lock = &sur40->lock;
747 	sur40->vdev.queue = &sur40->queue;
748 	video_set_drvdata(&sur40->vdev, sur40);
749 
750 	/* initialize the control handler for 4 controls */
751 	v4l2_ctrl_handler_init(&sur40->hdl, 4);
752 	sur40->v4l2.ctrl_handler = &sur40->hdl;
753 	sur40->vsvideo = (SUR40_CONTRAST_DEF << 4) | SUR40_GAIN_DEF;
754 
755 	v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_BRIGHTNESS,
756 	  SUR40_BRIGHTNESS_MIN, SUR40_BRIGHTNESS_MAX, 1, clamp(brightness,
757 	  (uint)SUR40_BRIGHTNESS_MIN, (uint)SUR40_BRIGHTNESS_MAX));
758 
759 	v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_CONTRAST,
760 	  SUR40_CONTRAST_MIN, SUR40_CONTRAST_MAX, 1, clamp(contrast,
761 	  (uint)SUR40_CONTRAST_MIN, (uint)SUR40_CONTRAST_MAX));
762 
763 	v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops, V4L2_CID_GAIN,
764 	  SUR40_GAIN_MIN, SUR40_GAIN_MAX, 1, clamp(gain,
765 	  (uint)SUR40_GAIN_MIN, (uint)SUR40_GAIN_MAX));
766 
767 	v4l2_ctrl_new_std(&sur40->hdl, &sur40_ctrl_ops,
768 	  V4L2_CID_BACKLIGHT_COMPENSATION, SUR40_BACKLIGHT_MIN,
769 	  SUR40_BACKLIGHT_MAX, 1, SUR40_BACKLIGHT_DEF);
770 
771 	v4l2_ctrl_handler_setup(&sur40->hdl);
772 
773 	if (sur40->hdl.error) {
774 		dev_err(&interface->dev,
775 			"Unable to register video controls.");
776 		v4l2_ctrl_handler_free(&sur40->hdl);
777 		goto err_unreg_v4l2;
778 	}
779 
780 	error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
781 	if (error) {
782 		dev_err(&interface->dev,
783 			"Unable to register video subdevice.");
784 		goto err_unreg_video;
785 	}
786 
787 	/* we can register the device now, as it is ready */
788 	usb_set_intfdata(interface, sur40);
789 	dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
790 
791 	return 0;
792 
793 err_unreg_video:
794 	video_unregister_device(&sur40->vdev);
795 err_unreg_v4l2:
796 	v4l2_device_unregister(&sur40->v4l2);
797 err_free_buffer:
798 	kfree(sur40->bulk_in_buffer);
799 err_free_polldev:
800 	input_free_polled_device(sur40->input);
801 err_free_dev:
802 	kfree(sur40);
803 
804 	return error;
805 }
806 
807 /* Unregister device & clean up. */
808 static void sur40_disconnect(struct usb_interface *interface)
809 {
810 	struct sur40_state *sur40 = usb_get_intfdata(interface);
811 
812 	v4l2_ctrl_handler_free(&sur40->hdl);
813 	video_unregister_device(&sur40->vdev);
814 	v4l2_device_unregister(&sur40->v4l2);
815 
816 	input_unregister_polled_device(sur40->input);
817 	input_free_polled_device(sur40->input);
818 	kfree(sur40->bulk_in_buffer);
819 	kfree(sur40);
820 
821 	usb_set_intfdata(interface, NULL);
822 	dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
823 }
824 
825 /*
826  * Setup the constraints of the queue: besides setting the number of planes
827  * per buffer and the size and allocation context of each plane, it also
828  * checks if sufficient buffers have been allocated. Usually 3 is a good
829  * minimum number: many DMA engines need a minimum of 2 buffers in the
830  * queue and you need to have another available for userspace processing.
831  */
832 static int sur40_queue_setup(struct vb2_queue *q,
833 		       unsigned int *nbuffers, unsigned int *nplanes,
834 		       unsigned int sizes[], struct device *alloc_devs[])
835 {
836 	struct sur40_state *sur40 = vb2_get_drv_priv(q);
837 
838 	if (q->num_buffers + *nbuffers < 3)
839 		*nbuffers = 3 - q->num_buffers;
840 
841 	if (*nplanes)
842 		return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
843 
844 	*nplanes = 1;
845 	sizes[0] = sur40->pix_fmt.sizeimage;
846 
847 	return 0;
848 }
849 
850 /*
851  * Prepare the buffer for queueing to the DMA engine: check and set the
852  * payload size.
853  */
854 static int sur40_buffer_prepare(struct vb2_buffer *vb)
855 {
856 	struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
857 	unsigned long size = sur40->pix_fmt.sizeimage;
858 
859 	if (vb2_plane_size(vb, 0) < size) {
860 		dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
861 			 vb2_plane_size(vb, 0), size);
862 		return -EINVAL;
863 	}
864 
865 	vb2_set_plane_payload(vb, 0, size);
866 	return 0;
867 }
868 
869 /*
870  * Queue this buffer to the DMA engine.
871  */
872 static void sur40_buffer_queue(struct vb2_buffer *vb)
873 {
874 	struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
875 	struct sur40_buffer *buf = (struct sur40_buffer *)vb;
876 
877 	spin_lock(&sur40->qlock);
878 	list_add_tail(&buf->list, &sur40->buf_list);
879 	spin_unlock(&sur40->qlock);
880 }
881 
882 static void return_all_buffers(struct sur40_state *sur40,
883 			       enum vb2_buffer_state state)
884 {
885 	struct sur40_buffer *buf, *node;
886 
887 	spin_lock(&sur40->qlock);
888 	list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
889 		vb2_buffer_done(&buf->vb.vb2_buf, state);
890 		list_del(&buf->list);
891 	}
892 	spin_unlock(&sur40->qlock);
893 }
894 
895 /*
896  * Start streaming. First check if the minimum number of buffers have been
897  * queued. If not, then return -ENOBUFS and the vb2 framework will call
898  * this function again the next time a buffer has been queued until enough
899  * buffers are available to actually start the DMA engine.
900  */
901 static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
902 {
903 	struct sur40_state *sur40 = vb2_get_drv_priv(vq);
904 
905 	sur40->sequence = 0;
906 	return 0;
907 }
908 
909 /*
910  * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
911  * and passed on to the vb2 framework marked as STATE_ERROR.
912  */
913 static void sur40_stop_streaming(struct vb2_queue *vq)
914 {
915 	struct sur40_state *sur40 = vb2_get_drv_priv(vq);
916 	vb2_wait_for_all_buffers(vq);
917 	sur40->sequence = -1;
918 
919 	/* Release all active buffers */
920 	return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
921 }
922 
923 /* V4L ioctl */
924 static int sur40_vidioc_querycap(struct file *file, void *priv,
925 				 struct v4l2_capability *cap)
926 {
927 	struct sur40_state *sur40 = video_drvdata(file);
928 
929 	strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
930 	strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
931 	usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
932 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
933 		V4L2_CAP_READWRITE |
934 		V4L2_CAP_STREAMING;
935 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
936 	return 0;
937 }
938 
939 static int sur40_vidioc_enum_input(struct file *file, void *priv,
940 				   struct v4l2_input *i)
941 {
942 	if (i->index != 0)
943 		return -EINVAL;
944 	i->type = V4L2_INPUT_TYPE_TOUCH;
945 	i->std = V4L2_STD_UNKNOWN;
946 	strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
947 	i->capabilities = 0;
948 	return 0;
949 }
950 
951 static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
952 {
953 	return (i == 0) ? 0 : -EINVAL;
954 }
955 
956 static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
957 {
958 	*i = 0;
959 	return 0;
960 }
961 
962 static int sur40_vidioc_try_fmt(struct file *file, void *priv,
963 			    struct v4l2_format *f)
964 {
965 	switch (f->fmt.pix.pixelformat) {
966 	case V4L2_PIX_FMT_GREY:
967 		f->fmt.pix = sur40_pix_format[1];
968 		break;
969 
970 	default:
971 		f->fmt.pix = sur40_pix_format[0];
972 		break;
973 	}
974 
975 	return 0;
976 }
977 
978 static int sur40_vidioc_s_fmt(struct file *file, void *priv,
979 			    struct v4l2_format *f)
980 {
981 	struct sur40_state *sur40 = video_drvdata(file);
982 
983 	switch (f->fmt.pix.pixelformat) {
984 	case V4L2_PIX_FMT_GREY:
985 		sur40->pix_fmt = sur40_pix_format[1];
986 		break;
987 
988 	default:
989 		sur40->pix_fmt = sur40_pix_format[0];
990 		break;
991 	}
992 
993 	f->fmt.pix = sur40->pix_fmt;
994 	return 0;
995 }
996 
997 static int sur40_vidioc_g_fmt(struct file *file, void *priv,
998 			    struct v4l2_format *f)
999 {
1000 	struct sur40_state *sur40 = video_drvdata(file);
1001 
1002 	f->fmt.pix = sur40->pix_fmt;
1003 	return 0;
1004 }
1005 
1006 static int sur40_s_ctrl(struct v4l2_ctrl *ctrl)
1007 {
1008 	struct sur40_state *sur40  = container_of(ctrl->handler,
1009 	  struct sur40_state, hdl);
1010 	u8 value = sur40->vsvideo;
1011 
1012 	switch (ctrl->id) {
1013 	case V4L2_CID_BRIGHTNESS:
1014 		sur40_set_irlevel(sur40, ctrl->val);
1015 		break;
1016 	case V4L2_CID_CONTRAST:
1017 		value = (value & 0x0f) | (ctrl->val << 4);
1018 		sur40_set_vsvideo(sur40, value);
1019 		break;
1020 	case V4L2_CID_GAIN:
1021 		value = (value & 0xf0) | (ctrl->val);
1022 		sur40_set_vsvideo(sur40, value);
1023 		break;
1024 	case V4L2_CID_BACKLIGHT_COMPENSATION:
1025 		sur40_set_preprocessor(sur40, ctrl->val);
1026 		break;
1027 	}
1028 	return 0;
1029 }
1030 
1031 static int sur40_ioctl_parm(struct file *file, void *priv,
1032 			    struct v4l2_streamparm *p)
1033 {
1034 	if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1035 		return -EINVAL;
1036 
1037 	p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1038 	p->parm.capture.timeperframe.numerator = 1;
1039 	p->parm.capture.timeperframe.denominator = 60;
1040 	p->parm.capture.readbuffers = 3;
1041 	return 0;
1042 }
1043 
1044 static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
1045 				 struct v4l2_fmtdesc *f)
1046 {
1047 	if (f->index >= ARRAY_SIZE(sur40_pix_format))
1048 		return -EINVAL;
1049 
1050 	f->pixelformat = sur40_pix_format[f->index].pixelformat;
1051 	f->flags = 0;
1052 	return 0;
1053 }
1054 
1055 static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
1056 					struct v4l2_frmsizeenum *f)
1057 {
1058 	struct sur40_state *sur40 = video_drvdata(file);
1059 
1060 	if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
1061 		&& (f->pixel_format != V4L2_PIX_FMT_GREY)))
1062 		return -EINVAL;
1063 
1064 	f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1065 	f->discrete.width  = sur40->pix_fmt.width;
1066 	f->discrete.height = sur40->pix_fmt.height;
1067 	return 0;
1068 }
1069 
1070 static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
1071 					    struct v4l2_frmivalenum *f)
1072 {
1073 	struct sur40_state *sur40 = video_drvdata(file);
1074 
1075 	if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
1076 		&& (f->pixel_format != V4L2_PIX_FMT_GREY))
1077 		|| (f->width  != sur40->pix_fmt.width)
1078 		|| (f->height != sur40->pix_fmt.height))
1079 		return -EINVAL;
1080 
1081 	f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1082 	f->discrete.denominator  = 60;
1083 	f->discrete.numerator = 1;
1084 	return 0;
1085 }
1086 
1087 
1088 static const struct usb_device_id sur40_table[] = {
1089 	{ USB_DEVICE(ID_MICROSOFT, ID_SUR40) },  /* Samsung SUR40 */
1090 	{ }                                      /* terminating null entry */
1091 };
1092 MODULE_DEVICE_TABLE(usb, sur40_table);
1093 
1094 /* V4L2 structures */
1095 static const struct vb2_ops sur40_queue_ops = {
1096 	.queue_setup		= sur40_queue_setup,
1097 	.buf_prepare		= sur40_buffer_prepare,
1098 	.buf_queue		= sur40_buffer_queue,
1099 	.start_streaming	= sur40_start_streaming,
1100 	.stop_streaming		= sur40_stop_streaming,
1101 	.wait_prepare		= vb2_ops_wait_prepare,
1102 	.wait_finish		= vb2_ops_wait_finish,
1103 };
1104 
1105 static const struct vb2_queue sur40_queue = {
1106 	.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1107 	/*
1108 	 * VB2_USERPTR in currently not enabled: passing a user pointer to
1109 	 * dma-sg will result in segment sizes that are not a multiple of
1110 	 * 512 bytes, which is required by the host controller.
1111 	*/
1112 	.io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
1113 	.buf_struct_size = sizeof(struct sur40_buffer),
1114 	.ops = &sur40_queue_ops,
1115 	.mem_ops = &vb2_dma_sg_memops,
1116 	.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
1117 	.min_buffers_needed = 3,
1118 };
1119 
1120 static const struct v4l2_file_operations sur40_video_fops = {
1121 	.owner = THIS_MODULE,
1122 	.open = v4l2_fh_open,
1123 	.release = vb2_fop_release,
1124 	.unlocked_ioctl = video_ioctl2,
1125 	.read = vb2_fop_read,
1126 	.mmap = vb2_fop_mmap,
1127 	.poll = vb2_fop_poll,
1128 };
1129 
1130 static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
1131 
1132 	.vidioc_querycap	= sur40_vidioc_querycap,
1133 
1134 	.vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
1135 	.vidioc_try_fmt_vid_cap	= sur40_vidioc_try_fmt,
1136 	.vidioc_s_fmt_vid_cap	= sur40_vidioc_s_fmt,
1137 	.vidioc_g_fmt_vid_cap	= sur40_vidioc_g_fmt,
1138 
1139 	.vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
1140 	.vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
1141 
1142 	.vidioc_g_parm = sur40_ioctl_parm,
1143 	.vidioc_s_parm = sur40_ioctl_parm,
1144 
1145 	.vidioc_enum_input	= sur40_vidioc_enum_input,
1146 	.vidioc_g_input		= sur40_vidioc_g_input,
1147 	.vidioc_s_input		= sur40_vidioc_s_input,
1148 
1149 	.vidioc_reqbufs		= vb2_ioctl_reqbufs,
1150 	.vidioc_create_bufs	= vb2_ioctl_create_bufs,
1151 	.vidioc_querybuf	= vb2_ioctl_querybuf,
1152 	.vidioc_qbuf		= vb2_ioctl_qbuf,
1153 	.vidioc_dqbuf		= vb2_ioctl_dqbuf,
1154 	.vidioc_expbuf		= vb2_ioctl_expbuf,
1155 
1156 	.vidioc_streamon	= vb2_ioctl_streamon,
1157 	.vidioc_streamoff	= vb2_ioctl_streamoff,
1158 };
1159 
1160 static const struct video_device sur40_video_device = {
1161 	.name = DRIVER_LONG,
1162 	.fops = &sur40_video_fops,
1163 	.ioctl_ops = &sur40_video_ioctl_ops,
1164 	.release = video_device_release_empty,
1165 };
1166 
1167 /* USB-specific object needed to register this driver with the USB subsystem. */
1168 static struct usb_driver sur40_driver = {
1169 	.name = DRIVER_SHORT,
1170 	.probe = sur40_probe,
1171 	.disconnect = sur40_disconnect,
1172 	.id_table = sur40_table,
1173 };
1174 
1175 module_usb_driver(sur40_driver);
1176 
1177 MODULE_AUTHOR(DRIVER_AUTHOR);
1178 MODULE_DESCRIPTION(DRIVER_DESC);
1179 MODULE_LICENSE("GPL");
1180