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