linux-uk-proxy.c (2812B)
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 8 #include <stdlib.h> 9 #include <stdio.h> 10 #include <fcntl.h> 11 #include <unistd.h> 12 #include <assert.h> 13 #include <sys/ioctl.h> 14 #include <string.h> 15 16 #include <linux-uk-proxy.h> 17 18 static int _dev_fd = -1; 19 20 int 21 linux_uk_proxy_open(void) 22 { 23 if (_dev_fd >= 0) { 24 return _dev_fd; 25 } 26 27 if ((_dev_fd = open(LUK_DEVICE_NAME, O_RDWR | O_SYNC | O_DSYNC | O_RSYNC)) < 0) { 28 /* Try inserting it from the local directory */ 29 perror("open " LUK_DEVICE_NAME ": "); 30 return -1; 31 } 32 33 return _dev_fd; 34 } 35 36 int 37 linux_uk_proxy_close(void) 38 { 39 close(_dev_fd); 40 return 0; 41 } 42 43 int 44 linux_uk_proxy_service_create(const char* service, unsigned int q_size, unsigned int flags) 45 { 46 luk_ioctl_t io; 47 48 strcpy(io.service, service); 49 io.args.create.q_size = q_size; 50 io.args.create.flags = flags; 51 if (ioctl(_dev_fd, LUK_SERVICE_CREATE, &io) >= 0) { 52 return (io.rc); 53 } 54 55 return (-1); 56 } 57 58 int 59 linux_uk_proxy_service_destroy(const char* service) 60 { 61 luk_ioctl_t io; 62 63 strcpy(io.service, service); 64 if (ioctl(_dev_fd, LUK_SERVICE_DESTROY, &io) >= 0) { 65 return (io.rc); 66 } 67 68 return (-1); 69 } 70 71 int 72 linux_uk_proxy_recv(const char* service, void* data, unsigned int* len) 73 { 74 luk_ioctl_t io; 75 76 strcpy(io.service, service); 77 io.args.recv.data = (uint64_t)(unsigned long)data; 78 io.args.recv.len = *len; 79 80 /* 81 * Return code -2 means that a signal was received (e.g. SIGSTOP 82 * from GDB). In this case no message is ready, and the operation 83 * should simply be retried. 84 */ 85 do { 86 if (ioctl(_dev_fd, LUK_SERVICE_RECV, &io) < 0) { 87 return -1; 88 } 89 } while (io.rc == -2); 90 91 *len = io.args.recv.len; 92 return io.rc; 93 } 94 95 int 96 linux_uk_proxy_send(const char* service, void* data, unsigned int len) 97 { 98 luk_ioctl_t io; 99 100 strcpy(io.service, service); 101 io.args.send.data = (uint64_t)(unsigned long)data; 102 io.args.send.len = len; 103 104 /* 105 * Return code -2 means that a signal was received (e.g. SIGSTOP 106 * from GDB). In this case the message was not sent, and the 107 * operation should simply be retried. 108 */ 109 do { 110 if (ioctl(_dev_fd, LUK_SERVICE_SEND, &io) < 0) { 111 return -1; 112 } 113 } while (io.rc == -2); 114 115 return io.rc; 116 } 117 118 int 119 linux_uk_proxy_suspend(const char* service) 120 { 121 luk_ioctl_t io; 122 123 strcpy(io.service, service); 124 125 ioctl(_dev_fd, LUK_SERVICE_SUSPEND, &io); 126 return io.rc; 127 } 128 129 int 130 linux_uk_proxy_resume(const char* service) 131 { 132 luk_ioctl_t io; 133 134 strcpy(io.service, service); 135 136 ioctl(_dev_fd, LUK_SERVICE_RESUME, &io); 137 return io.rc; 138 }