1 /* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs
3 * Copyright (C) 2012 Antonio Ospite <ospite@studenti.unina.it>
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.
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.
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/>.
28 #include "serialize.h"
30 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
32 /* If we're not using GNU C, elide __attribute__
33 * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
36 # define __attribute__(x) /*NOTHING*/
39 /* Control shared library symbols visibility */
40 #if defined _WIN32 || defined __CYGWIN__
41 #define AM7XXX_PUBLIC __declspec(dllexport)
45 #define AM7XXX_PUBLIC __attribute__ ((visibility ("default")))
46 #define AM7XXX_LOCAL __attribute__ ((visibility ("hidden")))
53 static void log_message(am7xxx_context *ctx,
58 ...) __attribute__ ((format (printf, 5, 6)));
60 #define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__)
61 #define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__)
62 #define warning(ctx, ...) log_message(ctx, AM7XXX_LOG_WARNING, __func__, 0, __VA_ARGS__)
63 #define info(ctx, ...) log_message(ctx, AM7XXX_LOG_INFO, __func__, 0, __VA_ARGS__)
64 #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__)
65 #define trace(ctx, ...) log_message(ctx, AM7XXX_LOG_TRACE, NULL, 0, __VA_ARGS__)
67 struct am7xxx_usb_device_descriptor {
73 static struct am7xxx_usb_device_descriptor supported_devices[] = {
80 .name = "Philips/Sagemcom PicoPix 1020",
86 /* The header size on the wire is known to be always 24 bytes, regardless of
87 * the memory configuration enforced by different architectures or compilers
88 * for struct am7xxx_header
90 #define AM7XXX_HEADER_WIRE_SIZE 24
92 struct _am7xxx_device {
93 libusb_device_handle *usb_device;
94 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
99 struct _am7xxx_context {
100 libusb_context *usb_context;
102 am7xxx_device *devices_list;
106 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
107 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
108 AM7XXX_PACKET_TYPE_POWER = 0x04,
109 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
110 } am7xxx_packet_type;
112 struct am7xxx_generic_header {
119 struct am7xxx_devinfo_header {
120 uint32_t native_width;
121 uint32_t native_height;
126 struct am7xxx_image_header {
133 struct am7xxx_power_header {
140 * Examples of packet headers:
143 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
146 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
149 struct am7xxx_header {
150 uint32_t packet_type;
152 uint8_t header_data_len;
156 struct am7xxx_generic_header data;
157 struct am7xxx_devinfo_header devinfo;
158 struct am7xxx_image_header image;
159 struct am7xxx_power_header power;
165 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
167 if (ctx == NULL || d == NULL)
170 debug(ctx, "Info header:\n");
171 debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
172 debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
173 debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
174 debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
177 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
179 if (ctx == NULL || i == NULL)
182 debug(ctx, "Image header:\n");
183 debug(ctx, "\tformat: 0x%08x (%u)\n", i->format, i->format);
184 debug(ctx, "\twidth: 0x%08x (%u)\n", i->width, i->width);
185 debug(ctx, "\theight: 0x%08x (%u)\n", i->height, i->height);
186 debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
189 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
191 if (ctx == NULL || p == NULL)
194 debug(ctx, "Power header:\n");
195 debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
196 debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
197 debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
200 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
202 if (ctx == NULL || h == NULL)
205 debug(ctx, "BEGIN\n");
206 debug(ctx, "packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
207 debug(ctx, "unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
208 debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
209 debug(ctx, "unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
210 debug(ctx, "unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
212 switch(h->packet_type) {
213 case AM7XXX_PACKET_TYPE_DEVINFO:
214 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
217 case AM7XXX_PACKET_TYPE_IMAGE:
218 debug_dump_image_header(ctx, &(h->header_data.image));
221 case AM7XXX_PACKET_TYPE_POWER:
222 debug_dump_power_header(ctx, &(h->header_data.power));
226 debug(ctx, "Packet type not supported!\n");
229 debug(ctx, "END\n\n");
232 static inline unsigned int in_80chars(unsigned int i)
234 /* The 3 below is the length of "xx " where xx is the hex string
235 * representation of a byte */
236 return ((i+1) % (80/3));
239 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
240 uint8_t *buffer, unsigned int len)
244 if (ctx == NULL || buffer == NULL || len == 0)
249 trace(ctx, "%s\n", message);
251 for (i = 0; i < len; i++) {
252 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
257 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
263 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
264 uint8_t *buffer, unsigned int len)
273 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
278 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
279 if (ret != 0 || (unsigned int)transferred != len) {
280 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
281 ret, transferred, len);
285 trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
290 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
295 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
297 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
298 if (ret != 0 || (unsigned int)transferred != len) {
299 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
300 ret, transferred, len);
307 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
309 uint8_t **buffer_iterator = &buffer;
311 put_le32(h->packet_type, buffer_iterator);
312 put_8(h->unknown0, buffer_iterator);
313 put_8(h->header_data_len, buffer_iterator);
314 put_8(h->unknown2, buffer_iterator);
315 put_8(h->unknown3, buffer_iterator);
316 put_le32(h->header_data.data.field0, buffer_iterator);
317 put_le32(h->header_data.data.field1, buffer_iterator);
318 put_le32(h->header_data.data.field2, buffer_iterator);
319 put_le32(h->header_data.data.field3, buffer_iterator);
322 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
324 uint8_t **buffer_iterator = &buffer;
326 h->packet_type = get_le32(buffer_iterator);
327 h->unknown0 = get_8(buffer_iterator);
328 h->header_data_len = get_8(buffer_iterator);
329 h->unknown2 = get_8(buffer_iterator);
330 h->unknown3 = get_8(buffer_iterator);
331 h->header_data.data.field0 = get_le32(buffer_iterator);
332 h->header_data.data.field1 = get_le32(buffer_iterator);
333 h->header_data.data.field2 = get_le32(buffer_iterator);
334 h->header_data.data.field3 = get_le32(buffer_iterator);
337 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
341 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
345 unserialize_header(dev->buffer, h);
347 debug_dump_header(dev->ctx, h);
355 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
359 debug_dump_header(dev->ctx, h);
361 serialize_header(h, dev->buffer);
362 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
364 error(dev->ctx, "failed to send data\n");
369 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
370 * and print the message unconditionally, this makes it possible to print
371 * fatal messages even early on initialization, before the context has been
373 static void log_message(am7xxx_context *ctx,
375 const char *function,
382 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
384 fprintf(stderr, "%s", function);
386 fprintf(stderr, "[%d]", line);
387 fprintf(stderr, ": ");
391 vfprintf(stderr, fmt, ap);
398 static am7xxx_device *add_new_device(am7xxx_context *ctx)
400 am7xxx_device **devices_list;
401 am7xxx_device *new_device;
404 fatal("context must not be NULL!\n");
408 devices_list = &(ctx->devices_list);
410 new_device = malloc(sizeof(*new_device));
411 if (new_device == NULL) {
412 fatal("cannot allocate a new device (%s)\n", strerror(errno));
415 memset(new_device, 0, sizeof(*new_device));
417 new_device->ctx = ctx;
419 if (*devices_list == NULL) {
420 *devices_list = new_device;
422 am7xxx_device *prev = *devices_list;
425 prev->next = new_device;
430 static am7xxx_device *find_device(am7xxx_context *ctx,
431 unsigned int device_index)
434 am7xxx_device *current;
437 fatal("context must not be NULL!\n");
441 current = ctx->devices_list;
442 while (current && i++ < device_index)
443 current = current->next;
449 SCAN_OP_BUILD_DEVLIST,
454 * This is where the central logic of multi-device support is.
456 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
457 * 'dev' are ignored; the function returns 0 on success and a negative value
460 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
461 * device with index 'open_device_index' and returns the correspondent
462 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
463 * 1 if the device was already open and a negative value on error.
466 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
467 * the caller might want to call am7xxx_shutdown() in order to remove
468 * devices possibly added before the failure.
470 static int scan_devices(am7xxx_context *ctx, scan_op op,
471 unsigned int open_device_index, am7xxx_device **dev)
474 libusb_device** list;
475 unsigned int current_index;
480 fatal("context must not be NULL!\n");
483 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
484 error(ctx, "device scan done already? Abort!\n");
488 num_devices = libusb_get_device_list(ctx->usb_context, &list);
489 if (num_devices < 0) {
495 for (i = 0; i < num_devices; i++) {
496 struct libusb_device_descriptor desc;
499 ret = libusb_get_device_descriptor(list[i], &desc);
503 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
504 if (desc.idVendor == supported_devices[j].vendor_id
505 && desc.idProduct == supported_devices[j].product_id) {
507 if (op == SCAN_OP_BUILD_DEVLIST) {
508 am7xxx_device *new_device;
509 info(ctx, "am7xxx device found, index: %d, name: %s\n",
511 supported_devices[j].name);
512 new_device = add_new_device(ctx);
513 if (new_device == NULL) {
514 /* XXX, the caller may want
515 * to call am7xxx_shutdown() if
516 * we fail here, as we may have
517 * added some devices already
519 debug(ctx, "Cannot create a new device\n");
523 } else if (op == SCAN_OP_OPEN_DEVICE &&
524 current_index == open_device_index) {
526 *dev = find_device(ctx, open_device_index);
532 /* the usb device has already been opened */
533 if ((*dev)->usb_device) {
534 debug(ctx, "(*dev)->usb_device already set\n");
539 ret = libusb_open(list[i], &((*dev)->usb_device));
541 debug(ctx, "libusb_open failed\n");
545 libusb_set_configuration((*dev)->usb_device, 1);
546 libusb_claim_interface((*dev)->usb_device, 0);
554 /* if we made it up to here we didn't find any device to open */
555 if (op == SCAN_OP_OPEN_DEVICE) {
556 error(ctx, "Cannot find any device to open\n");
561 /* everything went fine when building the device list */
564 libusb_free_device_list(list, 1);
570 AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
574 *ctx = malloc(sizeof(**ctx));
576 fatal("cannot allocate the context (%s)\n", strerror(errno));
580 memset(*ctx, 0, sizeof(**ctx));
582 /* Set the highest log level during initialization */
583 (*ctx)->log_level = AM7XXX_LOG_TRACE;
585 ret = libusb_init(&((*ctx)->usb_context));
587 goto out_free_context;
589 libusb_set_debug((*ctx)->usb_context, 3);
591 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
593 error(*ctx, "scan_devices() failed\n");
594 am7xxx_shutdown(*ctx);
598 /* Set a quieter log level as default for normal operation */
599 (*ctx)->log_level = AM7XXX_LOG_ERROR;
609 AM7XXX_PUBLIC void am7xxx_shutdown(am7xxx_context *ctx)
611 am7xxx_device *current;
614 fatal("context must not be NULL!\n");
618 current = ctx->devices_list;
620 am7xxx_device *next = current->next;
621 am7xxx_close_device(current);
626 libusb_exit(ctx->usb_context);
631 AM7XXX_PUBLIC void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
633 ctx->log_level = log_level;
636 AM7XXX_PUBLIC int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
637 unsigned int device_index)
642 fatal("context must not be NULL!\n");
646 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
649 } else if (ret > 0) {
650 warning(ctx, "device %d already open\n", device_index);
658 AM7XXX_PUBLIC int am7xxx_close_device(am7xxx_device *dev)
661 fatal("dev must not be NULL!\n");
664 if (dev->usb_device) {
665 libusb_release_interface(dev->usb_device, 0);
666 libusb_close(dev->usb_device);
667 dev->usb_device = NULL;
672 AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
673 am7xxx_device_info *device_info)
676 struct am7xxx_header h = {
677 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
679 .header_data_len = 0x00,
692 ret = send_header(dev, &h);
696 ret = read_header(dev, &h);
700 device_info->native_width = h.header_data.devinfo.native_width;
701 device_info->native_height = h.header_data.devinfo.native_height;
703 /* No reason to expose these in the public API until we know what they mean */
704 device_info->unknown0 = h.header_data.devinfo.unknown0;
705 device_info->unknown1 = h.header_data.devinfo.unknown1;
711 AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
712 unsigned int upscale,
713 unsigned int original_width,
714 unsigned int original_height,
715 unsigned int *scaled_width,
716 unsigned int *scaled_height)
719 am7xxx_device_info device_info;
724 ret = am7xxx_get_device_info(dev, &device_info);
726 error(dev->ctx, "cannot get device info\n");
731 * Check if we need to rescale; if the input image fits the native
732 * dimensions there is no need to, unless we want to upscale.
735 original_width <= device_info.native_width &&
736 original_height <= device_info.native_height ) {
737 debug(dev->ctx, "CASE 0, no rescaling, the original image fits already\n");
738 *scaled_width = original_width;
739 *scaled_height = original_height;
743 /* Input dimensions relative to the device native dimensions */
744 width_ratio = (float)original_width / device_info.native_width;
745 height_ratio = (float)original_height / device_info.native_height;
747 if (width_ratio > height_ratio) {
749 * The input is proportionally "wider" than the device viewport
750 * so its height needs to be adjusted
752 debug(dev->ctx, "CASE 1, original image wider, adjust the scaled height\n");
753 *scaled_width = device_info.native_width;
754 *scaled_height = (unsigned int)lroundf(original_height / width_ratio);
755 } else if (width_ratio < height_ratio) {
757 * The input is proportionally "taller" than the device viewport
758 * so its width needs to be adjusted
760 debug(dev->ctx, "CASE 2 original image taller, adjust the scaled width\n");
761 *scaled_width = (unsigned int)lroundf(original_width / height_ratio);
762 *scaled_height = device_info.native_height;
764 debug(dev->ctx, "CASE 3, just rescale, same aspect ratio already\n");
765 *scaled_width = device_info.native_width;
766 *scaled_height = device_info.native_height;
768 debug(dev->ctx, "scaled dimensions: %dx%d\n", *scaled_width, *scaled_height);
773 AM7XXX_PUBLIC int am7xxx_send_image(am7xxx_device *dev,
774 am7xxx_image_format format,
778 unsigned int image_size)
781 struct am7xxx_header h = {
782 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
784 .header_data_len = sizeof(struct am7xxx_image_header),
792 .image_size = image_size,
797 ret = send_header(dev, &h);
801 if (image == NULL || image_size == 0) {
802 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
806 return send_data(dev, image, image_size);
809 AM7XXX_PUBLIC int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
812 struct am7xxx_header h = {
813 .packet_type = AM7XXX_PACKET_TYPE_POWER,
815 .header_data_len = sizeof(struct am7xxx_power_header),
821 case AM7XXX_POWER_OFF:
822 h.header_data.power.bit2 = 0;
823 h.header_data.power.bit1 = 0;
824 h.header_data.power.bit0 = 0;
827 case AM7XXX_POWER_LOW:
828 h.header_data.power.bit2 = 0;
829 h.header_data.power.bit1 = 0;
830 h.header_data.power.bit0 = 1;
832 case AM7XXX_POWER_MIDDLE:
833 h.header_data.power.bit2 = 0;
834 h.header_data.power.bit1 = 1;
835 h.header_data.power.bit0 = 0;
838 case AM7XXX_POWER_HIGH:
839 h.header_data.power.bit2 = 0;
840 h.header_data.power.bit1 = 1;
841 h.header_data.power.bit0 = 1;
844 case AM7XXX_POWER_TURBO:
845 h.header_data.power.bit2 = 1;
846 h.header_data.power.bit1 = 0;
847 h.header_data.power.bit0 = 0;
851 error(dev->ctx, "Power mode not supported!\n");
855 ret = send_header(dev, &h);