msgqueue.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * linux/drivers/acorn/scsi/msgqueue.c
  3. *
  4. * Copyright (C) 1997-1998 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * message queue handling
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/stddef.h>
  15. #include <linux/init.h>
  16. #include "msgqueue.h"
  17. /*
  18. * Function: struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
  19. * Purpose : Allocate a message queue entry
  20. * Params : msgq - message queue to claim entry for
  21. * Returns : message queue entry or NULL.
  22. */
  23. static struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
  24. {
  25. struct msgqueue_entry *mq;
  26. if ((mq = msgq->free) != NULL)
  27. msgq->free = mq->next;
  28. return mq;
  29. }
  30. /*
  31. * Function: void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
  32. * Purpose : free a message queue entry
  33. * Params : msgq - message queue to free entry from
  34. * mq - message queue entry to free
  35. */
  36. static void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
  37. {
  38. if (mq) {
  39. mq->next = msgq->free;
  40. msgq->free = mq;
  41. }
  42. }
  43. /*
  44. * Function: void msgqueue_initialise(MsgQueue_t *msgq)
  45. * Purpose : initialise a message queue
  46. * Params : msgq - queue to initialise
  47. */
  48. void msgqueue_initialise(MsgQueue_t *msgq)
  49. {
  50. int i;
  51. msgq->qe = NULL;
  52. msgq->free = &msgq->entries[0];
  53. for (i = 0; i < NR_MESSAGES; i++)
  54. msgq->entries[i].next = &msgq->entries[i + 1];
  55. msgq->entries[NR_MESSAGES - 1].next = NULL;
  56. }
  57. /*
  58. * Function: void msgqueue_free(MsgQueue_t *msgq)
  59. * Purpose : free a queue
  60. * Params : msgq - queue to free
  61. */
  62. void msgqueue_free(MsgQueue_t *msgq)
  63. {
  64. }
  65. /*
  66. * Function: int msgqueue_msglength(MsgQueue_t *msgq)
  67. * Purpose : calculate the total length of all messages on the message queue
  68. * Params : msgq - queue to examine
  69. * Returns : number of bytes of messages in queue
  70. */
  71. int msgqueue_msglength(MsgQueue_t *msgq)
  72. {
  73. struct msgqueue_entry *mq = msgq->qe;
  74. int length = 0;
  75. for (mq = msgq->qe; mq; mq = mq->next)
  76. length += mq->msg.length;
  77. return length;
  78. }
  79. /*
  80. * Function: struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
  81. * Purpose : return a message
  82. * Params : msgq - queue to obtain message from
  83. * : msgno - message number
  84. * Returns : pointer to message string, or NULL
  85. */
  86. struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
  87. {
  88. struct msgqueue_entry *mq;
  89. for (mq = msgq->qe; mq && msgno; mq = mq->next, msgno--);
  90. return mq ? &mq->msg : NULL;
  91. }
  92. /*
  93. * Function: int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
  94. * Purpose : add a message onto a message queue
  95. * Params : msgq - queue to add message on
  96. * length - length of message
  97. * ... - message bytes
  98. * Returns : != 0 if successful
  99. */
  100. int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
  101. {
  102. struct msgqueue_entry *mq = mqe_alloc(msgq);
  103. va_list ap;
  104. if (mq) {
  105. struct msgqueue_entry **mqp;
  106. int i;
  107. va_start(ap, length);
  108. for (i = 0; i < length; i++)
  109. mq->msg.msg[i] = va_arg(ap, unsigned int);
  110. va_end(ap);
  111. mq->msg.length = length;
  112. mq->msg.fifo = 0;
  113. mq->next = NULL;
  114. mqp = &msgq->qe;
  115. while (*mqp)
  116. mqp = &(*mqp)->next;
  117. *mqp = mq;
  118. }
  119. return mq != NULL;
  120. }
  121. /*
  122. * Function: void msgqueue_flush(MsgQueue_t *msgq)
  123. * Purpose : flush all messages from message queue
  124. * Params : msgq - queue to flush
  125. */
  126. void msgqueue_flush(MsgQueue_t *msgq)
  127. {
  128. struct msgqueue_entry *mq, *mqnext;
  129. for (mq = msgq->qe; mq; mq = mqnext) {
  130. mqnext = mq->next;
  131. mqe_free(msgq, mq);
  132. }
  133. msgq->qe = NULL;
  134. }
  135. EXPORT_SYMBOL(msgqueue_initialise);
  136. EXPORT_SYMBOL(msgqueue_free);
  137. EXPORT_SYMBOL(msgqueue_msglength);
  138. EXPORT_SYMBOL(msgqueue_getmsg);
  139. EXPORT_SYMBOL(msgqueue_addmsg);
  140. EXPORT_SYMBOL(msgqueue_flush);
  141. MODULE_AUTHOR("Russell King");
  142. MODULE_DESCRIPTION("SCSI message queue handling");
  143. MODULE_LICENSE("GPL");