sur40.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/delay.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/completion.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/usb.h>
  32. #include <linux/printk.h>
  33. #include <linux/input-polldev.h>
  34. #include <linux/input/mt.h>
  35. #include <linux/usb/input.h>
  36. #include <linux/videodev2.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-dev.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include <media/videobuf2-v4l2.h>
  41. #include <media/videobuf2-dma-sg.h>
  42. /* read 512 bytes from endpoint 0x86 -> get header + blobs */
  43. struct sur40_header {
  44. __le16 type; /* always 0x0001 */
  45. __le16 count; /* count of blobs (if 0: continue prev. packet) */
  46. __le32 packet_id; /* unique ID for all packets in one frame */
  47. __le32 timestamp; /* milliseconds (inc. by 16 or 17 each frame) */
  48. __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
  49. } __packed;
  50. struct sur40_blob {
  51. __le16 blob_id;
  52. u8 action; /* 0x02 = enter/exit, 0x03 = update (?) */
  53. u8 unknown; /* always 0x01 or 0x02 (no idea what this is?) */
  54. __le16 bb_pos_x; /* upper left corner of bounding box */
  55. __le16 bb_pos_y;
  56. __le16 bb_size_x; /* size of bounding box */
  57. __le16 bb_size_y;
  58. __le16 pos_x; /* finger tip position */
  59. __le16 pos_y;
  60. __le16 ctr_x; /* centroid position */
  61. __le16 ctr_y;
  62. __le16 axis_x; /* somehow related to major/minor axis, mostly: */
  63. __le16 axis_y; /* axis_x == bb_size_y && axis_y == bb_size_x */
  64. __le32 angle; /* orientation in radians relative to x axis -
  65. actually an IEEE754 float, don't use in kernel */
  66. __le32 area; /* size in pixels/pressure (?) */
  67. u8 padding[32];
  68. } __packed;
  69. /* combined header/blob data */
  70. struct sur40_data {
  71. struct sur40_header header;
  72. struct sur40_blob blobs[];
  73. } __packed;
  74. /* read 512 bytes from endpoint 0x82 -> get header below
  75. * continue reading 16k blocks until header.size bytes read */
  76. struct sur40_image_header {
  77. __le32 magic; /* "SUBF" */
  78. __le32 packet_id;
  79. __le32 size; /* always 0x0007e900 = 960x540 */
  80. __le32 timestamp; /* milliseconds (increases by 16 or 17 each frame) */
  81. __le32 unknown; /* "epoch?" always 02/03 00 00 00 */
  82. } __packed;
  83. /* version information */
  84. #define DRIVER_SHORT "sur40"
  85. #define DRIVER_LONG "Samsung SUR40"
  86. #define DRIVER_AUTHOR "Florian 'floe' Echtler <floe@butterbrot.org>"
  87. #define DRIVER_DESC "Surface2.0/SUR40/PixelSense input driver"
  88. /* vendor and device IDs */
  89. #define ID_MICROSOFT 0x045e
  90. #define ID_SUR40 0x0775
  91. /* sensor resolution */
  92. #define SENSOR_RES_X 1920
  93. #define SENSOR_RES_Y 1080
  94. /* touch data endpoint */
  95. #define TOUCH_ENDPOINT 0x86
  96. /* video data endpoint */
  97. #define VIDEO_ENDPOINT 0x82
  98. /* video header fields */
  99. #define VIDEO_HEADER_MAGIC 0x46425553
  100. #define VIDEO_PACKET_SIZE 16384
  101. /* polling interval (ms) */
  102. #define POLL_INTERVAL 1
  103. /* maximum number of contacts FIXME: this is a guess? */
  104. #define MAX_CONTACTS 64
  105. /* control commands */
  106. #define SUR40_GET_VERSION 0xb0 /* 12 bytes string */
  107. #define SUR40_UNKNOWN1 0xb3 /* 5 bytes */
  108. #define SUR40_UNKNOWN2 0xc1 /* 24 bytes */
  109. #define SUR40_GET_STATE 0xc5 /* 4 bytes state (?) */
  110. #define SUR40_GET_SENSORS 0xb1 /* 8 bytes sensors */
  111. static const struct v4l2_pix_format sur40_pix_format[] = {
  112. {
  113. .pixelformat = V4L2_TCH_FMT_TU08,
  114. .width = SENSOR_RES_X / 2,
  115. .height = SENSOR_RES_Y / 2,
  116. .field = V4L2_FIELD_NONE,
  117. .colorspace = V4L2_COLORSPACE_SRGB,
  118. .bytesperline = SENSOR_RES_X / 2,
  119. .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
  120. },
  121. {
  122. .pixelformat = V4L2_PIX_FMT_GREY,
  123. .width = SENSOR_RES_X / 2,
  124. .height = SENSOR_RES_Y / 2,
  125. .field = V4L2_FIELD_NONE,
  126. .colorspace = V4L2_COLORSPACE_SRGB,
  127. .bytesperline = SENSOR_RES_X / 2,
  128. .sizeimage = (SENSOR_RES_X/2) * (SENSOR_RES_Y/2),
  129. }
  130. };
  131. /* master device state */
  132. struct sur40_state {
  133. struct usb_device *usbdev;
  134. struct device *dev;
  135. struct input_polled_dev *input;
  136. struct v4l2_device v4l2;
  137. struct video_device vdev;
  138. struct mutex lock;
  139. struct v4l2_pix_format pix_fmt;
  140. struct vb2_queue queue;
  141. struct list_head buf_list;
  142. spinlock_t qlock;
  143. int sequence;
  144. struct sur40_data *bulk_in_buffer;
  145. size_t bulk_in_size;
  146. u8 bulk_in_epaddr;
  147. char phys[64];
  148. };
  149. struct sur40_buffer {
  150. struct vb2_v4l2_buffer vb;
  151. struct list_head list;
  152. };
  153. /* forward declarations */
  154. static const struct video_device sur40_video_device;
  155. static const struct vb2_queue sur40_queue;
  156. static void sur40_process_video(struct sur40_state *sur40);
  157. /*
  158. * Note: an earlier, non-public version of this driver used USB_RECIP_ENDPOINT
  159. * here by mistake which is very likely to have corrupted the firmware EEPROM
  160. * on two separate SUR40 devices. Thanks to Alan Stern who spotted this bug.
  161. * Should you ever run into a similar problem, the background story to this
  162. * incident and instructions on how to fix the corrupted EEPROM are available
  163. * at https://floe.butterbrot.org/matrix/hacking/surface/brick.html
  164. */
  165. /* command wrapper */
  166. static int sur40_command(struct sur40_state *dev,
  167. u8 command, u16 index, void *buffer, u16 size)
  168. {
  169. return usb_control_msg(dev->usbdev, usb_rcvctrlpipe(dev->usbdev, 0),
  170. command,
  171. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  172. 0x00, index, buffer, size, 1000);
  173. }
  174. /* Initialization routine, called from sur40_open */
  175. static int sur40_init(struct sur40_state *dev)
  176. {
  177. int result;
  178. u8 *buffer;
  179. buffer = kmalloc(24, GFP_KERNEL);
  180. if (!buffer) {
  181. result = -ENOMEM;
  182. goto error;
  183. }
  184. /* stupidly replay the original MS driver init sequence */
  185. result = sur40_command(dev, SUR40_GET_VERSION, 0x00, buffer, 12);
  186. if (result < 0)
  187. goto error;
  188. result = sur40_command(dev, SUR40_GET_VERSION, 0x01, buffer, 12);
  189. if (result < 0)
  190. goto error;
  191. result = sur40_command(dev, SUR40_GET_VERSION, 0x02, buffer, 12);
  192. if (result < 0)
  193. goto error;
  194. result = sur40_command(dev, SUR40_UNKNOWN2, 0x00, buffer, 24);
  195. if (result < 0)
  196. goto error;
  197. result = sur40_command(dev, SUR40_UNKNOWN1, 0x00, buffer, 5);
  198. if (result < 0)
  199. goto error;
  200. result = sur40_command(dev, SUR40_GET_VERSION, 0x03, buffer, 12);
  201. /*
  202. * Discard the result buffer - no known data inside except
  203. * some version strings, maybe extract these sometime...
  204. */
  205. error:
  206. kfree(buffer);
  207. return result;
  208. }
  209. /*
  210. * Callback routines from input_polled_dev
  211. */
  212. /* Enable the device, polling will now start. */
  213. static void sur40_open(struct input_polled_dev *polldev)
  214. {
  215. struct sur40_state *sur40 = polldev->private;
  216. dev_dbg(sur40->dev, "open\n");
  217. sur40_init(sur40);
  218. }
  219. /* Disable device, polling has stopped. */
  220. static void sur40_close(struct input_polled_dev *polldev)
  221. {
  222. struct sur40_state *sur40 = polldev->private;
  223. dev_dbg(sur40->dev, "close\n");
  224. /*
  225. * There is no known way to stop the device, so we simply
  226. * stop polling.
  227. */
  228. }
  229. /*
  230. * This function is called when a whole contact has been processed,
  231. * so that it can assign it to a slot and store the data there.
  232. */
  233. static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)
  234. {
  235. int wide, major, minor;
  236. int bb_size_x = le16_to_cpu(blob->bb_size_x);
  237. int bb_size_y = le16_to_cpu(blob->bb_size_y);
  238. int pos_x = le16_to_cpu(blob->pos_x);
  239. int pos_y = le16_to_cpu(blob->pos_y);
  240. int ctr_x = le16_to_cpu(blob->ctr_x);
  241. int ctr_y = le16_to_cpu(blob->ctr_y);
  242. int slotnum = input_mt_get_slot_by_key(input, blob->blob_id);
  243. if (slotnum < 0 || slotnum >= MAX_CONTACTS)
  244. return;
  245. input_mt_slot(input, slotnum);
  246. input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
  247. wide = (bb_size_x > bb_size_y);
  248. major = max(bb_size_x, bb_size_y);
  249. minor = min(bb_size_x, bb_size_y);
  250. input_report_abs(input, ABS_MT_POSITION_X, pos_x);
  251. input_report_abs(input, ABS_MT_POSITION_Y, pos_y);
  252. input_report_abs(input, ABS_MT_TOOL_X, ctr_x);
  253. input_report_abs(input, ABS_MT_TOOL_Y, ctr_y);
  254. /* TODO: use a better orientation measure */
  255. input_report_abs(input, ABS_MT_ORIENTATION, wide);
  256. input_report_abs(input, ABS_MT_TOUCH_MAJOR, major);
  257. input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
  258. }
  259. /* core function: poll for new input data */
  260. static void sur40_poll(struct input_polled_dev *polldev)
  261. {
  262. struct sur40_state *sur40 = polldev->private;
  263. struct input_dev *input = polldev->input;
  264. int result, bulk_read, need_blobs, packet_blobs, i;
  265. u32 uninitialized_var(packet_id);
  266. struct sur40_header *header = &sur40->bulk_in_buffer->header;
  267. struct sur40_blob *inblob = &sur40->bulk_in_buffer->blobs[0];
  268. dev_dbg(sur40->dev, "poll\n");
  269. need_blobs = -1;
  270. do {
  271. /* perform a blocking bulk read to get data from the device */
  272. result = usb_bulk_msg(sur40->usbdev,
  273. usb_rcvbulkpipe(sur40->usbdev, sur40->bulk_in_epaddr),
  274. sur40->bulk_in_buffer, sur40->bulk_in_size,
  275. &bulk_read, 1000);
  276. dev_dbg(sur40->dev, "received %d bytes\n", bulk_read);
  277. if (result < 0) {
  278. dev_err(sur40->dev, "error in usb_bulk_read\n");
  279. return;
  280. }
  281. result = bulk_read - sizeof(struct sur40_header);
  282. if (result % sizeof(struct sur40_blob) != 0) {
  283. dev_err(sur40->dev, "transfer size mismatch\n");
  284. return;
  285. }
  286. /* first packet? */
  287. if (need_blobs == -1) {
  288. need_blobs = le16_to_cpu(header->count);
  289. dev_dbg(sur40->dev, "need %d blobs\n", need_blobs);
  290. packet_id = le32_to_cpu(header->packet_id);
  291. }
  292. /*
  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.
  296. */
  297. if (packet_id != header->packet_id)
  298. dev_dbg(sur40->dev, "packet ID mismatch\n");
  299. packet_blobs = result / sizeof(struct sur40_blob);
  300. dev_dbg(sur40->dev, "received %d blobs\n", packet_blobs);
  301. /* packets always contain at least 4 blobs, even if empty */
  302. if (packet_blobs > need_blobs)
  303. packet_blobs = need_blobs;
  304. for (i = 0; i < packet_blobs; i++) {
  305. need_blobs--;
  306. dev_dbg(sur40->dev, "processing blob\n");
  307. sur40_report_blob(&(inblob[i]), input);
  308. }
  309. } while (need_blobs > 0);
  310. input_mt_sync_frame(input);
  311. input_sync(input);
  312. sur40_process_video(sur40);
  313. }
  314. /* deal with video data */
  315. static void sur40_process_video(struct sur40_state *sur40)
  316. {
  317. struct sur40_image_header *img = (void *)(sur40->bulk_in_buffer);
  318. struct sur40_buffer *new_buf;
  319. struct usb_sg_request sgr;
  320. struct sg_table *sgt;
  321. int result, bulk_read;
  322. if (!vb2_start_streaming_called(&sur40->queue))
  323. return;
  324. /* get a new buffer from the list */
  325. spin_lock(&sur40->qlock);
  326. if (list_empty(&sur40->buf_list)) {
  327. dev_dbg(sur40->dev, "buffer queue empty\n");
  328. spin_unlock(&sur40->qlock);
  329. return;
  330. }
  331. new_buf = list_entry(sur40->buf_list.next, struct sur40_buffer, list);
  332. list_del(&new_buf->list);
  333. spin_unlock(&sur40->qlock);
  334. dev_dbg(sur40->dev, "buffer acquired\n");
  335. /* retrieve data via bulk read */
  336. result = usb_bulk_msg(sur40->usbdev,
  337. usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT),
  338. sur40->bulk_in_buffer, sur40->bulk_in_size,
  339. &bulk_read, 1000);
  340. if (result < 0) {
  341. dev_err(sur40->dev, "error in usb_bulk_read\n");
  342. goto err_poll;
  343. }
  344. if (bulk_read != sizeof(struct sur40_image_header)) {
  345. dev_err(sur40->dev, "received %d bytes (%zd expected)\n",
  346. bulk_read, sizeof(struct sur40_image_header));
  347. goto err_poll;
  348. }
  349. if (le32_to_cpu(img->magic) != VIDEO_HEADER_MAGIC) {
  350. dev_err(sur40->dev, "image magic mismatch\n");
  351. goto err_poll;
  352. }
  353. if (le32_to_cpu(img->size) != sur40->pix_fmt.sizeimage) {
  354. dev_err(sur40->dev, "image size mismatch\n");
  355. goto err_poll;
  356. }
  357. dev_dbg(sur40->dev, "header acquired\n");
  358. sgt = vb2_dma_sg_plane_desc(&new_buf->vb.vb2_buf, 0);
  359. result = usb_sg_init(&sgr, sur40->usbdev,
  360. usb_rcvbulkpipe(sur40->usbdev, VIDEO_ENDPOINT), 0,
  361. sgt->sgl, sgt->nents, sur40->pix_fmt.sizeimage, 0);
  362. if (result < 0) {
  363. dev_err(sur40->dev, "error %d in usb_sg_init\n", result);
  364. goto err_poll;
  365. }
  366. usb_sg_wait(&sgr);
  367. if (sgr.status < 0) {
  368. dev_err(sur40->dev, "error %d in usb_sg_wait\n", sgr.status);
  369. goto err_poll;
  370. }
  371. dev_dbg(sur40->dev, "image acquired\n");
  372. /* return error if streaming was stopped in the meantime */
  373. if (sur40->sequence == -1)
  374. return;
  375. /* mark as finished */
  376. new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
  377. new_buf->vb.sequence = sur40->sequence++;
  378. new_buf->vb.field = V4L2_FIELD_NONE;
  379. vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  380. dev_dbg(sur40->dev, "buffer marked done\n");
  381. return;
  382. err_poll:
  383. vb2_buffer_done(&new_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  384. }
  385. /* Initialize input device parameters. */
  386. static void sur40_input_setup(struct input_dev *input_dev)
  387. {
  388. __set_bit(EV_KEY, input_dev->evbit);
  389. __set_bit(EV_ABS, input_dev->evbit);
  390. input_set_abs_params(input_dev, ABS_MT_POSITION_X,
  391. 0, SENSOR_RES_X, 0, 0);
  392. input_set_abs_params(input_dev, ABS_MT_POSITION_Y,
  393. 0, SENSOR_RES_Y, 0, 0);
  394. input_set_abs_params(input_dev, ABS_MT_TOOL_X,
  395. 0, SENSOR_RES_X, 0, 0);
  396. input_set_abs_params(input_dev, ABS_MT_TOOL_Y,
  397. 0, SENSOR_RES_Y, 0, 0);
  398. /* max value unknown, but major/minor axis
  399. * can never be larger than screen */
  400. input_set_abs_params(input_dev, ABS_MT_TOUCH_MAJOR,
  401. 0, SENSOR_RES_X, 0, 0);
  402. input_set_abs_params(input_dev, ABS_MT_TOUCH_MINOR,
  403. 0, SENSOR_RES_Y, 0, 0);
  404. input_set_abs_params(input_dev, ABS_MT_ORIENTATION, 0, 1, 0, 0);
  405. input_mt_init_slots(input_dev, MAX_CONTACTS,
  406. INPUT_MT_DIRECT | INPUT_MT_DROP_UNUSED);
  407. }
  408. /* Check candidate USB interface. */
  409. static int sur40_probe(struct usb_interface *interface,
  410. const struct usb_device_id *id)
  411. {
  412. struct usb_device *usbdev = interface_to_usbdev(interface);
  413. struct sur40_state *sur40;
  414. struct usb_host_interface *iface_desc;
  415. struct usb_endpoint_descriptor *endpoint;
  416. struct input_polled_dev *poll_dev;
  417. int error;
  418. /* Check if we really have the right interface. */
  419. iface_desc = &interface->altsetting[0];
  420. if (iface_desc->desc.bInterfaceClass != 0xFF)
  421. return -ENODEV;
  422. if (iface_desc->desc.bNumEndpoints < 5)
  423. return -ENODEV;
  424. /* Use endpoint #4 (0x86). */
  425. endpoint = &iface_desc->endpoint[4].desc;
  426. if (endpoint->bEndpointAddress != TOUCH_ENDPOINT)
  427. return -ENODEV;
  428. /* Allocate memory for our device state and initialize it. */
  429. sur40 = kzalloc(sizeof(struct sur40_state), GFP_KERNEL);
  430. if (!sur40)
  431. return -ENOMEM;
  432. poll_dev = input_allocate_polled_device();
  433. if (!poll_dev) {
  434. error = -ENOMEM;
  435. goto err_free_dev;
  436. }
  437. /* initialize locks/lists */
  438. INIT_LIST_HEAD(&sur40->buf_list);
  439. spin_lock_init(&sur40->qlock);
  440. mutex_init(&sur40->lock);
  441. /* Set up polled input device control structure */
  442. poll_dev->private = sur40;
  443. poll_dev->poll_interval = POLL_INTERVAL;
  444. poll_dev->open = sur40_open;
  445. poll_dev->poll = sur40_poll;
  446. poll_dev->close = sur40_close;
  447. /* Set up regular input device structure */
  448. sur40_input_setup(poll_dev->input);
  449. poll_dev->input->name = DRIVER_LONG;
  450. usb_to_input_id(usbdev, &poll_dev->input->id);
  451. usb_make_path(usbdev, sur40->phys, sizeof(sur40->phys));
  452. strlcat(sur40->phys, "/input0", sizeof(sur40->phys));
  453. poll_dev->input->phys = sur40->phys;
  454. poll_dev->input->dev.parent = &interface->dev;
  455. sur40->usbdev = usbdev;
  456. sur40->dev = &interface->dev;
  457. sur40->input = poll_dev;
  458. /* use the bulk-in endpoint tested above */
  459. sur40->bulk_in_size = usb_endpoint_maxp(endpoint);
  460. sur40->bulk_in_epaddr = endpoint->bEndpointAddress;
  461. sur40->bulk_in_buffer = kmalloc(sur40->bulk_in_size, GFP_KERNEL);
  462. if (!sur40->bulk_in_buffer) {
  463. dev_err(&interface->dev, "Unable to allocate input buffer.");
  464. error = -ENOMEM;
  465. goto err_free_polldev;
  466. }
  467. /* register the polled input device */
  468. error = input_register_polled_device(poll_dev);
  469. if (error) {
  470. dev_err(&interface->dev,
  471. "Unable to register polled input device.");
  472. goto err_free_buffer;
  473. }
  474. /* register the video master device */
  475. snprintf(sur40->v4l2.name, sizeof(sur40->v4l2.name), "%s", DRIVER_LONG);
  476. error = v4l2_device_register(sur40->dev, &sur40->v4l2);
  477. if (error) {
  478. dev_err(&interface->dev,
  479. "Unable to register video master device.");
  480. goto err_unreg_v4l2;
  481. }
  482. /* initialize the lock and subdevice */
  483. sur40->queue = sur40_queue;
  484. sur40->queue.drv_priv = sur40;
  485. sur40->queue.lock = &sur40->lock;
  486. sur40->queue.dev = sur40->dev;
  487. /* initialize the queue */
  488. error = vb2_queue_init(&sur40->queue);
  489. if (error)
  490. goto err_unreg_v4l2;
  491. sur40->pix_fmt = sur40_pix_format[0];
  492. sur40->vdev = sur40_video_device;
  493. sur40->vdev.v4l2_dev = &sur40->v4l2;
  494. sur40->vdev.lock = &sur40->lock;
  495. sur40->vdev.queue = &sur40->queue;
  496. video_set_drvdata(&sur40->vdev, sur40);
  497. error = video_register_device(&sur40->vdev, VFL_TYPE_TOUCH, -1);
  498. if (error) {
  499. dev_err(&interface->dev,
  500. "Unable to register video subdevice.");
  501. goto err_unreg_video;
  502. }
  503. /* we can register the device now, as it is ready */
  504. usb_set_intfdata(interface, sur40);
  505. dev_dbg(&interface->dev, "%s is now attached\n", DRIVER_DESC);
  506. return 0;
  507. err_unreg_video:
  508. video_unregister_device(&sur40->vdev);
  509. err_unreg_v4l2:
  510. v4l2_device_unregister(&sur40->v4l2);
  511. err_free_buffer:
  512. kfree(sur40->bulk_in_buffer);
  513. err_free_polldev:
  514. input_free_polled_device(sur40->input);
  515. err_free_dev:
  516. kfree(sur40);
  517. return error;
  518. }
  519. /* Unregister device & clean up. */
  520. static void sur40_disconnect(struct usb_interface *interface)
  521. {
  522. struct sur40_state *sur40 = usb_get_intfdata(interface);
  523. video_unregister_device(&sur40->vdev);
  524. v4l2_device_unregister(&sur40->v4l2);
  525. input_unregister_polled_device(sur40->input);
  526. input_free_polled_device(sur40->input);
  527. kfree(sur40->bulk_in_buffer);
  528. kfree(sur40);
  529. usb_set_intfdata(interface, NULL);
  530. dev_dbg(&interface->dev, "%s is now disconnected\n", DRIVER_DESC);
  531. }
  532. /*
  533. * Setup the constraints of the queue: besides setting the number of planes
  534. * per buffer and the size and allocation context of each plane, it also
  535. * checks if sufficient buffers have been allocated. Usually 3 is a good
  536. * minimum number: many DMA engines need a minimum of 2 buffers in the
  537. * queue and you need to have another available for userspace processing.
  538. */
  539. static int sur40_queue_setup(struct vb2_queue *q,
  540. unsigned int *nbuffers, unsigned int *nplanes,
  541. unsigned int sizes[], struct device *alloc_devs[])
  542. {
  543. struct sur40_state *sur40 = vb2_get_drv_priv(q);
  544. if (q->num_buffers + *nbuffers < 3)
  545. *nbuffers = 3 - q->num_buffers;
  546. if (*nplanes)
  547. return sizes[0] < sur40->pix_fmt.sizeimage ? -EINVAL : 0;
  548. *nplanes = 1;
  549. sizes[0] = sur40->pix_fmt.sizeimage;
  550. return 0;
  551. }
  552. /*
  553. * Prepare the buffer for queueing to the DMA engine: check and set the
  554. * payload size.
  555. */
  556. static int sur40_buffer_prepare(struct vb2_buffer *vb)
  557. {
  558. struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
  559. unsigned long size = sur40->pix_fmt.sizeimage;
  560. if (vb2_plane_size(vb, 0) < size) {
  561. dev_err(&sur40->usbdev->dev, "buffer too small (%lu < %lu)\n",
  562. vb2_plane_size(vb, 0), size);
  563. return -EINVAL;
  564. }
  565. vb2_set_plane_payload(vb, 0, size);
  566. return 0;
  567. }
  568. /*
  569. * Queue this buffer to the DMA engine.
  570. */
  571. static void sur40_buffer_queue(struct vb2_buffer *vb)
  572. {
  573. struct sur40_state *sur40 = vb2_get_drv_priv(vb->vb2_queue);
  574. struct sur40_buffer *buf = (struct sur40_buffer *)vb;
  575. spin_lock(&sur40->qlock);
  576. list_add_tail(&buf->list, &sur40->buf_list);
  577. spin_unlock(&sur40->qlock);
  578. }
  579. static void return_all_buffers(struct sur40_state *sur40,
  580. enum vb2_buffer_state state)
  581. {
  582. struct sur40_buffer *buf, *node;
  583. spin_lock(&sur40->qlock);
  584. list_for_each_entry_safe(buf, node, &sur40->buf_list, list) {
  585. vb2_buffer_done(&buf->vb.vb2_buf, state);
  586. list_del(&buf->list);
  587. }
  588. spin_unlock(&sur40->qlock);
  589. }
  590. /*
  591. * Start streaming. First check if the minimum number of buffers have been
  592. * queued. If not, then return -ENOBUFS and the vb2 framework will call
  593. * this function again the next time a buffer has been queued until enough
  594. * buffers are available to actually start the DMA engine.
  595. */
  596. static int sur40_start_streaming(struct vb2_queue *vq, unsigned int count)
  597. {
  598. struct sur40_state *sur40 = vb2_get_drv_priv(vq);
  599. sur40->sequence = 0;
  600. return 0;
  601. }
  602. /*
  603. * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
  604. * and passed on to the vb2 framework marked as STATE_ERROR.
  605. */
  606. static void sur40_stop_streaming(struct vb2_queue *vq)
  607. {
  608. struct sur40_state *sur40 = vb2_get_drv_priv(vq);
  609. vb2_wait_for_all_buffers(vq);
  610. sur40->sequence = -1;
  611. /* Release all active buffers */
  612. return_all_buffers(sur40, VB2_BUF_STATE_ERROR);
  613. }
  614. /* V4L ioctl */
  615. static int sur40_vidioc_querycap(struct file *file, void *priv,
  616. struct v4l2_capability *cap)
  617. {
  618. struct sur40_state *sur40 = video_drvdata(file);
  619. strlcpy(cap->driver, DRIVER_SHORT, sizeof(cap->driver));
  620. strlcpy(cap->card, DRIVER_LONG, sizeof(cap->card));
  621. usb_make_path(sur40->usbdev, cap->bus_info, sizeof(cap->bus_info));
  622. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TOUCH |
  623. V4L2_CAP_READWRITE |
  624. V4L2_CAP_STREAMING;
  625. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  626. return 0;
  627. }
  628. static int sur40_vidioc_enum_input(struct file *file, void *priv,
  629. struct v4l2_input *i)
  630. {
  631. if (i->index != 0)
  632. return -EINVAL;
  633. i->type = V4L2_INPUT_TYPE_TOUCH;
  634. i->std = V4L2_STD_UNKNOWN;
  635. strlcpy(i->name, "In-Cell Sensor", sizeof(i->name));
  636. i->capabilities = 0;
  637. return 0;
  638. }
  639. static int sur40_vidioc_s_input(struct file *file, void *priv, unsigned int i)
  640. {
  641. return (i == 0) ? 0 : -EINVAL;
  642. }
  643. static int sur40_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  644. {
  645. *i = 0;
  646. return 0;
  647. }
  648. static int sur40_vidioc_try_fmt(struct file *file, void *priv,
  649. struct v4l2_format *f)
  650. {
  651. switch (f->fmt.pix.pixelformat) {
  652. case V4L2_PIX_FMT_GREY:
  653. f->fmt.pix = sur40_pix_format[1];
  654. break;
  655. default:
  656. f->fmt.pix = sur40_pix_format[0];
  657. break;
  658. }
  659. return 0;
  660. }
  661. static int sur40_vidioc_s_fmt(struct file *file, void *priv,
  662. struct v4l2_format *f)
  663. {
  664. struct sur40_state *sur40 = video_drvdata(file);
  665. switch (f->fmt.pix.pixelformat) {
  666. case V4L2_PIX_FMT_GREY:
  667. sur40->pix_fmt = sur40_pix_format[1];
  668. break;
  669. default:
  670. sur40->pix_fmt = sur40_pix_format[0];
  671. break;
  672. }
  673. f->fmt.pix = sur40->pix_fmt;
  674. return 0;
  675. }
  676. static int sur40_vidioc_g_fmt(struct file *file, void *priv,
  677. struct v4l2_format *f)
  678. {
  679. struct sur40_state *sur40 = video_drvdata(file);
  680. f->fmt.pix = sur40->pix_fmt;
  681. return 0;
  682. }
  683. static int sur40_ioctl_parm(struct file *file, void *priv,
  684. struct v4l2_streamparm *p)
  685. {
  686. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  687. return -EINVAL;
  688. p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  689. p->parm.capture.timeperframe.numerator = 1;
  690. p->parm.capture.timeperframe.denominator = 60;
  691. p->parm.capture.readbuffers = 3;
  692. return 0;
  693. }
  694. static int sur40_vidioc_enum_fmt(struct file *file, void *priv,
  695. struct v4l2_fmtdesc *f)
  696. {
  697. if (f->index >= ARRAY_SIZE(sur40_pix_format))
  698. return -EINVAL;
  699. f->pixelformat = sur40_pix_format[f->index].pixelformat;
  700. f->flags = 0;
  701. return 0;
  702. }
  703. static int sur40_vidioc_enum_framesizes(struct file *file, void *priv,
  704. struct v4l2_frmsizeenum *f)
  705. {
  706. struct sur40_state *sur40 = video_drvdata(file);
  707. if ((f->index != 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
  708. && (f->pixel_format != V4L2_PIX_FMT_GREY)))
  709. return -EINVAL;
  710. f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  711. f->discrete.width = sur40->pix_fmt.width;
  712. f->discrete.height = sur40->pix_fmt.height;
  713. return 0;
  714. }
  715. static int sur40_vidioc_enum_frameintervals(struct file *file, void *priv,
  716. struct v4l2_frmivalenum *f)
  717. {
  718. struct sur40_state *sur40 = video_drvdata(file);
  719. if ((f->index > 0) || ((f->pixel_format != V4L2_TCH_FMT_TU08)
  720. && (f->pixel_format != V4L2_PIX_FMT_GREY))
  721. || (f->width != sur40->pix_fmt.width)
  722. || (f->height != sur40->pix_fmt.height))
  723. return -EINVAL;
  724. f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  725. f->discrete.denominator = 60;
  726. f->discrete.numerator = 1;
  727. return 0;
  728. }
  729. static const struct usb_device_id sur40_table[] = {
  730. { USB_DEVICE(ID_MICROSOFT, ID_SUR40) }, /* Samsung SUR40 */
  731. { } /* terminating null entry */
  732. };
  733. MODULE_DEVICE_TABLE(usb, sur40_table);
  734. /* V4L2 structures */
  735. static const struct vb2_ops sur40_queue_ops = {
  736. .queue_setup = sur40_queue_setup,
  737. .buf_prepare = sur40_buffer_prepare,
  738. .buf_queue = sur40_buffer_queue,
  739. .start_streaming = sur40_start_streaming,
  740. .stop_streaming = sur40_stop_streaming,
  741. .wait_prepare = vb2_ops_wait_prepare,
  742. .wait_finish = vb2_ops_wait_finish,
  743. };
  744. static const struct vb2_queue sur40_queue = {
  745. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  746. /*
  747. * VB2_USERPTR in currently not enabled: passing a user pointer to
  748. * dma-sg will result in segment sizes that are not a multiple of
  749. * 512 bytes, which is required by the host controller.
  750. */
  751. .io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF,
  752. .buf_struct_size = sizeof(struct sur40_buffer),
  753. .ops = &sur40_queue_ops,
  754. .mem_ops = &vb2_dma_sg_memops,
  755. .timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC,
  756. .min_buffers_needed = 3,
  757. };
  758. static const struct v4l2_file_operations sur40_video_fops = {
  759. .owner = THIS_MODULE,
  760. .open = v4l2_fh_open,
  761. .release = vb2_fop_release,
  762. .unlocked_ioctl = video_ioctl2,
  763. .read = vb2_fop_read,
  764. .mmap = vb2_fop_mmap,
  765. .poll = vb2_fop_poll,
  766. };
  767. static const struct v4l2_ioctl_ops sur40_video_ioctl_ops = {
  768. .vidioc_querycap = sur40_vidioc_querycap,
  769. .vidioc_enum_fmt_vid_cap = sur40_vidioc_enum_fmt,
  770. .vidioc_try_fmt_vid_cap = sur40_vidioc_try_fmt,
  771. .vidioc_s_fmt_vid_cap = sur40_vidioc_s_fmt,
  772. .vidioc_g_fmt_vid_cap = sur40_vidioc_g_fmt,
  773. .vidioc_enum_framesizes = sur40_vidioc_enum_framesizes,
  774. .vidioc_enum_frameintervals = sur40_vidioc_enum_frameintervals,
  775. .vidioc_g_parm = sur40_ioctl_parm,
  776. .vidioc_s_parm = sur40_ioctl_parm,
  777. .vidioc_enum_input = sur40_vidioc_enum_input,
  778. .vidioc_g_input = sur40_vidioc_g_input,
  779. .vidioc_s_input = sur40_vidioc_s_input,
  780. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  781. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  782. .vidioc_querybuf = vb2_ioctl_querybuf,
  783. .vidioc_qbuf = vb2_ioctl_qbuf,
  784. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  785. .vidioc_expbuf = vb2_ioctl_expbuf,
  786. .vidioc_streamon = vb2_ioctl_streamon,
  787. .vidioc_streamoff = vb2_ioctl_streamoff,
  788. };
  789. static const struct video_device sur40_video_device = {
  790. .name = DRIVER_LONG,
  791. .fops = &sur40_video_fops,
  792. .ioctl_ops = &sur40_video_ioctl_ops,
  793. .release = video_device_release_empty,
  794. };
  795. /* USB-specific object needed to register this driver with the USB subsystem. */
  796. static struct usb_driver sur40_driver = {
  797. .name = DRIVER_SHORT,
  798. .probe = sur40_probe,
  799. .disconnect = sur40_disconnect,
  800. .id_table = sur40_table,
  801. };
  802. module_usb_driver(sur40_driver);
  803. MODULE_AUTHOR(DRIVER_AUTHOR);
  804. MODULE_DESCRIPTION(DRIVER_DESC);
  805. MODULE_LICENSE("GPL");