182ea27d4fb2650cfb0cda591b9b5173e7ad68f3
[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 <errno.h>
22
23 #include "am7xxx.h"
24 #include "serialize.h"
25
26 #define AM7XXX_VENDOR_ID  0x1de1
27 #define AM7XXX_PRODUCT_ID 0xc101
28
29 #if 1
30 static uint8_t reference_image_header[] = {
31         0x02, 0x00, 0x00, 0x00,
32         0x00,
33         0x10,
34         0x3e,
35         0x10,
36         0x01, 0x00, 0x00, 0x00,
37         0x20, 0x03, 0x00, 0x00,
38         0xe0, 0x01, 0x00, 0x00,
39         0x53, 0xE8, 0x00, 0x00
40 };
41 #endif
42
43 static void dump_image_header(struct am7xxx_image_header *i)
44 {
45         if (i == NULL)
46                 return;
47
48         printf("Image header:\n");
49         printf("format:      0x%08x (%u)\n", i->format, i->format);
50         printf("width:       0x%08x (%u)\n", i->width, i->width);
51         printf("height:      0x%08x (%u)\n", i->height, i->height);
52         printf("image size:  0x%08x (%u)\n", i->image_size, i->image_size);
53 }
54
55 static void dump_header(struct am7xxx_header *h)
56 {
57         if (h == NULL)
58                 return;
59
60         printf("packet_type:     0x%08x (%u)\n", h->packet_type, h->packet_type);
61         printf("unknown0:        0x%02hhx (%hhu)\n", h->unknown0, h->unknown0);
62         printf("header_data_len: 0x%02hhx (%hhu)\n", h->header_data_len, h->header_data_len);
63         printf("unknown2:        0x%02hhx (%hhu)\n", h->unknown2, h->unknown2);
64         printf("unknown3:        0x%02hhx (%hhu)\n", h->unknown3, h->unknown3);
65
66         switch(h->packet_type) {
67         case AM7XXX_PACKET_TYPE_IMAGE:
68                 dump_image_header(&(h->header_data.image));
69                 break;
70
71         default:
72                 printf("Packet type not supported!\n");
73                 break;
74         }
75
76         fflush(stdout);
77 }
78
79 static inline unsigned int in_80chars(unsigned int i)
80 {
81         return ((i+1) % (80/3));
82 }
83
84 static void dump_buffer(uint8_t *buffer, unsigned int len)
85 {
86         unsigned int i;
87
88         if (buffer == NULL || len == 0)
89                 return;
90
91         for (i = 0; i < len; i++) {
92                 printf("%02hhX%c", buffer[i], (in_80chars(i) && (i < len - 1)) ? ' ' : '\n');
93         }
94         fflush(stdout);
95 }
96
97 static int send_data(am7xxx_device dev, uint8_t *buffer, unsigned int len)
98 {
99         int ret;
100         int transferred;
101
102         dump_buffer(buffer, len);
103
104         ret = libusb_bulk_transfer(dev, 1, buffer, len, &transferred, 0);
105         if (ret != 0 || (unsigned int)transferred != len) {
106                 fprintf(stderr, "Error: ret: %d\ttransferred: %d (expected %u)\n",
107                         ret, transferred, len);
108                 return ret;
109         }
110
111         return 0;
112 }
113
114 static void serialize_header(struct am7xxx_header *h, uint8_t *buffer)
115 {
116         uint8_t **buffer_iterator = &buffer;
117
118         put_le32(h->packet_type, buffer_iterator);
119         put_8(h->unknown0, buffer_iterator);
120         put_8(h->header_data_len, buffer_iterator);
121         put_8(h->unknown2, buffer_iterator);
122         put_8(h->unknown3, buffer_iterator);
123         put_le32(h->header_data.data.field0, buffer_iterator);
124         put_le32(h->header_data.data.field1, buffer_iterator);
125         put_le32(h->header_data.data.field2, buffer_iterator);
126         put_le32(h->header_data.data.field3, buffer_iterator);
127 }
128
129 static int send_header(am7xxx_device dev, struct am7xxx_header *h)
130 {
131         uint8_t *buffer;
132         int ret;
133
134         buffer = calloc(AM7XXX_HEADER_WIRE_SIZE, 1);
135         if (buffer == NULL) {
136                 perror("calloc buffer");
137                 return -ENOMEM;
138         }
139
140         serialize_header(h, buffer);
141         ret = send_data(dev, buffer, AM7XXX_HEADER_WIRE_SIZE);
142         if (ret < 0)
143                 fprintf(stderr, "send_header: failed to send data.\n");
144
145         free(buffer);
146         return ret;
147 }
148
149 am7xxx_device am7xxx_init(void)
150 {
151         am7xxx_device dev;
152
153         libusb_init(NULL);
154         libusb_set_debug(NULL, 3);
155
156         dev = libusb_open_device_with_vid_pid(NULL,
157                                               AM7XXX_VENDOR_ID,
158                                               AM7XXX_PRODUCT_ID);
159         if (dev == NULL) {
160                 errno = ENODEV;
161                 perror("libusb_open_device_with_vid_pid");
162                 goto out_libusb_exit;
163         }
164
165         libusb_set_configuration(dev, 1);
166         libusb_claim_interface(dev, 0);
167
168         return dev;
169
170 out_libusb_exit:
171         libusb_exit(NULL);
172         return NULL;
173 }
174
175 void am7xxx_shutdown(am7xxx_device dev)
176 {
177         if (dev) {
178                 libusb_close(dev);
179                 libusb_exit(NULL);
180         }
181 }
182
183 int am7xxx_send_image(am7xxx_device dev,
184                       am7xxx_image_format format,
185                       unsigned int width,
186                       unsigned int height,
187                       uint8_t *image,
188                       unsigned int size)
189 {
190         int ret;
191         struct am7xxx_header h = {
192                 .packet_type     = AM7XXX_PACKET_TYPE_IMAGE,
193                 .unknown0        = 0x00,
194                 .header_data_len = sizeof(struct am7xxx_image_header),
195                 .unknown2        = 0x3e,
196                 .unknown3        = 0x10,
197                 .header_data = {
198                         .image = {
199                                 .format     = format,
200                                 .width      = width,
201                                 .height     = height,
202                                 .image_size = size,
203                         },
204                 },
205         };
206
207         dump_header(&h);
208         printf("\n");
209
210         printf("Dump Buffers\n");
211         dump_buffer(reference_image_header, sizeof(struct am7xxx_header));
212
213         ret = send_header(dev, &h);
214         if (ret < 0)
215                 return ret;
216
217         if (image == NULL || size == 0)
218                 return 0;
219
220         return send_data(dev, image, size);
221 }