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 ="Aiptek PocketCinema T25",
90 .name = "Philips/Sagemcom PicoPix 1020",
96 /* The header size on the wire is known to be always 24 bytes, regardless of
97 * the memory configuration enforced by different architectures or compilers
98 * for struct am7xxx_header
100 #define AM7XXX_HEADER_WIRE_SIZE 24
102 struct _am7xxx_device {
103 libusb_device_handle *usb_device;
104 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
109 struct _am7xxx_context {
110 libusb_context *usb_context;
112 am7xxx_device *devices_list;
116 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
117 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
118 AM7XXX_PACKET_TYPE_POWER = 0x04,
119 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
120 } am7xxx_packet_type;
122 struct am7xxx_generic_header {
129 struct am7xxx_devinfo_header {
130 uint32_t native_width;
131 uint32_t native_height;
136 struct am7xxx_image_header {
143 struct am7xxx_power_header {
150 * Examples of packet headers:
153 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
156 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
159 /* Direction of the communication from the host point of view */
160 #define AM7XXX_DIRECTION_OUT 0 /* host -> device */
161 #define AM7XXX_DIRECTION_IN 1 /* host <- device */
163 struct am7xxx_header {
164 uint32_t packet_type;
166 uint8_t header_data_len;
170 struct am7xxx_generic_header data;
171 struct am7xxx_devinfo_header devinfo;
172 struct am7xxx_image_header image;
173 struct am7xxx_power_header power;
179 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
181 if (ctx == NULL || d == NULL)
184 debug(ctx, "Info header:\n");
185 debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
186 debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
187 debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
188 debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
191 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
193 if (ctx == NULL || i == NULL)
196 debug(ctx, "Image header:\n");
197 debug(ctx, "\tformat: 0x%08x (%u)\n", i->format, i->format);
198 debug(ctx, "\twidth: 0x%08x (%u)\n", i->width, i->width);
199 debug(ctx, "\theight: 0x%08x (%u)\n", i->height, i->height);
200 debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
203 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
205 if (ctx == NULL || p == NULL)
208 debug(ctx, "Power header:\n");
209 debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
210 debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
211 debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
214 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
216 if (ctx == NULL || h == NULL)
219 debug(ctx, "BEGIN\n");
220 debug(ctx, "packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
221 debug(ctx, "direction: 0x%02hhx (%hhu)\n", h->direction, h->direction);
222 debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
223 debug(ctx, "unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
224 debug(ctx, "unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
226 switch(h->packet_type) {
227 case AM7XXX_PACKET_TYPE_DEVINFO:
228 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
231 case AM7XXX_PACKET_TYPE_IMAGE:
232 debug_dump_image_header(ctx, &(h->header_data.image));
235 case AM7XXX_PACKET_TYPE_POWER:
236 debug_dump_power_header(ctx, &(h->header_data.power));
240 debug(ctx, "Packet type not supported!\n");
243 debug(ctx, "END\n\n");
246 static inline unsigned int in_80chars(unsigned int i)
248 /* The 3 below is the length of "xx " where xx is the hex string
249 * representation of a byte */
250 return ((i+1) % (80/3));
253 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
254 uint8_t *buffer, unsigned int len)
258 if (ctx == NULL || buffer == NULL || len == 0)
263 trace(ctx, "%s\n", message);
265 for (i = 0; i < len; i++) {
266 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
271 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
277 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
278 uint8_t *buffer, unsigned int len)
287 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
292 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
293 if (ret != 0 || (unsigned int)transferred != len) {
294 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
295 ret, transferred, len);
299 trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
304 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
309 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
311 ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
312 if (ret != 0 || (unsigned int)transferred != len) {
313 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
314 ret, transferred, len);
321 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
323 uint8_t **buffer_iterator = &buffer;
325 put_le32(h->packet_type, buffer_iterator);
326 put_8(h->direction, buffer_iterator);
327 put_8(h->header_data_len, buffer_iterator);
328 put_8(h->unknown2, buffer_iterator);
329 put_8(h->unknown3, buffer_iterator);
330 put_le32(h->header_data.data.field0, buffer_iterator);
331 put_le32(h->header_data.data.field1, buffer_iterator);
332 put_le32(h->header_data.data.field2, buffer_iterator);
333 put_le32(h->header_data.data.field3, buffer_iterator);
336 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
338 uint8_t **buffer_iterator = &buffer;
340 h->packet_type = get_le32(buffer_iterator);
341 h->direction = get_8(buffer_iterator);
342 h->header_data_len = get_8(buffer_iterator);
343 h->unknown2 = get_8(buffer_iterator);
344 h->unknown3 = get_8(buffer_iterator);
345 h->header_data.data.field0 = get_le32(buffer_iterator);
346 h->header_data.data.field1 = get_le32(buffer_iterator);
347 h->header_data.data.field2 = get_le32(buffer_iterator);
348 h->header_data.data.field3 = get_le32(buffer_iterator);
351 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
355 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
359 unserialize_header(dev->buffer, h);
361 debug_dump_header(dev->ctx, h);
363 if (h->direction == AM7XXX_DIRECTION_IN) {
367 "Received a packet with direction AM7XXX_DIRECTION_OUT, weird!\n");
375 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
379 debug_dump_header(dev->ctx, h);
381 serialize_header(h, dev->buffer);
382 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
384 error(dev->ctx, "failed to send data\n");
389 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
390 * and print the message unconditionally, this makes it possible to print
391 * fatal messages even early on initialization, before the context has been
393 static void log_message(am7xxx_context *ctx,
395 const char *function,
402 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
404 fprintf(stderr, "%s", function);
406 fprintf(stderr, "[%d]", line);
407 fprintf(stderr, ": ");
411 vfprintf(stderr, fmt, ap);
418 static am7xxx_device *add_new_device(am7xxx_context *ctx)
420 am7xxx_device **devices_list;
421 am7xxx_device *new_device;
424 fatal("context must not be NULL!\n");
428 devices_list = &(ctx->devices_list);
430 new_device = malloc(sizeof(*new_device));
431 if (new_device == NULL) {
432 fatal("cannot allocate a new device (%s)\n", strerror(errno));
435 memset(new_device, 0, sizeof(*new_device));
437 new_device->ctx = ctx;
439 if (*devices_list == NULL) {
440 *devices_list = new_device;
442 am7xxx_device *prev = *devices_list;
445 prev->next = new_device;
450 static am7xxx_device *find_device(am7xxx_context *ctx,
451 unsigned int device_index)
454 am7xxx_device *current;
457 fatal("context must not be NULL!\n");
461 current = ctx->devices_list;
462 while (current && i++ < device_index)
463 current = current->next;
469 SCAN_OP_BUILD_DEVLIST,
474 * This is where the central logic of multi-device support is.
476 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
477 * 'dev' are ignored; the function returns 0 on success and a negative value
480 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
481 * device with index 'open_device_index' and returns the correspondent
482 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
483 * 1 if the device was already open and a negative value on error.
486 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
487 * the caller might want to call am7xxx_shutdown() in order to remove
488 * devices possibly added before the failure.
490 static int scan_devices(am7xxx_context *ctx, scan_op op,
491 unsigned int open_device_index, am7xxx_device **dev)
494 libusb_device** list;
495 unsigned int current_index;
500 fatal("context must not be NULL!\n");
503 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
504 error(ctx, "device scan done already? Abort!\n");
508 num_devices = libusb_get_device_list(ctx->usb_context, &list);
509 if (num_devices < 0) {
515 for (i = 0; i < num_devices; i++) {
516 struct libusb_device_descriptor desc;
519 ret = libusb_get_device_descriptor(list[i], &desc);
523 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
524 if (desc.idVendor == supported_devices[j].vendor_id
525 && desc.idProduct == supported_devices[j].product_id) {
527 if (op == SCAN_OP_BUILD_DEVLIST) {
528 am7xxx_device *new_device;
529 info(ctx, "am7xxx device found, index: %d, name: %s\n",
531 supported_devices[j].name);
532 new_device = add_new_device(ctx);
533 if (new_device == NULL) {
534 /* XXX, the caller may want
535 * to call am7xxx_shutdown() if
536 * we fail here, as we may have
537 * added some devices already
539 debug(ctx, "Cannot create a new device\n");
543 } else if (op == SCAN_OP_OPEN_DEVICE &&
544 current_index == open_device_index) {
546 *dev = find_device(ctx, open_device_index);
552 /* the usb device has already been opened */
553 if ((*dev)->usb_device) {
554 debug(ctx, "(*dev)->usb_device already set\n");
559 ret = libusb_open(list[i], &((*dev)->usb_device));
561 debug(ctx, "libusb_open failed\n");
565 libusb_set_configuration((*dev)->usb_device, 2);
566 libusb_claim_interface((*dev)->usb_device, 0);
574 /* if we made it up to here we didn't find any device to open */
575 if (op == SCAN_OP_OPEN_DEVICE) {
576 error(ctx, "Cannot find any device to open\n");
581 /* everything went fine when building the device list */
584 libusb_free_device_list(list, 1);
590 AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
594 *ctx = malloc(sizeof(**ctx));
596 fatal("cannot allocate the context (%s)\n", strerror(errno));
600 memset(*ctx, 0, sizeof(**ctx));
602 /* Set the highest log level during initialization */
603 (*ctx)->log_level = AM7XXX_LOG_TRACE;
605 ret = libusb_init(&((*ctx)->usb_context));
607 goto out_free_context;
609 libusb_set_debug((*ctx)->usb_context, 3);
611 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
613 error(*ctx, "scan_devices() failed\n");
614 am7xxx_shutdown(*ctx);
618 /* Set a quieter log level as default for normal operation */
619 (*ctx)->log_level = AM7XXX_LOG_ERROR;
629 AM7XXX_PUBLIC void am7xxx_shutdown(am7xxx_context *ctx)
631 am7xxx_device *current;
634 fatal("context must not be NULL!\n");
638 current = ctx->devices_list;
640 am7xxx_device *next = current->next;
641 am7xxx_close_device(current);
646 libusb_exit(ctx->usb_context);
651 AM7XXX_PUBLIC void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
653 ctx->log_level = log_level;
656 AM7XXX_PUBLIC int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
657 unsigned int device_index)
662 fatal("context must not be NULL!\n");
666 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
669 } else if (ret > 0) {
670 warning(ctx, "device %d already open\n", device_index);
678 AM7XXX_PUBLIC int am7xxx_close_device(am7xxx_device *dev)
681 fatal("dev must not be NULL!\n");
684 if (dev->usb_device) {
685 libusb_release_interface(dev->usb_device, 0);
686 libusb_close(dev->usb_device);
687 dev->usb_device = NULL;
692 AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
693 am7xxx_device_info *device_info)
696 struct am7xxx_header h = {
697 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
698 .direction = AM7XXX_DIRECTION_OUT,
699 .header_data_len = 0x00,
712 ret = send_header(dev, &h);
716 ret = read_header(dev, &h);
720 device_info->native_width = h.header_data.devinfo.native_width;
721 device_info->native_height = h.header_data.devinfo.native_height;
723 /* No reason to expose these in the public API until we know what they mean */
724 device_info->unknown0 = h.header_data.devinfo.unknown0;
725 device_info->unknown1 = h.header_data.devinfo.unknown1;
731 AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
732 unsigned int upscale,
733 unsigned int original_width,
734 unsigned int original_height,
735 unsigned int *scaled_width,
736 unsigned int *scaled_height)
739 am7xxx_device_info device_info;
744 ret = am7xxx_get_device_info(dev, &device_info);
746 error(dev->ctx, "cannot get device info\n");
751 * Check if we need to rescale; if the input image fits the native
752 * dimensions there is no need to, unless we want to upscale.
755 original_width <= device_info.native_width &&
756 original_height <= device_info.native_height ) {
757 debug(dev->ctx, "CASE 0, no rescaling, the original image fits already\n");
758 *scaled_width = original_width;
759 *scaled_height = original_height;
763 /* Input dimensions relative to the device native dimensions */
764 width_ratio = (float)original_width / device_info.native_width;
765 height_ratio = (float)original_height / device_info.native_height;
767 if (width_ratio > height_ratio) {
769 * The input is proportionally "wider" than the device viewport
770 * so its height needs to be adjusted
772 debug(dev->ctx, "CASE 1, original image wider, adjust the scaled height\n");
773 *scaled_width = device_info.native_width;
774 *scaled_height = (unsigned int)lroundf(original_height / width_ratio);
775 } else if (width_ratio < height_ratio) {
777 * The input is proportionally "taller" than the device viewport
778 * so its width needs to be adjusted
780 debug(dev->ctx, "CASE 2 original image taller, adjust the scaled width\n");
781 *scaled_width = (unsigned int)lroundf(original_width / height_ratio);
782 *scaled_height = device_info.native_height;
784 debug(dev->ctx, "CASE 3, just rescale, same aspect ratio already\n");
785 *scaled_width = device_info.native_width;
786 *scaled_height = device_info.native_height;
788 debug(dev->ctx, "scaled dimensions: %dx%d\n", *scaled_width, *scaled_height);
793 AM7XXX_PUBLIC int am7xxx_send_image(am7xxx_device *dev,
794 am7xxx_image_format format,
798 unsigned int image_size)
801 struct am7xxx_header h = {
802 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
803 .direction = AM7XXX_DIRECTION_OUT,
804 .header_data_len = sizeof(struct am7xxx_image_header),
812 .image_size = image_size,
817 ret = send_header(dev, &h);
821 if (image == NULL || image_size == 0) {
822 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
826 return send_data(dev, image, image_size);
829 AM7XXX_PUBLIC int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
832 struct am7xxx_header h = {
833 .packet_type = AM7XXX_PACKET_TYPE_POWER,
834 .direction = AM7XXX_DIRECTION_OUT,
835 .header_data_len = sizeof(struct am7xxx_power_header),
841 case AM7XXX_POWER_OFF:
842 h.header_data.power.bit2 = 0;
843 h.header_data.power.bit1 = 0;
844 h.header_data.power.bit0 = 0;
847 case AM7XXX_POWER_LOW:
848 h.header_data.power.bit2 = 0;
849 h.header_data.power.bit1 = 0;
850 h.header_data.power.bit0 = 1;
853 case AM7XXX_POWER_MIDDLE:
854 h.header_data.power.bit2 = 0;
855 h.header_data.power.bit1 = 1;
856 h.header_data.power.bit0 = 0;
859 case AM7XXX_POWER_HIGH:
860 h.header_data.power.bit2 = 0;
861 h.header_data.power.bit1 = 1;
862 h.header_data.power.bit0 = 1;
865 case AM7XXX_POWER_TURBO:
866 h.header_data.power.bit2 = 1;
867 h.header_data.power.bit1 = 0;
868 h.header_data.power.bit0 = 0;
872 error(dev->ctx, "Power mode not supported!\n");
876 ret = send_header(dev, &h);