Back to home page

LXR

 
 

    


0001 /*
0002  *  Sound core.  This file is composed of two parts.  sound_class
0003  *  which is common to both OSS and ALSA and OSS sound core which
0004  *  is used OSS or emulation of it.
0005  */
0006 
0007 /*
0008  * First, the common part.
0009  */
0010 #include <linux/module.h>
0011 #include <linux/device.h>
0012 #include <linux/err.h>
0013 #include <linux/kdev_t.h>
0014 #include <linux/major.h>
0015 #include <sound/core.h>
0016 
0017 #ifdef CONFIG_SOUND_OSS_CORE
0018 static int __init init_oss_soundcore(void);
0019 static void cleanup_oss_soundcore(void);
0020 #else
0021 static inline int init_oss_soundcore(void)  { return 0; }
0022 static inline void cleanup_oss_soundcore(void)  { }
0023 #endif
0024 
0025 struct class *sound_class;
0026 EXPORT_SYMBOL(sound_class);
0027 
0028 MODULE_DESCRIPTION("Core sound module");
0029 MODULE_AUTHOR("Alan Cox");
0030 MODULE_LICENSE("GPL");
0031 
0032 static char *sound_devnode(struct device *dev, umode_t *mode)
0033 {
0034     if (MAJOR(dev->devt) == SOUND_MAJOR)
0035         return NULL;
0036     return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));
0037 }
0038 
0039 static int __init init_soundcore(void)
0040 {
0041     int rc;
0042 
0043     rc = init_oss_soundcore();
0044     if (rc)
0045         return rc;
0046 
0047     sound_class = class_create(THIS_MODULE, "sound");
0048     if (IS_ERR(sound_class)) {
0049         cleanup_oss_soundcore();
0050         return PTR_ERR(sound_class);
0051     }
0052 
0053     sound_class->devnode = sound_devnode;
0054 
0055     return 0;
0056 }
0057 
0058 static void __exit cleanup_soundcore(void)
0059 {
0060     cleanup_oss_soundcore();
0061     class_destroy(sound_class);
0062 }
0063 
0064 subsys_initcall(init_soundcore);
0065 module_exit(cleanup_soundcore);
0066 
0067 
0068 #ifdef CONFIG_SOUND_OSS_CORE
0069 /*
0070  *  OSS sound core handling. Breaks out sound functions to submodules
0071  *  
0072  *  Author:     Alan Cox <alan@lxorguk.ukuu.org.uk>
0073  *
0074  *  Fixes:
0075  *
0076  *
0077  *  This program is free software; you can redistribute it and/or
0078  *  modify it under the terms of the GNU General Public License
0079  *  as published by the Free Software Foundation; either version
0080  *  2 of the License, or (at your option) any later version.
0081  *
0082  *                         --------------------
0083  * 
0084  *  Top level handler for the sound subsystem. Various devices can
0085  *  plug into this. The fact they don't all go via OSS doesn't mean 
0086  *  they don't have to implement the OSS API. There is a lot of logic
0087  *  to keeping much of the OSS weight out of the code in a compatibility
0088  *  module, but it's up to the driver to rember to load it...
0089  *
0090  *  The code provides a set of functions for registration of devices
0091  *  by type. This is done rather than providing a single call so that
0092  *  we can hide any future changes in the internals (eg when we go to
0093  *  32bit dev_t) from the modules and their interface.
0094  *
0095  *  Secondly we need to allocate the dsp, dsp16 and audio devices as
0096  *  one. Thus we misuse the chains a bit to simplify this.
0097  *
0098  *  Thirdly to make it more fun and for 2.3.x and above we do all
0099  *  of this using fine grained locking.
0100  *
0101  *  FIXME: we have to resolve modules and fine grained load/unload
0102  *  locking at some point in 2.3.x.
0103  */
0104 
0105 #include <linux/init.h>
0106 #include <linux/slab.h>
0107 #include <linux/types.h>
0108 #include <linux/kernel.h>
0109 #include <linux/sound.h>
0110 #include <linux/kmod.h>
0111 
0112 #define SOUND_STEP 16
0113 
0114 struct sound_unit
0115 {
0116     int unit_minor;
0117     const struct file_operations *unit_fops;
0118     struct sound_unit *next;
0119     char name[32];
0120 };
0121 
0122 #ifdef CONFIG_SOUND_MSNDCLAS
0123 extern int msnd_classic_init(void);
0124 #endif
0125 #ifdef CONFIG_SOUND_MSNDPIN
0126 extern int msnd_pinnacle_init(void);
0127 #endif
0128 
0129 /*
0130  * By default, OSS sound_core claims full legacy minor range (0-255)
0131  * of SOUND_MAJOR to trap open attempts to any sound minor and
0132  * requests modules using custom sound-slot/service-* module aliases.
0133  * The only benefit of doing this is allowing use of custom module
0134  * aliases instead of the standard char-major-* ones.  This behavior
0135  * prevents alternative OSS implementation and is scheduled to be
0136  * removed.
0137  *
0138  * CONFIG_SOUND_OSS_CORE_PRECLAIM and soundcore.preclaim_oss kernel
0139  * parameter are added to allow distros and developers to try and
0140  * switch to alternative implementations without needing to rebuild
0141  * the kernel in the meantime.  If preclaim_oss is non-zero, the
0142  * kernel will behave the same as before.  All SOUND_MAJOR minors are
0143  * preclaimed and the custom module aliases along with standard chrdev
0144  * ones are emitted if a missing device is opened.  If preclaim_oss is
0145  * zero, sound_core only grabs what's actually in use and for missing
0146  * devices only the standard chrdev aliases are requested.
0147  *
0148  * All these clutters are scheduled to be removed along with
0149  * sound-slot/service-* module aliases.
0150  */
0151 #ifdef CONFIG_SOUND_OSS_CORE_PRECLAIM
0152 static int preclaim_oss = 1;
0153 #else
0154 static int preclaim_oss = 0;
0155 #endif
0156 
0157 module_param(preclaim_oss, int, 0444);
0158 
0159 static int soundcore_open(struct inode *, struct file *);
0160 
0161 static const struct file_operations soundcore_fops =
0162 {
0163     /* We must have an owner or the module locking fails */
0164     .owner  = THIS_MODULE,
0165     .open   = soundcore_open,
0166     .llseek = noop_llseek,
0167 };
0168 
0169 /*
0170  *  Low level list operator. Scan the ordered list, find a hole and
0171  *  join into it. Called with the lock asserted
0172  */
0173 
0174 static int __sound_insert_unit(struct sound_unit * s, struct sound_unit **list, const struct file_operations *fops, int index, int low, int top)
0175 {
0176     int n=low;
0177 
0178     if (index < 0) {    /* first free */
0179 
0180         while (*list && (*list)->unit_minor<n)
0181             list=&((*list)->next);
0182 
0183         while(n<top)
0184         {
0185             /* Found a hole ? */
0186             if(*list==NULL || (*list)->unit_minor>n)
0187                 break;
0188             list=&((*list)->next);
0189             n+=SOUND_STEP;
0190         }
0191 
0192         if(n>=top)
0193             return -ENOENT;
0194     } else {
0195         n = low+(index*16);
0196         while (*list) {
0197             if ((*list)->unit_minor==n)
0198                 return -EBUSY;
0199             if ((*list)->unit_minor>n)
0200                 break;
0201             list=&((*list)->next);
0202         }
0203     }   
0204         
0205     /*
0206      *  Fill it in
0207      */
0208      
0209     s->unit_minor=n;
0210     s->unit_fops=fops;
0211     
0212     /*
0213      *  Link it
0214      */
0215      
0216     s->next=*list;
0217     *list=s;
0218     
0219     
0220     return n;
0221 }
0222 
0223 /*
0224  *  Remove a node from the chain. Called with the lock asserted
0225  */
0226  
0227 static struct sound_unit *__sound_remove_unit(struct sound_unit **list, int unit)
0228 {
0229     while(*list)
0230     {
0231         struct sound_unit *p=*list;
0232         if(p->unit_minor==unit)
0233         {
0234             *list=p->next;
0235             return p;
0236         }
0237         list=&(p->next);
0238     }
0239     printk(KERN_ERR "Sound device %d went missing!\n", unit);
0240     return NULL;
0241 }
0242 
0243 /*
0244  *  This lock guards the sound loader list.
0245  */
0246 
0247 static DEFINE_SPINLOCK(sound_loader_lock);
0248 
0249 /*
0250  *  Allocate the controlling structure and add it to the sound driver
0251  *  list. Acquires locks as needed
0252  */
0253 
0254 static int sound_insert_unit(struct sound_unit **list, const struct file_operations *fops, int index, int low, int top, const char *name, umode_t mode, struct device *dev)
0255 {
0256     struct sound_unit *s = kmalloc(sizeof(*s), GFP_KERNEL);
0257     int r;
0258 
0259     if (!s)
0260         return -ENOMEM;
0261 
0262     spin_lock(&sound_loader_lock);
0263 retry:
0264     r = __sound_insert_unit(s, list, fops, index, low, top);
0265     spin_unlock(&sound_loader_lock);
0266     
0267     if (r < 0)
0268         goto fail;
0269     else if (r < SOUND_STEP)
0270         sprintf(s->name, "sound/%s", name);
0271     else
0272         sprintf(s->name, "sound/%s%d", name, r / SOUND_STEP);
0273 
0274     if (!preclaim_oss) {
0275         /*
0276          * Something else might have grabbed the minor.  If
0277          * first free slot is requested, rescan with @low set
0278          * to the next unit; otherwise, -EBUSY.
0279          */
0280         r = __register_chrdev(SOUND_MAJOR, s->unit_minor, 1, s->name,
0281                       &soundcore_fops);
0282         if (r < 0) {
0283             spin_lock(&sound_loader_lock);
0284             __sound_remove_unit(list, s->unit_minor);
0285             if (index < 0) {
0286                 low = s->unit_minor + SOUND_STEP;
0287                 goto retry;
0288             }
0289             spin_unlock(&sound_loader_lock);
0290             return -EBUSY;
0291         }
0292     }
0293 
0294     device_create(sound_class, dev, MKDEV(SOUND_MAJOR, s->unit_minor),
0295               NULL, "%s", s->name+6);
0296     return s->unit_minor;
0297 
0298 fail:
0299     kfree(s);
0300     return r;
0301 }
0302 
0303 /*
0304  *  Remove a unit. Acquires locks as needed. The drivers MUST have
0305  *  completed the removal before their file operations become
0306  *  invalid.
0307  */
0308     
0309 static void sound_remove_unit(struct sound_unit **list, int unit)
0310 {
0311     struct sound_unit *p;
0312 
0313     spin_lock(&sound_loader_lock);
0314     p = __sound_remove_unit(list, unit);
0315     spin_unlock(&sound_loader_lock);
0316     if (p) {
0317         if (!preclaim_oss)
0318             __unregister_chrdev(SOUND_MAJOR, p->unit_minor, 1,
0319                         p->name);
0320         device_destroy(sound_class, MKDEV(SOUND_MAJOR, p->unit_minor));
0321         kfree(p);
0322     }
0323 }
0324 
0325 /*
0326  *  Allocations
0327  *
0328  *  0   *16     Mixers
0329  *  1   *8      Sequencers
0330  *  2   *16     Midi
0331  *  3   *16     DSP
0332  *  4   *16     SunDSP
0333  *  5   *16     DSP16
0334  *  6   --      sndstat (obsolete)
0335  *  7   *16     unused
0336  *  8   --      alternate sequencer (see above)
0337  *  9   *16     raw synthesizer access
0338  *  10  *16     unused
0339  *  11  *16     unused
0340  *  12  *16     unused
0341  *  13  *16     unused
0342  *  14  *16     unused
0343  *  15  *16     unused
0344  */
0345 
0346 static struct sound_unit *chains[SOUND_STEP];
0347 
0348 /**
0349  *  register_sound_special_device - register a special sound node
0350  *  @fops: File operations for the driver
0351  *  @unit: Unit number to allocate
0352  *      @dev: device pointer
0353  *
0354  *  Allocate a special sound device by minor number from the sound
0355  *  subsystem.
0356  *
0357  *  Return: The allocated number is returned on success. On failure,
0358  *  a negative error code is returned.
0359  */
0360  
0361 int register_sound_special_device(const struct file_operations *fops, int unit,
0362                   struct device *dev)
0363 {
0364     const int chain = unit % SOUND_STEP;
0365     int max_unit = 256;
0366     const char *name;
0367     char _name[16];
0368 
0369     switch (chain) {
0370         case 0:
0371         name = "mixer";
0372         break;
0373         case 1:
0374         name = "sequencer";
0375         if (unit >= SOUND_STEP)
0376             goto __unknown;
0377         max_unit = unit + 1;
0378         break;
0379         case 2:
0380         name = "midi";
0381         break;
0382         case 3:
0383         name = "dsp";
0384         break;
0385         case 4:
0386         name = "audio";
0387         break;
0388         case 5:
0389         name = "dspW";
0390         break;
0391         case 8:
0392         name = "sequencer2";
0393         if (unit >= SOUND_STEP)
0394             goto __unknown;
0395         max_unit = unit + 1;
0396         break;
0397         case 9:
0398         name = "dmmidi";
0399         break;
0400         case 10:
0401         name = "dmfm";
0402         break;
0403         case 12:
0404         name = "adsp";
0405         break;
0406         case 13:
0407         name = "amidi";
0408         break;
0409         case 14:
0410         name = "admmidi";
0411         break;
0412         default:
0413             {
0414             __unknown:
0415             sprintf(_name, "unknown%d", chain);
0416                 if (unit >= SOUND_STEP)
0417                     strcat(_name, "-");
0418                 name = _name;
0419         }
0420         break;
0421     }
0422     return sound_insert_unit(&chains[chain], fops, -1, unit, max_unit,
0423                  name, S_IRUSR | S_IWUSR, dev);
0424 }
0425  
0426 EXPORT_SYMBOL(register_sound_special_device);
0427 
0428 int register_sound_special(const struct file_operations *fops, int unit)
0429 {
0430     return register_sound_special_device(fops, unit, NULL);
0431 }
0432 
0433 EXPORT_SYMBOL(register_sound_special);
0434 
0435 /**
0436  *  register_sound_mixer - register a mixer device
0437  *  @fops: File operations for the driver
0438  *  @dev: Unit number to allocate
0439  *
0440  *  Allocate a mixer device. Unit is the number of the mixer requested.
0441  *  Pass -1 to request the next free mixer unit.
0442  *
0443  *  Return: On success, the allocated number is returned. On failure,
0444  *  a negative error code is returned.
0445  */
0446 
0447 int register_sound_mixer(const struct file_operations *fops, int dev)
0448 {
0449     return sound_insert_unit(&chains[0], fops, dev, 0, 128,
0450                  "mixer", S_IRUSR | S_IWUSR, NULL);
0451 }
0452 
0453 EXPORT_SYMBOL(register_sound_mixer);
0454 
0455 /**
0456  *  register_sound_midi - register a midi device
0457  *  @fops: File operations for the driver
0458  *  @dev: Unit number to allocate
0459  *
0460  *  Allocate a midi device. Unit is the number of the midi device requested.
0461  *  Pass -1 to request the next free midi unit.
0462  *
0463  *  Return: On success, the allocated number is returned. On failure,
0464  *  a negative error code is returned.
0465  */
0466 
0467 int register_sound_midi(const struct file_operations *fops, int dev)
0468 {
0469     return sound_insert_unit(&chains[2], fops, dev, 2, 130,
0470                  "midi", S_IRUSR | S_IWUSR, NULL);
0471 }
0472 
0473 EXPORT_SYMBOL(register_sound_midi);
0474 
0475 /*
0476  *  DSP's are registered as a triple. Register only one and cheat
0477  *  in open - see below.
0478  */
0479  
0480 /**
0481  *  register_sound_dsp - register a DSP device
0482  *  @fops: File operations for the driver
0483  *  @dev: Unit number to allocate
0484  *
0485  *  Allocate a DSP device. Unit is the number of the DSP requested.
0486  *  Pass -1 to request the next free DSP unit.
0487  *
0488  *  This function allocates both the audio and dsp device entries together
0489  *  and will always allocate them as a matching pair - eg dsp3/audio3
0490  *
0491  *  Return: On success, the allocated number is returned. On failure,
0492  *  a negative error code is returned.
0493  */
0494 
0495 int register_sound_dsp(const struct file_operations *fops, int dev)
0496 {
0497     return sound_insert_unit(&chains[3], fops, dev, 3, 131,
0498                  "dsp", S_IWUSR | S_IRUSR, NULL);
0499 }
0500 
0501 EXPORT_SYMBOL(register_sound_dsp);
0502 
0503 /**
0504  *  unregister_sound_special - unregister a special sound device
0505  *  @unit: unit number to allocate
0506  *
0507  *  Release a sound device that was allocated with
0508  *  register_sound_special(). The unit passed is the return value from
0509  *  the register function.
0510  */
0511 
0512 
0513 void unregister_sound_special(int unit)
0514 {
0515     sound_remove_unit(&chains[unit % SOUND_STEP], unit);
0516 }
0517  
0518 EXPORT_SYMBOL(unregister_sound_special);
0519 
0520 /**
0521  *  unregister_sound_mixer - unregister a mixer
0522  *  @unit: unit number to allocate
0523  *
0524  *  Release a sound device that was allocated with register_sound_mixer().
0525  *  The unit passed is the return value from the register function.
0526  */
0527 
0528 void unregister_sound_mixer(int unit)
0529 {
0530     sound_remove_unit(&chains[0], unit);
0531 }
0532 
0533 EXPORT_SYMBOL(unregister_sound_mixer);
0534 
0535 /**
0536  *  unregister_sound_midi - unregister a midi device
0537  *  @unit: unit number to allocate
0538  *
0539  *  Release a sound device that was allocated with register_sound_midi().
0540  *  The unit passed is the return value from the register function.
0541  */
0542 
0543 void unregister_sound_midi(int unit)
0544 {
0545     sound_remove_unit(&chains[2], unit);
0546 }
0547 
0548 EXPORT_SYMBOL(unregister_sound_midi);
0549 
0550 /**
0551  *  unregister_sound_dsp - unregister a DSP device
0552  *  @unit: unit number to allocate
0553  *
0554  *  Release a sound device that was allocated with register_sound_dsp().
0555  *  The unit passed is the return value from the register function.
0556  *
0557  *  Both of the allocated units are released together automatically.
0558  */
0559 
0560 void unregister_sound_dsp(int unit)
0561 {
0562     sound_remove_unit(&chains[3], unit);
0563 }
0564 
0565 
0566 EXPORT_SYMBOL(unregister_sound_dsp);
0567 
0568 static struct sound_unit *__look_for_unit(int chain, int unit)
0569 {
0570     struct sound_unit *s;
0571     
0572     s=chains[chain];
0573     while(s && s->unit_minor <= unit)
0574     {
0575         if(s->unit_minor==unit)
0576             return s;
0577         s=s->next;
0578     }
0579     return NULL;
0580 }
0581 
0582 static int soundcore_open(struct inode *inode, struct file *file)
0583 {
0584     int chain;
0585     int unit = iminor(inode);
0586     struct sound_unit *s;
0587     const struct file_operations *new_fops = NULL;
0588 
0589     chain=unit&0x0F;
0590     if(chain==4 || chain==5)    /* dsp/audio/dsp16 */
0591     {
0592         unit&=0xF0;
0593         unit|=3;
0594         chain=3;
0595     }
0596     
0597     spin_lock(&sound_loader_lock);
0598     s = __look_for_unit(chain, unit);
0599     if (s)
0600         new_fops = fops_get(s->unit_fops);
0601     if (preclaim_oss && !new_fops) {
0602         spin_unlock(&sound_loader_lock);
0603 
0604         /*
0605          *  Please, don't change this order or code.
0606          *  For ALSA slot means soundcard and OSS emulation code
0607          *  comes as add-on modules which aren't depend on
0608          *  ALSA toplevel modules for soundcards, thus we need
0609          *  load them at first.   [Jaroslav Kysela <perex@jcu.cz>]
0610          */
0611         request_module("sound-slot-%i", unit>>4);
0612         request_module("sound-service-%i-%i", unit>>4, chain);
0613 
0614         /*
0615          * sound-slot/service-* module aliases are scheduled
0616          * for removal in favor of the standard char-major-*
0617          * module aliases.  For the time being, generate both
0618          * the legacy and standard module aliases to ease
0619          * transition.
0620          */
0621         if (request_module("char-major-%d-%d", SOUND_MAJOR, unit) > 0)
0622             request_module("char-major-%d", SOUND_MAJOR);
0623 
0624         spin_lock(&sound_loader_lock);
0625         s = __look_for_unit(chain, unit);
0626         if (s)
0627             new_fops = fops_get(s->unit_fops);
0628     }
0629     spin_unlock(&sound_loader_lock);
0630     if (new_fops) {
0631         /*
0632          * We rely upon the fact that we can't be unloaded while the
0633          * subdriver is there.
0634          */
0635         int err = 0;
0636         replace_fops(file, new_fops);
0637 
0638         if (file->f_op->open)
0639             err = file->f_op->open(inode,file);
0640 
0641         return err;
0642     }
0643     return -ENODEV;
0644 }
0645 
0646 MODULE_ALIAS_CHARDEV_MAJOR(SOUND_MAJOR);
0647 
0648 static void cleanup_oss_soundcore(void)
0649 {
0650     /* We have nothing to really do here - we know the lists must be
0651        empty */
0652     unregister_chrdev(SOUND_MAJOR, "sound");
0653 }
0654 
0655 static int __init init_oss_soundcore(void)
0656 {
0657     if (preclaim_oss &&
0658         register_chrdev(SOUND_MAJOR, "sound", &soundcore_fops) < 0) {
0659         printk(KERN_ERR "soundcore: sound device already in use.\n");
0660         return -EBUSY;
0661     }
0662 
0663     return 0;
0664 }
0665 
0666 #endif /* CONFIG_SOUND_OSS_CORE */