#!/bin/bash

# Sound-via-pnp-script for Thinkpad soundcards that use ISA drivers.
# and possibly other computers with onboardi CS4239/CS4610 or CS4236 cards

# Changelog:
# provided by Mkarcher 22 January 2006  (published on thinkwiki.org)
# First version made for Thinkpad 600E  with CS4239/CS4610 
# that do not work with the PCI driver and are not recognized by the
# PnP code of snd-cs4236

# 20090101 modified by jpduyx
# make it a little more generic, and make predefined configuration that works with a CS4236 sound card on a Thinkpad 770E

# Configuration
# uncomment the ID's for the thinkpad model you have (todo: auto find these, or get them from a pnp id database?)
# find Your ids with lspnp (in pnptool package on Debian)
# part of output from lspnp on TP 770E:
#00:06 CSC0010 Crystal PnP audio system control registers
#00:07 CSC0000 Crystal PnP audio system CODEC
#00:08 CSC0001 Crystal PnP audio system joystick
#00:09 CSC0003 Crystal PnP audio system MPU-401 compatible

# Thinkpad 770E with CS4236
WSPNPID=CSC0000
CTLPNPID=CSC0010

#Thinkpad 600E with CS4239/CS4610
#WSPNPID=CSC0100
#CTLPNPID=CSC0110  

# search sound card pnp device
for dev in /sys/bus/pnp/devices/*
do
  grep $WSPNPID $dev/id > /dev/null && WSSDEV=$dev
  grep $CTLPNPID $dev/id > /dev/null && CTLDEV=$dev
done

# activate devices 
# Thinkpad boots with devices disabled unless "fast boot" is turned off

echo activate > $WSSDEV/resources
echo activate > $CTLDEV/resources

# parse resource settings
{ read
 read bla port1
 read bla port2
 read bla port3
 read bla irq
 read bla dma1
 read bla dma2
 # Hack: with PnPBIOS: ports are: port1: WSS, port2: OPL, port3: sb (unneeded)
 #       with ACPI-PnP:ports are: port1: OPL, port2: sb, port3: WSS
 # (ACPI bios seems to be wrong here, the PnP-card-code in snd-cs4236.c uses the
 #  PnPBIOS port order)
 # Detect port order using the fixed OPL port as reference
 if [ ${port2%%-*} = 0x388 ]
 then
   # PnPBIOS: usual order
   port=${port1%%-*}
   oplport=${port2%%-*}
 else
   # ACPI: mixed-up order
   port=${port3%%-*}
   oplport=${port1%%-*}
 fi
 } < $WSSDEV/resources

{ read
 read bla port1
 cport=${port1%%-*}
} < $CTLDEV/resources

# load the module

modprobe --ignore-install snd-cs4236 port=$port cport=$cport fm_port=$oplport irq=$irq dma1=$dma1 dma2=$dma2 isapnp=0 index=0 && /lib/alsa/modprobe-post-install snd-cs4236

