ajoystick.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /****************************************************************************
  2. * drivers/input/ajoystick.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /* This file provides a driver for a standard analog joystick device. An
  21. * analog joystick refers to a joystick that provides X/Y positional data as
  22. * integer values such as might be provides by Analog-to-Digital Conversion
  23. * (ADC). The analog positional data may also be accompanied by discrete
  24. * button data.
  25. *
  26. * The analog joystick driver exports a standard character driver
  27. * interface. By convention, the analog joystick is registered as an input
  28. * device at /dev/ajoyN where N uniquely identifies the driver instance.
  29. */
  30. /****************************************************************************
  31. * Included Files
  32. ****************************************************************************/
  33. #include <nuttx/config.h>
  34. #include <sys/types.h>
  35. #include <stdbool.h>
  36. #include <string.h>
  37. #include <poll.h>
  38. #include <assert.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <nuttx/kmalloc.h>
  42. #include <nuttx/signal.h>
  43. #include <nuttx/random.h>
  44. #include <nuttx/fs/fs.h>
  45. #include <nuttx/input/ajoystick.h>
  46. #include <nuttx/irq.h>
  47. /****************************************************************************
  48. * Private Types
  49. ****************************************************************************/
  50. /* This structure provides the state of one analog joystick driver */
  51. struct ajoy_upperhalf_s
  52. {
  53. /* Saved binding to the lower half analog joystick driver */
  54. FAR const struct ajoy_lowerhalf_s *au_lower;
  55. ajoy_buttonset_t au_enabled; /* Set of currently enabled button interrupts */
  56. ajoy_buttonset_t au_sample; /* Last sampled button states */
  57. sem_t au_exclsem; /* Supports exclusive access to the device */
  58. /* The following is a singly linked list of open references to the
  59. * joystick device.
  60. */
  61. FAR struct ajoy_open_s *au_open;
  62. };
  63. /* This structure describes the state of one open joystick driver instance */
  64. struct ajoy_open_s
  65. {
  66. /* Supports a singly linked list */
  67. FAR struct ajoy_open_s *ao_flink;
  68. /* The following will be true if we are closing */
  69. volatile bool ao_closing;
  70. /* Joystick event notification information */
  71. pid_t ao_pid;
  72. struct ajoy_notify_s ao_notify;
  73. struct sigwork_s ao_work;
  74. /* Poll event information */
  75. struct ajoy_pollevents_s ao_pollevents;
  76. /* The following is a list if poll structures of threads waiting for
  77. * driver events.
  78. */
  79. FAR struct pollfd *ao_fds[CONFIG_INPUT_AJOYSTICK_NPOLLWAITERS];
  80. };
  81. /****************************************************************************
  82. * Private Function Prototypes
  83. ****************************************************************************/
  84. /* Semaphore helpers */
  85. static inline int ajoy_takesem(sem_t *sem);
  86. #define ajoy_givesem(s) nxsem_post(s);
  87. /* Sampling and Interrupt handling */
  88. static void ajoy_enable(FAR struct ajoy_upperhalf_s *priv);
  89. static void ajoy_interrupt(FAR const struct ajoy_lowerhalf_s *lower,
  90. FAR void *arg);
  91. /* Sampling */
  92. static void ajoy_sample(FAR struct ajoy_upperhalf_s *priv);
  93. /* Character driver methods */
  94. static int ajoy_open(FAR struct file *filep);
  95. static int ajoy_close(FAR struct file *filep);
  96. static ssize_t ajoy_read(FAR struct file *filep, FAR char *buffer,
  97. size_t buflen);
  98. static int ajoy_ioctl(FAR struct file *filep, int cmd,
  99. unsigned long arg);
  100. static int ajoy_poll(FAR struct file *filep, FAR struct pollfd *fds,
  101. bool setup);
  102. /****************************************************************************
  103. * Private Data
  104. ****************************************************************************/
  105. static const struct file_operations ajoy_fops =
  106. {
  107. ajoy_open, /* open */
  108. ajoy_close, /* close */
  109. ajoy_read, /* read */
  110. 0, /* write */
  111. 0, /* seek */
  112. ajoy_ioctl, /* ioctl */
  113. ajoy_poll /* poll */
  114. };
  115. /****************************************************************************
  116. * Private Functions
  117. ****************************************************************************/
  118. /****************************************************************************
  119. * Name: ajoy_takesem
  120. ****************************************************************************/
  121. static inline int ajoy_takesem(sem_t *sem)
  122. {
  123. return nxsem_wait(sem);
  124. }
  125. /****************************************************************************
  126. * Name: ajoy_enable
  127. ****************************************************************************/
  128. static void ajoy_enable(FAR struct ajoy_upperhalf_s *priv)
  129. {
  130. FAR const struct ajoy_lowerhalf_s *lower;
  131. FAR struct ajoy_open_s *opriv;
  132. ajoy_buttonset_t press;
  133. ajoy_buttonset_t release;
  134. irqstate_t flags;
  135. int i;
  136. DEBUGASSERT(priv);
  137. lower = priv->au_lower;
  138. DEBUGASSERT(lower);
  139. /* This routine is called both task level and interrupt level, so
  140. * interrupts must be disabled.
  141. */
  142. flags = enter_critical_section();
  143. /* Visit each opened reference to the device */
  144. press = 0;
  145. release = 0;
  146. for (opriv = priv->au_open; opriv; opriv = opriv->ao_flink)
  147. {
  148. /* Are there any poll waiters? */
  149. for (i = 0; i < CONFIG_INPUT_AJOYSTICK_NPOLLWAITERS; i++)
  150. {
  151. if (opriv->ao_fds[i])
  152. {
  153. /* Yes.. OR in the poll event buttons */
  154. press |= opriv->ao_pollevents.ap_press;
  155. release |= opriv->ao_pollevents.ap_release;
  156. break;
  157. }
  158. }
  159. /* OR in the signal events */
  160. press |= opriv->ao_notify.an_press;
  161. release |= opriv->ao_notify.an_release;
  162. }
  163. /* Enable/disable button interrupts */
  164. DEBUGASSERT(lower->al_enable);
  165. if (press != 0 || release != 0)
  166. {
  167. /* Enable interrupts with the new button set */
  168. lower->al_enable(lower, press, release,
  169. (ajoy_handler_t)ajoy_interrupt, priv);
  170. }
  171. else
  172. {
  173. /* Disable further interrupts */
  174. lower->al_enable(lower, 0, 0, NULL, NULL);
  175. }
  176. leave_critical_section(flags);
  177. }
  178. /****************************************************************************
  179. * Name: ajoy_interrupt
  180. ****************************************************************************/
  181. static void ajoy_interrupt(FAR const struct ajoy_lowerhalf_s *lower,
  182. FAR void *arg)
  183. {
  184. FAR struct ajoy_upperhalf_s *priv = (FAR struct ajoy_upperhalf_s *)arg;
  185. DEBUGASSERT(priv);
  186. /* Process the next sample */
  187. ajoy_sample(priv);
  188. }
  189. /****************************************************************************
  190. * Name: ajoy_sample
  191. ****************************************************************************/
  192. static void ajoy_sample(FAR struct ajoy_upperhalf_s *priv)
  193. {
  194. FAR const struct ajoy_lowerhalf_s *lower;
  195. FAR struct ajoy_open_s *opriv;
  196. ajoy_buttonset_t sample;
  197. ajoy_buttonset_t change;
  198. ajoy_buttonset_t press;
  199. ajoy_buttonset_t release;
  200. irqstate_t flags;
  201. int i;
  202. DEBUGASSERT(priv);
  203. lower = priv->au_lower;
  204. DEBUGASSERT(lower);
  205. /* This routine is called both task level and interrupt level, so
  206. * interrupts must be disabled.
  207. */
  208. flags = enter_critical_section();
  209. /* Sample the new button state */
  210. DEBUGASSERT(lower->al_buttons);
  211. sample = lower->al_buttons(lower);
  212. add_ui_randomness(sample);
  213. /* Determine which buttons have been newly pressed and which have been
  214. * newly released.
  215. */
  216. change = sample ^ priv->au_sample;
  217. press = change & sample;
  218. DEBUGASSERT(lower->al_supported);
  219. release = change & (lower->al_supported(lower) & ~sample);
  220. /* Visit each opened reference to the device */
  221. for (opriv = priv->au_open; opriv; opriv = opriv->ao_flink)
  222. {
  223. /* Have any poll events occurred? */
  224. if ((press & opriv->ao_pollevents.ap_press) != 0 ||
  225. (release & opriv->ao_pollevents.ap_release) != 0)
  226. {
  227. /* Yes.. Notify all waiters */
  228. for (i = 0; i < CONFIG_INPUT_AJOYSTICK_NPOLLWAITERS; i++)
  229. {
  230. FAR struct pollfd *fds = opriv->ao_fds[i];
  231. if (fds)
  232. {
  233. fds->revents |= (fds->events & POLLIN);
  234. if (fds->revents != 0)
  235. {
  236. iinfo("Report events: %02x\n", fds->revents);
  237. nxsem_post(fds->sem);
  238. }
  239. }
  240. }
  241. }
  242. /* Have any signal events occurred? */
  243. if ((press & opriv->ao_notify.an_press) != 0 ||
  244. (release & opriv->ao_notify.an_release) != 0)
  245. {
  246. /* Yes.. Signal the waiter */
  247. opriv->ao_notify.an_event.sigev_value.sival_int = sample;
  248. nxsig_notification(opriv->ao_pid, &opriv->ao_notify.an_event,
  249. SI_QUEUE, &opriv->ao_work);
  250. }
  251. }
  252. /* Enable/disable interrupt handling */
  253. ajoy_enable(priv);
  254. priv->au_sample = sample;
  255. leave_critical_section(flags);
  256. }
  257. /****************************************************************************
  258. * Name: ajoy_open
  259. ****************************************************************************/
  260. static int ajoy_open(FAR struct file *filep)
  261. {
  262. FAR struct inode *inode;
  263. FAR struct ajoy_upperhalf_s *priv;
  264. FAR struct ajoy_open_s *opriv;
  265. FAR const struct ajoy_lowerhalf_s *lower;
  266. ajoy_buttonset_t supported;
  267. int ret;
  268. DEBUGASSERT(filep && filep->f_inode);
  269. inode = filep->f_inode;
  270. DEBUGASSERT(inode->i_private);
  271. priv = (FAR struct ajoy_upperhalf_s *)inode->i_private;
  272. /* Get exclusive access to the driver structure */
  273. ret = ajoy_takesem(&priv->au_exclsem);
  274. if (ret < 0)
  275. {
  276. ierr("ERROR: ajoy_takesem failed: %d\n", ret);
  277. return ret;
  278. }
  279. /* Allocate a new open structure */
  280. opriv = (FAR struct ajoy_open_s *)kmm_zalloc(sizeof(struct ajoy_open_s));
  281. if (!opriv)
  282. {
  283. ierr("ERROR: Failed to allocate open structure\n");
  284. ret = -ENOMEM;
  285. goto errout_with_sem;
  286. }
  287. /* Initialize the open structure */
  288. lower = priv->au_lower;
  289. DEBUGASSERT(lower && lower->al_supported);
  290. supported = lower->al_supported(lower);
  291. opriv->ao_pollevents.ap_press = supported;
  292. opriv->ao_pollevents.ap_release = supported;
  293. /* Attach the open structure to the device */
  294. opriv->ao_flink = priv->au_open;
  295. priv->au_open = opriv;
  296. /* Attach the open structure to the file structure */
  297. filep->f_priv = (FAR void *)opriv;
  298. ret = OK;
  299. errout_with_sem:
  300. ajoy_givesem(&priv->au_exclsem);
  301. return ret;
  302. }
  303. /****************************************************************************
  304. * Name: ajoy_close
  305. ****************************************************************************/
  306. static int ajoy_close(FAR struct file *filep)
  307. {
  308. FAR struct inode *inode;
  309. FAR struct ajoy_upperhalf_s *priv;
  310. FAR struct ajoy_open_s *opriv;
  311. FAR struct ajoy_open_s *curr;
  312. FAR struct ajoy_open_s *prev;
  313. irqstate_t flags;
  314. bool closing;
  315. int ret;
  316. DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
  317. opriv = filep->f_priv;
  318. inode = filep->f_inode;
  319. DEBUGASSERT(inode->i_private);
  320. priv = (FAR struct ajoy_upperhalf_s *)inode->i_private;
  321. /* Handle an improbable race conditions with the following atomic test
  322. * and set.
  323. *
  324. * This is actually a pretty feeble attempt to handle this. The
  325. * improbable race condition occurs if two different threads try to
  326. * close the joystick driver at the same time. The rule: don't do
  327. * that! It is feeble because we do not really enforce stale pointer
  328. * detection anyway.
  329. */
  330. flags = enter_critical_section();
  331. closing = opriv->ao_closing;
  332. opriv->ao_closing = true;
  333. leave_critical_section(flags);
  334. if (closing)
  335. {
  336. /* Another thread is doing the close */
  337. return OK;
  338. }
  339. /* Get exclusive access to the driver structure */
  340. ret = ajoy_takesem(&priv->au_exclsem);
  341. if (ret < 0)
  342. {
  343. ierr("ERROR: ajoy_takesem failed: %d\n", ret);
  344. return ret;
  345. }
  346. /* Find the open structure in the list of open structures for the device */
  347. for (prev = NULL, curr = priv->au_open;
  348. curr && curr != opriv;
  349. prev = curr, curr = curr->ao_flink);
  350. DEBUGASSERT(curr);
  351. if (!curr)
  352. {
  353. ierr("ERROR: Failed to find open entry\n");
  354. ret = -ENOENT;
  355. goto errout_with_exclsem;
  356. }
  357. /* Remove the structure from the device */
  358. if (prev)
  359. {
  360. prev->ao_flink = opriv->ao_flink;
  361. }
  362. else
  363. {
  364. priv->au_open = opriv->ao_flink;
  365. }
  366. /* Cancel any pending notification */
  367. nxsig_cancel_notification(&opriv->ao_work);
  368. /* And free the open structure */
  369. kmm_free(opriv);
  370. /* Enable/disable interrupt handling */
  371. ajoy_enable(priv);
  372. ret = OK;
  373. errout_with_exclsem:
  374. ajoy_givesem(&priv->au_exclsem);
  375. return ret;
  376. }
  377. /****************************************************************************
  378. * Name: ajoy_read
  379. ****************************************************************************/
  380. static ssize_t ajoy_read(FAR struct file *filep, FAR char *buffer,
  381. size_t len)
  382. {
  383. FAR struct inode *inode;
  384. FAR struct ajoy_upperhalf_s *priv;
  385. FAR const struct ajoy_lowerhalf_s *lower;
  386. int ret;
  387. DEBUGASSERT(filep && filep->f_inode);
  388. inode = filep->f_inode;
  389. DEBUGASSERT(inode->i_private);
  390. priv = (FAR struct ajoy_upperhalf_s *)inode->i_private;
  391. /* Make sure that the buffer is sufficiently large to hold at least one
  392. * complete sample.
  393. *
  394. * REVISIT: Should also check buffer alignment.
  395. */
  396. if (len < sizeof(struct ajoy_sample_s))
  397. {
  398. ierr("ERROR: buffer too small: %lu\n", (unsigned long)len);
  399. return -EINVAL;
  400. }
  401. /* Get exclusive access to the driver structure */
  402. ret = ajoy_takesem(&priv->au_exclsem);
  403. if (ret < 0)
  404. {
  405. ierr("ERROR: ajoy_takesem failed: %d\n", ret);
  406. return ret;
  407. }
  408. /* Read and return the current state of the joystick buttons */
  409. lower = priv->au_lower;
  410. DEBUGASSERT(lower && lower->al_sample);
  411. ret = lower->al_sample(lower, (FAR struct ajoy_sample_s *)buffer);
  412. if (ret >= 0)
  413. {
  414. ret = sizeof(struct ajoy_sample_s);
  415. }
  416. ajoy_givesem(&priv->au_exclsem);
  417. return (ssize_t)ret;
  418. }
  419. /****************************************************************************
  420. * Name: ajoy_ioctl
  421. ****************************************************************************/
  422. static int ajoy_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  423. {
  424. FAR struct inode *inode;
  425. FAR struct ajoy_upperhalf_s *priv;
  426. FAR struct ajoy_open_s *opriv;
  427. FAR const struct ajoy_lowerhalf_s *lower;
  428. int ret;
  429. DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
  430. opriv = filep->f_priv;
  431. inode = filep->f_inode;
  432. DEBUGASSERT(inode->i_private);
  433. priv = (FAR struct ajoy_upperhalf_s *)inode->i_private;
  434. /* Get exclusive access to the driver structure */
  435. ret = ajoy_takesem(&priv->au_exclsem);
  436. if (ret < 0)
  437. {
  438. ierr("ERROR: ajoy_takesem failed: %d\n", ret);
  439. return ret;
  440. }
  441. /* Handle the ioctl command */
  442. ret = -EINVAL;
  443. switch (cmd)
  444. {
  445. /* Command: AJOYIOC_SUPPORTED
  446. * Description: Report the set of button events supported by the
  447. * hardware;
  448. * Argument: A pointer to writeable integer value in which to return
  449. * the set of supported buttons.
  450. * Return: Zero (OK) on success. Minus one will be returned on
  451. * failure with the errno value set appropriately.
  452. */
  453. case AJOYIOC_SUPPORTED:
  454. {
  455. FAR int *supported = (FAR int *)((uintptr_t)arg);
  456. if (supported)
  457. {
  458. lower = priv->au_lower;
  459. DEBUGASSERT(lower && lower->al_supported);
  460. *supported = (int)lower->al_supported(lower);
  461. ret = OK;
  462. }
  463. }
  464. break;
  465. /* Command: AJOYIOC_POLLEVENTS
  466. * Description: Specify the set of button events that can cause a poll()
  467. * to awaken. The default is all button depressions and
  468. * all button releases (all supported buttons);
  469. * Argument: A read-only pointer to an instance of struct
  470. * ajoy_pollevents_s
  471. * Return: Zero (OK) on success. Minus one will be returned on
  472. * failure with the errno value set appropriately.
  473. */
  474. case AJOYIOC_POLLEVENTS:
  475. {
  476. FAR struct ajoy_pollevents_s *pollevents =
  477. (FAR struct ajoy_pollevents_s *)((uintptr_t)arg);
  478. if (pollevents)
  479. {
  480. /* Save the poll events */
  481. opriv->ao_pollevents.ap_press = pollevents->ap_press;
  482. opriv->ao_pollevents.ap_release = pollevents->ap_release;
  483. /* Enable/disable interrupt handling */
  484. ajoy_enable(priv);
  485. ret = OK;
  486. }
  487. }
  488. break;
  489. /* Command: AJOYIOC_REGISTER
  490. * Description: Register to receive a signal whenever there is a change
  491. * in any of the joystick discrete inputs. This feature,
  492. * of course, depends upon interrupt GPIO support from the
  493. * platform.
  494. * Argument: A read-only pointer to an instance of struct
  495. * ajoy_notify_s
  496. * Return: Zero (OK) on success. Minus one will be returned on
  497. * failure with the errno value set appropriately.
  498. */
  499. case AJOYIOC_REGISTER:
  500. {
  501. FAR struct ajoy_notify_s *notify =
  502. (FAR struct ajoy_notify_s *)((uintptr_t)arg);
  503. if (notify)
  504. {
  505. /* Save the notification events */
  506. opriv->ao_notify.an_press = notify->an_press;
  507. opriv->ao_notify.an_release = notify->an_release;
  508. opriv->ao_notify.an_event = notify->an_event;
  509. opriv->ao_pid = getpid();
  510. /* Enable/disable interrupt handling */
  511. ajoy_enable(priv);
  512. ret = OK;
  513. }
  514. }
  515. break;
  516. default:
  517. ierr("ERROR: Unrecognized command: %ld\n", cmd);
  518. ret = -ENOTTY;
  519. break;
  520. }
  521. ajoy_givesem(&priv->au_exclsem);
  522. return ret;
  523. }
  524. /****************************************************************************
  525. * Name: ajoy_poll
  526. ****************************************************************************/
  527. static int ajoy_poll(FAR struct file *filep, FAR struct pollfd *fds,
  528. bool setup)
  529. {
  530. FAR struct inode *inode;
  531. FAR struct ajoy_upperhalf_s *priv;
  532. FAR struct ajoy_open_s *opriv;
  533. int ret;
  534. int i;
  535. DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
  536. opriv = filep->f_priv;
  537. inode = filep->f_inode;
  538. DEBUGASSERT(inode->i_private);
  539. priv = (FAR struct ajoy_upperhalf_s *)inode->i_private;
  540. /* Get exclusive access to the driver structure */
  541. ret = ajoy_takesem(&priv->au_exclsem);
  542. if (ret < 0)
  543. {
  544. ierr("ERROR: ajoy_takesem failed: %d\n", ret);
  545. return ret;
  546. }
  547. /* Are we setting up the poll? Or tearing it down? */
  548. if (setup)
  549. {
  550. /* This is a request to set up the poll. Find an available
  551. * slot for the poll structure reference
  552. */
  553. for (i = 0; i < CONFIG_INPUT_AJOYSTICK_NPOLLWAITERS; i++)
  554. {
  555. /* Find an available slot */
  556. if (!opriv->ao_fds[i])
  557. {
  558. /* Bind the poll structure and this slot */
  559. opriv->ao_fds[i] = fds;
  560. fds->priv = &opriv->ao_fds[i];
  561. break;
  562. }
  563. }
  564. if (i >= CONFIG_INPUT_AJOYSTICK_NPOLLWAITERS)
  565. {
  566. ierr("ERROR: Too man poll waiters\n");
  567. fds->priv = NULL;
  568. ret = -EBUSY;
  569. goto errout_with_dusem;
  570. }
  571. }
  572. else if (fds->priv)
  573. {
  574. /* This is a request to tear down the poll. */
  575. FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
  576. #ifdef CONFIG_DEBUG_FEATURES
  577. if (!slot)
  578. {
  579. ierr("ERROR: Poll slot not found\n");
  580. ret = -EIO;
  581. goto errout_with_dusem;
  582. }
  583. #endif
  584. /* Remove all memory of the poll setup */
  585. *slot = NULL;
  586. fds->priv = NULL;
  587. }
  588. errout_with_dusem:
  589. ajoy_givesem(&priv->au_exclsem);
  590. return ret;
  591. }
  592. /****************************************************************************
  593. * Public Functions
  594. ****************************************************************************/
  595. /****************************************************************************
  596. * Name: ajoy_register
  597. *
  598. * Description:
  599. * Bind the lower half analog joystick driver to an instance of the
  600. * upper half analog joystick driver and register the composite character
  601. * driver as the specific device.
  602. *
  603. * Input Parameters:
  604. * devname - The name of the analog joystick device to be registers.
  605. * This should be a string of the form "/priv/ajoyN" where N is the
  606. * minor device number.
  607. * lower - An instance of the platform-specific analog joystick lower
  608. * half driver.
  609. *
  610. * Returned Value:
  611. * Zero (OK) is returned on success. Otherwise a negated errno value is
  612. * returned to indicate the nature of the failure.
  613. *
  614. ****************************************************************************/
  615. int ajoy_register(FAR const char *devname,
  616. FAR const struct ajoy_lowerhalf_s *lower)
  617. {
  618. FAR struct ajoy_upperhalf_s *priv;
  619. int ret;
  620. DEBUGASSERT(devname && lower);
  621. /* Allocate a new ajoystick driver instance */
  622. priv = (FAR struct ajoy_upperhalf_s *)
  623. kmm_zalloc(sizeof(struct ajoy_upperhalf_s));
  624. if (!priv)
  625. {
  626. ierr("ERROR: Failed to allocate device structure\n");
  627. return -ENOMEM;
  628. }
  629. /* Make sure that all ajoystick interrupts are disabled */
  630. DEBUGASSERT(lower->al_enable);
  631. lower->al_enable(lower, 0, 0, NULL, NULL);
  632. /* Initialize the new ajoystick driver instance */
  633. priv->au_lower = lower;
  634. nxsem_init(&priv->au_exclsem, 0, 1);
  635. DEBUGASSERT(lower->al_buttons);
  636. priv->au_sample = lower->al_buttons(lower);
  637. /* And register the ajoystick driver */
  638. ret = register_driver(devname, &ajoy_fops, 0666, priv);
  639. if (ret < 0)
  640. {
  641. ierr("ERROR: register_driver failed: %d\n", ret);
  642. goto errout_with_priv;
  643. }
  644. return OK;
  645. errout_with_priv:
  646. nxsem_destroy(&priv->au_exclsem);
  647. kmm_free(priv);
  648. return ret;
  649. }