20b0ddf3996fe0848cd2ab49c80f87f1cf67e5ca
[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         am7xxx_context *ctx;
59         am7xxx_device *next;
60 };
61
62 struct _am7xxx_context {
63         libusb_context *usb_context;
64         am7xxx_device *devices_list;
65 };
66
67 typedef enum {
68         AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
69         AM7XXX_PACKET_TYPE_IMAGE   = 0x02,
70         AM7XXX_PACKET_TYPE_POWER   = 0x04,
71         AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
72 } am7xxx_packet_type;
73
74 struct am7xxx_generic_header {
75         uint32_t field0;
76         uint32_t field1;
77         uint32_t field2;
78         uint32_t field3;
79 };
80
81 struct am7xxx_devinfo_header {
82         uint32_t native_width;
83         uint32_t native_height;
84         uint32_t unknown0;
85         uint32_t unknown1;
86 };
87
88 struct am7xxx_image_header {
89         uint32_t format;
90         uint32_t width;
91         uint32_t height;
92         uint32_t image_size;
93 };
94
95 struct am7xxx_power_header {
96         uint32_t bit2;
97         uint32_t bit1;
98         uint32_t bit0;
99 };
100
101 /*
102  * Examples of packet headers:
103  *
104  * Image header:
105  * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
106  *
107  * Power header:
108  * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
109  */
110
111 struct am7xxx_header {
112         uint32_t packet_type;
113         uint8_t unknown0;
114         uint8_t header_data_len;
115         uint8_t unknown2;
116         uint8_t unknown3;
117         union {
118                 struct am7xxx_generic_header data;
119                 struct am7xxx_devinfo_header devinfo;
120                 struct am7xxx_image_header image;
121                 struct am7xxx_power_header power;
122         } header_data;
123 };
124
125
126 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
127 {
128         if (d == NULL)
129                 return;
130
131         printf("Info header:\n");
132         printf("\tnative_width:  0x%08x (%u)\n", d->native_width, d->native_width);
133         printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
134         printf("\tunknown0:      0x%08x (%u)\n", d->unknown0, d->unknown0);
135         printf("\tunknown1:      0x%08x (%u)\n", d->unknown1, d->unknown1);
136 }
137
138 static void dump_image_header(struct am7xxx_image_header *i)
139 {
140         if (i == NULL)
141                 return;
142
143         printf("Image header:\n");
144         printf("\tformat:     0x%08x (%u)\n", i->format, i->format);
145         printf("\twidth:      0x%08x (%u)\n", i->width, i->width);
146         printf("\theight:     0x%08x (%u)\n", i->height, i->height);
147         printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
148 }
149
150 static void dump_power_header(struct am7xxx_power_header *p)
151 {
152         if (p == NULL)
153                 return;
154
155         printf("Power header:\n");
156         printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
157         printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
158         printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
159 }
160
161 static void dump_header(struct am7xxx_header *h)
162 {
163         if (h == NULL)
164                 return;
165
166         printf("packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
167         printf("unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
168         printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
169         printf("unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
170         printf("unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
171
172         switch(h->packet_type) {
173         case AM7XXX_PACKET_TYPE_DEVINFO:
174                 dump_devinfo_header(&(h->header_data.devinfo));
175                 break;
176
177         case AM7XXX_PACKET_TYPE_IMAGE:
178                 dump_image_header(&(h->header_data.image));
179                 break;
180
181         case AM7XXX_PACKET_TYPE_POWER:
182                 dump_power_header(&(h->header_data.power));
183                 break;
184
185         default:
186                 printf("Packet type not supported!\n");
187                 break;
188         }
189
190         fflush(stdout);
191 }
192
193 static inline unsigned int in_80chars(unsigned int i)
194 {
195         /* The 3 below is the length of "xx " where xx is the hex string
196          * representation of a byte */
197         return ((i+1) % (80/3));
198 }
199
200 static void dump_buffer(uint8_t *buffer, unsigned int len)
201 {
202         unsigned int i;
203
204         if (buffer == NULL || len == 0)
205                 return;
206
207         for (i = 0; i < len; i++) {
208                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
209         }
210         fflush(stdout);
211 }
212
213 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
214 {
215         int ret;
216         int transferred = 0;
217
218         ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
219         if (ret != 0 || (unsigned int)transferred != len) {
220                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
221                         ret, transferred, len);
222                 return ret;
223         }
224
225 #if DEBUG
226         printf("\n<-- received\n");
227         dump_buffer(buffer, len);
228         printf("\n");
229 #endif
230
231         return 0;
232 }
233
234 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
235 {
236         int ret;
237         int transferred = 0;
238
239 #if DEBUG
240         printf("\nsending -->\n");
241         dump_buffer(buffer, len);
242         printf("\n");
243 #endif
244
245         ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
246         if (ret != 0 || (unsigned int)transferred != len) {
247                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
248                         ret, transferred, len);
249                 return ret;
250         }
251
252         return 0;
253 }
254
255 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
256 {
257         uint8_t **buffer_iterator = &buffer;
258
259         put_le32(h->packet_type, buffer_iterator);
260         put_8(h->unknown0, buffer_iterator);
261         put_8(h->header_data_len, buffer_iterator);
262         put_8(h->unknown2, buffer_iterator);
263         put_8(h->unknown3, buffer_iterator);
264         put_le32(h->header_data.data.field0, buffer_iterator);
265         put_le32(h->header_data.data.field1, buffer_iterator);
266         put_le32(h->header_data.data.field2, buffer_iterator);
267         put_le32(h->header_data.data.field3, buffer_iterator);
268 }
269
270 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
271 {
272         uint8_t **buffer_iterator = &buffer;
273
274         h->packet_type = get_le32(buffer_iterator);
275         h->unknown0 = get_8(buffer_iterator);
276         h->header_data_len = get_8(buffer_iterator);
277         h->unknown2 = get_8(buffer_iterator);
278         h->unknown3 = get_8(buffer_iterator);
279         h->header_data.data.field0 = get_le32(buffer_iterator);
280         h->header_data.data.field1 = get_le32(buffer_iterator);
281         h->header_data.data.field2 = get_le32(buffer_iterator);
282         h->header_data.data.field3 = get_le32(buffer_iterator);
283 }
284
285 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
286 {
287         int ret;
288
289         ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
290         if (ret < 0)
291                 goto out;
292
293         unserialize_header(dev->buffer, h);
294
295 #if DEBUG
296         printf("\n");
297         dump_header(h);
298         printf("\n");
299 #endif
300
301         ret = 0;
302
303 out:
304         return ret;
305 }
306
307 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
308 {
309         int ret;
310
311 #if DEBUG
312         printf("\n");
313         dump_header(h);
314         printf("\n");
315 #endif
316
317         serialize_header(h, dev->buffer);
318         ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
319         if (ret < 0)
320                 fprintf(stderr, "send_header: failed to send data.\n");
321
322         return ret;
323 }
324
325 static am7xxx_device *add_new_device(am7xxx_context *ctx)
326 {
327         am7xxx_device **devices_list;
328         am7xxx_device *new_device;
329
330         if (ctx == NULL) {
331                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
332                 return NULL;
333         }
334
335         devices_list = &(ctx->devices_list);
336
337         new_device = malloc(sizeof(*new_device));
338         if (new_device == NULL) {
339                 perror("malloc");
340                 return NULL;
341         }
342         memset(new_device, 0, sizeof(*new_device));
343
344         new_device->ctx = ctx;
345
346         if (*devices_list == NULL) {
347                 *devices_list = new_device;
348         } else {
349                 am7xxx_device *prev = *devices_list;
350                 while (prev->next)
351                         prev = prev->next;
352                 prev->next = new_device;
353         }
354         return new_device;
355 }
356
357 static am7xxx_device *find_device(am7xxx_context *ctx,
358                                   unsigned int device_index)
359 {
360         unsigned int i = 0;
361         am7xxx_device *current;
362
363         if (ctx == NULL) {
364                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
365                 return NULL;
366         }
367
368         current = ctx->devices_list;
369         while (current && i++ < device_index)
370                 current = current->next;
371
372         return current;
373 }
374
375 typedef enum {
376         SCAN_OP_BUILD_DEVLIST,
377         SCAN_OP_OPEN_DEVICE,
378 } scan_op;
379
380 /**
381  * This is where the central logic of multi-device support is.
382  *
383  * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
384  * 'dev' are ignored; the function returns 0 on success and a negative value
385  * on error.
386  *
387  * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
388  * device with index 'open_device_index' and returns the correspondent
389  * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
390  * 1 if the device was already open and a negative value on error.
391  * 
392  * NOTES:
393  * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
394  * the caller might want to call am7xxx_shutdown() in order to remove
395  * devices possibly added before the failure.
396  */
397 static int scan_devices(am7xxx_context *ctx, scan_op op,
398                         unsigned int open_device_index, am7xxx_device **dev)
399 {
400         int num_devices;
401         libusb_device** list;
402         unsigned int current_index;
403         int i;
404         int ret;
405
406         if (ctx == NULL) {
407                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
408                 return -EINVAL;
409         }
410         if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
411                 fprintf(stderr, "%s: device scan done already? Abort!\n", __func__);
412                 return -EINVAL;
413         }
414
415         num_devices = libusb_get_device_list(ctx->usb_context, &list);
416         if (num_devices < 0) {
417                 ret = -ENODEV;
418                 goto out;
419         }
420
421         current_index = 0;
422         for (i = 0; i < num_devices; i++) {
423                 struct libusb_device_descriptor desc;
424                 unsigned int j;
425
426                 ret = libusb_get_device_descriptor(list[i], &desc);
427                 if (ret < 0)
428                         continue;
429
430                 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
431                         if (desc.idVendor == supported_devices[j].vendor_id
432                             && desc.idProduct == supported_devices[j].product_id) {
433
434                                 if (op == SCAN_OP_BUILD_DEVLIST) {
435                                         am7xxx_device *new_device;
436                                         /* debug */
437                                         printf("am7xxx device found, index: %d, name: %s\n",
438                                                current_index,
439                                                supported_devices[j].name);
440                                         new_device = add_new_device(ctx);
441                                         if (new_device == NULL) {
442                                                 /* XXX, the caller may want
443                                                  * to call am7xxx_shutdown() if
444                                                  * we fail here, as we may have
445                                                  * added some devices already
446                                                  */
447                                                 ret = -ENODEV;
448                                                 goto out;
449                                         }
450                                 } else if (op == SCAN_OP_OPEN_DEVICE &&
451                                            current_index == open_device_index) {
452
453                                         *dev = find_device(ctx, open_device_index);
454                                         if (*dev == NULL) {
455                                                 ret = -ENODEV;
456                                                 goto out;
457                                         }
458
459                                         /* the usb device has already been opened */
460                                         if ((*dev)->usb_device) {
461                                                 ret = 1;
462                                                 goto out;
463                                         }
464
465                                         ret = libusb_open(list[i], &((*dev)->usb_device));
466                                         if (ret < 0)
467                                                 goto out;
468
469                                         libusb_set_configuration((*dev)->usb_device, 1);
470                                         libusb_claim_interface((*dev)->usb_device, 0);
471                                         goto out;
472                                 }
473                                 current_index++;
474                         }
475                 }
476         }
477
478         /* if we made it up to here we didn't find any device to open */
479         if (op == SCAN_OP_OPEN_DEVICE) {
480                 ret = -ENODEV;
481                 goto out;
482         }
483
484         /* everything went fine when building the device list */
485         ret = 0;
486 out:
487         libusb_free_device_list(list, 1);
488         return ret;
489 }
490
491 int am7xxx_init(am7xxx_context **ctx)
492 {
493         int ret = 0;
494
495         *ctx = malloc(sizeof(**ctx));
496         if (*ctx == NULL) {
497                 perror("malloc");
498                 ret = -ENOMEM;
499                 goto out;
500         }
501         memset(*ctx, 0, sizeof(**ctx));
502
503         ret = libusb_init(&((*ctx)->usb_context));
504         if (ret < 0)
505                 goto out_free_context;
506
507         libusb_set_debug((*ctx)->usb_context, 3);
508
509         ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
510         if (ret < 0) {
511                 fprintf(stderr, "%s: scan_devices failed\n", __func__);
512                 am7xxx_shutdown(*ctx);
513                 goto out;
514         }
515
516         return 0;
517
518 out_free_context:
519         free(*ctx);
520         *ctx = NULL;
521 out:
522         return ret;
523 }
524
525 void am7xxx_shutdown(am7xxx_context *ctx)
526 {
527         am7xxx_device *current;
528
529         if (ctx == NULL) {
530                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
531                 return;
532         }
533
534         current = ctx->devices_list;
535         while (current) {
536                 am7xxx_device *next = current->next;
537                 am7xxx_close_device(current);
538                 free(current);
539                 current = next;
540         }
541
542         libusb_exit(ctx->usb_context);
543         free(ctx);
544         ctx = NULL;
545 }
546
547 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
548                        unsigned int device_index)
549 {
550         int ret;
551
552         if (ctx == NULL) {
553                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
554                 return -EINVAL;
555         }
556
557         ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
558         if (ret < 0) {
559                 errno = ENODEV;
560         } else if (ret > 0) {
561                 /* warning */
562                 fprintf(stderr, "%s: device %d already open\n", __func__, device_index);
563                 errno = EBUSY;
564                 ret = -EBUSY;
565         }
566
567         return ret;
568 }
569
570 int am7xxx_close_device(am7xxx_device *dev)
571 {
572         if (dev == NULL) {
573                 fprintf(stderr, "%s: dev must not be NULL!\n", __func__);
574                 return -EINVAL;
575         }
576         if (dev->usb_device) {
577                 libusb_release_interface(dev->usb_device, 0);
578                 libusb_close(dev->usb_device);
579                 dev->usb_device = NULL;
580         }
581         return 0;
582 }
583
584 int am7xxx_get_device_info(am7xxx_device *dev,
585                            unsigned int *native_width,
586                            unsigned int *native_height,
587                            unsigned int *unknown0,
588                            unsigned int *unknown1)
589 {
590         int ret;
591         struct am7xxx_header h = {
592                 .packet_type     = AM7XXX_PACKET_TYPE_DEVINFO,
593                 .unknown0        = 0x00,
594                 .header_data_len = 0x00,
595                 .unknown2        = 0x3e,
596                 .unknown3        = 0x10,
597                 .header_data = {
598                         .devinfo = {
599                                 .native_width  = 0,
600                                 .native_height = 0,
601                                 .unknown0      = 0,
602                                 .unknown1      = 0,
603                         },
604                 },
605         };
606
607         ret = send_header(dev, &h);
608         if (ret < 0)
609                 return ret;
610
611         ret = read_header(dev, &h);
612         if (ret < 0)
613                 return ret;
614
615         *native_width = h.header_data.devinfo.native_width;
616         *native_height = h.header_data.devinfo.native_height;
617         *unknown0 = h.header_data.devinfo.unknown0;
618         *unknown1 = h.header_data.devinfo.unknown1;
619
620         return 0;
621 }
622
623 int am7xxx_send_image(am7xxx_device *dev,
624                       am7xxx_image_format format,
625                       unsigned int width,
626                       unsigned int height,
627                       uint8_t *image,
628                       unsigned int size)
629 {
630         int ret;
631         struct am7xxx_header h = {
632                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
633                 .unknown0        = 0x00,
634                 .header_data_len = sizeof(struct am7xxx_image_header),
635                 .unknown2        = 0x3e,
636                 .unknown3        = 0x10,
637                 .header_data = {
638                         .image = {
639                                 .format     = format,
640                                 .width      = width,
641                                 .height     = height,
642                                 .image_size = size,
643                         },
644                 },
645         };
646
647         ret = send_header(dev, &h);
648         if (ret < 0)
649                 return ret;
650
651         if (image == NULL || size == 0)
652                 return 0;
653
654         return send_data(dev, image, size);
655 }
656
657 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
658 {
659         int ret;
660         struct am7xxx_header h = {
661                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
662                 .unknown0        = 0x00,
663                 .header_data_len = sizeof(struct am7xxx_power_header),
664                 .unknown2        = 0x3e,
665                 .unknown3        = 0x10,
666         };
667
668         switch(mode) {
669         case AM7XXX_POWER_OFF:
670                 h.header_data.power.bit2 = 0;
671                 h.header_data.power.bit1 = 0;
672                 h.header_data.power.bit0 = 0;
673                 break;
674
675         case AM7XXX_POWER_LOW:
676                 h.header_data.power.bit2 = 0;
677                 h.header_data.power.bit1 = 0;
678                 h.header_data.power.bit0 = 1;
679
680         case AM7XXX_POWER_MIDDLE:
681                 h.header_data.power.bit2 = 0;
682                 h.header_data.power.bit1 = 1;
683                 h.header_data.power.bit0 = 0;
684                 break;
685
686         case AM7XXX_POWER_HIGH:
687                 h.header_data.power.bit2 = 0;
688                 h.header_data.power.bit1 = 1;
689                 h.header_data.power.bit0 = 1;
690                 break;
691
692         case AM7XXX_POWER_TURBO:
693                 h.header_data.power.bit2 = 1;
694                 h.header_data.power.bit1 = 0;
695                 h.header_data.power.bit0 = 0;
696                 break;
697
698         default:
699                 fprintf(stderr, "Unsupported power mode.\n");
700                 return -EINVAL;
701         };
702
703         ret = send_header(dev, &h);
704         if (ret < 0)
705                 return ret;
706
707         return 0;
708 }