1 /* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs
3 * Copyright (C) 2012-2014 Antonio Ospite <ao2@ao2.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"
31 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
34 * If we're not using GNU C, elide __attribute__
35 * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
38 #define __attribute__(x) /* NOTHING */
41 /* Control shared library symbols visibility */
42 #if defined _WIN32 || defined __CYGWIN__
43 #define AM7XXX_PUBLIC __declspec(dllexport)
47 #define AM7XXX_PUBLIC __attribute__ ((visibility ("default")))
48 #define AM7XXX_LOCAL __attribute__ ((visibility ("hidden")))
55 static void log_message(am7xxx_context *ctx,
57 const char *function_name,
60 ...) __attribute__ ((format (printf, 5, 6)));
62 #define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__)
63 #define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__)
64 #define warning(ctx, ...) log_message(ctx, AM7XXX_LOG_WARNING, __func__, 0, __VA_ARGS__)
65 #define info(ctx, ...) log_message(ctx, AM7XXX_LOG_INFO, __func__, 0, __VA_ARGS__)
66 #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__)
67 #define trace(ctx, ...) log_message(ctx, AM7XXX_LOG_TRACE, NULL, 0, __VA_ARGS__)
70 int (*set_power_mode)(am7xxx_device *dev, am7xxx_power_mode power);
71 int (*set_zoom_mode)(am7xxx_device *dev, am7xxx_zoom_mode zoom);
74 struct am7xxx_usb_device_descriptor {
78 uint8_t configuration; /* The bConfigurationValue of the device */
79 uint8_t interface_number; /* The bInterfaceNumber of the device */
80 struct am7xxx_ops ops;
83 static int default_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power);
84 static int picopix_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power);
85 static int default_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom);
86 static int picopix_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom);
88 #define DEFAULT_OPS { \
89 .set_power_mode = default_set_power_mode, \
90 .set_zoom_mode = default_set_zoom_mode, \
93 static const struct am7xxx_usb_device_descriptor supported_devices[] = {
99 .interface_number = 0,
105 .product_id = 0x5501,
107 .interface_number = 0,
111 .name ="Aiptek PocketCinema T25",
113 .product_id = 0x2144,
115 .interface_number = 0,
119 .name = "Philips/Sagemcom PicoPix 1020",
121 .product_id = 0x000e,
123 .interface_number = 0,
127 .name = "Philips/Sagemcom PicoPix 2055",
129 .product_id = 0x0016,
131 .interface_number = 0,
133 .set_power_mode = picopix_set_power_mode,
134 .set_zoom_mode = picopix_set_zoom_mode,
138 .name = "Philips/Sagemcom PicoPix 2330",
140 .product_id = 0x0019,
142 .interface_number = 0,
146 /* The header size on the wire is known to be always 24 bytes, regardless of
147 * the memory configuration enforced by different architectures or compilers
148 * for struct am7xxx_header
150 #define AM7XXX_HEADER_WIRE_SIZE 24
152 struct _am7xxx_device {
153 libusb_device_handle *usb_device;
154 struct libusb_transfer *transfer;
155 int transfer_completed;
156 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
157 am7xxx_device_info *device_info;
159 const struct am7xxx_usb_device_descriptor *desc;
163 struct _am7xxx_context {
164 libusb_context *usb_context;
166 am7xxx_device *devices_list;
170 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
171 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
172 AM7XXX_PACKET_TYPE_POWER = 0x04,
173 AM7XXX_PACKET_TYPE_ZOOM = 0x05,
174 AM7XXX_PACKET_TYPE_PICOPIX_POWER_LOW = 0x15,
175 AM7XXX_PACKET_TYPE_PICOPIX_POWER_MEDIUM = 0x16,
176 AM7XXX_PACKET_TYPE_PICOPIX_POWER_HIGH = 0x17,
177 AM7XXX_PACKET_TYPE_PICOPIX_ENABLE_TI = 0x18,
178 AM7XXX_PACKET_TYPE_PICOPIX_DISABLE_TI = 0x19,
179 } am7xxx_packet_type;
181 struct am7xxx_generic_header {
188 struct am7xxx_devinfo_header {
189 uint32_t native_width;
190 uint32_t native_height;
195 struct am7xxx_image_header {
202 struct am7xxx_power_header {
208 struct am7xxx_zoom_header {
214 * Examples of packet headers:
217 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
220 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
223 /* Direction of the communication from the host point of view */
224 #define AM7XXX_DIRECTION_OUT 0 /* host -> device */
225 #define AM7XXX_DIRECTION_IN 1 /* host <- device */
227 struct am7xxx_header {
228 uint32_t packet_type;
230 uint8_t header_data_len;
234 struct am7xxx_generic_header data;
235 struct am7xxx_devinfo_header devinfo;
236 struct am7xxx_image_header image;
237 struct am7xxx_power_header power;
238 struct am7xxx_zoom_header zoom;
244 static void debug_dump_generic_header(am7xxx_context *ctx, struct am7xxx_generic_header *g)
246 if (ctx == NULL || g == NULL)
249 debug(ctx, "Generic header:\n");
250 debug(ctx, "\tfield0: 0x%08x (%u)\n", g->field0, g->field0);
251 debug(ctx, "\tfield1: 0x%08x (%u)\n", g->field1, g->field1);
252 debug(ctx, "\tfield2: 0x%08x (%u)\n", g->field2, g->field2);
253 debug(ctx, "\tfield3: 0x%08x (%u)\n", g->field3, g->field3);
256 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
258 if (ctx == NULL || d == NULL)
261 debug(ctx, "Info header:\n");
262 debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
263 debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
264 debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
265 debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
268 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
270 if (ctx == NULL || i == NULL)
273 debug(ctx, "Image header:\n");
274 debug(ctx, "\tformat: 0x%08x (%u)\n", i->format, i->format);
275 debug(ctx, "\twidth: 0x%08x (%u)\n", i->width, i->width);
276 debug(ctx, "\theight: 0x%08x (%u)\n", i->height, i->height);
277 debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
280 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
282 if (ctx == NULL || p == NULL)
285 debug(ctx, "Power header:\n");
286 debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
287 debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
288 debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
291 static void debug_dump_zoom_header(am7xxx_context *ctx, struct am7xxx_zoom_header *z)
293 if (ctx == NULL || z == NULL)
296 debug(ctx, "Zoom header:\n");
297 debug(ctx, "\tbit1: 0x%08x (%u)\n", z->bit1, z->bit1);
298 debug(ctx, "\tbit0: 0x%08x (%u)\n", z->bit0, z->bit0);
301 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
303 if (ctx == NULL || h == NULL)
306 debug(ctx, "BEGIN\n");
307 debug(ctx, "packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
308 debug(ctx, "direction: 0x%02hhx (%hhu) (%s)\n", h->direction, h->direction,
309 h->direction == AM7XXX_DIRECTION_IN ? "IN" :
310 h->direction == AM7XXX_DIRECTION_OUT ? "OUT" :
312 debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
313 debug(ctx, "unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
314 debug(ctx, "unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
316 switch(h->packet_type) {
317 case AM7XXX_PACKET_TYPE_DEVINFO:
318 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
321 case AM7XXX_PACKET_TYPE_IMAGE:
322 debug_dump_image_header(ctx, &(h->header_data.image));
325 case AM7XXX_PACKET_TYPE_POWER:
326 debug_dump_power_header(ctx, &(h->header_data.power));
329 case AM7XXX_PACKET_TYPE_ZOOM:
330 debug_dump_zoom_header(ctx, &(h->header_data.zoom));
334 debug(ctx, "Parsing data not supported for this packet type!\n");
335 debug_dump_generic_header(ctx, &(h->header_data.data));
338 debug(ctx, "END\n\n");
341 static inline unsigned int in_80chars(unsigned int i)
343 /* The 3 below is the length of "xx " where xx is the hex string
344 * representation of a byte */
345 return ((i + 1) % (80 / 3));
348 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
349 uint8_t *buffer, unsigned int len)
353 if (ctx == NULL || buffer == NULL || len == 0)
358 trace(ctx, "%s\n", message);
360 for (i = 0; i < len; i++) {
361 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
366 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
372 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
373 uint8_t *buffer, unsigned int len)
382 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
388 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
389 if (ret != 0 || (unsigned int)transferred != len) {
390 error(dev->ctx, "%s. Transferred: %d (expected %u)\n",
391 libusb_error_name(ret), transferred, len);
395 trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
400 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
405 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
408 ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
409 if (ret != 0 || (unsigned int)transferred != len) {
410 error(dev->ctx, "%s. Transferred: %d (expected %u)\n",
411 libusb_error_name(ret), transferred, len);
418 static void LIBUSB_CALL send_data_async_complete_cb(struct libusb_transfer *transfer)
420 am7xxx_device *dev = (am7xxx_device *)(transfer->user_data);
421 int *completed = &(dev->transfer_completed);
422 int transferred = transfer->actual_length;
425 if (transferred != transfer->length) {
426 error(dev->ctx, "transferred: %d (expected %u)\n",
427 transferred, transfer->length);
430 switch (transfer->status) {
431 case LIBUSB_TRANSFER_COMPLETED:
434 case LIBUSB_TRANSFER_TIMED_OUT:
435 ret = LIBUSB_ERROR_TIMEOUT;
437 case LIBUSB_TRANSFER_STALL:
438 ret = LIBUSB_ERROR_PIPE;
440 case LIBUSB_TRANSFER_OVERFLOW:
441 ret = LIBUSB_ERROR_OVERFLOW;
443 case LIBUSB_TRANSFER_NO_DEVICE:
444 ret = LIBUSB_ERROR_NO_DEVICE;
446 case LIBUSB_TRANSFER_ERROR:
447 case LIBUSB_TRANSFER_CANCELLED:
448 ret = LIBUSB_ERROR_IO;
451 error(dev->ctx, "unrecognised status code %d", transfer->status);
452 ret = LIBUSB_ERROR_OTHER;
456 error(dev->ctx, "libusb transfer failed: %s",
457 libusb_error_name(ret));
459 libusb_free_transfer(transfer);
465 static inline void wait_for_trasfer_completed(am7xxx_device *dev)
467 while (!dev->transfer_completed) {
468 int ret = libusb_handle_events_completed(dev->ctx->usb_context,
469 &(dev->transfer_completed));
471 if (ret == LIBUSB_ERROR_INTERRUPTED)
473 error(dev->ctx, "libusb_handle_events failed: %s, cancelling transfer and retrying",
474 libusb_error_name(ret));
475 libusb_cancel_transfer(dev->transfer);
481 static int send_data_async(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
484 uint8_t *transfer_buffer;
486 dev->transfer = libusb_alloc_transfer(0);
487 if (dev->transfer == NULL) {
488 error(dev->ctx, "cannot allocate transfer (%s)\n",
493 /* Make a copy of the buffer so the caller can safely reuse it just
494 * after libusb_submit_transfer() has returned. This technique
495 * requires more dynamic allocations compared to a proper
496 * double-buffering approach but it takes a lot less code. */
497 transfer_buffer = malloc(len);
498 if (transfer_buffer == NULL) {
499 error(dev->ctx, "cannot allocate transfer buffer (%s)\n",
504 memcpy(transfer_buffer, buffer, len);
506 dev->transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER;
507 libusb_fill_bulk_transfer(dev->transfer, dev->usb_device, 0x1,
508 transfer_buffer, len,
509 send_data_async_complete_cb, dev, 0);
511 /* wait for the previous transfer to complete */
512 wait_for_trasfer_completed(dev);
514 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
516 dev->transfer_completed = 0;
517 ret = libusb_submit_transfer(dev->transfer);
524 libusb_free_transfer(dev->transfer);
525 dev->transfer = NULL;
529 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
531 uint8_t **buffer_iterator = &buffer;
533 put_le32(h->packet_type, buffer_iterator);
534 put_8(h->direction, buffer_iterator);
535 put_8(h->header_data_len, buffer_iterator);
536 put_8(h->unknown2, buffer_iterator);
537 put_8(h->unknown3, buffer_iterator);
538 put_le32(h->header_data.data.field0, buffer_iterator);
539 put_le32(h->header_data.data.field1, buffer_iterator);
540 put_le32(h->header_data.data.field2, buffer_iterator);
541 put_le32(h->header_data.data.field3, buffer_iterator);
544 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
546 uint8_t **buffer_iterator = &buffer;
548 h->packet_type = get_le32(buffer_iterator);
549 h->direction = get_8(buffer_iterator);
550 h->header_data_len = get_8(buffer_iterator);
551 h->unknown2 = get_8(buffer_iterator);
552 h->unknown3 = get_8(buffer_iterator);
553 h->header_data.data.field0 = get_le32(buffer_iterator);
554 h->header_data.data.field1 = get_le32(buffer_iterator);
555 h->header_data.data.field2 = get_le32(buffer_iterator);
556 h->header_data.data.field3 = get_le32(buffer_iterator);
559 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
563 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
567 unserialize_header(dev->buffer, h);
569 if (h->direction == AM7XXX_DIRECTION_IN) {
573 "Expected an AM7XXX_DIRECTION_IN packet, got one with direction = %d. Weird!\n",
578 debug_dump_header(dev->ctx, h);
584 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
588 debug_dump_header(dev->ctx, h);
590 /* For symmetry with read_header() we should check here for
591 * h->direction == AM7XXX_DIRECTION_OUT but we just ensure that in all
592 * the callers and save some cycles here.
595 serialize_header(h, dev->buffer);
597 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
599 error(dev->ctx, "failed to send data\n");
604 static int send_command(am7xxx_device *dev, am7xxx_packet_type type)
606 struct am7xxx_header h = {
608 .direction = AM7XXX_DIRECTION_OUT,
609 .header_data_len = 0x00,
622 return send_header(dev, &h);
626 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
627 * and print the message unconditionally, this makes it possible to print
628 * fatal messages even early on initialization, before the context has been
630 static void log_message(am7xxx_context *ctx,
632 const char *function_name,
639 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
641 fprintf(stderr, "%s", function_name);
643 fprintf(stderr, "[%d]", line);
644 fprintf(stderr, ": ");
648 vfprintf(stderr, fmt, ap);
655 static am7xxx_device *add_new_device(am7xxx_context *ctx,
656 const struct am7xxx_usb_device_descriptor *desc)
658 am7xxx_device **devices_list;
659 am7xxx_device *new_device;
662 fatal("context must not be NULL!\n");
666 new_device = malloc(sizeof(*new_device));
667 if (new_device == NULL) {
668 debug(ctx, "cannot allocate a new device (%s)\n", strerror(errno));
671 memset(new_device, 0, sizeof(*new_device));
673 new_device->ctx = ctx;
674 new_device->desc = desc;
675 new_device->transfer_completed = 1;
677 devices_list = &(ctx->devices_list);
679 if (*devices_list == NULL) {
680 *devices_list = new_device;
682 am7xxx_device *prev = *devices_list;
685 prev->next = new_device;
690 static am7xxx_device *find_device(am7xxx_context *ctx,
691 unsigned int device_index)
694 am7xxx_device *current;
697 fatal("context must not be NULL!\n");
701 current = ctx->devices_list;
702 while (current && i++ < device_index)
703 current = current->next;
708 static int open_device(am7xxx_context *ctx,
709 unsigned int device_index,
710 libusb_device *usb_dev,
714 int current_configuration;
716 *dev = find_device(ctx, device_index);
722 /* the usb device has already been opened */
723 if ((*dev)->usb_device) {
728 ret = libusb_open(usb_dev, &((*dev)->usb_device));
730 debug(ctx, "libusb_open failed: %s\n", libusb_error_name(ret));
734 /* XXX, the device is now open, if any of the calls below fail we need
735 * to close it again before bailing out.
738 current_configuration = -1;
739 ret = libusb_get_configuration((*dev)->usb_device,
740 ¤t_configuration);
742 debug(ctx, "libusb_get_configuration failed: %s\n",
743 libusb_error_name(ret));
744 goto out_libusb_close;
747 if (current_configuration != (*dev)->desc->configuration) {
749 * In principle, before setting a new configuration, kernel
750 * drivers should be detached from _all_ interfaces; for
751 * example calling something like the following "invented"
752 * function _before_ setting the new configuration:
754 * libusb_detach_all_kernel_drivers((*dev)->usb_device);
756 * However, in practice, this is not necessary for most
757 * devices as they have only one configuration.
759 * When a device only has one configuration:
761 * - if there was a kernel driver bound to the device, it
762 * had already set the configuration and the call below
765 * - if no kernel driver was bound to the device, the call
768 ret = libusb_set_configuration((*dev)->usb_device,
769 (*dev)->desc->configuration);
771 debug(ctx, "libusb_set_configuration failed: %s\n",
772 libusb_error_name(ret));
773 debug(ctx, "Cannot set configuration %hhu\n",
774 (*dev)->desc->configuration);
775 goto out_libusb_close;
779 libusb_set_auto_detach_kernel_driver((*dev)->usb_device, 1);
781 ret = libusb_claim_interface((*dev)->usb_device,
782 (*dev)->desc->interface_number);
784 debug(ctx, "libusb_claim_interface failed: %s\n",
785 libusb_error_name(ret));
786 debug(ctx, "Cannot claim interface %hhu\n",
787 (*dev)->desc->interface_number);
788 goto out_libusb_close;
791 /* Checking that the configuration has not changed, as suggested in
792 * http://libusb.sourceforge.net/api-1.0/caveats.html
794 current_configuration = -1;
795 ret = libusb_get_configuration((*dev)->usb_device,
796 ¤t_configuration);
798 debug(ctx, "libusb_get_configuration after claim failed: %s\n",
799 libusb_error_name(ret));
800 goto out_libusb_release_interface;
803 if (current_configuration != (*dev)->desc->configuration) {
804 debug(ctx, "libusb configuration changed (expected: %hhu, current: %d)\n",
805 (*dev)->desc->configuration, current_configuration);
807 goto out_libusb_release_interface;
812 out_libusb_release_interface:
813 libusb_release_interface((*dev)->usb_device,
814 (*dev)->desc->interface_number);
816 libusb_close((*dev)->usb_device);
817 (*dev)->usb_device = NULL;
822 SCAN_OP_BUILD_DEVLIST,
827 * This is where the central logic of multi-device support is.
829 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
830 * 'dev' are ignored; the function returns 0 on success or a negative value
833 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
834 * device with index 'open_device_index' and returns the correspondent
835 * am7xxx_device in the 'dev' parameter; the function returns the value from
836 * open_device(), which is 0 on success, 1 if the device was already open or
837 * a negative value on error.
840 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
841 * the caller might want to call am7xxx_shutdown() in order to remove
842 * devices possibly added before the failure.
844 static int scan_devices(am7xxx_context *ctx, scan_op op,
845 unsigned int open_device_index, am7xxx_device **dev)
848 libusb_device **list;
849 unsigned int current_index;
854 fatal("context must not be NULL!\n");
857 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
858 error(ctx, "device scan done already? Abort!\n");
862 num_devices = libusb_get_device_list(ctx->usb_context, &list);
863 if (num_devices < 0) {
869 for (i = 0; i < num_devices; i++) {
870 struct libusb_device_descriptor desc;
873 ret = libusb_get_device_descriptor(list[i], &desc);
877 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
878 if (desc.idVendor == supported_devices[j].vendor_id &&
879 desc.idProduct == supported_devices[j].product_id) {
881 if (op == SCAN_OP_BUILD_DEVLIST) {
882 am7xxx_device *new_device;
883 info(ctx, "am7xxx device found, index: %d, name: %s\n",
885 supported_devices[j].name);
886 new_device = add_new_device(ctx, &supported_devices[j]);
887 if (new_device == NULL) {
888 /* XXX, the caller may want
889 * to call am7xxx_shutdown() if
890 * we fail here, as we may have
891 * added some devices already
893 debug(ctx, "Cannot create a new device\n");
897 } else if (op == SCAN_OP_OPEN_DEVICE &&
898 current_index == open_device_index) {
900 ret = open_device(ctx,
905 debug(ctx, "open_device failed\n");
907 /* exit the loop unconditionally after
908 * attempting to open the device
909 * requested by the user */
917 /* if we made it up to here when op == SCAN_OP_OPEN_DEVICE,
918 * no devices to open had been found. */
919 if (op == SCAN_OP_OPEN_DEVICE) {
920 error(ctx, "Cannot find any device to open\n");
925 /* everything went fine when building the device list */
928 libusb_free_device_list(list, 1);
932 /* Device specific operations */
934 static int default_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
937 struct am7xxx_header h = {
938 .packet_type = AM7XXX_PACKET_TYPE_POWER,
939 .direction = AM7XXX_DIRECTION_OUT,
940 .header_data_len = sizeof(struct am7xxx_power_header),
946 case AM7XXX_POWER_OFF:
947 h.header_data.power.bit2 = 0;
948 h.header_data.power.bit1 = 0;
949 h.header_data.power.bit0 = 0;
952 case AM7XXX_POWER_LOW:
953 h.header_data.power.bit2 = 0;
954 h.header_data.power.bit1 = 0;
955 h.header_data.power.bit0 = 1;
958 case AM7XXX_POWER_MIDDLE:
959 h.header_data.power.bit2 = 0;
960 h.header_data.power.bit1 = 1;
961 h.header_data.power.bit0 = 0;
964 case AM7XXX_POWER_HIGH:
965 h.header_data.power.bit2 = 0;
966 h.header_data.power.bit1 = 1;
967 h.header_data.power.bit0 = 1;
970 case AM7XXX_POWER_TURBO:
971 h.header_data.power.bit2 = 1;
972 h.header_data.power.bit1 = 0;
973 h.header_data.power.bit0 = 0;
977 error(dev->ctx, "Unsupported power mode.\n");
981 ret = send_header(dev, &h);
988 static int picopix_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
991 case AM7XXX_POWER_LOW:
992 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_LOW);
994 case AM7XXX_POWER_MIDDLE:
995 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_MEDIUM);
997 case AM7XXX_POWER_HIGH:
998 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_HIGH);
1000 case AM7XXX_POWER_OFF:
1001 case AM7XXX_POWER_TURBO:
1003 error(dev->ctx, "Unsupported power mode.\n");
1008 static int default_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
1011 struct am7xxx_header h = {
1012 .packet_type = AM7XXX_PACKET_TYPE_ZOOM,
1013 .direction = AM7XXX_DIRECTION_OUT,
1014 .header_data_len = sizeof(struct am7xxx_zoom_header),
1020 case AM7XXX_ZOOM_ORIGINAL:
1021 h.header_data.zoom.bit1 = 0;
1022 h.header_data.zoom.bit0 = 0;
1026 h.header_data.zoom.bit1 = 0;
1027 h.header_data.zoom.bit0 = 1;
1030 case AM7XXX_ZOOM_H_V:
1031 h.header_data.zoom.bit1 = 1;
1032 h.header_data.zoom.bit0 = 0;
1035 case AM7XXX_ZOOM_TEST:
1036 h.header_data.zoom.bit1 = 1;
1037 h.header_data.zoom.bit0 = 1;
1040 case AM7XXX_ZOOM_TELE:
1042 error(dev->ctx, "Unsupported zoom mode.\n");
1046 ret = send_header(dev, &h);
1053 static int picopix_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
1056 am7xxx_packet_type packet_type;
1059 case AM7XXX_ZOOM_ORIGINAL:
1060 packet_type = AM7XXX_PACKET_TYPE_PICOPIX_DISABLE_TI;
1063 case AM7XXX_ZOOM_TELE:
1064 packet_type = AM7XXX_PACKET_TYPE_PICOPIX_ENABLE_TI;
1068 case AM7XXX_ZOOM_H_V:
1069 case AM7XXX_ZOOM_TEST:
1071 error(dev->ctx, "Unsupported zoom mode.\n");
1075 ret = send_command(dev, packet_type);
1079 /* The Windows drivers wait for 100ms and send the same command again,
1080 * probably to overcome a firmware deficiency */
1085 return send_command(dev, packet_type);
1090 AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
1094 *ctx = malloc(sizeof(**ctx));
1096 fatal("cannot allocate the context (%s)\n", strerror(errno));
1099 memset(*ctx, 0, sizeof(**ctx));
1101 /* Set the highest log level during initialization */
1102 (*ctx)->log_level = AM7XXX_LOG_TRACE;
1104 ret = libusb_init(&((*ctx)->usb_context));
1106 error(*ctx, "libusb_init failed: %s\n", libusb_error_name(ret));
1107 goto out_free_context;
1110 libusb_set_debug((*ctx)->usb_context, LIBUSB_LOG_LEVEL_INFO);
1112 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
1114 error(*ctx, "scan_devices() failed\n");
1115 am7xxx_shutdown(*ctx);
1119 /* Set a quieter log level as default for normal operation */
1120 (*ctx)->log_level = AM7XXX_LOG_ERROR;
1130 AM7XXX_PUBLIC void am7xxx_shutdown(am7xxx_context *ctx)
1132 am7xxx_device *current;
1135 fatal("context must not be NULL!\n");
1139 current = ctx->devices_list;
1141 am7xxx_device *next = current->next;
1142 am7xxx_close_device(current);
1143 free(current->device_info);
1148 libusb_exit(ctx->usb_context);
1153 AM7XXX_PUBLIC void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
1155 ctx->log_level = log_level;
1158 AM7XXX_PUBLIC int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
1159 unsigned int device_index)
1164 fatal("context must not be NULL!\n");
1168 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
1172 } else if (ret > 0) {
1173 warning(ctx, "device %d already open\n", device_index);
1179 /* Philips/Sagemcom PicoPix projectors require that the DEVINFO packet
1180 * is the first one to be sent to the device in order for it to
1181 * successfully return the correct device information.
1183 * NOTE: am7xxx_get_device_info() will fetch the actual device info
1184 * from the device only the very first time it's called for a given
1185 * device, otherwise, it'll return a cached version of the device info
1186 * (from a previous call to am7xxx_open_device(), for instance).
1188 ret = am7xxx_get_device_info(*dev, NULL);
1190 error(ctx, "cannot get device info\n");
1196 AM7XXX_PUBLIC int am7xxx_close_device(am7xxx_device *dev)
1199 fatal("dev must not be NULL!\n");
1202 if (dev->usb_device) {
1203 wait_for_trasfer_completed(dev);
1204 libusb_release_interface(dev->usb_device, dev->desc->interface_number);
1205 libusb_close(dev->usb_device);
1206 dev->usb_device = NULL;
1211 AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
1212 am7xxx_device_info *device_info)
1215 struct am7xxx_header h;
1217 /* if there is a cached copy of the device info, just return that */
1218 if (dev->device_info)
1221 ret = send_command(dev, AM7XXX_PACKET_TYPE_DEVINFO);
1225 memset(&h, 0, sizeof(h));
1226 ret = read_header(dev, &h);
1230 if (h.packet_type != AM7XXX_PACKET_TYPE_DEVINFO) {
1231 error(dev->ctx, "expected packet type: %d, got %d instead!\n",
1232 AM7XXX_PACKET_TYPE_DEVINFO, h.packet_type);
1237 dev->device_info = malloc(sizeof(*dev->device_info));
1238 if (dev->device_info == NULL) {
1239 error(dev->ctx, "cannot allocate a device info (%s)\n",
1243 memset(dev->device_info, 0, sizeof(*dev->device_info));
1245 dev->device_info->native_width = h.header_data.devinfo.native_width;
1246 dev->device_info->native_height = h.header_data.devinfo.native_height;
1248 /* No reason to expose these in the public API until we know what they mean */
1249 dev->device_info->unknown0 = h.header_data.devinfo.unknown0;
1250 dev->device_info->unknown1 = h.header_data.devinfo.unknown1;
1255 memcpy(device_info, dev->device_info, sizeof(*device_info));
1259 AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
1260 unsigned int upscale,
1261 unsigned int original_width,
1262 unsigned int original_height,
1263 unsigned int *scaled_width,
1264 unsigned int *scaled_height)
1266 am7xxx_device_info device_info;
1271 ret = am7xxx_get_device_info(dev, &device_info);
1273 error(dev->ctx, "cannot get device info\n");
1278 * Check if we need to rescale; if the input image fits the native
1279 * dimensions there is no need to, unless we want to upscale.
1282 original_width <= device_info.native_width &&
1283 original_height <= device_info.native_height ) {
1284 debug(dev->ctx, "CASE 0, no rescaling, the original image fits already\n");
1285 *scaled_width = original_width;
1286 *scaled_height = original_height;
1290 /* Input dimensions relative to the device native dimensions */
1291 width_ratio = (float)original_width / device_info.native_width;
1292 height_ratio = (float)original_height / device_info.native_height;
1294 if (width_ratio > height_ratio) {
1296 * The input is proportionally "wider" than the device viewport
1297 * so its height needs to be adjusted
1299 debug(dev->ctx, "CASE 1, original image wider, adjust the scaled height\n");
1300 *scaled_width = device_info.native_width;
1301 *scaled_height = (unsigned int)lroundf(original_height / width_ratio);
1302 } else if (width_ratio < height_ratio) {
1304 * The input is proportionally "taller" than the device viewport
1305 * so its width needs to be adjusted
1307 debug(dev->ctx, "CASE 2 original image taller, adjust the scaled width\n");
1308 *scaled_width = (unsigned int)lroundf(original_width / height_ratio);
1309 *scaled_height = device_info.native_height;
1311 debug(dev->ctx, "CASE 3, just rescale, same aspect ratio already\n");
1312 *scaled_width = device_info.native_width;
1313 *scaled_height = device_info.native_height;
1315 debug(dev->ctx, "scaled dimensions: %dx%d\n", *scaled_width, *scaled_height);
1320 AM7XXX_PUBLIC int am7xxx_send_image(am7xxx_device *dev,
1321 am7xxx_image_format format,
1323 unsigned int height,
1325 unsigned int image_size)
1328 struct am7xxx_header h = {
1329 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
1330 .direction = AM7XXX_DIRECTION_OUT,
1331 .header_data_len = sizeof(struct am7xxx_image_header),
1339 .image_size = image_size,
1344 ret = send_header(dev, &h);
1348 if (image == NULL || image_size == 0) {
1349 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
1353 return send_data(dev, image, image_size);
1356 AM7XXX_PUBLIC int am7xxx_send_image_async(am7xxx_device *dev,
1357 am7xxx_image_format format,
1359 unsigned int height,
1361 unsigned int image_size)
1364 struct am7xxx_header h = {
1365 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
1366 .direction = AM7XXX_DIRECTION_OUT,
1367 .header_data_len = sizeof(struct am7xxx_image_header),
1375 .image_size = image_size,
1380 ret = send_header(dev, &h);
1384 if (image == NULL || image_size == 0) {
1385 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
1389 return send_data_async(dev, image, image_size);
1392 AM7XXX_PUBLIC int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
1394 if (dev->desc->ops.set_power_mode == NULL) {
1396 "setting power mode is unsupported on this device\n");
1400 return dev->desc->ops.set_power_mode(dev, power);
1403 AM7XXX_PUBLIC int am7xxx_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
1405 if (dev->desc->ops.set_zoom_mode == NULL) {
1407 "setting zoom mode is unsupported on this device\n");
1411 return dev->desc->ops.set_zoom_mode(dev, zoom);