| 1 |
/* pdrm11.c -- interfaces directly with the camera |
| 2 |
* |
| 3 |
* Copyright 2003 David Hogue <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library 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 GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the |
| 17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 |
* Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "pdrm11.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include <gphoto2/gphoto2.h> |
| 29 |
#include "libgphoto2/gphoto2-endian.h" |
| 30 |
|
| 31 |
#define GP_MODULE "pdrm11" |
| 32 |
#define ETIMEDOUT 110 |
| 33 |
|
| 34 |
/* do not sleep during fuzzing */ |
| 35 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 36 |
# define usleep(x) |
| 37 |
#endif |
| 38 |
|
| 39 |
static int pdrm11_select_file(GPPort *port, uint16_t file); |
| 40 |
|
| 41 |
int pdrm11_init(GPPort *port) |
| 42 |
{ |
| 43 |
unsigned char buf[20]; |
| 44 |
int timeout = 50; |
| 45 |
|
| 46 |
gp_port_set_timeout(port,1000); |
| 47 |
|
| 48 |
/* exactly what windows driver does */ |
| 49 |
gp_port_usb_msg_read (port, 0x01, PDRM11_CMD_READY, 0, (char *)buf, 4); |
| 50 |
gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_PING3, 0, NULL, 0); |
| 51 |
gp_port_usb_msg_read (port, 0x01, PDRM11_CMD_READY, 0, (char *)buf, 4); |
| 52 |
gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_INIT1, 0, NULL, 0); |
| 53 |
gp_port_usb_msg_read (port, 0x01, PDRM11_CMD_READY, 0, (char *)buf, 4); |
| 54 |
gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_INIT2, 0, NULL, 0); |
| 55 |
gp_port_usb_msg_read (port, 0x01, PDRM11_CMD_READY, 0, (char *)buf, 4); |
| 56 |
|
| 57 |
gp_port_usb_msg_read (port, 0x01, PDRM11_CMD_ZERO, 0, (char *)buf, 2); |
| 58 |
if(buf[0] || buf[1]) { |
| 59 |
/* I haven't seen anything other than 00 00 yet |
| 60 |
* unless the connection is bad */ |
| 61 |
GP_DEBUG("PDRM11_CMD_ZERO: %x %x", buf[0], buf[1]); |
| 62 |
return(GP_ERROR); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/* wait til the camera is ready */ |
| 67 |
do { |
| 68 |
usleep(200000); |
| 69 |
GP_DEBUG("waiting..."); |
| 70 |
|
| 71 |
timeout--; |
| 72 |
if( gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_READY, 0, (char *)buf, 4) == -ETIMEDOUT ) |
| 73 |
timeout = 0; |
| 74 |
} while( !((buf[3] == 0x25) && (buf[0] == 1)) && timeout ); |
| 75 |
|
| 76 |
/* what good is this? */ |
| 77 |
usleep(400000); |
| 78 |
|
| 79 |
if(!timeout) |
| 80 |
return(GP_ERROR_TIMEOUT); |
| 81 |
else |
| 82 |
return(GP_OK); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
int pdrm11_get_filenames(GPPort *port, CameraList *list) |
| 88 |
{ |
| 89 |
unsigned int i, j; |
| 90 |
uint32_t numPics; |
| 91 |
char name[20]; |
| 92 |
char buf[30]; |
| 93 |
|
| 94 |
|
| 95 |
gp_port_set_timeout(port, 10000); |
| 96 |
CHECK(gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_NUMPICS, 0, buf, 10)); |
| 97 |
/* trying to remain endian friendly */ |
| 98 |
numPics = le16atoh(&buf[2]) + (le16atoh(&buf[0]) * 1024); |
| 99 |
GP_DEBUG("found %d pictures", numPics); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
for(i=1; i<numPics+1; i++) { |
| 104 |
CHECK( pdrm11_select_file(port, i) ); |
| 105 |
|
| 106 |
CHECK(gp_port_usb_msg_read(port, 0x01, 0xe600, i, buf, 14)); |
| 107 |
|
| 108 |
/* the filename is 12 chars starting at the third byte */ |
| 109 |
CHECK(gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_FILENAME, i, buf, 26)); |
| 110 |
for(j=0; j<12; j+=2) { |
| 111 |
name[j] = buf[j+2+1]; |
| 112 |
name[j+1] = buf[j+2]; |
| 113 |
} |
| 114 |
name[12] = '\0'; |
| 115 |
|
| 116 |
GP_DEBUG("%s",name); |
| 117 |
gp_list_append(list, name, NULL); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
return(GP_OK); |
| 122 |
} |
| 123 |
|
| 124 |
static int pdrm11_select_file(GPPort *port, uint16_t file) |
| 125 |
{ |
| 126 |
char buf[10]; |
| 127 |
|
| 128 |
uint16_t picNum = htole16(file); |
| 129 |
uint16_t file_type; |
| 130 |
|
| 131 |
/* byte 4 of PDRM11_CMD_GET_INFO determines if the file is a jpeg or tiff */ |
| 132 |
CHECK(gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_INFO, file, buf, 8)); |
| 133 |
file_type = htole16(buf[4]); |
| 134 |
|
| 135 |
CHECK( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_SELECT_PIC1, file, (char*)&picNum, 2) ); |
| 136 |
CHECK( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_SELECT_PIC2, file, (char*)&file_type, 2) ); |
| 137 |
|
| 138 |
return(GP_OK); |
| 139 |
} |
| 140 |
|
| 141 |
#if 0 |
| 142 |
static int pdrm11_ping(GPPort *port) |
| 143 |
{ |
| 144 |
CHECK( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_PING1, 1, NULL, 0) ); |
| 145 |
CHECK( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_PING2, 1, NULL, 0) ); |
| 146 |
|
| 147 |
return(GP_OK); |
| 148 |
} |
| 149 |
#endif |
| 150 |
|
| 151 |
|
| 152 |
int pdrm11_get_file(CameraFilesystem *fs, const char *filename, CameraFileType type, |
| 153 |
CameraFile *file, GPPort *port, uint16_t picNum) |
| 154 |
{ |
| 155 |
uint32_t size = 0; |
| 156 |
uint16_t thumbsize = 0; |
| 157 |
uint8_t buf[30]; |
| 158 |
uint8_t *image; |
| 159 |
uint8_t temp; |
| 160 |
unsigned int i; |
| 161 |
int ret; |
| 162 |
int file_type; |
| 163 |
|
| 164 |
|
| 165 |
gp_port_set_timeout(port,10000); |
| 166 |
CHECK( pdrm11_select_file(port, picNum) ); |
| 167 |
|
| 168 |
if(type == GP_FILE_TYPE_PREVIEW) { |
| 169 |
CHECK(gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_INFO, picNum, (char *)buf, 8)); |
| 170 |
file_type = buf[4]; |
| 171 |
|
| 172 |
CHECK( gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_THUMBSIZE, picNum, (char *)buf, 14) ); |
| 173 |
thumbsize = le16atoh( &buf[8] ); |
| 174 |
|
| 175 |
/* add 1 to file size only for jpeg thumbnails */ |
| 176 |
if(file_type == 1) { |
| 177 |
GP_DEBUG("thumbnail file_type: %s.", "jpeg"); |
| 178 |
size = (uint32_t)thumbsize + 1; |
| 179 |
} else if(file_type == 2) { |
| 180 |
/* NOTE: tiff thumbnails are 160x120 pixel 8bpc rgb images, NOT jpegs... */ |
| 181 |
GP_DEBUG("thumbnail file_type: %s.", "tiff"); |
| 182 |
size = (uint32_t)thumbsize; |
| 183 |
} else { |
| 184 |
GP_DEBUG("Unknown thumbnail file format!"); |
| 185 |
return(GP_ERROR_NOT_SUPPORTED); |
| 186 |
} |
| 187 |
|
| 188 |
} else if(type == GP_FILE_TYPE_NORMAL) { |
| 189 |
CHECK( gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_GET_FILESIZE, picNum, (char *)buf, 26) ); |
| 190 |
size = le32atoh( &buf[18] ); |
| 191 |
} else { |
| 192 |
GP_DEBUG("Unsupported file type!"); |
| 193 |
return(GP_ERROR_NOT_SUPPORTED); |
| 194 |
} |
| 195 |
|
| 196 |
GP_DEBUG("size: %d 0x%x", size, size); |
| 197 |
|
| 198 |
image = malloc(size); |
| 199 |
if(!image) |
| 200 |
return(GP_ERROR_NO_MEMORY); |
| 201 |
|
| 202 |
if(type == GP_FILE_TYPE_PREVIEW) { |
| 203 |
CHECK_AND_FREE( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_GET_THUMB, picNum, NULL, 0), image ); |
| 204 |
} else { |
| 205 |
CHECK_AND_FREE( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_GET_PIC, picNum, NULL, 0), image ); |
| 206 |
} |
| 207 |
|
| 208 |
ret = gp_port_read(port, (char *)image, size); |
| 209 |
if(ret < GP_OK || (unsigned int)ret != size) { |
| 210 |
GP_DEBUG("failed to read from port. Giving it one more try..."); |
| 211 |
ret = gp_port_read(port, (char *)image, size); |
| 212 |
if(ret < GP_OK || (unsigned int)ret != size) { |
| 213 |
GP_DEBUG("gp_port_read returned %d 0x%x. size: %d 0x%x", ret, ret, size, size); |
| 214 |
free (image); |
| 215 |
return(GP_ERROR_IO_READ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* swap the bytes for the thumbnail, but not the file */ |
| 220 |
if(type == GP_FILE_TYPE_PREVIEW) { |
| 221 |
for(i=0; i<size; i+=2) { |
| 222 |
temp = image[i]; |
| 223 |
image[i] = image[i+1]; |
| 224 |
image[i+1] = temp; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
gp_file_set_mime_type(file, GP_MIME_JPEG); |
| 230 |
gp_file_set_data_and_size(file, (char *)image, size); |
| 231 |
|
| 232 |
return(GP_OK); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
int pdrm11_delete_file(GPPort *port, int picNum) |
| 238 |
{ |
| 239 |
uint8_t buf[2]; |
| 240 |
|
| 241 |
/* for some reason the windows driver sends b200 twice |
| 242 |
* once in pdrm11_select_file and once before. dunno why */ |
| 243 |
CHECK( gp_port_usb_msg_write(port, 0x01, PDRM11_CMD_SELECT_PIC1, picNum, (char*)&picNum, 2) ); |
| 244 |
CHECK( pdrm11_select_file(port, picNum) ); |
| 245 |
|
| 246 |
/* should always be 00 00 */ |
| 247 |
gp_port_usb_msg_read(port, 0x01, PDRM11_CMD_DELETE, picNum, (char *)buf, 2); |
| 248 |
if( (buf[0] != 0) || (buf[1] !=0) ) { |
| 249 |
GP_DEBUG("should have read 00 00. actually read %2x %2x.", buf[0], buf[1]); |
| 250 |
return(GP_ERROR); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
return(GP_OK); |
| 255 |
} |