6fbee5c881fabc17a3ce89e00970e985ba656968
[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 <stdarg.h>
23 #include <errno.h>
24 #include <libusb.h>
25 #include <math.h>
26
27 #include "am7xxx.h"
28 #include "serialize.h"
29
30 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
31
32 /* If we're not using GNU C, elide __attribute__
33  * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
34  */
35 #ifndef __GNUC__
36 #  define  __attribute__(x)  /*NOTHING*/
37 #endif
38
39 /* Control shared library symbols visibility */
40 #if defined _WIN32 || defined __CYGWIN__
41         #define AM7XXX_PUBLIC __declspec(dllexport)
42         #define AM7XXX_LOCAL
43 #else
44         #if __GNUC__ >= 4
45                 #define AM7XXX_PUBLIC __attribute__ ((visibility ("default")))
46                 #define AM7XXX_LOCAL  __attribute__ ((visibility ("hidden")))
47         #else
48                 #define AM7XXX_PUBLIC
49                 #define AM7XXX_LOCAL
50         #endif
51 #endif
52
53 static void log_message(am7xxx_context *ctx,
54                         int level,
55                         const char *function,
56                         int line,
57                         const char *fmt,
58                         ...) __attribute__ ((format (printf, 5, 6)));
59
60 #define fatal(...)        log_message(NULL, AM7XXX_LOG_FATAL,   __func__, __LINE__, __VA_ARGS__)
61 #define error(ctx, ...)   log_message(ctx,  AM7XXX_LOG_ERROR,   __func__, __LINE__, __VA_ARGS__)
62 #define warning(ctx, ...) log_message(ctx,  AM7XXX_LOG_WARNING, __func__, 0,        __VA_ARGS__)
63 #define info(ctx, ...)    log_message(ctx,  AM7XXX_LOG_INFO,    __func__, 0,        __VA_ARGS__)
64 #define debug(ctx, ...)   log_message(ctx,  AM7XXX_LOG_DEBUG,   __func__, 0,        __VA_ARGS__)
65 #define trace(ctx, ...)   log_message(ctx,  AM7XXX_LOG_TRACE,   NULL,     0,        __VA_ARGS__)
66
67 struct am7xxx_ops {
68         int (*set_power_mode)(am7xxx_device *dev, am7xxx_power_mode power);
69         int (*set_zoom_mode)(am7xxx_device *dev, am7xxx_zoom_mode zoom);
70 };
71
72 struct am7xxx_usb_device_descriptor {
73         const char *name;
74         uint16_t vendor_id;
75         uint16_t product_id;
76         uint8_t configuration;    /* The bConfigurationValue of the device */
77         uint8_t interface_number; /* The bInterfaceNumber of the device */
78         struct am7xxx_ops ops;
79 };
80
81 static int default_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power);
82 static int picopix_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power);
83 static int default_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom);
84
85 #define DEFAULT_OPS { \
86         .set_power_mode = default_set_power_mode, \
87         .set_zoom_mode = default_set_zoom_mode, \
88 }
89
90 static const struct am7xxx_usb_device_descriptor supported_devices[] = {
91         {
92                 .name       = "Acer C110",
93                 .vendor_id  = 0x1de1,
94                 .product_id = 0xc101,
95                 .configuration    = 2,
96                 .interface_number = 0,
97                 .ops = DEFAULT_OPS,
98         },
99         {
100                 .name       = "Acer C112",
101                 .vendor_id  = 0x1de1,
102                 .product_id = 0x5501,
103                 .configuration    = 2,
104                 .interface_number = 0,
105                 .ops = DEFAULT_OPS,
106         },
107         {
108                 .name       ="Aiptek PocketCinema T25",
109                 .vendor_id  = 0x08ca,
110                 .product_id = 0x2144,
111                 .configuration    = 2,
112                 .interface_number = 0,
113                 .ops = DEFAULT_OPS,
114         },
115         {
116                 .name       = "Philips/Sagemcom PicoPix 1020",
117                 .vendor_id  = 0x21e7,
118                 .product_id = 0x000e,
119                 .configuration    = 2,
120                 .interface_number = 0,
121                 .ops = DEFAULT_OPS,
122         },
123         {
124                 .name       = "Philips/Sagemcom PicoPix 2055",
125                 .vendor_id  = 0x21e7,
126                 .product_id = 0x0016,
127                 .configuration    = 2,
128                 .interface_number = 0,
129                 .ops = {
130                         .set_power_mode = picopix_set_power_mode,
131                 },
132         },
133         {
134                 .name       = "Philips/Sagemcom PicoPix 2330",
135                 .vendor_id  = 0x21e7,
136                 .product_id = 0x0019,
137                 .configuration    = 1,
138                 .interface_number = 0,
139         },
140 };
141
142 /* The header size on the wire is known to be always 24 bytes, regardless of
143  * the memory configuration enforced by different architectures or compilers
144  * for struct am7xxx_header
145  */
146 #define AM7XXX_HEADER_WIRE_SIZE 24
147
148 struct _am7xxx_device {
149         libusb_device_handle *usb_device;
150         struct libusb_transfer *transfer;
151         int transfer_completed;
152         uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
153         am7xxx_device_info *device_info;
154         am7xxx_context *ctx;
155         const struct am7xxx_usb_device_descriptor *desc;
156         am7xxx_device *next;
157 };
158
159 struct _am7xxx_context {
160         libusb_context *usb_context;
161         int log_level;
162         am7xxx_device *devices_list;
163 };
164
165 typedef enum {
166         AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
167         AM7XXX_PACKET_TYPE_IMAGE   = 0x02,
168         AM7XXX_PACKET_TYPE_POWER   = 0x04,
169         AM7XXX_PACKET_TYPE_ZOOM    = 0x05,
170         AM7XXX_PACKET_TYPE_PICOPIX_POWER_LOW    = 0x15,
171         AM7XXX_PACKET_TYPE_PICOPIX_POWER_MEDIUM = 0x16,
172         AM7XXX_PACKET_TYPE_PICOPIX_POWER_HIGH   = 0x17,
173 } am7xxx_packet_type;
174
175 struct am7xxx_generic_header {
176         uint32_t field0;
177         uint32_t field1;
178         uint32_t field2;
179         uint32_t field3;
180 };
181
182 struct am7xxx_devinfo_header {
183         uint32_t native_width;
184         uint32_t native_height;
185         uint32_t unknown0;
186         uint32_t unknown1;
187 };
188
189 struct am7xxx_image_header {
190         uint32_t format;
191         uint32_t width;
192         uint32_t height;
193         uint32_t image_size;
194 };
195
196 struct am7xxx_power_header {
197         uint32_t bit2;
198         uint32_t bit1;
199         uint32_t bit0;
200 };
201
202 struct am7xxx_zoom_header {
203         uint32_t bit1;
204         uint32_t bit0;
205 };
206
207 /*
208  * Examples of packet headers:
209  *
210  * Image header:
211  * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
212  *
213  * Power header:
214  * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
215  */
216
217 /* Direction of the communication from the host point of view */
218 #define AM7XXX_DIRECTION_OUT 0 /* host -> device */
219 #define AM7XXX_DIRECTION_IN  1 /* host <- device */
220
221 struct am7xxx_header {
222         uint32_t packet_type;
223         uint8_t direction;
224         uint8_t header_data_len;
225         uint8_t unknown2;
226         uint8_t unknown3;
227         union {
228                 struct am7xxx_generic_header data;
229                 struct am7xxx_devinfo_header devinfo;
230                 struct am7xxx_image_header image;
231                 struct am7xxx_power_header power;
232                 struct am7xxx_zoom_header zoom;
233         } header_data;
234 };
235
236
237 #ifdef DEBUG
238 static void debug_dump_generic_header(am7xxx_context *ctx, struct am7xxx_generic_header *g)
239 {
240         if (ctx == NULL || g == NULL)
241                 return;
242
243         debug(ctx, "Generic header:\n");
244         debug(ctx, "\tfield0:  0x%08x (%u)\n", g->field0, g->field0);
245         debug(ctx, "\tfield1:  0x%08x (%u)\n", g->field1, g->field1);
246         debug(ctx, "\tfield2:  0x%08x (%u)\n", g->field2, g->field2);
247         debug(ctx, "\tfield3:  0x%08x (%u)\n", g->field3, g->field3);
248 }
249
250 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
251 {
252         if (ctx == NULL || d == NULL)
253                 return;
254
255         debug(ctx, "Info header:\n");
256         debug(ctx, "\tnative_width:  0x%08x (%u)\n", d->native_width, d->native_width);
257         debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
258         debug(ctx, "\tunknown0:      0x%08x (%u)\n", d->unknown0, d->unknown0);
259         debug(ctx, "\tunknown1:      0x%08x (%u)\n", d->unknown1, d->unknown1);
260 }
261
262 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
263 {
264         if (ctx == NULL || i == NULL)
265                 return;
266
267         debug(ctx, "Image header:\n");
268         debug(ctx, "\tformat:     0x%08x (%u)\n", i->format, i->format);
269         debug(ctx, "\twidth:      0x%08x (%u)\n", i->width, i->width);
270         debug(ctx, "\theight:     0x%08x (%u)\n", i->height, i->height);
271         debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
272 }
273
274 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
275 {
276         if (ctx == NULL || p == NULL)
277                 return;
278
279         debug(ctx, "Power header:\n");
280         debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
281         debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
282         debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
283 }
284
285 static void debug_dump_zoom_header(am7xxx_context *ctx, struct am7xxx_zoom_header *z)
286 {
287         if (ctx == NULL || z == NULL)
288                 return;
289
290         debug(ctx, "Zoom header:\n");
291         debug(ctx, "\tbit1: 0x%08x (%u)\n", z->bit1, z->bit1);
292         debug(ctx, "\tbit0: 0x%08x (%u)\n", z->bit0, z->bit0);
293 }
294
295 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
296 {
297         if (ctx == NULL || h == NULL)
298                 return;
299
300         debug(ctx, "BEGIN\n");
301         debug(ctx, "packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
302         debug(ctx, "direction:       0x%02hhx (%hhu) (%s)\n", h->direction, h->direction,
303               h->direction == AM7XXX_DIRECTION_IN ? "IN" :
304               h->direction == AM7XXX_DIRECTION_OUT ? "OUT" :
305               "UNKNOWN");
306         debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
307         debug(ctx, "unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
308         debug(ctx, "unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
309
310         switch(h->packet_type) {
311         case AM7XXX_PACKET_TYPE_DEVINFO:
312                 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
313                 break;
314
315         case AM7XXX_PACKET_TYPE_IMAGE:
316                 debug_dump_image_header(ctx, &(h->header_data.image));
317                 break;
318
319         case AM7XXX_PACKET_TYPE_POWER:
320                 debug_dump_power_header(ctx, &(h->header_data.power));
321                 break;
322
323         case AM7XXX_PACKET_TYPE_ZOOM:
324                 debug_dump_zoom_header(ctx, &(h->header_data.zoom));
325                 break;
326
327         default:
328                 debug(ctx, "Parsing data not supported for this packet type!\n");
329                 debug_dump_generic_header(ctx, &(h->header_data.data));
330                 break;
331         }
332         debug(ctx, "END\n\n");
333 }
334
335 static inline unsigned int in_80chars(unsigned int i)
336 {
337         /* The 3 below is the length of "xx " where xx is the hex string
338          * representation of a byte */
339         return ((i+1) % (80/3));
340 }
341
342 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
343                               uint8_t *buffer, unsigned int len)
344 {
345         unsigned int i;
346
347         if (ctx == NULL || buffer == NULL || len == 0)
348                 return;
349
350         trace(ctx, "\n");
351         if (message)
352                 trace(ctx, "%s\n", message);
353
354         for (i = 0; i < len; i++) {
355                 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
356         }
357         trace(ctx, "\n");
358 }
359 #else
360 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
361 {
362         (void)ctx;
363         (void)h;
364 }
365
366 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
367                               uint8_t *buffer, unsigned int len)
368 {
369         (void)ctx;
370         (void)message;
371         (void)buffer;
372         (void)len;
373 }
374 #endif /* DEBUG */
375
376 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
377 {
378         int ret;
379         int transferred = 0;
380
381         ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
382         if (ret != 0 || (unsigned int)transferred != len) {
383                 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
384                       ret, transferred, len);
385                 return ret;
386         }
387
388         trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
389
390         return 0;
391 }
392
393 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
394 {
395         int ret;
396         int transferred = 0;
397
398         trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
399
400         ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
401         if (ret != 0 || (unsigned int)transferred != len) {
402                 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
403                       ret, transferred, len);
404                 return ret;
405         }
406
407         return 0;
408 }
409
410 static void send_data_async_complete_cb(struct libusb_transfer *transfer)
411 {
412         am7xxx_device *dev = (am7xxx_device *)(transfer->user_data);
413         int *completed = &(dev->transfer_completed);
414         int transferred = transfer->actual_length;
415         int ret;
416
417         if (transferred != transfer->length) {
418                 error(dev->ctx, "transferred: %d (expected %u)\n",
419                       transferred, transfer->length);
420         }
421
422         switch (transfer->status) {
423         case LIBUSB_TRANSFER_COMPLETED:
424                 ret = 0;
425                 break;
426         case LIBUSB_TRANSFER_TIMED_OUT:
427                 ret = LIBUSB_ERROR_TIMEOUT;
428                 break;
429         case LIBUSB_TRANSFER_STALL:
430                 ret = LIBUSB_ERROR_PIPE;
431                 break;
432         case LIBUSB_TRANSFER_OVERFLOW:
433                 ret = LIBUSB_ERROR_OVERFLOW;
434                 break;
435         case LIBUSB_TRANSFER_NO_DEVICE:
436                 ret = LIBUSB_ERROR_NO_DEVICE;
437                 break;
438         case LIBUSB_TRANSFER_ERROR:
439         case LIBUSB_TRANSFER_CANCELLED:
440                 ret = LIBUSB_ERROR_IO;
441                 break;
442         default:
443                 error(dev->ctx, "unrecognised status code %d", transfer->status);
444                 ret = LIBUSB_ERROR_OTHER;
445         }
446
447         if (ret < 0)
448                 error(dev->ctx, "libusb transfer failed: %s",
449                       libusb_error_name(ret));
450
451         libusb_free_transfer(transfer);
452         transfer = NULL;
453
454         *completed = 1;
455 }
456
457 static inline void wait_for_trasfer_completed(am7xxx_device *dev)
458 {
459         while (!dev->transfer_completed) {
460                 int ret = libusb_handle_events_completed(dev->ctx->usb_context,
461                                                          &(dev->transfer_completed));
462                 if (ret < 0) {
463                         if (ret == LIBUSB_ERROR_INTERRUPTED)
464                                 continue;
465                         error(dev->ctx, "libusb_handle_events failed: %s, cancelling transfer and retrying",
466                               libusb_error_name(ret));
467                         libusb_cancel_transfer(dev->transfer);
468                         continue;
469                 }
470         }
471 }
472
473 static int send_data_async(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
474 {
475         int ret;
476         uint8_t *transfer_buffer;
477
478         dev->transfer = libusb_alloc_transfer(0);
479         if (dev->transfer == NULL) {
480                 error(dev->ctx, "cannot allocate transfer (%s)\n",
481                       strerror(errno));
482                 return -ENOMEM;
483         }
484
485         /* Make a copy of the buffer so the caller can safely reuse it just
486          * after libusb_submit_transfer() has returned. This technique
487          * requires more allocations than a proper double-buffering approach
488          * but it takes a lot less code. */
489         transfer_buffer = malloc(len);
490         if (transfer_buffer == NULL) {
491                 error(dev->ctx, "cannot allocate transfer buffer (%s)\n",
492                       strerror(errno));
493                 ret = -ENOMEM;
494                 goto err;
495         }
496         memcpy(transfer_buffer, buffer, len);
497
498         dev->transfer->flags |= LIBUSB_TRANSFER_FREE_BUFFER;
499         libusb_fill_bulk_transfer(dev->transfer, dev->usb_device, 0x1,
500                                   transfer_buffer, len,
501                                   send_data_async_complete_cb, dev, 0);
502
503         /* wait for the previous transfer to complete */
504         wait_for_trasfer_completed(dev);
505
506         trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
507
508         dev->transfer_completed = 0;
509         ret = libusb_submit_transfer(dev->transfer);
510         if (ret < 0)
511                 goto err;
512
513         return 0;
514
515 err:
516         libusb_free_transfer(dev->transfer);
517         dev->transfer = NULL;
518         return ret;
519 }
520
521 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
522 {
523         uint8_t **buffer_iterator = &buffer;
524
525         put_le32(h->packet_type, buffer_iterator);
526         put_8(h->direction, buffer_iterator);
527         put_8(h->header_data_len, buffer_iterator);
528         put_8(h->unknown2, buffer_iterator);
529         put_8(h->unknown3, buffer_iterator);
530         put_le32(h->header_data.data.field0, buffer_iterator);
531         put_le32(h->header_data.data.field1, buffer_iterator);
532         put_le32(h->header_data.data.field2, buffer_iterator);
533         put_le32(h->header_data.data.field3, buffer_iterator);
534 }
535
536 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
537 {
538         uint8_t **buffer_iterator = &buffer;
539
540         h->packet_type = get_le32(buffer_iterator);
541         h->direction = get_8(buffer_iterator);
542         h->header_data_len = get_8(buffer_iterator);
543         h->unknown2 = get_8(buffer_iterator);
544         h->unknown3 = get_8(buffer_iterator);
545         h->header_data.data.field0 = get_le32(buffer_iterator);
546         h->header_data.data.field1 = get_le32(buffer_iterator);
547         h->header_data.data.field2 = get_le32(buffer_iterator);
548         h->header_data.data.field3 = get_le32(buffer_iterator);
549 }
550
551 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
552 {
553         int ret;
554
555         ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
556         if (ret < 0)
557                 goto out;
558
559         unserialize_header(dev->buffer, h);
560
561         if (h->direction == AM7XXX_DIRECTION_IN) {
562                 ret = 0;
563         } else {
564                 error(dev->ctx,
565                       "Expected an AM7XXX_DIRECTION_IN packet, got one with direction = %d. Weird!\n",
566                       h->direction);
567                 ret = -EINVAL;
568         }
569
570         debug_dump_header(dev->ctx, h);
571
572 out:
573         return ret;
574 }
575
576 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
577 {
578         int ret;
579
580         debug_dump_header(dev->ctx, h);
581
582         /* For symmetry with read_header() we should check here for
583          * h->direction == AM7XXX_DIRECTION_OUT but we just ensure that in all
584          * the callers and save some cycles here.
585          */
586
587         serialize_header(h, dev->buffer);
588
589         ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
590         if (ret < 0)
591                 error(dev->ctx, "failed to send data\n");
592
593         return ret;
594 }
595
596 static int send_command(am7xxx_device *dev, am7xxx_packet_type type)
597 {
598         struct am7xxx_header h = {
599                 .packet_type     = type,
600                 .direction       = AM7XXX_DIRECTION_OUT,
601                 .header_data_len = 0x00,
602                 .unknown2        = 0x3e,
603                 .unknown3        = 0x10,
604                 .header_data = {
605                         .data = {
606                                 .field0 = 0,
607                                 .field1 = 0,
608                                 .field2 = 0,
609                                 .field3 = 0,
610                         },
611                 },
612         };
613
614         return send_header(dev, &h);
615 }
616
617
618 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
619  * and print the message unconditionally, this makes it possible to print
620  * fatal messages even early on initialization, before the context has been
621  * set up */
622 static void log_message(am7xxx_context *ctx,
623                         int level,
624                         const char *function,
625                         int line,
626                         const char *fmt,
627                         ...)
628 {
629         va_list ap;
630
631         if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
632                 if (function) {
633                         fprintf(stderr, "%s", function);
634                         if (line)
635                                 fprintf(stderr, "[%d]", line);
636                         fprintf(stderr, ": ");
637                 }
638
639                 va_start(ap, fmt);
640                 vfprintf(stderr, fmt, ap);
641                 va_end(ap);
642         }
643
644         return;
645 }
646
647 static am7xxx_device *add_new_device(am7xxx_context *ctx,
648                                      const struct am7xxx_usb_device_descriptor *desc)
649 {
650         am7xxx_device **devices_list;
651         am7xxx_device *new_device;
652
653         if (ctx == NULL) {
654                 fatal("context must not be NULL!\n");
655                 return NULL;
656         }
657
658         new_device = malloc(sizeof(*new_device));
659         if (new_device == NULL) {
660                 fatal("cannot allocate a new device (%s)\n", strerror(errno));
661                 return NULL;
662         }
663         memset(new_device, 0, sizeof(*new_device));
664
665         new_device->ctx = ctx;
666         new_device->desc = desc;
667         new_device->transfer_completed = 1;
668
669         devices_list = &(ctx->devices_list);
670
671         if (*devices_list == NULL) {
672                 *devices_list = new_device;
673         } else {
674                 am7xxx_device *prev = *devices_list;
675                 while (prev->next)
676                         prev = prev->next;
677                 prev->next = new_device;
678         }
679         return new_device;
680 }
681
682 static am7xxx_device *find_device(am7xxx_context *ctx,
683                                   unsigned int device_index)
684 {
685         unsigned int i = 0;
686         am7xxx_device *current;
687
688         if (ctx == NULL) {
689                 fatal("context must not be NULL!\n");
690                 return NULL;
691         }
692
693         current = ctx->devices_list;
694         while (current && i++ < device_index)
695                 current = current->next;
696
697         return current;
698 }
699
700 typedef enum {
701         SCAN_OP_BUILD_DEVLIST,
702         SCAN_OP_OPEN_DEVICE,
703 } scan_op;
704
705 /**
706  * This is where the central logic of multi-device support is.
707  *
708  * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
709  * 'dev' are ignored; the function returns 0 on success and a negative value
710  * on error.
711  *
712  * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
713  * device with index 'open_device_index' and returns the correspondent
714  * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
715  * 1 if the device was already open and a negative value on error.
716  *
717  * NOTES:
718  * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
719  * the caller might want to call am7xxx_shutdown() in order to remove
720  * devices possibly added before the failure.
721  */
722 static int scan_devices(am7xxx_context *ctx, scan_op op,
723                         unsigned int open_device_index, am7xxx_device **dev)
724 {
725         ssize_t num_devices;
726         libusb_device** list;
727         unsigned int current_index;
728         int i;
729         int ret;
730
731         if (ctx == NULL) {
732                 fatal("context must not be NULL!\n");
733                 return -EINVAL;
734         }
735         if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
736                 error(ctx, "device scan done already? Abort!\n");
737                 return -EINVAL;
738         }
739
740         num_devices = libusb_get_device_list(ctx->usb_context, &list);
741         if (num_devices < 0) {
742                 ret = -ENODEV;
743                 goto out;
744         }
745
746         current_index = 0;
747         for (i = 0; i < num_devices; i++) {
748                 struct libusb_device_descriptor desc;
749                 unsigned int j;
750
751                 ret = libusb_get_device_descriptor(list[i], &desc);
752                 if (ret < 0)
753                         continue;
754
755                 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
756                         if (desc.idVendor == supported_devices[j].vendor_id &&
757                             desc.idProduct == supported_devices[j].product_id) {
758
759                                 if (op == SCAN_OP_BUILD_DEVLIST) {
760                                         am7xxx_device *new_device;
761                                         info(ctx, "am7xxx device found, index: %d, name: %s\n",
762                                              current_index,
763                                              supported_devices[j].name);
764                                         new_device = add_new_device(ctx, &supported_devices[j]);
765                                         if (new_device == NULL) {
766                                                 /* XXX, the caller may want
767                                                  * to call am7xxx_shutdown() if
768                                                  * we fail here, as we may have
769                                                  * added some devices already
770                                                  */
771                                                 debug(ctx, "Cannot create a new device\n");
772                                                 ret = -ENODEV;
773                                                 goto out;
774                                         }
775                                 } else if (op == SCAN_OP_OPEN_DEVICE &&
776                                            current_index == open_device_index) {
777
778                                         *dev = find_device(ctx, open_device_index);
779                                         if (*dev == NULL) {
780                                                 ret = -ENODEV;
781                                                 goto out;
782                                         }
783
784                                         /* the usb device has already been opened */
785                                         if ((*dev)->usb_device) {
786                                                 debug(ctx, "(*dev)->usb_device already set\n");
787                                                 ret = 1;
788                                                 goto out;
789                                         }
790
791                                         ret = libusb_open(list[i], &((*dev)->usb_device));
792                                         if (ret < 0) {
793                                                 debug(ctx, "libusb_open failed\n");
794                                                 goto out;
795                                         }
796
797                                         /* XXX, the device is now open, if any
798                                          * of the calls below fail we need to
799                                          * close it again before bailing out.
800                                          */
801
802                                         ret = libusb_set_configuration((*dev)->usb_device,
803                                                                        (*dev)->desc->configuration);
804                                         if (ret < 0) {
805                                                 debug(ctx, "libusb_set_configuration failed\n");
806                                                 debug(ctx, "Cannot set configuration %hhu\n",
807                                                       (*dev)->desc->configuration);
808                                                 goto out_libusb_close;
809                                         }
810
811                                         ret = libusb_claim_interface((*dev)->usb_device,
812                                                                      (*dev)->desc->interface_number);
813                                         if (ret < 0) {
814                                                 debug(ctx, "libusb_claim_interface failed\n");
815                                                 debug(ctx, "Cannot claim interface %hhu\n",
816                                                       (*dev)->desc->interface_number);
817 out_libusb_close:
818                                                 libusb_close((*dev)->usb_device);
819                                                 (*dev)->usb_device = NULL;
820                                                 goto out;
821                                         }
822
823                                         goto out;
824                                 }
825                                 current_index++;
826                         }
827                 }
828         }
829
830         /* if we made it up to here we didn't find any device to open */
831         if (op == SCAN_OP_OPEN_DEVICE) {
832                 error(ctx, "Cannot find any device to open\n");
833                 ret = -ENODEV;
834                 goto out;
835         }
836
837         /* everything went fine when building the device list */
838         ret = 0;
839 out:
840         libusb_free_device_list(list, 1);
841         return ret;
842 }
843
844 /* Device specific operations */
845
846 static int default_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
847 {
848         int ret;
849         struct am7xxx_header h = {
850                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
851                 .direction       = AM7XXX_DIRECTION_OUT,
852                 .header_data_len = sizeof(struct am7xxx_power_header),
853                 .unknown2        = 0x3e,
854                 .unknown3        = 0x10,
855         };
856
857         switch(power) {
858         case AM7XXX_POWER_OFF:
859                 h.header_data.power.bit2 = 0;
860                 h.header_data.power.bit1 = 0;
861                 h.header_data.power.bit0 = 0;
862                 break;
863
864         case AM7XXX_POWER_LOW:
865                 h.header_data.power.bit2 = 0;
866                 h.header_data.power.bit1 = 0;
867                 h.header_data.power.bit0 = 1;
868                 break;
869
870         case AM7XXX_POWER_MIDDLE:
871                 h.header_data.power.bit2 = 0;
872                 h.header_data.power.bit1 = 1;
873                 h.header_data.power.bit0 = 0;
874                 break;
875
876         case AM7XXX_POWER_HIGH:
877                 h.header_data.power.bit2 = 0;
878                 h.header_data.power.bit1 = 1;
879                 h.header_data.power.bit0 = 1;
880                 break;
881
882         case AM7XXX_POWER_TURBO:
883                 h.header_data.power.bit2 = 1;
884                 h.header_data.power.bit1 = 0;
885                 h.header_data.power.bit0 = 0;
886                 break;
887
888         default:
889                 error(dev->ctx, "Unsupported power mode.\n");
890                 return -EINVAL;
891         };
892
893         ret = send_header(dev, &h);
894         if (ret < 0)
895                 return ret;
896
897         return 0;
898 }
899
900 static int picopix_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
901 {
902         switch(power) {
903         case AM7XXX_POWER_LOW:
904                 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_LOW);
905
906         case AM7XXX_POWER_MIDDLE:
907                 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_MEDIUM);
908
909         case AM7XXX_POWER_HIGH:
910                 return send_command(dev, AM7XXX_PACKET_TYPE_PICOPIX_POWER_HIGH);
911
912         case AM7XXX_POWER_OFF:
913         case AM7XXX_POWER_TURBO:
914         default:
915                 error(dev->ctx, "Unsupported power mode.\n");
916                 return -EINVAL;
917         };
918 }
919
920 static int default_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
921 {
922         int ret;
923         struct am7xxx_header h = {
924                 .packet_type     = AM7XXX_PACKET_TYPE_ZOOM,
925                 .direction       = AM7XXX_DIRECTION_OUT,
926                 .header_data_len = sizeof(struct am7xxx_zoom_header),
927                 .unknown2        = 0x3e,
928                 .unknown3        = 0x10,
929         };
930
931         switch(zoom) {
932         case AM7XXX_ZOOM_ORIGINAL:
933                 h.header_data.zoom.bit1 = 0;
934                 h.header_data.zoom.bit0 = 0;
935                 break;
936
937         case AM7XXX_ZOOM_H:
938                 h.header_data.zoom.bit1 = 0;
939                 h.header_data.zoom.bit0 = 1;
940                 break;
941
942         case AM7XXX_ZOOM_H_V:
943                 h.header_data.zoom.bit1 = 1;
944                 h.header_data.zoom.bit0 = 0;
945                 break;
946
947         case AM7XXX_ZOOM_TEST:
948                 h.header_data.zoom.bit1 = 1;
949                 h.header_data.zoom.bit0 = 1;
950                 break;
951
952         case AM7XXX_ZOOM_TELE:
953         default:
954                 error(dev->ctx, "Unsupported zoom mode.\n");
955                 return -EINVAL;
956         };
957
958         ret = send_header(dev, &h);
959         if (ret < 0)
960                 return ret;
961
962         return 0;
963 }
964
965 /* Public API */
966
967 AM7XXX_PUBLIC int am7xxx_init(am7xxx_context **ctx)
968 {
969         int ret;
970
971         *ctx = malloc(sizeof(**ctx));
972         if (*ctx == NULL) {
973                 fatal("cannot allocate the context (%s)\n", strerror(errno));
974                 ret = -ENOMEM;
975                 goto out;
976         }
977         memset(*ctx, 0, sizeof(**ctx));
978
979         /* Set the highest log level during initialization */
980         (*ctx)->log_level = AM7XXX_LOG_TRACE;
981
982         ret = libusb_init(&((*ctx)->usb_context));
983         if (ret < 0)
984                 goto out_free_context;
985
986         libusb_set_debug((*ctx)->usb_context, LIBUSB_LOG_LEVEL_INFO);
987
988         ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
989         if (ret < 0) {
990                 error(*ctx, "scan_devices() failed\n");
991                 am7xxx_shutdown(*ctx);
992                 goto out;
993         }
994
995         /* Set a quieter log level as default for normal operation */
996         (*ctx)->log_level = AM7XXX_LOG_ERROR;
997         return 0;
998
999 out_free_context:
1000         free(*ctx);
1001         *ctx = NULL;
1002 out:
1003         return ret;
1004 }
1005
1006 AM7XXX_PUBLIC void am7xxx_shutdown(am7xxx_context *ctx)
1007 {
1008         am7xxx_device *current;
1009
1010         if (ctx == NULL) {
1011                 fatal("context must not be NULL!\n");
1012                 return;
1013         }
1014
1015         current = ctx->devices_list;
1016         while (current) {
1017                 am7xxx_device *next = current->next;
1018                 am7xxx_close_device(current);
1019                 free(current->device_info);
1020                 free(current);
1021                 current = next;
1022         }
1023
1024         libusb_exit(ctx->usb_context);
1025         free(ctx);
1026         ctx = NULL;
1027 }
1028
1029 AM7XXX_PUBLIC void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
1030 {
1031         ctx->log_level = log_level;
1032 }
1033
1034 AM7XXX_PUBLIC int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
1035                        unsigned int device_index)
1036 {
1037         int ret;
1038
1039         if (ctx == NULL) {
1040                 fatal("context must not be NULL!\n");
1041                 return -EINVAL;
1042         }
1043
1044         ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
1045         if (ret < 0) {
1046                 errno = ENODEV;
1047                 goto out;
1048         } else if (ret > 0) {
1049                 warning(ctx, "device %d already open\n", device_index);
1050                 errno = EBUSY;
1051                 ret = -EBUSY;
1052                 goto out;
1053         }
1054
1055         /* Philips/Sagemcom PicoPix projectors require that the DEVINFO packet
1056          * is the first one to be sent to the device in order for it to
1057          * successfully return the correct device information.
1058          *
1059          * So, if there is not a cached version of it (from a previous open),
1060          * we ask for device info at open time,
1061          */
1062         if ((*dev)->device_info == NULL) {
1063                 ret = am7xxx_get_device_info(*dev, NULL);
1064                 if (ret < 0)
1065                         error(ctx, "cannot get device info\n");
1066         }
1067
1068 out:
1069         return ret;
1070 }
1071
1072 AM7XXX_PUBLIC int am7xxx_close_device(am7xxx_device *dev)
1073 {
1074         if (dev == NULL) {
1075                 fatal("dev must not be NULL!\n");
1076                 return -EINVAL;
1077         }
1078         if (dev->usb_device) {
1079                 wait_for_trasfer_completed(dev);
1080                 libusb_release_interface(dev->usb_device, dev->desc->interface_number);
1081                 libusb_close(dev->usb_device);
1082                 dev->usb_device = NULL;
1083         }
1084         return 0;
1085 }
1086
1087 AM7XXX_PUBLIC int am7xxx_get_device_info(am7xxx_device *dev,
1088                            am7xxx_device_info *device_info)
1089 {
1090         int ret;
1091         struct am7xxx_header h = { 0 };
1092
1093         if (dev->device_info) {
1094                 memcpy(device_info, dev->device_info, sizeof(*device_info));
1095                 return 0;
1096         }
1097
1098         ret = send_command(dev, AM7XXX_PACKET_TYPE_DEVINFO);
1099         if (ret < 0)
1100                 return ret;
1101
1102         ret = read_header(dev, &h);
1103         if (ret < 0)
1104                 return ret;
1105
1106         if (h.packet_type != AM7XXX_PACKET_TYPE_DEVINFO) {
1107                 error(dev->ctx, "expected packet type: %d, got %d instead!\n",
1108                       AM7XXX_PACKET_TYPE_DEVINFO, h.packet_type);
1109                 errno = ENOTSUP;
1110                 return -ENOTSUP;
1111         }
1112
1113         dev->device_info = malloc(sizeof(*dev->device_info));
1114         if (dev->device_info == NULL) {
1115                 error(dev->ctx, "cannot allocate a device info (%s)\n",
1116                        strerror(errno));
1117                 return -ENOMEM;
1118         }
1119         memset(dev->device_info, 0, sizeof(*dev->device_info));
1120
1121         dev->device_info->native_width = h.header_data.devinfo.native_width;
1122         dev->device_info->native_height = h.header_data.devinfo.native_height;
1123 #if 0
1124         /* No reason to expose these in the public API until we know what they mean */
1125         dev->device_info->unknown0 = h.header_data.devinfo.unknown0;
1126         dev->device_info->unknown1 = h.header_data.devinfo.unknown1;
1127 #endif
1128
1129         return 0;
1130 }
1131
1132 AM7XXX_PUBLIC int am7xxx_calc_scaled_image_dimensions(am7xxx_device *dev,
1133                                         unsigned int upscale,
1134                                         unsigned int original_width,
1135                                         unsigned int original_height,
1136                                         unsigned int *scaled_width,
1137                                         unsigned int *scaled_height)
1138 {
1139
1140         am7xxx_device_info device_info;
1141         float width_ratio;
1142         float height_ratio;
1143         int ret;
1144
1145         ret = am7xxx_get_device_info(dev, &device_info);
1146         if (ret < 0) {
1147                 error(dev->ctx, "cannot get device info\n");
1148                 return ret;
1149         }
1150
1151         /*
1152          * Check if we need to rescale; if the input image fits the native
1153          * dimensions there is no need to, unless we want to upscale.
1154          */
1155         if (!upscale &&
1156             original_width <= device_info.native_width &&
1157             original_height <= device_info.native_height ) {
1158                 debug(dev->ctx, "CASE 0, no rescaling, the original image fits already\n");
1159                 *scaled_width = original_width;
1160                 *scaled_height = original_height;
1161                 return 0;
1162         }
1163
1164         /* Input dimensions relative to the device native dimensions */
1165         width_ratio =  (float)original_width / device_info.native_width;
1166         height_ratio = (float)original_height / device_info.native_height;
1167
1168         if (width_ratio > height_ratio) {
1169                 /*
1170                  * The input is proportionally "wider" than the device viewport
1171                  * so its height needs to be adjusted
1172                  */
1173                 debug(dev->ctx, "CASE 1, original image wider, adjust the scaled height\n");
1174                 *scaled_width = device_info.native_width;
1175                 *scaled_height = (unsigned int)lroundf(original_height / width_ratio);
1176         } else if (width_ratio < height_ratio) {
1177                 /*
1178                  * The input is proportionally "taller" than the device viewport
1179                  * so its width needs to be adjusted
1180                  */
1181                 debug(dev->ctx, "CASE 2 original image taller, adjust the scaled width\n");
1182                 *scaled_width = (unsigned int)lroundf(original_width / height_ratio);
1183                 *scaled_height = device_info.native_height;
1184         } else {
1185                 debug(dev->ctx, "CASE 3, just rescale, same aspect ratio already\n");
1186                 *scaled_width = device_info.native_width;
1187                 *scaled_height = device_info.native_height;
1188         }
1189         debug(dev->ctx, "scaled dimensions: %dx%d\n", *scaled_width, *scaled_height);
1190
1191         return 0;
1192 }
1193
1194 AM7XXX_PUBLIC int am7xxx_send_image(am7xxx_device *dev,
1195                       am7xxx_image_format format,
1196                       unsigned int width,
1197                       unsigned int height,
1198                       uint8_t *image,
1199                       unsigned int image_size)
1200 {
1201         int ret;
1202         struct am7xxx_header h = {
1203                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
1204                 .direction       = AM7XXX_DIRECTION_OUT,
1205                 .header_data_len = sizeof(struct am7xxx_image_header),
1206                 .unknown2        = 0x3e,
1207                 .unknown3        = 0x10,
1208                 .header_data = {
1209                         .image = {
1210                                 .format     = format,
1211                                 .width      = width,
1212                                 .height     = height,
1213                                 .image_size = image_size,
1214                         },
1215                 },
1216         };
1217
1218         ret = send_header(dev, &h);
1219         if (ret < 0)
1220                 return ret;
1221
1222         if (image == NULL || image_size == 0) {
1223                 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
1224                 return 0;
1225         }
1226
1227         return send_data(dev, image, image_size);
1228 }
1229
1230 AM7XXX_PUBLIC int am7xxx_send_image_async(am7xxx_device *dev,
1231                                           am7xxx_image_format format,
1232                                           unsigned int width,
1233                                           unsigned int height,
1234                                           uint8_t *image,
1235                                           unsigned int image_size)
1236 {
1237         int ret;
1238         struct am7xxx_header h = {
1239                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
1240                 .direction       = AM7XXX_DIRECTION_OUT,
1241                 .header_data_len = sizeof(struct am7xxx_image_header),
1242                 .unknown2        = 0x3e,
1243                 .unknown3        = 0x10,
1244                 .header_data = {
1245                         .image = {
1246                                 .format     = format,
1247                                 .width      = width,
1248                                 .height     = height,
1249                                 .image_size = image_size,
1250                         },
1251                 },
1252         };
1253
1254         ret = send_header(dev, &h);
1255         if (ret < 0)
1256                 return ret;
1257
1258         if (image == NULL || image_size == 0) {
1259                 warning(dev->ctx, "Not sending any data, check the 'image' or 'image_size' parameters\n");
1260                 return 0;
1261         }
1262
1263         return send_data_async(dev, image, image_size);
1264 }
1265
1266 AM7XXX_PUBLIC int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode power)
1267 {
1268         if (dev->desc->ops.set_power_mode == NULL) {
1269                 warning(dev->ctx,
1270                         "setting power mode is unsupported on this device\n");
1271                 return 0;
1272         }
1273
1274         return dev->desc->ops.set_power_mode(dev, power);
1275 }
1276
1277 AM7XXX_PUBLIC int am7xxx_set_zoom_mode(am7xxx_device *dev, am7xxx_zoom_mode zoom)
1278 {
1279         if (dev->desc->ops.set_zoom_mode == NULL) {
1280                 warning(dev->ctx,
1281                         "setting zoom mode is unsupported on this device\n");
1282                 return 0;
1283         }
1284
1285         return dev->desc->ops.set_zoom_mode(dev, zoom);
1286 }