huangcm
2025-04-26 bff3d3009b0d3a2be3ed92e4817fcabee9e76955
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (c) 2016 AllWinner Technology Co., Ltd. <www.allwinnertech.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */
 
#include <linux/module.h>
#include <linux/etherdevice.h>
#include <linux/netdevice.h>
#include <net/netlink.h>
#include <net/sock.h>
#include <linux/workqueue.h>
#include <linux/types.h>
 
#define    EVT_MAX_SIZE        256
#define    NETLINK_PCM_SUNXI    31
 
#undef    SUNXI_NETLINK_TEST
 
enum {
   SNDRV_PCM_NETLINK_CLOSE,
   SNDRV_PCM_NETLINK_START,
   SNDRV_PCM_NETLINK_NOUSE,
};
 
#ifdef    SUNXI_NETLINK_TEST
static void sunxi_netlink_period(struct work_struct *ws);
DECLARE_DELAYED_WORK(writer_work, sunxi_netlink_period);
#endif
 
struct evt_entry {
   struct list_head list;
   char evt_data[EVT_MAX_SIZE];
   int size;
};
 
struct xrun_event {
   int ref_cnt;
   struct sock *sock;
   struct list_head evtq;
   spinlock_t evt_lock;
   struct list_head freeq;
   struct work_struct ws;
};
 
struct pcm_netlink_status {
   int pid;
   int status;
   struct mutex mutex_lock;
};
 
static struct xrun_event pcm_event;
static struct pcm_netlink_status pcm_netlink;
 
static void free_event_entry(struct evt_entry *e)
{
   kfree(e);
}
 
static inline struct evt_entry *alloc_evt_entry(void)
{
   return kmalloc(sizeof(struct evt_entry), GFP_ATOMIC);
}
 
static struct evt_entry *get_evt_entry(void)
{
   struct evt_entry *e;
 
   if (list_empty(&pcm_event.freeq))
       e = alloc_evt_entry();
   else {
       e = list_entry(pcm_event.freeq.next, struct evt_entry, list);
       list_del(&e->list);
   }
 
   return e;
}
 
static void put_evt_entry(struct evt_entry *e)
{
   list_add_tail(&e->list, &pcm_event.freeq);
}
 
static int netlink_send(struct sock *sock, int group,
       u16 type, void *msg, int len)
{
   struct sk_buff *skb = NULL;
   struct nlmsghdr *nlh;
   int ret = 0;
 
   skb = nlmsg_new(len, GFP_ATOMIC);
   if (!skb) {
       kfree_skb(skb);
       return -EMSGSIZE;
   }
 
   nlh = nlmsg_put(skb, 0, 0, type, len, 0);
   if (!nlh) {
       kfree_skb(skb);
       return -EMSGSIZE;
   }
   memcpy(nlmsg_data(nlh), msg, len);
 
   NETLINK_CB(skb).portid = 0;
   NETLINK_CB(skb).dst_group = 0;
 
   if (pcm_netlink.status > SNDRV_PCM_NETLINK_CLOSE) {
       ret = netlink_unicast(sock, skb, pcm_netlink.pid, GFP_ATOMIC);
       if (ret < 0)
           pcm_netlink.status = SNDRV_PCM_NETLINK_CLOSE;
   }
   return ret;
}
 
static void _pcm_event_send(struct work_struct *work)
{
   unsigned long flags;
   struct evt_entry *e;
 
   spin_lock_irqsave(&pcm_event.evt_lock, flags);
 
   while (!list_empty(&pcm_event.evtq)) {
       e = list_entry(pcm_event.evtq.next, struct evt_entry, list);
       spin_unlock_irqrestore(&pcm_event.evt_lock, flags);
       netlink_send(pcm_event.sock, 0, 0, e->evt_data, e->size);
       spin_lock_irqsave(&pcm_event.evt_lock, flags);
       list_del(&e->list);
       put_evt_entry(e);
   }
 
   spin_unlock_irqrestore(&pcm_event.evt_lock, flags);
}
 
