noteram_driver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /****************************************************************************
  2. * drivers/note/noteram_driver.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. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sched.h>
  26. #include <fcntl.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <nuttx/spinlock.h>
  31. #include <nuttx/sched.h>
  32. #include <nuttx/sched_note.h>
  33. #include <nuttx/note/noteram_driver.h>
  34. #include <nuttx/fs/fs.h>
  35. /****************************************************************************
  36. * Private Types
  37. ****************************************************************************/
  38. struct noteram_info_s
  39. {
  40. volatile unsigned int ni_head;
  41. volatile unsigned int ni_tail;
  42. volatile unsigned int ni_read;
  43. unsigned int ni_overwrite;
  44. uint8_t ni_buffer[CONFIG_DRIVER_NOTERAM_BUFSIZE];
  45. };
  46. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  47. struct noteram_taskname_info_s
  48. {
  49. uint8_t size;
  50. uint8_t pid[2];
  51. char name[1];
  52. };
  53. struct noteram_taskname_s
  54. {
  55. int buffer_used;
  56. char buffer[CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE];
  57. };
  58. #endif
  59. /****************************************************************************
  60. * Private Function Prototypes
  61. ****************************************************************************/
  62. static int noteram_open(FAR struct file *filep);
  63. static ssize_t noteram_read(FAR struct file *filep,
  64. FAR char *buffer, size_t buflen);
  65. static int noteram_ioctl(struct file *filep, int cmd, unsigned long arg);
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. static const struct file_operations g_noteram_fops =
  70. {
  71. noteram_open, /* open */
  72. NULL, /* close */
  73. noteram_read, /* read */
  74. NULL, /* write */
  75. NULL, /* seek */
  76. noteram_ioctl, /* ioctl */
  77. NULL /* poll */
  78. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  79. , 0 /* unlink */
  80. #endif
  81. };
  82. static struct noteram_info_s g_noteram_info =
  83. {
  84. #ifdef CONFIG_DRIVER_NOTERAM_DEFAULT_NOOVERWRITE
  85. .ni_overwrite = NOTERAM_MODE_OVERWRITE_DISABLE
  86. #else
  87. .ni_overwrite = NOTERAM_MODE_OVERWRITE_ENABLE
  88. #endif
  89. };
  90. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  91. static struct noteram_taskname_s g_noteram_taskname;
  92. #endif
  93. #ifdef CONFIG_SMP
  94. static volatile spinlock_t g_noteram_lock;
  95. #endif
  96. /****************************************************************************
  97. * Private Functions
  98. ****************************************************************************/
  99. /****************************************************************************
  100. * Name: noteram_find_taskname
  101. *
  102. * Description:
  103. * Find task name info corresponding to the specified PID
  104. *
  105. * Input Parameters:
  106. * PID - Task ID
  107. *
  108. * Returned Value:
  109. * Pointer to the task name info
  110. * If the corresponding info doesn't exist in the buffer, NULL is returned.
  111. *
  112. ****************************************************************************/
  113. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  114. static FAR struct noteram_taskname_info_s *noteram_find_taskname(pid_t pid)
  115. {
  116. int n;
  117. FAR struct noteram_taskname_info_s *ti;
  118. for (n = 0; n < g_noteram_taskname.buffer_used; )
  119. {
  120. ti = (FAR struct noteram_taskname_info_s *)
  121. &g_noteram_taskname.buffer[n];
  122. if (ti->pid[0] + (ti->pid[1] << 8) == pid)
  123. {
  124. return ti;
  125. }
  126. n += ti->size;
  127. }
  128. return NULL;
  129. }
  130. #endif
  131. /****************************************************************************
  132. * Name: noteram_record_taskname
  133. *
  134. * Description:
  135. * Record the task name info of the specified task
  136. *
  137. * Input Parameters:
  138. * PID - Task ID
  139. * name - task name
  140. *
  141. * Returned Value:
  142. * None
  143. *
  144. ****************************************************************************/
  145. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  146. static void noteram_record_taskname(pid_t pid, const char *name)
  147. {
  148. FAR struct noteram_taskname_info_s *ti;
  149. size_t tilen;
  150. size_t namelen;
  151. namelen = strlen(name);
  152. DEBUGASSERT(namelen <= CONFIG_TASK_NAME_SIZE);
  153. tilen = sizeof(struct noteram_taskname_info_s) + namelen;
  154. DEBUGASSERT(tilen <= UCHAR_MAX);
  155. if (g_noteram_taskname.buffer_used + tilen >
  156. CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE)
  157. {
  158. /* No space in the buffer - ignored */
  159. return;
  160. }
  161. ti = (FAR struct noteram_taskname_info_s *)
  162. &g_noteram_taskname.buffer[g_noteram_taskname.buffer_used];
  163. ti->size = tilen;
  164. ti->pid[0] = pid & 0xff;
  165. ti->pid[1] = (pid >> 8) & 0xff;
  166. strncpy(ti->name, name, namelen + 1);
  167. g_noteram_taskname.buffer_used += tilen;
  168. }
  169. #endif
  170. /****************************************************************************
  171. * Name: noteram_remove_taskname
  172. *
  173. * Description:
  174. * Remove the task name info corresponding to the specified PID
  175. *
  176. * Input Parameters:
  177. * PID - Task ID
  178. *
  179. * Returned Value:
  180. * None
  181. *
  182. ****************************************************************************/
  183. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  184. static void noteram_remove_taskname(pid_t pid)
  185. {
  186. FAR struct noteram_taskname_info_s *ti;
  187. size_t tilen;
  188. char *src;
  189. int sindex;
  190. ti = noteram_find_taskname(pid);
  191. if (ti == NULL)
  192. {
  193. return;
  194. }
  195. tilen = ti->size;
  196. src = (char *)ti + tilen;
  197. sindex = src - g_noteram_taskname.buffer;
  198. memcpy(ti, src, g_noteram_taskname.buffer_used - sindex);
  199. g_noteram_taskname.buffer_used -= tilen;
  200. }
  201. #endif
  202. /****************************************************************************
  203. * Name: noteram_get_taskname
  204. *
  205. * Description:
  206. * Get the task name string of the specified PID
  207. *
  208. * Input Parameters:
  209. * PID - Task ID
  210. *
  211. * Returned Value:
  212. * Pointer to the task name string
  213. * If the corresponding name doesn't exist in the buffer, NULL is returned.
  214. *
  215. ****************************************************************************/
  216. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  217. static const char *noteram_get_taskname(pid_t pid)
  218. {
  219. irqstate_t irq_mask;
  220. const char *ret = NULL;
  221. FAR struct noteram_taskname_info_s *ti;
  222. FAR struct tcb_s *tcb;
  223. irq_mask = enter_critical_section();
  224. ti = noteram_find_taskname(pid);
  225. if (ti != NULL)
  226. {
  227. ret = ti->name;
  228. }
  229. else
  230. {
  231. tcb = nxsched_get_tcb(pid);
  232. if (tcb != NULL)
  233. {
  234. noteram_record_taskname(pid, tcb->name);
  235. ret = tcb->name;
  236. }
  237. }
  238. leave_critical_section(irq_mask);
  239. return ret;
  240. }
  241. #endif
  242. /****************************************************************************
  243. * Name: noteram_buffer_clear
  244. *
  245. * Description:
  246. * Clear all contents of the circular buffer.
  247. *
  248. * Input Parameters:
  249. * None.
  250. *
  251. * Returned Value:
  252. * None.
  253. *
  254. ****************************************************************************/
  255. static void noteram_buffer_clear(void)
  256. {
  257. irqstate_t flags;
  258. flags = enter_critical_section();
  259. g_noteram_info.ni_tail = g_noteram_info.ni_head;
  260. g_noteram_info.ni_read = g_noteram_info.ni_head;
  261. if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_OVERFLOW)
  262. {
  263. g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_DISABLE;
  264. }
  265. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  266. g_noteram_taskname.buffer_used = 0;
  267. #endif
  268. leave_critical_section(flags);
  269. }
  270. /****************************************************************************
  271. * Name: noteram_next
  272. *
  273. * Description:
  274. * Return the circular buffer index at offset from the specified index
  275. * value, handling wraparound
  276. *
  277. * Input Parameters:
  278. * ndx - Old circular buffer index
  279. *
  280. * Returned Value:
  281. * New circular buffer index
  282. *
  283. ****************************************************************************/
  284. static inline unsigned int noteram_next(unsigned int ndx,
  285. unsigned int offset)
  286. {
  287. ndx += offset;
  288. if (ndx >= CONFIG_DRIVER_NOTERAM_BUFSIZE)
  289. {
  290. ndx -= CONFIG_DRIVER_NOTERAM_BUFSIZE;
  291. }
  292. return ndx;
  293. }
  294. /****************************************************************************
  295. * Name: noteram_length
  296. *
  297. * Description:
  298. * Length of data currently in circular buffer.
  299. *
  300. * Input Parameters:
  301. * None
  302. *
  303. * Returned Value:
  304. * Length of data currently in circular buffer.
  305. *
  306. ****************************************************************************/
  307. static unsigned int noteram_length(void)
  308. {
  309. unsigned int head = g_noteram_info.ni_head;
  310. unsigned int tail = g_noteram_info.ni_tail;
  311. if (tail > head)
  312. {
  313. head += CONFIG_DRIVER_NOTERAM_BUFSIZE;
  314. }
  315. return head - tail;
  316. }
  317. /****************************************************************************
  318. * Name: noteram_unread_length
  319. *
  320. * Description:
  321. * Length of unread data currently in circular buffer.
  322. *
  323. * Input Parameters:
  324. * None
  325. *
  326. * Returned Value:
  327. * Length of unread data currently in circular buffer.
  328. *
  329. ****************************************************************************/
  330. static unsigned int noteram_unread_length(void)
  331. {
  332. unsigned int head = g_noteram_info.ni_head;
  333. unsigned int read = g_noteram_info.ni_read;
  334. if (read > head)
  335. {
  336. head += CONFIG_DRIVER_NOTERAM_BUFSIZE;
  337. }
  338. return head - read;
  339. }
  340. /****************************************************************************
  341. * Name: noteram_remove
  342. *
  343. * Description:
  344. * Remove the variable length note from the tail of the circular buffer
  345. *
  346. * Input Parameters:
  347. * None
  348. *
  349. * Returned Value:
  350. * None
  351. *
  352. * Assumptions:
  353. * We are within a critical section.
  354. *
  355. ****************************************************************************/
  356. static void noteram_remove(void)
  357. {
  358. FAR struct note_common_s *note;
  359. unsigned int tail;
  360. unsigned int length;
  361. /* Get the tail index of the circular buffer */
  362. tail = g_noteram_info.ni_tail;
  363. DEBUGASSERT(tail < CONFIG_DRIVER_NOTERAM_BUFSIZE);
  364. /* Get the length of the note at the tail index */
  365. note = (FAR struct note_common_s *)&g_noteram_info.ni_buffer[tail];
  366. length = note->nc_length;
  367. DEBUGASSERT(length <= noteram_length());
  368. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  369. if (note->nc_type == NOTE_STOP)
  370. {
  371. /* The name of the task is no longer needed because the task is deleted
  372. * and the corresponding notes are lost.
  373. */
  374. noteram_remove_taskname(note->nc_pid[0] + (note->nc_pid[1] << 8));
  375. }
  376. #endif
  377. /* Increment the tail index to remove the entire note from the circular
  378. * buffer.
  379. */
  380. if (g_noteram_info.ni_read == g_noteram_info.ni_tail)
  381. {
  382. /* The read index also needs increment. */
  383. g_noteram_info.ni_read = noteram_next(tail, length);
  384. }
  385. g_noteram_info.ni_tail = noteram_next(tail, length);
  386. }
  387. /****************************************************************************
  388. * Name: noteram_get
  389. *
  390. * Description:
  391. * Get the next note from the read index of the circular buffer.
  392. *
  393. * Input Parameters:
  394. * buffer - Location to return the next note
  395. * buflen - The length of the user provided buffer.
  396. *
  397. * Returned Value:
  398. * On success, the positive, non-zero length of the return note is
  399. * provided. Zero is returned only if the circular buffer is empty. A
  400. * negated errno value is returned in the event of any failure.
  401. *
  402. ****************************************************************************/
  403. static ssize_t noteram_get(FAR uint8_t *buffer, size_t buflen)
  404. {
  405. FAR struct note_common_s *note;
  406. irqstate_t flags;
  407. unsigned int remaining;
  408. unsigned int read;
  409. ssize_t notelen;
  410. size_t circlen;
  411. DEBUGASSERT(buffer != NULL);
  412. flags = enter_critical_section();
  413. /* Verify that the circular buffer is not empty */
  414. circlen = noteram_unread_length();
  415. if (circlen <= 0)
  416. {
  417. notelen = 0;
  418. goto errout_with_csection;
  419. }
  420. /* Get the read index of the circular buffer */
  421. read = g_noteram_info.ni_read;
  422. DEBUGASSERT(read < CONFIG_DRIVER_NOTERAM_BUFSIZE);
  423. /* Get the length of the note at the read index */
  424. note = (FAR struct note_common_s *)&g_noteram_info.ni_buffer[read];
  425. notelen = note->nc_length;
  426. DEBUGASSERT(notelen <= circlen);
  427. /* Is the user buffer large enough to hold the note? */
  428. if (buflen < notelen)
  429. {
  430. /* Skip the large note so that we do not get constipated. */
  431. g_noteram_info.ni_read = noteram_next(read, notelen);
  432. /* and return an error */
  433. notelen = -EFBIG;
  434. goto errout_with_csection;
  435. }
  436. /* Loop until the note has been transferred to the user buffer */
  437. remaining = (unsigned int)notelen;
  438. while (remaining > 0)
  439. {
  440. /* Copy the next byte at the read index */
  441. *buffer++ = g_noteram_info.ni_buffer[read];
  442. /* Adjust indices and counts */
  443. read = noteram_next(read, 1);
  444. remaining--;
  445. }
  446. g_noteram_info.ni_read = read;
  447. errout_with_csection:
  448. leave_critical_section(flags);
  449. return notelen;
  450. }
  451. /****************************************************************************
  452. * Name: noteram_size
  453. *
  454. * Description:
  455. * Return the size of the next note at the read index of the circular
  456. * buffer.
  457. *
  458. * Input Parameters:
  459. * None.
  460. *
  461. * Returned Value:
  462. * Zero is returned if the circular buffer is empty. Otherwise, the size
  463. * of the next note is returned.
  464. *
  465. ****************************************************************************/
  466. static ssize_t noteram_size(void)
  467. {
  468. FAR struct note_common_s *note;
  469. irqstate_t flags;
  470. unsigned int read;
  471. ssize_t notelen;
  472. size_t circlen;
  473. flags = enter_critical_section();
  474. /* Verify that the circular buffer is not empty */
  475. circlen = noteram_unread_length();
  476. if (circlen <= 0)
  477. {
  478. notelen = 0;
  479. goto errout_with_csection;
  480. }
  481. /* Get the read index of the circular buffer */
  482. read = g_noteram_info.ni_read;
  483. DEBUGASSERT(read < CONFIG_DRIVER_NOTERAM_BUFSIZE);
  484. /* Get the length of the note at the read index */
  485. note = (FAR struct note_common_s *)&g_noteram_info.ni_buffer[read];
  486. notelen = note->nc_length;
  487. DEBUGASSERT(notelen <= circlen);
  488. errout_with_csection:
  489. leave_critical_section(flags);
  490. return notelen;
  491. }
  492. /****************************************************************************
  493. * Name: noteram_open
  494. ****************************************************************************/
  495. static int noteram_open(FAR struct file *filep)
  496. {
  497. /* Reset the read index of the circular buffer */
  498. g_noteram_info.ni_read = g_noteram_info.ni_tail;
  499. return OK;
  500. }
  501. /****************************************************************************
  502. * Name: noteram_read
  503. ****************************************************************************/
  504. static ssize_t noteram_read(FAR struct file *filep,
  505. FAR char *buffer, size_t buflen)
  506. {
  507. ssize_t notelen;
  508. ssize_t retlen ;
  509. DEBUGASSERT(filep != 0 && buffer != NULL && buflen > 0);
  510. /* Then loop, adding as many notes as possible to the user buffer. */
  511. retlen = 0;
  512. sched_lock();
  513. do
  514. {
  515. /* Get the next note (removing it from the buffer) */
  516. notelen = noteram_get((FAR uint8_t *)buffer, buflen);
  517. if (notelen < 0)
  518. {
  519. /* We were unable to read the next note, probably because it will
  520. * not fit into the user buffer.
  521. */
  522. if (retlen == 0)
  523. {
  524. /* If nothing was read then report the error. Otherwise,
  525. * just silently drop the note.
  526. */
  527. retlen = notelen;
  528. }
  529. break;
  530. }
  531. /* Update pointers from the note that was transferred */
  532. retlen += notelen;
  533. buffer += notelen;
  534. buflen -= notelen;
  535. /* Will the next note fit? There is a race here and even if the next
  536. * note will fit, it may fail still when noteram_get() is called.
  537. *
  538. * It won't fit (or an error occurred). Return what we have without
  539. * trying to get the next note (which would cause it to be deleted).
  540. */
  541. notelen = noteram_size();
  542. }
  543. while (notelen > 0 && notelen <= buflen);
  544. sched_unlock();
  545. return retlen;
  546. }
  547. /****************************************************************************
  548. * Name: noteram_ioctl
  549. ****************************************************************************/
  550. static int noteram_ioctl(struct file *filep, int cmd, unsigned long arg)
  551. {
  552. int ret = -ENOSYS;
  553. /* Handle the ioctl commands */
  554. switch (cmd)
  555. {
  556. /* NOTERAM_CLEAR
  557. * - Clear all contents of the circular buffer
  558. * Argument: Ignored
  559. */
  560. case NOTERAM_CLEAR:
  561. noteram_buffer_clear();
  562. ret = OK;
  563. break;
  564. /* NOTERAM_GETMODE
  565. * - Get overwrite mode
  566. * Argument: A writable pointer to unsigned int
  567. */
  568. case NOTERAM_GETMODE:
  569. if (arg == 0)
  570. {
  571. ret = -EINVAL;
  572. }
  573. else
  574. {
  575. *(unsigned int *)arg = g_noteram_info.ni_overwrite;
  576. ret = OK;
  577. }
  578. break;
  579. /* NOTERAM_SETMODE
  580. * - Set overwrite mode
  581. * Argument: A read-only pointer to unsigned int
  582. */
  583. case NOTERAM_SETMODE:
  584. if (arg == 0)
  585. {
  586. ret = -EINVAL;
  587. }
  588. else
  589. {
  590. g_noteram_info.ni_overwrite = *(unsigned int *)arg;
  591. ret = OK;
  592. }
  593. break;
  594. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  595. /* NOTERAM_GETTASKNAME
  596. * - Get task name string
  597. * Argument: A writable pointer to struct note_get_taskname_s
  598. * Result: If -ESRCH, the corresponding task name doesn't
  599. * exist.
  600. */
  601. case NOTERAM_GETTASKNAME:
  602. {
  603. struct noteram_get_taskname_s *param;
  604. const char *taskname;
  605. if (arg == 0)
  606. {
  607. ret = -EINVAL;
  608. break;
  609. }
  610. param = (struct noteram_get_taskname_s *)arg;
  611. taskname = noteram_get_taskname(param->pid);
  612. if (taskname != NULL)
  613. {
  614. strncpy(param->taskname, taskname, CONFIG_TASK_NAME_SIZE + 1);
  615. param->taskname[CONFIG_TASK_NAME_SIZE] = '\0';
  616. ret = 0;
  617. }
  618. else
  619. {
  620. param->taskname[0] = '\0';
  621. ret = -ESRCH;
  622. }
  623. }
  624. break;
  625. #endif
  626. default:
  627. break;
  628. }
  629. return ret;
  630. }
  631. /****************************************************************************
  632. * Public Functions
  633. ****************************************************************************/
  634. /****************************************************************************
  635. * Name: sched_note_add
  636. *
  637. * Description:
  638. * Add the variable length note to the transport layer
  639. *
  640. * Input Parameters:
  641. * note - The note buffer
  642. * notelen - The buffer length
  643. *
  644. * Returned Value:
  645. * None
  646. *
  647. * Assumptions:
  648. * We are within a critical section.
  649. *
  650. ****************************************************************************/
  651. void sched_note_add(FAR const void *note, size_t notelen)
  652. {
  653. FAR const char *buf = note;
  654. unsigned int head;
  655. unsigned int next;
  656. irqstate_t flags;
  657. flags = up_irq_save();
  658. #ifdef CONFIG_SMP
  659. spin_lock_wo_note(&g_noteram_lock);
  660. #endif
  661. if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_OVERFLOW)
  662. {
  663. #ifdef CONFIG_SMP
  664. spin_unlock_wo_note(&g_noteram_lock);
  665. #endif
  666. up_irq_restore(flags);
  667. return;
  668. }
  669. #if CONFIG_DRIVER_NOTERAM_TASKNAME_BUFSIZE > 0
  670. /* Record the name if the new task was created */
  671. {
  672. FAR struct note_start_s *note_st;
  673. note_st = (FAR struct note_start_s *)note;
  674. if (note_st->nst_cmn.nc_type == NOTE_START)
  675. {
  676. noteram_record_taskname(note_st->nst_cmn.nc_pid[0] +
  677. (note_st->nst_cmn.nc_pid[1] << 8),
  678. note_st->nst_name);
  679. }
  680. }
  681. #endif
  682. /* Get the index to the head of the circular buffer */
  683. DEBUGASSERT(note != NULL && notelen < CONFIG_DRIVER_NOTERAM_BUFSIZE);
  684. head = g_noteram_info.ni_head;
  685. /* Loop until all bytes have been transferred to the circular buffer */
  686. while (notelen > 0)
  687. {
  688. /* Get the next head index. Would it collide with the current tail
  689. * index?
  690. */
  691. next = noteram_next(head, 1);
  692. if (next == g_noteram_info.ni_tail)
  693. {
  694. if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_DISABLE)
  695. {
  696. /* Stop recording if not in overwrite mode */
  697. g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW;
  698. #ifdef CONFIG_SMP
  699. spin_unlock_wo_note(&g_noteram_lock);
  700. #endif
  701. up_irq_restore(flags);
  702. return;
  703. }
  704. /* Yes, then remove the note at the tail index */
  705. noteram_remove();
  706. }
  707. /* Save the next byte at the head index */
  708. g_noteram_info.ni_buffer[head] = *buf++;
  709. head = next;
  710. notelen--;
  711. }
  712. g_noteram_info.ni_head = head;
  713. #ifdef CONFIG_SMP
  714. spin_unlock_wo_note(&g_noteram_lock);
  715. #endif
  716. up_irq_restore(flags);
  717. }
  718. /****************************************************************************
  719. * Name: noteram_register
  720. *
  721. * Description:
  722. * Register a serial driver at /dev/note that can be used by an
  723. * application to read data from the circular note buffer.
  724. *
  725. * Input Parameters:
  726. * None.
  727. *
  728. * Returned Value:
  729. * Zero on succress. A negated errno value is returned on a failure.
  730. *
  731. ****************************************************************************/
  732. int noteram_register(void)
  733. {
  734. return register_driver("/dev/note", &g_noteram_fops, 0666, NULL);
  735. }