Remove generated files
[framework/connectivity/libgphoto2.git] / camlibs / digigr8 / digigr8.c
1 /*
2  * digigr8.c
3  *
4  * Part of libgphoto2/camlibs/digigr8
5  * Here are the functions which are needed to get data from the camera.  
6  *
7  * Copyright (c) 2005 Theodore Kilgore <kilgota@auburn.edu>
8  * Camera library support under libgphoto2.1.1 for camera(s) 
9  * with chipset from Service & Quality Technologies, Taiwan. 
10  * Cameras supported by this driver have Product ID 0x905C, 0x9050, or.
11  * 0x913D.
12  *
13  * Licensed under GNU Lesser General Public License, as part of Gphoto
14  * camera support project. For a copy of the license, see the file 
15  * COPYING in the main source tree of libgphoto2.
16  */    
17
18 #include <config.h>
19
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <math.h>
27 #ifdef OS2
28 #include <db.h>
29 #endif
30
31 #include <gphoto2/gphoto2.h>
32 #include <gphoto2/gphoto2-port.h>
33 #include "digigr8.h"
34
35 #define GP_MODULE "digigr8" 
36
37 #define SQWRITE gp_port_usb_msg_write
38 #define SQREAD  gp_port_usb_msg_read
39
40 int 
41 digi_init (GPPort *port, CameraPrivateLibrary *priv)
42 {
43         char c[0x14];
44         int i,j=0;
45         unsigned char *catalog = calloc(0x4010,1);
46         unsigned char *catalog_tmp;
47
48         if (!catalog) return GP_ERROR_NO_MEMORY;
49
50         SQWRITE (port, 0x0c, 0x14f4, 0x0, NULL, 0);
51         SQREAD (port, 0x0c, 0xf5, 0x00, c, 0x14);
52         SQWRITE (port, 0x0c, 0x1440, 0x110f, NULL, 0);
53         digi_reset (port);
54         SQWRITE (port, 0x0c, 0x14f0, 0x0, NULL, 0);
55
56         gp_port_read (port, c, 0x14);   
57         digi_reset(port);
58         SQWRITE (port, 0x0c, 0x20, 0x40, NULL, 0);
59         gp_port_read(port, (char *)catalog, 0x4000); /* We need 16 bytes for each photo. */
60         digi_reset (port);
61
62         /* The first occurence of a zero denotes end of files entries */
63         for (i=0; i<0x4000 && catalog[i]; i+=16);
64         priv->nb_entries = i>>4;
65         catalog_tmp = realloc(catalog, i+16);
66         memset (catalog_tmp+i, 0, 16);
67         if (i) {
68                 /*
69                  * 0x913c cameras allow individual photo deletion. This causes 
70                  * the relevant catalog line to start with 0x64. So the related
71                  * lines of config data must be removed, and the deleted 
72                  * images need to be cast out from the count.
73                  */
74
75                 for (j=0; j<i; j+=16) {
76                         if ((!catalog[j])||(catalog_tmp[j] == 0x64)) {
77                                 memcpy(catalog_tmp+j, catalog_tmp+j+16, i+16-j);
78                                 priv->nb_entries -- ;
79                         }
80                 }
81                 if (catalog_tmp) priv->catalog = catalog_tmp;
82                 else priv->catalog = catalog;
83         } else {
84                 priv->catalog = NULL;   /* We just have freed catalog_tmp */
85         }
86
87         digi_reset (port);
88         priv->last_fetched_entry = -1;
89
90         priv->init_done=1;
91         return GP_OK;
92 }
93
94
95 int
96 digi_get_comp_ratio (CameraPrivateLibrary *priv, int entry)
97 {
98         switch (priv->catalog[16*entry]) {
99         case 0x61:
100         case 0x62:
101         case 0x63:
102         case 0x76: return 1;
103         case 0x41:
104         case 0x42:
105         case 0x43:
106         case 0x52:
107         case 0x53:
108         case 0x56: 
109         case 0x72: return 0;
110         default:
111                 GP_DEBUG ("Your camera has unknown resolution settings.\n");
112                         return GP_ERROR;
113         }
114 }
115
116 int
117 digi_get_data_size (CameraPrivateLibrary *priv, int entry)
118 {
119         return ((priv->catalog[16*entry + 6] <<16) 
120                 | (priv->catalog[16*entry + 5] <<8) 
121                 | (priv->catalog[16*entry + 4] ) );
122 }
123
124 int
125 digi_get_picture_width (CameraPrivateLibrary *priv, int entry)
126 {
127         switch (priv->catalog[16*entry]) {  
128         case 0x41:
129         case 0x52:
130         case 0x61: return 352;
131         case 0x42:
132         case 0x62:
133         case 0x72: return 176;
134         case 0x43:
135         case 0x53:
136         case 0x63: return 320;
137         case 0x56:
138         case 0x76: return 640;
139         default:
140                 GP_DEBUG ("Your pictures have unknown width.\n");
141                         return 0;
142         }
143 }
144
145 int
146 digi_rewind (GPPort *port, CameraPrivateLibrary *priv)
147 {
148         static char dummy_buf[0x4000];
149         
150         
151         GP_DEBUG("REWIND cam's data pointer");
152
153         /* One has to read the catalog to rewind the data stream...
154          * I don't know if it's by design. There ought to be something better to do... :-/
155          */
156
157         digi_reset(port);
158         SQWRITE (port, 0x0c, 0x20, 0x40, NULL, 0); /* Access config */
159         gp_port_read(port, dummy_buf, 0x4000); 
160         digi_reset (port);
161
162         priv->last_fetched_entry = -1;
163         return GP_OK;
164 }
165
166
167 int
168 digi_reset (GPPort *port)
169 {
170         /* Release current register */
171         SQWRITE (port, 0x0c, 0xa0, 0x00, NULL, 0);
172
173         return GP_OK;
174 }
175
176
177 int
178 digi_read_picture_data (GPPort *port, unsigned char *data, int size, int n )
179 {
180
181
182         int remainder = size % 0x8000;
183         int offset = 0;
184         if (!n) {
185                 SQWRITE (port, 0x0c, 0x30, 0x00, NULL, 0); 
186         }       
187         while ((offset + 0x8000 < size)) {
188                 gp_port_read (port, (char *)data + offset, 0x8000);
189                         offset = offset + 0x8000;
190         }
191         gp_port_read (port, (char *)data + offset, remainder);
192
193         return GP_OK;
194
195
196 int digi_delete_all     (GPPort *port, CameraPrivateLibrary *priv) 
197 {
198         int size;
199         int num_pics;
200         unsigned char *get_size;
201         unsigned char *junk=NULL;
202
203         get_size=malloc(0x50);
204         if(!get_size) 
205                 return GP_ERROR_NO_MEMORY;
206         num_pics = priv->nb_entries;
207         GP_DEBUG("number of entries is %i\n", num_pics);
208         digi_reset (port);
209         digi_reset (port);
210         if(!num_pics) {
211                 GP_DEBUG("Camera is already empty!\n");
212                 return GP_OK;
213         }
214         SQWRITE (port, 0x0c, 0x1440, 0x110f, NULL, 0);
215         if ((gp_port_read(port, (char *)get_size, 0x50)!=0x50)) {
216                 GP_DEBUG("Error in reading data\n");
217                 return GP_ERROR;
218         }
219         GP_DEBUG("get_size[0x40] = 0x%x\n", get_size[0x40]);
220         size = get_size[0x40]|(get_size[0x41]<<8)|(get_size[0x42]<<16)|
221                                                 (get_size[0x43]<<24);
222         GP_DEBUG("size = 0x%x\n", size);
223         if(size <= 0xff) {
224                 free(get_size);
225                 GP_DEBUG("No size to read. This will not work.\n");
226                 digi_reset(port);
227                 return GP_OK;
228         }
229         junk = malloc(size);
230         if(! junk) {
231                 GP_DEBUG("allocation of junk space failed\n");
232                 return GP_ERROR_NO_MEMORY;
233         }
234         gp_port_read(port, (char *)junk, size);
235         free(junk); 
236         digi_reset (port);
237
238         return GP_OK;
239 }
240