11573Srgrimes#!/bin/sh /etc/rc.common
21573Srgrimes# Copyright (C) 2008 OpenWrt.org
31573SrgrimesSTART=90
41573Srgrimes
51573SrgrimesCONFIGFS_DIR="/config/gpiommc"
61573Srgrimes
71573Srgrimes# add_device(name, DI_pin, DO_pin, CLK_pin, CS_pin, mode)
81573Srgrimesadd_device() {
91573Srgrimes	local dir="$CONFIGFS_DIR/$1"
101573Srgrimes
111573Srgrimes	mkdir -p $dir
121573Srgrimes	[ $? -eq 0 ] || return 1
131573Srgrimes	echo $2 > $dir/gpio_data_in
141573Srgrimes	[ $? -eq 0 ] || return 1
151573Srgrimes	echo $3 > $dir/gpio_data_out
161573Srgrimes	[ $? -eq 0 ] || return 1
171573Srgrimes	echo $4 > $dir/gpio_clock
181573Srgrimes	[ $? -eq 0 ] || return 1
191573Srgrimes	echo $5 > $dir/gpio_chipselect
201573Srgrimes	[ $? -eq 0 ] || return 1
211573Srgrimes	echo $6 > $dir/spi_mode
221573Srgrimes	[ $? -eq 0 ] || return 1
231573Srgrimes	# XXX We have more config options available. Use defaults for now.
241573Srgrimes
251573Srgrimes	echo 1 > $dir/register
261573Srgrimes	[ $? -eq 0 ] || return 1
271573Srgrimes
281573Srgrimes	return 0
291573Srgrimes}
301573Srgrimes
311573Srgrimes# remove_device(name)
321573Srgrimesremove_device() {
331573Srgrimes	local dir="$CONFIGFS_DIR/$1"
341573Srgrimes
3523658Speter	rmdir $dir
361573Srgrimes}
3790039Sobrien
3890039Sobrienmount_configfs() {
391573Srgrimes	# FIXME: This should probably be done somewhere else.
4071579Sdeischen	mount | grep configfs
411573Srgrimes	if [ $? -eq 0 ]; then
421573Srgrimes		# already mounted
4353812Salfred		return 0
4461193Skris	fi
4553812Salfred	mkdir -p /config
4671579Sdeischen	[ $? -eq 0 ] || return 1
471573Srgrimes	mount configfs -t configfs /config
4871579Sdeischen	[ $? -eq 0 ] || return 1
4971579Sdeischen
501573Srgrimes	return 0
511573Srgrimes}
521573Srgrimes
531573Srgrimesstart_service() {
5471579Sdeischen	local section="$1"
5569656Sdeischen	config_get "name" "$section" "name"
561573Srgrimes	config_get "DI_pin" "$section" "DI_pin"
5769656Sdeischen	config_get "DO_pin" "$section" "DO_pin"
581573Srgrimes	config_get "CLK_pin" "$section" "CLK_pin"
591573Srgrimes	config_get "CS_pin" "$section" "CS_pin"
6023658Speter	config_get "mode" "$section" "mode"
6123658Speter	config_get_bool "enabled" "$section" "enabled" '1'
6223658Speter	[ "$enabled" -gt 0 ] && add_device "$name" $DI_pin $DO_pin $CLK_pin $CS_pin $mode &
6323658Speter}
6423658Speter
6523658Speterstop_service() {
6671579Sdeischen	local section="$1"
671573Srgrimes	config_get "name" "$section" "name"
681573Srgrimes	remove_device "$name"
6923658Speter}
701573Srgrimes
711573Srgrimesstart() {
7233665Sjb	# Make sure configfs is mounted
7323658Speter	mount_configfs
741573Srgrimes	[ $? -eq 0 ] || return 1
751573Srgrimes
7623658Speter	config_load "mmc_over_gpio"
771573Srgrimes	config_foreach start_service "mmc_over_gpio"
781573Srgrimes}
791573Srgrimes
8023658Speterstop() {
8123658Speter	config_load "mmc_over_gpio"
821573Srgrimes	config_foreach stop_service "mmc_over_gpio"
831573Srgrimes}
841573Srgrimes