r5u87x / loader.c

  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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * Ricoh R5U87x series USB firmware loader.
 * Copyright (c) 2008-2009 Alexander Hixon <alex@alexhixon.com>
 * 
 * Loading routines based off those used in the original r5u870 kernel
 * driver, written by Sam Revitch <samr7@cs.washington.edu>. See README
 * for additional credits.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA
 */

#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <usb.h>

#include "loader.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define R5U870_REG_VFLIP_EX	0x30
#define R5U870_REG_HFLIP_EX	0x31

static gchar    *firmware       = "ucode/r5u87x-%vid%-%pid%.fw";	// relative path to firmware files
                                                                    // if files are not found here, loader will look
                                                                    // in UCODE_PATH for files - defined in Makefile
                                                                    // and config.h
static gboolean force_clear     = FALSE;
static gboolean no_load         = FALSE;

static gchar    *dump;
static gboolean dump_ucode      = FALSE;
static gboolean reload          = FALSE;

static GOptionEntry entries[] = {
    { "firmware", 'f', 0,
        G_OPTION_ARG_FILENAME, &firmware,
        "Path to microcode. %vid% and %pid% are substituted in.", NULL
    },
    { "force-clear", 0, 0,
        G_OPTION_ARG_NONE, &force_clear,
        "Forcefully clear the device before uploading microcode.", NULL
    },
    { "pretend", 0, 0,
        G_OPTION_ARG_NONE, &no_load,
        "Don't actually load any microcode on to the device.", NULL
    },
    { "dump-ucode", 0, 0,
        G_OPTION_ARG_NONE, &dump_ucode,
        "Dump microcode to compiled code.", NULL
    },
#ifdef ENABLE_RELOAD
    { "reload", 0, 0,
        G_OPTION_ARG_NONE, &reload,
        "Reload uvcvideo module when complete.", NULL
    },
#endif
};

gchar *
replace_str(gchar *str, gchar *orig, gchar *rep)
{
  static char buffer[4096];
  char *p;

  if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
    return str;

  strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig'
  buffer[p-str] = '\0';

  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;
}

struct usb_device *
find_device (const struct device_info devices[],
	     const struct device_info **info) {
    const struct device_info *devinfo;
    gint i = 0;
    struct usb_bus *busses;
    struct usb_bus *bus;
    
    busses = usb_get_busses();
    for (bus = busses; bus; bus = bus->next) {
        struct usb_device *dev;
        
        for (dev = bus->devices; dev; dev = dev->next) {
            do {
                devinfo = &devices[i];
                if (dev->descriptor.idVendor == devinfo->usb_vendor &&
                    dev->descriptor.idProduct == devinfo->usb_product &&
                    dev->descriptor.idVendor != 0) {
                    
		    *info = devinfo;
                    return dev;
                }
                
                i++;
            } while (devinfo->usb_vendor != 0);
            
            i = 0;
        }
    }
    
    return NULL;
}

gint
r5u87x_image_flip(struct usb_dev_handle *handle, gboolean hflip,
		  gboolean vflip) {
    gint res;

    res = usb_control_msg (handle, USB_SEND, 0xc2, hflip, R5U870_REG_HFLIP_EX,
                NULL, 0, TIMEOUT);
    if (res < 0) {
        loader_warn ("Unable to set hflip to %u; res: %d\n", hflip, res);
        return res;
    }

    res = usb_control_msg (handle, USB_SEND, 0xc2, vflip, R5U870_REG_VFLIP_EX,
                NULL, 0, TIMEOUT);
    if (res < 0) {
        loader_warn ("Unable to set vflip to %u; res: %d\n", vflip, res);
        return res;
    }

    return 0;
}

