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