summaryrefslogtreecommitdiff
path: root/libgphoto2_port/usbscsi/linux.c
blob: 80223bc76c1cb977c79988a431511b2e6d4449a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* SCSI commands to USB Mass storage devices port library for Linux
 * 
 *   Copyright (c) 2010-2012 Hans de Goede <hdegoede@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _BSD_SOURCE	/* for flock */

#include "config.h"
#include <gphoto2/gphoto2-port-library.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_SCSI_SG_H
# include <scsi/sg.h>
#endif

#include <gphoto2/gphoto2-port-result.h>
#include <gphoto2/gphoto2-port-log.h>
#include <gphoto2/gphoto2-port.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (GETTEXT_PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif 
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

#define CHECK(result) {int r=(result); if (r<0) return (r);}

struct _GPPortPrivateLibrary {
	int fd;       /* Device handle */
};

GPPortType
gp_port_library_type () 
{
	return GP_PORT_USB_SCSI;
}

static int
gp_port_usbscsi_lock (GPPort *port)
{
#if HAVE_FLOCK
	gp_log (GP_LOG_DEBUG, "gphoto2-port-usbscsi",
		"Trying to lock '%s'...", port->settings.usbscsi.path);

	if (flock(port->pl->fd, LOCK_EX | LOCK_NB) != 0) {
		switch (errno) {
		case EWOULDBLOCK:
			gp_port_set_error (port,
				_("Device '%s' is locked by another app."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO_LOCK;
		default:
			gp_port_set_error (port,
				_("Failed to lock '%s' (%m)."),
				port->settings.usbscsi.path);
			return GP_ERROR_IO;
		}
	}
#else
	gp_log (GP_LOG_DEBUG, "gphoto2-port-usbscsi",
		"Locking '%s' not possible, flock not availbale.", port->settings.usbscsi.path);
#endif
	return GP_OK;
}

static int
gp_port_usbscsi_unlock (GPPort *port)
{
#ifdef HAVE_FLOCK
	if (flock(port->pl->fd, LOCK_UN) != 0) {
		gp_port_set_error (port, _("Failed to unlock '%s' (%m)."),
				   port->settings.usbscsi.path);
		return GP_ERROR_IO;
	}
#endif
	return GP_OK;
}

static const char *
gp_port_usbscsi_resolve_symlink (const char *link)
{
	ssize_t ret;
	static char path[PATH_MAX + 1];
	char *slash, buf[PATH_MAX + 1];
	struct stat st;
	int len;

	snprintf (path, sizeof(path), "%s", link);

	do {
		ret = readlink (path, buf, PATH_MAX);
		if (ret < 0)
			return NULL;
		buf[ret] = 0;

		slash = strrchr (path, '/');
		if (buf[0] == '/' || slash == NULL) {
			snprintf (path, sizeof(path), "%s", buf);
		} else {
			*slash = 0;
			len = strlen (path);
			snprintf (path + len, sizeof (path) - len, "/%s", buf);
		}

		if (stat (path, &st))
			return NULL;
	} while (S_ISLNK(st.st_mode));

	return path;
}

static int 
gp_port_usbscsi_get_usb_id (const char *sg,
	unsigned short *vendor_id, unsigned short *product_id)
{
	FILE *f;
	char c, *s, buf[32], path[PATH_MAX + 1];
	char *xpath;

	snprintf (path, sizeof (path), "/sys/class/scsi_generic/%s", sg);
	xpath = gp_port_usbscsi_resolve_symlink(path);
	if (xpath) {
		snprintf (path, sizeof (path), "%s/../../../../../modalias",
			  gp_port_usbscsi_resolve_symlink(path));
	} else {
		/* older kernels, perhaps also works on newer ones */
		snprintf (path, sizeof (path), "/sys/class/scsi_generic/%s/device/../../../modalias", sg);
	}

	f = fopen (path, "r");
	if (!f)
		return GP_ERROR_IO_SUPPORTED_USB;

	s = fgets (buf, sizeof(buf), f);
	fclose (f);

	if (!s)
		return GP_ERROR_IO_SUPPORTED_USB;

	if (sscanf (s, "usb:v%4hxp%4hx%c", vendor_id, product_id, &c) != 3 ||
	    c != 'd')
		return GP_ERROR_IO_SUPPORTED_USB;

	return GP_OK;
}

int
gp_port_library_list (GPPortInfoList *list)
{
	DIR *dir;
	struct dirent *dirent;
	GPPortInfo info;
	unsigned short vendor_id, product_id;

	dir = opendir ("/sys/class/scsi_generic");
	if (dir == NULL)
		return GP_OK;

	while ((dirent = readdir (dir))) {
		char path[4096];
		if (gp_port_usbscsi_get_usb_id (dirent->d_name,
				&vendor_id, &product_id) != GP_OK)
			continue; /* Not a usb device */

		gp_port_info_new (&info);
		gp_port_info_set_type (info, GP_PORT_USB_SCSI);
		snprintf (path, sizeof (path),
			  "usbscsi:/dev/%s",
			  dirent->d_name);
		gp_port_info_set_path (info, path);
		gp_port_info_set_name (info, _("USB Mass Storage raw SCSI"));
		CHECK (gp_port_info_list_append (list, info))
	}
	closedir (dir);
	return GP_OK;
}

static int
gp_port_usbscsi_init (GPPort *port)
{
	port->pl = calloc (1, sizeof (GPPortPrivateLibrary));
	if (!port->pl)
		return GP_ERROR_NO_MEMORY;

	port->pl->fd = -1;

	return GP_OK;
}

static int
gp_port_usbscsi_exit (GPPort *port)
{
	if (!port)
		return GP_ERROR_BAD_PARAMETERS;

	if (port->pl) {
		free (port->pl);
		port->pl = NULL;
	}

	return GP_OK;
}

static int
gp_port_usbscsi_open (GPPort *port)
{
	int result, i;
	const int max_tries = 5;
	const char *path = port->settings.usbscsi.path;

	port->pl->fd = open (path, O_RDWR);
	if (port->pl->fd == -1) {
		gp_port_set_error (port, _("Failed to open '%s' (%m)."), path);
		return GP_ERROR_IO;
	}

	result = gp_port_usbscsi_lock (port);
	for (i = 0; i < max_tries && result == GP_ERROR_IO_LOCK; i++) {
		gp_log (GP_LOG_DEBUG, "gphoto2-port-usbscsi",
			"Failed to get a lock, trying again...");
		sleep (1);
		result = gp_port_usbscsi_lock (port);
	}
	if (result != GP_OK) {
		close (port->pl->fd);
		port->pl->fd = -1;
	}
	return result;
}

static int
gp_port_usbscsi_close (GPPort *port)
{
	int result;

	if (!port || port->pl->fd == -1)
		return GP_OK;

	result = gp_port_usbscsi_unlock (port);

	if (close (port->pl->fd) == -1) {
		gp_port_set_error (port, _("Could not close "
			"'%s' (%m)."), port->settings.usbscsi.path);
		return GP_ERROR_IO;
	}
	port->pl->fd = -1;

	return result;
}

static int gp_port_usbscsi_send_scsi_cmd (GPPort *port, int to_dev, char *cmd,
	int cmd_size, char *sense, int sense_size, char *data, int data_size)
{
#ifdef HAVE_SCSI_SG_H
	sg_io_hdr_t io_hdr;

	if (!port)
		return GP_ERROR_BAD_PARAMETERS;

	/* The device needs to be opened for that operation */
	if (port->pl->fd == -1)
		CHECK (gp_port_usbscsi_open (port))

	memset(sense, 0, sense_size);
	memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
	if (to_dev) {
		io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
	} else {
		memset (data, 0, data_size);
		io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
	}
	io_hdr.interface_id = 'S';
	io_hdr.cmdp = (unsigned char *)cmd;
	io_hdr.cmd_len = cmd_size;
	io_hdr.sbp = (unsigned char *)sense;
	io_hdr.mx_sb_len = sense_size;
	io_hdr.dxferp = (unsigned char *)data;
	io_hdr.dxfer_len = data_size;
	/*io_hdr.timeout = 1500;*/
	io_hdr.timeout = port->timeout;
	gp_log (GP_LOG_DEBUG, "port/usbscsi", "setting scsi command timeout to %d", port->timeout);
	if (io_hdr.timeout < 1500)
		io_hdr.timeout = 1500;

	if (ioctl (port->pl->fd, SG_IO, &io_hdr) < 0)
	{
		gp_port_set_error (port, _("Could not send scsi command to: "
			"'%s' (%m)."), port->settings.usbscsi.path);
		return GP_ERROR_IO;
	}
	return GP_OK;
#else
	return GP_ERROR_NOT_SUPPORTED;
#endif
}

static int
gp_port_usbscsi_update (GPPort *port)
{
	if (!port)
		return GP_ERROR_BAD_PARAMETERS;

	memcpy (&port->settings, &port->settings_pending,
		sizeof (port->settings));

	return GP_OK;
}

static int
gp_port_usbscsi_find_device(GPPort *port, int idvendor, int idproduct)
{
	unsigned short vendor_id, product_id;
	const char *sg;

	if (!port)
		return GP_ERROR_BAD_PARAMETERS;

	sg = strrchr (port->settings.usbscsi.path, '/');
	if (!sg)
		return GP_ERROR_BAD_PARAMETERS;
	sg++;

	CHECK (gp_port_usbscsi_get_usb_id (sg, &vendor_id, &product_id))
	if (vendor_id != idvendor || product_id != idproduct)
		return GP_ERROR_IO_USB_FIND;

	return GP_OK;
}

GPPortOperations *
gp_port_library_operations ()
{
	GPPortOperations *ops;

	ops = malloc (sizeof (GPPortOperations));
	if (!ops)
		return (NULL);
	memset (ops, 0, sizeof (GPPortOperations)); 

	ops->init   = gp_port_usbscsi_init;
	ops->exit   = gp_port_usbscsi_exit;
	ops->open   = gp_port_usbscsi_open;
	ops->close  = gp_port_usbscsi_close;
	ops->send_scsi_cmd = gp_port_usbscsi_send_scsi_cmd;
	ops->update = gp_port_usbscsi_update;
	ops->find_device = gp_port_usbscsi_find_device;

	return (ops);
}