Implement am7xxx_set_power_mode()
[libam7xxx.git] / src / am7xxx.c
1 /* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs
2  *
3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 #include "am7xxx.h"
24 #include "serialize.h"
25
26 #define AM7XXX_VENDOR_ID  0x1de1
27 #define AM7XXX_PRODUCT_ID 0xc101
28
29 static void dump_image_header(struct am7xxx_image_header *i)
30 {
31         if (i == NULL)
32                 return;
33
34         printf("Image header:\n");
35         printf("\tformat:     0x%08x (%u)\n", i->format, i->format);
36         printf("\twidth:      0x%08x (%u)\n", i->width, i->width);
37         printf("\theight:     0x%08x (%u)\n", i->height, i->height);
38         printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
39 }
40
41 static void dump_power_header(struct am7xxx_power_header *p)
42 {
43         if (p == NULL)
44                 return;
45
46         printf("Power header:\n");
47         printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
48         printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
49         printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
50 }
51
52 static void dump_header(struct am7xxx_header *h)
53 {
54         if (h == NULL)
55                 return;
56
57         printf("packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
58         printf("unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
59         printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
60         printf("unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
61         printf("unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
62
63         switch(h->packet_type) {
64         case AM7XXX_PACKET_TYPE_IMAGE:
65                 dump_image_header(&(h->header_data.image));
66                 break;
67
68         case AM7XXX_PACKET_TYPE_POWER:
69                 dump_power_header(&(h->header_data.power));
70                 break;
71
72         default:
73                 printf("Packet type not supported!\n");
74                 break;
75         }
76
77         fflush(stdout);
78 }
79
80 static inline unsigned int in_80chars(unsigned int i)
81 {
82         /* The 3 below is the length of "xx " where xx is the hex string
83          * representation of a byte */
84         return ((i+1) % (80/3));
85 }
86
87 static void dump_buffer(uint8_t *buffer, unsigned int len)
88 {
89         unsigned int i;
90
91         if (buffer == NULL || len == 0)
92                 return;
93
94         for (i = 0; i < len; i++) {
95                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
96         }
97         fflush(stdout);
98 }
99
100 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
101 {
102         int ret;
103         int transferred;
104
105 #if DEBUG
106         dump_buffer(buffer, len);
107         printf("\n");
108 #endif
109
110         ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
111         if (ret != 0 || (unsigned int)transferred != len) {
112                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
113                         ret, transferred, len);
114                 return ret;
115         }
116
117         return 0;
118 }
119
120 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
121 {
122         uint8_t **buffer_iterator = &buffer;
123
124         put_le32(h->packet_type, buffer_iterator);
125         put_8(h->unknown0, buffer_iterator);
126         put_8(h->header_data_len, buffer_iterator);
127         put_8(h->unknown2, buffer_iterator);
128         put_8(h->unknown3, buffer_iterator);
129         put_le32(h->header_data.data.field0, buffer_iterator);
130         put_le32(h->header_data.data.field1, buffer_iterator);
131         put_le32(h->header_data.data.field2, buffer_iterator);
132         put_le32(h->header_data.data.field3, buffer_iterator);
133 }
134
135 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
136 {
137         uint8_t *buffer;
138         int ret;
139
140 #if DEBUG
141         dump_header(h);
142         printf("\n");
143 #endif
144
145         buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
146         if (buffer == NULL) {
147                 perror("calloc buffer");
148                 return -ENOMEM;
149         }
150
151         serialize_header(h, buffer);
152         ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
153         if (ret < 0)
154                 fprintf(stderr, "send_header: failed to send data.\n");
155
156         free(buffer);
157         return ret;
158 }
159
160 am7xxx_device am7xxx_init(void)
161 {
162         am7xxx_device dev;
163
164         libusb_init(NULL);
165         libusb_set_debug(NULL, 3);
166
167         dev = libusb_open_device_with_vid_pid(NULL,
168                                               AM7XXX_VENDOR_ID,
169                                               AM7XXX_PRODUCT_ID);
170         if (dev == NULL) {
171                 errno = ENODEV;
172                 perror("libusb_open_device_with_vid_pid");
173                 goto out_libusb_exit;
174         }
175
176         libusb_set_configuration(dev, 1);
177         libusb_claim_interface(dev, 0);
178
179         return dev;
180
181 out_libusb_exit:
182         libusb_exit(NULL);
183         return NULL;
184 }
185
186 void am7xxx_shutdown(am7xxx_device dev)
187 {
188         if (dev) {
189                 libusb_close(dev);
190                 libusb_exit(NULL);
191         }
192 }
193
194 int am7xxx_send_image(am7xxx_device dev,
195                       am7xxx_image_format format,
196                       unsigned int width,
197                       unsigned int height,
198                       uint8_t *image,
199                       unsigned int size)
200 {
201         int ret;
202         struct am7xxx_header h = {
203                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
204                 .unknown0        = 0x00,
205                 .header_data_len = sizeof(struct am7xxx_image_header),
206                 .unknown2        = 0x3e,
207                 .unknown3        = 0x10,
208                 .header_data = {
209                         .image = {
210                                 .format     = format,
211                                 .width      = width,
212                                 .height     = height,
213                                 .image_size = size,
214                         },
215                 },
216         };
217
218         ret = send_header(dev, &h);
219         if (ret < 0)
220                 return ret;
221
222         if (image == NULL || size == 0)
223                 return 0;
224
225         return send_data(dev, image, size);
226 }
227
228 int am7xxx_set_power_mode(am7xxx_device dev, am7xxx_power_mode mode)
229 {
230         int ret;
231         struct am7xxx_header h = {
232                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
233                 .unknown0        = 0x00,
234                 .header_data_len = sizeof(struct am7xxx_power_header),
235                 .unknown2        = 0x3e,
236                 .unknown3        = 0x10,
237         };
238
239         switch(mode) {
240         case AM7XXX_POWER_OFF:
241                 h.header_data.power.bit2 = 0;
242                 h.header_data.power.bit1 = 0;
243                 h.header_data.power.bit0 = 0;
244                 break;
245
246         case AM7XXX_POWER_LOW:
247                 h.header_data.power.bit2 = 0;
248                 h.header_data.power.bit1 = 0;
249                 h.header_data.power.bit0 = 1;
250
251         case AM7XXX_POWER_MIDDLE:
252                 h.header_data.power.bit2 = 0;
253                 h.header_data.power.bit1 = 1;
254                 h.header_data.power.bit0 = 0;
255                 break;
256
257         case AM7XXX_POWER_HIGH:
258                 h.header_data.power.bit2 = 0;
259                 h.header_data.power.bit1 = 1;
260                 h.header_data.power.bit0 = 1;
261                 break;
262
263         case AM7XXX_POWER_TURBO:
264                 h.header_data.power.bit2 = 1;
265                 h.header_data.power.bit1 = 0;
266                 h.header_data.power.bit0 = 0;
267                 break;
268
269         default:
270                 fprintf(stderr, "Unsupported power mode.\n");
271                 return -EINVAL;
272         };
273
274         ret = send_header(dev, &h);
275         if (ret < 0)
276                 return ret;
277
278         return 0;
279 }