1 /* can_queue.c - CAN message queues
2 * Linux CAN-bus device driver.
3 * New CAN queues by Pavel Pisa - OCERA team member
4 * email:pisa@cmp.felk.cvut.cz
5 * This software is released under the GPL-License.
6 * Version lincan-0.2 9 Jul 2003
10 #include <linux/module.h>
11 #include <linux/version.h>
12 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
13 #include <linux/malloc.h>
15 #include <linux/slab.h>
17 #include <linux/wait.h>
18 #include "../include/can.h"
19 #include "../include/can_queue.h"
22 #define DEBUGQUE(fmt,args...) printk(KERN_ERR "can_queue (debug): " fmt,\
25 atomic_t edge_num_cnt;
27 #define DEBUGQUE(fmt,args...)
30 #define CANQUE_ROUNDROB 1
33 * canque_fifo_flush_slots - free all ready slots from the FIFO
34 * @fifo: pointer to the FIFO structure
36 * The caller should be prepared to handle situations, when some
37 * slots are held by input or output side slots processing.
38 * These slots cannot be flushed or their processing interrupted.
40 * Return Value: The nonzero value indicates, that queue has not been
41 * empty before the function call.
43 int canque_fifo_flush_slots(struct canque_fifo_t *fifo)
47 struct canque_slot_t *slot;
48 spin_lock_irqsave(&fifo->fifo_lock, flags);
50 *fifo->tail=fifo->flist;
53 fifo->tail=&fifo->head;
54 ret=canque_fifo_test_and_set_fl(fifo,EMPTY);
55 spin_unlock_irqrestore(&fifo->fifo_lock, flags);
61 * canque_fifo_init_slots - initialize one CAN FIFO
62 * @fifo: pointer to the FIFO structure
63 * @slotsnr: number of requested slots
65 * Return Value: The negative value indicates, that there is no memory
66 * to allocate space for the requested number of the slots.
68 int canque_fifo_init_slots(struct canque_fifo_t *fifo, int slotsnr)
71 struct canque_slot_t *slot;
72 if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
73 size=sizeof(struct canque_slot_t)*slotsnr;
74 fifo->entry=kmalloc(size,GFP_KERNEL);
75 if(!fifo->entry) return -1;
84 fifo->tail=&fifo->head;
85 canque_fifo_set_fl(fifo,EMPTY);
90 * canque_fifo_done - frees slots allocated for CAN FIFO
91 * @fifo: pointer to the FIFO structure
93 int canque_fifo_done(struct canque_fifo_t *fifo)
101 /* atomic_dec_and_test(&qedge->edge_used);
102 void atomic_inc(&qedge->edge_used);
103 list_add_tail(struct list_head *new, struct list_head *head)
104 list_for_each(edge,qends->inlist);
105 list_entry(ptr, type, member)
109 * canque_get_inslot - finds one outgoing edge and allocates slot from it
110 * @qends: ends structure belonging to calling communication object
111 * @qedgep: place to store pointer to found edge
112 * @slotp: place to store pointer to allocated slot
113 * @cmd: command type for slot
115 * Function looks for the first non-blocked outgoing edge in @qends structure
116 * and tries to allocate slot from it.
117 * Return Value: If there is no usable edge or there is no free slot in edge
118 * negative value is returned.
120 int canque_get_inslot(struct canque_ends_t *qends,
121 struct canque_edge_t **qedgep, struct canque_slot_t **slotp, int cmd)
125 struct canque_edge_t *edge;
127 spin_lock_irqsave(&qends->ends_lock, flags);
128 if(!list_empty(&qends->inlist)){
129 edge=list_entry(qends->inlist.next,struct canque_edge_t,inpeers);
130 if(!canque_fifo_test_fl(&edge->fifo,BLOCK)&&!canque_fifo_test_fl(&edge->fifo,DEAD)){
131 atomic_inc(&edge->edge_used);
132 spin_unlock_irqrestore(&qends->ends_lock, flags);
133 ret=canque_fifo_get_inslot(&edge->fifo, slotp, cmd);
136 DEBUGQUE("canque_get_inslot cmd=%d found edge %d\n",cmd,edge->edge_num);
140 spin_lock_irqsave(&qends->ends_lock, flags);
141 if(atomic_dec_and_test(&edge->edge_used))
142 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
145 spin_unlock_irqrestore(&qends->ends_lock, flags);
147 DEBUGQUE("canque_get_inslot cmd=%d failed\n",cmd);
152 * canque_get_inslot4id - finds best outgoing edge and slot for given ID
153 * @qends: ends structure belonging to calling communication object
154 * @qedgep: place to store pointer to found edge
155 * @slotp: place to store pointer to allocated slot
156 * @cmd: command type for slot
157 * @id: communication ID of message to send into edge
158 * @prio: optional priority of message
160 * Function looks for the non-blocked outgoing edge accepting messages
161 * with given ID. If edge is found, slot is allocated from that edge.
162 * The edges with non-zero mask are preferred over edges open to all messages.
163 * If more edges with mask accepts given message ID, the edge with
164 * highest priority below or equal to required priority is selected.
165 * Return Value: If there is no usable edge or there is no free slot in edge
166 * negative value is returned.
168 int canque_get_inslot4id(struct canque_ends_t *qends,
169 struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
170 int cmd, unsigned long id, int prio)
174 struct canque_edge_t *edge, *bestedge=NULL;
175 struct list_head *entry;
177 spin_lock_irqsave(&qends->ends_lock, flags);
178 list_for_each(entry,&qends->inlist){
179 edge=list_entry(entry,struct canque_edge_t,inpeers);
180 if(canque_fifo_test_fl(&edge->fifo,BLOCK)||canque_fifo_test_fl(&edge->fifo,DEAD))
182 if((id^edge->filtid)&edge->filtmask)
185 if(bestedge->filtmask){
186 if (!edge->filtmask) continue;
193 if(bestedge->edge_prio<edge->edge_prio){
194 if(edge->edge_prio>prio) continue;
196 if(bestedge->edge_prio<=prio) continue;
201 if((edge=bestedge)!=NULL){
202 atomic_inc(&edge->edge_used);
203 spin_unlock_irqrestore(&qends->ends_lock, flags);
204 ret=canque_fifo_get_inslot(&edge->fifo, slotp, cmd);
207 DEBUGQUE("canque_get_inslot4id cmd=%d id=%ld prio=%d found edge %d\n",cmd,id,prio,edge->edge_num);
210 spin_lock_irqsave(&qends->ends_lock, flags);
211 if(atomic_dec_and_test(&edge->edge_used))
212 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
214 spin_unlock_irqrestore(&qends->ends_lock, flags);
216 DEBUGQUE("canque_get_inslot4id cmd=%d id=%ld prio=%d failed\n",cmd,id,prio);
222 * canque_put_inslot - schedules filled slot for processing
223 * @qends: ends structure belonging to calling communication object
224 * @qedge: edge slot belong to
225 * @slot: pointer to the prepared slot
227 * Puts slot previously acquired by canque_get_inslot() or canque_get_inslot4id()
228 * function call into FIFO queue and activates edge processing if needed.
229 * Return Value: Positive value informs, that activation of output end
232 int canque_put_inslot(struct canque_ends_t *qends,
233 struct canque_edge_t *qedge, struct canque_slot_t *slot)
237 ret=canque_fifo_put_inslot(&qedge->fifo,slot);
239 canque_activate_edge(qends,qedge);
240 canque_notify_outends(qedge,CANQUEUE_NOTIFY_PROC);
242 spin_lock_irqsave(&qends->ends_lock, flags);
243 if(atomic_dec_and_test(&qedge->edge_used))
244 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
245 spin_unlock_irqrestore(&qends->ends_lock, flags);
246 DEBUGQUE("canque_put_inslot for edge %d returned %d\n",qedge->edge_num,ret);
251 * canque_abort_inslot - aborts preparation of the message in the slot
252 * @qends: ends structure belonging to calling communication object
253 * @qedge: edge slot belong to
254 * @slot: pointer to the previously allocated slot
256 * Frees slot previously acquired by canque_get_inslot() or canque_get_inslot4id()
257 * function call. Used when message copying into slot fails.
258 * Return Value: Positive value informs, that queue full state has been negated.
260 int canque_abort_inslot(struct canque_ends_t *qends,
261 struct canque_edge_t *qedge, struct canque_slot_t *slot)
265 ret=canque_fifo_abort_inslot(&qedge->fifo,slot);
267 canque_notify_outends(qedge,CANQUEUE_NOTIFY_SPACE);
269 spin_lock_irqsave(&qends->ends_lock, flags);
270 if(atomic_dec_and_test(&qedge->edge_used))
271 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
272 spin_unlock_irqrestore(&qends->ends_lock, flags);
273 DEBUGQUE("canque_abort_inslot for edge %d returned %d\n",qedge->edge_num,ret);
278 * canque_filter_msg2edges - sends message into all edges which accept its ID
279 * @qends: ends structure belonging to calling communication object
280 * @msg: pointer to CAN message
282 * Sends message to all outgoing edges connected to the given ends, which accepts
283 * message communication ID.
284 * Return Value: Returns number of edges message has been send to
286 int canque_filter_msg2edges(struct canque_ends_t *qends, struct canmsg_t *msg)
291 struct canque_edge_t *edge;
292 struct list_head *entry;
293 struct canque_slot_t *slot;
295 DEBUGQUE("canque_filter_msg2edges for msg ID %ld\n",msg->id);
296 spin_lock_irqsave(&qends->ends_lock, flags);
297 list_for_each(entry,&qends->inlist){
298 edge=list_entry(entry,struct canque_edge_t,inpeers);
299 if(canque_fifo_test_fl(&edge->fifo,BLOCK)||canque_fifo_test_fl(&edge->fifo,DEAD))
301 /* FIXME: the next comparison should be outside of ends lock */
302 if((msg->id^edge->filtid)&edge->filtmask)
304 atomic_inc(&edge->edge_used);
305 spin_unlock_irqrestore(&qends->ends_lock, flags);
306 ret=canque_fifo_get_inslot(&edge->fifo, &slot, 0);
310 ret=canque_fifo_put_inslot(&edge->fifo,slot);
312 canque_activate_edge(qends,edge);
313 canque_notify_outends(edge,CANQUEUE_NOTIFY_PROC);
317 spin_lock_irqsave(&qends->ends_lock, flags);
318 if(atomic_dec_and_test(&edge->edge_used))
319 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
321 spin_unlock_irqrestore(&qends->ends_lock, flags);
322 DEBUGQUE("canque_filter_msg2edges sent msg ID %ld to %d edges\n",msg->id,destnr);
327 * canque_test_outslot - test and retrieve ready slot for given ends
328 * @qends: ends structure belonging to calling communication object
329 * @qedgep: place to store pointer to found edge
330 * @slotp: place to store pointer to received slot
332 * Function takes highest priority active incoming edge and retrieves
333 * oldest ready slot from it.
334 * Return Value: Negative value informs, that there is no ready output
335 * slot for given ends. Positive value is equal to the command
336 * slot has been allocated by the input side.
338 int canque_test_outslot(struct canque_ends_t *qends,
339 struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
343 struct canque_edge_t *edge;
345 spin_lock_irqsave(&qends->ends_lock, flags);
346 for(prio=CANQUEUE_PRIO_NR;--prio>=0;){
347 if(!list_empty(&qends->active[prio])){
348 edge=list_entry(qends->active[prio].next,struct canque_edge_t,outpeers);
349 atomic_inc(&edge->edge_used);
350 spin_unlock_irqrestore(&qends->ends_lock, flags);
352 DEBUGQUE("canque_test_outslot found edge %d\n",edge->edge_num);
353 return canque_fifo_test_outslot(&edge->fifo, slotp);
356 spin_unlock_irqrestore(&qends->ends_lock, flags);
358 DEBUGQUE("canque_test_outslot no ready slot\n");
363 * canque_free_outslot - frees processed output slot
364 * @qends: ends structure belonging to calling communication object
365 * @qedge: edge slot belong to
366 * @slot: pointer to the processed slot
368 * Function releases processed slot previously acquired by canque_test_outslot()
370 * Return Value: Return value informs if input side has been notified
371 * to know about change of edge state
373 int canque_free_outslot(struct canque_ends_t *qends,
374 struct canque_edge_t *qedge, struct canque_slot_t *slot)
378 ret=canque_fifo_free_outslot(&qedge->fifo, slot);
379 if(ret&CAN_FIFOF_EMPTY){
380 canque_notify_inends(qedge,CANQUEUE_NOTIFY_EMPTY);
382 if(ret&CAN_FIFOF_FULL)
383 canque_notify_inends(qedge,CANQUEUE_NOTIFY_SPACE);
384 spin_lock_irqsave(&qends->ends_lock, flags);
385 if((ret&CAN_FIFOF_EMPTY) || CANQUE_ROUNDROB){
386 spin_lock(&qedge->fifo.fifo_lock);
387 if(canque_fifo_test_fl(&qedge->fifo,EMPTY)){
388 list_del(&qedge->outpeers);
389 list_add(&qedge->outpeers,&qends->idle);
393 list_del(&qedge->outpeers);
394 list_add_tail(&qedge->outpeers,&qends->active[qedge->edge_prio]);
396 #endif /*CANQUE_ROUNDROB*/
397 spin_unlock(&qedge->fifo.fifo_lock);
399 if(atomic_dec_and_test(&qedge->edge_used))
400 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
401 spin_unlock_irqrestore(&qends->ends_lock, flags);
402 DEBUGQUE("canque_free_outslot for edge %d returned %d\n",qedge->edge_num,ret);
407 * canque_again_outslot - reschedule output slot to process it again later
408 * @qends: ends structure belonging to calling communication object
409 * @qedge: edge slot belong to
410 * @slot: pointer to the slot for re-processing
412 * Function reschedules slot previously acquired by canque_test_outslot()
413 * function call for second time processing.
414 * Return Value: Function cannot fail.
416 int canque_again_outslot(struct canque_ends_t *qends,
417 struct canque_edge_t *qedge, struct canque_slot_t *slot)
421 ret=canque_fifo_again_outslot(&qedge->fifo, slot);
422 spin_lock_irqsave(&qends->ends_lock, flags);
423 if(atomic_dec_and_test(&qedge->edge_used))
424 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
425 spin_unlock_irqrestore(&qends->ends_lock, flags);
426 DEBUGQUE("canque_again_outslot for edge %d returned %d\n",qedge->edge_num,ret);
431 * canque_set_filt - sets filter for specified edge
432 * @qedge: pointer to the edge
433 * @filtid: ID to set for the edge
434 * @filtmask: mask used for ID match check
436 * Return Value: Negative value is returned if edge is in the process of delete.
438 int canque_set_filt(struct canque_edge_t *qedge,
439 unsigned long filtid, unsigned long filtmask)
444 spin_lock_irqsave(&qedge->fifo.fifo_lock,flags);
445 atomic_inc(&qedge->edge_used);
446 qedge->filtid=filtid;
447 qedge->filtmask=filtmask;
448 if(canque_fifo_test_fl(&qedge->fifo,DEAD)) ret=-1;
449 else ret=canque_fifo_test_and_set_fl(&qedge->fifo,BLOCK)?1:0;
451 spin_unlock_irqrestore(&qedge->fifo.fifo_lock,flags);
453 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_FILTCH);
455 spin_lock_irqsave(&qedge->fifo.fifo_lock,flags);
456 if(!ret)canque_fifo_clear_fl(&qedge->fifo,BLOCK);
457 if(atomic_dec_and_test(&qedge->edge_used))
458 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
459 spin_unlock_irqrestore(&qedge->fifo.fifo_lock,flags);
461 DEBUGQUE("canque_set_filt for edge %d, ID %ld and mask %ld returned %d\n",qedge->edge_num,filtid,filtmask,ret);
466 * canque_flush - fluesh all ready slots in the edge
467 * @qedge: pointer to the edge
469 * Tries to flush all allocated slots from the edge, but there could
470 * exist some slots associated to edge which are processed by input
471 * or output side and cannot be flushed at this moment.
472 * Return Value: The nonzero value indicates, that queue has not been
473 * empty before the function call.
475 int canque_flush(struct canque_edge_t *qedge)
480 atomic_inc(&qedge->edge_used);
481 ret=canque_fifo_flush_slots(&qedge->fifo);
483 canque_notify_inends(qedge,CANQUEUE_NOTIFY_EMPTY);
484 canque_notify_inends(qedge,CANQUEUE_NOTIFY_SPACE);
485 spin_lock_irqsave(&qedge->outends->ends_lock, flags);
486 spin_lock(&qedge->fifo.fifo_lock);
487 if(canque_fifo_test_fl(&qedge->fifo,EMPTY)){
488 list_del(&qedge->outpeers);
489 list_add(&qedge->outpeers,&qedge->outends->idle);
491 if(atomic_dec_and_test(&qedge->edge_used))
492 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
493 spin_unlock(&qedge->fifo.fifo_lock);
494 spin_unlock_irqrestore(&qedge->outends->ends_lock, flags);
496 DEBUGQUE("canque_flush for edge %d returned %d\n",qedge->edge_num,ret);
501 * canqueue_ends_init_gen - subsystem independent routine to initialize ends state
502 * @qends: pointer to the ends structure
504 * Return Value: Cannot fail.
506 int canqueue_ends_init_gen(struct canque_ends_t *qends)
509 for(i=CANQUEUE_PRIO_NR;--i>=0;){
510 INIT_LIST_HEAD(&qends->active[i]);
512 INIT_LIST_HEAD(&qends->idle);
513 INIT_LIST_HEAD(&qends->inlist);
514 spin_lock_init(&qends->ends_lock);
520 * canqueue_notify_kern - notification callback handler for Linux userspace clients
521 * @qends: pointer to the callback side ends structure
522 * @qedge: edge which invoked notification
523 * @what: notification type
525 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
527 DEBUGQUE("canqueue_notify_kern for edge %d and event %d\n",qedge->edge_num,what);
529 case CANQUEUE_NOTIFY_EMPTY:
530 wake_up_interruptible(&qends->endinfo.fileinfo.emptyq);
532 case CANQUEUE_NOTIFY_SPACE:
533 wake_up_interruptible(&qends->endinfo.fileinfo.writeq);
535 case CANQUEUE_NOTIFY_PROC:
536 wake_up_interruptible(&qends->endinfo.fileinfo.readq);
538 case CANQUEUE_NOTIFY_NOUSR:
539 wake_up_interruptible(&qends->endinfo.fileinfo.readq);
540 wake_up_interruptible(&qends->endinfo.fileinfo.writeq);
541 wake_up_interruptible(&qends->endinfo.fileinfo.emptyq);
543 case CANQUEUE_NOTIFY_DEAD:
544 if(atomic_read(&qedge->edge_used)>0)
545 atomic_dec(&qedge->edge_used);
547 case CANQUEUE_NOTIFY_ATACH:
548 atomic_inc(&qedge->edge_used);
554 * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
555 * @qends: pointer to the callback side ends structure
557 int canqueue_ends_init_kern(struct canque_ends_t *qends)
559 canqueue_ends_init_gen(qends);
561 init_waitqueue_head(&qends->endinfo.fileinfo.readq);
562 init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
563 init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
564 qends->notify=canqueue_notify_kern;
565 DEBUGQUE("canqueue_ends_init_kern\n");
571 * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
572 * @qends: ends structure belonging to calling communication object
573 * @qedgep: place to store pointer to found edge
574 * @slotp: place to store pointer to allocated slot
575 * @cmd: command type for slot
576 * @id: communication ID of message to send into edge
577 * @prio: optional priority of message
579 * Same as canque_get_inslot4id(), except, that it waits for free slot
580 * in case, that queue is full. Function is specific for Linux userspace clients.
581 * Return Value: If there is no usable edge negative value is returned.
583 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
584 struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
585 int cmd, unsigned long id, int prio)
588 DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
589 wait_event_interruptible((qends->endinfo.fileinfo.writeq),
590 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
595 * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
596 * @qends: ends structure belonging to calling communication object
597 * @qedgep: place to store pointer to found edge
598 * @slotp: place to store pointer to received slot
600 * The same as canque_test_outslot(), except it waits in the case, that there is
601 * no ready slot for given ends. Function is specific for Linux userspace clients.
602 * Return Value: Negative value informs, that there is no ready output
603 * slot for given ends. Positive value is equal to the command
604 * slot has been allocated by the input side.
606 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
607 struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
610 DEBUGQUE("canque_get_outslot_wait_kern\n");
611 wait_event_interruptible((qends->endinfo.fileinfo.readq),
612 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
617 * canque_sync_wait_kern - wait for all slots processing
618 * @qends: ends structure belonging to calling communication object
619 * @qedge: pointer to edge
621 * Functions waits for ends transition into empty state.
622 * Return Value: Positive value indicates, that edge empty state has been reached.
623 * Negative or zero value informs about interrupted wait or other problem.
625 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
628 DEBUGQUE("canque_sync_wait_kern\n");
629 wait_event_interruptible((qends->endinfo.fileinfo.emptyq),
630 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
636 * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
637 * @slotsnr: required number of slots in the newly allocated edge structure
639 * Return Value: Returns pointer to allocated slot structure or %NULL if
640 * there is not enough memory to process operation.
642 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
644 struct canque_edge_t *qedge;
645 qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
646 if(qedge == NULL) return NULL;
648 memset(qedge,0,sizeof(struct canque_edge_t));
649 spin_lock_init(&qedge->fifo.fifo_lock);
650 if(canque_fifo_init_slots(&qedge->fifo, slotsnr)<0){
652 DEBUGQUE("canque_new_edge_kern failed\n");
655 atomic_set(&qedge->edge_used,0);
658 qedge->edge_prio = 0;
660 /* not exactly clean, but enough for debugging */
661 atomic_inc(&edge_num_cnt);
662 qedge->edge_num=atomic_read(&edge_num_cnt);
663 #endif /* CAN_DEBUG */
664 DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
669 * canqueue_connect_edge - connect edge between two communication entities
670 * @qedge: pointer to edge
671 * @inends: pointer to ends the input of the edge should be connected to
672 * @outends: pointer to ends the output of the edge should be connected to
674 * Return Value: Negative value informs about failed operation.
676 int canqueue_connect_edge(struct canque_edge_t *qedge, struct canque_ends_t *inends, struct canque_ends_t *outends)
679 if(qedge == NULL) return -1;
680 DEBUGQUE("canqueue_connect_edge %d\n",qedge->edge_num);
681 atomic_inc(&qedge->edge_used);
682 spin_lock_irqsave(&inends->ends_lock, flags);
683 spin_lock(&outends->ends_lock);
684 spin_lock(&qedge->fifo.fifo_lock);
685 qedge->inends=inends;
686 list_add(&qedge->inpeers,&inends->inlist);
687 qedge->outends=outends;
688 list_add(&qedge->outpeers,&outends->idle);
689 spin_unlock(&qedge->fifo.fifo_lock);
690 spin_unlock(&outends->ends_lock);
691 spin_unlock_irqrestore(&inends->ends_lock, flags);
692 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_ATACH);
694 spin_lock_irqsave(&qedge->fifo.fifo_lock, flags);
695 if(atomic_dec_and_test(&qedge->edge_used))
696 canque_notify_bothends(qedge,CANQUEUE_NOTIFY_NOUSR);
697 spin_unlock_irqrestore(&qedge->fifo.fifo_lock, flags);
702 * canqueue_disconnect_edge - disconnect edge from communicating entities
703 * @qedge: pointer to edge
705 * Return Value: Negative value means, that edge is used and cannot
706 * be disconnected. Operation has to be delayed.
708 int canqueue_disconnect_edge(struct canque_edge_t *qedge)
712 struct canque_ends_t *inends, *outends;
714 inends=qedge->inends;
715 if(inends) spin_lock_irqsave(&inends->ends_lock,flags);
716 outends=qedge->outends;
717 if(outends) spin_lock(&outends->ends_lock);
718 spin_lock(&qedge->fifo.fifo_lock);
719 if(atomic_read(&qedge->edge_used)==0) {
721 list_del(&qedge->outpeers);
725 list_del(&qedge->inpeers);
730 spin_unlock(&qedge->fifo.fifo_lock);
731 if(outends) spin_unlock(&outends->ends_lock);
732 if(inends) spin_unlock_irqrestore(&inends->ends_lock,flags);
733 DEBUGQUE("canqueue_disconnect_edge %d returned %d\n",qedge->edge_num,ret);
738 * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
739 * @qends: ends structure belonging to calling communication object
740 * @qedge: pointer to edge
742 * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
744 * Return Value: Negative value means, that edge is used and cannot
745 * be disconnected yet. Operation has to be delayed.
747 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
749 canque_fifo_set_fl(&qedge->fifo,BLOCK);
750 DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
751 if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
752 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
753 if(atomic_read(&qedge->edge_used)>0)
754 atomic_dec(&qedge->edge_used);
755 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
756 wait_event_interruptible((qends->endinfo.fileinfo.emptyq),
757 (canqueue_disconnect_edge(qedge)>=0));
760 DEBUGQUE("canqueue_disconnect_edge_kern failed\n");
766 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
768 struct canque_edge_t *edge;
771 spin_lock_irqsave(&qends->ends_lock,flags);
772 if(list_empty(list)){
773 spin_unlock_irqrestore(&qends->ends_lock,flags);
776 if(list == &qends->inlist)
777 edge=list_entry(list->next,struct canque_edge_t,inpeers);
779 edge=list_entry(list->next,struct canque_edge_t,outpeers);
780 atomic_inc(&edge->edge_used);
781 spin_unlock_irqrestore(&qends->ends_lock,flags);
782 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
783 /* Free edge memory */
784 canque_fifo_done(&edge->fifo);
787 DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
788 DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
794 void canqueue_block_list(struct canque_ends_t *qends, struct list_head *list)
796 struct canque_edge_t *edge;
797 struct list_head *entry;
799 /* has to be called with qends->ends_lock already locked */
800 list_for_each(entry,&qends->inlist){
801 if(list == &qends->inlist)
802 edge=list_entry(list->next,struct canque_edge_t,inpeers);
804 edge=list_entry(list->next,struct canque_edge_t,outpeers);
805 canque_fifo_set_fl(&edge->fifo,BLOCK);
811 * canqueue_ends_done_kern - finalizing of the ends structure for Linux kernel clients
812 * @qends: pointer to ends structure
813 * @sync: flag indicating, that user wants to wait for processing of all remaining
816 * Return Value: Function should be designed such way to not fail.
818 int canqueue_ends_done_kern(struct canque_ends_t *qends, int sync)
823 DEBUGQUE("canqueue_ends_done_kern\n");
824 spin_lock_irqsave(&qends->ends_lock,flags);
825 canqueue_block_list(qends, &qends->idle);
826 for(i=CANQUEUE_PRIO_NR;--i>=0;){
827 canqueue_block_list(qends, &qends->active[i]);
829 canqueue_block_list(qends, &qends->idle);
830 canqueue_block_list(qends, &qends->inlist);
831 spin_unlock_irqrestore(&qends->ends_lock,flags);
833 for(i=CANQUEUE_PRIO_NR;--i>=0;){
834 canqueue_disconnect_list_kern(qends, &qends->active[i]);
836 canqueue_disconnect_list_kern(qends, &qends->idle);
837 canqueue_disconnect_list_kern(qends, &qends->inlist);
839 wake_up_interruptible(&qends->endinfo.fileinfo.readq);
840 wake_up_interruptible(&qends->endinfo.fileinfo.writeq);
841 wake_up_interruptible(&qends->endinfo.fileinfo.emptyq);