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;
149 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
154 printf("Info header:\n");
155 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
156 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
157 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
158 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
161 static void dump_image_header(struct am7xxx_image_header *i)
166 printf("Image header:\n");
167 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
168 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
169 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
170 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
173 static void dump_power_header(struct am7xxx_power_header *p)
178 printf("Power header:\n");
179 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
180 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
181 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
184 static void dump_header(struct am7xxx_header *h)
189 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
190 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
191 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
192 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
193 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
195 switch(h->packet_type) {
196 case AM7XXX_PACKET_TYPE_DEVINFO:
197 dump_devinfo_header(&(h->header_data.devinfo));
200 case AM7XXX_PACKET_TYPE_IMAGE:
201 dump_image_header(&(h->header_data.image));
204 case AM7XXX_PACKET_TYPE_POWER:
205 dump_power_header(&(h->header_data.power));
209 printf("Packet type not supported!\n");
216 static inline unsigned int in_80chars(unsigned int i)
218 /* The 3 below is the length of "xx " where xx is the hex string
219 * representation of a byte */
220 return ((i+1) % (80/3));
223 static void dump_buffer(uint8_t *buffer, unsigned int len)
227 if (buffer == NULL || len == 0)
230 for (i = 0; i < len; i++) {
231 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
236 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
241 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
242 if (ret != 0 || (unsigned int)transferred != len) {
243 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
244 ret, transferred, len);
249 printf("\n<-- received\n");
250 dump_buffer(buffer, len);
257 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
263 printf("\nsending -->\n");
264 dump_buffer(buffer, len);
268 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
269 if (ret != 0 || (unsigned int)transferred != len) {
270 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
271 ret, transferred, len);
278 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
280 uint8_t **buffer_iterator = &buffer;
282 put_le32(h->packet_type, buffer_iterator);
283 put_8(h->unknown0, buffer_iterator);
284 put_8(h->header_data_len, buffer_iterator);
285 put_8(h->unknown2, buffer_iterator);
286 put_8(h->unknown3, buffer_iterator);
287 put_le32(h->header_data.data.field0, buffer_iterator);
288 put_le32(h->header_data.data.field1, buffer_iterator);
289 put_le32(h->header_data.data.field2, buffer_iterator);
290 put_le32(h->header_data.data.field3, buffer_iterator);
293 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
295 uint8_t **buffer_iterator = &buffer;
297 h->packet_type = get_le32(buffer_iterator);
298 h->unknown0 = get_8(buffer_iterator);
299 h->header_data_len = get_8(buffer_iterator);
300 h->unknown2 = get_8(buffer_iterator);
301 h->unknown3 = get_8(buffer_iterator);
302 h->header_data.data.field0 = get_le32(buffer_iterator);
303 h->header_data.data.field1 = get_le32(buffer_iterator);
304 h->header_data.data.field2 = get_le32(buffer_iterator);
305 h->header_data.data.field3 = get_le32(buffer_iterator);
308 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
312 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
316 unserialize_header(dev->buffer, h);
330 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
340 serialize_header(h, dev->buffer);
341 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
343 fprintf(stderr, "send_header: failed to send data.\n");
348 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
349 * and print the message unconditionally, this makes it possible to print
350 * fatal messages even early on initialization, before the context has been
352 static void log_message(am7xxx_context *ctx,
354 const char *function,
361 if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
363 fprintf(stderr, "%s", function);
365 fprintf(stderr, "[%d]", line);
366 fprintf(stderr, ": ");
370 vfprintf(stderr, fmt, ap);
377 static am7xxx_device *add_new_device(am7xxx_context *ctx)
379 am7xxx_device **devices_list;
380 am7xxx_device *new_device;
383 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
387 devices_list = &(ctx->devices_list);
389 new_device = malloc(sizeof(*new_device));
390 if (new_device == NULL) {
394 memset(new_device, 0, sizeof(*new_device));
396 new_device->ctx = ctx;
398 if (*devices_list == NULL) {
399 *devices_list = new_device;
401 am7xxx_device *prev = *devices_list;
404 prev->next = new_device;
409 static am7xxx_device *find_device(am7xxx_context *ctx,
410 unsigned int device_index)
413 am7xxx_device *current;
416 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
420 current = ctx->devices_list;
421 while (current && i++ < device_index)
422 current = current->next;
428 SCAN_OP_BUILD_DEVLIST,
433 * This is where the central logic of multi-device support is.
435 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
436 * 'dev' are ignored; the function returns 0 on success and a negative value
439 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
440 * device with index 'open_device_index' and returns the correspondent
441 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
442 * 1 if the device was already open and a negative value on error.
445 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
446 * the caller might want to call am7xxx_shutdown() in order to remove
447 * devices possibly added before the failure.
449 static int scan_devices(am7xxx_context *ctx, scan_op op,
450 unsigned int open_device_index, am7xxx_device **dev)
453 libusb_device** list;
454 unsigned int current_index;
459 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
462 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
463 fprintf(stderr, "%s: device scan done already? Abort!\n", __func__);
467 num_devices = libusb_get_device_list(ctx->usb_context, &list);
468 if (num_devices < 0) {
474 for (i = 0; i < num_devices; i++) {
475 struct libusb_device_descriptor desc;
478 ret = libusb_get_device_descriptor(list[i], &desc);
482 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
483 if (desc.idVendor == supported_devices[j].vendor_id
484 && desc.idProduct == supported_devices[j].product_id) {
486 if (op == SCAN_OP_BUILD_DEVLIST) {
487 am7xxx_device *new_device;
489 printf("am7xxx device found, index: %d, name: %s\n",
491 supported_devices[j].name);
492 new_device = add_new_device(ctx);
493 if (new_device == NULL) {
494 /* XXX, the caller may want
495 * to call am7xxx_shutdown() if
496 * we fail here, as we may have
497 * added some devices already
502 } else if (op == SCAN_OP_OPEN_DEVICE &&
503 current_index == open_device_index) {
505 *dev = find_device(ctx, open_device_index);
511 /* the usb device has already been opened */
512 if ((*dev)->usb_device) {
517 ret = libusb_open(list[i], &((*dev)->usb_device));
521 libusb_set_configuration((*dev)->usb_device, 1);
522 libusb_claim_interface((*dev)->usb_device, 0);
530 /* if we made it up to here we didn't find any device to open */
531 if (op == SCAN_OP_OPEN_DEVICE) {
536 /* everything went fine when building the device list */
539 libusb_free_device_list(list, 1);
543 int am7xxx_init(am7xxx_context **ctx)
547 *ctx = malloc(sizeof(**ctx));
553 memset(*ctx, 0, sizeof(**ctx));
555 /* Set the highest log level during initialization */
556 (*ctx)->log_level = AM7XXX_LOG_TRACE;
558 ret = libusb_init(&((*ctx)->usb_context));
560 goto out_free_context;
562 libusb_set_debug((*ctx)->usb_context, 3);
564 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
566 fprintf(stderr, "%s: scan_devices failed\n", __func__);
567 am7xxx_shutdown(*ctx);
571 /* Set a quieter log level as default for normal operation */
572 (*ctx)->log_level = AM7XXX_LOG_ERROR;
582 void am7xxx_shutdown(am7xxx_context *ctx)
584 am7xxx_device *current;
587 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
591 current = ctx->devices_list;
593 am7xxx_device *next = current->next;
594 am7xxx_close_device(current);
599 libusb_exit(ctx->usb_context);
604 void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
606 ctx->log_level = log_level;
609 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
610 unsigned int device_index)
615 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
619 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
622 } else if (ret > 0) {
624 fprintf(stderr, "%s: device %d already open\n", __func__, device_index);
632 int am7xxx_close_device(am7xxx_device *dev)
635 fprintf(stderr, "%s: dev must not be NULL!\n", __func__);
638 if (dev->usb_device) {
639 libusb_release_interface(dev->usb_device, 0);
640 libusb_close(dev->usb_device);
641 dev->usb_device = NULL;
646 int am7xxx_get_device_info(am7xxx_device *dev,
647 unsigned int *native_width,
648 unsigned int *native_height,
649 unsigned int *unknown0,
650 unsigned int *unknown1)
653 struct am7xxx_header h = {
654 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
656 .header_data_len = 0x00,
669 ret = send_header(dev, &h);
673 ret = read_header(dev, &h);
677 *native_width = h.header_data.devinfo.native_width;
678 *native_height = h.header_data.devinfo.native_height;
679 *unknown0 = h.header_data.devinfo.unknown0;
680 *unknown1 = h.header_data.devinfo.unknown1;
685 int am7xxx_send_image(am7xxx_device *dev,
686 am7xxx_image_format format,
693 struct am7xxx_header h = {
694 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
696 .header_data_len = sizeof(struct am7xxx_image_header),
709 ret = send_header(dev, &h);
713 if (image == NULL || size == 0)
716 return send_data(dev, image, size);
719 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
722 struct am7xxx_header h = {
723 .packet_type = AM7XXX_PACKET_TYPE_POWER,
725 .header_data_len = sizeof(struct am7xxx_power_header),
731 case AM7XXX_POWER_OFF:
732 h.header_data.power.bit2 = 0;
733 h.header_data.power.bit1 = 0;
734 h.header_data.power.bit0 = 0;
737 case AM7XXX_POWER_LOW:
738 h.header_data.power.bit2 = 0;
739 h.header_data.power.bit1 = 0;
740 h.header_data.power.bit0 = 1;
742 case AM7XXX_POWER_MIDDLE:
743 h.header_data.power.bit2 = 0;
744 h.header_data.power.bit1 = 1;
745 h.header_data.power.bit0 = 0;
748 case AM7XXX_POWER_HIGH:
749 h.header_data.power.bit2 = 0;
750 h.header_data.power.bit1 = 1;
751 h.header_data.power.bit0 = 1;
754 case AM7XXX_POWER_TURBO:
755 h.header_data.power.bit2 = 1;
756 h.header_data.power.bit1 = 0;
757 h.header_data.power.bit0 = 0;
761 fprintf(stderr, "Unsupported power mode.\n");
765 ret = send_header(dev, &h);