cdcbf3f7b0f6e8918daec7c7c945a94526746d5e
[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_devinfo_header(struct am7xxx_devinfo_header *d)
30 {
31         if (d == NULL)
32                 return;
33
34         printf("Info header:\n");
35         printf("\tnative_width:  0x%08x (%u)\n", d->native_width, d->native_width);
36         printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
37         printf("\tunknown0:      0x%08x (%u)\n", d->unknown0, d->unknown0);
38         printf("\tunknown1:      0x%08x (%u)\n", d->unknown1, d->unknown1);
39 }
40
41 static void dump_image_header(struct am7xxx_image_header *i)
42 {
43         if (i == NULL)
44                 return;
45
46         printf("Image header:\n");
47         printf("\tformat:     0x%08x (%u)\n", i->format, i->format);
48         printf("\twidth:      0x%08x (%u)\n", i->width, i->width);
49         printf("\theight:     0x%08x (%u)\n", i->height, i->height);
50         printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
51 }
52
53 static void dump_power_header(struct am7xxx_power_header *p)
54 {
55         if (p == NULL)
56                 return;
57
58         printf("Power header:\n");
59         printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
60         printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
61         printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
62 }
63
64 static void dump_header(struct am7xxx_header *h)
65 {
66         if (h == NULL)
67                 return;
68
69         printf("packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
70         printf("unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
71         printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
72         printf("unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
73         printf("unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
74
75         switch(h->packet_type) {
76         case AM7XXX_PACKET_TYPE_DEVINFO:
77                 dump_devinfo_header(&(h->header_data.devinfo));
78                 break;
79
80         case AM7XXX_PACKET_TYPE_IMAGE:
81                 dump_image_header(&(h->header_data.image));
82                 break;
83
84         case AM7XXX_PACKET_TYPE_POWER:
85                 dump_power_header(&(h->header_data.power));
86                 break;
87
88         default:
89                 printf("Packet type not supported!\n");
90                 break;
91         }
92
93         fflush(stdout);
94 }
95
96 static inline unsigned int in_80chars(unsigned int i)
97 {
98         /* The 3 below is the length of "xx " where xx is the hex string
99          * representation of a byte */
100         return ((i+1) % (80/3));
101 }
102
103 static void dump_buffer(uint8_t *buffer, unsigned int len)
104 {
105         unsigned int i;
106
107         if (buffer == NULL || len == 0)
108                 return;
109
110         for (i = 0; i < len; i++) {
111                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
112         }
113         fflush(stdout);
114 }
115
116 static int read_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
117 {
118         int ret;
119         int transferred;
120
121         ret = libusb_bulk_transfer(dev, 0x81, buffer, len, &transferred, 0);
122         if (ret != 0 || (unsigned int)transferred != len) {
123                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
124                         ret, transferred, len);
125                 return ret;
126         }
127
128 #if DEBUG
129         printf("\n<-- received\n");
130         dump_buffer(buffer, len);
131         printf("\n");
132 #endif
133
134         return 0;
135 }
136
137 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
138 {
139         int ret;
140         int transferred;
141
142 #if DEBUG
143         printf("\nsending -->\n");
144         dump_buffer(buffer, len);
145         printf("\n");
146 #endif
147
148         ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
149         if (ret != 0 || (unsigned int)transferred != len) {
150                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
151                         ret, transferred, len);
152                 return ret;
153         }
154
155         return 0;
156 }
157
158 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
159 {
160         uint8_t **buffer_iterator = &buffer;
161
162         put_le32(h->packet_type, buffer_iterator);
163         put_8(h->unknown0, buffer_iterator);
164         put_8(h->header_data_len, buffer_iterator);
165         put_8(h->unknown2, buffer_iterator);
166         put_8(h->unknown3, buffer_iterator);
167         put_le32(h->header_data.data.field0, buffer_iterator);
168         put_le32(h->header_data.data.field1, buffer_iterator);
169         put_le32(h->header_data.data.field2, buffer_iterator);
170         put_le32(h->header_data.data.field3, buffer_iterator);
171 }
172
173 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
174 {
175         uint8_t **buffer_iterator = &buffer;
176
177         h->packet_type = get_le32(buffer_iterator);
178         h->unknown0 = get_8(buffer_iterator);
179         h->header_data_len = get_8(buffer_iterator);
180         h->unknown2 = get_8(buffer_iterator);
181         h->unknown3 = get_8(buffer_iterator);
182         h->header_data.data.field0 = get_le32(buffer_iterator);
183         h->header_data.data.field1 = get_le32(buffer_iterator);
184         h->header_data.data.field2 = get_le32(buffer_iterator);
185         h->header_data.data.field3 = get_le32(buffer_iterator);
186 }
187
188 static int read_header(am7xxx_device dev, struct am7xxx_header *h)
189 {
190         uint8_t *buffer;
191         int ret;
192
193         buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
194         if (buffer == NULL) {
195                 perror("calloc buffer");
196                 return -ENOMEM;
197         }
198
199         ret = read_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
200         if (ret < 0)
201                 goto out;
202
203         unserialize_header(buffer, h);
204
205 #if DEBUG
206         printf("\n");
207         dump_header(h);
208         printf("\n");
209 #endif
210
211         ret = 0;
212
213 out:
214         free(buffer);
215         return ret;
216 }
217
218 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
219 {
220         uint8_t *buffer;
221         int ret;
222
223 #if DEBUG
224         printf("\n");
225         dump_header(h);
226         printf("\n");
227 #endif
228
229         buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
230         if (buffer == NULL) {
231                 perror("calloc buffer");
232                 return -ENOMEM;
233         }
234
235         serialize_header(h, buffer);
236         ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
237         if (ret < 0)
238                 fprintf(stderr, "send_header: failed to send data.\n");
239
240         free(buffer);
241         return ret;
242 }
243
244 am7xxx_device am7xxx_init(void)
245 {
246         am7xxx_device dev;
247
248         libusb_init(NULL);
249         libusb_set_debug(NULL, 3);
250
251         dev = libusb_open_device_with_vid_pid(NULL,
252                                               AM7XXX_VENDOR_ID,
253                                               AM7XXX_PRODUCT_ID);
254         if (dev == NULL) {
255                 errno = ENODEV;
256                 perror("libusb_open_device_with_vid_pid");
257                 goto out_libusb_exit;
258         }
259
260         libusb_set_configuration(dev, 1);
261         libusb_claim_interface(dev, 0);
262
263         return dev;
264
265 out_libusb_exit:
266         libusb_exit(NULL);
267         return NULL;
268 }
269
270 void am7xxx_shutdown(am7xxx_device dev)
271 {
272         if (dev) {
273                 libusb_close(dev);
274                 libusb_exit(NULL);
275         }
276 }
277
278 int am7xxx_get_device_info(am7xxx_device dev,
279                            unsigned int *native_width,
280                            unsigned int *native_height,
281                            unsigned int *unknown0,
282                            unsigned int *unknown1)
283 {
284         int ret;
285         struct am7xxx_header h = {
286                 .packet_type     = AM7XXX_PACKET_TYPE_DEVINFO,
287                 .unknown0        = 0x00,
288                 .header_data_len = 0x00,
289                 .unknown2        = 0x3e,
290                 .unknown3        = 0x10,
291                 .header_data = {
292                         .devinfo = {
293                                 .native_width  = 0,
294                                 .native_height = 0,
295                                 .unknown0      = 0,
296                                 .unknown1      = 0,
297                         },
298                 },
299         };
300
301         ret = send_header(dev, &h);
302         if (ret < 0)
303                 return ret;
304
305         ret = read_header(dev, &h);
306         if (ret < 0)
307                 return ret;
308
309         *native_width = h.header_data.devinfo.native_width;
310         *native_height = h.header_data.devinfo.native_height;
311         *unknown0 = h.header_data.devinfo.unknown0;
312         *unknown1 = h.header_data.devinfo.unknown1;
313
314         return 0;
315 }
316
317 int am7xxx_send_image(am7xxx_device dev,
318                       am7xxx_image_format format,
319                       unsigned int width,
320                       unsigned int height,
321                       uint8_t *image,
322                       unsigned int size)
323 {
324         int ret;
325         struct am7xxx_header h = {
326                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
327                 .unknown0        = 0x00,
328                 .header_data_len = sizeof(struct am7xxx_image_header),
329                 .unknown2        = 0x3e,
330                 .unknown3        = 0x10,
331                 .header_data = {
332                         .image = {
333                                 .format     = format,
334                                 .width      = width,
335                                 .height     = height,
336                                 .image_size = size,
337                         },
338                 },
339         };
340
341         ret = send_header(dev, &h);
342         if (ret < 0)
343                 return ret;
344
345         if (image == NULL || size == 0)
346                 return 0;
347
348         return send_data(dev, image, size);
349 }
350
351 int am7xxx_set_power_mode(am7xxx_device dev, am7xxx_power_mode mode)
352 {
353         int ret;
354         struct am7xxx_header h = {
355                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
356                 .unknown0        = 0x00,
357                 .header_data_len = sizeof(struct am7xxx_power_header),
358                 .unknown2        = 0x3e,
359                 .unknown3        = 0x10,
360         };
361
362         switch(mode) {
363         case AM7XXX_POWER_OFF:
364                 h.header_data.power.bit2 = 0;
365                 h.header_data.power.bit1 = 0;
366                 h.header_data.power.bit0 = 0;
367                 break;
368
369         case AM7XXX_POWER_LOW:
370                 h.header_data.power.bit2 = 0;
371                 h.header_data.power.bit1 = 0;
372                 h.header_data.power.bit0 = 1;
373
374         case AM7XXX_POWER_MIDDLE:
375                 h.header_data.power.bit2 = 0;
376                 h.header_data.power.bit1 = 1;
377                 h.header_data.power.bit0 = 0;
378                 break;
379
380         case AM7XXX_POWER_HIGH:
381                 h.header_data.power.bit2 = 0;
382                 h.header_data.power.bit1 = 1;
383                 h.header_data.power.bit0 = 1;
384                 break;
385
386         case AM7XXX_POWER_TURBO:
387                 h.header_data.power.bit2 = 1;
388                 h.header_data.power.bit1 = 0;
389                 h.header_data.power.bit0 = 0;
390                 break;
391
392         default:
393                 fprintf(stderr, "Unsupported power mode.\n");
394                 return -EINVAL;
395         };
396
397         ret = send_header(dev, &h);
398         if (ret < 0)
399                 return ret;
400
401         return 0;
402 }