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
29 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
34 printf("Info header:\n");
35 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
36 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
37 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
38 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
41 static void dump_image_header(struct am7xxx_image_header *i)
46 printf("Image header:\n");
47 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
48 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
49 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
50 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
53 static void dump_power_header(struct am7xxx_power_header *p)
58 printf("Power header:\n");
59 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
60 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
61 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
64 static void dump_header(struct am7xxx_header *h)
69 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
70 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
71 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
72 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
73 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
75 switch(h->packet_type) {
76 case AM7XXX_PACKET_TYPE_DEVINFO:
77 dump_devinfo_header(&(h->header_data.devinfo));
80 case AM7XXX_PACKET_TYPE_IMAGE:
81 dump_image_header(&(h->header_data.image));
84 case AM7XXX_PACKET_TYPE_POWER:
85 dump_power_header(&(h->header_data.power));
89 printf("Packet type not supported!\n");
96 static inline unsigned int in_80chars(unsigned int i)
98 /* The 3 below is the length of "xx " where xx is the hex string
99 * representation of a byte */
100 return ((i+1) % (80/3));
103 static void dump_buffer(uint8_t *buffer, unsigned int len)
107 if (buffer == NULL || len == 0)
110 for (i = 0; i < len; i++) {
111 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
116 static int read_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
121 ret = libusb_bulk_transfer(dev, 0x81, buffer, len, &transferred, 0);
122 if (ret != 0 || (unsigned int)transferred != len) {
123 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
124 ret, transferred, len);
129 printf("\n<-- received\n");
130 dump_buffer(buffer, len);
137 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
143 printf("\nsending -->\n");
144 dump_buffer(buffer, len);
148 ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
149 if (ret != 0 || (unsigned int)transferred != len) {
150 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
151 ret, transferred, len);
158 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
160 uint8_t **buffer_iterator = &buffer;
162 put_le32(h->packet_type, buffer_iterator);
163 put_8(h->unknown0, buffer_iterator);
164 put_8(h->header_data_len, buffer_iterator);
165 put_8(h->unknown2, buffer_iterator);
166 put_8(h->unknown3, buffer_iterator);
167 put_le32(h->header_data.data.field0, buffer_iterator);
168 put_le32(h->header_data.data.field1, buffer_iterator);
169 put_le32(h->header_data.data.field2, buffer_iterator);
170 put_le32(h->header_data.data.field3, buffer_iterator);
173 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
175 uint8_t **buffer_iterator = &buffer;
177 h->packet_type = get_le32(buffer_iterator);
178 h->unknown0 = get_8(buffer_iterator);
179 h->header_data_len = get_8(buffer_iterator);
180 h->unknown2 = get_8(buffer_iterator);
181 h->unknown3 = get_8(buffer_iterator);
182 h->header_data.data.field0 = get_le32(buffer_iterator);
183 h->header_data.data.field1 = get_le32(buffer_iterator);
184 h->header_data.data.field2 = get_le32(buffer_iterator);
185 h->header_data.data.field3 = get_le32(buffer_iterator);
188 static int read_header(am7xxx_device dev, struct am7xxx_header *h)
193 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
194 if (buffer == NULL) {
195 perror("calloc buffer");
199 ret = read_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
203 unserialize_header(buffer, h);
218 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
229 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
230 if (buffer == NULL) {
231 perror("calloc buffer");
235 serialize_header(h, buffer);
236 ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
238 fprintf(stderr, "send_header: failed to send data.\n");
244 am7xxx_device am7xxx_init(void)
249 libusb_set_debug(NULL, 3);
251 dev = libusb_open_device_with_vid_pid(NULL,
256 perror("libusb_open_device_with_vid_pid");
257 goto out_libusb_exit;
260 libusb_set_configuration(dev, 1);
261 libusb_claim_interface(dev, 0);
270 void am7xxx_shutdown(am7xxx_device dev)
278 int am7xxx_get_device_info(am7xxx_device dev,
279 unsigned int *native_width,
280 unsigned int *native_height,
281 unsigned int *unknown0,
282 unsigned int *unknown1)
285 struct am7xxx_header h = {
286 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
288 .header_data_len = 0x00,
301 ret = send_header(dev, &h);
305 ret = read_header(dev, &h);
309 *native_width = h.header_data.devinfo.native_width;
310 *native_height = h.header_data.devinfo.native_height;
311 *unknown0 = h.header_data.devinfo.unknown0;
312 *unknown1 = h.header_data.devinfo.unknown1;
317 int am7xxx_send_image(am7xxx_device dev,
318 am7xxx_image_format format,
325 struct am7xxx_header h = {
326 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
328 .header_data_len = sizeof(struct am7xxx_image_header),
341 ret = send_header(dev, &h);
345 if (image == NULL || size == 0)
348 return send_data(dev, image, size);
351 int am7xxx_set_power_mode(am7xxx_device dev, am7xxx_power_mode mode)
354 struct am7xxx_header h = {
355 .packet_type = AM7XXX_PACKET_TYPE_POWER,
357 .header_data_len = sizeof(struct am7xxx_power_header),
363 case AM7XXX_POWER_OFF:
364 h.header_data.power.bit2 = 0;
365 h.header_data.power.bit1 = 0;
366 h.header_data.power.bit0 = 0;
369 case AM7XXX_POWER_LOW:
370 h.header_data.power.bit2 = 0;
371 h.header_data.power.bit1 = 0;
372 h.header_data.power.bit0 = 1;
374 case AM7XXX_POWER_MIDDLE:
375 h.header_data.power.bit2 = 0;
376 h.header_data.power.bit1 = 1;
377 h.header_data.power.bit0 = 0;
380 case AM7XXX_POWER_HIGH:
381 h.header_data.power.bit2 = 0;
382 h.header_data.power.bit1 = 1;
383 h.header_data.power.bit0 = 1;
386 case AM7XXX_POWER_TURBO:
387 h.header_data.power.bit2 = 1;
388 h.header_data.power.bit1 = 0;
389 h.header_data.power.bit0 = 0;
393 fprintf(stderr, "Unsupported power mode.\n");
397 ret = send_header(dev, &h);