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 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
31 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
32 AM7XXX_PACKET_TYPE_POWER = 0x04,
33 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
36 struct am7xxx_generic_header {
43 struct am7xxx_devinfo_header {
44 uint32_t native_width;
45 uint32_t native_height;
50 struct am7xxx_image_header {
57 struct am7xxx_power_header {
64 * Examples of packet headers:
67 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
70 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
73 /* The header size on the wire is known to be always 24 bytes, regardless of
74 * the memory configuration enforced by different architechtures or compilers
75 * for struct am7xxx_header
77 #define AM7XXX_HEADER_WIRE_SIZE 24
79 struct am7xxx_header {
82 uint8_t header_data_len;
86 struct am7xxx_generic_header data;
87 struct am7xxx_devinfo_header devinfo;
88 struct am7xxx_image_header image;
89 struct am7xxx_power_header power;
94 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
99 printf("Info header:\n");
100 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
101 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
102 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
103 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
106 static void dump_image_header(struct am7xxx_image_header *i)
111 printf("Image header:\n");
112 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
113 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
114 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
115 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
118 static void dump_power_header(struct am7xxx_power_header *p)
123 printf("Power header:\n");
124 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
125 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
126 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
129 static void dump_header(struct am7xxx_header *h)
134 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
135 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
136 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
137 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
138 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
140 switch(h->packet_type) {
141 case AM7XXX_PACKET_TYPE_DEVINFO:
142 dump_devinfo_header(&(h->header_data.devinfo));
145 case AM7XXX_PACKET_TYPE_IMAGE:
146 dump_image_header(&(h->header_data.image));
149 case AM7XXX_PACKET_TYPE_POWER:
150 dump_power_header(&(h->header_data.power));
154 printf("Packet type not supported!\n");
161 static inline unsigned int in_80chars(unsigned int i)
163 /* The 3 below is the length of "xx " where xx is the hex string
164 * representation of a byte */
165 return ((i+1) % (80/3));
168 static void dump_buffer(uint8_t *buffer, unsigned int len)
172 if (buffer == NULL || len == 0)
175 for (i = 0; i < len; i++) {
176 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
181 static int read_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
186 ret = libusb_bulk_transfer(dev, 0x81, buffer, len, &transferred, 0);
187 if (ret != 0 || (unsigned int)transferred != len) {
188 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
189 ret, transferred, len);
194 printf("\n<-- received\n");
195 dump_buffer(buffer, len);
202 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
208 printf("\nsending -->\n");
209 dump_buffer(buffer, len);
213 ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
214 if (ret != 0 || (unsigned int)transferred != len) {
215 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
216 ret, transferred, len);
223 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
225 uint8_t **buffer_iterator = &buffer;
227 put_le32(h->packet_type, buffer_iterator);
228 put_8(h->unknown0, buffer_iterator);
229 put_8(h->header_data_len, buffer_iterator);
230 put_8(h->unknown2, buffer_iterator);
231 put_8(h->unknown3, buffer_iterator);
232 put_le32(h->header_data.data.field0, buffer_iterator);
233 put_le32(h->header_data.data.field1, buffer_iterator);
234 put_le32(h->header_data.data.field2, buffer_iterator);
235 put_le32(h->header_data.data.field3, buffer_iterator);
238 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
240 uint8_t **buffer_iterator = &buffer;
242 h->packet_type = get_le32(buffer_iterator);
243 h->unknown0 = get_8(buffer_iterator);
244 h->header_data_len = get_8(buffer_iterator);
245 h->unknown2 = get_8(buffer_iterator);
246 h->unknown3 = get_8(buffer_iterator);
247 h->header_data.data.field0 = get_le32(buffer_iterator);
248 h->header_data.data.field1 = get_le32(buffer_iterator);
249 h->header_data.data.field2 = get_le32(buffer_iterator);
250 h->header_data.data.field3 = get_le32(buffer_iterator);
253 static int read_header(am7xxx_device dev, struct am7xxx_header *h)
258 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
259 if (buffer == NULL) {
260 perror("calloc buffer");
264 ret = read_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
268 unserialize_header(buffer, h);
283 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
294 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
295 if (buffer == NULL) {
296 perror("calloc buffer");
300 serialize_header(h, buffer);
301 ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
303 fprintf(stderr, "send_header: failed to send data.\n");
309 am7xxx_device am7xxx_init(void)
314 libusb_set_debug(NULL, 3);
316 dev = libusb_open_device_with_vid_pid(NULL,
321 perror("libusb_open_device_with_vid_pid");
322 goto out_libusb_exit;
325 libusb_set_configuration(dev, 1);
326 libusb_claim_interface(dev, 0);
335 void am7xxx_shutdown(am7xxx_device dev)
343 int am7xxx_get_device_info(am7xxx_device dev,
344 unsigned int *native_width,
345 unsigned int *native_height,
346 unsigned int *unknown0,
347 unsigned int *unknown1)
350 struct am7xxx_header h = {
351 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
353 .header_data_len = 0x00,
366 ret = send_header(dev, &h);
370 ret = read_header(dev, &h);
374 *native_width = h.header_data.devinfo.native_width;
375 *native_height = h.header_data.devinfo.native_height;
376 *unknown0 = h.header_data.devinfo.unknown0;
377 *unknown1 = h.header_data.devinfo.unknown1;
382 int am7xxx_send_image(am7xxx_device dev,
383 am7xxx_image_format format,
390 struct am7xxx_header h = {
391 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
393 .header_data_len = sizeof(struct am7xxx_image_header),
406 ret = send_header(dev, &h);
410 if (image == NULL || size == 0)
413 return send_data(dev, image, size);
416 int am7xxx_set_power_mode(am7xxx_device dev, am7xxx_power_mode mode)
419 struct am7xxx_header h = {
420 .packet_type = AM7XXX_PACKET_TYPE_POWER,
422 .header_data_len = sizeof(struct am7xxx_power_header),
428 case AM7XXX_POWER_OFF:
429 h.header_data.power.bit2 = 0;
430 h.header_data.power.bit1 = 0;
431 h.header_data.power.bit0 = 0;
434 case AM7XXX_POWER_LOW:
435 h.header_data.power.bit2 = 0;
436 h.header_data.power.bit1 = 0;
437 h.header_data.power.bit0 = 1;
439 case AM7XXX_POWER_MIDDLE:
440 h.header_data.power.bit2 = 0;
441 h.header_data.power.bit1 = 1;
442 h.header_data.power.bit0 = 0;
445 case AM7XXX_POWER_HIGH:
446 h.header_data.power.bit2 = 0;
447 h.header_data.power.bit1 = 1;
448 h.header_data.power.bit0 = 1;
451 case AM7XXX_POWER_TURBO:
452 h.header_data.power.bit2 = 1;
453 h.header_data.power.bit1 = 0;
454 h.header_data.power.bit0 = 0;
458 fprintf(stderr, "Unsupported power mode.\n");
462 ret = send_header(dev, &h);