Enscript Output

extractedLnx/linux/drivers/sound/emu10k1/audio.c_emu10k1_audio_ioctl.c

static int emu10k1_audio_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct emu10k1_wavedevice *wave_dev = (struct emu10k1_wavedevice *) file->private_data;
	struct woinst *woinst = NULL;
	struct wiinst *wiinst = NULL;
	int val = 0;
	u32 bytestocopy;
	unsigned long flags;

	DPF(4, "emu10k1_audio_ioctl()\n");

	if (file->f_mode & FMODE_WRITE)
		woinst = wave_dev->woinst;

	if (file->f_mode & FMODE_READ)
		wiinst = wave_dev->wiinst;

	switch (cmd) {
	case OSS_GETVERSION:
		DPF(2, "OSS_GETVERSION:\n");
		return put_user(SOUND_VERSION, (int *) arg);

	case SNDCTL_DSP_RESET:
		DPF(2, "SNDCTL_DSP_RESET:\n");
		wave_dev->enablebits = PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT;

		if (file->f_mode & FMODE_WRITE) {
			spin_lock_irqsave(&woinst->lock, flags);

			if (woinst->state & WAVE_STATE_OPEN) {
				if (woinst->mmapped) {
					int i;

					/* Undo marking the pages as reserved */
					for (i = 0; i < woinst->buffer.pages; i++)
						mem_map_unreserve(virt_to_page(woinst->buffer.addr[i]));
				}

				emu10k1_waveout_close(wave_dev);
			}

			woinst->mmapped = 0;
			woinst->total_copied = 0;
			woinst->total_played = 0;
			woinst->blocks = 0;

			spin_unlock_irqrestore(&woinst->lock, flags);
		}

		if (file->f_mode & FMODE_READ) {
			spin_lock_irqsave(&wiinst->lock, flags);

			if (wiinst->state & WAVE_STATE_OPEN)
				emu10k1_wavein_close(wave_dev);

			wiinst->mmapped = 0;
			wiinst->total_recorded = 0;
			wiinst->blocks = 0;
			spin_unlock_irqrestore(&wiinst->lock, flags);
		}

		break;

	case SNDCTL_DSP_SYNC:
		DPF(2, "SNDCTL_DSP_SYNC:\n");

		if (file->f_mode & FMODE_WRITE) {

			spin_lock_irqsave(&woinst->lock, flags);

			if (woinst->state & WAVE_STATE_OPEN) {

				if (woinst->state & WAVE_STATE_STARTED)
					while ((woinst->total_played < woinst->total_copied)
					       && !signal_pending(current)) {
						spin_unlock_irqrestore(&woinst->lock, flags);
						interruptible_sleep_on(&woinst->wait_queue);
						spin_lock_irqsave(&woinst->lock, flags);
					}

				if (woinst->mmapped) {
					int i;

					/* Undo marking the pages as reserved */
					for (i = 0; i < woinst->buffer.pages; i++)
						mem_map_unreserve(virt_to_page(woinst->buffer.addr[i]));
				}

				emu10k1_waveout_close(wave_dev);
			}

			woinst->mmapped = 0;
			woinst->total_copied = 0;
			woinst->total_played = 0;
			woinst->blocks = 0;

			spin_unlock_irqrestore(&woinst->lock, flags);
		}

		if (file->f_mode & FMODE_READ) {
			spin_lock_irqsave(&wiinst->lock, flags);

			if (wiinst->state & WAVE_STATE_OPEN)
				emu10k1_wavein_close(wave_dev);

			wiinst->mmapped = 0;
			wiinst->total_recorded = 0;
			wiinst->blocks = 0;
			spin_unlock_irqrestore(&wiinst->lock, flags);
		}

		break;

	case SNDCTL_DSP_SETDUPLEX:
		DPF(2, "SNDCTL_DSP_SETDUPLEX:\n");
		break;

	case SNDCTL_DSP_GETCAPS:
		DPF(2, "SNDCTL_DSP_GETCAPS:\n");
		return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME | DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_COPROC, (int *) arg);

	case SNDCTL_DSP_SPEED:
		DPF(2, "SNDCTL_DSP_SPEED:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		DPD(2, "val is %d\n", val);

		if (val > 0) {
			if (file->f_mode & FMODE_READ) {
				struct wave_format format;

				spin_lock_irqsave(&wiinst->lock, flags);

				format = wiinst->format;
				format.samplingrate = val;

				if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&wiinst->lock, flags);
					return -EINVAL;
				}

				val = wiinst->format.samplingrate;

				spin_unlock_irqrestore(&wiinst->lock, flags);

				DPD(2, "set recording sampling rate -> %d\n", val);
			}

			if (file->f_mode & FMODE_WRITE) {
				struct wave_format format;

				spin_lock_irqsave(&woinst->lock, flags);

				format = woinst->format;
				format.samplingrate = val;

				if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&woinst->lock, flags);
					return -EINVAL;
				}

				val = woinst->format.samplingrate;

				spin_unlock_irqrestore(&woinst->lock, flags);

				DPD(2, "set playback sampling rate -> %d\n", val);
			}

			return put_user(val, (int *) arg);
		} else {
			if (file->f_mode & FMODE_READ)
				val = wiinst->format.samplingrate;
			else if (file->f_mode & FMODE_WRITE)
				val = woinst->format.samplingrate;

			return put_user(val, (int *) arg);
		}
		break;

	case SNDCTL_DSP_STEREO:
		DPF(2, "SNDCTL_DSP_STEREO:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		DPD(2, " val is %d\n", val);

		if (file->f_mode & FMODE_READ) {
			struct wave_format format;

			spin_lock_irqsave(&wiinst->lock, flags);

			format = wiinst->format;
			format.channels = val ? 2 : 1;

			if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
				spin_unlock_irqrestore(&wiinst->lock, flags);
				return -EINVAL;
			}

			val = wiinst->format.channels - 1;

			spin_unlock_irqrestore(&wiinst->lock, flags);
			DPD(2, "set recording stereo -> %d\n", val);
		}

		if (file->f_mode & FMODE_WRITE) {
			struct wave_format format;

			spin_lock_irqsave(&woinst->lock, flags);

			format = woinst->format;
			format.channels = val ? 2 : 1;

			if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
				spin_unlock_irqrestore(&woinst->lock, flags);
				return -EINVAL;
			}

			val = woinst->format.channels - 1;

			spin_unlock_irqrestore(&woinst->lock, flags);

			DPD(2, "set playback stereo -> %d\n", val);
		}

		return put_user(val, (int *) arg);

		break;

	case SNDCTL_DSP_CHANNELS:
		DPF(2, "SNDCTL_DSP_CHANNELS:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		DPD(2, " val is %d\n", val);

		if (val > 0) {
			if (file->f_mode & FMODE_READ) {
				struct wave_format format;

				spin_lock_irqsave(&wiinst->lock, flags);

				format = wiinst->format;
				format.channels = val;

				if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&wiinst->lock, flags);
					return -EINVAL;
				}

				val = wiinst->format.channels;

				spin_unlock_irqrestore(&wiinst->lock, flags);
				DPD(2, "set recording number of channels -> %d\n", val);
			}

			if (file->f_mode & FMODE_WRITE) {
				struct wave_format format;

				spin_lock_irqsave(&woinst->lock, flags);

				format = woinst->format;
				format.channels = val;

				if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&woinst->lock, flags);
					return -EINVAL;
				}

				val = woinst->format.channels;

				spin_unlock_irqrestore(&woinst->lock, flags);
				DPD(2, "set playback number of channels -> %d\n", val);
			}

			return put_user(val, (int *) arg);
		} else {
			if (file->f_mode & FMODE_READ)
				val = wiinst->format.channels;
			else if (file->f_mode & FMODE_WRITE)
				val = woinst->format.channels;

			return put_user(val, (int *) arg);
		}
		break;

	case SNDCTL_DSP_GETFMTS:
		DPF(2, "SNDCTL_DSP_GETFMTS:\n");

		if (file->f_mode & FMODE_READ)
			val = AFMT_S16_LE;
		else if (file->f_mode & FMODE_WRITE)
			val = AFMT_S16_LE | AFMT_U8;

		return put_user(val, (int *) arg);

	case SNDCTL_DSP_SETFMT:	/* Same as SNDCTL_DSP_SAMPLESIZE */
		DPF(2, "SNDCTL_DSP_SETFMT:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		DPD(2, " val is %d\n", val);

		if (val != AFMT_QUERY) {
			if (file->f_mode & FMODE_READ) {
				struct wave_format format;

				spin_lock_irqsave(&wiinst->lock, flags);

				format = wiinst->format;
				format.bitsperchannel = val;

				if (emu10k1_wavein_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&wiinst->lock, flags);
					return -EINVAL;
				}

				val = wiinst->format.bitsperchannel;

				spin_unlock_irqrestore(&wiinst->lock, flags);
				DPD(2, "set recording sample size -> %d\n", val);
			}

			if (file->f_mode & FMODE_WRITE) {
				struct wave_format format;

				spin_lock_irqsave(&woinst->lock, flags);

				format = woinst->format;
				format.bitsperchannel = val;

				if (emu10k1_waveout_setformat(wave_dev, &format) < 0) {
					spin_unlock_irqrestore(&woinst->lock, flags);
					return -EINVAL;
				}

				val = woinst->format.bitsperchannel;

				spin_unlock_irqrestore(&woinst->lock, flags);
				DPD(2, "set playback sample size -> %d\n", val);
			}

			return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);
		} else {
			if (file->f_mode & FMODE_READ)
				val = wiinst->format.bitsperchannel;
			else if (file->f_mode & FMODE_WRITE)
				val = woinst->format.bitsperchannel;

			return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);
		}
		break;

	case SOUND_PCM_READ_BITS:

		if (file->f_mode & FMODE_READ)
			val = wiinst->format.bitsperchannel;
		else if (file->f_mode & FMODE_WRITE)
			val = woinst->format.bitsperchannel;

		return put_user((val == 16) ? AFMT_S16_LE : AFMT_U8, (int *) arg);

	case SOUND_PCM_READ_RATE:

		if (file->f_mode & FMODE_READ)
			val = wiinst->format.samplingrate;
		else if (file->f_mode & FMODE_WRITE)
			val = woinst->format.samplingrate;

		return put_user(val, (int *) arg);

	case SOUND_PCM_READ_CHANNELS:

		if (file->f_mode & FMODE_READ)
			val = wiinst->format.channels;
		else if (file->f_mode & FMODE_WRITE)
			val = woinst->format.channels;

		return put_user(val, (int *) arg);

	case SOUND_PCM_WRITE_FILTER:
		DPF(2, "SOUND_PCM_WRITE_FILTER: not implemented\n");
		break;

	case SOUND_PCM_READ_FILTER:
		DPF(2, "SOUND_PCM_READ_FILTER: not implemented\n");
		break;

	case SNDCTL_DSP_SETSYNCRO:
		DPF(2, "SNDCTL_DSP_SETSYNCRO: not implemented\n");
		break;

	case SNDCTL_DSP_GETTRIGGER:
		DPF(2, "SNDCTL_DSP_GETTRIGGER:\n");

		if (file->f_mode & FMODE_WRITE && (wave_dev->enablebits & PCM_ENABLE_OUTPUT))
			val |= PCM_ENABLE_OUTPUT;

		if (file->f_mode & FMODE_READ && (wave_dev->enablebits & PCM_ENABLE_INPUT))
			val |= PCM_ENABLE_INPUT;

		return put_user(val, (int *) arg);

	case SNDCTL_DSP_SETTRIGGER:
		DPF(2, "SNDCTL_DSP_SETTRIGGER:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		if (file->f_mode & FMODE_WRITE) {
			spin_lock_irqsave(&woinst->lock, flags);

			if (val & PCM_ENABLE_OUTPUT) {
				wave_dev->enablebits |= PCM_ENABLE_OUTPUT;
				if (woinst->state & WAVE_STATE_OPEN)
					emu10k1_waveout_start(wave_dev);
			} else {
				wave_dev->enablebits &= ~PCM_ENABLE_OUTPUT;
				if (woinst->state & WAVE_STATE_STARTED)
					emu10k1_waveout_stop(wave_dev);
			}

			spin_unlock_irqrestore(&woinst->lock, flags);
		}

		if (file->f_mode & FMODE_READ) {
			spin_lock_irqsave(&wiinst->lock, flags);

			if (val & PCM_ENABLE_INPUT) {
				wave_dev->enablebits |= PCM_ENABLE_INPUT;
				if (wiinst->state & WAVE_STATE_OPEN)
					emu10k1_wavein_start(wave_dev);
			} else {
				wave_dev->enablebits &= ~PCM_ENABLE_INPUT;
				if (wiinst->state & WAVE_STATE_STARTED)
					emu10k1_wavein_stop(wave_dev);
			}

			spin_unlock_irqrestore(&wiinst->lock, flags);
		}
		break;

	case SNDCTL_DSP_GETOSPACE:
		{
			audio_buf_info info;

			DPF(4, "SNDCTL_DSP_GETOSPACE:\n");

			if (!(file->f_mode & FMODE_WRITE))
				return -EINVAL;

			spin_lock_irqsave(&woinst->lock, flags);

			if (woinst->state & WAVE_STATE_OPEN) {
				emu10k1_waveout_update(woinst);
				emu10k1_waveout_getxfersize(woinst, &bytestocopy);
				info.bytes = bytestocopy;
			} else {
				calculate_ofrag(woinst);
				info.bytes = woinst->buffer.size;
			}
			spin_unlock_irqrestore(&woinst->lock, flags);

			info.fragstotal = woinst->buffer.numfrags;
			info.fragments = info.bytes / woinst->buffer.fragment_size;
			info.fragsize = woinst->buffer.fragment_size;

			if (copy_to_user((int *) arg, &info, sizeof(info)))
				return -EFAULT;
		}
		break;

	case SNDCTL_DSP_GETISPACE:
		{
			audio_buf_info info;

			DPF(4, "SNDCTL_DSP_GETISPACE:\n");

			if (!(file->f_mode & FMODE_READ))
				return -EINVAL;

			spin_lock_irqsave(&wiinst->lock, flags);
			if (wiinst->state & WAVE_STATE_OPEN) {
				emu10k1_wavein_update(wave_dev->card, wiinst);
				emu10k1_wavein_getxfersize(wiinst, &bytestocopy);
				info.bytes = bytestocopy;
			} else {
				calculate_ifrag(wiinst);
				info.bytes = 0;
			}
			spin_unlock_irqrestore(&wiinst->lock, flags);

			info.fragstotal = wiinst->buffer.numfrags;
			info.fragments = info.bytes / wiinst->buffer.fragment_size;
			info.fragsize = wiinst->buffer.fragment_size;

			if (copy_to_user((int *) arg, &info, sizeof(info)))
				return -EFAULT;
		}
		break;

	case SNDCTL_DSP_NONBLOCK:
		DPF(2, "SNDCTL_DSP_NONBLOCK:\n");

		file->f_flags |= O_NONBLOCK;
		break;

	case SNDCTL_DSP_GETODELAY:
		DPF(4, "SNDCTL_DSP_GETODELAY:\n");

		if (!(file->f_mode & FMODE_WRITE))
			return -EINVAL;

		spin_lock_irqsave(&woinst->lock, flags);
		if (woinst->state & WAVE_STATE_OPEN) {
			emu10k1_waveout_update(woinst);
			emu10k1_waveout_getxfersize(woinst, &bytestocopy);
			val = woinst->buffer.size - bytestocopy;
		} else
			val = 0;

		spin_unlock_irqrestore(&woinst->lock, flags);

		return put_user(val, (int *) arg);

	case SNDCTL_DSP_GETIPTR:
		{
			count_info cinfo;

			DPF(4, "SNDCTL_DSP_GETIPTR: \n");

			if (!(file->f_mode & FMODE_READ))
				return -EINVAL;

			spin_lock_irqsave(&wiinst->lock, flags);

			if (wiinst->state & WAVE_STATE_OPEN) {
				emu10k1_wavein_update(wave_dev->card, wiinst);
				cinfo.ptr = wiinst->buffer.hw_pos;
				cinfo.bytes = cinfo.ptr + wiinst->total_recorded - wiinst->total_recorded % wiinst->buffer.size;
				cinfo.blocks = cinfo.bytes / wiinst->buffer.fragment_size - wiinst->blocks;
				wiinst->blocks = cinfo.bytes / wiinst->buffer.fragment_size;
			} else {
				cinfo.ptr = 0;
				cinfo.bytes = 0;
				cinfo.blocks = 0;
			}

			spin_unlock_irqrestore(&wiinst->lock, flags);

			if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
				return -EFAULT;
		}
		break;

	case SNDCTL_DSP_GETOPTR:
		{
			count_info cinfo;

			DPF(4, "SNDCTL_DSP_GETOPTR:\n");

			if (!(file->f_mode & FMODE_WRITE))
				return -EINVAL;

			spin_lock_irqsave(&woinst->lock, flags);

			if (woinst->state & WAVE_STATE_OPEN) {
				emu10k1_waveout_update(woinst);
				cinfo.ptr = woinst->buffer.hw_pos;
				cinfo.bytes = cinfo.ptr + woinst->total_played - woinst->total_played % woinst->buffer.size;
				cinfo.blocks = cinfo.bytes / woinst->buffer.fragment_size - woinst->blocks;
				woinst->blocks = cinfo.bytes / woinst->buffer.fragment_size;
			} else {
				cinfo.ptr = 0;
				cinfo.bytes = 0;
				cinfo.blocks = 0;
			}
			if(woinst->mmapped)
				woinst->buffer.bytestocopy %= woinst->buffer.fragment_size;

			spin_unlock_irqrestore(&woinst->lock, flags);

			if (copy_to_user((void *) arg, &cinfo, sizeof(cinfo)))
				return -EFAULT;
		}
		break;

	case SNDCTL_DSP_GETBLKSIZE:
		DPF(2, "SNDCTL_DSP_GETBLKSIZE:\n");

		if (file->f_mode & FMODE_WRITE) {
			spin_lock_irqsave(&woinst->lock, flags);

			calculate_ofrag(woinst);
			val = woinst->buffer.fragment_size;

			spin_unlock_irqrestore(&woinst->lock, flags);
		}

		if (file->f_mode & FMODE_READ) {
			spin_lock_irqsave(&wiinst->lock, flags);

			calculate_ifrag(wiinst);
			val = wiinst->buffer.fragment_size;

			spin_unlock_irqrestore(&wiinst->lock, flags);
		}

		return put_user(val, (int *) arg);

		break;

	case SNDCTL_DSP_POST:
		if (file->f_mode & FMODE_WRITE) {
			spin_lock_irqsave(&woinst->lock, flags);

			if (!(woinst->state & WAVE_STATE_STARTED)
			    && (wave_dev->enablebits & PCM_ENABLE_OUTPUT)
			    && (woinst->total_copied > 0))
				emu10k1_waveout_start(wave_dev);

			spin_unlock_irqrestore(&woinst->lock, flags);
		}

		break;

	case SNDCTL_DSP_SUBDIVIDE:
		DPF(2, "SNDCTL_DSP_SUBDIVIDE: not implemented\n");
		break;

	case SNDCTL_DSP_SETFRAGMENT:
		DPF(2, "SNDCTL_DSP_SETFRAGMENT:\n");

		if (get_user(val, (int *) arg))
			return -EFAULT;

		DPD(2, "val is 0x%x\n", val);

		if (val == 0)
			return -EIO;

		if (file->f_mode & FMODE_WRITE) {
			if (woinst->state & WAVE_STATE_OPEN)
				return -EINVAL;	/* too late to change */

			woinst->buffer.ossfragshift = val & 0xffff;
			woinst->buffer.numfrags = (val >> 16) & 0xffff;
		}

		if (file->f_mode & FMODE_READ) {
			if (wiinst->state & WAVE_STATE_OPEN)
				return -EINVAL;	/* too late to change */

			wiinst->buffer.ossfragshift = val & 0xffff;
			wiinst->buffer.numfrags = (val >> 16) & 0xffff;
		}

		break;

	case SNDCTL_COPR_LOAD:
		{
			copr_buffer *buf;
			u32 i;
			int ret = -EFAULT;

			DPF(2, "SNDCTL_COPR_LOAD:\n");
			
			buf = kmalloc(sizeof(copr_buffer), GFP_KERNEL);
			if(buf == NULL)
				return -ENOMEM;

			if (copy_from_user(buf, (copr_buffer *) arg, sizeof(buf)))
				goto fail;

			if ((buf->command != 1) && (buf->command != 2))
			{
				ret = -EINVAL;
				goto fail;
			}

			if ((buf->offs < 0x100)
			    || (buf->offs < 0x000)
			    || (buf->offs + buf->len > 0x800) || (buf->len > 1000))
			{
				ret = -EINVAL;
				goto fail;
			}
			if (buf->command == 1) {
				for (i = 0; i < buf->len; i++)
					((u32 *) buf->data)[i] = sblive_readptr(wave_dev->card, buf->offs + i, 0);

				if (copy_to_user((copr_buffer *) arg, buf, sizeof(buf)))
					goto fail;
			} else {
				for (i = 0; i < buf->len; i++)
					sblive_writeptr(wave_dev->card, buf->offs + i, 0, ((u32 *) buf->data)[i]);
			}
			ret = 0;
		fail:			
			kfree(buf);
			return ret;
		}

	default:		/* Default is unrecognized command */
		DPD(2, "default: 0x%x\n", cmd);
		return -EINVAL;
	}
	return 0;
}

Generated by GNU enscript 1.6.4.