surface_aggregator_cdev.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Provides user-space access to the SSAM EC via the /dev/surface/aggregator
  4. * misc device. Intended for debugging and development.
  5. *
  6. * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/ioctl.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kfifo.h>
  12. #include <linux/kref.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/poll.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/slab.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/surface_aggregator/cdev.h>
  22. #include <linux/surface_aggregator/controller.h>
  23. #include <linux/surface_aggregator/serial_hub.h>
  24. #define SSAM_CDEV_DEVICE_NAME "surface_aggregator_cdev"
  25. /* -- Main structures. ------------------------------------------------------ */
  26. enum ssam_cdev_device_state {
  27. SSAM_CDEV_DEVICE_SHUTDOWN_BIT = BIT(0),
  28. };
  29. struct ssam_cdev {
  30. struct kref kref;
  31. struct rw_semaphore lock;
  32. struct device *dev;
  33. struct ssam_controller *ctrl;
  34. struct miscdevice mdev;
  35. unsigned long flags;
  36. struct rw_semaphore client_lock; /* Guards client list. */
  37. struct list_head client_list;
  38. };
  39. struct ssam_cdev_client;
  40. struct ssam_cdev_notifier {
  41. struct ssam_cdev_client *client;
  42. struct ssam_event_notifier nf;
  43. };
  44. struct ssam_cdev_client {
  45. struct ssam_cdev *cdev;
  46. struct list_head node;
  47. struct mutex notifier_lock; /* Guards notifier access for registration */
  48. struct ssam_cdev_notifier *notifier[SSH_NUM_EVENTS];
  49. struct mutex read_lock; /* Guards FIFO buffer read access */
  50. struct mutex write_lock; /* Guards FIFO buffer write access */
  51. DECLARE_KFIFO(buffer, u8, 4096);
  52. wait_queue_head_t waitq;
  53. struct fasync_struct *fasync;
  54. };
  55. static void __ssam_cdev_release(struct kref *kref)
  56. {
  57. kfree(container_of(kref, struct ssam_cdev, kref));
  58. }
  59. static struct ssam_cdev *ssam_cdev_get(struct ssam_cdev *cdev)
  60. {
  61. if (cdev)
  62. kref_get(&cdev->kref);
  63. return cdev;
  64. }
  65. static void ssam_cdev_put(struct ssam_cdev *cdev)
  66. {
  67. if (cdev)
  68. kref_put(&cdev->kref, __ssam_cdev_release);
  69. }
  70. /* -- Notifier handling. ---------------------------------------------------- */
  71. static u32 ssam_cdev_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
  72. {
  73. struct ssam_cdev_notifier *cdev_nf = container_of(nf, struct ssam_cdev_notifier, nf);
  74. struct ssam_cdev_client *client = cdev_nf->client;
  75. struct ssam_cdev_event event;
  76. size_t n = struct_size(&event, data, in->length);
  77. /* Translate event. */
  78. event.target_category = in->target_category;
  79. event.target_id = in->target_id;
  80. event.command_id = in->command_id;
  81. event.instance_id = in->instance_id;
  82. event.length = in->length;
  83. mutex_lock(&client->write_lock);
  84. /* Make sure we have enough space. */
  85. if (kfifo_avail(&client->buffer) < n) {
  86. dev_warn(client->cdev->dev,
  87. "buffer full, dropping event (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
  88. in->target_category, in->target_id, in->command_id, in->instance_id);
  89. mutex_unlock(&client->write_lock);
  90. return 0;
  91. }
  92. /* Copy event header and payload. */
  93. kfifo_in(&client->buffer, (const u8 *)&event, struct_size(&event, data, 0));
  94. kfifo_in(&client->buffer, &in->data[0], in->length);
  95. mutex_unlock(&client->write_lock);
  96. /* Notify waiting readers. */
  97. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  98. wake_up_interruptible(&client->waitq);
  99. /*
  100. * Don't mark events as handled, this is the job of a proper driver and
  101. * not the debugging interface.
  102. */
  103. return 0;
  104. }
  105. static int ssam_cdev_notifier_register(struct ssam_cdev_client *client, u8 tc, int priority)
  106. {
  107. const u16 rqid = ssh_tc_to_rqid(tc);
  108. const u16 event = ssh_rqid_to_event(rqid);
  109. struct ssam_cdev_notifier *nf;
  110. int status;
  111. lockdep_assert_held_read(&client->cdev->lock);
  112. /* Validate notifier target category. */
  113. if (!ssh_rqid_is_event(rqid))
  114. return -EINVAL;
  115. mutex_lock(&client->notifier_lock);
  116. /* Check if the notifier has already been registered. */
  117. if (client->notifier[event]) {
  118. mutex_unlock(&client->notifier_lock);
  119. return -EEXIST;
  120. }
  121. /* Allocate new notifier. */
  122. nf = kzalloc_obj(*nf);
  123. if (!nf) {
  124. mutex_unlock(&client->notifier_lock);
  125. return -ENOMEM;
  126. }
  127. /*
  128. * Create a dummy notifier with the minimal required fields for
  129. * observer registration. Note that we can skip fully specifying event
  130. * and registry here as we do not need any matching and use silent
  131. * registration, which does not enable the corresponding event.
  132. */
  133. nf->client = client;
  134. nf->nf.base.fn = ssam_cdev_notifier;
  135. nf->nf.base.priority = priority;
  136. nf->nf.event.id.target_category = tc;
  137. nf->nf.event.mask = 0; /* Do not do any matching. */
  138. nf->nf.flags = SSAM_EVENT_NOTIFIER_OBSERVER;
  139. /* Register notifier. */
  140. status = ssam_notifier_register(client->cdev->ctrl, &nf->nf);
  141. if (status)
  142. kfree(nf);
  143. else
  144. client->notifier[event] = nf;
  145. mutex_unlock(&client->notifier_lock);
  146. return status;
  147. }
  148. static int ssam_cdev_notifier_unregister(struct ssam_cdev_client *client, u8 tc)
  149. {
  150. const u16 rqid = ssh_tc_to_rqid(tc);
  151. const u16 event = ssh_rqid_to_event(rqid);
  152. int status;
  153. lockdep_assert_held_read(&client->cdev->lock);
  154. /* Validate notifier target category. */
  155. if (!ssh_rqid_is_event(rqid))
  156. return -EINVAL;
  157. mutex_lock(&client->notifier_lock);
  158. /* Check if the notifier is currently registered. */
  159. if (!client->notifier[event]) {
  160. mutex_unlock(&client->notifier_lock);
  161. return -ENOENT;
  162. }
  163. /* Unregister and free notifier. */
  164. status = ssam_notifier_unregister(client->cdev->ctrl, &client->notifier[event]->nf);
  165. kfree(client->notifier[event]);
  166. client->notifier[event] = NULL;
  167. mutex_unlock(&client->notifier_lock);
  168. return status;
  169. }
  170. static void ssam_cdev_notifier_unregister_all(struct ssam_cdev_client *client)
  171. {
  172. int i;
  173. down_read(&client->cdev->lock);
  174. /*
  175. * This function may be used during shutdown, thus we need to test for
  176. * cdev->ctrl instead of the SSAM_CDEV_DEVICE_SHUTDOWN_BIT bit.
  177. */
  178. if (client->cdev->ctrl) {
  179. for (i = 0; i < SSH_NUM_EVENTS; i++)
  180. ssam_cdev_notifier_unregister(client, i + 1);
  181. } else {
  182. int count = 0;
  183. /*
  184. * Device has been shut down. Any notifier remaining is a bug,
  185. * so warn about that as this would otherwise hardly be
  186. * noticeable. Nevertheless, free them as well.
  187. */
  188. mutex_lock(&client->notifier_lock);
  189. for (i = 0; i < SSH_NUM_EVENTS; i++) {
  190. count += !!(client->notifier[i]);
  191. kfree(client->notifier[i]);
  192. client->notifier[i] = NULL;
  193. }
  194. mutex_unlock(&client->notifier_lock);
  195. WARN_ON(count > 0);
  196. }
  197. up_read(&client->cdev->lock);
  198. }
  199. /* -- IOCTL functions. ------------------------------------------------------ */
  200. static long ssam_cdev_request(struct ssam_cdev_client *client, struct ssam_cdev_request __user *r)
  201. {
  202. struct ssam_cdev_request rqst;
  203. struct ssam_request spec = {};
  204. struct ssam_response rsp = {};
  205. const void __user *plddata;
  206. void __user *rspdata;
  207. int status = 0, ret = 0, tmp;
  208. lockdep_assert_held_read(&client->cdev->lock);
  209. ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
  210. if (ret)
  211. goto out;
  212. plddata = u64_to_user_ptr(rqst.payload.data);
  213. rspdata = u64_to_user_ptr(rqst.response.data);
  214. /* Setup basic request fields. */
  215. spec.target_category = rqst.target_category;
  216. spec.target_id = rqst.target_id;
  217. spec.command_id = rqst.command_id;
  218. spec.instance_id = rqst.instance_id;
  219. spec.flags = 0;
  220. spec.length = rqst.payload.length;
  221. spec.payload = NULL;
  222. if (rqst.flags & SSAM_CDEV_REQUEST_HAS_RESPONSE)
  223. spec.flags |= SSAM_REQUEST_HAS_RESPONSE;
  224. if (rqst.flags & SSAM_CDEV_REQUEST_UNSEQUENCED)
  225. spec.flags |= SSAM_REQUEST_UNSEQUENCED;
  226. rsp.capacity = rqst.response.length;
  227. rsp.length = 0;
  228. rsp.pointer = NULL;
  229. /* Get request payload from user-space. */
  230. if (spec.length) {
  231. if (!plddata) {
  232. ret = -EINVAL;
  233. goto out;
  234. }
  235. /*
  236. * Note: spec.length is limited to U16_MAX bytes via struct
  237. * ssam_cdev_request. This is slightly larger than the
  238. * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
  239. * underlying protocol (note that nothing remotely this size
  240. * should ever be allocated in any normal case). This size is
  241. * validated later in ssam_request_do_sync(), for allocation
  242. * the bound imposed by u16 should be enough.
  243. */
  244. spec.payload = kzalloc(spec.length, GFP_KERNEL);
  245. if (!spec.payload) {
  246. ret = -ENOMEM;
  247. goto out;
  248. }
  249. if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
  250. ret = -EFAULT;
  251. goto out;
  252. }
  253. }
  254. /* Allocate response buffer. */
  255. if (rsp.capacity) {
  256. if (!rspdata) {
  257. ret = -EINVAL;
  258. goto out;
  259. }
  260. /*
  261. * Note: rsp.capacity is limited to U16_MAX bytes via struct
  262. * ssam_cdev_request. This is slightly larger than the
  263. * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
  264. * underlying protocol (note that nothing remotely this size
  265. * should ever be allocated in any normal case). In later use,
  266. * this capacity does not have to be strictly bounded, as it
  267. * is only used as an output buffer to be written to. For
  268. * allocation the bound imposed by u16 should be enough.
  269. */
  270. rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
  271. if (!rsp.pointer) {
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. }
  276. /* Perform request. */
  277. status = ssam_request_do_sync(client->cdev->ctrl, &spec, &rsp);
  278. if (status)
  279. goto out;
  280. /* Copy response to user-space. */
  281. if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
  282. ret = -EFAULT;
  283. out:
  284. /* Always try to set response-length and status. */
  285. tmp = put_user(rsp.length, &r->response.length);
  286. if (tmp)
  287. ret = tmp;
  288. tmp = put_user(status, &r->status);
  289. if (tmp)
  290. ret = tmp;
  291. /* Cleanup. */
  292. kfree(spec.payload);
  293. kfree(rsp.pointer);
  294. return ret;
  295. }
  296. static long ssam_cdev_notif_register(struct ssam_cdev_client *client,
  297. const struct ssam_cdev_notifier_desc __user *d)
  298. {
  299. struct ssam_cdev_notifier_desc desc;
  300. long ret;
  301. lockdep_assert_held_read(&client->cdev->lock);
  302. ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  303. if (ret)
  304. return ret;
  305. return ssam_cdev_notifier_register(client, desc.target_category, desc.priority);
  306. }
  307. static long ssam_cdev_notif_unregister(struct ssam_cdev_client *client,
  308. const struct ssam_cdev_notifier_desc __user *d)
  309. {
  310. struct ssam_cdev_notifier_desc desc;
  311. long ret;
  312. lockdep_assert_held_read(&client->cdev->lock);
  313. ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  314. if (ret)
  315. return ret;
  316. return ssam_cdev_notifier_unregister(client, desc.target_category);
  317. }
  318. static long ssam_cdev_event_enable(struct ssam_cdev_client *client,
  319. const struct ssam_cdev_event_desc __user *d)
  320. {
  321. struct ssam_cdev_event_desc desc;
  322. struct ssam_event_registry reg;
  323. struct ssam_event_id id;
  324. long ret;
  325. lockdep_assert_held_read(&client->cdev->lock);
  326. /* Read descriptor from user-space. */
  327. ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  328. if (ret)
  329. return ret;
  330. /* Translate descriptor. */
  331. reg.target_category = desc.reg.target_category;
  332. reg.target_id = desc.reg.target_id;
  333. reg.cid_enable = desc.reg.cid_enable;
  334. reg.cid_disable = desc.reg.cid_disable;
  335. id.target_category = desc.id.target_category;
  336. id.instance = desc.id.instance;
  337. /* Disable event. */
  338. return ssam_controller_event_enable(client->cdev->ctrl, reg, id, desc.flags);
  339. }
  340. static long ssam_cdev_event_disable(struct ssam_cdev_client *client,
  341. const struct ssam_cdev_event_desc __user *d)
  342. {
  343. struct ssam_cdev_event_desc desc;
  344. struct ssam_event_registry reg;
  345. struct ssam_event_id id;
  346. long ret;
  347. lockdep_assert_held_read(&client->cdev->lock);
  348. /* Read descriptor from user-space. */
  349. ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
  350. if (ret)
  351. return ret;
  352. /* Translate descriptor. */
  353. reg.target_category = desc.reg.target_category;
  354. reg.target_id = desc.reg.target_id;
  355. reg.cid_enable = desc.reg.cid_enable;
  356. reg.cid_disable = desc.reg.cid_disable;
  357. id.target_category = desc.id.target_category;
  358. id.instance = desc.id.instance;
  359. /* Disable event. */
  360. return ssam_controller_event_disable(client->cdev->ctrl, reg, id, desc.flags);
  361. }
  362. /* -- File operations. ------------------------------------------------------ */
  363. static int ssam_cdev_device_open(struct inode *inode, struct file *filp)
  364. {
  365. struct miscdevice *mdev = filp->private_data;
  366. struct ssam_cdev_client *client;
  367. struct ssam_cdev *cdev = container_of(mdev, struct ssam_cdev, mdev);
  368. /* Initialize client */
  369. client = vzalloc(sizeof(*client));
  370. if (!client)
  371. return -ENOMEM;
  372. client->cdev = ssam_cdev_get(cdev);
  373. INIT_LIST_HEAD(&client->node);
  374. mutex_init(&client->notifier_lock);
  375. mutex_init(&client->read_lock);
  376. mutex_init(&client->write_lock);
  377. INIT_KFIFO(client->buffer);
  378. init_waitqueue_head(&client->waitq);
  379. filp->private_data = client;
  380. /* Attach client. */
  381. down_write(&cdev->client_lock);
  382. if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  383. up_write(&cdev->client_lock);
  384. mutex_destroy(&client->write_lock);
  385. mutex_destroy(&client->read_lock);
  386. mutex_destroy(&client->notifier_lock);
  387. ssam_cdev_put(client->cdev);
  388. vfree(client);
  389. return -ENODEV;
  390. }
  391. list_add_tail(&client->node, &cdev->client_list);
  392. up_write(&cdev->client_lock);
  393. stream_open(inode, filp);
  394. return 0;
  395. }
  396. static int ssam_cdev_device_release(struct inode *inode, struct file *filp)
  397. {
  398. struct ssam_cdev_client *client = filp->private_data;
  399. /* Force-unregister all remaining notifiers of this client. */
  400. ssam_cdev_notifier_unregister_all(client);
  401. /* Detach client. */
  402. down_write(&client->cdev->client_lock);
  403. list_del(&client->node);
  404. up_write(&client->cdev->client_lock);
  405. /* Free client. */
  406. mutex_destroy(&client->write_lock);
  407. mutex_destroy(&client->read_lock);
  408. mutex_destroy(&client->notifier_lock);
  409. ssam_cdev_put(client->cdev);
  410. vfree(client);
  411. return 0;
  412. }
  413. static long __ssam_cdev_device_ioctl(struct ssam_cdev_client *client, unsigned int cmd,
  414. unsigned long arg)
  415. {
  416. lockdep_assert_held_read(&client->cdev->lock);
  417. switch (cmd) {
  418. case SSAM_CDEV_REQUEST:
  419. return ssam_cdev_request(client, (struct ssam_cdev_request __user *)arg);
  420. case SSAM_CDEV_NOTIF_REGISTER:
  421. return ssam_cdev_notif_register(client,
  422. (struct ssam_cdev_notifier_desc __user *)arg);
  423. case SSAM_CDEV_NOTIF_UNREGISTER:
  424. return ssam_cdev_notif_unregister(client,
  425. (struct ssam_cdev_notifier_desc __user *)arg);
  426. case SSAM_CDEV_EVENT_ENABLE:
  427. return ssam_cdev_event_enable(client, (struct ssam_cdev_event_desc __user *)arg);
  428. case SSAM_CDEV_EVENT_DISABLE:
  429. return ssam_cdev_event_disable(client, (struct ssam_cdev_event_desc __user *)arg);
  430. default:
  431. return -ENOTTY;
  432. }
  433. }
  434. static long ssam_cdev_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  435. {
  436. struct ssam_cdev_client *client = file->private_data;
  437. long status;
  438. /* Ensure that controller is valid for as long as we need it. */
  439. if (down_read_killable(&client->cdev->lock))
  440. return -ERESTARTSYS;
  441. if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags)) {
  442. up_read(&client->cdev->lock);
  443. return -ENODEV;
  444. }
  445. status = __ssam_cdev_device_ioctl(client, cmd, arg);
  446. up_read(&client->cdev->lock);
  447. return status;
  448. }
  449. static ssize_t ssam_cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
  450. {
  451. struct ssam_cdev_client *client = file->private_data;
  452. struct ssam_cdev *cdev = client->cdev;
  453. unsigned int copied;
  454. int status = 0;
  455. if (down_read_killable(&cdev->lock))
  456. return -ERESTARTSYS;
  457. /* Make sure we're not shut down. */
  458. if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  459. up_read(&cdev->lock);
  460. return -ENODEV;
  461. }
  462. do {
  463. /* Check availability, wait if necessary. */
  464. if (kfifo_is_empty(&client->buffer)) {
  465. up_read(&cdev->lock);
  466. if (file->f_flags & O_NONBLOCK)
  467. return -EAGAIN;
  468. status = wait_event_interruptible(client->waitq,
  469. !kfifo_is_empty(&client->buffer) ||
  470. test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT,
  471. &cdev->flags));
  472. if (status < 0)
  473. return status;
  474. if (down_read_killable(&cdev->lock))
  475. return -ERESTARTSYS;
  476. /* Need to check that we're not shut down again. */
  477. if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
  478. up_read(&cdev->lock);
  479. return -ENODEV;
  480. }
  481. }
  482. /* Try to read from FIFO. */
  483. if (mutex_lock_interruptible(&client->read_lock)) {
  484. up_read(&cdev->lock);
  485. return -ERESTARTSYS;
  486. }
  487. status = kfifo_to_user(&client->buffer, buf, count, &copied);
  488. mutex_unlock(&client->read_lock);
  489. if (status < 0) {
  490. up_read(&cdev->lock);
  491. return status;
  492. }
  493. /* We might not have gotten anything, check this here. */
  494. if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
  495. up_read(&cdev->lock);
  496. return -EAGAIN;
  497. }
  498. } while (copied == 0);
  499. up_read(&cdev->lock);
  500. return copied;
  501. }
  502. static __poll_t ssam_cdev_poll(struct file *file, struct poll_table_struct *pt)
  503. {
  504. struct ssam_cdev_client *client = file->private_data;
  505. __poll_t events = 0;
  506. if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags))
  507. return EPOLLHUP | EPOLLERR;
  508. poll_wait(file, &client->waitq, pt);
  509. if (!kfifo_is_empty(&client->buffer))
  510. events |= EPOLLIN | EPOLLRDNORM;
  511. return events;
  512. }
  513. static int ssam_cdev_fasync(int fd, struct file *file, int on)
  514. {
  515. struct ssam_cdev_client *client = file->private_data;
  516. return fasync_helper(fd, file, on, &client->fasync);
  517. }
  518. static const struct file_operations ssam_controller_fops = {
  519. .owner = THIS_MODULE,
  520. .open = ssam_cdev_device_open,
  521. .release = ssam_cdev_device_release,
  522. .read = ssam_cdev_read,
  523. .poll = ssam_cdev_poll,
  524. .fasync = ssam_cdev_fasync,
  525. .unlocked_ioctl = ssam_cdev_device_ioctl,
  526. .compat_ioctl = ssam_cdev_device_ioctl,
  527. };
  528. /* -- Device and driver setup ----------------------------------------------- */
  529. static int ssam_dbg_device_probe(struct platform_device *pdev)
  530. {
  531. struct ssam_controller *ctrl;
  532. struct ssam_cdev *cdev;
  533. int status;
  534. ctrl = ssam_client_bind(&pdev->dev);
  535. if (IS_ERR(ctrl))
  536. return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
  537. cdev = kzalloc_obj(*cdev);
  538. if (!cdev)
  539. return -ENOMEM;
  540. kref_init(&cdev->kref);
  541. init_rwsem(&cdev->lock);
  542. cdev->ctrl = ctrl;
  543. cdev->dev = &pdev->dev;
  544. cdev->mdev.parent = &pdev->dev;
  545. cdev->mdev.minor = MISC_DYNAMIC_MINOR;
  546. cdev->mdev.name = "surface_aggregator";
  547. cdev->mdev.nodename = "surface/aggregator";
  548. cdev->mdev.fops = &ssam_controller_fops;
  549. init_rwsem(&cdev->client_lock);
  550. INIT_LIST_HEAD(&cdev->client_list);
  551. status = misc_register(&cdev->mdev);
  552. if (status) {
  553. kfree(cdev);
  554. return status;
  555. }
  556. platform_set_drvdata(pdev, cdev);
  557. return 0;
  558. }
  559. static void ssam_dbg_device_remove(struct platform_device *pdev)
  560. {
  561. struct ssam_cdev *cdev = platform_get_drvdata(pdev);
  562. struct ssam_cdev_client *client;
  563. /*
  564. * Mark device as shut-down. Prevent new clients from being added and
  565. * new operations from being executed.
  566. */
  567. set_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags);
  568. down_write(&cdev->client_lock);
  569. /* Remove all notifiers registered by us. */
  570. list_for_each_entry(client, &cdev->client_list, node) {
  571. ssam_cdev_notifier_unregister_all(client);
  572. }
  573. /* Wake up async clients. */
  574. list_for_each_entry(client, &cdev->client_list, node) {
  575. kill_fasync(&client->fasync, SIGIO, POLL_HUP);
  576. }
  577. /* Wake up blocking clients. */
  578. list_for_each_entry(client, &cdev->client_list, node) {
  579. wake_up_interruptible(&client->waitq);
  580. }
  581. up_write(&cdev->client_lock);
  582. /*
  583. * The controller is only guaranteed to be valid for as long as the
  584. * driver is bound. Remove controller so that any lingering open files
  585. * cannot access it any more after we're gone.
  586. */
  587. down_write(&cdev->lock);
  588. cdev->ctrl = NULL;
  589. cdev->dev = NULL;
  590. up_write(&cdev->lock);
  591. misc_deregister(&cdev->mdev);
  592. ssam_cdev_put(cdev);
  593. }
  594. static struct platform_device *ssam_cdev_device;
  595. static struct platform_driver ssam_cdev_driver = {
  596. .probe = ssam_dbg_device_probe,
  597. .remove = ssam_dbg_device_remove,
  598. .driver = {
  599. .name = SSAM_CDEV_DEVICE_NAME,
  600. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  601. },
  602. };
  603. static int __init ssam_debug_init(void)
  604. {
  605. int status;
  606. ssam_cdev_device = platform_device_alloc(SSAM_CDEV_DEVICE_NAME,
  607. PLATFORM_DEVID_NONE);
  608. if (!ssam_cdev_device)
  609. return -ENOMEM;
  610. status = platform_device_add(ssam_cdev_device);
  611. if (status)
  612. goto err_device;
  613. status = platform_driver_register(&ssam_cdev_driver);
  614. if (status)
  615. goto err_driver;
  616. return 0;
  617. err_driver:
  618. platform_device_del(ssam_cdev_device);
  619. err_device:
  620. platform_device_put(ssam_cdev_device);
  621. return status;
  622. }
  623. module_init(ssam_debug_init);
  624. static void __exit ssam_debug_exit(void)
  625. {
  626. platform_driver_unregister(&ssam_cdev_driver);
  627. platform_device_unregister(ssam_cdev_device);
  628. }
  629. module_exit(ssam_debug_exit);
  630. MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  631. MODULE_DESCRIPTION("User-space interface for Surface System Aggregator Module");
  632. MODULE_LICENSE("GPL");