summaryrefslogtreecommitdiff
path: root/util/mtp-probe.c
blob: 2bb899541d79ffe38259b532fbadc4518ff846f2 (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
/**
 * \file mtp-probe.c
 * Program to probe newly connected device interfaces from
 * userspace to determine if they are MTP devices, used for
 * udev rules.
 *
 * Invoke the program from udev to check it for MTP signatures,
 * e.g.
 * ATTR{bDeviceClass}=="ff",
 * PROGRAM="<path>/mtp-probe /sys$env{DEVPATH} $attr{busnum} $attr{devnum}",
 * RESULT=="1", ENV{ID_MTP_DEVICE}="1", ENV{ID_MEDIA_PLAYER}="1",
 * SYMLINK+="libmtp-%k", MODE="666"
 *
 * Is you issue this before testing your /var/log/messages
 * will be more verbose:
 *
 * udevadm control --log-priority=debug
 *
 * Exits with status code 1 if the device is an MTP device,
 * else exits with 0.
 *
 * Copyright (C) 2011-2012 Linus Walleij <triad@df.lth.se>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef __linux__
#error "This program should only be compiled for Linux!"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libmtp.h>
#include <regex.h>
#include <fcntl.h>

enum ep_type {
  OTHER_EP,
  BULK_OUT_EP,
  BULK_IN_EP,
  INTERRUPT_IN_EP,
  INTERRUPT_OUT_EP,
};

static enum ep_type get_ep_type(char *path)
{
  char pbuf[FILENAME_MAX];
  int len = strlen(path);
  int fd;
  char buf[128];
  int bread;
  int is_out = 0;
  int is_in = 0;
  int is_bulk = 0;
  int is_interrupt = 0;
  int i;

  strcpy(pbuf, path);
  pbuf[len++] = '/';

  /* Check the type */
  strncpy(pbuf + len, "type", FILENAME_MAX - len);
  pbuf[FILENAME_MAX - 1] = '\0'; /* Sentinel */

  fd = open(pbuf, O_RDONLY);
  if (fd < 0)
    return OTHER_EP;
  bread = read(fd, buf, sizeof(buf));
  close(fd);
  if (bread < 2)
    return OTHER_EP;

  for (i = 0; i < bread; i++)
    if(buf[i] == 0x0d || buf[i] == 0x0a)
      buf[i] = '\0';

  if (!strcmp(buf, "Bulk"))
    is_bulk = 1;
  if (!strcmp(buf, "Interrupt"))
    is_interrupt = 1;

  /* Check the direction */
  strncpy(pbuf + len, "direction", FILENAME_MAX - len);
  pbuf[FILENAME_MAX - 1] = '\0'; /* Sentinel */

  fd = open(pbuf, O_RDONLY);
  if (fd < 0)
    return OTHER_EP;
  bread = read(fd, buf, sizeof(buf));
  close(fd);
  if (bread < 2)
    return OTHER_EP;

  for (i = 0; i < bread; i++)
    if(buf[i] == 0x0d || buf[i] == 0x0a)
      buf[i] = '\0';

  if (!strcmp(buf, "in"))
    is_in = 1;
  if (!strcmp(buf, "out"))
    is_out = 1;

  if (is_bulk && is_in)
    return BULK_IN_EP;
  if (is_bulk && is_out)
    return BULK_OUT_EP;
  if (is_interrupt && is_in)
    return INTERRUPT_IN_EP;
  if (is_interrupt && is_out)
    return INTERRUPT_OUT_EP;

  return OTHER_EP;
}

static int has_3_ep(char *path)
{
  char pbuf[FILENAME_MAX];
  int len = strlen(path);
  int fd;
  char buf[128];
  int bread;

  strcpy(pbuf, path);
  pbuf[len++] = '/';
  strncpy(pbuf + len, "bNumEndpoints", FILENAME_MAX - len);
  pbuf[FILENAME_MAX - 1] = '\0'; /* Sentinel */

  fd = open(pbuf, O_RDONLY);
  if (fd < 0)
    return -1;
  /* Read all contents to buffer */
  bread = read(fd, buf, sizeof(buf));
  close(fd);
  if (bread < 2)
    return 0;

  /* 0x30, 0x33 = "03", maybe we should parse it? */
  if (buf[0] == 0x30 && buf[1] == 0x33)
    return 1;

  return 0;
}