gint
r5u87x_ucode_upload (gint firmware, struct usb_dev_handle *handle, gint size) {
    gint length, remaining, address, index, res;
    unsigned char header[3], payload[1024];
    
    gint dump_fd;
    
    index = 0;
    remaining = size;
    
    loader_msg ("Sending microcode to camera...\n");
    
    if (dump_ucode) {
        loader_msg ("Dumping microcode to %s\n", dump);
        if ((dump_fd = g_open (dump, O_WRONLY | O_CREAT, 0666)) == -1) {
            loader_error ("Failed to open microcode dump.\n");
        }
    }
    
    while (remaining > 0) {
        if (remaining < 3) {
            loader_warn ("Microcode file is incomplete; message %d\n", index);
            return -EBADMSG;
        }
        
        // load in packet header
        if ((length = read (firmware, header, 3)) != 3) {
            if (length == 0) {
                return 0;
            } else {
                loader_warn ("Reading firmware header chunk failed\n");
                return -EBADMSG;
            }
        }
        
        length = header[0];
        address = header[1] | (header[2] << 8);
        remaining -= 3;
        
        // Read the payload
        if (length < 0 || length >= 1024) {
            loader_warn ("Bad payload length %d (%i).\n", length, index);
            return -EBADMSG;
        } else if (read (firmware, payload, length) != length) {
            loader_warn ("Failed to read firmware data.\n");
            return -EBADMSG;
        }
        
        // If we're dumping, just write the payload
        if (dump_ucode) {
            loader_msg ("Wrote %d bytes in message %d\n", length, index);
            write (dump_fd, payload, length);
        }
        
        if (no_load == FALSE) {
            res = usb_control_msg (handle, USB_SEND, 0xa0, address, 0,
                (char *)payload, length, TIMEOUT);
            
            if (res < 0) {
                loader_warn ("Command failed. Msg: %d; res: %d\n", index, res);
                return res;
            }
            
            if (res != length) {
                loader_warn ("Command failed. Msg: %d; res: %d; exp: %d\n", 
                    index, res, length);
                return res;
            }
        }
        
        remaining -= length;
        index++;
    }
    
    if (dump_ucode) {
        loader_msg ("Completed dumping of microcode.\n");
        close (dump_fd);
    }
    
    return 0;
}

gint
r5u87x_ucode_status (struct usb_dev_handle *handle) {
    gchar buf[1];
    gint res;
    
    res = usb_control_msg (handle, USB_RECV, 0xa4, 0, 0, buf, 1, TIMEOUT);
    if (res != 1) {
        loader_warn ("Failed to get microcode status.\n");
        return res;
    }
    
    loader_msg ("Camera reports %s microcode state.\n",
        buf[0] == 1 ? "positive" : "negative");
    
    return buf[0] == 1 ? TRUE : FALSE;
}

gint
r5u87x_ucode_version (struct usb_dev_handle *handle, gint *version) {
    gchar buf[2];
    gint res;
    
    res = usb_control_msg (handle, USB_RECV, 0xc3, 0, 0x0e, buf, 2, TIMEOUT);
    if (res != 2) {
        loader_warn ("Failed to get microcode version.\n");
        return res;
    }
    
    res = (buf[1] << 8) | buf[0];
    
    loader_msg ("Camera reports microcode version 0x%04x.\n", res);
    *version = res;
    
    return 0;
}

gint
r5u87x_ucode_enable (struct usb_dev_handle *handle) {
    gchar buf[1];
    gint res;
    buf[0] = 0;
    
    res = usb_control_msg (handle, USB_SEND, 0xa1, 0, 0, buf, 1, TIMEOUT);
    if (res != 1) {
        loader_warn ("Failed to enable microcode.\n");
        return res;
    }
    
    loader_msg ("Enabled microcode.\n");
    
    return 0;
}

gint
r5u87x_ucode_clear (struct usb_dev_handle *handle) {
    gchar buf[1];
    gint res;
    buf[0] = 1;
    res = usb_control_msg (handle, USB_SEND, 0xa6, 0, 0, buf, 1, TIMEOUT);
    
    // Windows driver liked to do a bit of zzz, so we do too.
    if (!res) {
        sleep (200);
    }
    
    if (res < 0) {
        loader_warn ("Failed to reset microcode.\n");
        return res;
    }
    
    loader_msg ("Reset microcode.\n");
    
    return res;
}

gchar*
usb_id_printf (gchar* src, struct usb_device *dev) {
    // Converts the template firmware name into one we're going to use for this
    // device.
    
    src = replace_str (src, "%pid%",
        g_strdup_printf ("%04x", dev->descriptor.idProduct));
    src = replace_str (src, "%vid%",
        g_strdup_printf ("%04x", dev->descriptor.idVendor));
    
    return src;
}

