sur40.c 26 KB

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