]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan.c
renamings... redo the request and messages part... also now there will be two threads...
[frescor/fna.git] / src_frescan / frescan.c
1 /*!
2  * @file frescan.c
3  *
4  * @brief the FRESCAN protocol
5  *
6  * @version 0.01
7  *
8  * @date 20-Feb-2008
9  *
10  * @author
11  *      Daniel Sangorrin
12  *
13  * @comments
14  *
15  * This file contains the implementation of the FRESCAN protocol
16  *
17  * @license
18  *
19  * See MaRTE OS license
20  *
21  */
22
23 #include <sys/marte_configuration_parameters.h> // PATH_MX
24 #include <fcntl.h>  // open
25 #include <unistd.h> // ioctl
26 #include <stdlib.h> // malloc
27 #include <string.h> // memcpy
28
29 #include <drivers/can.h>       // can_chip_t, can_frame_t
30 #include "frescan.h"           // frescan_init_params_t, frescan_send_params_t
31 #include "frescan_queues.h"    // init, enqueue, requeue
32 #include "frescan_data.h"      // init, the_networks
33 #include "frescan_servers.h"   // init, frame_sent
34 #include "frescan_debug.h"     // DEBUG
35 #include "frescan_id.h"        // frescan_id_set_field, frescan_id_get_field
36 #include "frescan_hw_buffer.h" // frescan_hw_buffer_update
37 #include "frescan_bwres_robjs.h"     // frescan_replyobjects_init
38 #include "frescan_servers_replenishments.h" // frescan_replenishments_xxx
39 #include "frescan_packets.h"
40
41 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
42                                     struct can_frame_t *frame);
43
44 static int frescan_hook_frame_sent(const struct can_chip_t *chip);
45
46 static int frescan_hook_frame_aborted(const struct can_chip_t *chip);
47
48 /**
49  * frescan_init - initializes the network and the internal structures
50  *
51  * @params: the initialization parameters
52  *
53  * This function initializes the frescan network protocol.
54  *
55  * First it opens and configures the corresponding CAN chip device. For the
56  * CAN chip acceptance filter we use a dual filter configuration. The first
57  * filter is to set my local address address and the second one is to allow
58  * broadcast messages.
59  *
60  * Once the CAN chip is configured we call the initialization functions of
61  * the rest of modules of frescan.
62  *
63  */
64
65 int frescan_init(frescan_init_params_t *params)
66 {
67         int fd, ret;
68         char can_path[PATH_MX];
69         struct ioctl_filters_t ioctl_filters;
70         struct can_filter_t filters[2];
71
72         snprintf(can_path, PATH_MX, "/dev/can%u", params->net);
73
74         DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "open %s\n", can_path);
75
76         fd = open (can_path, O_RDWR);
77         if (fd == -1) {
78                 ERROR ("could not open /dev/can%u\n", params->net);
79                 return -1;
80         }
81
82         DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set acceptance filters\n");
83
84         filters[0].mask = 0xFFFFFFFF;
85         frescan_id_set_field(&filters[0].mask, FRESCAN_FIELD_DEST,0x00);
86         filters[0].code = 0;
87         frescan_id_set_field(&filters[0].code, FRESCAN_FIELD_DEST,params->node);
88
89         filters[1].mask = filters[0].mask;
90         filters[1].code = 0;
91         frescan_id_set_field(&filters[1].code,
92                              FRESCAN_FIELD_DEST,
93                              FRESCAN_BROADCAST_ADDR);
94
95         ioctl_filters.filters = filters;
96         ioctl_filters.len = 2;
97
98         ret = ioctl(fd, CAN_IOCTL_SET_FILTERS, &ioctl_filters);
99         if (ret == -1) {
100                 ERROR ("ioctl CAN_IOCTL_SET_FILTERS failed /dev/can%u\n",
101                        params->net);
102                 return -1;
103         }
104
105         DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "set tx, rx, abort hooks\n");
106
107         ret = ioctl(fd, CAN_IOCTL_SET_TX_HOOK, frescan_hook_frame_sent);
108         if (ret == -1) {
109                 ERROR ("ioctl CAN_IOCTL_SET_TX_HOOK failed /dev/can%u\n",
110                        params->net);
111                 return -1;
112         }
113
114         ret = ioctl(fd, CAN_IOCTL_SET_RX_HOOK, frescan_hook_frame_recv);
115         if (ret == -1) {
116                 ERROR ("ioctl CAN_IOCTL_SET_RX_HOOK failed /dev/can%u\n",
117                        params->net);
118                 return -1;
119         }
120
121         ret = ioctl(fd, CAN_IOCTL_SET_AB_HOOK, frescan_hook_frame_aborted);
122         if (ret == -1) {
123                 ERROR ("ioctl CAN_IOCTL_SET_AB_HOOK failed /dev/can%u\n",
124                        params->net);
125                 return -1;
126         }
127
128         DEBUG(FRESCAN_INIT_ENABLE_DEBUG, "init the rest of modules\n");
129
130         ret = frescan_data_init(fd, params);
131         if (ret != 0) {
132                 ERROR("could not initialize the global data\n");
133                 return -1;
134         }
135
136         ret = frescan_packets_init();
137         if (ret != 0) {
138                 ERROR("could not initialize the packets pool\n");
139                 return -1;
140         }
141
142         ret = frescan_queues_init(&the_networks[params->net].queues, params);
143         if (ret != 0) {
144                 ERROR("could not initialize the queues\n");
145                 return -1;
146         }
147
148         ret = frescan_servers_init(params->net);
149         if (ret != 0) {
150                 ERROR("could not initialize the servers\n");
151                 return -1;
152         }
153
154         ret = frescan_replenishments_init(params->net);
155         if (ret != 0) {
156                 ERROR("could not initialize the replenishments\n");
157                 return -1;
158         }
159
160         return 0;
161 }
162
163 /**
164  * frescan_send - send a message
165  *
166  * @params: the parameters needed by the protocol to send the message
167  * @msg: the message buffer
168  * @size: the size of the message
169  *
170  * This is one of the main functions of the protocol and it provides the
171  * means to send a message through the protocol stack.
172  */
173
174 int frescan_send(const frescan_send_params_t *params,
175                  const uint8_t *msg,
176                  const size_t size)
177 {
178         int ret;
179         frescan_packet_t *packet;
180         frescan_prio_queue_t *pqueue;
181
182         DEBUG(FRESCAN_SEND_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
183               "checking arguments (msg size=%d)\n", size);
184
185         if ((params == NULL) || (msg == NULL) || (size == 0)) {
186                 ERROR("arguments are not ok\n");
187                 return -1;
188         }
189
190         DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a packet\n");
191
192         FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
193         packet = frescan_packets_alloc();
194         FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
195
196         if (packet == NULL) {
197                 ERROR("could not allocate packet\n");
198                 return -1;
199         }
200         packet->flags = params->flags; // set the flags (to remember them)
201
202         DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "allocating a frame\n");
203
204         FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
205         packet->frame = can_framespool_alloc();
206         FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
207
208         if (packet->frame == NULL) {
209                 ERROR("could not allocate frame\n");
210                 return -1;
211         }
212
213         DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set values for the frame header\n");
214         packet->frame->is_extended_format = true;
215         packet->frame->is_rtr = false;
216
217         if (params->flags & FRESCAN_FP) {
218                 // NOTE: frag id for fp is: FRESCAN_MX_IDS, so the servers can
219                 // have IDs in the range (0 .. FRESCAN_MX_IDS-1)
220                 frescan_id_set_field(&packet->frame->id,
221                                      FRESCAN_FIELD_FRAG_ID,
222                                      (uint32_t)FRESCAN_MX_IDS);
223
224                 frescan_id_set_field(&packet->frame->id,
225                                       FRESCAN_FIELD_PRIO,
226                                       (uint32_t)params->prio);
227         } else {
228                 // NOTE: the priority is put when the packet is dequeued
229                 // and it is the priority of th server
230                 frescan_id_set_field(&packet->frame->id,
231                                      FRESCAN_FIELD_FRAG_ID,
232                                      (uint32_t)params->ss);
233         }
234
235         frescan_id_set_field(&packet->frame->id,
236                              FRESCAN_FIELD_DEST,
237                              (uint32_t)params->to);
238
239         frescan_id_set_field(&packet->frame->id,
240                              FRESCAN_FIELD_SRC,
241                              (uint32_t)the_networks[params->net].local_node);
242
243         frescan_id_set_field(&packet->frame->id,
244                               FRESCAN_FIELD_CHAN,
245                               (uint32_t)params->channel);
246
247         DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "set the packet data bytes\n");
248         if (params->flags & FRESCAN_ASYNC) {
249                 // allocate a buffer and copy the data
250                 // NOTE: instead of this we could use a chain of frames but
251                 // i think it would be inefficient since each one can only
252                 // hold 8 user bytes and we need to write its headers.
253                 packet->buffer_head = (uint8_t *)malloc(size*sizeof(uint8_t));
254                 memcpy(packet->buffer_head, msg, size);
255         } else {
256                 packet->buffer_head = (uint8_t *)msg;
257         }
258
259         packet->buffer_read_pointer = packet->buffer_head;
260         packet->buffer_pending_bytes = size;
261         pqueue = the_networks[params->net].queues.tx_fp_queue;
262
263         DEBUG(FRESCAN_SEND_ENABLE_DEBUG, "enqueue the packet\n");
264         if (packet->flags & FRESCAN_FP) {
265                 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
266                 ret = frescan_pqueue_enqueue(pqueue, packet, params->prio);
267                 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
268
269                 if (ret != 0) {
270                         ERROR("could not enqueue the packet\n");
271                         return -1;
272                 }
273         } else {
274                 FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
275                 ret = frescan_servers_enqueue(params->net, params->ss, packet);
276                 FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
277
278                 if (ret != 0) {
279                         ERROR("could not enqueue the packet\n");
280                         return -1;
281                 }
282         }
283
284         FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
285         ret = frescan_hw_buffer_update(params->net);
286         FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
287
288         if (ret != 0) {
289                 ERROR("could not update hw buffer\n");
290                 return -1;
291         }
292
293         return 0;
294 }
295
296 /**
297  * frescan_recv - receive a message
298  *
299  * @params: the parameters needed by the protocol to receive the message
300  * @msg: the message buffer
301  * @size: the size of the message buffer
302  * @recv_bytes: the number of bytes received
303  * @from: the node that sent the message
304  * @prio: the priority of the message
305  *
306  * This is one of the main functions of the protocol and it provides the
307  * means to receive a message through the protocol stack.
308  */
309
310 int frescan_recv(const frescan_recv_params_t *params,
311                  uint8_t *msg,
312                  const size_t size,
313                  size_t *recv_bytes,
314                  frescan_node_t *from,
315                  frescan_prio_t *prio)
316 {
317         int ret;
318         frescan_prio_queue_t *pqueue;
319         bool blocking;
320         frescan_packet_t *head, *packet;
321
322         if (params->flags & FRESCAN_SYNC) {
323                 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
324                       "receive a packet in blocking mode\n");
325                 blocking = true;
326         } else {
327                 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
328                       "receive a packet in non-blocking mode\n");
329                 blocking = false;
330         }
331
332         pqueue = the_networks[params->net].queues.rx_channel_queues
333                                                              [params->channel];
334
335         ret = frescan_pqueue_dequeue(pqueue, &head, prio, blocking);
336         if (ret != 0) {
337                 ERROR ("could not dequeue packet\n");
338                 return -1;
339         }
340
341         if (head == NULL) {
342                 if (blocking == false) {
343                         DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
344                               "blocking false, no packets\n");
345                         *recv_bytes = 0;
346                         return 0;
347                 } else {
348                         ERROR ("blocking true, and packet = null\n");
349                         return -1;
350                 }
351         }
352
353         DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
354               "traverse the list of packets for this message\n");
355
356         *recv_bytes = 0;
357
358         FRESCAN_ACQUIRE_LOCK(&the_networks[params->net].lock);
359
360         list_for_each_entry(packet, &head->msg_list, msg_list) {
361                 // TODO: memory checks, delete the packets
362                 memcpy(msg + *recv_bytes,
363                        packet->frame->data,
364                        packet->frame->dlc);
365                 *recv_bytes += packet->frame->dlc;
366
367                 *from = (frescan_node_t)frescan_id_get_field(packet->frame->id,
368                                                              FRESCAN_FIELD_SRC);
369                 DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
370                       "ID Packet, dlc: %u, frame pool pos: %u, from:%u\n",
371                       packet->frame->dlc, packet->frame->pool_pos, *from);
372
373                 ret = can_framespool_free(packet->frame);
374                 if (ret != 0) {
375                         ERROR("could not free frame\n");
376                         return -1;
377                 }
378
379                 ret = frescan_packets_free(packet);
380                 if (ret != 0) {
381                         ERROR("could not free packet\n");
382                         return -1;
383                 }
384         }
385
386         ret = frescan_packets_free(head);
387
388         FRESCAN_RELEASE_LOCK(&the_networks[params->net].lock);
389
390         if (ret != 0) {
391                 ERROR("could not free head packet\n");
392                 return -1;
393         }
394
395         DEBUG(FRESCAN_RECV_ENABLE_DEBUG,
396               "received bytes: %u\n", *recv_bytes);
397
398         return 0;
399 }
400
401
402 /**
403  * frescan_hook_frame_recv - frame received hook
404  *
405  * This function will be called by the CAN driver's IRQ handler when a frame
406  * is received so we can store it in an appropiate queue.
407  *
408  * NOTE: in the future it could consist simply of signaling a semaphore to
409  * let a bottom half thread do the hard work.
410  */
411
412
413 static int frescan_hook_frame_recv (const struct can_chip_t *chip,
414                                     struct can_frame_t *frame)
415 {
416         int i, ret;
417         uint32_t prio, dest, src, channel, frag_id, frag_flag;
418         frescan_packet_t *packet, *head;
419         frescan_prio_queue_t *pqueue;
420         int net;
421
422         net = chip->minor;
423
424         DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG, "received a frame, net=%d\n", net);
425         DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
426               "%s %s, id=0x%X, dlc=%u, pool:%u\n",
427               (frame->is_extended_format) ? "Ext" : "Stnd",
428               (frame->is_rtr) ? "RTR Frame" : "DATA Frame",
429               frame->id,
430               frame->dlc,
431               frame->pool_pos);
432
433         for (i=0; i<frame->dlc; i++) {
434                 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
435                       "data[%d] = 0x%X;\n", i, frame->data[i]);
436         }
437
438         prio      = frescan_id_get_field(frame->id, FRESCAN_FIELD_PRIO);
439         dest      = frescan_id_get_field(frame->id, FRESCAN_FIELD_DEST);
440         src       = frescan_id_get_field(frame->id, FRESCAN_FIELD_SRC);
441         channel   = frescan_id_get_field(frame->id, FRESCAN_FIELD_CHAN);
442         frag_id   = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_ID);
443         frag_flag = frescan_id_get_field(frame->id, FRESCAN_FIELD_FRAG_FLAG);
444
445         DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
446               "prio:%u dest:%u src:%u chan:%u id:%u flag:%u\n",
447               prio, dest, src, channel, frag_id, frag_flag);
448
449         DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG,
450               "enqueue the packet in ID queue\n");
451         packet = frescan_packets_alloc();
452         packet->frame = frame;
453
454         if (frag_id == 0) {
455                 head = the_networks[net].id_fp_queues[prio];
456         } else {
457                 head = the_networks[net].id_queues[frag_id];
458         }
459
460         if (head == NULL) {
461                 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
462                                 FRESCAN_FRAG_ENABLE_DEBUG,
463                 "allocate head for id=%u\n", frag_id);
464                 head = frescan_packets_alloc();
465                 INIT_LIST_HEAD(&head->msg_list);
466
467                 if (frag_id == 0) {
468                         the_networks[net].id_fp_queues[prio] = head;
469                 } else {
470                         the_networks[net].id_queues[frag_id] = head;
471                 }
472         }
473
474         list_add_tail(&packet->msg_list, &head->msg_list);
475
476         if (frag_flag == false) {
477                 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
478                                 FRESCAN_FRAG_ENABLE_DEBUG,
479                 "message complete, move msg to channel\n");
480                 // TODO: select the highest priority??
481                 pqueue = the_networks[net].queues.rx_channel_queues[channel];
482                 ret = frescan_pqueue_enqueue(pqueue, head, prio);
483                 if (ret != 0) {
484                         ERROR("could not enqueue message in channel queue\n");
485                         return -1;
486                 }
487
488                 if (frag_id == 0) {
489                         the_networks[net].id_fp_queues[prio] = NULL;
490                 } else {
491                         the_networks[net].id_queues[frag_id] = NULL;
492                 }
493
494         } else {
495                 DEBUG(FRESCAN_RX_HOOK_ENABLE_DEBUG ||
496                                 FRESCAN_FRAG_ENABLE_DEBUG,
497                 "message not complete, wait for more fragments\n");
498         }
499
500         // NOTE: don't forget to free the frame and the packet when it is
501         // read by the user application
502
503         return 0;
504 };
505
506 /**
507  * frescan_hook_frame_sent - frame sent hook
508  *
509  * This function will be called by the CAN driver's IRQ handler when a frame
510  * is sent through the CAN bus so we can enqueue another one, signal a
511  * semaphore, consume sporadic server capacity...
512  */
513
514 static int frescan_hook_frame_sent(const struct can_chip_t *chip)
515 {
516         int ret;
517         frescan_packet_t *packet;
518         frescan_prio_queue_t *pqueue;
519         frescan_prio_t prio;
520         frescan_ss_t id;
521
522         packet = the_networks[chip->minor].last_packet;
523
524         id = frescan_id_get_field(packet->frame->id,
525                                   FRESCAN_FIELD_FRAG_ID);
526
527         DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
528               "frame sent, minor:%u flags:0x%X frag_id:0x%X\n",
529               chip->minor, packet->flags, id);
530
531         if (packet->flags & FRESCAN_SS) {
532                 DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
533                       "calling frame_sent + program repl for id:%u\n", id);
534
535                 ret = frescan_replenishment_program(chip->minor, id);
536                 if (ret != 0) {
537                         ERROR("could not program replenishment\n");
538                         return -1;
539                 }
540
541                 ret = frescan_servers_frame_sent(chip->minor, id);
542                 if (ret != 0) {
543                         ERROR("could not let the server a frame was sent\n");
544                         return -1;
545                 }
546         }
547
548         DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG || FRESCAN_FRAG_ENABLE_DEBUG,
549               "last packet buffer_pending_bytes=%u\n",
550               packet->buffer_pending_bytes);
551
552         if (packet->buffer_pending_bytes > 0) {
553                 if (packet->flags & FRESCAN_FP) {
554                         prio = frescan_id_get_field(packet->frame->id,
555                                         FRESCAN_FIELD_PRIO);
556
557                         DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
558                               "requeue fp packet, prio:%u\n", prio);
559
560                         pqueue = the_networks[chip->minor].queues.tx_fp_queue;
561                         ret = frescan_pqueue_requeue(pqueue, packet, prio);
562                         if (ret != 0) {
563                                 ERROR("could not requeue the packet\n");
564                                 return -1;
565                         }
566                 } else if (packet->flags & FRESCAN_SS) {
567                         DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
568                               "requeue server %u packet\n", id);
569                         ret = frescan_servers_requeue(chip->minor, id, packet);
570                         if (ret != 0) {
571                                 ERROR("could not requeue the packet\n");
572                                 return -1;
573                         }
574                 } else {
575                         ERROR("flags are not correct\n");
576                         return -1;
577                 }
578         } else {
579                 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
580                       "all packet fragmets sent, freeing the packet\n");
581
582                 ret = can_framespool_free(packet->frame);
583                 if (ret != 0)  {
584                         ERROR ("could not free the frame\n");
585                         return ret;
586                 }
587
588                 ret = frescan_packets_free(packet);
589                 if (ret != 0)  {
590                         ERROR ("could not free the packet\n");
591                         return ret;
592                 }
593
594                 // TODO: signal semaphore for send_sync
595         }
596
597         the_networks[chip->minor].last_packet = NULL;
598
599         ret = frescan_hw_buffer_update(chip->minor);
600         if (ret != 0) {
601                 ERROR("could not update hw buffer\n");
602                 return -1;
603         }
604
605         return 0;
606 };
607
608 /**
609  * frescan_hook_frame_aborted - frame frame aborted hook
610  *
611  * This function will be called by the CAN driver's IRQ handler when a frame
612  * is aborted (because another frame with higher priority is waiting). We
613  * have to requeue the frame and update the buffer.
614  */
615
616 static int frescan_hook_frame_aborted(const struct can_chip_t *chip)
617 {
618         int ret;
619         frescan_packet_t *packet;
620         frescan_prio_queue_t *pqueue;
621         frescan_prio_t prio;
622         frescan_ss_t id;
623
624         packet = the_networks[chip->minor].last_packet;
625
626         id = frescan_id_get_field(packet->frame->id,
627                                   FRESCAN_FIELD_FRAG_ID);
628
629         DEBUG(FRESCAN_SENT_HOOK_ENABLE_DEBUG,
630               "frame aborted, minor:%u flags:0x%X frag_id:0x%X\n",
631               chip->minor, packet->flags, id);
632
633         if (packet->flags & FRESCAN_FP) {
634                 prio = frescan_id_get_field(packet->frame->id,
635                                             FRESCAN_FIELD_PRIO);
636
637                 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
638                       "requeue fp packet, prio:%u\n", prio);
639
640                 pqueue = the_networks[chip->minor].queues.tx_fp_queue;
641                 ret = frescan_pqueue_requeue(pqueue, packet, prio);
642                 if (ret != 0) {
643                         ERROR("could not requeue the packet\n");
644                         return -1;
645                 }
646         } else if (packet->flags & FRESCAN_SS) {
647                 DEBUG(FRESCAN_FRAG_ENABLE_DEBUG,
648                       "requeue server %u packet\n", id);
649
650                 ret = frescan_servers_requeue(chip->minor, id, packet);
651                 if (ret != 0) {
652                         ERROR("could not requeue the packet\n");
653                         return -1;
654                 }
655         } else {
656                 ERROR("flags are not correct\n");
657                 return -1;
658         }
659
660         the_networks[chip->minor].last_packet = NULL;
661
662         ret = frescan_hw_buffer_update(chip->minor);
663         if (ret != 0) {
664                 ERROR("could not update hw buffer\n");
665                 return -1;
666         }
667
668         return 0;
669 }