| 1 | /* |
| 2 | Copyright (c) 2015 Raspberry Pi (Trading) Ltd. |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the copyright holder nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | // Utility for driving mailbox interface to VideoCore |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
| 35 | #include <assert.h> |
| 36 | #include <stdint.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <sys/ioctl.h> /* ioctl */ |
| 39 | |
| 40 | #define DEVICE_FILE_NAME "/dev/vcio" |
| 41 | #define MAJOR_NUM 100 |
| 42 | #define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *) |
| 43 | |
| 44 | /* |
| 45 | * use ioctl to send mbox property message |
| 46 | */ |
| 47 | |
| 48 | static int mbox_property(int file_desc, void *buf) |
| 49 | { |
| 50 | int ret_val = ioctl(file_desc, IOCTL_MBOX_PROPERTY, buf); |
| 51 | |
| 52 | if (ret_val < 0) { |
| 53 | printf("ioctl_set_msg failed:%d\n" , ret_val); |
| 54 | } |
| 55 | return ret_val; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | static int mbox_open() |
| 60 | { |
| 61 | int file_desc; |
| 62 | |
| 63 | // open a char device file used for communicating with kernel mbox driver |
| 64 | file_desc = open(DEVICE_FILE_NAME, 0); |
| 65 | if (file_desc < 0) { |
| 66 | printf("Can't open device file: %s\n" , DEVICE_FILE_NAME); |
| 67 | printf("Try creating a device file with: sudo mknod %s c %d 0\n" , DEVICE_FILE_NAME, MAJOR_NUM); |
| 68 | exit(-1); |
| 69 | } |
| 70 | return file_desc; |
| 71 | } |
| 72 | |
| 73 | static void mbox_close(int file_desc) { |
| 74 | close(file_desc); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | #define MAX_WORDS 256 |
| 79 | |
| 80 | int main(int argc, char *argv[]) |
| 81 | { |
| 82 | int mb = mbox_open(); |
| 83 | unsigned words[MAX_WORDS] = {}; |
| 84 | int i; |
| 85 | |
| 86 | if (argc < 2 || argc+1 >= MAX_WORDS) |
| 87 | { |
| 88 | printf("Usage %s [word0] [word1] ...\n" , argv[0]); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | words[0] = (argc+2) * sizeof words[0]; |
| 93 | words[1] = 0; // process request |
| 94 | for (i=1; i<argc; i++) |
| 95 | words[i+1] = strtoul(argv[i], 0, 0); |
| 96 | |
| 97 | words[argc+1] = 0; // end tag |
| 98 | |
| 99 | int s = mbox_property(mb, words); |
| 100 | |
| 101 | mbox_close(mb); |
| 102 | |
| 103 | for (i=0; i<argc+2; i++) |
| 104 | printf("0x%08x " , words[i]); |
| 105 | printf("\n" ); |
| 106 | return s; |
| 107 | } |
| 108 | |