2 * Surface2.0/SUR40/PixelSense input driver
4 * Copyright (c) 2013 by Florian 'floe' Echtler <floe@butterbrot.org>
6 * Derived from the USB Skeleton driver 1.1,
7 * Copyright (c) 2003 Greg Kroah-Hartman (greg@kroah.com)
9 * and from the Apple USB BCM5974 multitouch driver,
10 * Copyright (c) 2008 Henrik Rydberg (rydberg@euromail.se)
12 * and from the generic hid-multitouch driver,
13 * Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/completion.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/printk.h>
31 #include <linux/input-polldev.h>
32 #include <linux/input/mt.h>
33 #include <linux/usb/input.h>
35 /* read 512 bytes from endpoint 0x86 -> get header + blobs */
38 __le16 type; /* always 0x0001 */
39 __le16 count; /* count of blobs (if 0: continue prev. packet) */
41 __le32 packet_id; /* unique ID for all packets in one frame */
43 __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
44 __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
52 u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
53 u8 unknown; /* always 0x01 or 0x02 (no idea what this is?) */
55 __le16 bb_pos_x; /* upper left corner of bounding box */
58 __le16 bb_size_x; /* size of bounding box */
61 __le16 pos_x; /* finger tip position */
64 __le16 ctr_x; /* centroid position */
67 __le16 axis_x; /* somehow related to major/minor axis, mostly: */
68 __le16 axis_y; /* axis_x == bb_size_y && axis_y == bb_size_x */
70 __le32 angle; /* orientation in radians relative to x axis -
71 actually an IEEE754 float, don't use in kernel */
73 __le32 area; /* size in pixels/pressure (?) */
79 /* combined header/blob data */
81 struct sur40_header header;
82 struct sur40_blob blobs[];
86 /* version information */
87 #define DRIVER_SHORT "sur40"
88 #define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
89 #define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
91 /* vendor and device IDs */
92 #define ID_MICROSOFT 0x045e
93 #define ID_SUR40 0x0775
95 /* sensor resolution */
96 #define SENSOR_RES_X 1920
97 #define SENSOR_RES_Y 1080
99 /* touch data endpoint */
100 #define TOUCH_ENDPOINT 0x86
102 /* polling interval (ms) */
103 #define POLL_INTERVAL 10
105 /* maximum number of contacts FIXME: this is a guess? */
106 #define MAX_CONTACTS 64
108 /* control commands */
109 #define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
110 #define SUR40_UNKNOWN1 0xb3 /* 5 bytes */
111 #define SUR40_UNKNOWN2 0xc1 /* 24 bytes */
113 #define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
114 #define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
117 * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
118 * here by mistake which is very likely to have corrupted the firmware EEPROM
119 * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
120 * Should you ever run into a similar problem, the background story to this
121 * incident and instructions on how to fix the corrupted EEPROM are available
122 * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
127 struct usb_device *usbdev;
129 struct input_polled_dev *input;
131 struct sur40_data *bulk_in_buffer;
138 static int sur40_command(struct sur40_state *dev,
139 u8 command, u16 index, void *buffer, u16 size)
141 return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
143 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
144 0x00, index, buffer, size, 1000);
147 /* Initialization routine, called from sur40_open */
148 static int sur40_init(struct sur40_state *dev)
153 /* stupidly replay the original MS driver init sequence */
154 result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
158 result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
162 result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
166 result = sur40_command(dev, SUR40_UNKNOWN2, 0x00, buffer, 24);
170 result = sur40_command(dev, SUR40_UNKNOWN1, 0x00, buffer, 5);
174 result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
177 * Discard the result buffer - no known data inside except
178 * some version strings, maybe extract these sometime...
185 * Callback routines from input_polled_dev
188 /* Enable the device, polling will now start. */
189 static void sur40_open(struct input_polled_dev *polldev)
191 struct sur40_state *sur40 = polldev->private;
193 dev_dbg(sur40->dev, "open\n");
197 /* Disable device, polling has stopped. */
198 static void sur40_close(struct input_polled_dev *polldev)
200 struct sur40_state *sur40 = polldev->private;
202 dev_dbg(sur40->dev, "close\n");
204 * There is no known way to stop the device, so we simply
210 * This function is called when a whole contact has been processed,
211 * so that it can assign it to a slot and store the data there.
213 static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
215 int wide, major, minor;
217 int bb_size_x = le16_to_cpu(blob->bb_size_x);
218 int bb_size_y = le16_to_cpu(blob->bb_size_y);
220 int pos_x = le16_to_cpu(blob->pos_x);
221 int pos_y = le16_to_cpu(blob->pos_y);
223 int ctr_x = le16_to_cpu(blob->ctr_x);
224 int ctr_y = le16_to_cpu(blob->ctr_y);
226 int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
227 if (slotnum < 0 || slotnum >= MAX_CONTACTS)
230 input_mt_slot(input, slotnum);
231 input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
232 wide = (bb_size_x > bb_size_y);
233 major = max(bb_size_x, bb_size_y);
234 minor = min(bb_size_x, bb_size_y);
236 input_report_abs(input, ABS_MT_POSITION_X, pos_x);
237 input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
238 input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
239 input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
241 /* TODO: use a better orientation measure */
242 input_report_abs(input, ABS_MT_ORIENTATION, wide);
243 input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
244 input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
247 /* core function: poll for new input data */
248 static void sur40_poll(struct input_polled_dev *polldev)
251 struct sur40_state *sur40 = polldev->private;
252 struct input_dev *input = polldev->input;
253 int result, bulk_read, need_blobs, packet_blobs, i;
254 u32 uninitialized_var(packet_id);
256 struct sur40_header *header = &sur40->bulk_in_buffer->header;
257 struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
259 dev_dbg(sur40->dev, "poll\n");
265 /* perform a blocking bulk read to get data from the device */
266 result = usb_bulk_msg(sur40->usbdev,
267 usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
268 sur40->bulk_in_buffer, sur40->bulk_in_size,
271 dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
274 dev_err(sur40->dev, "error in usb_bulk_read\n");
278 result = bulk_read - sizeof(struct sur40_header);
280 if (result % sizeof(struct sur40_blob) != 0) {
281 dev_err(sur40->dev, "transfer size mismatch\n");
286 if (need_blobs == -1) {
287 need_blobs = le16_to_cpu(header->count);
288 dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
289 packet_id = le32_to_cpu(header->packet_id);
293 * Sanity check. when video data is also being retrieved, the
294 * packet ID will usually increase in the middle of a series
295 * instead of at the end.
297 if (packet_id != header->packet_id)
298 dev_warn(sur40->dev, "packet ID mismatch\n");
300 packet_blobs = result / sizeof(struct sur40_blob);
301 dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
303 /* packets always contain at least 4 blobs, even if empty */
304 if (packet_blobs > need_blobs)
305 packet_blobs = need_blobs;
307 for (i = 0; i < packet_blobs; i++) {
309 dev_dbg(sur40->dev, "processing blob\n");
310 sur40_report_blob(&(inblob[i]), input);
313 } while (need_blobs > 0);
315 input_mt_sync_frame(input);
319 /* Initialize input device parameters. */
320 static void sur40_input_setup(struct input_dev *input_dev)
322 __set_bit(EV_KEY, input_dev->evbit);
323 __set_bit(EV_ABS, input_dev->evbit);
325 input_set_abs_params(input_dev, ABS_MT_POSITION_X,
326 0, SENSOR_RES_X, 0, 0);
327 input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
328 0, SENSOR_RES_Y, 0, 0);
330 input_set_abs_params(input_dev, ABS_MT_TOOL_X,
331 0, SENSOR_RES_X, 0, 0);
332 input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
333 0, SENSOR_RES_Y, 0, 0);
335 /* max value unknown, but major/minor axis
336 * can never be larger than screen */
337 input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
338 0, SENSOR_RES_X, 0, 0);
339 input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
340 0, SENSOR_RES_Y, 0, 0);
342 input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
344 input_mt_init_slots(input_dev, MAX_CONTACTS,
345 INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
348 /* Check candidate USB interface. */
349 static int sur40_probe(struct usb_interface *interface,
350 const struct usb_device_id *id)
352 struct usb_device *usbdev = interface_to_usbdev(interface);
353 struct sur40_state *sur40;
354 struct usb_host_interface *iface_desc;
355 struct usb_endpoint_descriptor *endpoint;
356 struct input_polled_dev *poll_dev;
359 /* Check if we really have the right interface. */
360 iface_desc = &interface->altsetting[0];
361 if (iface_desc->desc.bInterfaceClass != 0xFF)
364 /* Use endpoint #4 (0x86). */
365 endpoint = &iface_desc->endpoint[4].desc;
366 if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
369 /* Allocate memory for our device state and initialize it. */
370 sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
374 poll_dev = input_allocate_polled_device();
380 /* Set up polled input device control structure */
381 poll_dev->private = sur40;
382 poll_dev->poll_interval = POLL_INTERVAL;
383 poll_dev->open = sur40_open;
384 poll_dev->poll = sur40_poll;
385 poll_dev->close = sur40_close;
387 /* Set up regular input device structure */
388 sur40_input_setup(poll_dev->input);
390 poll_dev->input->name = "Samsung SUR40";
391 usb_to_input_id(usbdev, &poll_dev->input->id);
392 usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
393 strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
394 poll_dev->input->phys = sur40->phys;
395 poll_dev->input->dev.parent = &interface->dev;
397 sur40->usbdev = usbdev;
398 sur40->dev = &interface->dev;
399 sur40->input = poll_dev;
401 /* use the bulk-in endpoint tested above */
402 sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
403 sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
404 sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
405 if (!sur40->bulk_in_buffer) {
406 dev_err(&interface->dev, "Unable to allocate input buffer.");
408 goto err_free_polldev;
411 error = input_register_polled_device(poll_dev);
413 dev_err(&interface->dev,
414 "Unable to register polled input device.");
415 goto err_free_buffer;
418 /* we can register the device now, as it is ready */
419 usb_set_intfdata(interface, sur40);
420 dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
425 kfree(sur40->bulk_in_buffer);
427 input_free_polled_device(sur40->input);
434 /* Unregister device & clean up. */
435 static void sur40_disconnect(struct usb_interface *interface)
437 struct sur40_state *sur40 = usb_get_intfdata(interface);
439 input_unregister_polled_device(sur40->input);
440 input_free_polled_device(sur40->input);
441 kfree(sur40->bulk_in_buffer);
444 usb_set_intfdata(interface, NULL);
445 dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
448 static const struct usb_device_id sur40_table[] = {
449 { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
450 { } /* terminating null entry */
452 MODULE_DEVICE_TABLE(usb, sur40_table);
454 /* USB-specific object needed to register this driver with the USB subsystem. */
455 static struct usb_driver sur40_driver = {
456 .name = DRIVER_SHORT,
457 .probe = sur40_probe,
458 .disconnect = sur40_disconnect,
459 .id_table = sur40_table,
462 module_usb_driver(sur40_driver);
464 MODULE_AUTHOR(DRIVER_AUTHOR);
465 MODULE_DESCRIPTION(DRIVER_DESC);
466 MODULE_LICENSE("GPL");