1/* Copyright (c) 2003-2011
2 * Stefano Ceccherini <stefano.ceccherini@gmail.com>. All rights reserved.
3 */
4#include "debug.h"
5#include <Debug.h>
6
7#include <KernelExport.h>
8#include <Errors.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12
13#include "wb840.h"
14#include "device.h"
15#include "driver.h"
16
17#define MAX_CARDS 4
18
19int32 api_version = B_CUR_DRIVER_API_VERSION;
20
21pci_module_info* gPci;
22char* gDevNameList[MAX_CARDS + 1];
23pci_info* gDevList[MAX_CARDS];
24
25
26static bool
27probe(pci_info* item)
28{
29	if ((item->vendor_id == WB_VENDORID && item->device_id == WB_DEVICEID_840F)
30			|| (item->vendor_id == CP_VENDORID && item->device_id == CP_DEVICEID_RL100))
31		return true;
32	return false;
33}
34
35
36status_t
37init_hardware(void)
38{
39	LOG((DEVICE_NAME ": init_hardware\n"));
40	return B_OK;
41}
42
43
44status_t
45init_driver(void)
46{
47	struct pci_info* item = NULL;
48	int index = 0;
49	int card_found = 0;
50	char devName[64];
51	status_t status;
52
53	LOG((DEVICE_NAME ": init_driver\n"));
54
55#ifdef DEBUG
56	set_dprintf_enabled(true);
57#endif
58
59	status = get_module(B_PCI_MODULE_NAME, (module_info**)&gPci);
60	if (status < B_OK)
61		return status;
62
63	item = (pci_info*)malloc(sizeof(pci_info));
64	if (item == NULL) {
65		put_module(B_PCI_MODULE_NAME);
66		return B_NO_MEMORY;
67	}
68
69	while (gPci->get_nth_pci_info(index, item) == B_OK) {
70		if (probe(item)) {
71			gPci->write_pci_config(item->bus, item->device, item->function,
72				PCI_command, 2, PCI_command_master | gPci->read_pci_config(
73					item->bus, item->device, item->function,
74					PCI_command, 2));
75			gDevList[card_found++] = item;
76
77			dprintf(DEVICE_NAME ": revision = %x\n", item->revision);
78
79			item = (pci_info *)malloc(sizeof(pci_info));
80		}
81		index++;
82	}
83	free(item);
84
85	gDevList[card_found] = NULL;
86
87	if (card_found == 0) {
88		put_module(B_PCI_MODULE_NAME);
89		return ENODEV;
90	}
91
92	for (index = 0; index < card_found; index++) {
93		sprintf(devName, DEVICE_NAME "/%d", index);
94		LOG((DEVICE_NAME ":enabled %s\n", devName));
95		gDevNameList[index] = strdup(devName);
96	}
97
98	gDevNameList[index] = NULL;
99
100	return B_OK;
101}
102
103
104void
105uninit_driver(void)
106{
107	int32 i = 0;
108
109	LOG((DEVICE_NAME ": uninit_driver()\n"));
110	while(gDevNameList[i] != NULL) {
111		free(gDevList[i]);
112		free(gDevNameList[i]);
113		i++;
114	}
115
116	put_module(B_PCI_MODULE_NAME);
117}
118
119
120const char**
121publish_devices()
122{
123	return (const char**)gDevNameList;
124}
125
126
127device_hooks*
128find_device(const char* name)
129{
130	int32 i;
131	char* item;
132
133	LOG((DEVICE_NAME ": find_device()\n"));
134	// Find device name
135	for (i = 0; (item = gDevNameList[i]); i++) {
136		if (!strcmp(name, item)) {
137			return &gDeviceHooks;
138		}
139	}
140	return NULL; // Device not found
141}
142