xref: /OK3568_Linux_fs/kernel/sound/drivers/opl4/opl4_proc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Functions for the OPL4 proc file
4*4882a593Smuzhiyun  * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "opl4_local.h"
8*4882a593Smuzhiyun #include <linux/vmalloc.h>
9*4882a593Smuzhiyun #include <linux/export.h>
10*4882a593Smuzhiyun #include <sound/info.h>
11*4882a593Smuzhiyun 
snd_opl4_mem_proc_open(struct snd_info_entry * entry,unsigned short mode,void ** file_private_data)12*4882a593Smuzhiyun static int snd_opl4_mem_proc_open(struct snd_info_entry *entry,
13*4882a593Smuzhiyun 				  unsigned short mode, void **file_private_data)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = entry->private_data;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun 	mutex_lock(&opl4->access_mutex);
18*4882a593Smuzhiyun 	if (opl4->memory_access) {
19*4882a593Smuzhiyun 		mutex_unlock(&opl4->access_mutex);
20*4882a593Smuzhiyun 		return -EBUSY;
21*4882a593Smuzhiyun 	}
22*4882a593Smuzhiyun 	opl4->memory_access++;
23*4882a593Smuzhiyun 	mutex_unlock(&opl4->access_mutex);
24*4882a593Smuzhiyun 	return 0;
25*4882a593Smuzhiyun }
26*4882a593Smuzhiyun 
snd_opl4_mem_proc_release(struct snd_info_entry * entry,unsigned short mode,void * file_private_data)27*4882a593Smuzhiyun static int snd_opl4_mem_proc_release(struct snd_info_entry *entry,
28*4882a593Smuzhiyun 				     unsigned short mode, void *file_private_data)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = entry->private_data;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	mutex_lock(&opl4->access_mutex);
33*4882a593Smuzhiyun 	opl4->memory_access--;
34*4882a593Smuzhiyun 	mutex_unlock(&opl4->access_mutex);
35*4882a593Smuzhiyun 	return 0;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
snd_opl4_mem_proc_read(struct snd_info_entry * entry,void * file_private_data,struct file * file,char __user * _buf,size_t count,loff_t pos)38*4882a593Smuzhiyun static ssize_t snd_opl4_mem_proc_read(struct snd_info_entry *entry,
39*4882a593Smuzhiyun 				      void *file_private_data,
40*4882a593Smuzhiyun 				      struct file *file, char __user *_buf,
41*4882a593Smuzhiyun 				      size_t count, loff_t pos)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = entry->private_data;
44*4882a593Smuzhiyun 	char* buf;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	buf = vmalloc(count);
47*4882a593Smuzhiyun 	if (!buf)
48*4882a593Smuzhiyun 		return -ENOMEM;
49*4882a593Smuzhiyun 	snd_opl4_read_memory(opl4, buf, pos, count);
50*4882a593Smuzhiyun 	if (copy_to_user(_buf, buf, count)) {
51*4882a593Smuzhiyun 		vfree(buf);
52*4882a593Smuzhiyun 		return -EFAULT;
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun 	vfree(buf);
55*4882a593Smuzhiyun 	return count;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun 
snd_opl4_mem_proc_write(struct snd_info_entry * entry,void * file_private_data,struct file * file,const char __user * _buf,size_t count,loff_t pos)58*4882a593Smuzhiyun static ssize_t snd_opl4_mem_proc_write(struct snd_info_entry *entry,
59*4882a593Smuzhiyun 				       void *file_private_data,
60*4882a593Smuzhiyun 				       struct file *file,
61*4882a593Smuzhiyun 				       const char __user *_buf,
62*4882a593Smuzhiyun 				       size_t count, loff_t pos)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct snd_opl4 *opl4 = entry->private_data;
65*4882a593Smuzhiyun 	char *buf;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	buf = vmalloc(count);
68*4882a593Smuzhiyun 	if (!buf)
69*4882a593Smuzhiyun 		return -ENOMEM;
70*4882a593Smuzhiyun 	if (copy_from_user(buf, _buf, count)) {
71*4882a593Smuzhiyun 		vfree(buf);
72*4882a593Smuzhiyun 		return -EFAULT;
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 	snd_opl4_write_memory(opl4, buf, pos, count);
75*4882a593Smuzhiyun 	vfree(buf);
76*4882a593Smuzhiyun 	return count;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun static const struct snd_info_entry_ops snd_opl4_mem_proc_ops = {
80*4882a593Smuzhiyun 	.open = snd_opl4_mem_proc_open,
81*4882a593Smuzhiyun 	.release = snd_opl4_mem_proc_release,
82*4882a593Smuzhiyun 	.read = snd_opl4_mem_proc_read,
83*4882a593Smuzhiyun 	.write = snd_opl4_mem_proc_write,
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
snd_opl4_create_proc(struct snd_opl4 * opl4)86*4882a593Smuzhiyun int snd_opl4_create_proc(struct snd_opl4 *opl4)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct snd_info_entry *entry;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	entry = snd_info_create_card_entry(opl4->card, "opl4-mem", opl4->card->proc_root);
91*4882a593Smuzhiyun 	if (entry) {
92*4882a593Smuzhiyun 		if (opl4->hardware < OPL3_HW_OPL4_ML) {
93*4882a593Smuzhiyun 			/* OPL4 can access 4 MB external ROM/SRAM */
94*4882a593Smuzhiyun 			entry->mode |= 0200;
95*4882a593Smuzhiyun 			entry->size = 4 * 1024 * 1024;
96*4882a593Smuzhiyun 		} else {
97*4882a593Smuzhiyun 			/* OPL4-ML has 1 MB internal ROM */
98*4882a593Smuzhiyun 			entry->size = 1 * 1024 * 1024;
99*4882a593Smuzhiyun 		}
100*4882a593Smuzhiyun 		entry->content = SNDRV_INFO_CONTENT_DATA;
101*4882a593Smuzhiyun 		entry->c.ops = &snd_opl4_mem_proc_ops;
102*4882a593Smuzhiyun 		entry->module = THIS_MODULE;
103*4882a593Smuzhiyun 		entry->private_data = opl4;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 	opl4->proc_entry = entry;
106*4882a593Smuzhiyun 	return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
snd_opl4_free_proc(struct snd_opl4 * opl4)109*4882a593Smuzhiyun void snd_opl4_free_proc(struct snd_opl4 *opl4)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	snd_info_free_entry(opl4->proc_entry);
112*4882a593Smuzhiyun }
113