sur40.c 27 KB

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