WIP FPC-III support
[linux/fpc-iii.git] / drivers / scsi / arm / msgqueue.c
blob58115831362f917c0029b31a772eb3d0e6822227
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/acorn/scsi/msgqueue.c
5 * Copyright (C) 1997-1998 Russell King
7 * message queue handling
8 */
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/stddef.h>
12 #include <linux/init.h>
14 #include "msgqueue.h"
17 * Function: struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
18 * Purpose : Allocate a message queue entry
19 * Params : msgq - message queue to claim entry for
20 * Returns : message queue entry or NULL.
22 static struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
24 struct msgqueue_entry *mq;
26 if ((mq = msgq->free) != NULL)
27 msgq->free = mq->next;
29 return mq;
33 * Function: void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
34 * Purpose : free a message queue entry
35 * Params : msgq - message queue to free entry from
36 * mq - message queue entry to free
38 static void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
40 if (mq) {
41 mq->next = msgq->free;
42 msgq->free = mq;
47 * Function: void msgqueue_initialise(MsgQueue_t *msgq)
48 * Purpose : initialise a message queue
49 * Params : msgq - queue to initialise
51 void msgqueue_initialise(MsgQueue_t *msgq)
53 int i;
55 msgq->qe = NULL;
56 msgq->free = &msgq->entries[0];
58 for (i = 0; i < NR_MESSAGES; i++)
59 msgq->entries[i].next = &msgq->entries[i + 1];
61 msgq->entries[NR_MESSAGES - 1].next = NULL;
66 * Function: void msgqueue_free(MsgQueue_t *msgq)
67 * Purpose : free a queue
68 * Params : msgq - queue to free
70 void msgqueue_free(MsgQueue_t *msgq)
75 * Function: int msgqueue_msglength(MsgQueue_t *msgq)
76 * Purpose : calculate the total length of all messages on the message queue
77 * Params : msgq - queue to examine
78 * Returns : number of bytes of messages in queue
80 int msgqueue_msglength(MsgQueue_t *msgq)
82 struct msgqueue_entry *mq = msgq->qe;
83 int length = 0;
85 for (mq = msgq->qe; mq; mq = mq->next)
86 length += mq->msg.length;
88 return length;
92 * Function: struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
93 * Purpose : return a message
94 * Params : msgq - queue to obtain message from
95 * : msgno - message number
96 * Returns : pointer to message string, or NULL
98 struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
100 struct msgqueue_entry *mq;
102 for (mq = msgq->qe; mq && msgno; mq = mq->next, msgno--);
104 return mq ? &mq->msg : NULL;
108 * Function: int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
109 * Purpose : add a message onto a message queue
110 * Params : msgq - queue to add message on
111 * length - length of message
112 * ... - message bytes
113 * Returns : != 0 if successful
115 int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
117 struct msgqueue_entry *mq = mqe_alloc(msgq);
118 va_list ap;
120 if (mq) {
121 struct msgqueue_entry **mqp;
122 int i;
124 va_start(ap, length);
125 for (i = 0; i < length; i++)
126 mq->msg.msg[i] = va_arg(ap, unsigned int);
127 va_end(ap);
129 mq->msg.length = length;
130 mq->msg.fifo = 0;
131 mq->next = NULL;
133 mqp = &msgq->qe;
134 while (*mqp)
135 mqp = &(*mqp)->next;
137 *mqp = mq;
140 return mq != NULL;
144 * Function: void msgqueue_flush(MsgQueue_t *msgq)
145 * Purpose : flush all messages from message queue
146 * Params : msgq - queue to flush
148 void msgqueue_flush(MsgQueue_t *msgq)
150 struct msgqueue_entry *mq, *mqnext;
152 for (mq = msgq->qe; mq; mq = mqnext) {
153 mqnext = mq->next;
154 mqe_free(msgq, mq);
156 msgq->qe = NULL;
159 EXPORT_SYMBOL(msgqueue_initialise);
160 EXPORT_SYMBOL(msgqueue_free);
161 EXPORT_SYMBOL(msgqueue_msglength);
162 EXPORT_SYMBOL(msgqueue_getmsg);
163 EXPORT_SYMBOL(msgqueue_addmsg);
164 EXPORT_SYMBOL(msgqueue_flush);
166 MODULE_AUTHOR("Russell King");
167 MODULE_DESCRIPTION("SCSI message queue handling");
168 MODULE_LICENSE("GPL");