90dcc8c9d0d1caf0dd3e086ae6952189463fa604
[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         printf("\nsending -->\n");
107         dump_buffer(buffer, len);
108         printf("\n");
109 #endif
110
111         ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
112         if (ret != 0 || (unsigned int)transferred != len) {
113                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
114                         ret, transferred, len);
115                 return ret;
116         }
117
118         return 0;
119 }
120
121 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
122 {
123         uint8_t **buffer_iterator = &buffer;
124
125         put_le32(h->packet_type, buffer_iterator);
126         put_8(h->unknown0, buffer_iterator);
127         put_8(h->header_data_len, buffer_iterator);
128         put_8(h->unknown2, buffer_iterator);
129         put_8(h->unknown3, buffer_iterator);
130         put_le32(h->header_data.data.field0, buffer_iterator);
131         put_le32(h->header_data.data.field1, buffer_iterator);
132         put_le32(h->header_data.data.field2, buffer_iterator);
133         put_le32(h->header_data.data.field3, buffer_iterator);
134 }
135
136 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
137 {
138         uint8_t *buffer;
139         int ret;
140
141 #if DEBUG
142         printf("\n");
143         dump_header(h);
144         printf("\n");
145 #endif
146
147         buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
148         if (buffer == NULL) {
149                 perror("calloc buffer");
150                 return -ENOMEM;
151         }
152
153         serialize_header(h, buffer);
154         ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
155         if (ret < 0)
156                 fprintf(stderr, "send_header: failed to send data.\n");
157
158         free(buffer);
159         return ret;
160 }
161
162 am7xxx_device am7xxx_init(void)
163 {
164         am7xxx_device dev;
165
166         libusb_init(NULL);
167         libusb_set_debug(NULL, 3);
168
169         dev = libusb_open_device_with_vid_pid(NULL,
170                                               AM7XXX_VENDOR_ID,
171                                               AM7XXX_PRODUCT_ID);
172         if (dev == NULL) {
173                 errno = ENODEV;
174                 perror("libusb_open_device_with_vid_pid");
175                 goto out_libusb_exit;
176         }
177
178         libusb_set_configuration(dev, 1);
179         libusb_claim_interface(dev, 0);
180
181         return dev;
182
183 out_libusb_exit:
184         libusb_exit(NULL);
185         return NULL;
186 }
187
188 void am7xxx_shutdown(am7xxx_device dev)
189 {
190         if (dev) {
191                 libusb_close(dev);
192                 libusb_exit(NULL);
193         }
194 }
195
196 int am7xxx_send_image(am7xxx_device dev,
197                       am7xxx_image_format format,
198                       unsigned int width,
199                       unsigned int height,
200                       uint8_t *image,
201                       unsigned int size)
202 {
203         int ret;
204         struct am7xxx_header h = {
205                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
206                 .unknown0        = 0x00,
207                 .header_data_len = sizeof(struct am7xxx_image_header),
208                 .unknown2        = 0x3e,
209                 .unknown3        = 0x10,
210                 .header_data = {
211                         .image = {
212                                 .format     = format,
213                                 .width      = width,
214                                 .height     = height,
215                                 .image_size = size,
216                         },
217                 },
218         };
219
220         ret = send_header(dev, &h);
221         if (ret < 0)
222                 return ret;
223
224         if (image == NULL || size == 0)
225                 return 0;
226
227         return send_data(dev, image, size);
228 }
229
230 int am7xxx_set_power_mode(am7xxx_device dev, am7xxx_power_mode mode)
231 {
232         int ret;
233         struct am7xxx_header h = {
234                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
235                 .unknown0        = 0x00,
236                 .header_data_len = sizeof(struct am7xxx_power_header),
237                 .unknown2        = 0x3e,
238                 .unknown3        = 0x10,
239         };
240
241         switch(mode) {
242         case AM7XXX_POWER_OFF:
243                 h.header_data.power.bit2 = 0;
244                 h.header_data.power.bit1 = 0;
245                 h.header_data.power.bit0 = 0;
246                 break;
247
248         case AM7XXX_POWER_LOW:
249                 h.header_data.power.bit2 = 0;
250                 h.header_data.power.bit1 = 0;
251                 h.header_data.power.bit0 = 1;
252
253         case AM7XXX_POWER_MIDDLE:
254                 h.header_data.power.bit2 = 0;
255                 h.header_data.power.bit1 = 1;
256                 h.header_data.power.bit0 = 0;
257                 break;
258
259         case AM7XXX_POWER_HIGH:
260                 h.header_data.power.bit2 = 0;
261                 h.header_data.power.bit1 = 1;
262                 h.header_data.power.bit0 = 1;
263                 break;
264
265         case AM7XXX_POWER_TURBO:
266                 h.header_data.power.bit2 = 1;
267                 h.header_data.power.bit1 = 0;
268                 h.header_data.power.bit0 = 0;
269                 break;
270
271         default:
272                 fprintf(stderr, "Unsupported power mode.\n");
273                 return -EINVAL;
274         };
275
276         ret = send_header(dev, &h);
277         if (ret < 0)
278                 return ret;
279
280         return 0;
281 }