Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Linux Software

Writing Kernel Drivers 72

There's a pretty cool article running on Linux.com about the how-to for writing kernel drivers - in this case, the PC speaker. Interesting case and it's interesting to see what's going on in there.
This discussion has been archived. No new comments can be posted.

Writing Kernel Drivers

Comments Filter:
  • I wouldn't say that mod points are radioactive, but they *could be* bits of jagged glass...

    I'm logged into a different account right now because I have mod points on another account but I'm stuck with Win98 at work. Those combo boxes quickly deplete the pool of Windows resource handles, causing all sorts of strange rendering problems, and occasionally cause a crash.

    AFAIK, it's only a problem for Win9x. It works just fine with NT systems, and I doubt that it would ever be an issue in Windows 3.1, simply becauase you'd crash long before then, thanks to its yet smaller pool of handles.
  • by Anonymous Coward
    Wolf3d supported it (as did any game that supported the "Disney Sound Source" or something to that effect).

    Yes, that means that Disney sold a sound card that hooked up to your parallel port and was nothing more than 8 resistors.
  • by Anonymous Coward
    I was one of the ones who modded you down. The problem isn't with your post, it's that I've received moderator points 3 days in a row now. I've given up moderating on merit and just randomly assign mod points to the first few posts I see. Something's screwed up with the system...
  • The Linux Journal [linuxjournal.com] had a great article by Alessandro Rubini [mailto] on how to build your own parallel port audio device for Linux with just a couple of chips. The article was on page 70 of LJ issue number 53, September 1998. There is a listing [ssc.com] of the driver at available at the LJ website.
  • by voidptr ( 609 ) on Tuesday May 01, 2001 @01:35PM (#252789) Homepage Journal
    A good portion of his article was over-riding the Scheduler interrupt vector to give control to his driver > 100HZ. Is there any better way to do this, since it seems like this method is a little suspect (And multiple drivers using this technique might not work together.)

    I'm developing a driver for a bit of cheap hardware that unloads a high-frequency counter onto the host processor and needs to be serviced at about 30kHZ.
  • There was also a hack by which you could place a normal audio casette into the Commodore C2n Datasette drive, and read the varying audio pitches as rudimentary digital data- and output it via the SID chip to play audio tapes. Boy did the quality suck! But you could recognize the tune being played, barely.

    Well, the lack of quality is due to the limitations of the tape drive, it wasn't meant to sample sounds...

    SID is well capable of playing sampled sounds at 8000 hz, 4 bits, mono. This is done by sending the stuff to the volume register - volume change makes a snap, and volume of the snap depends on the value.

    Theoretically, it'd be possible to sample something using a quality sampler (like PC or Amiga sound hardware), convert to SID-playable quality, and play it. When using normal C64, sample rate is limited to around 8000 hz, but when using "modern" hardware like PC with HardSID card or C64 with SuperCPU you might be able to get higher sample rate.

  • This is way cool, and it's coolness is not in the least diminished by the fact that something very much like this was used on my old TRS-80 Model I computer, back in, oh, 1981.

    There was no audio (hell, there was barely video) on that machine, and the original data storage medium was cassette tapes. Stored at about 300 baud, or so. The output of the cassette port was about the same +5v / 0v range, and people used the same approach to store data on the tape (I think it's called Frequency Shift Keying, but I'm not sure).

    Anyway, people also realized early on that you could hook an audio amplifier to the output, and do some rudimentary sound effects. People played with it more, got some very nicely done 'drivers' (as it were) that were easy to call from BASIC, and, volia!, you've added laserbeam sound effects to your Android Nim program.

    Then someone really got good with sound (I think it was Leo Christopherson's Ride of the Valkyries program, but it might have been Dancing Demon, too), with a lot more advanced and cool-sounding effects. Then it got really out of hand when someone figured out how to to speech synthesis. Still, to this day, some of the best speech I've heard from a computer.

    And this, ladies and gentlemen, was on a 1.77 MHz Z-80.

    This is yet another example of how, with all the advanced hardware we've got today, we've lost sight of how to "do more with less." Everyone could learn from this...

  • You can use WinDBG (a free download from MS) to
    investigate the memory.dmp file.
    HTH.
  • It's a kernel debugger (if you can't tell with the
    title) so you can get a stack trace, dump
    various kernel mode data structures, find out
    what the various threads were doing and so forth.
  • You mean it won't spit out cryptic messages in white text on a blue background, displayed with a progress meter for the creation of todays incomprehensible MEMORY.DMP file? Seems like it's just not up to Microsoft's level.

    --
  • I think you're talking about Visual Basic. It does all that, but I haven't heard of any plans to port it to Linux.

    --
  • You can use Notepad, too. Does WinDBG give any useful info? Info that can be found in the Knowledge Base? Unlike many Event Log errors, that is.

    If it's useful, I'll have to give it a poke or two.

    --
  • I think it was just a gwbasic proggie...
    10 PLAY "edabd...."
    etc.
    I think I have it somewhere. I also think there's a program to play those gwbasic PLAY "" things on linux...
  • by vherva ( 22270 ) on Tuesday May 01, 2001 @12:59PM (#252798)
    Come on, Savolainen can't be that hard to type... You could even cut'n'paste these days.

    Also there has already been one pcspeaker patch [linux.org] for ages - it would've been nice if that had been mentioned.

    Other than that, nice article.

  • by jfunk ( 33224 ) <jfunk@roadrunner.nf.net> on Tuesday May 01, 2001 @02:40PM (#252799) Homepage
    Yes it was supported. Try using the "Covox Speech Thing" or "Disney Sound Source" drivers. They're all simple DACs that used the Dx lines on the parallel port for the data. ISA sound cards worked on the same principle, but were accessed directly on the bus.

    I built one of these and it was sweet. It also worked under Windows 3.x.

    A Linux driver would be simple as well, if it hasn't already been done.
  • by Tower ( 37395 ) on Tuesday May 01, 2001 @01:17PM (#252800)
    From the article:
    "Suppose that you want to publish a Phd thesis on how to wash clothes using your brand of washing machine. You'd write a sequence of steps starting from:

    1) Insert the power cord into the wall socket and switch on the power"

    Now, if you happened to be writing a PhD thesis, wouldn't you make 'insert the power cord into the wall socket' and 'switch on the power' two separate methods, possibly expounding for at least half a page as to the meaning of 'switch on the power', which is rather vague - is that the circuit breaker, or is there some power switch on the washer that I haven't seen yet? (hint: there isn't). Granted, a graduate thesis doesn't need to state the obvious as often as undergrad work, but it sure doesn't hurt. Of course, why someone would write a PhD thesis about the operation (rather than the design) of a new washing machine is beyond me... that might be better covered in the patent ;-)

    --
  • "Kernel Drivers"? Does anyone call them that? I would have called them "device drivers". Is this because they don't all deal with devices?
    --
  • I remember some application that came with Netware 3.11 that would play music and realistic speech over the pc speaker. Sounded pretty good too.
  • Please india has a very big market for PCs. You can buy a decent PC(600 celeron) with sound and speakers for $600(Rs.28000). Many people in India have PCs at home and many are able to afford it.

    Have you ever visited India to make that generalization? Are you aware of the buying potential of individuals in that country?

  • You forgot the cassette interface on the back of my original IBM Personal Computer.
  • by DragonWyatt ( 62035 ) on Tuesday May 01, 2001 @03:05PM (#252805) Homepage
    Dude, the C64 (Commodore) was truly advanced. It had an awesome SID chip that could do all these wonderful things, and the machine was only 1mhz (.999082 in the UK b/c 50hz!).

    But I'm not here to talk about the C64.

    There was a program for the 5.25 disk drive that played "A bicycle built for two" by vibrating the heads at varying frequencies. It was a machine language program downloaded into the drive's 2kb buffer, which redirected the boot (IPL?) routine- the code was downloaded to the drive, then the drive was reset.
    Its last task after playing the music was to restore the original boot vector, and reset again!
    This was undoubtedly inspired by (written by?!!?) the same guys who would cause old IBM drum disks to walk across the floor.

    There was also a hack by which you could place a normal audio casette into the Commodore C2n Datasette drive, and read the varying audio pitches as rudimentary digital data- and output it via the SID chip to play audio tapes. Boy did the quality suck! But you could recognize the tune being played, barely.
  • Yeah, actually (hold on) it's "Impossible Mission [tripod.com]". A great game, but darn was it hard. I never got far. :(
  • by selectspec ( 74651 ) on Tuesday May 01, 2001 @01:11PM (#252807)
    Both have sections "anticipating 2.4" which give you an idea of where to look for changes. The real problem is going to be with the next major revision, where jiffies might get tossed, the I/O api gets a new face. However, for someone without a clue on how to write a kernel driver, both books are a good start.
  • by selectspec ( 74651 ) on Tuesday May 01, 2001 @12:56PM (#252808)
    There are 2 awesome books on kernel and driver development from our friends at O'Reilly.

    Understanding the Linux Kernel

    and

    Linux Device Drivers
  • Good.

    And now you know how-to make the driver part. Youre almost done...
  • Ooh - that brings back memories of frantically switching cables over after the tape had finished loading so that you could get the full glory of the dramatic words 'ROBOT ATTACK'. Dancing Demon was great!
    -- Andrem
  • by jon_c ( 100593 )
    And i didn't think anything could sound crappier then the generic $9.95 speakers my work gave me.

  • by 13013dobbs ( 113910 ) on Tuesday May 01, 2001 @01:20PM (#252812) Homepage
    RIAA found out and sued over it.
  • well...that's one way of telling you something went wrong....

    It's nature's way of telling you in a song.
    --

  • by ozbird ( 127571 ) on Tuesday May 01, 2001 @02:37PM (#252814)
    There was no audio (hell, there was barely video) on that machine, and the original data storage medium was cassette tapes. Stored at about 300 baud, or so. The output of the cassette port was about the same +5v / 0v range, and people used the same approach to store data on the tape (I think it's called Frequency Shift Keying, but I'm not sure).

    The TRS-80 cassette schematics can be seen here [inetnebr.com]. Unlike the PC speaker output which is a simple square wave (bilevel), the TRS-80 cassette output was trilevel - default was 1V, with 0V and 2V pulses individually generated.

    Data output to the cassette was generated as a series of positive and negative pulses approximating a sinewave; from memory, there was a clocking pulse-pair followed by another pulse-pair for "1" or nothing for "0". (I may have misremembered this; there were several utilities that increased the data density on the cassette, and I may be remembering one of these instead.)

    Audio sound was generated by manually generating the cassette pulses at given frequencies. Some bright sparks also figured out that you could generate two-voice music by using positive pulses for one voice, and negative pulses for the other. Very neat!

    See also the TRS-80 Documentation Preservation Page [discover-net.net] - very handy since these computers last a lot longer than their manuals...
  • People develop free software to solve problems which they have or to learn about stuff. One very common thing you see in ee/cs labs in Indian engineering schools is that you get only the most neccessary equipment. PC sound cards are not considered that important. In this context such an effort makes a lot of sense.
  • Why is the parent moderated as funny? I am most sure that the person making the comment was not attempting to be. The situation he describes is very true. A sound card is considered a luxury in many Indian schools!
  • err, not a CD...3.5" diskette...whatever.
  • I remember waaaay back when, when HomePC used to still be around, which was eventually replaced with somthing else...but as an added "sign on bonus", you got a special utilities and fun CD...of the most interesting was some WAV files for windows 3.1 start up and shutdown sounds like "Good Morning", and"Elvis has left the building". Of course, that was back in the day when PC's rarely came with sound cards, and my 386 sx 15, salvaged from my dad's work scrap pile, certianly did not. To solve this problem, they had a nifty thing that allowed you to play .wav files through your PC speaker. That, combined with www.twistedtunes.com (this was long before napster), I was able to play 20 meg wav file songs off my hard drive. It would only play the first 45 seconds or so, of course, the computer couldn't handle the buffering or somthing. I tried installing the win3.1 driver on an old P90 last year that was void a sound card, and had no luck; I was hoping I'd be able to play MP3's off it, but no luck.

    Anyone know of a windowze 9x speak.dll, or equivlent? That would come in awfully handy occasionally.
  • No not Mission Impossible...

    Impossible "something or other"...
    ---
  • Checkout http://support.microsoft.com/support/kb/articles/Q 261/1/86.ASP [microsoft.com] ; Microsoft has computers playing classical music all by themselves!

  • by campgod ( 155540 ) on Tuesday May 01, 2001 @05:24PM (#252821)
    If you can wait a (few) month(s), "the second edition (due in June) covers Kernel 2.4 and adds discussions of symmetric multiprocessing (SMP), Universal Service Bus (USB), and some new platforms." (from http://www.oreilly.com/catalog/linuxdrive2/) No, I don't work for them, but found the book very helpful in writing a driver for the PLX 9050. It gets my newbie seal of approval.
  • This article is great for somebody that is already knowledgeable of PC internals that would like to get into the device driver world. One caveat... the writer assumes the ISR of the system is 100HZ... while this is true for most systems, its generally a good idea to calculate the ISR values and adjust the timer calculations from there. That way your driver will match the HZ value of the ISR.

    Subvert Reality.
  • This has me thinking about a game called MissionImpossible on the C64 that included an impressive bit of voice synth running on some pretty modest hardware.

    If I remember it correctly...it said, "Stay a while...Stay Forever!"
  • Are you kidding? There's a HUGE market for PC speaker drivers! Personally, I'm waiting for Linux drivers for (1) dot matrix printers, (2) lightpens, (3) Laserdisc players, and (4) the human appendix. Come on, programmers, let's get coding! :-)

    /* Steve */
  • great... how many ways do we need of sending "beep"? I remember that I used to have this app for an 8088 that played the complete tonal "William Tell Overture" out of the PC speaker... I wonder what happened to it?

    Well sir, if you were to read the article you would find that this has nothing to do with sending a beep, but if you had an mp3 version of the william tell overture you could send that right over your pc speaker.. his device driver is used to play wav's and mp3's over the pc speaker, not just beeping...

  • Has everyone forgotten one of the neatest hacks of the trash-80? Someone (Christenson I believe) figured out how to write timing loops that used RFI to play tunes on a nearby AM radio. I bought the game just to see this puppy in action, towards the end of the TRS-80's life this was the main method of generating sound.
  • by djocyko ( 214429 ) on Tuesday May 01, 2001 @01:02PM (#252827)
    "The kernel will tell you if anything went wrong. In many cases, it'll reboot the system for you."

    well...that's one way of telling you something went wrong....

  • by Spinality ( 214521 ) on Tuesday May 01, 2001 @01:20PM (#252828) Homepage
    Quite a few comments below seem to think this article was cited because of providing information about the speaker as a device. Sheesh! Obviously, it was instead seen as interesting because it was a simple example of driver implementation issues, without all the gory complexity of BitBlts or nasty interrupts or odd data structures. It's a simple example, using a hardware component every user already knows.

    I at least was happy to see it listed here. I like simple examples.

    /rant
  • Both PC speaker and AWE 64 are ISA devices. Perhaps you meant more modern hardware.
  • A Linux driver would be simple as well, if it hasn't already been done.

    There has. Michael Beck/David Woodhouse's PC speaker patch [linux.org] also supports parallel port DACs (and has schematics on how to brew your own).

    -Marcel

  • by markom ( 220743 ) on Tuesday May 01, 2001 @01:09PM (#252831) Homepage
    Living in a economically disturbed environment when salaries often range around the price of a decent sound card (or equally fun device) teaches you one rather important lesson in life. Being a young engineer coming from an environment like that (Belgrade, Yugoslavia), taught me to do my best to solve the problems with the only thing I had - my brains. Not my wallet. When I moved out to one economically rather different and technically advanced place (Iceland), my hard time in doing so payed off.

    Effort to make working PC speaker kernel driver is not a waste, although it can be solved with a little bit of cash more eleganlty.

    Marko.
  • I am waiting for some genius in the linux community to write a /bin/writekerneldriver alias to vim that types everything in for me and then builds the make files, compiles and installs it, just it time for it to core and take down the system
  • Thanks for the link. The program in the article refused to compile for me - LOADS of errors. I suspect that is related to trying to cut'n'paste from Netscape, but getting Lynx to dump the page out didn't help either...
  • This was more that "play", for it had multiple harmonizing parts together, was a compiled .com file, and didn't have any libraries. all that I needed to set it was an app called "timing.com" which would change something in "willtell.com" to set the speed of the song for the computer. the problem is that it would max out how it could be set for a mid-grade 486, and wouldn't be able to be heard properly on any new PCs...
    "Titanic was 3hr and 17min long. They could have lost 3hr and 17min from that."
  • by TWX_the_Linux_Zealot ( 227666 ) on Tuesday May 01, 2001 @12:51PM (#252835) Journal
    great... how many ways do we need of sending "beep"?

    I remember that I used to have this app for an 8088 that played the complete tonal "William Tell Overture" out of the PC speaker... I wonder what happened to it?

    Hiyo Silver... AWAY!!!!

    "Titanic was 3hr and 17min long. They could have lost 3hr and 17min from that."
  • Now that we have an PCM capable pc speaker driver, who writes a driver for this nice printer port DACs some people made out of some resitors ?
    I read somewhere that they sounded better than the first pc sound cards. Now it is actually hard to belive that the simplest soundcard have cost $150 and up. They couldn't even do stereo. And at the same time a amiga had hardware wavetable-capable sound hardware.
  • >possibly expounding for at least half a page as to the meaning
    >of 'switch on the power', which is rather vague - is that the circuit breaker, or is there some power switch on the
    >washer that I haven't seen yet? (hint: there isn't).

    There's a power switch for every socket in India.

  • The speaker, that is. But couldn't the author's spend more time focusing on development of stuff like AWE64 sound cards and the like? ;-)
    Wow dude ! That would be super high end hardware ! No one could afford these ! Let him write drivers for the Ad Lib that soundcard's bleeding edge !

    More seriously, I don't see what's wrong with the AWE64 linux drivers. I am using one right now.
  • Well, there's always the fact that when you're writing a how-to for a novice audience (and many seasoned programmers would be novices when approaching the task of writing a kernel driver), you write an example that covers the necessary material, and as little else as possible. Doing an AWE64 or a 3Com 10/100 network card or a USB gadget driver might be a neat project, but would also involve a lot of particulars about the manufacturer's hardware that wouldn't have anything to do with what he's trying to teach.

    Anyway, it seems a fine approach to me. Between the knowledge in the article, available source code, and appropriate hardware specs, I imagine a determined hacker could start writing his first real device driver with this article as a starting point.

    OK,
    - B
    --

  • This may be obvious, but the ALSA driver has fairly decent documentation on how to add a specific piece of audio hardware to it.

    You can find it here. [tu-berlin.de]

  • Sorry, but ive had 15 mod points in the last 4 days
  • Check out One of the first computers to play music [mu.oz.au]. The way it was done was amazing with what they had to work with.
  • Plus which every PC has one of these speakers in it, so any Linux-using PC owner can try the driver straightaway. And if you make a mistake while you're hacking it, there's at least the possibility that it won't render your screen unreadable, or trash your hard disk ;-)
  • What I would really like to see is PCMCIA sound support -- no free OS so far has supported them, and unfortunately it's the only option I have for sound on my laptop.
  • I especially like the technique Matthews is using to overcome the lowly 1 bit DAC/timer tied to the PC speaker. However, he fails to mention it is a known method, called PWM, or pulse width modulation. It's a pretty cool and well known trick, but hardly genious as he accredits himself. Any "2nd year DSP student", as he self-admits, knows of this method.
  • by Gruneun ( 261463 ) on Tuesday May 01, 2001 @01:18PM (#252846)
    I'd be interested to see this guy as a teacher in a university. To tackle subjects like audio, hardware, and device drivers in one, relatively short article is impressive. To make it sound simple and easy-to-follow is astounding.

    If we had more instructors take the time to explain this way, we would have more people stay and graduate in Engineering and Computer Science. This would result in more capable new hires, fewer unfilled positions, less demand for tech people -- Hey, wait a minute! -- get that article down! Where's the /. effect when you need it?
  • The first sound I heard about was some old beast like that, but it didn't have a speaker. What happened is that it created radio intereference at a frequency based on the data going across the bus, and the instructions could be set up to create crude sound effects. Unfortunately, I read this too long ago to remember the source or find a link :(
  • The speaker, that is. But couldn't the author's spend more time focusing on development of stuff like AWE64 sound cards and the like? ;-)

    Dancin Santa
  • by Peridriga ( 308995 ) on Tuesday May 01, 2001 @01:51PM (#252849)
    Currently intrested in finding a Linux driver for my Furby... My Tux doll is lonely and needs something new and annoying to kill...

    --- My Karma is bigger than your...
    ------ This sentence no verb
  • My Commodore PET (c1977) has a speaker hacked onto one of the serial ports (iirc). POKE and PEEK to your heart's delight! I'm not much of a hardware guru, but damn was this cool :)

  • > I read somewhere that they sounded better than the first pc sound cards.

    They did. Many early 'cheap' soundcards (read: affordable by a student) did only midi synthesized sound, not samples. This also was a fun, cheap toy to play with. Unfortunately it was not supported by any game, IIRC.

  • Well, it does make me remember the time that a homemade 2-bit sound sampler connected to the userport of the cbm-64 enabled us to create samples, and play it back by manipulating the volume register in the SID ('sound' chip). Sound quality was horrible, but it was state of the art, and one could still understand quite easily how it all worked. Writing a playback driver in 6510 mnemonics sure was fun!

    Modern soundcards and lan devices are a lot harder to grasp, especially if it's the first time that you try to write something like a device-driver.

  • First: Kudos to Cherry for doing such a neat article!

    Second: Before everyone starts saying 'Win3.1 could do this' - it could but the driver was crap and went to 100% processor usage all the time.

    Third: Check out RTLinux (www.rtlinux.com [rtlinux.com]) for a very cool mod to give real time scheduling to Linux (and now FreeBSD!) - I know the 2.0 version has an example PC speaker driver written as a Real Time kernel module, don't know about the new 3.0 - go get the patches, go have fun!

    This is something I would love to see integrated into the tree at some point.
  • This article jogged my memory. I actually did the exact same thing in 1987 under MSDOS in assembler. I dug out the ZIP file and took a look.

    Steps involved:

    1. Registering driver: run PULSMOD.COM
    2. Hooking Interrupt: CLI; [play whole sound file]; STI
    3. Stopping playback in progress: Activate the hardware non-maskable interrupt via your aftermarket debugger board
    That's it. It seems like DOS enabled a much simpler and more elegant PC speaker hack. :-P
  • I recently had a linux box auto-reboot. It was while testing DRI Rage128 acceleration. In the middle of running FlightGear the screen blanked, then it appeared the machine had quietly and cleanly rebooted.

    At first I found this amazing, then annoying, then ultimately quite useful.

    There's no doubt that eventually I would have crashed X, or the kernel. It might have taken some time, and a lot of effort, and not a little swearing along the way. At least this way I achieved the same success in a minimum of time. I never even had to reach for the reset button.

    A new feature for 2.5 perhaps...

  • What do the programmers here use to print out their code? Do I hear "dot-matrix printer"?

THEGODDESSOFTHENETHASTWISTINGFINGERSANDHERVOICEISLIKEAJAVELININTHENIGHTDUDE

Working...