openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

bcm-knet-kcom.c (2088B)


      1 /*
      2  * 
      3  * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      4  * 
      5  * Copyright 2007-2019 Broadcom Inc. All rights reserved.
      6  *
      7  * File: 	kcom.c
      8  * Purpose: 	Provides a kcom interface using device IOCTL
      9  */
     10 
     11 #ifdef INCLUDE_KNET
     12 
     13 #include <fcntl.h>
     14 #include <unistd.h>
     15 #include <stdio.h>
     16 #include <assert.h>
     17 #include <sys/ioctl.h>
     18 #include <kcom.h>
     19 
     20 #include <bcm-knet.h>
     21 
     22 #define KNET_DEVICE_NAME "/dev/linux-bcm-knet"
     23 #define KNET_DEVICE_MAJOR 122
     24 #define KNET_DEVICE_MINOR 0
     25 
     26 static int kcom_fd = -1;
     27 
     28 char *
     29 bcm_knet_kcom_open(char *name)
     30 { 
     31     if (kcom_fd >= 0) {
     32 	return name;
     33     }
     34 
     35     if ((kcom_fd = open(KNET_DEVICE_NAME,
     36                         O_RDWR | O_SYNC | O_DSYNC | O_RSYNC)) < 0) {
     37 	perror("open " KNET_DEVICE_NAME ": ");
     38 	return NULL;
     39     }	
     40     
     41     return name;
     42 }
     43 
     44 int
     45 bcm_knet_kcom_close(void *handle)
     46 {
     47     close(kcom_fd);
     48     kcom_fd = -1;
     49 
     50     return 0;
     51 }
     52 
     53 int
     54 bcm_knet_kcom_msg_send(void *handle, void *msg,
     55                        unsigned int len, unsigned int bufsz)
     56 {
     57     bkn_ioctl_t io;
     58 
     59     io.len = len;
     60     io.bufsz = bufsz;
     61 
     62     /*
     63      * Pass pointer as 64-bit int in order to support 32-bit
     64      * applications running on a 64-bit kernel.
     65      */
     66     io.buf = (uint64_t)(unsigned long)msg;
     67 
     68     if (ioctl(kcom_fd, 0, &io) < 0) {
     69 	perror("send " KNET_DEVICE_NAME ": ");
     70         return -1;
     71     }
     72 
     73     if (io.rc < 0) {
     74         return -1;
     75     }
     76     return io.len;
     77 }
     78 
     79 int
     80 bcm_knet_kcom_msg_recv(void *handle, void *msg,
     81                        unsigned int bufsz)
     82 {
     83     bkn_ioctl_t io;
     84 
     85     io.len = 0;
     86     io.bufsz = bufsz;
     87 
     88     /*
     89      * Pass pointer as 64-bit int in order to support 32-bit
     90      * applications running on a 64-bit kernel.
     91      */
     92     io.buf = (uint64_t)(unsigned long)msg;
     93 
     94     do {
     95         if (ioctl(kcom_fd, 0, &io) < 0) {
     96             perror("recv " KNET_DEVICE_NAME ": ");
     97             return -1;
     98         }
     99 
    100         if (io.rc < 0) {
    101             return -1;
    102         }
    103     } while (io.len == 0);
    104 
    105     return io.len;
    106 }
    107 
    108 #endif
    109 
    110 int _bcm_knet_kcom_c_not_empty;