Debian Codegraph @

libgphoto2_port/usbscsi/linux.c

raw
1 /* SCSI commands to USB Mass storage devices port library for Linux
2 *
3 * Copyright (c) 2010-2012 Hans de Goede <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20
21 #define _DEFAULT_SOURCE /* for flock */
22
23 #include "config.h"
24 #include <gphoto2/gphoto2-port-library.h>
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_FCNTL_H
31 # include <fcntl.h>
32 #endif
33 #ifdef HAVE_LIMITS_H
34 # include <limits.h>
35 #endif
36 #ifdef HAVE_SYS_FILE_H
37 # include <sys/file.h>
38 # ifndef LOCK_SH
39 # define LOCK_SH 1
40 # endif
41 # ifndef LOCK_EX
42 # define LOCK_EX 2
43 # endif
44 # ifndef LOCK_NB
45 # define LOCK_NB 4
46 # endif
47 # ifndef LOCK_UN
48 # define LOCK_UN 4
49 # endif
50 #endif
51 #include <sys/stat.h>
52 #include <sys/types.h>
53 #ifdef HAVE_SYS_PARAM_H
54 # include <sys/param.h>
55 #endif
56 #ifdef HAVE_SYS_IOCTL_H
57 # include <sys/ioctl.h>
58 #endif
59 #ifdef HAVE_SCSI_SG_H
60 # include <scsi/sg.h>
61 #endif
62
63 #include <gphoto2/gphoto2-port-result.h>
64 #include <gphoto2/gphoto2-port-log.h>
65 #include <gphoto2/gphoto2-port.h>
66
67 #include "libgphoto2_port/i18n.h"
68
69
70 #define CHECK(result) {int r=(result); if (r<0) return (r);}
71
72 struct _GPPortPrivateLibrary {
73 int fd; /* Device handle */
74 };
75
76 GPPortType
77 gp_port_library_type ()
78 {
79 return GP_PORT_USB_SCSI;
80 }
81
82 static int
83 gp_port_usbscsi_lock (GPPort *port)
84 {
85 #if HAVE_FLOCK
86 GP_LOG_D ("Trying to lock '%s'...", port->settings.usbscsi.path);
87
88 if (flock(port->pl->fd, LOCK_EX | LOCK_NB) != 0) {
89 switch (errno) {
90 case EWOULDBLOCK:
91 gp_port_set_error (port,
92 _("Device '%s' is locked by another app."),
93 port->settings.usbscsi.path);
94 return GP_ERROR_IO_LOCK;
95 default:
96 gp_port_set_error (port,
97 _("Failed to lock '%s' (%m)."),
98 port->settings.usbscsi.path);
99 return GP_ERROR_IO;
100 }
101 }
102 #else
103 GP_LOG_D ("Locking '%s' not possible, flock not available.", port->settings.usbscsi.path);
104 #endif
105 return GP_OK;
106 }
107
108 static int
109 gp_port_usbscsi_unlock (GPPort *port)
110 {
111 #ifdef HAVE_FLOCK
112 if (flock(port->pl->fd, LOCK_UN) != 0) {
113 gp_port_set_error (port, _("Failed to unlock '%s' (%m)."),
114 port->settings.usbscsi.path);
115 return GP_ERROR_IO;
116 }
117 #endif
118 return GP_OK;
119 }
120
121 static const char *
122 gp_port_usbscsi_resolve_symlink (const char *link)
123 {
124 ssize_t ret;
125 static char path[PATH_MAX + 1];
126 char *slash, buf[PATH_MAX + 1];
127 struct stat st;
128 int len;
129
130 snprintf (path, sizeof(path), "%s", link);
131
132 do {
133 ret = readlink (path, buf, PATH_MAX);
134 if (ret < 0)
135 return NULL;
136 buf[ret] = 0;
137
138 slash = strrchr (path, '/');
139 if (buf[0] == '/' || slash == NULL) {
140 snprintf (path, sizeof(path), "%s", buf);
141 } else {
142 *slash = 0;
143 len = strlen (path);
144 if (snprintf (path + len, sizeof (path) - len, "/%s", buf) >= (int)sizeof (path) - len)
145 return NULL;
146 }
147
148 if (stat (path, &st))
149 return NULL;
150 } while (S_ISLNK(st.st_mode));
151
152 return path;
153 }
154
155 static int
156 gp_port_usbscsi_get_usb_id (const char *sg,
157 unsigned short *vendor_id, unsigned short *product_id)
158 {
159 FILE *f;
160 char c, *s, buf[32], path[PATH_MAX + 1];
161 const char *xpath;
162
163 snprintf (path, sizeof (path), "/sys/class/scsi_generic/%s", sg);
164 xpath = gp_port_usbscsi_resolve_symlink(path);
165 if (xpath) {
166 snprintf (path, sizeof (path), "%s/../../../../../modalias",
167 gp_port_usbscsi_resolve_symlink(path));
168 } else {
169 /* older kernels, perhaps also works on newer ones */
170 snprintf (path, sizeof (path), "/sys/class/scsi_generic/%s/device/../../../modalias", sg);
171 }
172
173 f = fopen (path, "r");
174 if (!f)
175 return GP_ERROR_IO_SUPPORTED_USB;
176
177 s = fgets (buf, sizeof(buf), f);
178 fclose (f);
179
180 if (!s)
181 return GP_ERROR_IO_SUPPORTED_USB;
182
183 if (sscanf (s, "usb:v%4hxp%4hx%c", vendor_id, product_id, &c) != 3 ||
184 c != 'd')
185 return GP_ERROR_IO_SUPPORTED_USB;
186
187 return GP_OK;
188 }
189
190 int
191 gp_port_library_list (GPPortInfoList *list)
192 {
193 gp_system_dir dir;
194 gp_system_dirent dirent;
195 int ret;
196 GPPortInfo info;
197 unsigned short vendor_id, product_id;
198
199 dir = gp_system_opendir ("/sys/class/scsi_generic");
200 if (dir == NULL)
201 return GP_OK;
202
203 while ((dirent = gp_system_readdir (dir))) {
204 char path[4096];
205 if (gp_port_usbscsi_get_usb_id (dirent->d_name,
206 &vendor_id, &product_id) != GP_OK)
207 continue; /* Not a usb device */
208
209 gp_port_info_new (&info);
210 gp_port_info_set_type (info, GP_PORT_USB_SCSI);
211 snprintf (path, sizeof (path),
212 "usbscsi:/dev/%s",
213 dirent->d_name);
214 gp_port_info_set_path (info, path);
215 gp_port_info_set_name (info, _("USB Mass Storage raw SCSI"));
216 ret = gp_port_info_list_append (list, info);
217 if (ret < GP_OK) /* can only be out of memory */
218 break;
219 }
220 gp_system_closedir (dir);
221 return GP_OK;
222 }
223
224 static int
225 gp_port_usbscsi_init (GPPort *port)
226 {
227 C_MEM (port->pl = calloc (1, sizeof (GPPortPrivateLibrary)));
228
229 port->pl->fd = -1;
230
231 return GP_OK;
232 }
233
234 static int
235 gp_port_usbscsi_exit (GPPort *port)
236 {
237 C_PARAMS (port);
238
239 free (port->pl);
240 port->pl = NULL;
241
242 return GP_OK;
243 }
244
245 static int
246 gp_port_usbscsi_open (GPPort *port)
247 {
248 int result, i;
249 const int max_tries = 5;
250 const char *path = port->settings.usbscsi.path;
251
252 port->pl->fd = open (path, O_RDWR);
253 if (port->pl->fd == -1) {
254 gp_port_set_error (port, _("Failed to open '%s' (%m)."), path);
255 return GP_ERROR_IO;
256 }
257
258 result = gp_port_usbscsi_lock (port);
259 for (i = 0; i < max_tries && result == GP_ERROR_IO_LOCK; i++) {
260 GP_LOG_D ("Failed to get a lock, trying again...");
261 sleep (1);
262 result = gp_port_usbscsi_lock (port);
263 }
264 if (result != GP_OK) {
265 close (port->pl->fd);
266 port->pl->fd = -1;
267 }
268 return result;
269 }
270
271 static int
272 gp_port_usbscsi_close (GPPort *port)
273 {
274 int result;
275
276 if (!port || port->pl->fd == -1)
277 return GP_OK;
278
279 result = gp_port_usbscsi_unlock (port);
280
281 if (close (port->pl->fd) == -1) {
282 gp_port_set_error (port, _("Could not close "
283 "'%s' (%m)."), port->settings.usbscsi.path);
284 return GP_ERROR_IO;
285 }
286 port->pl->fd = -1;
287
288 return result;
289 }
290
291 static int gp_port_usbscsi_send_scsi_cmd (GPPort *port, int to_dev, char *cmd,
292 int cmd_size, char *sense, int sense_size, char *data, int data_size)
293 {
294 #ifdef HAVE_SCSI_SG_H
295 sg_io_hdr_t io_hdr;
296
297 C_PARAMS (port);
298
299 /* The device needs to be opened for that operation */
300 if (port->pl->fd == -1)
301 CHECK (gp_port_usbscsi_open (port))
302
303 memset(sense, 0, sense_size);
304 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
305 if (to_dev) {
306 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
307 } else {
308 memset (data, 0, data_size);
309 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
310 }
311 io_hdr.interface_id = 'S';
312 io_hdr.cmdp = (unsigned char *)cmd;
313 io_hdr.cmd_len = cmd_size;
314 io_hdr.sbp = (unsigned char *)sense;
315 io_hdr.mx_sb_len = sense_size;
316 io_hdr.dxferp = (unsigned char *)data;
317 io_hdr.dxfer_len = data_size;
318 /*io_hdr.timeout = 1500;*/
319 io_hdr.timeout = port->timeout;
320 GP_LOG_D ("setting scsi command timeout to %d", port->timeout);
321 if (io_hdr.timeout < 1500)
322 io_hdr.timeout = 1500;
323
324 if (ioctl (port->pl->fd, SG_IO, &io_hdr) < 0)
325 {
326 gp_port_set_error (port, _("Could not send scsi command to: "
327 "'%s' (%m)."), port->settings.usbscsi.path);
328 return GP_ERROR_IO;
329 }
330 return GP_OK;
331 #else
332 return GP_ERROR_NOT_SUPPORTED;
333 #endif
334 }
335
336 static int
337 gp_port_usbscsi_update (GPPort *port)
338 {
339 C_PARAMS (port);
340
341 memcpy (&port->settings, &port->settings_pending,
342 sizeof (port->settings));
343
344 return GP_OK;
345 }
346
347 static int
348 gp_port_usbscsi_find_device(GPPort *port, int idvendor, int idproduct)
349 {
350 unsigned short vendor_id, product_id;
351 const char *sg;
352
353 C_PARAMS (port);
354
355 sg = strrchr (port->settings.usbscsi.path, '/');
356 C_PARAMS (sg);
357 sg++;
358
359 CHECK (gp_port_usbscsi_get_usb_id (sg, &vendor_id, &product_id))
360 if (vendor_id != idvendor || product_id != idproduct)
361 return GP_ERROR_IO_USB_FIND;
362
363 return GP_OK;
364 }
365
366 GPPortOperations *
367 gp_port_library_operations ()
368 {
369 GPPortOperations *ops;
370
371 ops = calloc (1, sizeof (GPPortOperations));
372 if (!ops)
373 return (NULL);
374
375 ops->init = gp_port_usbscsi_init;
376 ops->exit = gp_port_usbscsi_exit;
377 ops->open = gp_port_usbscsi_open;
378 ops->close = gp_port_usbscsi_close;
379 ops->send_scsi_cmd = gp_port_usbscsi_send_scsi_cmd;
380 ops->update = gp_port_usbscsi_update;
381 ops->find_device = gp_port_usbscsi_find_device;
382
383 return (ops);
384 }