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