sur40.c 25 KB

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