Root/jzboot/src/devmgr.c

1/*
2 * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors.
3 * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdlib.h>
20
21#include "devmgr.h"
22#include "debug.h"
23
24typedef struct device {
25    struct device *next;
26    uint16_t vid;
27    uint16_t pid;
28    void *data;
29} usb_device_t;
30
31static const struct {
32    uint16_t vid;
33    uint16_t pid;
34} devices[] = {
35    { 0x601a, 0x4740 },
36    { 0x601a, 0x4750 },
37    { 0x601a, 0x4760 },
38    { 0, 0 }
39};
40
41static usb_device_t *list = NULL;
42
43int is_ingenic(uint16_t vid, uint16_t pid) {
44    for(int i = 0; devices[i].vid != 0; i++)
45        if(devices[i].vid == vid && devices[i].pid == pid)
46            return 1;
47
48    return 0;
49}
50
51void add_device(uint16_t vid, uint16_t pid, void *data) {
52    usb_device_t *newdev = malloc(sizeof(usb_device_t));
53
54    newdev->next = list;
55    newdev->vid = vid;
56    newdev->pid = pid;
57    newdev->data = data;
58
59    list = newdev;
60
61    debug(LEVEL_DEBUG, "Device manager: registered %04hX:%04hX with data %p\n", vid, pid, data);
62}
63
64void enum_devices(void (*handler)(int idx, uint16_t vid, uint16_t pid, void *data)) {
65    int idx = 0;
66
67    for(usb_device_t *dev = list; dev; dev = dev->next)
68        handler(idx++, dev->vid, dev->pid, dev->data);
69}
70
71void *get_device(int idx) {
72    for(usb_device_t *dev = list; dev; dev = dev->next)
73        if(idx-- == 0)
74            return dev->data;
75
76    return NULL;
77}
78

Archive Download this file



interactive