blob: 8375b06ba8378b673e57338041e6cf752304663b [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
Florian Echtler0a904482018-02-08 03:43:03 -050084 u8 padding[24];
85
86 __le32 tag_id; /* valid when type == 0x04 (SUR40_TAG) */
87 __le32 unknown;
Florian Echtlerbdb5c572013-11-08 10:01:13 -080088
89} __packed;
90
91/* combined header/blob data */
92struct sur40_data {
93 struct sur40_header header;
94 struct sur40_blob blobs[];
95} __packed;
96
Florian Echtlere831cd22015-03-16 06:48:23 -030097/* read 512 bytes from endpoint 0x82 -> get header below
98 * continue reading 16k blocks until header.size bytes read */
99struct sur40_image_header {
100 __le32 magic; /* "SUBF" */
101 __le32 packet_id;
102 __le32 size; /* always 0x0007e900 = 960x540 */
103 __le32 timestamp; /* milliseconds (increases by 16 or 17 each frame) */
104 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
105} __packed;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800106
107/* version information */
108#define DRIVER_SHORT "sur40"
Florian Echtlere831cd22015-03-16 06:48:23 -0300109#define DRIVER_LONG "Samsung SUR40"
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800110#define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
111#define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
112
113/* vendor and device IDs */
114#define ID_MICROSOFT 0x045e
115#define ID_SUR40 0x0775
116
117/* sensor resolution */
118#define SENSOR_RES_X 1920
119#define SENSOR_RES_Y 1080
120
121/* touch data endpoint */
122#define TOUCH_ENDPOINT 0x86
123
Florian Echtlere831cd22015-03-16 06:48:23 -0300124/* video data endpoint */
125#define VIDEO_ENDPOINT 0x82
126
127/* video header fields */
128#define VIDEO_HEADER_MAGIC 0x46425553
129#define VIDEO_PACKET_SIZE 16384
130
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800131/* polling interval (ms) */
Florian Echtleraf766ee2016-05-31 17:15:32 -0300132#define POLL_INTERVAL 1
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800133
134/* maximum number of contacts FIXME: this is a guess? */
135#define MAX_CONTACTS 64
136
137/* control commands */
138#define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
Florian Echtler435915e2017-07-10 10:39:27 -0700139#define SUR40_ACCEL_CAPS 0xb3 /* 5 bytes */
140#define SUR40_SENSOR_CAPS 0xc1 /* 24 bytes */
141
142#define SUR40_POKE 0xc5 /* poke register byte */
143#define SUR40_PEEK 0xc4 /* 48 bytes registers */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800144
145#define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
146#define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
147
Florian Echtler435915e2017-07-10 10:39:27 -0700148#define SUR40_BLOB 0x01
149#define SUR40_TOUCH 0x02
150#define SUR40_TAG 0x04
151
Nick Dyerb4972652016-08-22 06:28:23 -0300152static const struct v4l2_pix_format sur40_pix_format[] = {
153 {
154 .pixelformat = V4L2_TCH_FMT_TU08,
155 .width = SENSOR_RES_X / 2,
156 .height = SENSOR_RES_Y / 2,
157 .field = V4L2_FIELD_NONE,
158 .colorspace = V4L2_COLORSPACE_SRGB,
159 .bytesperline = SENSOR_RES_X / 2,
160 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
161 },
162 {
163 .pixelformat = V4L2_PIX_FMT_GREY,
164 .width = SENSOR_RES_X / 2,
165 .height = SENSOR_RES_Y / 2,
166 .field = V4L2_FIELD_NONE,
167 .colorspace = V4L2_COLORSPACE_SRGB,
168 .bytesperline = SENSOR_RES_X / 2,
169 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
170 }
171};
172
Florian Echtlere831cd22015-03-16 06:48:23 -0300173/* master device state */
174struct sur40_state {
175
176 struct usb_device *usbdev;
177 struct device *dev;
178 struct input_polled_dev *input;
179
180 struct v4l2_device v4l2;
181 struct video_device vdev;
182 struct mutex lock;
Nick Dyerb4972652016-08-22 06:28:23 -0300183 struct v4l2_pix_format pix_fmt;
Florian Echtlere831cd22015-03-16 06:48:23 -0300184
185 struct vb2_queue queue;
Florian Echtlere831cd22015-03-16 06:48:23 -0300186 struct list_head buf_list;
187 spinlock_t qlock;
188 int sequence;
189
190 struct sur40_data *bulk_in_buffer;
191 size_t bulk_in_size;
192 u8 bulk_in_epaddr;
193
194 char phys[64];
195};
196
197struct sur40_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -0300198 struct vb2_v4l2_buffer vb;
Florian Echtlere831cd22015-03-16 06:48:23 -0300199 struct list_head list;
200};
201
202/* forward declarations */
203static const struct video_device sur40_video_device;
Florian Echtlere831cd22015-03-16 06:48:23 -0300204static const struct vb2_queue sur40_queue;
205static void sur40_process_video(struct sur40_state *sur40);
206
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800207/*
208 * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
209 * here by mistake which is very likely to have corrupted the firmware EEPROM
210 * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
211 * Should you ever run into a similar problem, the background story to this
212 * incident and instructions on how to fix the corrupted EEPROM are available
213 * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
214*/
215
Florian Echtlere831cd22015-03-16 06:48:23 -0300216/* command wrapper */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800217static int sur40_command(struct sur40_state *dev,
218 u8 command, u16 index, void *buffer, u16 size)
219{
220 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
221 command,
222 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
223 0x00, index, buffer, size, 1000);
224}
225
226/* Initialization routine, called from sur40_open */
227static int sur40_init(struct sur40_state *dev)
228{
229 int result;
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700230 u8 *buffer;
231
232 buffer = kmalloc(24, GFP_KERNEL);
233 if (!buffer) {
234 result = -ENOMEM;
235 goto error;
236 }
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800237
238 /* stupidly replay the original MS driver init sequence */
239 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
240 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700241 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800242
243 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
244 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700245 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800246
247 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
248 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700249 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800250
Florian Echtler435915e2017-07-10 10:39:27 -0700251 result = sur40_command(dev, SUR40_SENSOR_CAPS, 0x00, buffer, 24);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800252 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700253 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800254
Florian Echtler435915e2017-07-10 10:39:27 -0700255 result = sur40_command(dev, SUR40_ACCEL_CAPS, 0x00, buffer, 5);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800256 if (result < 0)
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700257 goto error;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800258
259 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
260
261 /*
262 * Discard the result buffer - no known data inside except
263 * some version strings, maybe extract these sometime...
264 */
Oliver Neukumd314e9e2016-03-23 14:36:56 -0700265error:
266 kfree(buffer);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800267 return result;
268}
269
270/*
271 * Callback routines from input_polled_dev
272 */
273
274/* Enable the device, polling will now start. */
275static void sur40_open(struct input_polled_dev *polldev)
276{
277 struct sur40_state *sur40 = polldev->private;
278
279 dev_dbg(sur40->dev, "open\n");
280 sur40_init(sur40);
281}
282
283/* Disable device, polling has stopped. */
284static void sur40_close(struct input_polled_dev *polldev)
285{
286 struct sur40_state *sur40 = polldev->private;
287
288 dev_dbg(sur40->dev, "close\n");
289 /*
290 * There is no known way to stop the device, so we simply
291 * stop polling.
292 */
293}
294
295/*
296 * This function is called when a whole contact has been processed,
297 * so that it can assign it to a slot and store the data there.
298 */
299static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
300{
301 int wide, major, minor;
Florian Echtler43234182017-07-10 10:40:28 -0700302 int bb_size_x, bb_size_y, pos_x, pos_y, ctr_x, ctr_y, slotnum;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800303
Florian Echtler43234182017-07-10 10:40:28 -0700304 if (blob->type != SUR40_TOUCH)
305 return;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800306
Florian Echtler43234182017-07-10 10:40:28 -0700307 slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800308 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
309 return;
310
Florian Echtler43234182017-07-10 10:40:28 -0700311 bb_size_x = le16_to_cpu(blob->bb_size_x);
312 bb_size_y = le16_to_cpu(blob->bb_size_y);
313
314 pos_x = le16_to_cpu(blob->pos_x);
315 pos_y = le16_to_cpu(blob->pos_y);
316
317 ctr_x = le16_to_cpu(blob->ctr_x);
318 ctr_y = le16_to_cpu(blob->ctr_y);
319
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800320 input_mt_slot(input, slotnum);
321 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
322 wide = (bb_size_x > bb_size_y);
323 major = max(bb_size_x, bb_size_y);
324 minor = min(bb_size_x, bb_size_y);
325
326 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
327 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
328 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
329 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
330
331 /* TODO: use a better orientation measure */
332 input_report_abs(input, ABS_MT_ORIENTATION, wide);
333 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
334 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
335}
336
337/* core function: poll for new input data */
338static void sur40_poll(struct input_polled_dev *polldev)
339{
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800340 struct sur40_state *sur40 = polldev->private;
341 struct input_dev *input = polldev->input;
342 int result, bulk_read, need_blobs, packet_blobs, i;
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800343 u32 uninitialized_var(packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800344
345 struct sur40_header *header = &sur40->bulk_in_buffer->header;
346 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
347
348 dev_dbg(sur40->dev, "poll\n");
349
350 need_blobs = -1;
351
352 do {
353
354 /* perform a blocking bulk read to get data from the device */
355 result = usb_bulk_msg(sur40->usbdev,
356 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
357 sur40->bulk_in_buffer, sur40->bulk_in_size,
358 &bulk_read, 1000);
359
360 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
361
362 if (result < 0) {
363 dev_err(sur40->dev, "error in usb_bulk_read\n");
364 return;
365 }
366
367 result = bulk_read - sizeof(struct sur40_header);
368
369 if (result % sizeof(struct sur40_blob) != 0) {
370 dev_err(sur40->dev, "transfer size mismatch\n");
371 return;
372 }
373
374 /* first packet? */
375 if (need_blobs == -1) {
376 need_blobs = le16_to_cpu(header->count);
377 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800378 packet_id = le32_to_cpu(header->packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800379 }
380
381 /*
382 * Sanity check. when video data is also being retrieved, the
383 * packet ID will usually increase in the middle of a series
Florian Echtler335abae2017-07-10 10:39:48 -0700384 * instead of at the end. However, the data is still consistent,
385 * so the packet ID is probably just valid for the first packet
386 * in a series.
387
Martin Kepplingerdd04dc62017-04-04 10:06:57 -0700388 if (packet_id != le32_to_cpu(header->packet_id))
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300389 dev_dbg(sur40->dev, "packet ID mismatch\n");
Florian Echtler335abae2017-07-10 10:39:48 -0700390 */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800391
392 packet_blobs = result / sizeof(struct sur40_blob);
393 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
394
395 /* packets always contain at least 4 blobs, even if empty */
396 if (packet_blobs > need_blobs)
397 packet_blobs = need_blobs;
398
399 for (i = 0; i < packet_blobs; i++) {
400 need_blobs--;
401 dev_dbg(sur40->dev, "processing blob\n");
402 sur40_report_blob(&(inblob[i]), input);
403 }
404
405 } while (need_blobs > 0);
406
407 input_mt_sync_frame(input);
408 input_sync(input);
Florian Echtlere831cd22015-03-16 06:48:23 -0300409
410 sur40_process_video(sur40);
411}
412
413/* deal with video data */
414static void sur40_process_video(struct sur40_state *sur40)
415{
416
417 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
418 struct sur40_buffer *new_buf;
419 struct usb_sg_request sgr;
420 struct sg_table *sgt;
421 int result, bulk_read;
422
423 if (!vb2_start_streaming_called(&sur40->queue))
424 return;
425
426 /* get a new buffer from the list */
427 spin_lock(&sur40->qlock);
Florian Echtlerc2529902015-03-31 06:43:28 -0300428 if (list_empty(&sur40->buf_list)) {
429 dev_dbg(sur40->dev, "buffer queue empty\n");
430 spin_unlock(&sur40->qlock);
431 return;
432 }
Florian Echtlere831cd22015-03-16 06:48:23 -0300433 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
434 list_del(&new_buf->list);
435 spin_unlock(&sur40->qlock);
436
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300437 dev_dbg(sur40->dev, "buffer acquired\n");
438
Florian Echtlere831cd22015-03-16 06:48:23 -0300439 /* retrieve data via bulk read */
440 result = usb_bulk_msg(sur40->usbdev,
441 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
442 sur40->bulk_in_buffer, sur40->bulk_in_size,
443 &bulk_read, 1000);
444
445 if (result < 0) {
446 dev_err(sur40->dev, "error in usb_bulk_read\n");
447 goto err_poll;
448 }
449
450 if (bulk_read != sizeof(struct sur40_image_header)) {
451 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
452 bulk_read, sizeof(struct sur40_image_header));
453 goto err_poll;
454 }
455
456 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
457 dev_err(sur40->dev, "image magic mismatch\n");
458 goto err_poll;
459 }
460
Nick Dyerb4972652016-08-22 06:28:23 -0300461 if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
Florian Echtlere831cd22015-03-16 06:48:23 -0300462 dev_err(sur40->dev, "image size mismatch\n");
463 goto err_poll;
464 }
465
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300466 dev_dbg(sur40->dev, "header acquired\n");
467
Junghak Sung2d700712015-09-22 10:30:30 -0300468 sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
Florian Echtlere831cd22015-03-16 06:48:23 -0300469
470 result = usb_sg_init(&sgr, sur40->usbdev,
471 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
Nick Dyerb4972652016-08-22 06:28:23 -0300472 sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
Florian Echtlere831cd22015-03-16 06:48:23 -0300473 if (result < 0) {
474 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
475 goto err_poll;
476 }
477
478 usb_sg_wait(&sgr);
479 if (sgr.status < 0) {
480 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
481 goto err_poll;
482 }
483
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300484 dev_dbg(sur40->dev, "image acquired\n");
485
Florian Echtler2b7eea82015-05-25 09:04:16 -0300486 /* return error if streaming was stopped in the meantime */
487 if (sur40->sequence == -1)
Florian Echtler6a858812016-05-31 17:15:33 -0300488 return;
Florian Echtler2b7eea82015-05-25 09:04:16 -0300489
Florian Echtlere831cd22015-03-16 06:48:23 -0300490 /* mark as finished */
Junghak Sungd6dd6452015-11-03 08:16:37 -0200491 new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300492 new_buf->vb.sequence = sur40->sequence++;
493 new_buf->vb.field = V4L2_FIELD_NONE;
494 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300495 dev_dbg(sur40->dev, "buffer marked done\n");
Florian Echtlere831cd22015-03-16 06:48:23 -0300496 return;
497
498err_poll:
Junghak Sung2d700712015-09-22 10:30:30 -0300499 vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800500}
501
502/* Initialize input device parameters. */
503static void sur40_input_setup(struct input_dev *input_dev)
504{
505 __set_bit(EV_KEY, input_dev->evbit);
506 __set_bit(EV_ABS, input_dev->evbit);
507
508 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
509 0, SENSOR_RES_X, 0, 0);
510 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
511 0, SENSOR_RES_Y, 0, 0);
512
513 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
514 0, SENSOR_RES_X, 0, 0);
515 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
516 0, SENSOR_RES_Y, 0, 0);
517
518 /* max value unknown, but major/minor axis
519 * can never be larger than screen */
520 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
521 0, SENSOR_RES_X, 0, 0);
522 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
523 0, SENSOR_RES_Y, 0, 0);
524
525 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
526
527 input_mt_init_slots(input_dev, MAX_CONTACTS,
528 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
529}
530
531/* Check candidate USB interface. */
532static int sur40_probe(struct usb_interface *interface,
533 const struct usb_device_id *id)
534{
535 struct usb_device *usbdev = interface_to_usbdev(interface);
536 struct sur40_state *sur40;
537 struct usb_host_interface *iface_desc;
538 struct usb_endpoint_descriptor *endpoint;
539 struct input_polled_dev *poll_dev;
540 int error;
541
542 /* Check if we really have the right interface. */
543 iface_desc = &interface->altsetting[0];
544 if (iface_desc->desc.bInterfaceClass != 0xFF)
545 return -ENODEV;
546
Johan Hovold92461f52017-03-16 11:43:09 -0700547 if (iface_desc->desc.bNumEndpoints < 5)
548 return -ENODEV;
549
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800550 /* Use endpoint #4 (0x86). */
551 endpoint = &iface_desc->endpoint[4].desc;
552 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
553 return -ENODEV;
554
555 /* Allocate memory for our device state and initialize it. */
556 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
557 if (!sur40)
558 return -ENOMEM;
559
560 poll_dev = input_allocate_polled_device();
561 if (!poll_dev) {
562 error = -ENOMEM;
563 goto err_free_dev;
564 }
565
Florian Echtlere831cd22015-03-16 06:48:23 -0300566 /* initialize locks/lists */
567 INIT_LIST_HEAD(&sur40->buf_list);
568 spin_lock_init(&sur40->qlock);
569 mutex_init(&sur40->lock);
570
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800571 /* Set up polled input device control structure */
572 poll_dev->private = sur40;
573 poll_dev->poll_interval = POLL_INTERVAL;
574 poll_dev->open = sur40_open;
575 poll_dev->poll = sur40_poll;
576 poll_dev->close = sur40_close;
577
578 /* Set up regular input device structure */
579 sur40_input_setup(poll_dev->input);
580
Florian Echtlere831cd22015-03-16 06:48:23 -0300581 poll_dev->input->name = DRIVER_LONG;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800582 usb_to_input_id(usbdev, &poll_dev->input->id);
583 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
584 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
585 poll_dev->input->phys = sur40->phys;
586 poll_dev->input->dev.parent = &interface->dev;
587
588 sur40->usbdev = usbdev;
589 sur40->dev = &interface->dev;
590 sur40->input = poll_dev;
591
592 /* use the bulk-in endpoint tested above */
593 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
594 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
595 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
596 if (!sur40->bulk_in_buffer) {
597 dev_err(&interface->dev, "Unable to allocate input buffer.");
598 error = -ENOMEM;
599 goto err_free_polldev;
600 }
601
Florian Echtlere831cd22015-03-16 06:48:23 -0300602 /* register the polled input device */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800603 error = input_register_polled_device(poll_dev);
604 if (error) {
605 dev_err(&interface->dev,
606 "Unable to register polled input device.");
607 goto err_free_buffer;
608 }
609
Florian Echtlere831cd22015-03-16 06:48:23 -0300610 /* register the video master device */
611 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
612 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
613 if (error) {
614 dev_err(&interface->dev,
615 "Unable to register video master device.");
616 goto err_unreg_v4l2;
617 }
618
619 /* initialize the lock and subdevice */
620 sur40->queue = sur40_queue;
621 sur40->queue.drv_priv = sur40;
622 sur40->queue.lock = &sur40->lock;
Hans Verkuil0e2f5112015-12-16 08:34:37 -0200623 sur40->queue.dev = sur40->dev;
Florian Echtlere831cd22015-03-16 06:48:23 -0300624
625 /* initialize the queue */
626 error = vb2_queue_init(&sur40->queue);
627 if (error)
628 goto err_unreg_v4l2;
629
Nick Dyerb4972652016-08-22 06:28:23 -0300630 sur40->pix_fmt = sur40_pix_format[0];
Florian Echtlere831cd22015-03-16 06:48:23 -0300631 sur40->vdev = sur40_video_device;
632 sur40->vdev.v4l2_dev = &sur40->v4l2;
633 sur40->vdev.lock = &sur40->lock;
634 sur40->vdev.queue = &sur40->queue;
635 video_set_drvdata(&sur40->vdev, sur40);
636
Nick Dyerb4972652016-08-22 06:28:23 -0300637 error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
Florian Echtlere831cd22015-03-16 06:48:23 -0300638 if (error) {
639 dev_err(&interface->dev,
640 "Unable to register video subdevice.");
641 goto err_unreg_video;
642 }
643
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800644 /* we can register the device now, as it is ready */
645 usb_set_intfdata(interface, sur40);
646 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
647
648 return 0;
649
Florian Echtlere831cd22015-03-16 06:48:23 -0300650err_unreg_video:
651 video_unregister_device(&sur40->vdev);
652err_unreg_v4l2:
653 v4l2_device_unregister(&sur40->v4l2);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800654err_free_buffer:
655 kfree(sur40->bulk_in_buffer);
656err_free_polldev:
657 input_free_polled_device(sur40->input);
658err_free_dev:
659 kfree(sur40);
660
661 return error;
662}
663
664/* Unregister device & clean up. */
665static void sur40_disconnect(struct usb_interface *interface)
666{
667 struct sur40_state *sur40 = usb_get_intfdata(interface);
668
Florian Echtlere831cd22015-03-16 06:48:23 -0300669 video_unregister_device(&sur40->vdev);
670 v4l2_device_unregister(&sur40->v4l2);
Florian Echtlere831cd22015-03-16 06:48:23 -0300671
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800672 input_unregister_polled_device(sur40->input);
673 input_free_polled_device(sur40->input);
674 kfree(sur40->bulk_in_buffer);
675 kfree(sur40);
676
677 usb_set_intfdata(interface, NULL);
678 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
679}
680
Florian Echtlere831cd22015-03-16 06:48:23 -0300681/*
682 * Setup the constraints of the queue: besides setting the number of planes
683 * per buffer and the size and allocation context of each plane, it also
684 * checks if sufficient buffers have been allocated. Usually 3 is a good
685 * minimum number: many DMA engines need a minimum of 2 buffers in the
686 * queue and you need to have another available for userspace processing.
687 */
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200688static int sur40_queue_setup(struct vb2_queue *q,
Florian Echtlere831cd22015-03-16 06:48:23 -0300689 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300690 unsigned int sizes[], struct device *alloc_devs[])
Florian Echtlere831cd22015-03-16 06:48:23 -0300691{
Nick Dyerb4972652016-08-22 06:28:23 -0300692 struct sur40_state *sur40 = vb2_get_drv_priv(q);
693
Florian Echtlere831cd22015-03-16 06:48:23 -0300694 if (q->num_buffers + *nbuffers < 3)
695 *nbuffers = 3 - q->num_buffers;
696
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200697 if (*nplanes)
Nick Dyerb4972652016-08-22 06:28:23 -0300698 return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
Florian Echtlere831cd22015-03-16 06:48:23 -0300699
700 *nplanes = 1;
Nick Dyerb4972652016-08-22 06:28:23 -0300701 sizes[0] = sur40->pix_fmt.sizeimage;
Florian Echtlere831cd22015-03-16 06:48:23 -0300702
703 return 0;
704}
705
706/*
707 * Prepare the buffer for queueing to the DMA engine: check and set the
708 * payload size.
709 */
710static int sur40_buffer_prepare(struct vb2_buffer *vb)
711{
712 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
Nick Dyerb4972652016-08-22 06:28:23 -0300713 unsigned long size = sur40->pix_fmt.sizeimage;
Florian Echtlere831cd22015-03-16 06:48:23 -0300714
715 if (vb2_plane_size(vb, 0) < size) {
716 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
717 vb2_plane_size(vb, 0), size);
718 return -EINVAL;
719 }
720
721 vb2_set_plane_payload(vb, 0, size);
722 return 0;
723}
724
725/*
726 * Queue this buffer to the DMA engine.
727 */
728static void sur40_buffer_queue(struct vb2_buffer *vb)
729{
730 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
731 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
732
733 spin_lock(&sur40->qlock);
734 list_add_tail(&buf->list, &sur40->buf_list);
735 spin_unlock(&sur40->qlock);
736}
737
738static void return_all_buffers(struct sur40_state *sur40,
739 enum vb2_buffer_state state)
740{
741 struct sur40_buffer *buf, *node;
742
743 spin_lock(&sur40->qlock);
744 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
Junghak Sung2d700712015-09-22 10:30:30 -0300745 vb2_buffer_done(&buf->vb.vb2_buf, state);
Florian Echtlere831cd22015-03-16 06:48:23 -0300746 list_del(&buf->list);
747 }
748 spin_unlock(&sur40->qlock);
749}
750
751/*
752 * Start streaming. First check if the minimum number of buffers have been
753 * queued. If not, then return -ENOBUFS and the vb2 framework will call
754 * this function again the next time a buffer has been queued until enough
755 * buffers are available to actually start the DMA engine.
756 */
757static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
758{
759 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
760
761 sur40->sequence = 0;
762 return 0;
763}
764
765/*
766 * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
767 * and passed on to the vb2 framework marked as STATE_ERROR.
768 */
769static void sur40_stop_streaming(struct vb2_queue *vq)
770{
771 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
Florian Echtler6a858812016-05-31 17:15:33 -0300772 vb2_wait_for_all_buffers(vq);
Florian Echtler2b7eea82015-05-25 09:04:16 -0300773 sur40->sequence = -1;
Florian Echtlere831cd22015-03-16 06:48:23 -0300774
775 /* Release all active buffers */
776 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
777}
778
779/* V4L ioctl */
780static int sur40_vidioc_querycap(struct file *file, void *priv,
781 struct v4l2_capability *cap)
782{
783 struct sur40_state *sur40 = video_drvdata(file);
784
785 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
786 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
787 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
Nick Dyerb4972652016-08-22 06:28:23 -0300788 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
Florian Echtlere831cd22015-03-16 06:48:23 -0300789 V4L2_CAP_READWRITE |
790 V4L2_CAP_STREAMING;
791 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
792 return 0;
793}
794
795static int sur40_vidioc_enum_input(struct file *file, void *priv,
796 struct v4l2_input *i)
797{
798 if (i->index != 0)
799 return -EINVAL;
Nick Dyerb4972652016-08-22 06:28:23 -0300800 i->type = V4L2_INPUT_TYPE_TOUCH;
Florian Echtlere831cd22015-03-16 06:48:23 -0300801 i->std = V4L2_STD_UNKNOWN;
802 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
803 i->capabilities = 0;
804 return 0;
805}
806
807static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
808{
809 return (i == 0) ? 0 : -EINVAL;
810}
811
812static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
813{
814 *i = 0;
815 return 0;
816}
817
Nick Dyerb4972652016-08-22 06:28:23 -0300818static int sur40_vidioc_try_fmt(struct file *file, void *priv,
Florian Echtlere831cd22015-03-16 06:48:23 -0300819 struct v4l2_format *f)
820{
Nick Dyerb4972652016-08-22 06:28:23 -0300821 switch (f->fmt.pix.pixelformat) {
822 case V4L2_PIX_FMT_GREY:
823 f->fmt.pix = sur40_pix_format[1];
824 break;
825
826 default:
827 f->fmt.pix = sur40_pix_format[0];
828 break;
829 }
830
831 return 0;
832}
833
834static int sur40_vidioc_s_fmt(struct file *file, void *priv,
835 struct v4l2_format *f)
836{
837 struct sur40_state *sur40 = video_drvdata(file);
838
839 switch (f->fmt.pix.pixelformat) {
840 case V4L2_PIX_FMT_GREY:
841 sur40->pix_fmt = sur40_pix_format[1];
842 break;
843
844 default:
845 sur40->pix_fmt = sur40_pix_format[0];
846 break;
847 }
848
849 f->fmt.pix = sur40->pix_fmt;
850 return 0;
851}
852
853static int sur40_vidioc_g_fmt(struct file *file, void *priv,
854 struct v4l2_format *f)
855{
856 struct sur40_state *sur40 = video_drvdata(file);
857
858 f->fmt.pix = sur40->pix_fmt;
Florian Echtlere831cd22015-03-16 06:48:23 -0300859 return 0;
860}
861
Florian Echtlerf223c302016-05-31 17:15:31 -0300862static int sur40_ioctl_parm(struct file *file, void *priv,
863 struct v4l2_streamparm *p)
864{
865 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
866 return -EINVAL;
867
868 p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
869 p->parm.capture.timeperframe.numerator = 1;
870 p->parm.capture.timeperframe.denominator = 60;
871 p->parm.capture.readbuffers = 3;
872 return 0;
873}
874
Florian Echtlere831cd22015-03-16 06:48:23 -0300875static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
876 struct v4l2_fmtdesc *f)
877{
Nick Dyerb4972652016-08-22 06:28:23 -0300878 if (f->index >= ARRAY_SIZE(sur40_pix_format))
Florian Echtlere831cd22015-03-16 06:48:23 -0300879 return -EINVAL;
Nick Dyerb4972652016-08-22 06:28:23 -0300880
881 f->pixelformat = sur40_pix_format[f->index].pixelformat;
Florian Echtlere831cd22015-03-16 06:48:23 -0300882 f->flags = 0;
883 return 0;
884}
885
Florian Echtlerda6e4672015-05-25 09:04:14 -0300886static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
887 struct v4l2_frmsizeenum *f)
888{
Nick Dyerb4972652016-08-22 06:28:23 -0300889 struct sur40_state *sur40 = video_drvdata(file);
890
891 if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
892 && (f->pixel_format != V4L2_PIX_FMT_GREY)))
Florian Echtlerda6e4672015-05-25 09:04:14 -0300893 return -EINVAL;
894
895 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
Nick Dyerb4972652016-08-22 06:28:23 -0300896 f->discrete.width = sur40->pix_fmt.width;
897 f->discrete.height = sur40->pix_fmt.height;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300898 return 0;
899}
900
901static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
902 struct v4l2_frmivalenum *f)
903{
Nick Dyerb4972652016-08-22 06:28:23 -0300904 struct sur40_state *sur40 = video_drvdata(file);
905
Florian Echtlerf223c302016-05-31 17:15:31 -0300906 if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
Nick Dyerb4972652016-08-22 06:28:23 -0300907 && (f->pixel_format != V4L2_PIX_FMT_GREY))
908 || (f->width != sur40->pix_fmt.width)
909 || (f->height != sur40->pix_fmt.height))
910 return -EINVAL;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300911
912 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
Florian Echtlerf223c302016-05-31 17:15:31 -0300913 f->discrete.denominator = 60;
Florian Echtlerda6e4672015-05-25 09:04:14 -0300914 f->discrete.numerator = 1;
915 return 0;
916}
917
918
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800919static const struct usb_device_id sur40_table[] = {
920 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
921 { } /* terminating null entry */
922};
923MODULE_DEVICE_TABLE(usb, sur40_table);
924
Florian Echtlere831cd22015-03-16 06:48:23 -0300925/* V4L2 structures */
926static const struct vb2_ops sur40_queue_ops = {
927 .queue_setup = sur40_queue_setup,
928 .buf_prepare = sur40_buffer_prepare,
929 .buf_queue = sur40_buffer_queue,
930 .start_streaming = sur40_start_streaming,
931 .stop_streaming = sur40_stop_streaming,
932 .wait_prepare = vb2_ops_wait_prepare,
933 .wait_finish = vb2_ops_wait_finish,
934};
935
936static const struct vb2_queue sur40_queue = {
937 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
938 /*
939 * VB2_USERPTR in currently not enabled: passing a user pointer to
940 * dma-sg will result in segment sizes that are not a multiple of
941 * 512 bytes, which is required by the host controller.
942 */
943 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
944 .buf_struct_size = sizeof(struct sur40_buffer),
945 .ops = &sur40_queue_ops,
946 .mem_ops = &vb2_dma_sg_memops,
947 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
948 .min_buffers_needed = 3,
949};
950
951static const struct v4l2_file_operations sur40_video_fops = {
952 .owner = THIS_MODULE,
953 .open = v4l2_fh_open,
954 .release = vb2_fop_release,
955 .unlocked_ioctl = video_ioctl2,
956 .read = vb2_fop_read,
957 .mmap = vb2_fop_mmap,
958 .poll = vb2_fop_poll,
959};
960
961static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
962
963 .vidioc_querycap = sur40_vidioc_querycap,
964
965 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
Nick Dyerb4972652016-08-22 06:28:23 -0300966 .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
967 .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
968 .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
Florian Echtlere831cd22015-03-16 06:48:23 -0300969
Florian Echtlerda6e4672015-05-25 09:04:14 -0300970 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
971 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
972
Florian Echtlerf223c302016-05-31 17:15:31 -0300973 .vidioc_g_parm = sur40_ioctl_parm,
974 .vidioc_s_parm = sur40_ioctl_parm,
975
Florian Echtlere831cd22015-03-16 06:48:23 -0300976 .vidioc_enum_input = sur40_vidioc_enum_input,
977 .vidioc_g_input = sur40_vidioc_g_input,
978 .vidioc_s_input = sur40_vidioc_s_input,
979
980 .vidioc_reqbufs = vb2_ioctl_reqbufs,
981 .vidioc_create_bufs = vb2_ioctl_create_bufs,
982 .vidioc_querybuf = vb2_ioctl_querybuf,
983 .vidioc_qbuf = vb2_ioctl_qbuf,
984 .vidioc_dqbuf = vb2_ioctl_dqbuf,
985 .vidioc_expbuf = vb2_ioctl_expbuf,
986
987 .vidioc_streamon = vb2_ioctl_streamon,
988 .vidioc_streamoff = vb2_ioctl_streamoff,
989};
990
991static const struct video_device sur40_video_device = {
992 .name = DRIVER_LONG,
993 .fops = &sur40_video_fops,
994 .ioctl_ops = &sur40_video_ioctl_ops,
995 .release = video_device_release_empty,
996};
997
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800998/* USB-specific object needed to register this driver with the USB subsystem. */
999static struct usb_driver sur40_driver = {
1000 .name = DRIVER_SHORT,
1001 .probe = sur40_probe,
1002 .disconnect = sur40_disconnect,
1003 .id_table = sur40_table,
1004};
1005
1006module_usb_driver(sur40_driver);
1007
1008MODULE_AUTHOR(DRIVER_AUTHOR);
1009MODULE_DESCRIPTION(DRIVER_DESC);
1010MODULE_LICENSE("GPL");