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[] = {
85 .name = "Philips/Sagemcom PicoPix 1020",
91 /* The header size on the wire is known to be always 24 bytes, regardless of
92 * the memory configuration enforced by different architectures or compilers
93 * for struct am7xxx_header
95 #define AM7XXX_HEADER_WIRE_SIZE 24
97 struct _am7xxx_device {
98 libusb_device_handle *usb_device;
99 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
104 struct _am7xxx_context {
105 libusb_context *usb_context;
107 am7xxx_device *devices_list;
111 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
112 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
113 AM7XXX_PACKET_TYPE_POWER = 0x04,
114 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
115 } am7xxx_packet_type;
117 struct am7xxx_generic_header {
124 struct am7xxx_devinfo_header {
125 uint32_t native_width;
126 uint32_t native_height;
131 struct am7xxx_image_header {
138 struct am7xxx_power_header {
145 * Examples of packet headers:
148 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
151 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
154 struct am7xxx_header {
155 uint32_t packet_type;
157 uint8_t header_data_len;
161 struct am7xxx_generic_header data;
162 struct am7xxx_devinfo_header devinfo;
163 struct am7xxx_image_header image;
164 struct am7xxx_power_header power;
170 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
172 if (ctx == NULL || d == NULL)
175 debug(ctx, "Info header:\n");
176 debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
177 debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
178 debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
179 debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
182 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
184 if (ctx == NULL || i == NULL)
187 debug(ctx, "Image header:\n");
188 debug(ctx, "\tformat: 0x%08x (%u)\n", i->format, i->format);
189 debug(ctx, "\twidth: 0x%08x (%u)\n", i->width, i->width);
190 debug(ctx, "\theight: 0x%08x (%u)\n", i->height, i->height);
191 debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
194 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
196 if (ctx == NULL || p == NULL)
199 debug(ctx, "Power header:\n");
200 debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
201 debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
202 debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
205 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
207 if (ctx == NULL || h == NULL)
210 debug(ctx, "BEGIN\n");
211 debug(ctx, "packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
212 debug(ctx, "unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
213 debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
214 debug(ctx, "unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
215 debug(ctx, "unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
217 switch(h->packet_type) {
218 case AM7XXX_PACKET_TYPE_DEVINFO:
219 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
222 case AM7XXX_PACKET_TYPE_IMAGE:
223 debug_dump_image_header(ctx, &(h->header_data.image));
226 case AM7XXX_PACKET_TYPE_POWER:
227 debug_dump_power_header(ctx, &(h->header_data.power));
231 debug(ctx, "Packet type not supported!\n");
234 debug(ctx, "END\n\n");
237 static inline unsigned int in_80chars(unsigned int i)
239 /* The 3 below is the length of "xx " where xx is the hex string
240 * representation of a byte */
241 return ((i+1) % (80/3));
244 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
245 uint8_t *buffer, unsigned int len)
249 if (ctx == NULL || buffer == NULL || len == 0)
254 trace(ctx, "%s\n", message);
256 for (i = 0; i < len; i++) {
257 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
262 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
268 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
269 uint8_t *buffer, unsigned int len)
278 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
283 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
284 if (ret != 0 || (unsigned int)transferred != len) {
285 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
286 ret, transferred, len);
290 trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
295 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
300 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
302 ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
303 if (ret != 0 || (unsigned int)transferred != len) {
304 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
305 ret, transferred, len);
312 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
314 uint8_t **buffer_iterator = &buffer;
316 put_le32(h->packet_type, buffer_iterator);
317 put_8(h->unknown0, buffer_iterator);
318 put_8(h->header_data_len, buffer_iterator);
319 put_8(h->unknown2, buffer_iterator);
320 put_8(h->unknown3, buffer_iterator);
321 put_le32(h->header_data.data.field0, buffer_iterator);
322 put_le32(h->header_data.data.field1, buffer_iterator);
323 put_le32(h->header_data.data.field2, buffer_iterator);
324 put_le32(h->header_data.data.field3, buffer_iterator);
327 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
329 uint8_t **buffer_iterator = &buffer;
331 h->packet_type = get_le32(buffer_iterator);
332 h->unknown0 = get_8(buffer_iterator);
333 h->header_data_len = get_8(buffer_iterator);
334 h->unknown2 = get_8(buffer_iterator);
335 h->unknown3 = get_8(buffer_iterator);
336 h->header_data.data.field0 = get_le32(buffer_iterator);
337 h->header_data.data.field1 = get_le32(buffer_iterator);
338 h->header_data.data.field2 = get_le32(buffer_iterator);
339 h->header_data.data.field3 = get_le32(buffer_iterator);
342 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
346 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
350 unserialize_header(dev->buffer, h);
352 debug_dump_header(dev->ctx, h);
360 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
364 debug_dump_header(dev->ctx, h);
366 serialize_header(h, dev->buffer);
367 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
369 error(dev->ctx, "failed to send data\n");
374 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
375 * and print the message unconditionally, this makes it possible to print
376 * fatal messages even early on initialization, before the context has been
378 static void log_message(am7xxx_context *ctx,
380 const char *function,
387 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
389 fprintf(stderr, "%s", function);
391 fprintf(stderr, "[%d]", line);
392 fprintf(stderr, ": ");
396 vfprintf(stderr, fmt, ap);
403 static am7xxx_device *add_new_device(am7xxx_context *ctx)
405 am7xxx_device **devices_list;
406 am7xxx_device *new_device;
409 fatal("context must not be NULL!\n");
413 devices_list = &(ctx->devices_list);
415 new_device = malloc(sizeof(*new_device));
416 if (new_device == NULL) {
417 fatal("cannot allocate a new device (%s)\n", strerror(errno));
420 memset(new_device, 0, sizeof(*new_device));
422 new_device->ctx = ctx;
424 if (*devices_list == NULL) {
425 *devices_list = new_device;
427 am7xxx_device *prev = *devices_list;
430 prev->next = new_device;
435 static am7xxx_device *find_device(am7xxx_context *ctx,
436 unsigned int device_index)
439 am7xxx_device *current;
442 fatal("context must not be NULL!\n");
446 current = ctx->devices_list;
447 while (current && i++ < device_index)
448 current = current->next;
454 SCAN_OP_BUILD_DEVLIST,
459 * This is where the central logic of multi-device support is.
461 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
462 * 'dev' are ignored; the function returns 0 on success and a negative value
465 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
466 * device with index 'open_device_index' and returns the correspondent
467 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
468 * 1 if the device was already open and a negative value on error.
471 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
472 * the caller might want to call am7xxx_shutdown() in order to remove
473 * devices possibly added before the failure.
475 static int scan_devices(am7xxx_context *ctx, scan_op op,
476 unsigned int open_device_index, am7xxx_device **dev)
479 libusb_device** list;
480 unsigned int current_index;
485 fatal("context must not be NULL!\n");
488 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
489 error(ctx, "device scan done already? Abort!\n");
493 num_devices = libusb_get_device_list(ctx->usb_context, &list);
494 if (num_devices < 0) {
500 for (i = 0; i < num_devices; i++) {
501 struct libusb_device_descriptor desc;
504 ret = libusb_get_device_descriptor(list[i], &desc);
508 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
509 if (desc.idVendor == supported_devices[j].vendor_id
510 && desc.idProduct == supported_devices[j].product_id) {
512 if (op == SCAN_OP_BUILD_DEVLIST) {
513 am7xxx_device *new_device;
514 info(ctx, "am7xxx device found, index: %d, name: %s\n",
516 supported_devices[j].name);
517 new_device = add_new_device(ctx);
518 if (new_device == NULL) {
519 /* XXX, the caller may want
520 * to call am7xxx_shutdown() if
521 * we fail here, as we may have
522 * added some devices already
524 debug(ctx, "Cannot create a new device\n");
528 } else if (op == SCAN_OP_OPEN_DEVICE &&
529 current_index == open_device_index) {
531 *dev = find_device(ctx, open_device_index);
537 /* the usb device has already been opened */
538 if ((*dev)->usb_device) {
539 debug(ctx, "(*dev)->usb_device already set\n");
544 ret = libusb_open(list[i], &((*dev)->usb_device));
546 debug(ctx, "libusb_open failed\n");
550 libusb_set_configuration((*dev)->usb_device, 2);
551 libusb_claim_interface((*dev)->usb_device, 0);
559 /* if we made it up to here we didn't find any device to open */
560 if (op == SCAN_OP_OPEN_DEVICE) {
561 error(ctx, "Cannot find any device to open\n");
566 /* everything went fine when building the device list */
569 libusb_free_device_list(list, 1);
575 AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
579 *ctx = malloc(sizeof(**ctx));
581 fatal("cannot allocate the context (%s)\n", strerror(errno));
585 memset(*ctx, 0, sizeof(**ctx));
587 /* Set the highest log level during initialization */
588 (*ctx)->log_level = AM7XXX_LOG_TRACE;
590 ret = libusb_init(&((*ctx)->usb_context));
592 goto out_free_context;
594 libusb_set_debug((*ctx)->usb_context, 3);
596 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
598 error(*ctx, "scan_devices() failed\n");
599 am7xxx_shutdown(*ctx);
603 /* Set a quieter log level as default for normal operation */
604 (*ctx)->log_level = AM7XXX_LOG_ERROR;
614 AM7XXX_PUBLIC void am7xxx_shutdown(am7xxx_context *ctx)
616 am7xxx_device *current;
619 fatal("context must not be NULL!\n");
623 current = ctx->devices_list;
625 am7xxx_device *next = current->next;
626 am7xxx_close_device(current);
631 libusb_exit(ctx->usb_context);
636 AM7XXX_PUBLIC void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
638 ctx->log_level = log_level;
641 AM7XXX_PUBLIC int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
642 unsigned int device_index)
647 fatal("context must not be NULL!\n");
651 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
654 } else if (ret > 0) {
655 warning(ctx, "device %d already open\n", device_index);
663 AM7XXX_PUBLIC int am7xxx_close_device(am7xxx_device *dev)
666 fatal("dev must not be NULL!\n");
669 if (dev->usb_device) {
670 libusb_release_interface(dev->usb_device, 0);
671 libusb_close(dev->usb_device);
672 dev->usb_device = NULL;
677 AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
678 am7xxx_device_info *device_info)
681 struct am7xxx_header h = {
682 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
684 .header_data_len = 0x00,
697 ret = send_header(dev, &h);
701 ret = read_header(dev, &h);
705 device_info->native_width = h.header_data.devinfo.native_width;
706 device_info->native_height = h.header_data.devinfo.native_height;
708 /* No reason to expose these in the public API until we know what they mean */
709 device_info->unknown0 = h.header_data.devinfo.unknown0;
710 device_info->unknown1 = h.header_data.devinfo.unknown1;
716 AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
717 unsigned int upscale,
718 unsigned int original_width,
719 unsigned int original_height,
720 unsigned int *scaled_width,
721 unsigned int *scaled_height)
724 am7xxx_device_info device_info;
729 ret = am7xxx_get_device_info(dev, &device_info);
731 error(dev->ctx, "cannot get device info\n");
736 * Check if we need to rescale; if the input image fits the native
737 * dimensions there is no need to, unless we want to upscale.
740 original_width <= device_info.native_width &&
741 original_height <= device_info.native_height ) {
742 debug(dev->ctx, "CASE 0, no rescaling, the original image fits already\n");
743 *scaled_width = original_width;
744 *scaled_height = original_height;
748 /* Input dimensions relative to the device native dimensions */
749 width_ratio = (float)original_width / device_info.native_width;
750 height_ratio = (float)original_height / device_info.native_height;
752 if (width_ratio > height_ratio) {
754 * The input is proportionally "wider" than the device viewport
755 * so its height needs to be adjusted
757 debug(dev->ctx, "CASE 1, original image wider, adjust the scaled height\n");
758 *scaled_width = device_info.native_width;
759 *scaled_height = (unsigned int)lroundf(original_height / width_ratio);
760 } else if (width_ratio < height_ratio) {
762 * The input is proportionally "taller" than the device viewport
763 * so its width needs to be adjusted
765 debug(dev->ctx, "CASE 2 original image taller, adjust the scaled width\n");
766 *scaled_width = (unsigned int)lroundf(original_width / height_ratio);
767 *scaled_height = device_info.native_height;
769 debug(dev->ctx, "CASE 3, just rescale, same aspect ratio already\n");
770 *scaled_width = device_info.native_width;
771 *scaled_height = device_info.native_height;
773 debug(dev->ctx, "scaled dimensions: %dx%d\n", *scaled_width, *scaled_height);
778 AM7XXX_PUBLIC int am7xxx_send_image(am7xxx_device *dev,
779 am7xxx_image_format format,
783 unsigned int image_size)
786 struct am7xxx_header h = {
787 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
789 .header_data_len = sizeof(struct am7xxx_image_header),
797 .image_size = image_size,
802 ret = send_header(dev, &h);
806 if (image == NULL || image_size == 0) {
807 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
811 return send_data(dev, image, image_size);
814 AM7XXX_PUBLIC int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
817 struct am7xxx_header h = {
818 .packet_type = AM7XXX_PACKET_TYPE_POWER,
820 .header_data_len = sizeof(struct am7xxx_power_header),
826 case AM7XXX_POWER_OFF:
827 h.header_data.power.bit2 = 0;
828 h.header_data.power.bit1 = 0;
829 h.header_data.power.bit0 = 0;
832 case AM7XXX_POWER_LOW:
833 h.header_data.power.bit2 = 0;
834 h.header_data.power.bit1 = 0;
835 h.header_data.power.bit0 = 1;
837 case AM7XXX_POWER_MIDDLE:
838 h.header_data.power.bit2 = 0;
839 h.header_data.power.bit1 = 1;
840 h.header_data.power.bit0 = 0;
843 case AM7XXX_POWER_HIGH:
844 h.header_data.power.bit2 = 0;
845 h.header_data.power.bit1 = 1;
846 h.header_data.power.bit0 = 1;
849 case AM7XXX_POWER_TURBO:
850 h.header_data.power.bit2 = 1;
851 h.header_data.power.bit1 = 0;
852 h.header_data.power.bit0 = 0;
856 error(dev->ctx, "Power mode not supported!\n");
860 ret = send_header(dev, &h);