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