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 AM7XXX_VENDOR_ID 0x1de1
29 #define AM7XXX_PRODUCT_ID 0xc101
31 /* The header size on the wire is known to be always 24 bytes, regardless of
32 * the memory configuration enforced by different architechtures or compilers
33 * for struct am7xxx_header
35 #define AM7XXX_HEADER_WIRE_SIZE 24
37 struct _am7xxx_device {
38 libusb_device_handle *usb_device;
39 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
43 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
44 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
45 AM7XXX_PACKET_TYPE_POWER = 0x04,
46 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
49 struct am7xxx_generic_header {
56 struct am7xxx_devinfo_header {
57 uint32_t native_width;
58 uint32_t native_height;
63 struct am7xxx_image_header {
70 struct am7xxx_power_header {
77 * Examples of packet headers:
80 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
83 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
86 struct am7xxx_header {
89 uint8_t header_data_len;
93 struct am7xxx_generic_header data;
94 struct am7xxx_devinfo_header devinfo;
95 struct am7xxx_image_header image;
96 struct am7xxx_power_header power;
101 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
106 printf("Info header:\n");
107 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
108 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
109 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
110 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
113 static void dump_image_header(struct am7xxx_image_header *i)
118 printf("Image header:\n");
119 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
120 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
121 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
122 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
125 static void dump_power_header(struct am7xxx_power_header *p)
130 printf("Power header:\n");
131 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
132 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
133 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
136 static void dump_header(struct am7xxx_header *h)
141 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
142 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
143 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
144 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
145 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
147 switch(h->packet_type) {
148 case AM7XXX_PACKET_TYPE_DEVINFO:
149 dump_devinfo_header(&(h->header_data.devinfo));
152 case AM7XXX_PACKET_TYPE_IMAGE:
153 dump_image_header(&(h->header_data.image));
156 case AM7XXX_PACKET_TYPE_POWER:
157 dump_power_header(&(h->header_data.power));
161 printf("Packet type not supported!\n");
168 static inline unsigned int in_80chars(unsigned int i)
170 /* The 3 below is the length of "xx " where xx is the hex string
171 * representation of a byte */
172 return ((i+1) % (80/3));
175 static void dump_buffer(uint8_t *buffer, unsigned int len)
179 if (buffer == NULL || len == 0)
182 for (i = 0; i < len; i++) {
183 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
188 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
193 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
194 if (ret != 0 || (unsigned int)transferred != len) {
195 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
196 ret, transferred, len);
201 printf("\n<-- received\n");
202 dump_buffer(buffer, len);
209 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
215 printf("\nsending -->\n");
216 dump_buffer(buffer, len);
220 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
221 if (ret != 0 || (unsigned int)transferred != len) {
222 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
223 ret, transferred, len);
230 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
232 uint8_t **buffer_iterator = &buffer;
234 put_le32(h->packet_type, buffer_iterator);
235 put_8(h->unknown0, buffer_iterator);
236 put_8(h->header_data_len, buffer_iterator);
237 put_8(h->unknown2, buffer_iterator);
238 put_8(h->unknown3, buffer_iterator);
239 put_le32(h->header_data.data.field0, buffer_iterator);
240 put_le32(h->header_data.data.field1, buffer_iterator);
241 put_le32(h->header_data.data.field2, buffer_iterator);
242 put_le32(h->header_data.data.field3, buffer_iterator);
245 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
247 uint8_t **buffer_iterator = &buffer;
249 h->packet_type = get_le32(buffer_iterator);
250 h->unknown0 = get_8(buffer_iterator);
251 h->header_data_len = get_8(buffer_iterator);
252 h->unknown2 = get_8(buffer_iterator);
253 h->unknown3 = get_8(buffer_iterator);
254 h->header_data.data.field0 = get_le32(buffer_iterator);
255 h->header_data.data.field1 = get_le32(buffer_iterator);
256 h->header_data.data.field2 = get_le32(buffer_iterator);
257 h->header_data.data.field3 = get_le32(buffer_iterator);
260 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
264 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
268 unserialize_header(dev->buffer, h);
282 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
292 serialize_header(h, dev->buffer);
293 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
295 fprintf(stderr, "send_header: failed to send data.\n");
300 am7xxx_device *am7xxx_init(void)
304 am7xxx_device *dev = malloc(sizeof(*dev));
309 memset(dev, 0, sizeof(*dev));
312 libusb_set_debug(NULL, 3);
314 dev->usb_device = libusb_open_device_with_vid_pid(NULL,
317 if (dev->usb_device == NULL) {
319 perror("libusb_open_device_with_vid_pid");
320 goto out_libusb_exit;
323 libusb_set_configuration(dev->usb_device, 1);
324 libusb_claim_interface(dev->usb_device, 0);
335 void am7xxx_shutdown(am7xxx_device *dev)
338 libusb_close(dev->usb_device);
345 int am7xxx_get_device_info(am7xxx_device *dev,
346 unsigned int *native_width,
347 unsigned int *native_height,
348 unsigned int *unknown0,
349 unsigned int *unknown1)
352 struct am7xxx_header h = {
353 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
355 .header_data_len = 0x00,
368 ret = send_header(dev, &h);
372 ret = read_header(dev, &h);
376 *native_width = h.header_data.devinfo.native_width;
377 *native_height = h.header_data.devinfo.native_height;
378 *unknown0 = h.header_data.devinfo.unknown0;
379 *unknown1 = h.header_data.devinfo.unknown1;
384 int am7xxx_send_image(am7xxx_device *dev,
385 am7xxx_image_format format,
392 struct am7xxx_header h = {
393 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
395 .header_data_len = sizeof(struct am7xxx_image_header),
408 ret = send_header(dev, &h);
412 if (image == NULL || size == 0)
415 return send_data(dev, image, size);
418 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
421 struct am7xxx_header h = {
422 .packet_type = AM7XXX_PACKET_TYPE_POWER,
424 .header_data_len = sizeof(struct am7xxx_power_header),
430 case AM7XXX_POWER_OFF:
431 h.header_data.power.bit2 = 0;
432 h.header_data.power.bit1 = 0;
433 h.header_data.power.bit0 = 0;
436 case AM7XXX_POWER_LOW:
437 h.header_data.power.bit2 = 0;
438 h.header_data.power.bit1 = 0;
439 h.header_data.power.bit0 = 1;
441 case AM7XXX_POWER_MIDDLE:
442 h.header_data.power.bit2 = 0;
443 h.header_data.power.bit1 = 1;
444 h.header_data.power.bit0 = 0;
447 case AM7XXX_POWER_HIGH:
448 h.header_data.power.bit2 = 0;
449 h.header_data.power.bit1 = 1;
450 h.header_data.power.bit0 = 1;
453 case AM7XXX_POWER_TURBO:
454 h.header_data.power.bit2 = 1;
455 h.header_data.power.bit1 = 0;
456 h.header_data.power.bit0 = 0;
460 fprintf(stderr, "Unsupported power mode.\n");
464 ret = send_header(dev, &h);