cmake: fix libusb search
[libam7xxx.git] / src / am7xxx.c
1 /* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs
2  *
3  * Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <libusb.h>
24
25 #include "am7xxx.h"
26 #include "serialize.h"
27
28 #define AM7XXX_VENDOR_ID  0x1de1
29 #define AM7XXX_PRODUCT_ID 0xc101
30
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
34  */
35 #define AM7XXX_HEADER_WIRE_SIZE 24
36
37 struct _am7xxx_device {
38         libusb_device_handle *usb_device;
39         uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
40 };
41
42 typedef enum {
43         AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
44         AM7XXX_PACKET_TYPE_IMAGE   = 0x02,
45         AM7XXX_PACKET_TYPE_POWER   = 0x04,
46         AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
47 } am7xxx_packet_type;
48
49 struct am7xxx_generic_header {
50         uint32_t field0;
51         uint32_t field1;
52         uint32_t field2;
53         uint32_t field3;
54 };
55
56 struct am7xxx_devinfo_header {
57         uint32_t native_width;
58         uint32_t native_height;
59         uint32_t unknown0;
60         uint32_t unknown1;
61 };
62
63 struct am7xxx_image_header {
64         uint32_t format;
65         uint32_t width;
66         uint32_t height;
67         uint32_t image_size;
68 };
69
70 struct am7xxx_power_header {
71         uint32_t bit2;
72         uint32_t bit1;
73         uint32_t bit0;
74 };
75
76 /*
77  * Examples of packet headers:
78  *
79  * Image header:
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
81  *
82  * Power header:
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
84  */
85
86 struct am7xxx_header {
87         uint32_t packet_type;
88         uint8_t unknown0;
89         uint8_t header_data_len;
90         uint8_t unknown2;
91         uint8_t unknown3;
92         union {
93                 struct am7xxx_generic_header data;
94                 struct am7xxx_devinfo_header devinfo;
95                 struct am7xxx_image_header image;
96                 struct am7xxx_power_header power;
97         } header_data;
98 };
99
100
101 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
102 {
103         if (d == NULL)
104                 return;
105
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);
111 }
112
113 static void dump_image_header(struct am7xxx_image_header *i)
114 {
115         if (i == NULL)
116                 return;
117
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);
123 }
124
125 static void dump_power_header(struct am7xxx_power_header *p)
126 {
127         if (p == NULL)
128                 return;
129
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);
134 }
135
136 static void dump_header(struct am7xxx_header *h)
137 {
138         if (h == NULL)
139                 return;
140
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);
146
147         switch(h->packet_type) {
148         case AM7XXX_PACKET_TYPE_DEVINFO:
149                 dump_devinfo_header(&(h->header_data.devinfo));
150                 break;
151
152         case AM7XXX_PACKET_TYPE_IMAGE:
153                 dump_image_header(&(h->header_data.image));
154                 break;
155
156         case AM7XXX_PACKET_TYPE_POWER:
157                 dump_power_header(&(h->header_data.power));
158                 break;
159
160         default:
161                 printf("Packet type not supported!\n");
162                 break;
163         }
164
165         fflush(stdout);
166 }
167
168 static inline unsigned int in_80chars(unsigned int i)
169 {
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));
173 }
174
175 static void dump_buffer(uint8_t *buffer, unsigned int len)
176 {
177         unsigned int i;
178
179         if (buffer == NULL || len == 0)
180                 return;
181
182         for (i = 0; i < len; i++) {
183                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
184         }
185         fflush(stdout);
186 }
187
188 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
189 {
190         int ret;
191         int transferred = 0;
192
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);
197                 return ret;
198         }
199
200 #if DEBUG
201         printf("\n<-- received\n");
202         dump_buffer(buffer, len);
203         printf("\n");
204 #endif
205
206         return 0;
207 }
208
209 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
210 {
211         int ret;
212         int transferred = 0;
213
214 #if DEBUG
215         printf("\nsending -->\n");
216         dump_buffer(buffer, len);
217         printf("\n");
218 #endif
219
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);
224                 return ret;
225         }
226
227         return 0;
228 }
229
230 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
231 {
232         uint8_t **buffer_iterator = &buffer;
233
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);
243 }
244
245 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
246 {
247         uint8_t **buffer_iterator = &buffer;
248
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);
258 }
259
260 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
261 {
262         int ret;
263
264         ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
265         if (ret < 0)
266                 goto out;
267
268         unserialize_header(dev->buffer, h);
269
270 #if DEBUG
271         printf("\n");
272         dump_header(h);
273         printf("\n");
274 #endif
275
276         ret = 0;
277
278 out:
279         return ret;
280 }
281
282 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
283 {
284         int ret;
285
286 #if DEBUG
287         printf("\n");
288         dump_header(h);
289         printf("\n");
290 #endif
291
292         serialize_header(h, dev->buffer);
293         ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
294         if (ret < 0)
295                 fprintf(stderr, "send_header: failed to send data.\n");
296
297         return ret;
298 }
299
300 am7xxx_device *am7xxx_init(void)
301 {
302         unsigned int i;
303
304         am7xxx_device *dev = malloc(sizeof(*dev));
305         if (dev == NULL) {
306                 perror("malloc");
307                 goto out;
308         }
309         memset(dev, 0, sizeof(*dev));
310
311         libusb_init(NULL);
312         libusb_set_debug(NULL, 3);
313
314         dev->usb_device = libusb_open_device_with_vid_pid(NULL,
315                                               AM7XXX_VENDOR_ID,
316                                               AM7XXX_PRODUCT_ID);
317         if (dev->usb_device == NULL) {
318                 errno = ENODEV;
319                 perror("libusb_open_device_with_vid_pid");
320                 goto out_libusb_exit;
321         }
322
323         libusb_set_configuration(dev->usb_device, 1);
324         libusb_claim_interface(dev->usb_device, 0);
325
326         return dev;
327
328 out_libusb_exit:
329         libusb_exit(NULL);
330         free(dev);
331 out:
332         return NULL;
333 }
334
335 void am7xxx_shutdown(am7xxx_device *dev)
336 {
337         if (dev) {
338                 libusb_close(dev->usb_device);
339                 libusb_exit(NULL);
340                 free(dev);
341                 dev = NULL;
342         }
343 }
344
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)
350 {
351         int ret;
352         struct am7xxx_header h = {
353                 .packet_type     = AM7XXX_PACKET_TYPE_DEVINFO,
354                 .unknown0        = 0x00,
355                 .header_data_len = 0x00,
356                 .unknown2        = 0x3e,
357                 .unknown3        = 0x10,
358                 .header_data = {
359                         .devinfo = {
360                                 .native_width  = 0,
361                                 .native_height = 0,
362                                 .unknown0      = 0,
363                                 .unknown1      = 0,
364                         },
365                 },
366         };
367
368         ret = send_header(dev, &h);
369         if (ret < 0)
370                 return ret;
371
372         ret = read_header(dev, &h);
373         if (ret < 0)
374                 return ret;
375
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;
380
381         return 0;
382 }
383
384 int am7xxx_send_image(am7xxx_device *dev,
385                       am7xxx_image_format format,
386                       unsigned int width,
387                       unsigned int height,
388                       uint8_t *image,
389                       unsigned int size)
390 {
391         int ret;
392         struct am7xxx_header h = {
393                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
394                 .unknown0        = 0x00,
395                 .header_data_len = sizeof(struct am7xxx_image_header),
396                 .unknown2        = 0x3e,
397                 .unknown3        = 0x10,
398                 .header_data = {
399                         .image = {
400                                 .format     = format,
401                                 .width      = width,
402                                 .height     = height,
403                                 .image_size = size,
404                         },
405                 },
406         };
407
408         ret = send_header(dev, &h);
409         if (ret < 0)
410                 return ret;
411
412         if (image == NULL || size == 0)
413                 return 0;
414
415         return send_data(dev, image, size);
416 }
417
418 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
419 {
420         int ret;
421         struct am7xxx_header h = {
422                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
423                 .unknown0        = 0x00,
424                 .header_data_len = sizeof(struct am7xxx_power_header),
425                 .unknown2        = 0x3e,
426                 .unknown3        = 0x10,
427         };
428
429         switch(mode) {
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;
434                 break;
435
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;
440
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;
445                 break;
446
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;
451                 break;
452
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;
457                 break;
458
459         default:
460                 fprintf(stderr, "Unsupported power mode.\n");
461                 return -EINVAL;
462         };
463
464         ret = send_header(dev, &h);
465         if (ret < 0)
466                 return ret;
467
468         return 0;
469 }