sur40.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Surface2.0/SUR40/PixelSense input driver
  3. *
  4. * Copyright (c) 2013 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. * 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.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/delay.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/completion.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/usb.h>
  29. #include <linux/printk.h>
  30. #include <linux/input-polldev.h>
  31. #include <linux/input/mt.h>
  32. #include <linux/usb/input.h>
  33. /* read 512 bytes from endpoint 0x86 -> get header + blobs */
  34. struct sur40_header {
  35. __le16 type; /* always 0x0001 */
  36. __le16 count; /* count of blobs (if 0: continue prev. packet) */
  37. __le32 packet_id; /* unique ID for all packets in one frame */
  38. __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
  39. __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
  40. } __packed;
  41. struct sur40_blob {
  42. __le16 blob_id;
  43. u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
  44. u8 unknown; /* always 0x01 or 0x02 (no idea what this is?) */
  45. __le16 bb_pos_x; /* upper left corner of bounding box */
  46. __le16 bb_pos_y;
  47. __le16 bb_size_x; /* size of bounding box */
  48. __le16 bb_size_y;
  49. __le16 pos_x; /* finger tip position */
  50. __le16 pos_y;
  51. __le16 ctr_x; /* centroid position */
  52. __le16 ctr_y;
  53. __le16 axis_x; /* somehow related to major/minor axis, mostly: */
  54. __le16 axis_y; /* axis_x == bb_size_y && axis_y == bb_size_x */
  55. __le32 angle; /* orientation in radians relative to x axis -
  56. actually an IEEE754 float, don't use in kernel */
  57. __le32 area; /* size in pixels/pressure (?) */
  58. u8 padding[32];
  59. } __packed;
  60. /* combined header/blob data */
  61. struct sur40_data {
  62. struct sur40_header header;
  63. struct sur40_blob blobs[];
  64. } __packed;
  65. /* version information */
  66. #define DRIVER_SHORT "sur40"
  67. #define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
  68. #define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
  69. /* vendor and device IDs */
  70. #define ID_MICROSOFT 0x045e
  71. #define ID_SUR40 0x0775
  72. /* sensor resolution */
  73. #define SENSOR_RES_X 1920
  74. #define SENSOR_RES_Y 1080
  75. /* touch data endpoint */
  76. #define TOUCH_ENDPOINT 0x86
  77. /* polling interval (ms) */
  78. #define POLL_INTERVAL 10
  79. /* maximum number of contacts FIXME: this is a guess? */
  80. #define MAX_CONTACTS 64
  81. /* control commands */
  82. #define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
  83. #define SUR40_UNKNOWN1 0xb3 /* 5 bytes */
  84. #define SUR40_UNKNOWN2 0xc1 /* 24 bytes */
  85. #define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
  86. #define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
  87. /*
  88. * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
  89. * here by mistake which is very likely to have corrupted the firmware EEPROM
  90. * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
  91. * Should you ever run into a similar problem, the background story to this
  92. * incident and instructions on how to fix the corrupted EEPROM are available
  93. * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
  94. */
  95. struct sur40_state {
  96. struct usb_device *usbdev;
  97. struct device *dev;
  98. struct input_polled_dev *input;
  99. struct sur40_data *bulk_in_buffer;
  100. size_t bulk_in_size;
  101. u8 bulk_in_epaddr;
  102. char phys[64];
  103. };
  104. static int sur40_command(struct sur40_state *dev,
  105. u8 command, u16 index, void *buffer, u16 size)
  106. {
  107. return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
  108. command,
  109. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  110. 0x00, index, buffer, size, 1000);
  111. }
  112. /* Initialization routine, called from sur40_open */
  113. static int sur40_init(struct sur40_state *dev)
  114. {
  115. int result;
  116. u8 buffer[24];
  117. /* stupidly replay the original MS driver init sequence */
  118. result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
  119. if (result < 0)
  120. return result;
  121. result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
  122. if (result < 0)
  123. return result;
  124. result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
  125. if (result < 0)
  126. return result;
  127. result = sur40_command(dev, SUR40_UNKNOWN2, 0x00, buffer, 24);
  128. if (result < 0)
  129. return result;
  130. result = sur40_command(dev, SUR40_UNKNOWN1, 0x00, buffer, 5);
  131. if (result < 0)
  132. return result;
  133. result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
  134. /*
  135. * Discard the result buffer - no known data inside except
  136. * some version strings, maybe extract these sometime...
  137. */
  138. return result;
  139. }
  140. /*
  141. * Callback routines from input_polled_dev
  142. */
  143. /* Enable the device, polling will now start. */
  144. static void sur40_open(struct input_polled_dev *polldev)
  145. {
  146. struct sur40_state *sur40 = polldev->private;
  147. dev_dbg(sur40->dev, "open\n");
  148. sur40_init(sur40);
  149. }
  150. /* Disable device, polling has stopped. */
  151. static void sur40_close(struct input_polled_dev *polldev)
  152. {
  153. struct sur40_state *sur40 = polldev->private;
  154. dev_dbg(sur40->dev, "close\n");
  155. /*
  156. * There is no known way to stop the device, so we simply
  157. * stop polling.
  158. */
  159. }
  160. /*
  161. * This function is called when a whole contact has been processed,
  162. * so that it can assign it to a slot and store the data there.
  163. */
  164. static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
  165. {
  166. int wide, major, minor;
  167. int bb_size_x = le16_to_cpu(blob->bb_size_x);
  168. int bb_size_y = le16_to_cpu(blob->bb_size_y);
  169. int pos_x = le16_to_cpu(blob->pos_x);
  170. int pos_y = le16_to_cpu(blob->pos_y);
  171. int ctr_x = le16_to_cpu(blob->ctr_x);
  172. int ctr_y = le16_to_cpu(blob->ctr_y);
  173. int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
  174. if (slotnum < 0 || slotnum >= MAX_CONTACTS)
  175. return;
  176. input_mt_slot(input, slotnum);
  177. input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
  178. wide = (bb_size_x > bb_size_y);
  179. major = max(bb_size_x, bb_size_y);
  180. minor = min(bb_size_x, bb_size_y);
  181. input_report_abs(input, ABS_MT_POSITION_X, pos_x);
  182. input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
  183. input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
  184. input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
  185. /* TODO: use a better orientation measure */
  186. input_report_abs(input, ABS_MT_ORIENTATION, wide);
  187. input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
  188. input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
  189. }
  190. /* core function: poll for new input data */
  191. static void sur40_poll(struct input_polled_dev *polldev)
  192. {
  193. struct sur40_state *sur40 = polldev->private;
  194. struct input_dev *input = polldev->input;
  195. int result, bulk_read, need_blobs, packet_blobs, i;
  196. u32 uninitialized_var(packet_id);
  197. struct sur40_header *header = &sur40->bulk_in_buffer->header;
  198. struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
  199. dev_dbg(sur40->dev, "poll\n");
  200. need_blobs = -1;
  201. do {
  202. /* perform a blocking bulk read to get data from the device */
  203. result = usb_bulk_msg(sur40->usbdev,
  204. usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
  205. sur40->bulk_in_buffer, sur40->bulk_in_size,
  206. &bulk_read, 1000);
  207. dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
  208. if (result < 0) {
  209. dev_err(sur40->dev, "error in usb_bulk_read\n");
  210. return;
  211. }
  212. result = bulk_read - sizeof(struct sur40_header);
  213. if (result % sizeof(struct sur40_blob) != 0) {
  214. dev_err(sur40->dev, "transfer size mismatch\n");
  215. return;
  216. }
  217. /* first packet? */
  218. if (need_blobs == -1) {
  219. need_blobs = le16_to_cpu(header->count);
  220. dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
  221. packet_id = le32_to_cpu(header->packet_id);
  222. }
  223. /*
  224. * Sanity check. when video data is also being retrieved, the
  225. * packet ID will usually increase in the middle of a series
  226. * instead of at the end.
  227. */
  228. if (packet_id != header->packet_id)
  229. dev_warn(sur40->dev, "packet ID mismatch\n");
  230. packet_blobs = result / sizeof(struct sur40_blob);
  231. dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
  232. /* packets always contain at least 4 blobs, even if empty */
  233. if (packet_blobs > need_blobs)
  234. packet_blobs = need_blobs;
  235. for (i = 0; i < packet_blobs; i++) {
  236. need_blobs--;
  237. dev_dbg(sur40->dev, "processing blob\n");
  238. sur40_report_blob(&(inblob[i]), input);
  239. }
  240. } while (need_blobs > 0);
  241. input_mt_sync_frame(input);
  242. input_sync(input);
  243. }
  244. /* Initialize input device parameters. */
  245. static void sur40_input_setup(struct input_dev *input_dev)
  246. {
  247. __set_bit(EV_KEY, input_dev->evbit);
  248. __set_bit(EV_ABS, input_dev->evbit);
  249. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  250. 0, SENSOR_RES_X, 0, 0);
  251. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  252. 0, SENSOR_RES_Y, 0, 0);
  253. input_set_abs_params(input_dev, ABS_MT_TOOL_X,
  254. 0, SENSOR_RES_X, 0, 0);
  255. input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
  256. 0, SENSOR_RES_Y, 0, 0);
  257. /* max value unknown, but major/minor axis
  258. * can never be larger than screen */
  259. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  260. 0, SENSOR_RES_X, 0, 0);
  261. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
  262. 0, SENSOR_RES_Y, 0, 0);
  263. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  264. input_mt_init_slots(input_dev, MAX_CONTACTS,
  265. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  266. }
  267. /* Check candidate USB interface. */
  268. static int sur40_probe(struct usb_interface *interface,
  269. const struct usb_device_id *id)
  270. {
  271. struct usb_device *usbdev = interface_to_usbdev(interface);
  272. struct sur40_state *sur40;
  273. struct usb_host_interface *iface_desc;
  274. struct usb_endpoint_descriptor *endpoint;
  275. struct input_polled_dev *poll_dev;
  276. int error;
  277. /* Check if we really have the right interface. */
  278. iface_desc = &interface->altsetting[0];
  279. if (iface_desc->desc.bInterfaceClass != 0xFF)
  280. return -ENODEV;
  281. /* Use endpoint #4 (0x86). */
  282. endpoint = &iface_desc->endpoint[4].desc;
  283. if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
  284. return -ENODEV;
  285. /* Allocate memory for our device state and initialize it. */
  286. sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
  287. if (!sur40)
  288. return -ENOMEM;
  289. poll_dev = input_allocate_polled_device();
  290. if (!poll_dev) {
  291. error = -ENOMEM;
  292. goto err_free_dev;
  293. }
  294. /* Set up polled input device control structure */
  295. poll_dev->private = sur40;
  296. poll_dev->poll_interval = POLL_INTERVAL;
  297. poll_dev->open = sur40_open;
  298. poll_dev->poll = sur40_poll;
  299. poll_dev->close = sur40_close;
  300. /* Set up regular input device structure */
  301. sur40_input_setup(poll_dev->input);
  302. poll_dev->input->name = "Samsung SUR40";
  303. usb_to_input_id(usbdev, &poll_dev->input->id);
  304. usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
  305. strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
  306. poll_dev->input->phys = sur40->phys;
  307. poll_dev->input->dev.parent = &interface->dev;
  308. sur40->usbdev = usbdev;
  309. sur40->dev = &interface->dev;
  310. sur40->input = poll_dev;
  311. /* use the bulk-in endpoint tested above */
  312. sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
  313. sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
  314. sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
  315. if (!sur40->bulk_in_buffer) {
  316. dev_err(&interface->dev, "Unable to allocate input buffer.");
  317. error = -ENOMEM;
  318. goto err_free_polldev;
  319. }
  320. error = input_register_polled_device(poll_dev);
  321. if (error) {
  322. dev_err(&interface->dev,
  323. "Unable to register polled input device.");
  324. goto err_free_buffer;
  325. }
  326. /* we can register the device now, as it is ready */
  327. usb_set_intfdata(interface, sur40);
  328. dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
  329. return 0;
  330. err_free_buffer:
  331. kfree(sur40->bulk_in_buffer);
  332. err_free_polldev:
  333. input_free_polled_device(sur40->input);
  334. err_free_dev:
  335. kfree(sur40);
  336. return error;
  337. }
  338. /* Unregister device & clean up. */
  339. static void sur40_disconnect(struct usb_interface *interface)
  340. {
  341. struct sur40_state *sur40 = usb_get_intfdata(interface);
  342. input_unregister_polled_device(sur40->input);
  343. input_free_polled_device(sur40->input);
  344. kfree(sur40->bulk_in_buffer);
  345. kfree(sur40);
  346. usb_set_intfdata(interface, NULL);
  347. dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
  348. }
  349. static const struct usb_device_id sur40_table[] = {
  350. { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
  351. { } /* terminating null entry */
  352. };
  353. MODULE_DEVICE_TABLE(usb, sur40_table);
  354. /* USB-specific object needed to register this driver with the USB subsystem. */
  355. static struct usb_driver sur40_driver = {
  356. .name = DRIVER_SHORT,
  357. .probe = sur40_probe,
  358. .disconnect = sur40_disconnect,
  359. .id_table = sur40_table,
  360. };
  361. module_usb_driver(sur40_driver);
  362. MODULE_AUTHOR(DRIVER_AUTHOR);
  363. MODULE_DESCRIPTION(DRIVER_DESC);
  364. MODULE_LICENSE("GPL");