blob: 8add986abc820a387e3c04aff81147d9300fa814 [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>
41#include <media/videobuf2-dma-sg.h>
Florian Echtlerbdb5c572013-11-08 10:01:13 -080042
43/* read 512 bytes from endpoint 0x86 -> get header + blobs */
44struct sur40_header {
45
46 __le16 type; /* always 0x0001 */
47 __le16 count; /* count of blobs (if 0: continue prev. packet) */
48
49 __le32 packet_id; /* unique ID for all packets in one frame */
50
51 __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
52 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
53
54} __packed;
55
56struct sur40_blob {
57
58 __le16 blob_id;
59
60 u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
61 u8 unknown; /* always 0x01 or 0x02 (no idea what this is?) */
62
63 __le16 bb_pos_x; /* upper left corner of bounding box */
64 __le16 bb_pos_y;
65
66 __le16 bb_size_x; /* size of bounding box */
67 __le16 bb_size_y;
68
69 __le16 pos_x; /* finger tip position */
70 __le16 pos_y;
71
72 __le16 ctr_x; /* centroid position */
73 __le16 ctr_y;
74
75 __le16 axis_x; /* somehow related to major/minor axis, mostly: */
76 __le16 axis_y; /* axis_x == bb_size_y && axis_y == bb_size_x */
77
78 __le32 angle; /* orientation in radians relative to x axis -
79 actually an IEEE754 float, don't use in kernel */
80
81 __le32 area; /* size in pixels/pressure (?) */
82
83 u8 padding[32];
84
85} __packed;
86
87/* combined header/blob data */
88struct sur40_data {
89 struct sur40_header header;
90 struct sur40_blob blobs[];
91} __packed;
92
Florian Echtlere831cd22015-03-16 06:48:23 -030093/* read 512 bytes from endpoint 0x82 -> get header below
94 * continue reading 16k blocks until header.size bytes read */
95struct sur40_image_header {
96 __le32 magic; /* "SUBF" */
97 __le32 packet_id;
98 __le32 size; /* always 0x0007e900 = 960x540 */
99 __le32 timestamp; /* milliseconds (increases by 16 or 17 each frame) */
100 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
101} __packed;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800102
103/* version information */
104#define DRIVER_SHORT "sur40"
Florian Echtlere831cd22015-03-16 06:48:23 -0300105#define DRIVER_LONG "Samsung SUR40"
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800106#define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
107#define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
108
109/* vendor and device IDs */
110#define ID_MICROSOFT 0x045e
111#define ID_SUR40 0x0775
112
113/* sensor resolution */
114#define SENSOR_RES_X 1920
115#define SENSOR_RES_Y 1080
116
117/* touch data endpoint */
118#define TOUCH_ENDPOINT 0x86
119
Florian Echtlere831cd22015-03-16 06:48:23 -0300120/* video data endpoint */
121#define VIDEO_ENDPOINT 0x82
122
123/* video header fields */
124#define VIDEO_HEADER_MAGIC 0x46425553
125#define VIDEO_PACKET_SIZE 16384
126
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800127/* polling interval (ms) */
Florian Echtlerca27ab32015-05-25 09:04:13 -0300128#define POLL_INTERVAL 4
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800129
130/* maximum number of contacts FIXME: this is a guess? */
131#define MAX_CONTACTS 64
132
133/* control commands */
134#define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
135#define SUR40_UNKNOWN1 0xb3 /* 5 bytes */
136#define SUR40_UNKNOWN2 0xc1 /* 24 bytes */
137
138#define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
139#define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
140
Florian Echtlere831cd22015-03-16 06:48:23 -0300141/* master device state */
142struct sur40_state {
143
144 struct usb_device *usbdev;
145 struct device *dev;
146 struct input_polled_dev *input;
147
148 struct v4l2_device v4l2;
149 struct video_device vdev;
150 struct mutex lock;
151
152 struct vb2_queue queue;
153 struct vb2_alloc_ctx *alloc_ctx;
154 struct list_head buf_list;
155 spinlock_t qlock;
156 int sequence;
157
158 struct sur40_data *bulk_in_buffer;
159 size_t bulk_in_size;
160 u8 bulk_in_epaddr;
161
162 char phys[64];
163};
164
165struct sur40_buffer {
166 struct vb2_buffer vb;
167 struct list_head list;
168};
169
170/* forward declarations */
171static const struct video_device sur40_video_device;
172static const struct v4l2_pix_format sur40_video_format;
173static const struct vb2_queue sur40_queue;
174static void sur40_process_video(struct sur40_state *sur40);
175
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800176/*
177 * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
178 * here by mistake which is very likely to have corrupted the firmware EEPROM
179 * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
180 * Should you ever run into a similar problem, the background story to this
181 * incident and instructions on how to fix the corrupted EEPROM are available
182 * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
183*/
184
Florian Echtlere831cd22015-03-16 06:48:23 -0300185/* command wrapper */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800186static int sur40_command(struct sur40_state *dev,
187 u8 command, u16 index, void *buffer, u16 size)
188{
189 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
190 command,
191 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
192 0x00, index, buffer, size, 1000);
193}
194
195/* Initialization routine, called from sur40_open */
196static int sur40_init(struct sur40_state *dev)
197{
198 int result;
199 u8 buffer[24];
200
201 /* stupidly replay the original MS driver init sequence */
202 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
203 if (result < 0)
204 return result;
205
206 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
207 if (result < 0)
208 return result;
209
210 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
211 if (result < 0)
212 return result;
213
214 result = sur40_command(dev, SUR40_UNKNOWN2, 0x00, buffer, 24);
215 if (result < 0)
216 return result;
217
218 result = sur40_command(dev, SUR40_UNKNOWN1, 0x00, buffer, 5);
219 if (result < 0)
220 return result;
221
222 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
223
224 /*
225 * Discard the result buffer - no known data inside except
226 * some version strings, maybe extract these sometime...
227 */
228
229 return result;
230}
231
232/*
233 * Callback routines from input_polled_dev
234 */
235
236/* Enable the device, polling will now start. */
237static void sur40_open(struct input_polled_dev *polldev)
238{
239 struct sur40_state *sur40 = polldev->private;
240
241 dev_dbg(sur40->dev, "open\n");
242 sur40_init(sur40);
243}
244
245/* Disable device, polling has stopped. */
246static void sur40_close(struct input_polled_dev *polldev)
247{
248 struct sur40_state *sur40 = polldev->private;
249
250 dev_dbg(sur40->dev, "close\n");
251 /*
252 * There is no known way to stop the device, so we simply
253 * stop polling.
254 */
255}
256
257/*
258 * This function is called when a whole contact has been processed,
259 * so that it can assign it to a slot and store the data there.
260 */
261static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
262{
263 int wide, major, minor;
264
265 int bb_size_x = le16_to_cpu(blob->bb_size_x);
266 int bb_size_y = le16_to_cpu(blob->bb_size_y);
267
268 int pos_x = le16_to_cpu(blob->pos_x);
269 int pos_y = le16_to_cpu(blob->pos_y);
270
271 int ctr_x = le16_to_cpu(blob->ctr_x);
272 int ctr_y = le16_to_cpu(blob->ctr_y);
273
274 int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
275 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
276 return;
277
278 input_mt_slot(input, slotnum);
279 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
280 wide = (bb_size_x > bb_size_y);
281 major = max(bb_size_x, bb_size_y);
282 minor = min(bb_size_x, bb_size_y);
283
284 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
285 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
286 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
287 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
288
289 /* TODO: use a better orientation measure */
290 input_report_abs(input, ABS_MT_ORIENTATION, wide);
291 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
292 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
293}
294
295/* core function: poll for new input data */
296static void sur40_poll(struct input_polled_dev *polldev)
297{
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800298 struct sur40_state *sur40 = polldev->private;
299 struct input_dev *input = polldev->input;
300 int result, bulk_read, need_blobs, packet_blobs, i;
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800301 u32 uninitialized_var(packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800302
303 struct sur40_header *header = &sur40->bulk_in_buffer->header;
304 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
305
306 dev_dbg(sur40->dev, "poll\n");
307
308 need_blobs = -1;
309
310 do {
311
312 /* perform a blocking bulk read to get data from the device */
313 result = usb_bulk_msg(sur40->usbdev,
314 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
315 sur40->bulk_in_buffer, sur40->bulk_in_size,
316 &bulk_read, 1000);
317
318 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
319
320 if (result < 0) {
321 dev_err(sur40->dev, "error in usb_bulk_read\n");
322 return;
323 }
324
325 result = bulk_read - sizeof(struct sur40_header);
326
327 if (result % sizeof(struct sur40_blob) != 0) {
328 dev_err(sur40->dev, "transfer size mismatch\n");
329 return;
330 }
331
332 /* first packet? */
333 if (need_blobs == -1) {
334 need_blobs = le16_to_cpu(header->count);
335 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
Dmitry Torokhovb884eb82013-11-26 11:03:57 -0800336 packet_id = le32_to_cpu(header->packet_id);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800337 }
338
339 /*
340 * Sanity check. when video data is also being retrieved, the
341 * packet ID will usually increase in the middle of a series
342 * instead of at the end.
343 */
344 if (packet_id != header->packet_id)
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300345 dev_dbg(sur40->dev, "packet ID mismatch\n");
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800346
347 packet_blobs = result / sizeof(struct sur40_blob);
348 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
349
350 /* packets always contain at least 4 blobs, even if empty */
351 if (packet_blobs > need_blobs)
352 packet_blobs = need_blobs;
353
354 for (i = 0; i < packet_blobs; i++) {
355 need_blobs--;
356 dev_dbg(sur40->dev, "processing blob\n");
357 sur40_report_blob(&(inblob[i]), input);
358 }
359
360 } while (need_blobs > 0);
361
362 input_mt_sync_frame(input);
363 input_sync(input);
Florian Echtlere831cd22015-03-16 06:48:23 -0300364
365 sur40_process_video(sur40);
366}
367
368/* deal with video data */
369static void sur40_process_video(struct sur40_state *sur40)
370{
371
372 struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
373 struct sur40_buffer *new_buf;
374 struct usb_sg_request sgr;
375 struct sg_table *sgt;
376 int result, bulk_read;
377
378 if (!vb2_start_streaming_called(&sur40->queue))
379 return;
380
381 /* get a new buffer from the list */
382 spin_lock(&sur40->qlock);
Florian Echtlerc2529902015-03-31 06:43:28 -0300383 if (list_empty(&sur40->buf_list)) {
384 dev_dbg(sur40->dev, "buffer queue empty\n");
385 spin_unlock(&sur40->qlock);
386 return;
387 }
Florian Echtlere831cd22015-03-16 06:48:23 -0300388 new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
389 list_del(&new_buf->list);
390 spin_unlock(&sur40->qlock);
391
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300392 dev_dbg(sur40->dev, "buffer acquired\n");
393
Florian Echtlere831cd22015-03-16 06:48:23 -0300394 /* retrieve data via bulk read */
395 result = usb_bulk_msg(sur40->usbdev,
396 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
397 sur40->bulk_in_buffer, sur40->bulk_in_size,
398 &bulk_read, 1000);
399
400 if (result < 0) {
401 dev_err(sur40->dev, "error in usb_bulk_read\n");
402 goto err_poll;
403 }
404
405 if (bulk_read != sizeof(struct sur40_image_header)) {
406 dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
407 bulk_read, sizeof(struct sur40_image_header));
408 goto err_poll;
409 }
410
411 if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
412 dev_err(sur40->dev, "image magic mismatch\n");
413 goto err_poll;
414 }
415
416 if (le32_to_cpu(img->size) != sur40_video_format.sizeimage) {
417 dev_err(sur40->dev, "image size mismatch\n");
418 goto err_poll;
419 }
420
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300421 dev_dbg(sur40->dev, "header acquired\n");
422
Florian Echtlere831cd22015-03-16 06:48:23 -0300423 sgt = vb2_dma_sg_plane_desc(&new_buf->vb, 0);
424
425 result = usb_sg_init(&sgr, sur40->usbdev,
426 usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
427 sgt->sgl, sgt->nents, sur40_video_format.sizeimage, 0);
428 if (result < 0) {
429 dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
430 goto err_poll;
431 }
432
433 usb_sg_wait(&sgr);
434 if (sgr.status < 0) {
435 dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
436 goto err_poll;
437 }
438
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300439 dev_dbg(sur40->dev, "image acquired\n");
440
Florian Echtlere831cd22015-03-16 06:48:23 -0300441 /* mark as finished */
442 v4l2_get_timestamp(&new_buf->vb.v4l2_buf.timestamp);
443 new_buf->vb.v4l2_buf.sequence = sur40->sequence++;
444 new_buf->vb.v4l2_buf.field = V4L2_FIELD_NONE;
445 vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_DONE);
Florian Echtler0cfdfcc2015-05-25 09:04:15 -0300446 dev_dbg(sur40->dev, "buffer marked done\n");
Florian Echtlere831cd22015-03-16 06:48:23 -0300447 return;
448
449err_poll:
450 vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_ERROR);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800451}
452
453/* Initialize input device parameters. */
454static void sur40_input_setup(struct input_dev *input_dev)
455{
456 __set_bit(EV_KEY, input_dev->evbit);
457 __set_bit(EV_ABS, input_dev->evbit);
458
459 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
460 0, SENSOR_RES_X, 0, 0);
461 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
462 0, SENSOR_RES_Y, 0, 0);
463
464 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
465 0, SENSOR_RES_X, 0, 0);
466 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
467 0, SENSOR_RES_Y, 0, 0);
468
469 /* max value unknown, but major/minor axis
470 * can never be larger than screen */
471 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
472 0, SENSOR_RES_X, 0, 0);
473 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
474 0, SENSOR_RES_Y, 0, 0);
475
476 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
477
478 input_mt_init_slots(input_dev, MAX_CONTACTS,
479 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
480}
481
482/* Check candidate USB interface. */
483static int sur40_probe(struct usb_interface *interface,
484 const struct usb_device_id *id)
485{
486 struct usb_device *usbdev = interface_to_usbdev(interface);
487 struct sur40_state *sur40;
488 struct usb_host_interface *iface_desc;
489 struct usb_endpoint_descriptor *endpoint;
490 struct input_polled_dev *poll_dev;
491 int error;
492
493 /* Check if we really have the right interface. */
494 iface_desc = &interface->altsetting[0];
495 if (iface_desc->desc.bInterfaceClass != 0xFF)
496 return -ENODEV;
497
498 /* Use endpoint #4 (0x86). */
499 endpoint = &iface_desc->endpoint[4].desc;
500 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
501 return -ENODEV;
502
503 /* Allocate memory for our device state and initialize it. */
504 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
505 if (!sur40)
506 return -ENOMEM;
507
508 poll_dev = input_allocate_polled_device();
509 if (!poll_dev) {
510 error = -ENOMEM;
511 goto err_free_dev;
512 }
513
Florian Echtlere831cd22015-03-16 06:48:23 -0300514 /* initialize locks/lists */
515 INIT_LIST_HEAD(&sur40->buf_list);
516 spin_lock_init(&sur40->qlock);
517 mutex_init(&sur40->lock);
518
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800519 /* Set up polled input device control structure */
520 poll_dev->private = sur40;
521 poll_dev->poll_interval = POLL_INTERVAL;
522 poll_dev->open = sur40_open;
523 poll_dev->poll = sur40_poll;
524 poll_dev->close = sur40_close;
525
526 /* Set up regular input device structure */
527 sur40_input_setup(poll_dev->input);
528
Florian Echtlere831cd22015-03-16 06:48:23 -0300529 poll_dev->input->name = DRIVER_LONG;
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800530 usb_to_input_id(usbdev, &poll_dev->input->id);
531 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
532 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
533 poll_dev->input->phys = sur40->phys;
534 poll_dev->input->dev.parent = &interface->dev;
535
536 sur40->usbdev = usbdev;
537 sur40->dev = &interface->dev;
538 sur40->input = poll_dev;
539
540 /* use the bulk-in endpoint tested above */
541 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
542 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
543 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
544 if (!sur40->bulk_in_buffer) {
545 dev_err(&interface->dev, "Unable to allocate input buffer.");
546 error = -ENOMEM;
547 goto err_free_polldev;
548 }
549
Florian Echtlere831cd22015-03-16 06:48:23 -0300550 /* register the polled input device */
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800551 error = input_register_polled_device(poll_dev);
552 if (error) {
553 dev_err(&interface->dev,
554 "Unable to register polled input device.");
555 goto err_free_buffer;
556 }
557
Florian Echtlere831cd22015-03-16 06:48:23 -0300558 /* register the video master device */
559 snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
560 error = v4l2_device_register(sur40->dev, &sur40->v4l2);
561 if (error) {
562 dev_err(&interface->dev,
563 "Unable to register video master device.");
564 goto err_unreg_v4l2;
565 }
566
567 /* initialize the lock and subdevice */
568 sur40->queue = sur40_queue;
569 sur40->queue.drv_priv = sur40;
570 sur40->queue.lock = &sur40->lock;
571
572 /* initialize the queue */
573 error = vb2_queue_init(&sur40->queue);
574 if (error)
575 goto err_unreg_v4l2;
576
577 sur40->alloc_ctx = vb2_dma_sg_init_ctx(sur40->dev);
578 if (IS_ERR(sur40->alloc_ctx)) {
579 dev_err(sur40->dev, "Can't allocate buffer context");
580 goto err_unreg_v4l2;
581 }
582
583 sur40->vdev = sur40_video_device;
584 sur40->vdev.v4l2_dev = &sur40->v4l2;
585 sur40->vdev.lock = &sur40->lock;
586 sur40->vdev.queue = &sur40->queue;
587 video_set_drvdata(&sur40->vdev, sur40);
588
589 error = video_register_device(&sur40->vdev, VFL_TYPE_GRABBER, -1);
590 if (error) {
591 dev_err(&interface->dev,
592 "Unable to register video subdevice.");
593 goto err_unreg_video;
594 }
595
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800596 /* we can register the device now, as it is ready */
597 usb_set_intfdata(interface, sur40);
598 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
599
600 return 0;
601
Florian Echtlere831cd22015-03-16 06:48:23 -0300602err_unreg_video:
603 video_unregister_device(&sur40->vdev);
604err_unreg_v4l2:
605 v4l2_device_unregister(&sur40->v4l2);
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800606err_free_buffer:
607 kfree(sur40->bulk_in_buffer);
608err_free_polldev:
609 input_free_polled_device(sur40->input);
610err_free_dev:
611 kfree(sur40);
612
613 return error;
614}
615
616/* Unregister device & clean up. */
617static void sur40_disconnect(struct usb_interface *interface)
618{
619 struct sur40_state *sur40 = usb_get_intfdata(interface);
620
Florian Echtlere831cd22015-03-16 06:48:23 -0300621 video_unregister_device(&sur40->vdev);
622 v4l2_device_unregister(&sur40->v4l2);
623 vb2_dma_sg_cleanup_ctx(sur40->alloc_ctx);
624
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800625 input_unregister_polled_device(sur40->input);
626 input_free_polled_device(sur40->input);
627 kfree(sur40->bulk_in_buffer);
628 kfree(sur40);
629
630 usb_set_intfdata(interface, NULL);
631 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
632}
633
Florian Echtlere831cd22015-03-16 06:48:23 -0300634/*
635 * Setup the constraints of the queue: besides setting the number of planes
636 * per buffer and the size and allocation context of each plane, it also
637 * checks if sufficient buffers have been allocated. Usually 3 is a good
638 * minimum number: many DMA engines need a minimum of 2 buffers in the
639 * queue and you need to have another available for userspace processing.
640 */
641static int sur40_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
642 unsigned int *nbuffers, unsigned int *nplanes,
643 unsigned int sizes[], void *alloc_ctxs[])
644{
645 struct sur40_state *sur40 = vb2_get_drv_priv(q);
646
647 if (q->num_buffers + *nbuffers < 3)
648 *nbuffers = 3 - q->num_buffers;
649
650 if (fmt && fmt->fmt.pix.sizeimage < sur40_video_format.sizeimage)
651 return -EINVAL;
652
653 *nplanes = 1;
654 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : sur40_video_format.sizeimage;
655 alloc_ctxs[0] = sur40->alloc_ctx;
656
657 return 0;
658}
659
660/*
661 * Prepare the buffer for queueing to the DMA engine: check and set the
662 * payload size.
663 */
664static int sur40_buffer_prepare(struct vb2_buffer *vb)
665{
666 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
667 unsigned long size = sur40_video_format.sizeimage;
668
669 if (vb2_plane_size(vb, 0) < size) {
670 dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
671 vb2_plane_size(vb, 0), size);
672 return -EINVAL;
673 }
674
675 vb2_set_plane_payload(vb, 0, size);
676 return 0;
677}
678
679/*
680 * Queue this buffer to the DMA engine.
681 */
682static void sur40_buffer_queue(struct vb2_buffer *vb)
683{
684 struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
685 struct sur40_buffer *buf = (struct sur40_buffer *)vb;
686
687 spin_lock(&sur40->qlock);
688 list_add_tail(&buf->list, &sur40->buf_list);
689 spin_unlock(&sur40->qlock);
690}
691
692static void return_all_buffers(struct sur40_state *sur40,
693 enum vb2_buffer_state state)
694{
695 struct sur40_buffer *buf, *node;
696
697 spin_lock(&sur40->qlock);
698 list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
699 vb2_buffer_done(&buf->vb, state);
700 list_del(&buf->list);
701 }
702 spin_unlock(&sur40->qlock);
703}
704
705/*
706 * Start streaming. First check if the minimum number of buffers have been
707 * queued. If not, then return -ENOBUFS and the vb2 framework will call
708 * this function again the next time a buffer has been queued until enough
709 * buffers are available to actually start the DMA engine.
710 */
711static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
712{
713 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
714
715 sur40->sequence = 0;
716 return 0;
717}
718
719/*
720 * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
721 * and passed on to the vb2 framework marked as STATE_ERROR.
722 */
723static void sur40_stop_streaming(struct vb2_queue *vq)
724{
725 struct sur40_state *sur40 = vb2_get_drv_priv(vq);
726
727 /* Release all active buffers */
728 return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
729}
730
731/* V4L ioctl */
732static int sur40_vidioc_querycap(struct file *file, void *priv,
733 struct v4l2_capability *cap)
734{
735 struct sur40_state *sur40 = video_drvdata(file);
736
737 strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
738 strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
739 usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
740 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
741 V4L2_CAP_READWRITE |
742 V4L2_CAP_STREAMING;
743 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
744 return 0;
745}
746
747static int sur40_vidioc_enum_input(struct file *file, void *priv,
748 struct v4l2_input *i)
749{
750 if (i->index != 0)
751 return -EINVAL;
752 i->type = V4L2_INPUT_TYPE_CAMERA;
753 i->std = V4L2_STD_UNKNOWN;
754 strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
755 i->capabilities = 0;
756 return 0;
757}
758
759static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
760{
761 return (i == 0) ? 0 : -EINVAL;
762}
763
764static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
765{
766 *i = 0;
767 return 0;
768}
769
770static int sur40_vidioc_fmt(struct file *file, void *priv,
771 struct v4l2_format *f)
772{
773 f->fmt.pix = sur40_video_format;
774 return 0;
775}
776
777static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
778 struct v4l2_fmtdesc *f)
779{
780 if (f->index != 0)
781 return -EINVAL;
782 strlcpy(f->description, "8-bit greyscale", sizeof(f->description));
783 f->pixelformat = V4L2_PIX_FMT_GREY;
784 f->flags = 0;
785 return 0;
786}
787
Florian Echtlerda6e4672015-05-25 09:04:14 -0300788static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
789 struct v4l2_frmsizeenum *f)
790{
791 if ((f->index != 0) || (f->pixel_format != V4L2_PIX_FMT_GREY))
792 return -EINVAL;
793
794 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
795 f->discrete.width = sur40_video_format.width;
796 f->discrete.height = sur40_video_format.height;
797 return 0;
798}
799
800static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
801 struct v4l2_frmivalenum *f)
802{
803 if ((f->index > 1) || (f->pixel_format != V4L2_PIX_FMT_GREY)
804 || (f->width != sur40_video_format.width)
805 || (f->height != sur40_video_format.height))
806 return -EINVAL;
807
808 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
809 f->discrete.denominator = 60/(f->index+1);
810 f->discrete.numerator = 1;
811 return 0;
812}
813
814
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800815static const struct usb_device_id sur40_table[] = {
816 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
817 { } /* terminating null entry */
818};
819MODULE_DEVICE_TABLE(usb, sur40_table);
820
Florian Echtlere831cd22015-03-16 06:48:23 -0300821/* V4L2 structures */
822static const struct vb2_ops sur40_queue_ops = {
823 .queue_setup = sur40_queue_setup,
824 .buf_prepare = sur40_buffer_prepare,
825 .buf_queue = sur40_buffer_queue,
826 .start_streaming = sur40_start_streaming,
827 .stop_streaming = sur40_stop_streaming,
828 .wait_prepare = vb2_ops_wait_prepare,
829 .wait_finish = vb2_ops_wait_finish,
830};
831
832static const struct vb2_queue sur40_queue = {
833 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
834 /*
835 * VB2_USERPTR in currently not enabled: passing a user pointer to
836 * dma-sg will result in segment sizes that are not a multiple of
837 * 512 bytes, which is required by the host controller.
838 */
839 .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
840 .buf_struct_size = sizeof(struct sur40_buffer),
841 .ops = &sur40_queue_ops,
842 .mem_ops = &vb2_dma_sg_memops,
843 .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
844 .min_buffers_needed = 3,
845};
846
847static const struct v4l2_file_operations sur40_video_fops = {
848 .owner = THIS_MODULE,
849 .open = v4l2_fh_open,
850 .release = vb2_fop_release,
851 .unlocked_ioctl = video_ioctl2,
852 .read = vb2_fop_read,
853 .mmap = vb2_fop_mmap,
854 .poll = vb2_fop_poll,
855};
856
857static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
858
859 .vidioc_querycap = sur40_vidioc_querycap,
860
861 .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
862 .vidioc_try_fmt_vid_cap = sur40_vidioc_fmt,
863 .vidioc_s_fmt_vid_cap = sur40_vidioc_fmt,
864 .vidioc_g_fmt_vid_cap = sur40_vidioc_fmt,
865
Florian Echtlerda6e4672015-05-25 09:04:14 -0300866 .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
867 .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
868
Florian Echtlere831cd22015-03-16 06:48:23 -0300869 .vidioc_enum_input = sur40_vidioc_enum_input,
870 .vidioc_g_input = sur40_vidioc_g_input,
871 .vidioc_s_input = sur40_vidioc_s_input,
872
873 .vidioc_reqbufs = vb2_ioctl_reqbufs,
874 .vidioc_create_bufs = vb2_ioctl_create_bufs,
875 .vidioc_querybuf = vb2_ioctl_querybuf,
876 .vidioc_qbuf = vb2_ioctl_qbuf,
877 .vidioc_dqbuf = vb2_ioctl_dqbuf,
878 .vidioc_expbuf = vb2_ioctl_expbuf,
879
880 .vidioc_streamon = vb2_ioctl_streamon,
881 .vidioc_streamoff = vb2_ioctl_streamoff,
882};
883
884static const struct video_device sur40_video_device = {
885 .name = DRIVER_LONG,
886 .fops = &sur40_video_fops,
887 .ioctl_ops = &sur40_video_ioctl_ops,
888 .release = video_device_release_empty,
889};
890
891static const struct v4l2_pix_format sur40_video_format = {
892 .pixelformat = V4L2_PIX_FMT_GREY,
893 .width = SENSOR_RES_X / 2,
894 .height = SENSOR_RES_Y / 2,
895 .field = V4L2_FIELD_NONE,
896 .colorspace = V4L2_COLORSPACE_SRGB,
897 .bytesperline = SENSOR_RES_X / 2,
898 .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
899};
900
Florian Echtlerbdb5c572013-11-08 10:01:13 -0800901/* USB-specific object needed to register this driver with the USB subsystem. */
902static struct usb_driver sur40_driver = {
903 .name = DRIVER_SHORT,
904 .probe = sur40_probe,
905 .disconnect = sur40_disconnect,
906 .id_table = sur40_table,
907};
908
909module_usb_driver(sur40_driver);
910
911MODULE_AUTHOR(DRIVER_AUTHOR);
912MODULE_DESCRIPTION(DRIVER_DESC);
913MODULE_LICENSE("GPL");