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 ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30 struct am7xxx_usb_device_descriptor {
36 static struct am7xxx_usb_device_descriptor supported_devices[] = {
43 .name = "Philips/Sagemcom PicoPix 1020",
49 /* The header size on the wire is known to be always 24 bytes, regardless of
50 * the memory configuration enforced by different architechtures or compilers
51 * for struct am7xxx_header
53 #define AM7XXX_HEADER_WIRE_SIZE 24
55 struct _am7xxx_device {
56 libusb_device_handle *usb_device;
57 uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
61 AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
62 AM7XXX_PACKET_TYPE_IMAGE = 0x02,
63 AM7XXX_PACKET_TYPE_POWER = 0x04,
64 AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
67 struct am7xxx_generic_header {
74 struct am7xxx_devinfo_header {
75 uint32_t native_width;
76 uint32_t native_height;
81 struct am7xxx_image_header {
88 struct am7xxx_power_header {
95 * Examples of packet headers:
98 * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
101 * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
104 struct am7xxx_header {
105 uint32_t packet_type;
107 uint8_t header_data_len;
111 struct am7xxx_generic_header data;
112 struct am7xxx_devinfo_header devinfo;
113 struct am7xxx_image_header image;
114 struct am7xxx_power_header power;
119 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
124 printf("Info header:\n");
125 printf("\tnative_width: 0x%08x (%u)\n", d->native_width, d->native_width);
126 printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
127 printf("\tunknown0: 0x%08x (%u)\n", d->unknown0, d->unknown0);
128 printf("\tunknown1: 0x%08x (%u)\n", d->unknown1, d->unknown1);
131 static void dump_image_header(struct am7xxx_image_header *i)
136 printf("Image header:\n");
137 printf("\tformat: 0x%08x (%u)\n", i->format, i->format);
138 printf("\twidth: 0x%08x (%u)\n", i->width, i->width);
139 printf("\theight: 0x%08x (%u)\n", i->height, i->height);
140 printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
143 static void dump_power_header(struct am7xxx_power_header *p)
148 printf("Power header:\n");
149 printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
150 printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
151 printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
154 static void dump_header(struct am7xxx_header *h)
159 printf("packet_type: 0x%08x (%u)\n", h->packet_type, h->packet_type);
160 printf("unknown0: 0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
161 printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
162 printf("unknown2: 0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
163 printf("unknown3: 0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
165 switch(h->packet_type) {
166 case AM7XXX_PACKET_TYPE_DEVINFO:
167 dump_devinfo_header(&(h->header_data.devinfo));
170 case AM7XXX_PACKET_TYPE_IMAGE:
171 dump_image_header(&(h->header_data.image));
174 case AM7XXX_PACKET_TYPE_POWER:
175 dump_power_header(&(h->header_data.power));
179 printf("Packet type not supported!\n");
186 static inline unsigned int in_80chars(unsigned int i)
188 /* The 3 below is the length of "xx " where xx is the hex string
189 * representation of a byte */
190 return ((i+1) % (80/3));
193 static void dump_buffer(uint8_t *buffer, unsigned int len)
197 if (buffer == NULL || len == 0)
200 for (i = 0; i < len; i++) {
201 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
206 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
211 ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
212 if (ret != 0 || (unsigned int)transferred != len) {
213 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
214 ret, transferred, len);
219 printf("\n<-- received\n");
220 dump_buffer(buffer, len);
227 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
233 printf("\nsending -->\n");
234 dump_buffer(buffer, len);
238 ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
239 if (ret != 0 || (unsigned int)transferred != len) {
240 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
241 ret, transferred, len);
248 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
250 uint8_t **buffer_iterator = &buffer;
252 put_le32(h->packet_type, buffer_iterator);
253 put_8(h->unknown0, buffer_iterator);
254 put_8(h->header_data_len, buffer_iterator);
255 put_8(h->unknown2, buffer_iterator);
256 put_8(h->unknown3, buffer_iterator);
257 put_le32(h->header_data.data.field0, buffer_iterator);
258 put_le32(h->header_data.data.field1, buffer_iterator);
259 put_le32(h->header_data.data.field2, buffer_iterator);
260 put_le32(h->header_data.data.field3, buffer_iterator);
263 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
265 uint8_t **buffer_iterator = &buffer;
267 h->packet_type = get_le32(buffer_iterator);
268 h->unknown0 = get_8(buffer_iterator);
269 h->header_data_len = get_8(buffer_iterator);
270 h->unknown2 = get_8(buffer_iterator);
271 h->unknown3 = get_8(buffer_iterator);
272 h->header_data.data.field0 = get_le32(buffer_iterator);
273 h->header_data.data.field1 = get_le32(buffer_iterator);
274 h->header_data.data.field2 = get_le32(buffer_iterator);
275 h->header_data.data.field3 = get_le32(buffer_iterator);
278 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
282 ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
286 unserialize_header(dev->buffer, h);
300 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
310 serialize_header(h, dev->buffer);
311 ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
313 fprintf(stderr, "send_header: failed to send data.\n");
318 am7xxx_device *am7xxx_init(void)
322 am7xxx_device *dev = malloc(sizeof(*dev));
327 memset(dev, 0, sizeof(*dev));
330 libusb_set_debug(NULL, 3);
332 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
333 dev->usb_device = libusb_open_device_with_vid_pid(NULL,
334 supported_devices[i].vendor_id,
335 supported_devices[i].product_id);
336 if (dev->usb_device) {
337 printf("%s: device found: %s\n", __func__, supported_devices[i].name);
341 if (dev->usb_device == NULL) {
343 perror("libusb_open_device_with_vid_pid");
344 goto out_libusb_exit;
347 libusb_set_configuration(dev->usb_device, 1);
348 libusb_claim_interface(dev->usb_device, 0);
359 void am7xxx_shutdown(am7xxx_device *dev)
362 libusb_close(dev->usb_device);
369 int am7xxx_get_device_info(am7xxx_device *dev,
370 unsigned int *native_width,
371 unsigned int *native_height,
372 unsigned int *unknown0,
373 unsigned int *unknown1)
376 struct am7xxx_header h = {
377 .packet_type = AM7XXX_PACKET_TYPE_DEVINFO,
379 .header_data_len = 0x00,
392 ret = send_header(dev, &h);
396 ret = read_header(dev, &h);
400 *native_width = h.header_data.devinfo.native_width;
401 *native_height = h.header_data.devinfo.native_height;
402 *unknown0 = h.header_data.devinfo.unknown0;
403 *unknown1 = h.header_data.devinfo.unknown1;
408 int am7xxx_send_image(am7xxx_device *dev,
409 am7xxx_image_format format,
416 struct am7xxx_header h = {
417 .packet_type = AM7XXX_PACKET_TYPE_IMAGE,
419 .header_data_len = sizeof(struct am7xxx_image_header),
432 ret = send_header(dev, &h);
436 if (image == NULL || size == 0)
439 return send_data(dev, image, size);
442 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
445 struct am7xxx_header h = {
446 .packet_type = AM7XXX_PACKET_TYPE_POWER,
448 .header_data_len = sizeof(struct am7xxx_power_header),
454 case AM7XXX_POWER_OFF:
455 h.header_data.power.bit2 = 0;
456 h.header_data.power.bit1 = 0;
457 h.header_data.power.bit0 = 0;
460 case AM7XXX_POWER_LOW:
461 h.header_data.power.bit2 = 0;
462 h.header_data.power.bit1 = 0;
463 h.header_data.power.bit0 = 1;
465 case AM7XXX_POWER_MIDDLE:
466 h.header_data.power.bit2 = 0;
467 h.header_data.power.bit1 = 1;
468 h.header_data.power.bit0 = 0;
471 case AM7XXX_POWER_HIGH:
472 h.header_data.power.bit2 = 0;
473 h.header_data.power.bit1 = 1;
474 h.header_data.power.bit0 = 1;
477 case AM7XXX_POWER_TURBO:
478 h.header_data.power.bit2 = 1;
479 h.header_data.power.bit1 = 0;
480 h.header_data.power.bit0 = 0;
484 fprintf(stderr, "Unsupported power mode.\n");
488 ret = send_header(dev, &h);