| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* vusb.c |
| 3 |
* |
| 4 |
* Copyright (c) 2015,2016 Marcus Meissner <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the |
| 18 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 |
* Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
#define _DARWIN_C_SOURCE |
| 22 |
#include "config.h" |
| 23 |
#include <gphoto2/gphoto2-port-library.h> |
| 24 |
|
| 25 |
#include "vcamera.h" |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#ifdef HAVE_SYS_PARAM_H |
| 33 |
#include <sys/param.h> |
| 34 |
#endif |
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
#include <gphoto2/gphoto2-port.h> |
| 38 |
#include <gphoto2/gphoto2-port-result.h> |
| 39 |
#include <gphoto2/gphoto2-port-log.h> |
| 40 |
|
| 41 |
#include "libgphoto2_port/i18n.h" |
| 42 |
|
| 43 |
|
| 44 |
#define CHECK(result) {int r=(result); if (r<0) return (r);} |
| 45 |
|
| 46 |
struct _GPPortPrivateLibrary { |
| 47 |
int isopen; |
| 48 |
vcamera *vcamera; |
| 49 |
}; |
| 50 |
|
| 51 |
GPPortType |
| 52 |
gp_port_library_type (void) |
| 53 |
{ |
| 54 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 55 |
return GP_PORT_USB; |
| 56 |
} |
| 57 |
|
| 58 |
int |
| 59 |
gp_port_library_list (GPPortInfoList *list) |
| 60 |
{ |
| 61 |
GPPortInfo info; |
| 62 |
|
| 63 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 64 |
|
| 65 |
CHECK (gp_port_info_new (&info)); |
| 66 |
gp_port_info_set_type (info, GP_PORT_USB); |
| 67 |
gp_port_info_set_name (info, ""); |
| 68 |
gp_port_info_set_path (info, "^usb:"); |
| 69 |
gp_port_info_list_append (list, info); /* do not check, might be -1 */ |
| 70 |
|
| 71 |
CHECK (gp_port_info_new (&info)); |
| 72 |
gp_port_info_set_type (info, GP_PORT_USB); |
| 73 |
gp_port_info_set_name (info, ""); |
| 74 |
gp_port_info_set_path (info, "^vusb:"); |
| 75 |
gp_port_info_list_append (list, info); /* do not check, might be -1 */ |
| 76 |
|
| 77 |
gp_port_info_new (&info); |
| 78 |
gp_port_info_set_type (info, GP_PORT_USB); |
| 79 |
gp_port_info_set_name (info, "Universal Serial Bus"); |
| 80 |
gp_port_info_set_path (info, "usb:001,001"); |
| 81 |
CHECK (gp_port_info_list_append (list, info)); |
| 82 |
|
| 83 |
gp_port_info_new (&info); |
| 84 |
gp_port_info_set_type (info, GP_PORT_USB_SCSI); |
| 85 |
gp_port_info_set_name (info, ""); |
| 86 |
gp_port_info_set_path (info, "^usbscsi:"); |
| 87 |
CHECK (gp_port_info_list_append (list, info)); |
| 88 |
|
| 89 |
gp_port_info_new (&info); |
| 90 |
gp_port_info_set_type (info, GP_PORT_USB_DISK_DIRECT); |
| 91 |
gp_port_info_set_name (info, ""); |
| 92 |
gp_port_info_set_path (info, "^usbdiskdirect:"); |
| 93 |
CHECK (gp_port_info_list_append (list, info)); |
| 94 |
return GP_OK; |
| 95 |
} |
| 96 |
|
| 97 |
static int gp_port_vusb_init (GPPort *dev) |
| 98 |
{ |
| 99 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 100 |
C_MEM (dev->pl = calloc (1, sizeof (GPPortPrivateLibrary))); |
| 101 |
|
| 102 |
dev->pl->vcamera = vcamera_new(NIKON_D750); |
| 103 |
dev->pl->vcamera->init(dev->pl->vcamera); |
| 104 |
|
| 105 |
return GP_OK; |
| 106 |
} |
| 107 |
|
| 108 |
static int |
| 109 |
gp_port_vusb_exit (GPPort *port) |
| 110 |
{ |
| 111 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 112 |
port->pl->vcamera->exit(port->pl->vcamera); |
| 113 |
free (port->pl->vcamera); |
| 114 |
port->pl->vcamera = NULL; |
| 115 |
free (port->pl); |
| 116 |
port->pl = NULL; |
| 117 |
|
| 118 |
|
| 119 |
return GP_OK; |
| 120 |
} |
| 121 |
|
| 122 |
static int |
| 123 |
gp_port_vusb_open (GPPort *port) |
| 124 |
{ |
| 125 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(%s)", port->settings.usb.port); |
| 126 |
if (port->pl->isopen) |
| 127 |
return GP_ERROR; |
| 128 |
port->pl->vcamera->open(port->pl->vcamera, port->settings.usb.port); |
| 129 |
port->pl->isopen = 1; |
| 130 |
return GP_OK; |
| 131 |
} |
| 132 |
|
| 133 |
static int |
| 134 |
gp_port_vusb_close (GPPort *port) |
| 135 |
{ |
| 136 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 137 |
if (!port->pl->isopen) |
| 138 |
return GP_ERROR; |
| 139 |
port->pl->vcamera->close(port->pl->vcamera); |
| 140 |
port->pl->isopen = 0; |
| 141 |
return GP_OK; |
| 142 |
} |
| 143 |
|
| 144 |
static int |
| 145 |
gp_port_vusb_write (GPPort *port, const char *bytes, int size) |
| 146 |
{ |
| 147 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(%d)", size); |
| 148 |
|
| 149 |
C_PARAMS (port && port->pl && port->pl->vcamera); |
| 150 |
return port->pl->vcamera->write(port->pl->vcamera, 0x02, (unsigned char*)bytes, size); |
| 151 |
} |
| 152 |
|
| 153 |
static int |
| 154 |
gp_port_vusb_read(GPPort *port, char *bytes, int size) |
| 155 |
{ |
| 156 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(%d)", size); |
| 157 |
return port->pl->vcamera->read(port->pl->vcamera, 0x81, (unsigned char*)bytes, size); |
| 158 |
} |
| 159 |
|
| 160 |
static int |
| 161 |
gp_port_vusb_send_scsi_cmd (GPPort *port, int to_dev, char *cmd, |
| 162 |
int cmd_size, char *sense, int sense_size, char *data, int data_size) |
| 163 |
{ |
| 164 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(dev=%d, cmdsize %d, sense_size %d, data_size %d)", to_dev, cmd_size, sense_size, data_size); |
| 165 |
return port->pl->vcamera->read(port->pl->vcamera, 0x81, (unsigned char*)data, data_size); |
| 166 |
} |
| 167 |
|
| 168 |
static int |
| 169 |
gp_port_vusb_seek (GPPort *port, int offset, int whence) |
| 170 |
{ |
| 171 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(offset %d, whence %d)", offset, whence); |
| 172 |
return GP_OK; |
| 173 |
} |
| 174 |
|
| 175 |
static int |
| 176 |
gp_port_vusb_reset(GPPort *port) |
| 177 |
{ |
| 178 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 179 |
C_PARAMS (port && port->pl); |
| 180 |
|
| 181 |
return GP_OK; |
| 182 |
} |
| 183 |
|
| 184 |
static int |
| 185 |
gp_port_vusb_check_int (GPPort *port, char *bytes, int size, int timeout) |
| 186 |
{ |
| 187 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(bytes=%d, timeout=%d)", size, timeout); |
| 188 |
C_PARAMS (port && port->pl && timeout >= 0); |
| 189 |
|
| 190 |
return port->pl->vcamera->readint(port->pl->vcamera, (unsigned char*)bytes, size, timeout); |
| 191 |
} |
| 192 |
|
| 193 |
static int |
| 194 |
gp_port_vusb_update (GPPort *port) |
| 195 |
{ |
| 196 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 197 |
return GP_OK; |
| 198 |
} |
| 199 |
|
| 200 |
static int |
| 201 |
gp_port_vusb_clear_halt_lib(GPPort *port, int ep) |
| 202 |
{ |
| 203 |
unsigned char internal_ep; |
| 204 |
|
| 205 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(ep=%d)", ep); |
| 206 |
C_PARAMS (port && port->pl); |
| 207 |
|
| 208 |
switch (ep) { |
| 209 |
case GP_PORT_USB_ENDPOINT_IN : |
| 210 |
internal_ep = port->settings.usb.inep; |
| 211 |
break; |
| 212 |
case GP_PORT_USB_ENDPOINT_OUT : |
| 213 |
internal_ep = port->settings.usb.outep; |
| 214 |
break; |
| 215 |
case GP_PORT_USB_ENDPOINT_INT : |
| 216 |
internal_ep = port->settings.usb.intep; |
| 217 |
break; |
| 218 |
default: |
| 219 |
gp_port_set_error (port, "Bad EndPoint argument 0x%x", ep); |
| 220 |
return GP_ERROR_BAD_PARAMETERS; |
| 221 |
} |
| 222 |
gp_log(GP_LOG_DEBUG,"gp_port_vusb_clear_halt_lib","clearing halt on ep 0x%x", internal_ep); |
| 223 |
/* now clear halt */ |
| 224 |
return GP_OK; |
| 225 |
} |
| 226 |
|
| 227 |
/* The next two functions support the nonstandard request types 0x41 (write) |
| 228 |
* and 0xc1 (read), which are occasionally needed. |
| 229 |
*/ |
| 230 |
static int |
| 231 |
gp_port_vusb_msg_interface_write_lib(GPPort *port, int request, |
| 232 |
int value, int index, char *bytes, int size) |
| 233 |
{ |
| 234 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(request=%d, value=%d, index=%d, size=%d)", request, value, index, size); |
| 235 |
return GP_OK; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
static int |
| 240 |
gp_port_vusb_msg_interface_read_lib(GPPort *port, int request, |
| 241 |
int value, int index, char *bytes, int size) |
| 242 |
{ |
| 243 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(req=%x, value=%x, index=%d, size=%d)", request, value, index, size); |
| 244 |
return port->pl->vcamera->read(port->pl->vcamera, 0x81, (unsigned char*)bytes, size); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/* The next two functions support the nonstandard request types 0x21 (write) |
| 249 |
* and 0xa1 (read), which are occasionally needed. |
| 250 |
*/ |
| 251 |
static int |
| 252 |
gp_port_vusb_msg_class_write_lib(GPPort *port, int request, |
| 253 |
int value, int index, char *bytes, int size) |
| 254 |
{ |
| 255 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(request=%d, value=%d, index=%d, size=%d)", request, value, index, size); |
| 256 |
return GP_OK; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static int |
| 262 |
gp_port_vusb_msg_class_read_lib(GPPort *port, int request, |
| 263 |
int value, int index, char *bytes, int size) |
| 264 |
{ |
| 265 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(request=%d, value=%d, index=%d, size=%d)", request, value, index, size); |
| 266 |
return GP_OK; /* or bytes */ |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static int |
| 271 |
gp_port_vusb_msg_write_lib(GPPort *port, int request, int value, int index, |
| 272 |
char *bytes, int size) |
| 273 |
{ |
| 274 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(request=%d, value=%d, index=%d, size=%d)", request, value, index, size); |
| 275 |
return GP_OK; |
| 276 |
} |
| 277 |
|
| 278 |
static int |
| 279 |
gp_port_vusb_msg_read_lib(GPPort *port, int request, int value, int index, |
| 280 |
char *bytes, int size) |
| 281 |
{ |
| 282 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(req=%x, value=%x, index=%x, size=%d)", request, value, index, size); |
| 283 |
return port->pl->vcamera->read(port->pl->vcamera, index, (unsigned char*)bytes, size); |
| 284 |
} |
| 285 |
|
| 286 |
static int |
| 287 |
gp_port_vusb_find_device_lib(GPPort *port, int idvendor, int idproduct) |
| 288 |
{ |
| 289 |
#ifdef FUZZ_PTP |
| 290 |
if ((idvendor == 0x04b0) && (idproduct == 0x0437)) { /* Nikon D750 */ |
| 291 |
#else |
| 292 |
GPPortInfo info; |
| 293 |
char *path, *s; |
| 294 |
static unsigned short vendor = 0x04b0, product = 0x0437; /* defaults are the Nikon D750 */ |
| 295 |
int fd; |
| 296 |
|
| 297 |
static char *lastpath = NULL; |
| 298 |
|
| 299 |
gp_port_get_info (port, &info); |
| 300 |
gp_port_info_get_path (info, &path); |
| 301 |
|
| 302 |
if (!lastpath || strcmp(path, lastpath)) { |
| 303 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(path=%s)", path); |
| 304 |
if (lastpath) { |
| 305 |
free(lastpath); |
| 306 |
} |
| 307 |
lastpath = strdup(path); |
| 308 |
|
| 309 |
s = strchr(path, ':')+1; |
| 310 |
fd = open(s, O_RDONLY); |
| 311 |
/*vendor = product = 0; */ |
| 312 |
if (fd != -1) { |
| 313 |
if (-1 == read( fd, &vendor, 2)) |
| 314 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"could not read vendor"); |
| 315 |
if (-1 == read( fd, &product, 2)) |
| 316 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"could not read product"); |
| 317 |
close(fd); |
| 318 |
} |
| 319 |
} |
| 320 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(using vendor 0x%04x,0x%04x)", vendor, product); |
| 321 |
|
| 322 |
if ((idvendor == vendor) && (idproduct == product)) { |
| 323 |
#endif |
| 324 |
port->settings.usb.config = 1; |
| 325 |
port->settings.usb.interface = 1; |
| 326 |
port->settings.usb.altsetting = 1; |
| 327 |
|
| 328 |
port->settings.usb.inep = 0x81; |
| 329 |
port->settings.usb.outep = 0x02; |
| 330 |
port->settings.usb.intep = 0x83; |
| 331 |
port->settings.usb.maxpacketsize = 512; |
| 332 |
return GP_OK; |
| 333 |
} |
| 334 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(0x%04x,0x%04x)", idvendor, idproduct); |
| 335 |
return GP_ERROR_IO_USB_FIND; |
| 336 |
} |
| 337 |
|
| 338 |
static int |
| 339 |
gp_port_vusb_find_device_by_class_lib(GPPort *port, int class, int subclass, int protocol) |
| 340 |
{ |
| 341 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"(0x%02x,0x%02x,0x%02x)", class, subclass, protocol); |
| 342 |
|
| 343 |
#ifdef FUZZ_PTP |
| 344 |
if ((class == 6) && (subclass == 1) && (protocol == 1)) { |
| 345 |
port->settings.usb.config = 1; |
| 346 |
port->settings.usb.interface = 1; |
| 347 |
port->settings.usb.altsetting = 1; |
| 348 |
|
| 349 |
port->settings.usb.inep = 0x81; |
| 350 |
port->settings.usb.outep = 0x02; |
| 351 |
port->settings.usb.intep = 0x83; |
| 352 |
port->settings.usb.maxpacketsize = 512; |
| 353 |
return GP_OK; |
| 354 |
} |
| 355 |
#endif |
| 356 |
return GP_ERROR_IO_USB_FIND; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
GPPortOperations * |
| 362 |
gp_port_library_operations (void) |
| 363 |
{ |
| 364 |
GPPortOperations *ops; |
| 365 |
|
| 366 |
gp_log(GP_LOG_DEBUG,__FUNCTION__,"()"); |
| 367 |
ops = calloc (1, sizeof (GPPortOperations)); |
| 368 |
if (!ops) |
| 369 |
return NULL; |
| 370 |
|
| 371 |
ops->init = gp_port_vusb_init; |
| 372 |
ops->exit = gp_port_vusb_exit; |
| 373 |
ops->open = gp_port_vusb_open; |
| 374 |
ops->close = gp_port_vusb_close; |
| 375 |
ops->read = gp_port_vusb_read; |
| 376 |
ops->write = gp_port_vusb_write; |
| 377 |
ops->reset = gp_port_vusb_reset; |
| 378 |
|
| 379 |
ops->check_int = gp_port_vusb_check_int; |
| 380 |
ops->update = gp_port_vusb_update; |
| 381 |
ops->clear_halt = gp_port_vusb_clear_halt_lib; |
| 382 |
ops->msg_write = gp_port_vusb_msg_write_lib; |
| 383 |
ops->msg_read = gp_port_vusb_msg_read_lib; |
| 384 |
|
| 385 |
ops->msg_interface_write = gp_port_vusb_msg_interface_write_lib; |
| 386 |
ops->msg_interface_read = gp_port_vusb_msg_interface_read_lib; |
| 387 |
ops->msg_class_write = gp_port_vusb_msg_class_write_lib; |
| 388 |
ops->msg_class_read = gp_port_vusb_msg_class_read_lib; |
| 389 |
ops->send_scsi_cmd = gp_port_vusb_send_scsi_cmd; |
| 390 |
ops->seek = gp_port_vusb_seek; |
| 391 |
|
| 392 |
ops->find_device = gp_port_vusb_find_device_lib; |
| 393 |
ops->find_device_by_class = gp_port_vusb_find_device_by_class_lib; |
| 394 |
return ops; |
| 395 |
} |