static int check_interface(char *sysfspath)
{
  char dirbuf[FILENAME_MAX];
  int len = strlen(sysfspath);
  DIR *dir;
  struct dirent *dent;
  regex_t r;
  int ret;
  int bulk_out_ep_found = 0;
  int bulk_in_ep_found = 0;
  int interrupt_in_ep_found = 0;

  ret = has_3_ep(sysfspath);
  if (ret <= 0)
    return ret;

  /* Yes it has three endpoints ... look even closer! */
  dir = opendir(sysfspath);
  if (!dir)
    return -1;

  strcpy(dirbuf, sysfspath);
  dirbuf[len++] = '/';

  /* Check for dirs that identify endpoints */
  ret = regcomp(&r, "^ep_[0-9a-f]+$", REG_EXTENDED | REG_NOSUB);
  if (ret)
    return -1;

  while ((dent = readdir(dir))) {
    struct stat st;

    /* No need to check those beginning with a period */
    if (dent->d_name[0] == '.')
      continue;

    strncpy(dirbuf + len, dent->d_name, FILENAME_MAX - len);
    dirbuf[FILENAME_MAX - 1] = '\0'; /* Sentinel */
    ret = lstat(dirbuf, &st);
    if (ret)
      continue;
    if (S_ISDIR(st.st_mode) && !regexec(&r, dent->d_name, 0, 0, 0)) {
      enum ep_type ept;

      ept = get_ep_type(dirbuf);
      if (ept == BULK_OUT_EP)
	bulk_out_ep_found = 1;
      else if (ept == BULK_IN_EP)
	bulk_in_ep_found = 1;
      else if (ept == INTERRUPT_IN_EP)
	interrupt_in_ep_found = 1;
    }
  }

  regfree(&r);
  closedir(dir);

  /*
   * If this is fulfilled the interface is an MTP candidate
   */
  if (bulk_out_ep_found &&
      bulk_in_ep_found &&
      interrupt_in_ep_found) {
    return 1;
  }

  return 0;
}

static int check_sysfs(char *sysfspath)
{
  char dirbuf[FILENAME_MAX];
  int len = strlen(sysfspath);
  DIR *dir;
  struct dirent *dent;
  regex_t r;
  int ret;
  int look_closer = 0;

  dir = opendir(sysfspath);
  if (!dir)
    return -1;

  strcpy(dirbuf, sysfspath);
  dirbuf[len++] = '/';

  /* Check for dirs that identify interfaces */
  ret = regcomp(&r, "^[0-9]+-[0-9]+\\:[0-9]+\\.[0-9]+$", REG_EXTENDED | REG_NOSUB);
  if (ret)
    return -1;

  while ((dent = readdir(dir))) {
    struct stat st;
    int ret;

    /* No need to check those beginning with a period */
    if (dent->d_name[0] == '.')
      continue;

    strncpy(dirbuf + len, dent->d_name, FILENAME_MAX - len);
    dirbuf[FILENAME_MAX - 1] = '\0'; /* Sentinel */
    ret = lstat(dirbuf, &st);
    if (ret)
      continue;

    /* Look closer at dirs that may be interfaces */
    if (S_ISDIR(st.st_mode)) {
      if (!regexec(&r, dent->d_name, 0, 0, 0))
      if (check_interface(dirbuf) > 0)
	/* potential MTP interface! */
	look_closer = 1;
    }
  }

  regfree(&r);
  closedir(dir);
  return look_closer;
}

int main (int argc, char **argv)
{
  char *fname;
  int busno;
  int devno;
  int ret;

  if (argc < 4) {
    syslog(LOG_INFO, "need device path, busnumber, device number as argument\n");
    printf("0");
    exit(0);
  }

  fname = argv[1];
  busno = atoi(argv[2]);
  devno = atoi(argv[3]);

  syslog(LOG_INFO, "checking bus %d, device %d: \"%s\"\n", busno, devno, fname);

  ret = check_sysfs(fname);
  /*
   * This means that regular directory check either agrees that this may be a
   * MTP device, or that it doesn't know (failed). In that case, kick the deeper
   * check inside LIBMTP.
   */
  if (ret != 0)
    ret = LIBMTP_Check_Specific_Device(busno, devno);
  if (ret) {
    syslog(LOG_INFO, "bus: %d, device: %d was an MTP device\n", busno, devno);
    printf("1");
  } else {
    syslog(LOG_INFO, "bus: %d, device: %d was not an MTP device\n", busno, devno);
    printf("0");
  }

  exit(0);
}