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