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/>.
27 #include "serialize.h"
29 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
31 /* If we're not using GNU C, elide __attribute__
32 * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
35 # define __attribute__(x) /*NOTHING*/
38 static void log_message(am7xxx_context *ctx,
43 ...) __attribute__ ((format (printf, 5, 6)));
45 #define fatal(...) log_message(NULL, AM7XXX_LOG_FATAL, __func__, __LINE__, __VA_ARGS__)
46 #define error(ctx, ...) log_message(ctx, AM7XXX_LOG_ERROR, __func__, __LINE__, __VA_ARGS__)
47 #define warning(ctx, ...) log_message(ctx, AM7XXX_LOG_WARNING, __func__, 0, __VA_ARGS__)
48 #define info(ctx, ...) log_message(ctx, AM7XXX_LOG_INFO, __func__, 0, __VA_ARGS__)
49 #define debug(ctx, ...) log_message(ctx, AM7XXX_LOG_DEBUG, __func__, 0, __VA_ARGS__)
50 #define trace(ctx, ...) log_message(ctx, AM7XXX_LOG_TRACE, NULL, 0, __VA_ARGS__)
52 struct am7xxx_usb_device_descriptor {
58 static struct am7xxx_usb_device_descriptor supported_devices[] = {
65 .name = "Philips/Sagemcom PicoPix 1020",
71 /* The header size on the wire is known to be always 24 bytes, regardless of
72 * the memory configuration enforced by different architechtures or compilers
73 * for struct am7xxx_header
75 #define AM7XXX_HEADER_WIRE_SIZE 24
77 struct _am7xxx_device {
78 libusb_device_handle *usb_device;
79 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
84 struct _am7xxx_context {
85 libusb_context *usb_context;
87 am7xxx_device *devices_list;
91 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
92 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
93 AM7XXX_PACKET_TYPE_POWER = 0x04,
94 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
97 struct am7xxx_generic_header {
104 struct am7xxx_devinfo_header {
105 uint32_t native_width;
106 uint32_t native_height;
111 struct am7xxx_image_header {
118 struct am7xxx_power_header {
125 * Examples of packet headers:
128 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
131 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
134 struct am7xxx_header {
135 uint32_t packet_type;
137 uint8_t header_data_len;
141 struct am7xxx_generic_header data;
142 struct am7xxx_devinfo_header devinfo;
143 struct am7xxx_image_header image;
144 struct am7xxx_power_header power;
150 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
152 if (ctx == NULL || d == NULL)
155 debug(ctx, "Info header:\n");
156 debug(ctx, "\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
157 debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
158 debug(ctx, "\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
159 debug(ctx, "\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
162 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
164 if (ctx == NULL || i == NULL)
167 debug(ctx, "Image header:\n");
168 debug(ctx, "\tformat: 0x%08x (%u)\n", i->format, i->format);
169 debug(ctx, "\twidth: 0x%08x (%u)\n", i->width, i->width);
170 debug(ctx, "\theight: 0x%08x (%u)\n", i->height, i->height);
171 debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
174 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
176 if (ctx == NULL || p == NULL)
179 debug(ctx, "Power header:\n");
180 debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
181 debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
182 debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
185 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
187 if (ctx == NULL || h == NULL)
190 debug(ctx, "BEGIN\n");
191 debug(ctx, "packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
192 debug(ctx, "unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
193 debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
194 debug(ctx, "unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
195 debug(ctx, "unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
197 switch(h->packet_type) {
198 case AM7XXX_PACKET_TYPE_DEVINFO:
199 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
202 case AM7XXX_PACKET_TYPE_IMAGE:
203 debug_dump_image_header(ctx, &(h->header_data.image));
206 case AM7XXX_PACKET_TYPE_POWER:
207 debug_dump_power_header(ctx, &(h->header_data.power));
211 debug(ctx, "Packet type not supported!\n");
214 debug(ctx, "END\n\n");
217 static inline unsigned int in_80chars(unsigned int i)
219 /* The 3 below is the length of "xx " where xx is the hex string
220 * representation of a byte */
221 return ((i+1) % (80/3));
224 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
225 uint8_t *buffer, unsigned int len)
229 if (ctx == NULL || buffer == NULL || len == 0)
234 trace(ctx, "%s\n", message);
236 for (i = 0; i < len; i++) {
237 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
242 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
247 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
248 uint8_t *buffer, unsigned int len)
257 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
262 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
263 if (ret != 0 || (unsigned int)transferred != len) {
264 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
265 ret, transferred, len);
269 trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
274 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
279 trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
281 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
282 if (ret != 0 || (unsigned int)transferred != len) {
283 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
284 ret, transferred, len);
291 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
293 uint8_t **buffer_iterator = &buffer;
295 put_le32(h->packet_type, buffer_iterator);
296 put_8(h->unknown0, buffer_iterator);
297 put_8(h->header_data_len, buffer_iterator);
298 put_8(h->unknown2, buffer_iterator);
299 put_8(h->unknown3, buffer_iterator);
300 put_le32(h->header_data.data.field0, buffer_iterator);
301 put_le32(h->header_data.data.field1, buffer_iterator);
302 put_le32(h->header_data.data.field2, buffer_iterator);
303 put_le32(h->header_data.data.field3, buffer_iterator);
306 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
308 uint8_t **buffer_iterator = &buffer;
310 h->packet_type = get_le32(buffer_iterator);
311 h->unknown0 = get_8(buffer_iterator);
312 h->header_data_len = get_8(buffer_iterator);
313 h->unknown2 = get_8(buffer_iterator);
314 h->unknown3 = get_8(buffer_iterator);
315 h->header_data.data.field0 = get_le32(buffer_iterator);
316 h->header_data.data.field1 = get_le32(buffer_iterator);
317 h->header_data.data.field2 = get_le32(buffer_iterator);
318 h->header_data.data.field3 = get_le32(buffer_iterator);
321 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
325 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
329 unserialize_header(dev->buffer, h);
331 debug_dump_header(dev->ctx, h);
339 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
343 debug_dump_header(dev->ctx, h);
345 serialize_header(h, dev->buffer);
346 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
348 error(dev->ctx, "failed to send data\n");
353 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
354 * and print the message unconditionally, this makes it possible to print
355 * fatal messages even early on initialization, before the context has been
357 static void log_message(am7xxx_context *ctx,
359 const char *function,
366 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
368 fprintf(stderr, "%s", function);
370 fprintf(stderr, "[%d]", line);
371 fprintf(stderr, ": ");
375 vfprintf(stderr, fmt, ap);
382 static am7xxx_device *add_new_device(am7xxx_context *ctx)
384 am7xxx_device **devices_list;
385 am7xxx_device *new_device;
388 fatal("context must not be NULL!\n");
392 devices_list = &(ctx->devices_list);
394 new_device = malloc(sizeof(*new_device));
395 if (new_device == NULL) {
396 fatal("cannot allocate a new device (%s)\n", strerror(errno));
399 memset(new_device, 0, sizeof(*new_device));
401 new_device->ctx = ctx;
403 if (*devices_list == NULL) {
404 *devices_list = new_device;
406 am7xxx_device *prev = *devices_list;
409 prev->next = new_device;
414 static am7xxx_device *find_device(am7xxx_context *ctx,
415 unsigned int device_index)
418 am7xxx_device *current;
421 fatal("context must not be NULL!\n");
425 current = ctx->devices_list;
426 while (current && i++ < device_index)
427 current = current->next;
433 SCAN_OP_BUILD_DEVLIST,
438 * This is where the central logic of multi-device support is.
440 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
441 * 'dev' are ignored; the function returns 0 on success and a negative value
444 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
445 * device with index 'open_device_index' and returns the correspondent
446 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
447 * 1 if the device was already open and a negative value on error.
450 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
451 * the caller might want to call am7xxx_shutdown() in order to remove
452 * devices possibly added before the failure.
454 static int scan_devices(am7xxx_context *ctx, scan_op op,
455 unsigned int open_device_index, am7xxx_device **dev)
458 libusb_device** list;
459 unsigned int current_index;
464 fatal("context must not be NULL!\n");
467 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
468 error(ctx, "device scan done already? Abort!\n");
472 num_devices = libusb_get_device_list(ctx->usb_context, &list);
473 if (num_devices < 0) {
479 for (i = 0; i < num_devices; i++) {
480 struct libusb_device_descriptor desc;
483 ret = libusb_get_device_descriptor(list[i], &desc);
487 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
488 if (desc.idVendor == supported_devices[j].vendor_id
489 && desc.idProduct == supported_devices[j].product_id) {
491 if (op == SCAN_OP_BUILD_DEVLIST) {
492 am7xxx_device *new_device;
493 info(ctx, "am7xxx device found, index: %d, name: %s\n",
495 supported_devices[j].name);
496 new_device = add_new_device(ctx);
497 if (new_device == NULL) {
498 /* XXX, the caller may want
499 * to call am7xxx_shutdown() if
500 * we fail here, as we may have
501 * added some devices already
503 debug(ctx, "Cannot create a new device\n");
507 } else if (op == SCAN_OP_OPEN_DEVICE &&
508 current_index == open_device_index) {
510 *dev = find_device(ctx, open_device_index);
516 /* the usb device has already been opened */
517 if ((*dev)->usb_device) {
518 debug(ctx, "(*dev)->usb_device already set\n");
523 ret = libusb_open(list[i], &((*dev)->usb_device));
525 debug(ctx, "libusb_open failed\n");
529 libusb_set_configuration((*dev)->usb_device, 1);
530 libusb_claim_interface((*dev)->usb_device, 0);
538 /* if we made it up to here we didn't find any device to open */
539 if (op == SCAN_OP_OPEN_DEVICE) {
540 error(ctx, "Cannot find any device to open\n");
545 /* everything went fine when building the device list */
548 libusb_free_device_list(list, 1);
552 int am7xxx_init(am7xxx_context **ctx)
556 *ctx = malloc(sizeof(**ctx));
558 fatal("cannot allocate the context (%s)\n", strerror(errno));
562 memset(*ctx, 0, sizeof(**ctx));
564 /* Set the highest log level during initialization */
565 (*ctx)->log_level = AM7XXX_LOG_TRACE;
567 ret = libusb_init(&((*ctx)->usb_context));
569 goto out_free_context;
571 libusb_set_debug((*ctx)->usb_context, 3);
573 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
575 error(*ctx, "scan_devices() failed\n");
576 am7xxx_shutdown(*ctx);
580 /* Set a quieter log level as default for normal operation */
581 (*ctx)->log_level = AM7XXX_LOG_ERROR;
591 void am7xxx_shutdown(am7xxx_context *ctx)
593 am7xxx_device *current;
596 fatal("context must not be NULL!\n");
600 current = ctx->devices_list;
602 am7xxx_device *next = current->next;
603 am7xxx_close_device(current);
608 libusb_exit(ctx->usb_context);
613 void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
615 ctx->log_level = log_level;
618 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
619 unsigned int device_index)
624 fatal("context must not be NULL!\n");
628 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
631 } else if (ret > 0) {
632 warning(ctx, "device %d already open\n", device_index);
640 int am7xxx_close_device(am7xxx_device *dev)
643 fatal("dev must not be NULL!\n");
646 if (dev->usb_device) {
647 libusb_release_interface(dev->usb_device, 0);
648 libusb_close(dev->usb_device);
649 dev->usb_device = NULL;
654 int am7xxx_get_device_info(am7xxx_device *dev,
655 unsigned int *native_width,
656 unsigned int *native_height,
657 unsigned int *unknown0,
658 unsigned int *unknown1)
661 struct am7xxx_header h = {
662 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
664 .header_data_len = 0x00,
677 ret = send_header(dev, &h);
681 ret = read_header(dev, &h);
685 *native_width = h.header_data.devinfo.native_width;
686 *native_height = h.header_data.devinfo.native_height;
687 *unknown0 = h.header_data.devinfo.unknown0;
688 *unknown1 = h.header_data.devinfo.unknown1;
693 int am7xxx_send_image(am7xxx_device *dev,
694 am7xxx_image_format format,
701 struct am7xxx_header h = {
702 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
704 .header_data_len = sizeof(struct am7xxx_image_header),
717 ret = send_header(dev, &h);
721 if (image == NULL || size == 0) {
722 warning(dev->ctx, "Not sending any data, check the 'data' or 'size' parameters\n");
726 return send_data(dev, image, size);
729 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
732 struct am7xxx_header h = {
733 .packet_type = AM7XXX_PACKET_TYPE_POWER,
735 .header_data_len = sizeof(struct am7xxx_power_header),
741 case AM7XXX_POWER_OFF:
742 h.header_data.power.bit2 = 0;
743 h.header_data.power.bit1 = 0;
744 h.header_data.power.bit0 = 0;
747 case AM7XXX_POWER_LOW:
748 h.header_data.power.bit2 = 0;
749 h.header_data.power.bit1 = 0;
750 h.header_data.power.bit0 = 1;
752 case AM7XXX_POWER_MIDDLE:
753 h.header_data.power.bit2 = 0;
754 h.header_data.power.bit1 = 1;
755 h.header_data.power.bit0 = 0;
758 case AM7XXX_POWER_HIGH:
759 h.header_data.power.bit2 = 0;
760 h.header_data.power.bit1 = 1;
761 h.header_data.power.bit0 = 1;
764 case AM7XXX_POWER_TURBO:
765 h.header_data.power.bit2 = 1;
766 h.header_data.power.bit1 = 0;
767 h.header_data.power.bit0 = 0;
771 error(dev->ctx, "Power mode not supported!\n");
775 ret = send_header(dev, &h);