2 * linux/drivers/acorn/scsi/msgqueue.c
4 * Copyright (C) 1997-1998 Russell King
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.
10 * message queue handling
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/stddef.h>
15 #include <linux/init.h>
20 * Function: struct msgqueue_entry *mqe_alloc(MsgQueue_t *msgq)
21 * Purpose : Allocate a message queue entry
22 * Params : msgq - message queue to claim entry for
23 * Returns : message queue entry or NULL.
25 static struct msgqueue_entry
*mqe_alloc(MsgQueue_t
*msgq
)
27 struct msgqueue_entry
*mq
;
29 if ((mq
= msgq
->free
) != NULL
)
30 msgq
->free
= mq
->next
;
36 * Function: void mqe_free(MsgQueue_t *msgq, struct msgqueue_entry *mq)
37 * Purpose : free a message queue entry
38 * Params : msgq - message queue to free entry from
39 * mq - message queue entry to free
41 static void mqe_free(MsgQueue_t
*msgq
, struct msgqueue_entry
*mq
)
44 mq
->next
= msgq
->free
;
50 * Function: void msgqueue_initialise(MsgQueue_t *msgq)
51 * Purpose : initialise a message queue
52 * Params : msgq - queue to initialise
54 void msgqueue_initialise(MsgQueue_t
*msgq
)
59 msgq
->free
= &msgq
->entries
[0];
61 for (i
= 0; i
< NR_MESSAGES
; i
++)
62 msgq
->entries
[i
].next
= &msgq
->entries
[i
+ 1];
64 msgq
->entries
[NR_MESSAGES
- 1].next
= NULL
;
69 * Function: void msgqueue_free(MsgQueue_t *msgq)
70 * Purpose : free a queue
71 * Params : msgq - queue to free
73 void msgqueue_free(MsgQueue_t
*msgq
)
78 * Function: int msgqueue_msglength(MsgQueue_t *msgq)
79 * Purpose : calculate the total length of all messages on the message queue
80 * Params : msgq - queue to examine
81 * Returns : number of bytes of messages in queue
83 int msgqueue_msglength(MsgQueue_t
*msgq
)
85 struct msgqueue_entry
*mq
= msgq
->qe
;
88 for (mq
= msgq
->qe
; mq
; mq
= mq
->next
)
89 length
+= mq
->msg
.length
;
95 * Function: struct message *msgqueue_getmsg(MsgQueue_t *msgq, int msgno)
96 * Purpose : return a message
97 * Params : msgq - queue to obtain message from
98 * : msgno - message number
99 * Returns : pointer to message string, or NULL
101 struct message
*msgqueue_getmsg(MsgQueue_t
*msgq
, int msgno
)
103 struct msgqueue_entry
*mq
;
105 for (mq
= msgq
->qe
; mq
&& msgno
; mq
= mq
->next
, msgno
--);
107 return mq
? &mq
->msg
: NULL
;
111 * Function: int msgqueue_addmsg(MsgQueue_t *msgq, int length, ...)
112 * Purpose : add a message onto a message queue
113 * Params : msgq - queue to add message on
114 * length - length of message
115 * ... - message bytes
116 * Returns : != 0 if successful
118 int msgqueue_addmsg(MsgQueue_t
*msgq
, int length
, ...)
120 struct msgqueue_entry
*mq
= mqe_alloc(msgq
);
124 struct msgqueue_entry
**mqp
;
127 va_start(ap
, length
);
128 for (i
= 0; i
< length
; i
++)
129 mq
->msg
.msg
[i
] = va_arg(ap
, unsigned int);
132 mq
->msg
.length
= length
;
147 * Function: void msgqueue_flush(MsgQueue_t *msgq)
148 * Purpose : flush all messages from message queue
149 * Params : msgq - queue to flush
151 void msgqueue_flush(MsgQueue_t
*msgq
)
153 struct msgqueue_entry
*mq
, *mqnext
;
155 for (mq
= msgq
->qe
; mq
; mq
= mqnext
) {
162 EXPORT_SYMBOL(msgqueue_initialise
);
163 EXPORT_SYMBOL(msgqueue_free
);
164 EXPORT_SYMBOL(msgqueue_msglength
);
165 EXPORT_SYMBOL(msgqueue_getmsg
);
166 EXPORT_SYMBOL(msgqueue_addmsg
);
167 EXPORT_SYMBOL(msgqueue_flush
);
169 MODULE_AUTHOR("Russell King");
170 MODULE_DESCRIPTION("SCSI message queue handling");
171 MODULE_LICENSE("GPL");