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_image_header(struct am7xxx_image_header *i)
34 printf("Image header:\n");
35 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
36 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
37 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
38 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
41 static void dump_header(struct am7xxx_header *h)
46 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
47 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
48 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
49 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
50 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
52 switch(h->packet_type) {
53 case AM7XXX_PACKET_TYPE_IMAGE:
54 dump_image_header(&(h->header_data.image));
58 printf("Packet type not supported!\n");
65 static inline unsigned int in_80chars(unsigned int i)
67 /* The 3 below is the length of "xx " where xx is the hex string
68 * representation of a byte */
69 return ((i+1) % (80/3));
72 static void dump_buffer(uint8_t *buffer, unsigned int len)
76 if (buffer == NULL || len == 0)
79 for (i = 0; i < len; i++) {
80 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
85 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
91 dump_buffer(buffer, len);
95 ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
96 if (ret != 0 || (unsigned int)transferred != len) {
97 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
98 ret, transferred, len);
105 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
107 uint8_t **buffer_iterator = &buffer;
109 put_le32(h->packet_type, buffer_iterator);
110 put_8(h->unknown0, buffer_iterator);
111 put_8(h->header_data_len, buffer_iterator);
112 put_8(h->unknown2, buffer_iterator);
113 put_8(h->unknown3, buffer_iterator);
114 put_le32(h->header_data.data.field0, buffer_iterator);
115 put_le32(h->header_data.data.field1, buffer_iterator);
116 put_le32(h->header_data.data.field2, buffer_iterator);
117 put_le32(h->header_data.data.field3, buffer_iterator);
120 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
130 buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
131 if (buffer == NULL) {
132 perror("calloc buffer");
136 serialize_header(h, buffer);
137 ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
139 fprintf(stderr, "send_header: failed to send data.\n");
145 am7xxx_device am7xxx_init(void)
150 libusb_set_debug(NULL, 3);
152 dev = libusb_open_device_with_vid_pid(NULL,
157 perror("libusb_open_device_with_vid_pid");
158 goto out_libusb_exit;
161 libusb_set_configuration(dev, 1);
162 libusb_claim_interface(dev, 0);
171 void am7xxx_shutdown(am7xxx_device dev)
179 int am7xxx_send_image(am7xxx_device dev,
180 am7xxx_image_format format,
187 struct am7xxx_header h = {
188 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
190 .header_data_len = sizeof(struct am7xxx_image_header),
203 ret = send_header(dev, &h);
207 if (image == NULL || size == 0)
210 return send_data(dev, image, size);