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/>.
26 #include "serialize.h"
28 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30 struct am7xxx_usb_device_descriptor {
36 static struct am7xxx_usb_device_descriptor supported_devices[] = {
43 .name = "Philips/Sagemcom PicoPix 1020",
49 /* The header size on the wire is known to be always 24 bytes, regardless of
50 * the memory configuration enforced by different architechtures or compilers
51 * for struct am7xxx_header
53 #define AM7XXX_HEADER_WIRE_SIZE 24
55 struct _am7xxx_device {
56 libusb_device_handle *usb_device;
57 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
61 struct _am7xxx_context {
62 libusb_context *usb_context;
63 am7xxx_device *devices_list;
67 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
68 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
69 AM7XXX_PACKET_TYPE_POWER = 0x04,
70 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
73 struct am7xxx_generic_header {
80 struct am7xxx_devinfo_header {
81 uint32_t native_width;
82 uint32_t native_height;
87 struct am7xxx_image_header {
94 struct am7xxx_power_header {
101 * Examples of packet headers:
104 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
107 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
110 struct am7xxx_header {
111 uint32_t packet_type;
113 uint8_t header_data_len;
117 struct am7xxx_generic_header data;
118 struct am7xxx_devinfo_header devinfo;
119 struct am7xxx_image_header image;
120 struct am7xxx_power_header power;
125 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
130 printf("Info header:\n");
131 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
132 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
133 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
134 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
137 static void dump_image_header(struct am7xxx_image_header *i)
142 printf("Image header:\n");
143 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
144 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
145 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
146 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
149 static void dump_power_header(struct am7xxx_power_header *p)
154 printf("Power header:\n");
155 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
156 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
157 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
160 static void dump_header(struct am7xxx_header *h)
165 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
166 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
167 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
168 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
169 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
171 switch(h->packet_type) {
172 case AM7XXX_PACKET_TYPE_DEVINFO:
173 dump_devinfo_header(&(h->header_data.devinfo));
176 case AM7XXX_PACKET_TYPE_IMAGE:
177 dump_image_header(&(h->header_data.image));
180 case AM7XXX_PACKET_TYPE_POWER:
181 dump_power_header(&(h->header_data.power));
185 printf("Packet type not supported!\n");
192 static inline unsigned int in_80chars(unsigned int i)
194 /* The 3 below is the length of "xx " where xx is the hex string
195 * representation of a byte */
196 return ((i+1) % (80/3));
199 static void dump_buffer(uint8_t *buffer, unsigned int len)
203 if (buffer == NULL || len == 0)
206 for (i = 0; i < len; i++) {
207 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
212 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
217 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
218 if (ret != 0 || (unsigned int)transferred != len) {
219 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
220 ret, transferred, len);
225 printf("\n<-- received\n");
226 dump_buffer(buffer, len);
233 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
239 printf("\nsending -->\n");
240 dump_buffer(buffer, len);
244 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
245 if (ret != 0 || (unsigned int)transferred != len) {
246 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
247 ret, transferred, len);
254 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
256 uint8_t **buffer_iterator = &buffer;
258 put_le32(h->packet_type, buffer_iterator);
259 put_8(h->unknown0, buffer_iterator);
260 put_8(h->header_data_len, buffer_iterator);
261 put_8(h->unknown2, buffer_iterator);
262 put_8(h->unknown3, buffer_iterator);
263 put_le32(h->header_data.data.field0, buffer_iterator);
264 put_le32(h->header_data.data.field1, buffer_iterator);
265 put_le32(h->header_data.data.field2, buffer_iterator);
266 put_le32(h->header_data.data.field3, buffer_iterator);
269 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
271 uint8_t **buffer_iterator = &buffer;
273 h->packet_type = get_le32(buffer_iterator);
274 h->unknown0 = get_8(buffer_iterator);
275 h->header_data_len = get_8(buffer_iterator);
276 h->unknown2 = get_8(buffer_iterator);
277 h->unknown3 = get_8(buffer_iterator);
278 h->header_data.data.field0 = get_le32(buffer_iterator);
279 h->header_data.data.field1 = get_le32(buffer_iterator);
280 h->header_data.data.field2 = get_le32(buffer_iterator);
281 h->header_data.data.field3 = get_le32(buffer_iterator);
284 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
288 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
292 unserialize_header(dev->buffer, h);
306 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
316 serialize_header(h, dev->buffer);
317 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
319 fprintf(stderr, "send_header: failed to send data.\n");
324 static am7xxx_device *add_new_device(am7xxx_device **devices_list)
326 am7xxx_device *new_device;
328 new_device = malloc(sizeof(*new_device));
329 if (new_device == NULL) {
333 memset(new_device, 0, sizeof(*new_device));
335 if (*devices_list == NULL) {
336 *devices_list = new_device;
338 am7xxx_device *prev = *devices_list;
341 prev->next = new_device;
346 static am7xxx_device *find_device(am7xxx_device *devices_list,
347 unsigned int device_index)
350 am7xxx_device *current = devices_list;
352 while (current && i++ < device_index)
353 current = current->next;
359 SCAN_OP_BUILD_DEVLIST,
364 * This is where the central logic of multi-device support is.
366 * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
367 * 'dev' are ignored; the function returns 0 on success and a negative value
370 * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
371 * device with index 'open_device_index' and returns the correspondent
372 * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
373 * 1 if the device was already open and a negative value on error.
376 * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
377 * the caller might want to call am7xxx_shutdown() in order to remove
378 * devices possibly added before the failure.
380 static int scan_devices(am7xxx_context *ctx, scan_op op,
381 unsigned int open_device_index, am7xxx_device **dev)
384 libusb_device** list;
385 unsigned int current_index;
390 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
393 if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
394 fprintf(stderr, "%s: device scan done already? Abort!\n", __func__);
398 num_devices = libusb_get_device_list(ctx->usb_context, &list);
399 if (num_devices < 0) {
405 for (i = 0; i < num_devices; i++) {
406 struct libusb_device_descriptor desc;
409 ret = libusb_get_device_descriptor(list[i], &desc);
413 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
414 if (desc.idVendor == supported_devices[j].vendor_id
415 && desc.idProduct == supported_devices[j].product_id) {
417 if (op == SCAN_OP_BUILD_DEVLIST) {
418 am7xxx_device *new_device;
420 printf("am7xxx device found, index: %d, name: %s\n",
422 supported_devices[j].name);
423 new_device = add_new_device(&(ctx->devices_list));
424 if (new_device == NULL) {
425 /* XXX, the caller may want
426 * to call am7xxx_shutdown() if
427 * we fail here, as we may have
428 * added some devices already
433 } else if (op == SCAN_OP_OPEN_DEVICE &&
434 current_index == open_device_index) {
436 *dev = find_device(ctx->devices_list, open_device_index);
442 /* the usb device has already been opened */
443 if ((*dev)->usb_device) {
448 ret = libusb_open(list[i], &((*dev)->usb_device));
452 libusb_set_configuration((*dev)->usb_device, 1);
453 libusb_claim_interface((*dev)->usb_device, 0);
461 /* if we made it up to here we didn't find any device to open */
462 if (op == SCAN_OP_OPEN_DEVICE) {
467 /* everything went fine when building the device list */
470 libusb_free_device_list(list, 1);
474 int am7xxx_init(am7xxx_context **ctx)
478 *ctx = malloc(sizeof(**ctx));
484 memset(*ctx, 0, sizeof(**ctx));
486 ret = libusb_init(&((*ctx)->usb_context));
488 goto out_free_context;
490 libusb_set_debug((*ctx)->usb_context, 3);
492 ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
494 fprintf(stderr, "%s: scan_devices failed\n", __func__);
495 am7xxx_shutdown(*ctx);
508 void am7xxx_shutdown(am7xxx_context *ctx)
510 am7xxx_device *current;
513 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
517 current = ctx->devices_list;
519 am7xxx_device *next = current->next;
520 am7xxx_close_device(current);
525 libusb_exit(ctx->usb_context);
530 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
531 unsigned int device_index)
536 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
540 ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
543 } else if (ret > 0) {
545 fprintf(stderr, "%s: device %d already open\n", __func__, device_index);
553 int am7xxx_close_device(am7xxx_device *dev)
556 fprintf(stderr, "%s: dev must not be NULL!\n", __func__);
559 if (dev->usb_device) {
560 libusb_release_interface(dev->usb_device, 0);
561 libusb_close(dev->usb_device);
562 dev->usb_device = NULL;
567 int am7xxx_get_device_info(am7xxx_device *dev,
568 unsigned int *native_width,
569 unsigned int *native_height,
570 unsigned int *unknown0,
571 unsigned int *unknown1)
574 struct am7xxx_header h = {
575 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
577 .header_data_len = 0x00,
590 ret = send_header(dev, &h);
594 ret = read_header(dev, &h);
598 *native_width = h.header_data.devinfo.native_width;
599 *native_height = h.header_data.devinfo.native_height;
600 *unknown0 = h.header_data.devinfo.unknown0;
601 *unknown1 = h.header_data.devinfo.unknown1;
606 int am7xxx_send_image(am7xxx_device *dev,
607 am7xxx_image_format format,
614 struct am7xxx_header h = {
615 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
617 .header_data_len = sizeof(struct am7xxx_image_header),
630 ret = send_header(dev, &h);
634 if (image == NULL || size == 0)
637 return send_data(dev, image, size);
640 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
643 struct am7xxx_header h = {
644 .packet_type = AM7XXX_PACKET_TYPE_POWER,
646 .header_data_len = sizeof(struct am7xxx_power_header),
652 case AM7XXX_POWER_OFF:
653 h.header_data.power.bit2 = 0;
654 h.header_data.power.bit1 = 0;
655 h.header_data.power.bit0 = 0;
658 case AM7XXX_POWER_LOW:
659 h.header_data.power.bit2 = 0;
660 h.header_data.power.bit1 = 0;
661 h.header_data.power.bit0 = 1;
663 case AM7XXX_POWER_MIDDLE:
664 h.header_data.power.bit2 = 0;
665 h.header_data.power.bit1 = 1;
666 h.header_data.power.bit0 = 0;
669 case AM7XXX_POWER_HIGH:
670 h.header_data.power.bit2 = 0;
671 h.header_data.power.bit1 = 1;
672 h.header_data.power.bit0 = 1;
675 case AM7XXX_POWER_TURBO:
676 h.header_data.power.bit2 = 1;
677 h.header_data.power.bit1 = 0;
678 h.header_data.power.bit0 = 0;
682 fprintf(stderr, "Unsupported power mode.\n");
686 ret = send_header(dev, &h);