djoystick.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /****************************************************************************
  2. * drivers/input/djoystick.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 discrete joystick device. A
  21. * discrete joystick refers to a joystick that could be implemented entirely
  22. * with GPIO input pins. So up, down, left, and right are all discrete
  23. * values like buttons (as opposed to integer values like you might obtain
  24. * from an analog joystick).
  25. *
  26. * The discrete joystick driver exports a standard character driver
  27. * interface. By convention, the discrete joystick is registered as an input
  28. * device at /dev/djoyN 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/djoystick.h>
  46. #include <nuttx/irq.h>
  47. /****************************************************************************
  48. * Private Types
  49. ****************************************************************************/
  50. /* This structure provides the state of one discrete joystick driver */
  51. struct djoy_upperhalf_s
  52. {
  53. /* Saved binding to the lower half discrete joystick driver */
  54. FAR const struct djoy_lowerhalf_s *du_lower;
  55. djoy_buttonset_t du_enabled; /* Set of currently enabled button interrupts */
  56. djoy_buttonset_t du_sample; /* Last sampled button states */
  57. sem_t du_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 djoy_open_s *du_open;
  62. };
  63. /* This structure describes the state of one open joystick driver instance */
  64. struct djoy_open_s
  65. {
  66. /* Supports a singly linked list */
  67. FAR struct djoy_open_s *do_flink;
  68. /* The following will be true if we are closing */
  69. volatile bool do_closing;
  70. /* Joystick event notification information */
  71. pid_t do_pid;
  72. struct djoy_notify_s do_notify;
  73. struct sigwork_s do_work;
  74. /* Poll event information */
  75. struct djoy_pollevents_s do_pollevents;
  76. /* The following is a list if poll structures of threads waiting for
  77. * driver events.
  78. */
  79. FAR struct pollfd *do_fds[CONFIG_INPUT_DJOYSTICK_NPOLLWAITERS];
  80. };
  81. /****************************************************************************
  82. * Private Function Prototypes
  83. ****************************************************************************/
  84. /* Semaphore helpers */
  85. static inline int djoy_takesem(sem_t *sem);
  86. #define djoy_givesem(s) nxsem_post(s);
  87. /* Sampling and Interrupt handling */
  88. static void djoy_enable(FAR struct djoy_upperhalf_s *priv);
  89. static void djoy_interrupt(FAR const struct djoy_lowerhalf_s *lower,
  90. FAR void *arg);
  91. /* Sampling */
  92. static void djoy_sample(FAR struct djoy_upperhalf_s *priv);
  93. /* Character driver methods */
  94. static int djoy_open(FAR struct file *filep);
  95. static int djoy_close(FAR struct file *filep);
  96. static ssize_t djoy_read(FAR struct file *filep, FAR char *buffer,
  97. size_t buflen);
  98. static int djoy_ioctl(FAR struct file *filep, int cmd,
  99. unsigned long arg);
  100. static int djoy_poll(FAR struct file *filep, FAR struct pollfd *fds,
  101. bool setup);
  102. /****************************************************************************
  103. * Private Data
  104. ****************************************************************************/
  105. static const struct file_operations djoy_fops =
  106. {
  107. djoy_open, /* open */
  108. djoy_close, /* close */
  109. djoy_read, /* read */
  110. 0, /* write */
  111. 0, /* seek */
  112. djoy_ioctl, /* ioctl */
  113. djoy_poll /* poll */
  114. };
  115. /****************************************************************************
  116. * Private Functions
  117. ****************************************************************************/
  118. /****************************************************************************
  119. * Name: djoy_takesem
  120. ****************************************************************************/
  121. static inline int djoy_takesem(sem_t *sem)
  122. {
  123. return nxsem_wait(sem);
  124. }
  125. /****************************************************************************
  126. * Name: djoy_enable
  127. ****************************************************************************/
  128. static void djoy_enable(FAR struct djoy_upperhalf_s *priv)
  129. {
  130. FAR const struct djoy_lowerhalf_s *lower;
  131. FAR struct djoy_open_s *opriv;
  132. djoy_buttonset_t press;
  133. djoy_buttonset_t release;
  134. irqstate_t flags;
  135. int i;
  136. DEBUGASSERT(priv);
  137. lower = priv->du_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->du_open; opriv; opriv = opriv->do_flink)
  147. {
  148. /* Are there any poll waiters? */
  149. for (i = 0; i < CONFIG_INPUT_DJOYSTICK_NPOLLWAITERS; i++)
  150. {
  151. if (opriv->do_fds[i])
  152. {
  153. /* Yes.. OR in the poll event buttons */
  154. press |= opriv->do_pollevents.dp_press;
  155. release |= opriv->do_pollevents.dp_release;
  156. break;
  157. }
  158. }
  159. /* OR in the signal events */
  160. press |= opriv->do_notify.dn_press;
  161. release |= opriv->do_notify.dn_release;
  162. }
  163. /* Enable/disable button interrupts */
  164. DEBUGASSERT(lower->dl_enable);
  165. if (press != 0 || release != 0)
  166. {
  167. /* Enable interrupts with the new button set */
  168. lower->dl_enable(lower, press, release,
  169. (djoy_interrupt_t)djoy_interrupt, priv);
  170. }
  171. else
  172. {
  173. /* Disable further interrupts */
  174. lower->dl_enable(lower, 0, 0, NULL, NULL);
  175. }
  176. leave_critical_section(flags);
  177. }
  178. /****************************************************************************
  179. * Name: djoy_interrupt
  180. ****************************************************************************/
  181. static void djoy_interrupt(FAR const struct djoy_lowerhalf_s *lower,
  182. FAR void *arg)
  183. {
  184. FAR struct djoy_upperhalf_s *priv = (FAR struct djoy_upperhalf_s *)arg;
  185. DEBUGASSERT(priv);
  186. /* Process the next sample */
  187. djoy_sample(priv);
  188. }
  189. /****************************************************************************
  190. * Name: djoy_sample
  191. ****************************************************************************/
  192. static void djoy_sample(FAR struct djoy_upperhalf_s *priv)
  193. {
  194. FAR const struct djoy_lowerhalf_s *lower;
  195. FAR struct djoy_open_s *opriv;
  196. djoy_buttonset_t sample;
  197. djoy_buttonset_t change;
  198. djoy_buttonset_t press;
  199. djoy_buttonset_t release;
  200. irqstate_t flags;
  201. int i;
  202. DEBUGASSERT(priv);
  203. lower = priv->du_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->dl_sample);
  211. sample = lower->dl_sample(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->du_sample;
  217. press = change & sample;
  218. DEBUGASSERT(lower->dl_supported);
  219. release = change & (lower->dl_supported(lower) & ~sample);
  220. /* Visit each opened reference to the device */
  221. for (opriv = priv->du_open; opriv; opriv = opriv->do_flink)
  222. {
  223. /* Have any poll events occurred? */
  224. if ((press & opriv->do_pollevents.dp_press) != 0 ||
  225. (release & opriv->do_pollevents.dp_release) != 0)
  226. {
  227. /* Yes.. Notify all waiters */
  228. for (i = 0; i < CONFIG_INPUT_DJOYSTICK_NPOLLWAITERS; i++)
  229. {
  230. FAR struct pollfd *fds = opriv->do_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->do_notify.dn_press) != 0 ||
  244. (release & opriv->do_notify.dn_release) != 0)
  245. {
  246. /* Yes.. Signal the waiter */
  247. opriv->do_notify.dn_event.sigev_value.sival_int = sample;
  248. nxsig_notification(opriv->do_pid, &opriv->do_notify.dn_event,
  249. SI_QUEUE, &opriv->do_work);
  250. }
  251. }
  252. /* Enable/disable interrupt handling */
  253. djoy_enable(priv);
  254. priv->du_sample = sample;
  255. leave_critical_section(flags);
  256. }
  257. /****************************************************************************
  258. * Name: djoy_open
  259. ****************************************************************************/
  260. static int djoy_open(FAR struct file *filep)
  261. {
  262. FAR struct inode *inode;
  263. FAR struct djoy_upperhalf_s *priv;
  264. FAR struct djoy_open_s *opriv;
  265. FAR const struct djoy_lowerhalf_s *lower;
  266. djoy_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 djoy_upperhalf_s *)inode->i_private;
  272. /* Get exclusive access to the driver structure */
  273. ret = djoy_takesem(&priv->du_exclsem);
  274. if (ret < 0)
  275. {
  276. ierr("ERROR: djoy_takesem failed: %d\n", ret);
  277. return ret;
  278. }
  279. /* Allocate a new open structure */
  280. opriv = (FAR struct djoy_open_s *)kmm_zalloc(sizeof(struct djoy_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->du_lower;
  289. DEBUGASSERT(lower && lower->dl_supported);
  290. supported = lower->dl_supported(lower);
  291. opriv->do_pollevents.dp_press = supported;
  292. opriv->do_pollevents.dp_release = supported;
  293. /* Attach the open structure to the device */
  294. opriv->do_flink = priv->du_open;
  295. priv->du_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. djoy_givesem(&priv->du_exclsem);
  301. return ret;
  302. }
  303. /****************************************************************************
  304. * Name: djoy_close
  305. ****************************************************************************/
  306. static int djoy_close(FAR struct file *filep)
  307. {
  308. FAR struct inode *inode;
  309. FAR struct djoy_upperhalf_s *priv;
  310. FAR struct djoy_open_s *opriv;
  311. FAR struct djoy_open_s *curr;
  312. FAR struct djoy_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 djoy_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->do_closing;
  332. opriv->do_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 = djoy_takesem(&priv->du_exclsem);
  341. if (ret < 0)
  342. {
  343. ierr("ERROR: djoy_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->du_open;
  348. curr && curr != opriv;
  349. prev = curr, curr = curr->do_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->do_flink = opriv->do_flink;
  361. }
  362. else
  363. {
  364. priv->du_open = opriv->do_flink;
  365. }
  366. /* Cancel any pending notification */
  367. nxsig_cancel_notification(&opriv->do_work);
  368. /* And free the open structure */
  369. kmm_free(opriv);
  370. /* Enable/disable interrupt handling */
  371. djoy_enable(priv);
  372. ret = OK;
  373. errout_with_exclsem:
  374. djoy_givesem(&priv->du_exclsem);
  375. return ret;
  376. }
  377. /****************************************************************************
  378. * Name: djoy_read
  379. ****************************************************************************/
  380. static ssize_t djoy_read(FAR struct file *filep, FAR char *buffer,
  381. size_t len)
  382. {
  383. FAR struct inode *inode;
  384. FAR struct djoy_upperhalf_s *priv;
  385. FAR const struct djoy_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 djoy_upperhalf_s *)inode->i_private;
  391. /* Make sure that the buffer is sufficiently large to hold at least one
  392. * complete sample.
  393. */
  394. if (len < sizeof(djoy_buttonset_t))
  395. {
  396. ierr("ERROR: buffer too small: %lu\n", (unsigned long)len);
  397. return -EINVAL;
  398. }
  399. /* Get exclusive access to the driver structure */
  400. ret = djoy_takesem(&priv->du_exclsem);
  401. if (ret < 0)
  402. {
  403. ierr("ERROR: djoy_takesem failed: %d\n", ret);
  404. return ret;
  405. }
  406. /* Read and return the current state of the joystick buttons */
  407. lower = priv->du_lower;
  408. DEBUGASSERT(lower && lower->dl_sample);
  409. priv->du_sample = lower->dl_sample(lower);
  410. *(FAR djoy_buttonset_t *)buffer = priv->du_sample;
  411. ret = sizeof(djoy_buttonset_t);
  412. djoy_givesem(&priv->du_exclsem);
  413. return (ssize_t)ret;
  414. }
  415. /****************************************************************************
  416. * Name: djoy_ioctl
  417. ****************************************************************************/
  418. static int djoy_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  419. {
  420. FAR struct inode *inode;
  421. FAR struct djoy_upperhalf_s *priv;
  422. FAR struct djoy_open_s *opriv;
  423. FAR const struct djoy_lowerhalf_s *lower;
  424. int ret;
  425. DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
  426. opriv = filep->f_priv;
  427. inode = filep->f_inode;
  428. DEBUGASSERT(inode->i_private);
  429. priv = (FAR struct djoy_upperhalf_s *)inode->i_private;
  430. /* Get exclusive access to the driver structure */
  431. ret = djoy_takesem(&priv->du_exclsem);
  432. if (ret < 0)
  433. {
  434. ierr("ERROR: djoy_takesem failed: %d\n", ret);
  435. return ret;
  436. }
  437. /* Handle the ioctl command */
  438. ret = -EINVAL;
  439. switch (cmd)
  440. {
  441. /* Command: DJOYIOC_SUPPORTED
  442. * Description: Report the set of button events supported by the
  443. * hardware;
  444. * Argument: A pointer to writeable integer value in which to return
  445. * the set of supported buttons.
  446. * Return: Zero (OK) on success. Minus one will be returned on
  447. * failure with the errno value set appropriately.
  448. */
  449. case DJOYIOC_SUPPORTED:
  450. {
  451. FAR int *supported = (FAR int *)((uintptr_t)arg);
  452. if (supported)
  453. {
  454. lower = priv->du_lower;
  455. DEBUGASSERT(lower && lower->dl_supported);
  456. *supported = (int)lower->dl_supported(lower);
  457. ret = OK;
  458. }
  459. }
  460. break;
  461. /* Command: DJOYIOC_POLLEVENTS
  462. * Description: Specify the set of button events that can cause a poll()
  463. * to awaken. The default is all button depressions and
  464. * all button releases (all supported buttons);
  465. * Argument: A read-only pointer to an instance of struct
  466. * djoy_pollevents_s
  467. * Return: Zero (OK) on success. Minus one will be returned on
  468. * failure with the errno value set appropriately.
  469. */
  470. case DJOYIOC_POLLEVENTS:
  471. {
  472. FAR struct djoy_pollevents_s *pollevents =
  473. (FAR struct djoy_pollevents_s *)((uintptr_t)arg);
  474. if (pollevents)
  475. {
  476. /* Save the poll events */
  477. opriv->do_pollevents.dp_press = pollevents->dp_press;
  478. opriv->do_pollevents.dp_release = pollevents->dp_release;
  479. /* Enable/disable interrupt handling */
  480. djoy_enable(priv);
  481. ret = OK;
  482. }
  483. }
  484. break;
  485. /* Command: DJOYIOC_REGISTER
  486. * Description: Register to receive a signal whenever there is a change
  487. * in any of the joystick discrete inputs. This feature,
  488. * of course, depends upon interrupt GPIO support from the
  489. * platform.
  490. * Argument: A read-only pointer to an instance of struct
  491. * djoy_notify_s
  492. * Return: Zero (OK) on success. Minus one will be returned on
  493. * failure with the errno value set appropriately.
  494. */
  495. case DJOYIOC_REGISTER:
  496. {
  497. FAR struct djoy_notify_s *notify =
  498. (FAR struct djoy_notify_s *)((uintptr_t)arg);
  499. if (notify)
  500. {
  501. /* Save the notification events */
  502. opriv->do_notify.dn_press = notify->dn_press;
  503. opriv->do_notify.dn_release = notify->dn_release;
  504. opriv->do_notify.dn_event = notify->dn_event;
  505. opriv->do_pid = getpid();
  506. /* Enable/disable interrupt handling */
  507. djoy_enable(priv);
  508. ret = OK;
  509. }
  510. }
  511. break;
  512. default:
  513. ierr("ERROR: Unrecognized command: %d\n", cmd);
  514. ret = -ENOTTY;
  515. break;
  516. }
  517. djoy_givesem(&priv->du_exclsem);
  518. return ret;
  519. }
  520. /****************************************************************************
  521. * Name: djoy_poll
  522. ****************************************************************************/
  523. static int djoy_poll(FAR struct file *filep, FAR struct pollfd *fds,
  524. bool setup)
  525. {
  526. FAR struct inode *inode;
  527. FAR struct djoy_upperhalf_s *priv;
  528. FAR struct djoy_open_s *opriv;
  529. int ret;
  530. int i;
  531. DEBUGASSERT(filep && filep->f_priv && filep->f_inode);
  532. opriv = filep->f_priv;
  533. inode = filep->f_inode;
  534. DEBUGASSERT(inode->i_private);
  535. priv = (FAR struct djoy_upperhalf_s *)inode->i_private;
  536. /* Get exclusive access to the driver structure */
  537. ret = djoy_takesem(&priv->du_exclsem);
  538. if (ret < 0)
  539. {
  540. ierr("ERROR: djoy_takesem failed: %d\n", ret);
  541. return ret;
  542. }
  543. /* Are we setting up the poll? Or tearing it down? */
  544. if (setup)
  545. {
  546. /* This is a request to set up the poll. Find an available
  547. * slot for the poll structure reference
  548. */
  549. for (i = 0; i < CONFIG_INPUT_DJOYSTICK_NPOLLWAITERS; i++)
  550. {
  551. /* Find an available slot */
  552. if (!opriv->do_fds[i])
  553. {
  554. /* Bind the poll structure and this slot */
  555. opriv->do_fds[i] = fds;
  556. fds->priv = &opriv->do_fds[i];
  557. break;
  558. }
  559. }
  560. if (i >= CONFIG_INPUT_DJOYSTICK_NPOLLWAITERS)
  561. {
  562. ierr("ERROR: Too man poll waiters\n");
  563. fds->priv = NULL;
  564. ret = -EBUSY;
  565. goto errout_with_dusem;
  566. }
  567. }
  568. else if (fds->priv)
  569. {
  570. /* This is a request to tear down the poll. */
  571. FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
  572. #ifdef CONFIG_DEBUG_FEATURES
  573. if (!slot)
  574. {
  575. ierr("ERROR: Poll slot not found\n");
  576. ret = -EIO;
  577. goto errout_with_dusem;
  578. }
  579. #endif
  580. /* Remove all memory of the poll setup */
  581. *slot = NULL;
  582. fds->priv = NULL;
  583. }
  584. errout_with_dusem:
  585. djoy_givesem(&priv->du_exclsem);
  586. return ret;
  587. }
  588. /****************************************************************************
  589. * Public Functions
  590. ****************************************************************************/
  591. /****************************************************************************
  592. * Name: djoy_register
  593. *
  594. * Description:
  595. * Bind the lower half discrete joystick driver to an instance of the
  596. * upper half discrete joystick driver and register the composite character
  597. * driver as the specific device.
  598. *
  599. * Input Parameters:
  600. * devname - The name of the discrete joystick device to be registers.
  601. * This should be a string of the form "/priv/djoyN" where N is the
  602. * minor device number.
  603. * lower - An instance of the platform-specific discrete joystick lower
  604. * half driver.
  605. *
  606. * Returned Value:
  607. * Zero (OK) is returned on success. Otherwise a negated errno value is
  608. * returned to indicate the nature of the failure.
  609. *
  610. ****************************************************************************/
  611. int djoy_register(FAR const char *devname,
  612. FAR const struct djoy_lowerhalf_s *lower)
  613. {
  614. FAR struct djoy_upperhalf_s *priv;
  615. int ret;
  616. DEBUGASSERT(devname && lower);
  617. /* Allocate a new djoystick driver instance */
  618. priv = (FAR struct djoy_upperhalf_s *)
  619. kmm_zalloc(sizeof(struct djoy_upperhalf_s));
  620. if (!priv)
  621. {
  622. ierr("ERROR: Failed to allocate device structure\n");
  623. return -ENOMEM;
  624. }
  625. /* Make sure that all djoystick interrupts are disabled */
  626. DEBUGASSERT(lower->dl_enable);
  627. lower->dl_enable(lower, 0, 0, NULL, NULL);
  628. /* Initialize the new djoystick driver instance */
  629. priv->du_lower = lower;
  630. nxsem_init(&priv->du_exclsem, 0, 1);
  631. DEBUGASSERT(lower->dl_sample);
  632. priv->du_sample = lower->dl_sample(lower);
  633. /* And register the djoystick driver */
  634. ret = register_driver(devname, &djoy_fops, 0666, priv);
  635. if (ret < 0)
  636. {
  637. ierr("ERROR: register_driver failed: %d\n", ret);
  638. nxsem_destroy(&priv->du_exclsem);
  639. kmm_free(priv);
  640. }
  641. return ret;
  642. }