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/>.
24 #include "serialize.h"
26 #define AM7XXX_VENDOR_ID 0x1de1
27 #define AM7XXX_PRODUCT_ID 0xc101
30 static uint8_t reference_image_header[] = {
31 0x02, 0x00, 0x00, 0x00,
36 0x01, 0x00, 0x00, 0x00,
37 0x20, 0x03, 0x00, 0x00,
38 0xe0, 0x01, 0x00, 0x00,
39 0x53, 0xE8, 0x00, 0x00
43 static void dump_image_header(struct am7xxx_image_header *i)
48 printf("Image header:\n");
49 printf("format: 0x%08x (%u)\n", i->format, i->format);
50 printf("width: 0x%08x (%u)\n", i->width, i->width);
51 printf("height: 0x%08x (%u)\n", i->height, i->height);
52 printf("image size: 0x%08x (%u)\n", i->image_size, i->image_size);
55 static void dump_header(struct am7xxx_header *h)
60 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
61 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
62 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
63 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
64 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
66 switch(h->packet_type) {
67 case AM7XXX_PACKET_TYPE_IMAGE:
68 dump_image_header(&(h->header_data.image));
72 printf("Packet type not supported!\n");
79 static inline unsigned int in_80chars(unsigned int i)
81 return ((i+1) % (80/3));
84 static void dump_buffer(uint8_t *buffer, unsigned int len)
88 if (buffer == NULL || len == 0)
91 for (i = 0; i < len; i++) {
92 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
97 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
102 dump_buffer(buffer, len);
104 ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
105 if (ret != 0 || (unsigned int)transferred != len) {
106 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
107 ret, transferred, len);
114 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
116 uint8_t **buffer_iterator = &buffer;
118 put_le32(h->packet_type, buffer_iterator);
119 put_8(h->unknown0, buffer_iterator);
120 put_8(h->header_data_len, buffer_iterator);
121 put_8(h->unknown2, buffer_iterator);
122 put_8(h->unknown3, buffer_iterator);
123 put_le32(h->header_data.data.field0, buffer_iterator);
124 put_le32(h->header_data.data.field1, buffer_iterator);
125 put_le32(h->header_data.data.field2, buffer_iterator);
126 put_le32(h->header_data.data.field3, buffer_iterator);
129 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
134 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
135 if (buffer == NULL) {
136 perror("calloc buffer");
140 serialize_header(h, buffer);
141 ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
143 fprintf(stderr, "send_header: failed to send data.\n");
149 am7xxx_device am7xxx_init(void)
154 libusb_set_debug(NULL, 3);
156 dev = libusb_open_device_with_vid_pid(NULL,
161 perror("libusb_open_device_with_vid_pid");
162 goto out_libusb_exit;
165 libusb_set_configuration(dev, 1);
166 libusb_claim_interface(dev, 0);
175 void am7xxx_shutdown(am7xxx_device dev)
183 int am7xxx_send_image(am7xxx_device dev,
184 am7xxx_image_format format,
191 struct am7xxx_header h = {
192 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
194 .header_data_len = sizeof(struct am7xxx_image_header),
210 printf("Dump Buffers\n");
211 dump_buffer(reference_image_header, sizeof(struct am7xxx_header));
213 ret = send_header(dev, &h);
217 if (image == NULL || size == 0)
220 return send_data(dev, image, size);