4cfc9a96e7a637e0b4031667e1901ca41d771005
[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 static void dump_devinfo_header(struct am7xxx_devinfo_header *d)
150 {
151         if (d == NULL)
152                 return;
153
154         printf("Info header:\n");
155         printf("\tnative_width:  0x%08x (%u)\n", d->native_width, d->native_width);
156         printf("\tnative_height: 0x%08x (%u)\n", d->native_height, d->native_height);
157         printf("\tunknown0:      0x%08x (%u)\n", d->unknown0, d->unknown0);
158         printf("\tunknown1:      0x%08x (%u)\n", d->unknown1, d->unknown1);
159 }
160
161 static void dump_image_header(struct am7xxx_image_header *i)
162 {
163         if (i == NULL)
164                 return;
165
166         printf("Image header:\n");
167         printf("\tformat:     0x%08x (%u)\n", i->format, i->format);
168         printf("\twidth:      0x%08x (%u)\n", i->width, i->width);
169         printf("\theight:     0x%08x (%u)\n", i->height, i->height);
170         printf("\timage size: 0x%08x (%u)\n", i->image_size, i->image_size);
171 }
172
173 static void dump_power_header(struct am7xxx_power_header *p)
174 {
175         if (p == NULL)
176                 return;
177
178         printf("Power header:\n");
179         printf("\tbit2: 0x%08x (%u)\n", p->bit2, p->bit2);
180         printf("\tbit1: 0x%08x (%u)\n", p->bit1, p->bit1);
181         printf("\tbit0: 0x%08x (%u)\n", p->bit0, p->bit0);
182 }
183
184 static void dump_header(struct am7xxx_header *h)
185 {
186         if (h == NULL)
187                 return;
188
189         printf("packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
190         printf("unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
191         printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
192         printf("unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
193         printf("unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
194
195         switch(h->packet_type) {
196         case AM7XXX_PACKET_TYPE_DEVINFO:
197                 dump_devinfo_header(&(h->header_data.devinfo));
198                 break;
199
200         case AM7XXX_PACKET_TYPE_IMAGE:
201                 dump_image_header(&(h->header_data.image));
202                 break;
203
204         case AM7XXX_PACKET_TYPE_POWER:
205                 dump_power_header(&(h->header_data.power));
206                 break;
207
208         default:
209                 printf("Packet type not supported!\n");
210                 break;
211         }
212
213         fflush(stdout);
214 }
215
216 static inline unsigned int in_80chars(unsigned int i)
217 {
218         /* The 3 below is the length of "xx " where xx is the hex string
219          * representation of a byte */
220         return ((i+1) % (80/3));
221 }
222
223 static void dump_buffer(uint8_t *buffer, unsigned int len)
224 {
225         unsigned int i;
226
227         if (buffer == NULL || len == 0)
228                 return;
229
230         for (i = 0; i < len; i++) {
231                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
232         }
233         fflush(stdout);
234 }
235
236 static int read_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
237 {
238         int ret;
239         int transferred = 0;
240
241         ret = libusb_bulk_transfer(dev->usb_device, 0x81, buffer, len, &transferred, 0);
242         if (ret != 0 || (unsigned int)transferred != len) {
243                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
244                         ret, transferred, len);
245                 return ret;
246         }
247
248 #if DEBUG
249         printf("\n<-- received\n");
250         dump_buffer(buffer, len);
251         printf("\n");
252 #endif
253
254         return 0;
255 }
256
257 static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
258 {
259         int ret;
260         int transferred = 0;
261
262 #if DEBUG
263         printf("\nsending -->\n");
264         dump_buffer(buffer, len);
265         printf("\n");
266 #endif
267
268         ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
269         if (ret != 0 || (unsigned int)transferred != len) {
270                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
271                         ret, transferred, len);
272                 return ret;
273         }
274
275         return 0;
276 }
277
278 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
279 {
280         uint8_t **buffer_iterator = &buffer;
281
282         put_le32(h->packet_type, buffer_iterator);
283         put_8(h->unknown0, buffer_iterator);
284         put_8(h->header_data_len, buffer_iterator);
285         put_8(h->unknown2, buffer_iterator);
286         put_8(h->unknown3, buffer_iterator);
287         put_le32(h->header_data.data.field0, buffer_iterator);
288         put_le32(h->header_data.data.field1, buffer_iterator);
289         put_le32(h->header_data.data.field2, buffer_iterator);
290         put_le32(h->header_data.data.field3, buffer_iterator);
291 }
292
293 static void unserialize_header(uint8_t *buffer, struct am7xxx_header *h)
294 {
295         uint8_t **buffer_iterator = &buffer;
296
297         h->packet_type = get_le32(buffer_iterator);
298         h->unknown0 = get_8(buffer_iterator);
299         h->header_data_len = get_8(buffer_iterator);
300         h->unknown2 = get_8(buffer_iterator);
301         h->unknown3 = get_8(buffer_iterator);
302         h->header_data.data.field0 = get_le32(buffer_iterator);
303         h->header_data.data.field1 = get_le32(buffer_iterator);
304         h->header_data.data.field2 = get_le32(buffer_iterator);
305         h->header_data.data.field3 = get_le32(buffer_iterator);
306 }
307
308 static int read_header(am7xxx_device *dev, struct am7xxx_header *h)
309 {
310         int ret;
311
312         ret = read_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
313         if (ret < 0)
314                 goto out;
315
316         unserialize_header(dev->buffer, h);
317
318 #if DEBUG
319         printf("\n");
320         dump_header(h);
321         printf("\n");
322 #endif
323
324         ret = 0;
325
326 out:
327         return ret;
328 }
329
330 static int send_header(am7xxx_device *dev, struct am7xxx_header *h)
331 {
332         int ret;
333
334 #if DEBUG
335         printf("\n");
336         dump_header(h);
337         printf("\n");
338 #endif
339
340         serialize_header(h, dev->buffer);
341         ret = send_data(dev, dev->buffer, AM7XXX_HEADER_WIRE_SIZE);
342         if (ret < 0)
343                 fprintf(stderr, "send_header: failed to send data.\n");
344
345         return ret;
346 }
347
348 /* When level == AM7XXX_LOG_FATAL do not check the log_level from the context
349  * and print the message unconditionally, this makes it possible to print
350  * fatal messages even early on initialization, before the context has been
351  * set up */
352 static void log_message(am7xxx_context *ctx,
353                         int level,
354                         const char *function,
355                         int line,
356                         const char *fmt,
357                         ...)
358 {
359         va_list ap;
360
361         if (level == AM7XXX_LOG_FATAL || (ctx && level <= ctx->log_level)) {
362                 if (function) {
363                         fprintf(stderr, "%s", function);
364                         if (line)
365                                 fprintf(stderr, "[%d]", line);
366                         fprintf(stderr, ": ");
367                 }
368
369                 va_start(ap, fmt);
370                 vfprintf(stderr, fmt, ap);
371                 va_end(ap);
372         }
373
374         return;
375 }
376
377 static am7xxx_device *add_new_device(am7xxx_context *ctx)
378 {
379         am7xxx_device **devices_list;
380         am7xxx_device *new_device;
381
382         if (ctx == NULL) {
383                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
384                 return NULL;
385         }
386
387         devices_list = &(ctx->devices_list);
388
389         new_device = malloc(sizeof(*new_device));
390         if (new_device == NULL) {
391                 perror("malloc");
392                 return NULL;
393         }
394         memset(new_device, 0, sizeof(*new_device));
395
396         new_device->ctx = ctx;
397
398         if (*devices_list == NULL) {
399                 *devices_list = new_device;
400         } else {
401                 am7xxx_device *prev = *devices_list;
402                 while (prev->next)
403                         prev = prev->next;
404                 prev->next = new_device;
405         }
406         return new_device;
407 }
408
409 static am7xxx_device *find_device(am7xxx_context *ctx,
410                                   unsigned int device_index)
411 {
412         unsigned int i = 0;
413         am7xxx_device *current;
414
415         if (ctx == NULL) {
416                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
417                 return NULL;
418         }
419
420         current = ctx->devices_list;
421         while (current && i++ < device_index)
422                 current = current->next;
423
424         return current;
425 }
426
427 typedef enum {
428         SCAN_OP_BUILD_DEVLIST,
429         SCAN_OP_OPEN_DEVICE,
430 } scan_op;
431
432 /**
433  * This is where the central logic of multi-device support is.
434  *
435  * When 'op' == SCAN_OP_BUILD_DEVLIST the parameters 'open_device_index' and
436  * 'dev' are ignored; the function returns 0 on success and a negative value
437  * on error.
438  *
439  * When 'op' == SCAN_OP_OPEN_DEVICE the function opens the supported USB
440  * device with index 'open_device_index' and returns the correspondent
441  * am7xxx_device in the 'dev' parameter; the function returns 0 on success,
442  * 1 if the device was already open and a negative value on error.
443  * 
444  * NOTES:
445  * if scan_devices() fails when called with 'op' == SCAN_OP_BUILD_DEVLIST,
446  * the caller might want to call am7xxx_shutdown() in order to remove
447  * devices possibly added before the failure.
448  */
449 static int scan_devices(am7xxx_context *ctx, scan_op op,
450                         unsigned int open_device_index, am7xxx_device **dev)
451 {
452         int num_devices;
453         libusb_device** list;
454         unsigned int current_index;
455         int i;
456         int ret;
457
458         if (ctx == NULL) {
459                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
460                 return -EINVAL;
461         }
462         if (op == SCAN_OP_BUILD_DEVLIST && ctx->devices_list != NULL) {
463                 fprintf(stderr, "%s: device scan done already? Abort!\n", __func__);
464                 return -EINVAL;
465         }
466
467         num_devices = libusb_get_device_list(ctx->usb_context, &list);
468         if (num_devices < 0) {
469                 ret = -ENODEV;
470                 goto out;
471         }
472
473         current_index = 0;
474         for (i = 0; i < num_devices; i++) {
475                 struct libusb_device_descriptor desc;
476                 unsigned int j;
477
478                 ret = libusb_get_device_descriptor(list[i], &desc);
479                 if (ret < 0)
480                         continue;
481
482                 for (j = 0; j < ARRAY_SIZE(supported_devices); j++) {
483                         if (desc.idVendor == supported_devices[j].vendor_id
484                             && desc.idProduct == supported_devices[j].product_id) {
485
486                                 if (op == SCAN_OP_BUILD_DEVLIST) {
487                                         am7xxx_device *new_device;
488                                         /* debug */
489                                         printf("am7xxx device found, index: %d, name: %s\n",
490                                                current_index,
491                                                supported_devices[j].name);
492                                         new_device = add_new_device(ctx);
493                                         if (new_device == NULL) {
494                                                 /* XXX, the caller may want
495                                                  * to call am7xxx_shutdown() if
496                                                  * we fail here, as we may have
497                                                  * added some devices already
498                                                  */
499                                                 ret = -ENODEV;
500                                                 goto out;
501                                         }
502                                 } else if (op == SCAN_OP_OPEN_DEVICE &&
503                                            current_index == open_device_index) {
504
505                                         *dev = find_device(ctx, open_device_index);
506                                         if (*dev == NULL) {
507                                                 ret = -ENODEV;
508                                                 goto out;
509                                         }
510
511                                         /* the usb device has already been opened */
512                                         if ((*dev)->usb_device) {
513                                                 ret = 1;
514                                                 goto out;
515                                         }
516
517                                         ret = libusb_open(list[i], &((*dev)->usb_device));
518                                         if (ret < 0)
519                                                 goto out;
520
521                                         libusb_set_configuration((*dev)->usb_device, 1);
522                                         libusb_claim_interface((*dev)->usb_device, 0);
523                                         goto out;
524                                 }
525                                 current_index++;
526                         }
527                 }
528         }
529
530         /* if we made it up to here we didn't find any device to open */
531         if (op == SCAN_OP_OPEN_DEVICE) {
532                 ret = -ENODEV;
533                 goto out;
534         }
535
536         /* everything went fine when building the device list */
537         ret = 0;
538 out:
539         libusb_free_device_list(list, 1);
540         return ret;
541 }
542
543 int am7xxx_init(am7xxx_context **ctx)
544 {
545         int ret = 0;
546
547         *ctx = malloc(sizeof(**ctx));
548         if (*ctx == NULL) {
549                 perror("malloc");
550                 ret = -ENOMEM;
551                 goto out;
552         }
553         memset(*ctx, 0, sizeof(**ctx));
554
555         /* Set the highest log level during initialization */
556         (*ctx)->log_level = AM7XXX_LOG_TRACE;
557
558         ret = libusb_init(&((*ctx)->usb_context));
559         if (ret < 0)
560                 goto out_free_context;
561
562         libusb_set_debug((*ctx)->usb_context, 3);
563
564         ret = scan_devices(*ctx, SCAN_OP_BUILD_DEVLIST , 0, NULL);
565         if (ret < 0) {
566                 fprintf(stderr, "%s: scan_devices failed\n", __func__);
567                 am7xxx_shutdown(*ctx);
568                 goto out;
569         }
570
571         /* Set a quieter log level as default for normal operation */
572         (*ctx)->log_level = AM7XXX_LOG_ERROR;
573         return 0;
574
575 out_free_context:
576         free(*ctx);
577         *ctx = NULL;
578 out:
579         return ret;
580 }
581
582 void am7xxx_shutdown(am7xxx_context *ctx)
583 {
584         am7xxx_device *current;
585
586         if (ctx == NULL) {
587                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
588                 return;
589         }
590
591         current = ctx->devices_list;
592         while (current) {
593                 am7xxx_device *next = current->next;
594                 am7xxx_close_device(current);
595                 free(current);
596                 current = next;
597         }
598
599         libusb_exit(ctx->usb_context);
600         free(ctx);
601         ctx = NULL;
602 }
603
604 void am7xxx_set_log_level(am7xxx_context *ctx, am7xxx_log_level log_level)
605 {
606         ctx->log_level = log_level;
607 }
608
609 int am7xxx_open_device(am7xxx_context *ctx, am7xxx_device **dev,
610                        unsigned int device_index)
611 {
612         int ret;
613
614         if (ctx == NULL) {
615                 fprintf(stderr, "%s: context must not be NULL!\n", __func__);
616                 return -EINVAL;
617         }
618
619         ret = scan_devices(ctx, SCAN_OP_OPEN_DEVICE, device_index, dev);
620         if (ret < 0) {
621                 errno = ENODEV;
622         } else if (ret > 0) {
623                 /* warning */
624                 fprintf(stderr, "%s: device %d already open\n", __func__, device_index);
625                 errno = EBUSY;
626                 ret = -EBUSY;
627         }
628
629         return ret;
630 }
631
632 int am7xxx_close_device(am7xxx_device *dev)
633 {
634         if (dev == NULL) {
635                 fprintf(stderr, "%s: dev must not be NULL!\n", __func__);
636                 return -EINVAL;
637         }
638         if (dev->usb_device) {
639                 libusb_release_interface(dev->usb_device, 0);
640                 libusb_close(dev->usb_device);
641                 dev->usb_device = NULL;
642         }
643         return 0;
644 }
645
646 int am7xxx_get_device_info(am7xxx_device *dev,
647                            unsigned int *native_width,
648                            unsigned int *native_height,
649                            unsigned int *unknown0,
650                            unsigned int *unknown1)
651 {
652         int ret;
653         struct am7xxx_header h = {
654                 .packet_type     = AM7XXX_PACKET_TYPE_DEVINFO,
655                 .unknown0        = 0x00,
656                 .header_data_len = 0x00,
657                 .unknown2        = 0x3e,
658                 .unknown3        = 0x10,
659                 .header_data = {
660                         .devinfo = {
661                                 .native_width  = 0,
662                                 .native_height = 0,
663                                 .unknown0      = 0,
664                                 .unknown1      = 0,
665                         },
666                 },
667         };
668
669         ret = send_header(dev, &h);
670         if (ret < 0)
671                 return ret;
672
673         ret = read_header(dev, &h);
674         if (ret < 0)
675                 return ret;
676
677         *native_width = h.header_data.devinfo.native_width;
678         *native_height = h.header_data.devinfo.native_height;
679         *unknown0 = h.header_data.devinfo.unknown0;
680         *unknown1 = h.header_data.devinfo.unknown1;
681
682         return 0;
683 }
684
685 int am7xxx_send_image(am7xxx_device *dev,
686                       am7xxx_image_format format,
687                       unsigned int width,
688                       unsigned int height,
689                       uint8_t *image,
690                       unsigned int size)
691 {
692         int ret;
693         struct am7xxx_header h = {
694                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
695                 .unknown0        = 0x00,
696                 .header_data_len = sizeof(struct am7xxx_image_header),
697                 .unknown2        = 0x3e,
698                 .unknown3        = 0x10,
699                 .header_data = {
700                         .image = {
701                                 .format     = format,
702                                 .width      = width,
703                                 .height     = height,
704                                 .image_size = size,
705                         },
706                 },
707         };
708
709         ret = send_header(dev, &h);
710         if (ret < 0)
711                 return ret;
712
713         if (image == NULL || size == 0)
714                 return 0;
715
716         return send_data(dev, image, size);
717 }
718
719 int am7xxx_set_power_mode(am7xxx_device *dev, am7xxx_power_mode mode)
720 {
721         int ret;
722         struct am7xxx_header h = {
723                 .packet_type     = AM7XXX_PACKET_TYPE_POWER,
724                 .unknown0        = 0x00,
725                 .header_data_len = sizeof(struct am7xxx_power_header),
726                 .unknown2        = 0x3e,
727                 .unknown3        = 0x10,
728         };
729
730         switch(mode) {
731         case AM7XXX_POWER_OFF:
732                 h.header_data.power.bit2 = 0;
733                 h.header_data.power.bit1 = 0;
734                 h.header_data.power.bit0 = 0;
735                 break;
736
737         case AM7XXX_POWER_LOW:
738                 h.header_data.power.bit2 = 0;
739                 h.header_data.power.bit1 = 0;
740                 h.header_data.power.bit0 = 1;
741
742         case AM7XXX_POWER_MIDDLE:
743                 h.header_data.power.bit2 = 0;
744                 h.header_data.power.bit1 = 1;
745                 h.header_data.power.bit0 = 0;
746                 break;
747
748         case AM7XXX_POWER_HIGH:
749                 h.header_data.power.bit2 = 0;
750                 h.header_data.power.bit1 = 1;
751                 h.header_data.power.bit0 = 1;
752                 break;
753
754         case AM7XXX_POWER_TURBO:
755                 h.header_data.power.bit2 = 1;
756                 h.header_data.power.bit1 = 0;
757                 h.header_data.power.bit0 = 0;
758                 break;
759
760         default:
761                 fprintf(stderr, "Unsupported power mode.\n");
762                 return -EINVAL;
763         };
764
765         ret = send_header(dev, &h);
766         if (ret < 0)
767                 return ret;
768
769         return 0;
770 }