ps3av: implement command video_pitch
[ps3linux_ps3vuart_tools.git] / ps3sm / dev.c
blobca7108feba4f0ba112c4186100d4c86cb434f4d5
1 /*-
2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdint.h>
30 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
37 #include "ps3sm_msg.h"
38 #include "dev.h"
40 static int fd = -1;
42 int
43 dev_open(const char *device)
45 if (fd >= 0)
46 close(fd);
48 fd = open(device, O_RDWR);
49 if (fd < 0)
50 return (-1);
52 return (0);
55 void
56 dev_close(void)
58 if (fd >= 0)
59 close(fd);
62 int
63 dev_write(const void *buf, size_t len)
65 if (fd < 0)
66 return (-1);
68 return (write(fd, buf, len));
71 int
72 dev_read(void *buf, size_t len)
74 char tmp[4096];
75 struct ps3sm_header *hdr;
76 int ret;
78 if (fd < 0)
79 return (-1);
81 hdr = (struct ps3sm_header *) tmp;
83 while (1) {
84 ret = read(fd, hdr, sizeof(*hdr));
85 if (ret < 0) {
86 if (errno != EAGAIN)
87 return (-1);
88 } else if (ret > 0) {
89 break;
93 while (1) {
94 ret = read(fd, hdr + 1, hdr->payload_length);
95 if (ret < 0) {
96 if (errno != EAGAIN)
97 return (-1);
98 } else if (ret > 0) {
99 break;
103 if (len > (sizeof(*hdr) + hdr->payload_length))
104 len = sizeof(*hdr) + hdr->payload_length;
106 memcpy(buf, tmp, len);
108 return (len);