/* Surface2.0/SUR40/PixelSense input driver v0.0.1 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Copyright (C) 2013 by Florian 'floe' Echtler Derived from the USB Skeleton driver 1.1, Copyright (C) 2003 Greg Kroah-Hartman (greg@kroah.com) Derived from the Apple USB BCM5974 multitouch driver, Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se) */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "surface-data.h" /* version information */ #define DRIVER_VERSION "0.0.1" #define DRIVER_SHORT "surface-input" #define DRIVER_AUTHOR "Florian 'floe' Echtler " #define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver" /* vendor and device IDs */ #define ID_MICROSOFT 0x045e #define ID_SURFACE 0x0775 /* touch data endpoint */ #define TOUCH_ENDPOINT 0x86 /* polling interval (ms) */ #define POLL_INTERVAL 10 /* maximum number of contacts */ #define MAX_CONTACTS 64 /* device ID table */ static const struct usb_device_id surface_table[] = { {USB_DEVICE(ID_MICROSOFT, ID_SURFACE)}, /* Microsoft Surface 2.0 */ {} /* terminating null entry */ }; /* control commands */ #define SURFACE_GET_VERSION 0xb0 /* 12 bytes string */ #define SURFACE_UNKNOWN1 0xb3 /* 5 bytes */ #define SURFACE_UNKNOWN2 0xc1 /* 24 bytes */ #define SURFACE_GET_STATE 0xc5 /* 4 bytes state (?) */ #define SURFACE_GET_SENSORS 0xb1 /* 8 bytes sensors */ /* FIXME: previous version used USB_RECIP_ENDPOINT, may have corrupted eeprom? */ #define surface_command(dev, command, index, buffer, size) \ usb_control_msg (dev->usbdev, usb_rcvctrlpipe (dev->usbdev, 0), command, \ USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 0x00, index, buffer, size, 1000) MODULE_DEVICE_TABLE(usb, surface_table); /* structure to hold all of our device specific stuff */ struct usb_surface { struct usb_device *usbdev; /* save the usb device pointer */ struct usb_interface *interface; /* the interface for this device */ struct input_polled_dev *input; /* struct for polled input device */ unsigned char *bulk_in_buffer; /* the buffer to receive data */ size_t bulk_in_size; /* the maximum bulk packet size */ __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ char phys[64]; }; /* initialization routine, called from surface_open */ static int surface_init(struct usb_surface *dev) { int result; __u8 buffer[24]; /* stupidly replay the original MS driver init sequence */ result = surface_command(dev, SURFACE_GET_VERSION, 0x00, buffer, 12 ); if (result < 0) return result; result = surface_command(dev, SURFACE_GET_VERSION, 0x01, buffer, 12 ); if (result < 0) return result; result = surface_command(dev, SURFACE_GET_VERSION, 0x02, buffer, 12 ); if (result < 0) return result; result = surface_command(dev, SURFACE_UNKNOWN2, 0x00, buffer, 24 ); if (result < 0) return result; result = surface_command(dev, SURFACE_UNKNOWN1, 0x00, buffer, 5 ); if (result < 0) return result; result = surface_command(dev, SURFACE_GET_VERSION, 0x03, buffer, 12 ); /* discard the result buffer - no known data inside except some version strings, maybe extract these sometime.. */ return result; } /* * callback routines from input_polled_dev */ /* enable the device, polling will now start */ void surface_open(struct input_polled_dev *polldev) { struct usb_surface *surface = polldev->private; pr_info("surface_open"); surface_init(surface); } /* disable device, polling has stopped */ void surface_close(struct input_polled_dev *polldev) { /* no known way to stop the device, except to stop polling */ pr_info("surface_close"); } /* * this function is called when a whole contact has been processed, * so that it can assign it to a slot and store the data there */ static void report_blob(struct surface_blob *blob, struct input_dev *input) { int wide, major, minor; int slotnum = input_mt_get_slot_by_key(input, blob->blob_id); if (slotnum < 0 || slotnum >= MAX_CONTACTS) return; /* FIXME: is this needed for the Pixelsense? */ /*if ((td->mtclass.quirks & MT_QUIRK_IGNORE_DUPLICATES) && mt) { struct input_mt *mt = input->mt; struct input_mt_slot *slot = &mt->slots[slotnum]; if (input_mt_is_active(slot) && input_mt_is_used(mt, slot)) return; }*/ input_mt_slot(input, slotnum); input_mt_report_slot_state(input, MT_TOOL_FINGER, 1); wide = (blob->bb_size_x > blob->bb_size_y); major = max(blob->bb_size_x, blob->bb_size_y); minor = min(blob->bb_size_x, blob->bb_size_y); input_event(input, EV_ABS, ABS_MT_POSITION_X, blob->pos_x); input_event(input, EV_ABS, ABS_MT_POSITION_Y, blob->pos_y); input_event(input, EV_ABS, ABS_MT_TOOL_X, blob->ctr_x); input_event(input, EV_ABS, ABS_MT_TOOL_Y, blob->ctr_y); input_event(input, EV_ABS, ABS_MT_ORIENTATION, wide); input_event(input, EV_ABS, ABS_MT_TOUCH_MAJOR, major); input_event(input, EV_ABS, ABS_MT_TOUCH_MINOR, minor); } /* core function: poll for new input data */ void surface_poll(struct input_polled_dev *polldev) { struct usb_surface *surface = polldev->private; struct input_dev *input = polldev->input; int result, bulk_read, need_blobs, packet_blobs, i; uint32_t packet_id; struct surface_header* header = (struct surface_header*)(surface->bulk_in_buffer); struct surface_blob* inblob = (struct surface_blob*)(surface->bulk_in_buffer+sizeof(struct surface_header)); need_blobs = -1; pr_info("surface_poll\n"); do { /* perform a blocking bulk read to get data from the device */ result = usb_bulk_msg (surface->usbdev, usb_rcvbulkpipe (surface->usbdev, surface->bulk_in_endpointAddr), surface->bulk_in_buffer, 512, &bulk_read, 1000); pr_info("got %d bytes\n",bulk_read); if (result < 0) { pr_err("error in usb_bulk_read"); return; } result = bulk_read - sizeof(struct surface_header); if (result % sizeof(struct surface_blob) != 0) { pr_err("transfer size mismatch"); return; } /* first packet? */ if (need_blobs == -1) { need_blobs = header->count; pr_info("expecting %d blobs\n",need_blobs); packet_id = header->packet_id; } /* sanity check. when video data is also being retrieved, the packet ID will usually increase in the middle of a series instead of at the end. */ if (packet_id != header->packet_id) pr_warn("packet ID mismatch\n"); packet_blobs = result / sizeof(struct surface_blob); pr_info("got %d blobs\n",packet_blobs); /* packets always contain at least 4 blobs, even if they are empty */ if (packet_blobs > need_blobs) packet_blobs = need_blobs; for (i = 0; i < packet_blobs; i++) { need_blobs--; pr_info("got a blob\n"); report_blob( &(inblob[i]), input ); } } while (need_blobs > 0); input_mt_sync_frame(input); input_sync(input); } /* * power management routines */ /* PM operations are nops as this driver does IO only during open() */ /*static int surface_suspend(struct usb_interface *intf, pm_message_t message) { return 0; } static int surface_resume(struct usb_interface *intf) { return 0; }*/ /* * housekeeping routines */ /* clean up all allocated buffers/structs */ static inline void surface_delete(struct usb_surface *surface) { input_free_polled_device(surface->input); kfree(surface->bulk_in_buffer); kfree(surface); } /* check candidate USB interface */ static int surface_probe(struct usb_interface *interface, const struct usb_device_id *id) { struct usb_device *usbdev = interface_to_usbdev(interface); struct usb_surface *surface; struct usb_host_interface *iface_desc; struct usb_endpoint_descriptor *endpoint; struct input_polled_dev* poll_dev; /* check if we really have the right interface */ iface_desc = &interface->altsetting[0]; if (iface_desc->desc.bInterfaceClass != 0xFF) return -ENODEV; /* allocate memory for our device state and initialize it */ surface = kzalloc(sizeof(struct usb_surface), GFP_KERNEL); poll_dev = input_allocate_polled_device(); surface->input = poll_dev; if (!surface || !poll_dev) return -ENOMEM; poll_dev->private = surface; poll_dev->poll_interval = POLL_INTERVAL; poll_dev->open = surface_open; poll_dev->poll = surface_poll; poll_dev->close = surface_close; poll_dev->input->name = "Samsung SUR40"; usb_to_input_id(usbdev,&(poll_dev->input->id)); usb_make_path(usbdev, surface->phys, sizeof(surface->phys)); strlcat(surface->phys, "/input0", sizeof(surface->phys)); poll_dev->input->phys = surface->phys; input_mt_init_slots(poll_dev->input, MAX_CONTACTS, INPUT_MT_DIRECT); surface->usbdev = usbdev; surface->interface = interface; /* use endpoint #4 (0x86) */ endpoint = &iface_desc->endpoint[4].desc; if (endpoint->bEndpointAddress == TOUCH_ENDPOINT) { /* we found a bulk in endpoint */ surface->bulk_in_size = le16_to_cpu(endpoint->wMaxPacketSize); surface->bulk_in_endpointAddr = endpoint->bEndpointAddress; surface->bulk_in_buffer = kmalloc(2*surface->bulk_in_size, GFP_KERNEL); if (!surface->bulk_in_buffer) { pr_err("Unable to allocate input buffer."); surface_delete(surface); return -ENOMEM; } } if (!(surface->bulk_in_endpointAddr)) { pr_err("Unable to find bulk-in endpoint."); surface_delete(surface); return -ENODEV; } /* we can register the device now, as it is ready */ usb_set_intfdata(interface, surface); if (input_register_polled_device(poll_dev)) { pr_err("Unable to register polled input device."); surface_delete(surface); return -ENODEV; } dev_info(&interface->dev,"%s now attached\n",DRIVER_DESC); return 0; } /* unregister device & clean up */ static void surface_disconnect(struct usb_interface *interface) { struct usb_surface *surface = usb_get_intfdata(interface); input_unregister_polled_device(surface->input); usb_set_intfdata(interface, NULL); surface_delete(surface); dev_info(&interface->dev, "%s now disconnected\n",DRIVER_DESC); } /* usb specific object needed to register this driver with the usb subsystem */ static struct usb_driver surface_driver = { .name = DRIVER_SHORT, .probe = surface_probe, .disconnect = surface_disconnect, /*.suspend = surface_suspend, .resume = surface_resume,*/ .id_table = surface_table, /*.supports_autosuspend = 1,*/ }; module_usb_driver(surface_driver); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL");