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