blob: f04adeaa45b3cbe766f4a2c2bf2c52a7e1960e2f [file] [log] [blame]
Florian Echtlerbdb5c572013-11-08 10:01:13 -08001/*
2 * Surface2.0/SUR40/PixelSense input driver
3 *
Florian Echtlere831cd22015-03-16 06:48:23 -03004 * Copyright (c) 2014 by Florian 'floe' Echtler <floe@butterbrot.org>
Florian Echtlerbdb5c572013-11-08 10:01:13 -08005 *
6 * Derived from the USB Skeleton driver 1.1,
7 * Copyright (c) 2003 Greg Kroah-Hartman (greg@kroah.com)
8 *
9 * and from the Apple USB BCM5974 multitouch driver,
10 * Copyright (c) 2008 Henrik Rydberg (rydberg@euromail.se)
11 *
12 * and from the generic hid-multitouch driver,
13 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
14 *
Florian Echtlere831cd22015-03-16 06:48:23 -030015 * and from the v4l2-pci-skeleton driver,
16 * Copyright (c) Copyright 2014 Cisco Systems, Inc.
17 *
Florian Echtlerbdb5c572013-11-08 10:01:13 -080018 * 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.
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>
Florian Echtlere831cd22015-03-16 06:48:23 -030037#include <linux/videodev2.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-dev.h>
40#include <media/v4l2-ioctl.h>
Junghak Sung2d700712015-09-22 10:30:30 -030041#include <media/videobuf2-v4l2.h>
Florian Echtlere831cd22015-03-16 06:48:23 -030042#include <media/videobuf2-dma-sg.h>
Florian Echtlerbdb5c572013-11-08 10:01:13 -080043
44/* read 512 bytes from endpoint 0x86 -> get header + blobs */
45struct sur40_header {
46
47 __le16 type; /* always 0x0001 */
48 __le16 count; /* count of blobs (if 0: continue prev. packet) */
49
50 __le32 packet_id; /* unique ID for all packets in one frame */
51
52 __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
53 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
54
55} __packed;
56
57struct sur40_blob {
58
59 __le16 blob_id;
60
61 u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
Florian Echtler435915e2017-07-10 10:39:27 -070062 u8 type; /* bitmask (0x01 blob, 0x02 touch, 0x04 tag) */
Florian Echtlerbdb5c572013-11-08 10:01:13 -080063
64 __le16 bb_pos_x; /* upper left corner of bounding box */
65 __le16 bb_pos_y;
66
67 __le16 bb_size_x; /* size of bounding box */
68 __le16 bb_size_y;
69
70 __le16 pos_x; /* finger tip position */
71 __le16 pos_y;
72
73 __le16 ctr_x; /* centroid position */
74 __le16 ctr_y;
75
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 */
78
79 __le32 angle; /* orientation in radians relative to x axis -
80 actually an IEEE754 float, don't use in kernel */
81
82 __le32 area; /* size in pixels/pressure (?) */
83
84 u8 padding[32];
85
86} __packed;
87
88/* combined header/blob data */
89struct sur40_data {
90 struct sur40_header header;
91 struct sur40_blob blobs[];
92} __packed;
93
Florian Echtlere831cd22015-03-16 06:48:23 -030094/* read 512 bytes from endpoint 0x82 -> get header below
95 * continue reading 16k blocks until header.size bytes read */
96struct 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;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800103
104/* version information */
105#define DRIVER_SHORT "sur40"
Florian Echtlere831cd22015-03-16 06:48:23 -0300106#define DRIVER_LONG "Samsung SUR40"
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800107#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
Florian Echtlere831cd22015-03-16 06:48:23 -0300121/* 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
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800128/* polling interval (ms) */
Florian Echtleraf766ee2016-05-31 17:15:32 -0300129#define POLL_INTERVAL 1
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800130
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 */
Florian Echtler435915e2017-07-10 10:39:27 -0700136#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 */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800141
142#define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
143#define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
144
Florian Echtler435915e2017-07-10 10:39:27 -0700145#define SUR40_BLOB 0x01
146#define SUR40_TOUCH 0x02
147#define SUR40_TAG 0x04
148
Nick Dyerb4972652016-08-22 06:28:23 -0300149static const struct v4l2_pix_format sur40_pix_format[] = {
150 {
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),
158 },
159 {
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),
167 }
168};
169
Florian Echtlere831cd22015-03-16 06:48:23 -0300170/* master device state */
171struct sur40_state {
172
173 struct usb_device *usbdev;
174 struct device *dev;
175 struct input_polled_dev *input;
176
177 struct v4l2_device v4l2;
178 struct video_device vdev;
179 struct mutex lock;
Nick Dyerb4972652016-08-22 06:28:23 -0300180 struct v4l2_pix_format pix_fmt;
Florian Echtlere831cd22015-03-16 06:48:23 -0300181
182 struct vb2_queue queue;
Florian Echtlere831cd22015-03-16 06:48:23 -0300183 struct list_head buf_list;
184 spinlock_t qlock;
185 int sequence;
186
187 struct sur40_data *bulk_in_buffer;
188 size_t bulk_in_size;
189 u8 bulk_in_epaddr;
190
191 char phys[64];
192};
193
194struct sur40_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -0300195 struct vb2_v4l2_buffer vb;
Florian Echtlere831cd22015-03-16 06:48:23 -0300196 struct list_head list;
197};
198
199/* forward declarations */
200static const struct video_device sur40_video_device;
Florian Echtlere831cd22015-03-16 06:48:23 -0300201static const struct vb2_queue sur40_queue;
202static void sur40_process_video(struct sur40_state *sur40);
203
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800204/*
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
211*/
212
Florian Echtlere831cd22015-03-16 06:48:23 -0300213/* command wrapper */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800214static int sur40_command(struct sur40_state *dev,
215 u8 command, u16 index, void *buffer, u16 size)
216{
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);
221}
222
223/* Initialization routine, called from sur40_open */
224static int sur40_init(struct sur40_state *dev)
225{
226 int result;
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700227 u8 *buffer;
228
229 buffer = kmalloc(24, GFP_KERNEL);
230 if (!buffer) {
231 result = -ENOMEM;
232 goto error;
233 }
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800234
235 /* stupidly replay the original MS driver init sequence */
236 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
237 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700238 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800239
240 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
241 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700242 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800243
244 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
245 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700246 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800247
Florian Echtler435915e2017-07-10 10:39:27 -0700248 result = sur40_command(dev, SUR40_SENSOR_CAPS, 0x00, buffer, 24);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800249 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700250 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800251
Florian Echtler435915e2017-07-10 10:39:27 -0700252 result = sur40_command(dev, SUR40_ACCEL_CAPS, 0x00, buffer, 5);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800253 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700254 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800255
256 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
257
258 /*
259 * Discard the result buffer - no known data inside except
260 * some version strings, maybe extract these sometime...
261 */
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700262error:
263 kfree(buffer);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800264 return result;
265}
266
267/*
268 * Callback routines from input_polled_dev
269 */
270
271/* Enable the device, polling will now start. */
272static void sur40_open(struct input_polled_dev *polldev)
273{
274 struct sur40_state *sur40 = polldev->private;
275
276 dev_dbg(sur40->dev, "open\n");
277 sur40_init(sur40);
278}
279
280/* Disable device, polling has stopped. */
281static void sur40_close(struct input_polled_dev *polldev)
282{
283 struct sur40_state *sur40 = polldev->private;
284
285 dev_dbg(sur40->dev, "close\n");
286 /*
287 * There is no known way to stop the device, so we simply
288 * stop polling.
289 */
290}
291
292/*
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.
295 */
296static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
297{
298 int wide, major, minor;
299
300 int bb_size_x = le16_to_cpu(blob->bb_size_x);
301 int bb_size_y = le16_to_cpu(blob->bb_size_y);
302
303 int pos_x = le16_to_cpu(blob->pos_x);
304 int pos_y = le16_to_cpu(blob->pos_y);
305
306 int ctr_x = le16_to_cpu(blob->ctr_x);
307 int ctr_y = le16_to_cpu(blob->ctr_y);
308
309 int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
310 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
311 return;
312
313 input_mt_slot(input, slotnum);
314 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
315 wide = (bb_size_x > bb_size_y);
316 major = max(bb_size_x, bb_size_y);
317 minor = min(bb_size_x, bb_size_y);
318
319 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
320 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
321 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
322 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
323
324 /* TODO: use a better orientation measure */
325 input_report_abs(input, ABS_MT_ORIENTATION, wide);
326 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
327 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
328}
329
330/* core function: poll for new input data */
331static void sur40_poll(struct input_polled_dev *polldev)
332{
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800333 struct sur40_state *sur40 = polldev->private;
334 struct input_dev *input = polldev->input;
335 int result, bulk_read, need_blobs, packet_blobs, i;
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800336 u32 uninitialized_var(packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800337
338 struct sur40_header *header = &sur40->bulk_in_buffer->header;
339 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
340
341 dev_dbg(sur40->dev, "poll\n");
342
343 need_blobs = -1;
344
345 do {
346
347 /* perform a blocking bulk read to get data from the device */
348 result = usb_bulk_msg(sur40->usbdev,
349 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
350 sur40->bulk_in_buffer, sur40->bulk_in_size,
351 &bulk_read, 1000);
352
353 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
354
355 if (result < 0) {
356 dev_err(sur40->dev, "error in usb_bulk_read\n");
357 return;
358 }
359
360 result = bulk_read - sizeof(struct sur40_header);
361
362 if (result % sizeof(struct sur40_blob) != 0) {
363 dev_err(sur40->dev, "transfer size mismatch\n");
364 return;
365 }
366
367 /* first packet? */
368 if (need_blobs == -1) {
369 need_blobs = le16_to_cpu(header->count);
370 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800371 packet_id = le32_to_cpu(header->packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800372 }
373
374 /*
375 * Sanity check. when video data is also being retrieved, the
376 * packet ID will usually increase in the middle of a series
Florian Echtler335abae2017-07-10 10:39:48 -0700377 * instead of at the end. However, the data is still consistent,
378 * so the packet ID is probably just valid for the first packet
379 * in a series.
380
Martin Kepplingerdd04dc62017-04-04 10:06:57 -0700381 if (packet_id != le32_to_cpu(header->packet_id))
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300382 dev_dbg(sur40->dev, "packet ID mismatch\n");
Florian Echtler335abae2017-07-10 10:39:48 -0700383 */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800384
385 packet_blobs = result / sizeof(struct sur40_blob);
386 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
387
388 /* packets always contain at least 4 blobs, even if empty */
389 if (packet_blobs > need_blobs)
390 packet_blobs = need_blobs;
391
392 for (i = 0; i < packet_blobs; i++) {
393 need_blobs--;
394 dev_dbg(sur40->dev, "processing blob\n");
395 sur40_report_blob(&(inblob[i]), input);
396 }
397
398 } while (need_blobs > 0);
399
400 input_mt_sync_frame(input);
401 input_sync(input);
Florian Echtlere831cd22015-03-16 06:48:23 -0300402
403 sur40_process_video(sur40);
404}
405
406/* deal with video data */
407static void sur40_process_video(struct sur40_state *sur40)
408{
409
410 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
411 struct sur40_buffer *new_buf;
412 struct usb_sg_request sgr;
413 struct sg_table *sgt;
414 int result, bulk_read;
415
416 if (!vb2_start_streaming_called(&sur40->queue))
417 return;
418
419 /* get a new buffer from the list */
420 spin_lock(&sur40->qlock);
Florian Echtlerc2529902015-03-31 06:43:28 -0300421 if (list_empty(&sur40->buf_list)) {
422 dev_dbg(sur40->dev, "buffer queue empty\n");
423 spin_unlock(&sur40->qlock);
424 return;
425 }
Florian Echtlere831cd22015-03-16 06:48:23 -0300426 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
427 list_del(&new_buf->list);
428 spin_unlock(&sur40->qlock);
429
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300430 dev_dbg(sur40->dev, "buffer acquired\n");
431
Florian Echtlere831cd22015-03-16 06:48:23 -0300432 /* retrieve data via bulk read */
433 result = usb_bulk_msg(sur40->usbdev,
434 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
435 sur40->bulk_in_buffer, sur40->bulk_in_size,
436 &bulk_read, 1000);
437
438 if (result < 0) {
439 dev_err(sur40->dev, "error in usb_bulk_read\n");
440 goto err_poll;
441 }
442
443 if (bulk_read != sizeof(struct sur40_image_header)) {
444 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
445 bulk_read, sizeof(struct sur40_image_header));
446 goto err_poll;
447 }
448
449 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
450 dev_err(sur40->dev, "image magic mismatch\n");
451 goto err_poll;
452 }
453
Nick Dyerb4972652016-08-22 06:28:23 -0300454 if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
Florian Echtlere831cd22015-03-16 06:48:23 -0300455 dev_err(sur40->dev, "image size mismatch\n");
456 goto err_poll;
457 }
458
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300459 dev_dbg(sur40->dev, "header acquired\n");
460
Junghak Sung2d700712015-09-22 10:30:30 -0300461 sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
Florian Echtlere831cd22015-03-16 06:48:23 -0300462
463 result = usb_sg_init(&sgr, sur40->usbdev,
464 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
Nick Dyerb4972652016-08-22 06:28:23 -0300465 sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
Florian Echtlere831cd22015-03-16 06:48:23 -0300466 if (result < 0) {
467 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
468 goto err_poll;
469 }
470
471 usb_sg_wait(&sgr);
472 if (sgr.status < 0) {
473 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
474 goto err_poll;
475 }
476
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300477 dev_dbg(sur40->dev, "image acquired\n");
478
Florian Echtler2b7eea82015-05-25 09:04:16 -0300479 /* return error if streaming was stopped in the meantime */
480 if (sur40->sequence == -1)
Florian Echtler6a858812016-05-31 17:15:33 -0300481 return;
Florian Echtler2b7eea82015-05-25 09:04:16 -0300482
Florian Echtlere831cd22015-03-16 06:48:23 -0300483 /* mark as finished */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200484 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300485 new_buf->vb.sequence = sur40->sequence++;
486 new_buf->vb.field = V4L2_FIELD_NONE;
487 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300488 dev_dbg(sur40->dev, "buffer marked done\n");
Florian Echtlere831cd22015-03-16 06:48:23 -0300489 return;
490
491err_poll:
Junghak Sung2d700712015-09-22 10:30:30 -0300492 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800493}
494
495/* Initialize input device parameters. */
496static void sur40_input_setup(struct input_dev *input_dev)
497{
498 __set_bit(EV_KEY, input_dev->evbit);
499 __set_bit(EV_ABS, input_dev->evbit);
500
501 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
502 0, SENSOR_RES_X, 0, 0);
503 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
504 0, SENSOR_RES_Y, 0, 0);
505
506 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
507 0, SENSOR_RES_X, 0, 0);
508 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
509 0, SENSOR_RES_Y, 0, 0);
510
511 /* max value unknown, but major/minor axis
512 * can never be larger than screen */
513 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
514 0, SENSOR_RES_X, 0, 0);
515 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
516 0, SENSOR_RES_Y, 0, 0);
517
518 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
519
520 input_mt_init_slots(input_dev, MAX_CONTACTS,
521 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
522}
523
524/* Check candidate USB interface. */
525static int sur40_probe(struct usb_interface *interface,
526 const struct usb_device_id *id)
527{
528 struct usb_device *usbdev = interface_to_usbdev(interface);
529 struct sur40_state *sur40;
530 struct usb_host_interface *iface_desc;
531 struct usb_endpoint_descriptor *endpoint;
532 struct input_polled_dev *poll_dev;
533 int error;
534
535 /* Check if we really have the right interface. */
536 iface_desc = &interface->altsetting[0];
537 if (iface_desc->desc.bInterfaceClass != 0xFF)
538 return -ENODEV;
539
Johan Hovold92461f52017-03-16 11:43:09 -0700540 if (iface_desc->desc.bNumEndpoints < 5)
541 return -ENODEV;
542
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800543 /* Use endpoint #4 (0x86). */
544 endpoint = &iface_desc->endpoint[4].desc;
545 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
546 return -ENODEV;
547
548 /* Allocate memory for our device state and initialize it. */
549 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
550 if (!sur40)
551 return -ENOMEM;
552
553 poll_dev = input_allocate_polled_device();
554 if (!poll_dev) {
555 error = -ENOMEM;
556 goto err_free_dev;
557 }
558
Florian Echtlere831cd22015-03-16 06:48:23 -0300559 /* initialize locks/lists */
560 INIT_LIST_HEAD(&sur40->buf_list);
561 spin_lock_init(&sur40->qlock);
562 mutex_init(&sur40->lock);
563
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800564 /* Set up polled input device control structure */
565 poll_dev->private = sur40;
566 poll_dev->poll_interval = POLL_INTERVAL;
567 poll_dev->open = sur40_open;
568 poll_dev->poll = sur40_poll;
569 poll_dev->close = sur40_close;
570
571 /* Set up regular input device structure */
572 sur40_input_setup(poll_dev->input);
573
Florian Echtlere831cd22015-03-16 06:48:23 -0300574 poll_dev->input->name = DRIVER_LONG;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800575 usb_to_input_id(usbdev, &poll_dev->input->id);
576 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
577 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
578 poll_dev->input->phys = sur40->phys;
579 poll_dev->input->dev.parent = &interface->dev;
580
581 sur40->usbdev = usbdev;
582 sur40->dev = &interface->dev;
583 sur40->input = poll_dev;
584
585 /* use the bulk-in endpoint tested above */
586 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
587 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
588 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
589 if (!sur40->bulk_in_buffer) {
590 dev_err(&interface->dev, "Unable to allocate input buffer.");
591 error = -ENOMEM;
592 goto err_free_polldev;
593 }
594
Florian Echtlere831cd22015-03-16 06:48:23 -0300595 /* register the polled input device */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800596 error = input_register_polled_device(poll_dev);
597 if (error) {
598 dev_err(&interface->dev,
599 "Unable to register polled input device.");
600 goto err_free_buffer;
601 }
602
Florian Echtlere831cd22015-03-16 06:48:23 -0300603 /* register the video master device */
604 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
605 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
606 if (error) {
607 dev_err(&interface->dev,
608 "Unable to register video master device.");
609 goto err_unreg_v4l2;
610 }
611
612 /* initialize the lock and subdevice */
613 sur40->queue = sur40_queue;
614 sur40->queue.drv_priv = sur40;
615 sur40->queue.lock = &sur40->lock;
Hans Verkuil0e2f5112015-12-16 08:34:37 -0200616 sur40->queue.dev = sur40->dev;
Florian Echtlere831cd22015-03-16 06:48:23 -0300617
618 /* initialize the queue */
619 error = vb2_queue_init(&sur40->queue);
620 if (error)
621 goto err_unreg_v4l2;
622
Nick Dyerb4972652016-08-22 06:28:23 -0300623 sur40->pix_fmt = sur40_pix_format[0];
Florian Echtlere831cd22015-03-16 06:48:23 -0300624 sur40->vdev = sur40_video_device;
625 sur40->vdev.v4l2_dev = &sur40->v4l2;
626 sur40->vdev.lock = &sur40->lock;
627 sur40->vdev.queue = &sur40->queue;
628 video_set_drvdata(&sur40->vdev, sur40);
629
Nick Dyerb4972652016-08-22 06:28:23 -0300630 error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
Florian Echtlere831cd22015-03-16 06:48:23 -0300631 if (error) {
632 dev_err(&interface->dev,
633 "Unable to register video subdevice.");
634 goto err_unreg_video;
635 }
636
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800637 /* we can register the device now, as it is ready */
638 usb_set_intfdata(interface, sur40);
639 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
640
641 return 0;
642
Florian Echtlere831cd22015-03-16 06:48:23 -0300643err_unreg_video:
644 video_unregister_device(&sur40->vdev);
645err_unreg_v4l2:
646 v4l2_device_unregister(&sur40->v4l2);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800647err_free_buffer:
648 kfree(sur40->bulk_in_buffer);
649err_free_polldev:
650 input_free_polled_device(sur40->input);
651err_free_dev:
652 kfree(sur40);
653
654 return error;
655}
656
657/* Unregister device & clean up. */
658static void sur40_disconnect(struct usb_interface *interface)
659{
660 struct sur40_state *sur40 = usb_get_intfdata(interface);
661
Florian Echtlere831cd22015-03-16 06:48:23 -0300662 video_unregister_device(&sur40->vdev);
663 v4l2_device_unregister(&sur40->v4l2);
Florian Echtlere831cd22015-03-16 06:48:23 -0300664
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800665 input_unregister_polled_device(sur40->input);
666 input_free_polled_device(sur40->input);
667 kfree(sur40->bulk_in_buffer);
668 kfree(sur40);
669
670 usb_set_intfdata(interface, NULL);
671 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
672}
673
Florian Echtlere831cd22015-03-16 06:48:23 -0300674/*
675 * Setup the constraints of the queue: besides setting the number of planes
676 * per buffer and the size and allocation context of each plane, it also
677 * checks if sufficient buffers have been allocated. Usually 3 is a good
678 * minimum number: many DMA engines need a minimum of 2 buffers in the
679 * queue and you need to have another available for userspace processing.
680 */
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200681static int sur40_queue_setup(struct vb2_queue *q,
Florian Echtlere831cd22015-03-16 06:48:23 -0300682 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300683 unsigned int sizes[], struct device *alloc_devs[])
Florian Echtlere831cd22015-03-16 06:48:23 -0300684{
Nick Dyerb4972652016-08-22 06:28:23 -0300685 struct sur40_state *sur40 = vb2_get_drv_priv(q);
686
Florian Echtlere831cd22015-03-16 06:48:23 -0300687 if (q->num_buffers + *nbuffers < 3)
688 *nbuffers = 3 - q->num_buffers;
689
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200690 if (*nplanes)
Nick Dyerb4972652016-08-22 06:28:23 -0300691 return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
Florian Echtlere831cd22015-03-16 06:48:23 -0300692
693 *nplanes = 1;
Nick Dyerb4972652016-08-22 06:28:23 -0300694 sizes[0] = sur40->pix_fmt.sizeimage;
Florian Echtlere831cd22015-03-16 06:48:23 -0300695
696 return 0;
697}
698
699/*
700 * Prepare the buffer for queueing to the DMA engine: check and set the
701 * payload size.
702 */
703static int sur40_buffer_prepare(struct vb2_buffer *vb)
704{
705 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
Nick Dyerb4972652016-08-22 06:28:23 -0300706 unsigned long size = sur40->pix_fmt.sizeimage;
Florian Echtlere831cd22015-03-16 06:48:23 -0300707
708 if (vb2_plane_size(vb, 0) < size) {
709 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
710 vb2_plane_size(vb, 0), size);
711 return -EINVAL;
712 }
713
714 vb2_set_plane_payload(vb, 0, size);
715 return 0;
716}
717
718/*
719 * Queue this buffer to the DMA engine.
720 */
721static void sur40_buffer_queue(struct vb2_buffer *vb)
722{
723 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
724 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
725
726 spin_lock(&sur40->qlock);
727 list_add_tail(&buf->list, &sur40->buf_list);
728 spin_unlock(&sur40->qlock);
729}
730
731static void return_all_buffers(struct sur40_state *sur40,
732 enum vb2_buffer_state state)
733{
734 struct sur40_buffer *buf, *node;
735
736 spin_lock(&sur40->qlock);
737 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
Junghak Sung2d700712015-09-22 10:30:30 -0300738 vb2_buffer_done(&buf->vb.vb2_buf, state);
Florian Echtlere831cd22015-03-16 06:48:23 -0300739 list_del(&buf->list);
740 }
741 spin_unlock(&sur40->qlock);
742}
743
744/*
745 * Start streaming. First check if the minimum number of buffers have been
746 * queued. If not, then return -ENOBUFS and the vb2 framework will call
747 * this function again the next time a buffer has been queued until enough
748 * buffers are available to actually start the DMA engine.
749 */
750static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
751{
752 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
753
754 sur40->sequence = 0;
755 return 0;
756}
757
758/*
759 * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
760 * and passed on to the vb2 framework marked as STATE_ERROR.
761 */
762static void sur40_stop_streaming(struct vb2_queue *vq)
763{
764 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
Florian Echtler6a858812016-05-31 17:15:33 -0300765 vb2_wait_for_all_buffers(vq);
Florian Echtler2b7eea82015-05-25 09:04:16 -0300766 sur40->sequence = -1;
Florian Echtlere831cd22015-03-16 06:48:23 -0300767
768 /* Release all active buffers */
769 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
770}
771
772/* V4L ioctl */
773static int sur40_vidioc_querycap(struct file *file, void *priv,
774 struct v4l2_capability *cap)
775{
776 struct sur40_state *sur40 = video_drvdata(file);
777
778 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
779 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
780 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
Nick Dyerb4972652016-08-22 06:28:23 -0300781 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
Florian Echtlere831cd22015-03-16 06:48:23 -0300782 V4L2_CAP_READWRITE |
783 V4L2_CAP_STREAMING;
784 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
785 return 0;
786}
787
788static int sur40_vidioc_enum_input(struct file *file, void *priv,
789 struct v4l2_input *i)
790{
791 if (i->index != 0)
792 return -EINVAL;
Nick Dyerb4972652016-08-22 06:28:23 -0300793 i->type = V4L2_INPUT_TYPE_TOUCH;
Florian Echtlere831cd22015-03-16 06:48:23 -0300794 i->std = V4L2_STD_UNKNOWN;
795 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
796 i->capabilities = 0;
797 return 0;
798}
799
800static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
801{
802 return (i == 0) ? 0 : -EINVAL;
803}
804
805static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
806{
807 *i = 0;
808 return 0;
809}
810
Nick Dyerb4972652016-08-22 06:28:23 -0300811static int sur40_vidioc_try_fmt(struct file *file, void *priv,
Florian Echtlere831cd22015-03-16 06:48:23 -0300812 struct v4l2_format *f)
813{
Nick Dyerb4972652016-08-22 06:28:23 -0300814 switch (f->fmt.pix.pixelformat) {
815 case V4L2_PIX_FMT_GREY:
816 f->fmt.pix = sur40_pix_format[1];
817 break;
818
819 default:
820 f->fmt.pix = sur40_pix_format[0];
821 break;
822 }
823
824 return 0;
825}
826
827static int sur40_vidioc_s_fmt(struct file *file, void *priv,
828 struct v4l2_format *f)
829{
830 struct sur40_state *sur40 = video_drvdata(file);
831
832 switch (f->fmt.pix.pixelformat) {
833 case V4L2_PIX_FMT_GREY:
834 sur40->pix_fmt = sur40_pix_format[1];
835 break;
836
837 default:
838 sur40->pix_fmt = sur40_pix_format[0];
839 break;
840 }
841
842 f->fmt.pix = sur40->pix_fmt;
843 return 0;
844}
845
846static int sur40_vidioc_g_fmt(struct file *file, void *priv,
847 struct v4l2_format *f)
848{
849 struct sur40_state *sur40 = video_drvdata(file);
850
851 f->fmt.pix = sur40->pix_fmt;
Florian Echtlere831cd22015-03-16 06:48:23 -0300852 return 0;
853}
854
Florian Echtlerf223c302016-05-31 17:15:31 -0300855static int sur40_ioctl_parm(struct file *file, void *priv,
856 struct v4l2_streamparm *p)
857{
858 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
859 return -EINVAL;
860
861 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
862 p->parm.capture.timeperframe.numerator = 1;
863 p->parm.capture.timeperframe.denominator = 60;
864 p->parm.capture.readbuffers = 3;
865 return 0;
866}
867
Florian Echtlere831cd22015-03-16 06:48:23 -0300868static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
869 struct v4l2_fmtdesc *f)
870{
Nick Dyerb4972652016-08-22 06:28:23 -0300871 if (f->index >= ARRAY_SIZE(sur40_pix_format))
Florian Echtlere831cd22015-03-16 06:48:23 -0300872 return -EINVAL;
Nick Dyerb4972652016-08-22 06:28:23 -0300873
874 f->pixelformat = sur40_pix_format[f->index].pixelformat;
Florian Echtlere831cd22015-03-16 06:48:23 -0300875 f->flags = 0;
876 return 0;
877}
878
Florian Echtlerda6e4672015-05-25 09:04:14 -0300879static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
880 struct v4l2_frmsizeenum *f)
881{
Nick Dyerb4972652016-08-22 06:28:23 -0300882 struct sur40_state *sur40 = video_drvdata(file);
883
884 if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
885 && (f->pixel_format != V4L2_PIX_FMT_GREY)))
Florian Echtlerda6e4672015-05-25 09:04:14 -0300886 return -EINVAL;
887
888 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
Nick Dyerb4972652016-08-22 06:28:23 -0300889 f->discrete.width = sur40->pix_fmt.width;
890 f->discrete.height = sur40->pix_fmt.height;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300891 return 0;
892}
893
894static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
895 struct v4l2_frmivalenum *f)
896{
Nick Dyerb4972652016-08-22 06:28:23 -0300897 struct sur40_state *sur40 = video_drvdata(file);
898
Florian Echtlerf223c302016-05-31 17:15:31 -0300899 if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
Nick Dyerb4972652016-08-22 06:28:23 -0300900 && (f->pixel_format != V4L2_PIX_FMT_GREY))
901 || (f->width != sur40->pix_fmt.width)
902 || (f->height != sur40->pix_fmt.height))
903 return -EINVAL;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300904
905 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
Florian Echtlerf223c302016-05-31 17:15:31 -0300906 f->discrete.denominator = 60;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300907 f->discrete.numerator = 1;
908 return 0;
909}
910
911
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800912static const struct usb_device_id sur40_table[] = {
913 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
914 { } /* terminating null entry */
915};
916MODULE_DEVICE_TABLE(usb, sur40_table);
917
Florian Echtlere831cd22015-03-16 06:48:23 -0300918/* V4L2 structures */
919static const struct vb2_ops sur40_queue_ops = {
920 .queue_setup = sur40_queue_setup,
921 .buf_prepare = sur40_buffer_prepare,
922 .buf_queue = sur40_buffer_queue,
923 .start_streaming = sur40_start_streaming,
924 .stop_streaming = sur40_stop_streaming,
925 .wait_prepare = vb2_ops_wait_prepare,
926 .wait_finish = vb2_ops_wait_finish,
927};
928
929static const struct vb2_queue sur40_queue = {
930 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
931 /*
932 * VB2_USERPTR in currently not enabled: passing a user pointer to
933 * dma-sg will result in segment sizes that are not a multiple of
934 * 512 bytes, which is required by the host controller.
935 */
936 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
937 .buf_struct_size = sizeof(struct sur40_buffer),
938 .ops = &sur40_queue_ops,
939 .mem_ops = &vb2_dma_sg_memops,
940 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
941 .min_buffers_needed = 3,
942};
943
944static const struct v4l2_file_operations sur40_video_fops = {
945 .owner = THIS_MODULE,
946 .open = v4l2_fh_open,
947 .release = vb2_fop_release,
948 .unlocked_ioctl = video_ioctl2,
949 .read = vb2_fop_read,
950 .mmap = vb2_fop_mmap,
951 .poll = vb2_fop_poll,
952};
953
954static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
955
956 .vidioc_querycap = sur40_vidioc_querycap,
957
958 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
Nick Dyerb4972652016-08-22 06:28:23 -0300959 .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
960 .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
961 .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
Florian Echtlere831cd22015-03-16 06:48:23 -0300962
Florian Echtlerda6e4672015-05-25 09:04:14 -0300963 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
964 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
965
Florian Echtlerf223c302016-05-31 17:15:31 -0300966 .vidioc_g_parm = sur40_ioctl_parm,
967 .vidioc_s_parm = sur40_ioctl_parm,
968
Florian Echtlere831cd22015-03-16 06:48:23 -0300969 .vidioc_enum_input = sur40_vidioc_enum_input,
970 .vidioc_g_input = sur40_vidioc_g_input,
971 .vidioc_s_input = sur40_vidioc_s_input,
972
973 .vidioc_reqbufs = vb2_ioctl_reqbufs,
974 .vidioc_create_bufs = vb2_ioctl_create_bufs,
975 .vidioc_querybuf = vb2_ioctl_querybuf,
976 .vidioc_qbuf = vb2_ioctl_qbuf,
977 .vidioc_dqbuf = vb2_ioctl_dqbuf,
978 .vidioc_expbuf = vb2_ioctl_expbuf,
979
980 .vidioc_streamon = vb2_ioctl_streamon,
981 .vidioc_streamoff = vb2_ioctl_streamoff,
982};
983
984static const struct video_device sur40_video_device = {
985 .name = DRIVER_LONG,
986 .fops = &sur40_video_fops,
987 .ioctl_ops = &sur40_video_ioctl_ops,
988 .release = video_device_release_empty,
989};
990
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800991/* USB-specific object needed to register this driver with the USB subsystem. */
992static struct usb_driver sur40_driver = {
993 .name = DRIVER_SHORT,
994 .probe = sur40_probe,
995 .disconnect = sur40_disconnect,
996 .id_table = sur40_table,
997};
998
999module_usb_driver(sur40_driver);
1000
1001MODULE_AUTHOR(DRIVER_AUTHOR);
1002MODULE_DESCRIPTION(DRIVER_DESC);
1003MODULE_LICENSE("GPL");