gint
load_firmware (struct usb_device *dev, usb_dev_handle *handle,
               const struct device_info *devinfo) {
    gint fd, res, dev_version;
    struct stat infobuf;

    dev_version = 0;
    
    firmware = usb_id_printf (firmware, dev);

    dump = g_strdup_printf ("r5u87x-dump-%04x-%04x.bin",
        dev->descriptor.idProduct, dev->descriptor.idVendor);
    
    loader_msg ("Found camera: %04x:%04x\n", dev->descriptor.idVendor,
        dev->descriptor.idProduct);
    
    // Open the firmware file
    if ((fd = g_open (firmware, O_RDONLY)) < 0) {
        #ifdef UCODE_PATH
        firmware = usb_id_printf (UCODE_PATH, dev);
        if ((fd = g_open (firmware, O_RDONLY)) < 0) {
            loader_error ("Failed to open %s. Does it exist?\n", firmware);
        }
        #else
        loader_error ("Failed to open %s. Does it exist?\n", firmware);
        #endif
    }
    
    // Possibly not the best way to do this, but hey, it's certainly easy
    // (without loading everything into memory, and compared to seeking around)
    if (stat (firmware, &infobuf) == -1) {
        loader_error ("Failed to get filesize of firmware (%s).\n", firmware);
    }
    
    // Check to see if there's already stuff on there.
    res = r5u87x_ucode_status (handle);
    if (res < 0) {
        return res;
    } else if (res == 1 && !force_clear) {
        // Microcode already uploaded.
        res = r5u87x_ucode_version (handle, &dev_version);
        if (dev_version == 0x0001) {
            loader_warn ("Bad version returned. You appear to be running a "
            "WDM device.\nSee http://www.bitbucket.org/ahixon/r5u87x/issue/6 "
            "for more information.\nSkipping clear for testing.\n");
        }
        
        if (res < 0) {
            return res;
        } else if (dev_version != devinfo->ucode_version &&
		   dev_version != 0x0001) {
            // Clear it out - ucode version and device version don't match.
            loader_warn ("Microcode versions don't match, clearing.\n");
            res = r5u87x_ucode_clear (handle);
            if (res < 0) {
                return res;
            }
            
            res = r5u87x_ucode_status (handle);
            if (res < 0) {
                return res;
            } else if (res == 1) {
                loader_warn ("Camera still has microcode even though we"
                    "cleared it.\n");
            }
        } else {
            // Camera already has microcode - it's all good.
            loader_msg ("Not doing anything - camera already setup.\n");
            return 0;
        }
    } else if (res == 1 && force_clear) {
        // Force reset if asked.
        res = r5u87x_ucode_clear (handle);
        if (res < 0) {
            return res;
        }
    } else if (force_clear) {
        loader_warn ("Not force-clearing because the device has no ucode.\n");
    }
    
    // Upload the microcode!
    res = r5u87x_ucode_upload (fd, handle, infobuf.st_size);
    
    // Close the microcode file
    close (fd);
    
    // and bail out if necessary.
    if (res < 0) {
        return res;
    }
    
    // Enable it after we do a successful upload.
    if (!no_load) {
        res = r5u87x_ucode_enable (handle);
        if (res < 0) {
            return res;
        }
        
        // Check versions to see if all is OK.
        dev_version = 0;
        res = r5u87x_ucode_version (handle, &dev_version);
        if (res < 0) {
            return res;
        } else if (dev_version != devinfo->ucode_version) {
            loader_warn ("Camera returned unexpected ucode version 0x%04x - "
                "expected 0x%04x\n", dev_version, devinfo->ucode_version);
            return -EBADMSG;
        }
    } else {
        loader_warn ("Skipping enabling of microcode and version checks; in "
            "pretend mode.\n");
    }
    
    return 0;
}

gint
main (gint argc, gchar *argv []) {
    GOptionContext *context;
    GError *error = NULL;
    const struct device_info *devinfo;
    struct usb_device *dev;
    usb_dev_handle *handle;
    
    g_set_prgname ("loader");
    context = g_option_context_new("- Ricoh R5U87x series firmware loader");
    g_option_context_add_main_entries (context, entries, NULL);
    
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        loader_error ("%s\n", error->message);
    }
    
    #ifdef VERSION
    loader_msg ("r5u87x firmware loader v%s\n\n", VERSION);
    #else
    loader_msg ("r5u87x firmware loader (unknown version)\n\n");
    #endif
    
    usb_init ();
    if (usb_find_busses () == 0) {
        loader_error ("Failed to find any USB busses.\n");
    } else if (usb_find_devices () == 0) {
        loader_error ("Failed to find any USB devices.\n");
    }
    
    loader_msg ("Searching for device...\n");
    
    dev = find_device (device_table, &devinfo);
    
    if (dev == NULL) {
        loader_error ("Failed to find any supported webcams.\n");
    }
    
    if (!(handle = usb_open (dev))) {
        loader_error ("Failed to open USB device.\n");
    }
    
    int res = load_firmware (dev, handle, devinfo);
    if (res < 0) {
        loader_error ("Failed to upload firmware to device: %s (code %d).\n%s",
            strerror (errno), res,
            res == -1 ? "Try running as root?\n" : "");
    } else {
        loader_msg ("\nSuccessfully uploaded firmware to device %04x:%04x!\n",
            dev->descriptor.idVendor, dev->descriptor.idProduct);
    }
    
    r5u87x_image_flip (handle, devinfo->hflip, devinfo->vflip);
    
    if (reload) {
        gint status;
        loader_msg ("Reloading uvcvideo module...\n");
        status = system ("modprobe -r uvcvideo; modprobe uvcvideo");
        if (status == -1) {
            loader_error ("Unable to create shell process.\n");
        } else if (status == 0) {
            loader_msg ("Finished.\n");
        } else {
            loader_warn ("Recevied non-zero return code: %d.\n", status);
        }
    }
    
    return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.