sur40.c 25 KB

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