summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/fiio/usb-fiio.c
blob: 3084ee016988b9df1ea05a7303d2f8cad9a0f024 (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
/***************************************************************************
 *             __________               __   ___
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2018 by Marcin Bukat
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdlib.h>
#include <sys/mount.h>
#include <string.h>
#include "config.h"
#include "disk.h"
#include "usb.h"
#include "sysfs.h"
#include "power.h"
#include "power-fiio.h"

const char * const sysfs_usb_online =
    "/sys/class/power_supply/usb/online";

int usb_detect(void)
{
    int present = 0;
    sysfs_get_int(sysfs_usb_online, &present);

    return present ? USB_INSERTED : USB_EXTRACTED;
}

static bool usb_enabled = 0;

void usb_enable(bool on)
{
    if (usb_enabled == on)
        return;

    usb_enabled = on;
    if (on)
    {
	system ("insmod /lib/modules/3.10.14/kernel/driver/usb/gadget/libcomposite.ko");
	system ("insmod /lib/modules/3.10.14/kernel/driver/usb/gadget/usb_f_mass_storage.ko");
	system ("insmod /lib/modules/3.10.14/kernel/driver/usb/gadget/g_mass_storage.ko file=/dev/mmcblk0 removable=1");
    }
    else
    {
	system ("rmmod g_mass_storage");
	system ("rmmod usb_f_mass_storage");
	system ("rmmod libcomposite");
    }
}

/* This is called by usb thread after usb extract in order to return
 * regular FS access
 *
 * returns the # of successful mounts
*/
int disk_mount_all(void)
{
    return 1;
}

/* This is called by usb thread after all threads ACKs usb inserted message
 *
 * returns the # of successful unmounts
 */
int disk_unmount_all(void)
{
    return 1;
}

void usb_init_device(void)
{
    system ("insmod /lib/modules/3.10.14/kernel/driver/staging/dwc2/dwc2.ko");
    usb_enable(true);
}