static void netlink_rcv(struct sk_buff *skb)
{
   struct nlmsghdr *nlh;
   int pid;
 
   nlh = (struct nlmsghdr *)skb->data;
   pid = nlh->nlmsg_pid; /*pid of sending process */
 
   /* user start capture? */
   if (!strncmp(nlmsg_data(nlh), "start", 5)) {
       pcm_netlink.pid = pid;
       pcm_netlink.status = SNDRV_PCM_NETLINK_START;
   }
   if (!strncmp(nlmsg_data(nlh), "close", 5))
       pcm_netlink.status = SNDRV_PCM_NETLINK_CLOSE;
 
#ifdef    SUNXI_NETLINK_TEST
   if (pcm_netlink.status)
       schedule_delayed_work(&writer_work, 5 * HZ);
#endif
}
 
static void printk_convert(struct evt_entry *e, const char *fmt, va_list args)
{
   e->size = vscnprintf(e->evt_data, EVT_MAX_SIZE, fmt, args);
 
   /* mark and strip a trailing newline */
   if (e->size && e->evt_data[e->size - 1] == '\n')
       e->size--;
}
 
void sunxi_netlink_printd(const char *fmt, ...)
{
   va_list args;
   struct evt_entry *e;
   unsigned long flags;
 
   spin_lock_irqsave(&pcm_event.evt_lock, flags);
   e = get_evt_entry();
   if (!e) {
       pr_err("pcm netlink memory not available\n");
       spin_unlock_irqrestore(&pcm_event.evt_lock, flags);
       return;
   }
   va_start(args, fmt);
 
   printk_convert(e, fmt, args);
 
   va_end(args);
 
   list_add_tail(&e->list, &pcm_event.evtq);
   spin_unlock_irqrestore(&pcm_event.evt_lock, flags);
 
   schedule_work(&pcm_event.ws);
}
 
#ifdef    SUNXI_NETLINK_TEST
static void sunxi_netlink_period(struct work_struct *ws)
{
   static int count = 1;
   sunxi_netlink_printd("hello form the pcm netlink test: %d\n", count);
   schedule_delayed_work(&writer_work, msecs_to_jiffies(10));
}
#endif
 
int sunxi_netlink_init(void)
{
   struct netlink_kernel_cfg cfg = {
       .input = netlink_rcv,
   };
 
   if (!pcm_event.ref_cnt) {
       pcm_event.sock = netlink_kernel_create(&init_net,
                   NETLINK_PCM_SUNXI, &cfg);
       if (!pcm_event.sock) {
           pr_err("netlink create failed\n");
           return -EMSGSIZE;
       }
       if (pcm_event.sock) {
           INIT_LIST_HEAD(&pcm_event.evtq);
           INIT_LIST_HEAD(&pcm_event.freeq);
           INIT_WORK(&pcm_event.ws, _pcm_event_send);
           spin_lock_init(&pcm_event.evt_lock);
       }
       pcm_netlink.status = SNDRV_PCM_NETLINK_CLOSE;
   }
 
   if (pcm_event.sock) {
       pcm_event.ref_cnt++;
       pr_debug("Creating PCM netlink successfully\n");
       return 0;
   }
 
   pr_debug("Creating PCM netlink is failed\n");
   return -EBUSY;
}
 
static void sunxi_netlink_exit(void)
{
   struct evt_entry *e, *temp;
   unsigned long flags;
   if (pcm_event.sock && --pcm_event.ref_cnt == 0) {
       spin_lock_irqsave(&pcm_event.evt_lock, flags);
 
       list_for_each_entry_safe(e, temp, &pcm_event.evtq, list) {
           list_del(&e->list);
           free_event_entry(e);
       }
       list_for_each_entry_safe(e, temp, &pcm_event.freeq, list) {
           list_del(&e->list);
           free_event_entry(e);
       }
 
       spin_unlock_irqrestore(&pcm_event.evt_lock, flags);
       netlink_kernel_release(pcm_event.sock);
       pcm_event.sock = NULL;
   }
}
 
module_init(sunxi_netlink_init);
module_exit(sunxi_netlink_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wolfgang huang <huangjinhui@allwinnertech.com>");
MODULE_DESCRIPTION("sunxi netlink interface");
MODULE_ALIAS("sunxi_netlink");