b58171806709925f7533f0c61e3eaf211a442af5
[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
26 #include "am7xxx.h"
27 #include "serialize.h"
28
29 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
30
31 /* If we're not using GNU C, elide __attribute__
32  * taken from: http://unixwiz.net/techtips/gnu-c-attributes.html)
33  */
34 #ifndef __GNUC__
35 #  define  __attribute__(x)  /*NOTHING*/
36 #endif
37
38 static void log_message(am7xxx_context *ctx,
39                         int level,
40                         const char *function,
41                         int line,
42                         const char *fmt,
43                         ...) __attribute__ ((format (printf, 5, 6)));
44
45 #define fatal(...)        log_message(NULL, AM7XXX_LOG_FATAL,   __func__, __LINE__, __VA_ARGS__)
46 #define error(ctx, ...)   log_message(ctx,  AM7XXX_LOG_ERROR,   __func__, __LINE__, __VA_ARGS__)
47 #define warning(ctx, ...) log_message(ctx,  AM7XXX_LOG_WARNING, __func__, 0,        __VA_ARGS__)
48 #define info(ctx, ...)    log_message(ctx,  AM7XXX_LOG_INFO,    __func__, 0,        __VA_ARGS__)
49 #define debug(ctx, ...)   log_message(ctx,  AM7XXX_LOG_DEBUG,   __func__, 0,        __VA_ARGS__)
50 #define trace(ctx, ...)   log_message(ctx,  AM7XXX_LOG_TRACE,   NULL,     0,        __VA_ARGS__)
51
52 struct am7xxx_usb_device_descriptor {
53         const char *name;
54         uint16_t vendor_id;
55         uint16_t product_id;
56 };
57
58 static struct am7xxx_usb_device_descriptor supported_devices[] = {
59         {
60                 .name       = "Acer C110",
61                 .vendor_id  = 0x1de1,
62                 .product_id = 0xc101,
63         },
64         {
65                 .name       = "Philips/Sagemcom PicoPix 1020",
66                 .vendor_id  = 0x21e7,
67                 .product_id = 0x000e,
68         },
69 };
70
71 /* The header size on the wire is known to be always 24 bytes, regardless of
72  * the memory configuration enforced by different architechtures or compilers
73  * for struct am7xxx_header
74  */
75 #define AM7XXX_HEADER_WIRE_SIZE 24
76
77 struct _am7xxx_device {
78         libusb_device_handle *usb_device;
79         uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE];
80         am7xxx_context *ctx;
81         am7xxx_device *next;
82 };
83
84 struct _am7xxx_context {
85         libusb_context *usb_context;
86         int log_level;
87         am7xxx_device *devices_list;
88 };
89
90 typedef enum {
91         AM7XXX_PACKET_TYPE_DEVINFO = 0x01,
92         AM7XXX_PACKET_TYPE_IMAGE   = 0x02,
93         AM7XXX_PACKET_TYPE_POWER   = 0x04,
94         AM7XXX_PACKET_TYPE_UNKNOWN = 0x05,
95 } am7xxx_packet_type;
96
97 struct am7xxx_generic_header {
98         uint32_t field0;
99         uint32_t field1;
100         uint32_t field2;
101         uint32_t field3;
102 };
103
104 struct am7xxx_devinfo_header {
105         uint32_t native_width;
106         uint32_t native_height;
107         uint32_t unknown0;
108         uint32_t unknown1;
109 };
110
111 struct am7xxx_image_header {
112         uint32_t format;
113         uint32_t width;
114         uint32_t height;
115         uint32_t image_size;
116 };
117
118 struct am7xxx_power_header {
119         uint32_t bit2;
120         uint32_t bit1;
121         uint32_t bit0;
122 };
123
124 /*
125  * Examples of packet headers:
126  *
127  * Image header:
128  * 02 00 00 00 00 10 3e 10 01 00 00 00 20 03 00 00 e0 01 00 00 53 E8 00 00
129  *
130  * Power header:
131  * 04 00 00 00 00 0c ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
132  */
133
134 struct am7xxx_header {
135         uint32_t packet_type;
136         uint8_t unknown0;
137         uint8_t header_data_len;
138         uint8_t unknown2;
139         uint8_t unknown3;
140         union {
141                 struct am7xxx_generic_header data;
142                 struct am7xxx_devinfo_header devinfo;
143                 struct am7xxx_image_header image;
144                 struct am7xxx_power_header power;
145         } header_data;
146 };
147
148
149 #ifdef DEBUG
150 static void debug_dump_devinfo_header(am7xxx_context *ctx, struct am7xxx_devinfo_header *d)
151 {
152         if (ctx == NULL || d == NULL)
153                 return;
154
155         debug(ctx, "Info header:\n");
156         debug(ctx, "\tnative_width:  0x%08x (%u)\n", d->native_width, d->native_width);
157         debug(ctx, "\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
158         debug(ctx, "\tunknown0:      0x%08x (%u)\n", d->unknown0, d->unknown0);
159         debug(ctx, "\tunknown1:      0x%08x (%u)\n", d->unknown1, d->unknown1);
160 }
161
162 static void debug_dump_image_header(am7xxx_context *ctx, struct am7xxx_image_header *i)
163 {
164         if (ctx == NULL || i == NULL)
165                 return;
166
167         debug(ctx, "Image header:\n");
168         debug(ctx, "\tformat:     0x%08x (%u)\n", i->format, i->format);
169         debug(ctx, "\twidth:      0x%08x (%u)\n", i->width, i->width);
170         debug(ctx, "\theight:     0x%08x (%u)\n", i->height, i->height);
171         debug(ctx, "\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
172 }
173
174 static void debug_dump_power_header(am7xxx_context *ctx, struct am7xxx_power_header *p)
175 {
176         if (ctx == NULL || p == NULL)
177                 return;
178
179         debug(ctx, "Power header:\n");
180         debug(ctx, "\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
181         debug(ctx, "\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
182         debug(ctx, "\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
183 }
184
185 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
186 {
187         if (ctx == NULL || h == NULL)
188                 return;
189
190         debug(ctx, "BEGIN\n");
191         debug(ctx, "packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
192         debug(ctx, "unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
193         debug(ctx, "header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
194         debug(ctx, "unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
195         debug(ctx, "unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
196
197         switch(h->packet_type) {
198         case AM7XXX_PACKET_TYPE_DEVINFO:
199                 debug_dump_devinfo_header(ctx, &(h->header_data.devinfo));
200                 break;
201
202         case AM7XXX_PACKET_TYPE_IMAGE:
203                 debug_dump_image_header(ctx, &(h->header_data.image));
204                 break;
205
206         case AM7XXX_PACKET_TYPE_POWER:
207                 debug_dump_power_header(ctx, &(h->header_data.power));
208                 break;
209
210         default:
211                 debug(ctx, "Packet type not supported!\n");
212                 break;
213         }
214         debug(ctx, "END\n\n");
215 }
216
217 static inline unsigned int in_80chars(unsigned int i)
218 {
219         /* The 3 below is the length of "xx " where xx is the hex string
220          * representation of a byte */
221         return ((i+1) % (80/3));
222 }
223
224 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
225                               uint8_t *buffer, unsigned int len)
226 {
227         unsigned int i;
228
229         if (ctx == NULL || buffer == NULL || len == 0)
230                 return;
231
232         trace(ctx, "\n");
233         if (message)
234                 trace(ctx, "%s\n", message);
235
236         for (i = 0; i < len; i++) {
237                 trace(ctx, "%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
238         }
239         trace(ctx, "\n");
240 }
241 #else
242 static void debug_dump_header(am7xxx_context *ctx, struct am7xxx_header *h)
243 {
244         (void)ctx;
245         (void)h;
246 }
247 static void trace_dump_buffer(am7xxx_context *ctx, const char *message,
248                               uint8_t *buffer, unsigned int len)
249 {
250         (void)ctx;
251         (void)message;
252         (void)buffer;
253         (void)len;
254 }
255 #endif /* DEBUG */
256
257 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
258 {
259         int ret;
260         int transferred = 0;
261
262         ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
263         if (ret != 0 || (unsigned int)transferred != len) {
264                 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
265                       ret, transferred, len);
266                 return ret;
267         }
268
269         trace_dump_buffer(dev->ctx, "<-- received", buffer, len);
270
271         return 0;
272 }
273
274 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
275 {
276         int ret;
277         int transferred = 0;
278
279         trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
280
281         ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
282         if (ret != 0 || (unsigned int)transferred != len) {
283                 error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
284                       ret, transferred, len);
285                 return ret;
286         }
287
288         return 0;
289 }
290
291 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
292 {
293         uint8_t **buffer_iterator = &buffer;
294
295         put_le32(h->packet_type, buffer_iterator);
296         put_8(h->unknown0, buffer_iterator);
297         put_8(h->header_data_len, buffer_iterator);
298         put_8(h->unknown2, buffer_iterator);
299         put_8(h->unknown3, buffer_iterator);
300         put_le32(h->header_data.data.field0, buffer_iterator);
301         put_le32(h->header_data.data.field1, buffer_iterator);
302         put_le32(h->header_data.data.field2, buffer_iterator);
303         put_le32(h->header_data.data.field3, buffer_iterator);
304 }
305
306 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
307 {
308         uint8_t **buffer_iterator = &buffer;
309
310         h->packet_type = get_le32(buffer_iterator);
311         h->unknown0 = get_8(buffer_iterator);
312         h->header_data_len = get_8(buffer_iterator);
313         h->unknown2 = get_8(buffer_iterator);
314         h->unknown3 = get_8(buffer_iterator);
315         h->header_data.data.field0 = get_le32(buffer_iterator);
316         h->header_data.data.field1 = get_le32(buffer_iterator);
317         h->header_data.data.field2 = get_le32(buffer_iterator);
318         h->header_data.data.field3 = get_le32(buffer_iterator);
319 }
320
321 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
322 {
323         int ret;
324
325         ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
326         if (ret < 0)
327                 goto out;
328
329         unserialize_header(dev->buffer, h);
330
331         debug_dump_header(dev->ctx, h);
332
333         ret = 0;
334
335 out:
336         return ret;
337 }
338
339 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
340 {
341         int ret;
342
343         debug_dump_header(dev->ctx, h);
344
345         serialize_header(h, dev->buffer);
346         ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
347         if (ret < 0)
348                 error(dev->ctx, "failed to send data\n");
349
350         return ret;
351 }
352
353 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
354  * and print the message unconditionally, this makes it possible to print
355  * fatal messages even early on initialization, before the context has been
356  * set up */
357 static void log_message(am7xxx_context *ctx,
358                         int level,
359                         const char *function,
360                         int line,
361                         const char *fmt,
362                         ...)
363 {
364         va_list ap;
365
366         if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
367                 if (function) {
368                         fprintf(stderr, "%s", function);
369                         if (line)
370                                 fprintf(stderr, "[%d]", line);
371                         fprintf(stderr, ": ");
372                 }
373
374                 va_start(ap, fmt);
375                 vfprintf(stderr, fmt, ap);
376                 va_end(ap);
377         }
378
379         return;
380 }
381
382 static am7xxx_device *add_new_device(am7xxx_context *ctx)
383 {
384         am7xxx_device **devices_list;
385         am7xxx_device *new_device;
386
387         if (ctx == NULL) {
388                 fatal("context must not be NULL!\n");
389                 return NULL;
390         }
391
392         devices_list = &(ctx->devices_list);
393
394         new_device = malloc(sizeof(*new_device));
395         if (new_device == NULL) {
396                 fatal("cannot allocate a new device (%s)\n", strerror(errno));
397                 return NULL;
398         }
399         memset(new_device, 0, sizeof(*new_device));
400
401         new_device->ctx = ctx;
402
403         if (*devices_list == NULL) {
404                 *devices_list = new_device;
405         } else {
406                 am7xxx_device *prev = *devices_list;
407                 while (prev->next)
408                         prev = prev->next;
409                 prev->next = new_device;
410         }
411         return new_device;
412 }
413
414 static am7xxx_device *find_device(am7xxx_context *ctx,
415                                   unsigned int device_index)
416 {
417         unsigned int i = 0;
418         am7xxx_device *current;
419
420         if (ctx == NULL) {
421                 fatal("context must not be NULL!\n");
422                 return NULL;
423         }
424
425         current = ctx->devices_list;
426         while (current && i++ < device_index)
427                 current = current->next;
428
429         return current;
430 }
431
432 typedef enum {
433         SCAN_OP_BUILD_DEVLIST,
434         SCAN_OP_OPEN_DEVICE,
435 } scan_op;
436
437 /**
438  * This is where the central logic of multi-device support is.
439  *
440  * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
441  * 'dev' are ignored; the function returns 0 on success and a negative value
442  * on error.
443  *
444  * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
445  * device with index 'open_device_index' and returns the correspondent
446  * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
447  * 1 if the device was already open and a negative value on error.
448  *
449  * NOTES:
450  * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
451  * the caller might want to call am7xxx_shutdown() in order to remove
452  * devices possibly added before the failure.
453  */
454 static int scan_devices(am7xxx_context *ctx, scan_op op,
455                         unsigned int open_device_index, am7xxx_device **dev)
456 {
457         int num_devices;
458         libusb_device** list;
459         unsigned int current_index;
460         int i;
461         int ret;
462
463         if (ctx == NULL) {
464                 fatal("context must not be NULL!\n");
465                 return -EINVAL;
466         }
467         if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
468                 error(ctx, "device scan done already? Abort!\n");
469                 return -EINVAL;
470         }
471
472         num_devices = libusb_get_device_list(ctx->usb_context, &list);
473         if (num_devices < 0) {
474                 ret = -ENODEV;
475                 goto out;
476         }
477
478         current_index = 0;
479         for (i = 0; i < num_devices; i++) {
480                 struct libusb_device_descriptor desc;
481                 unsigned int j;
482
483                 ret = libusb_get_device_descriptor(list[i], &desc);
484                 if (ret < 0)
485                         continue;
486
487                 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
488                         if (desc.idVendor == supported_devices[j].vendor_id
489                             && desc.idProduct == supported_devices[j].product_id) {
490
491                                 if (op == SCAN_OP_BUILD_DEVLIST) {
492                                         am7xxx_device *new_device;
493                                         info(ctx, "am7xxx device found, index: %d, name: %s\n",
494                                              current_index,
495                                              supported_devices[j].name);
496                                         new_device = add_new_device(ctx);
497                                         if (new_device == NULL) {
498                                                 /* XXX, the caller may want
499                                                  * to call am7xxx_shutdown() if
500                                                  * we fail here, as we may have
501                                                  * added some devices already
502                                                  */
503                                                 debug(ctx, "Cannot create a new device\n");
504                                                 ret = -ENODEV;
505                                                 goto out;
506                                         }
507                                 } else if (op == SCAN_OP_OPEN_DEVICE &&
508                                            current_index == open_device_index) {
509
510                                         *dev = find_device(ctx, open_device_index);
511                                         if (*dev == NULL) {
512                                                 ret = -ENODEV;
513                                                 goto out;
514                                         }
515
516                                         /* the usb device has already been opened */
517                                         if ((*dev)->usb_device) {
518                                                 debug(ctx, "(*dev)->usb_device already set\n");
519                                                 ret = 1;
520                                                 goto out;
521                                         }
522
523                                         ret = libusb_open(list[i], &((*dev)->usb_device));
524                                         if (ret < 0) {
525                                                 debug(ctx, "libusb_open failed\n");
526                                                 goto out;
527                                         }
528
529                                         libusb_set_configuration((*dev)->usb_device, 1);
530                                         libusb_claim_interface((*dev)->usb_device, 0);
531                                         goto out;
532                                 }
533                                 current_index++;
534                         }
535                 }
536         }
537
538         /* if we made it up to here we didn't find any device to open */
539         if (op == SCAN_OP_OPEN_DEVICE) {
540                 error(ctx, "Cannot find any device to open\n");
541                 ret = -ENODEV;
542                 goto out;
543         }
544
545         /* everything went fine when building the device list */
546         ret = 0;
547 out:
548         libusb_free_device_list(list, 1);
549         return ret;
550 }
551
552 int am7xxx_init(am7xxx_context **ctx)
553 {
554         int ret = 0;
555
556         *ctx = malloc(sizeof(**ctx));
557         if (*ctx == NULL) {
558                 fatal("cannot allocate the context (%s)\n", strerror(errno));
559                 ret = -ENOMEM;
560                 goto out;
561         }
562         memset(*ctx, 0, sizeof(**ctx));
563
564         /* Set the highest log level during initialization */
565         (*ctx)->log_level = AM7XXX_LOG_TRACE;
566
567         ret = libusb_init(&((*ctx)->usb_context));
568         if (ret < 0)
569                 goto out_free_context;
570
571         libusb_set_debug((*ctx)->usb_context, 3);
572
573         ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
574         if (ret < 0) {
575                 error(*ctx, "scan_devices() failed\n");
576                 am7xxx_shutdown(*ctx);
577                 goto out;
578         }
579
580         /* Set a quieter log level as default for normal operation */
581         (*ctx)->log_level = AM7XXX_LOG_ERROR;
582         return 0;
583
584 out_free_context:
585         free(*ctx);
586         *ctx = NULL;
587 out:
588         return ret;
589 }
590
591 void am7xxx_shutdown(am7xxx_context *ctx)
592 {
593         am7xxx_device *current;
594
595         if (ctx == NULL) {
596                 fatal("context must not be NULL!\n");
597                 return;
598         }
599
600         current = ctx->devices_list;
601         while (current) {
602                 am7xxx_device *next = current->next;
603                 am7xxx_close_device(current);
604                 free(current);
605                 current = next;
606         }
607
608         libusb_exit(ctx->usb_context);
609         free(ctx);
610         ctx = NULL;
611 }
612
613 void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
614 {
615         ctx->log_level = log_level;
616 }
617
618 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
619                        unsigned int device_index)
620 {
621         int ret;
622
623         if (ctx == NULL) {
624                 fatal("context must not be NULL!\n");
625                 return -EINVAL;
626         }
627
628         ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
629         if (ret < 0) {
630                 errno = ENODEV;
631         } else if (ret > 0) {
632                 warning(ctx, "device %d already open\n", device_index);
633                 errno = EBUSY;
634                 ret = -EBUSY;
635         }
636
637         return ret;
638 }
639
640 int am7xxx_close_device(am7xxx_device *dev)
641 {
642         if (dev == NULL) {
643                 fatal("dev must not be NULL!\n");
644                 return -EINVAL;
645         }
646         if (dev->usb_device) {
647                 libusb_release_interface(dev->usb_device, 0);
648                 libusb_close(dev->usb_device);
649                 dev->usb_device = NULL;
650         }
651         return 0;
652 }
653
654 int am7xxx_get_device_info(am7xxx_device *dev,
655                            am7xxx_device_info *device_info)
656 {
657         int ret;
658         struct am7xxx_header h = {
659                 .packet_type     = AM7XXX_PACKET_TYPE_DEVINFO,
660                 .unknown0        = 0x00,
661                 .header_data_len = 0x00,
662                 .unknown2        = 0x3e,
663                 .unknown3        = 0x10,
664                 .header_data = {
665                         .devinfo = {
666                                 .native_width  = 0,
667                                 .native_height = 0,
668                                 .unknown0      = 0,
669                                 .unknown1      = 0,
670                         },
671                 },
672         };
673
674         ret = send_header(dev, &h);
675         if (ret < 0)
676                 return ret;
677
678         ret = read_header(dev, &h);
679         if (ret < 0)
680                 return ret;
681
682         device_info->native_width = h.header_data.devinfo.native_width;
683         device_info->native_height = h.header_data.devinfo.native_height;
684 #if 0
685         /* No reason to expose these in the public API until we know what they mean */
686         device_info->unknown0 = h.header_data.devinfo.unknown0;
687         device_info->unknown1 = h.header_data.devinfo.unknown1;
688 #endif
689
690         return 0;
691 }
692
693 int am7xxx_send_image(am7xxx_device *dev,
694                       am7xxx_image_format format,
695                       unsigned int width,
696                       unsigned int height,
697                       uint8_t *image,
698                       unsigned int size)
699 {
700         int ret;
701         struct am7xxx_header h = {
702                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
703                 .unknown0        = 0x00,
704                 .header_data_len = sizeof(struct am7xxx_image_header),
705                 .unknown2        = 0x3e,
706                 .unknown3        = 0x10,
707                 .header_data = {
708                         .image = {
709                                 .format     = format,
710                                 .width      = width,
711                                 .height     = height,
712                                 .image_size = size,
713                         },
714                 },
715         };
716
717         ret = send_header(dev, &h);
718         if (ret < 0)
719                 return ret;
720
721         if (image == NULL || size == 0) {
722                 warning(dev->ctx, "Not sending any data, check the 'data' or 'size' parameters\n");
723                 return 0;
724         }
725
726         return send_data(dev, image, size);
727 }
728
729 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
730 {
731         int ret;
732         struct am7xxx_header h = {
733                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
734                 .unknown0        = 0x00,
735                 .header_data_len = sizeof(struct am7xxx_power_header),
736                 .unknown2        = 0x3e,
737                 .unknown3        = 0x10,
738         };
739
740         switch(mode) {
741         case AM7XXX_POWER_OFF:
742                 h.header_data.power.bit2 = 0;
743                 h.header_data.power.bit1 = 0;
744                 h.header_data.power.bit0 = 0;
745                 break;
746
747         case AM7XXX_POWER_LOW:
748                 h.header_data.power.bit2 = 0;
749                 h.header_data.power.bit1 = 0;
750                 h.header_data.power.bit0 = 1;
751
752         case AM7XXX_POWER_MIDDLE:
753                 h.header_data.power.bit2 = 0;
754                 h.header_data.power.bit1 = 1;
755                 h.header_data.power.bit0 = 0;
756                 break;
757
758         case AM7XXX_POWER_HIGH:
759                 h.header_data.power.bit2 = 0;
760                 h.header_data.power.bit1 = 1;
761                 h.header_data.power.bit0 = 1;
762                 break;
763
764         case AM7XXX_POWER_TURBO:
765                 h.header_data.power.bit2 = 1;
766                 h.header_data.power.bit1 = 0;
767                 h.header_data.power.bit0 = 0;
768                 break;
769
770         default:
771                 error(dev->ctx, "Power mode not supported!\n");
772                 return -EINVAL;
773         };
774
775         ret = send_header(dev, &h);
776         if (ret < 0)
777                 return ret;
778
779         return 0;
780 }