ALSA: seq: Some minor cleanups
cover ALSA: seq: Some minor cleanups
Hi,
while debugging the recent virmidi bug, I've found a few ugly appearance in the sequencer code, so spent a bit time for cleaning it up.
Most of changes are cosmetic, non-functional changes.
Takashi
===
Takashi Iwai (5):
ALSA: seq: Minor cleanup of MIDI event parser helpers
ALSA: seq: Remove dead codes
ALSA: seq: Fix leftovers at probe error path
ALSA: seq: Use no intrruptible mutex_lock
ALSA: seq: Drop unused 64bit division macros
include/sound/seq_midi_event.h | 6 +-- sound/core/seq/oss/seq_oss_midi.c | 2 +- sound/core/seq/seq.c | 33 ++++++------ sound/core/seq/seq_clientmgr.c | 28 +++------- sound/core/seq/seq_info.c | 10 +--- sound/core/seq/seq_info.h | 6 +-- sound/core/seq/seq_memory.c | 12 ----- sound/core/seq/seq_memory.h | 6 --- sound/core/seq/seq_midi.c | 24 ++++----- sound/core/seq/seq_midi_emul.c | 12 ----- sound/core/seq/seq_midi_event.c | 87 +++---------------------------- sound/core/seq/seq_queue.c | 12 +---- sound/core/seq/seq_queue.h | 27 ---------- sound/core/seq/seq_virmidi.c | 37 +------------ 14 files changed, 52 insertions(+), 250 deletions(-)
Signature · 1 line
2.18.0
PATCH 1/5 ALSA: seq: Minor cleanup of MIDI event parser helpers
snd_midi_event_encode_byte() can never fail, and it can return rather true/false. Change the return type to bool, adjust the argument to receive a MIDI byte as unsigned char, and adjust the comment accordingly. This allows callers to drop error checks, which simplifies the code.
Meanwhile, snd_midi_event_encode() helper is used only in seq_midi.c, and it can be better folded into it. This will reduce the total amount of lines in the end.
--- include/sound/seq_midi_event.h | 6 ++--- sound/core/seq/oss/seq_oss_midi.c | 2 +- sound/core/seq/seq_midi.c | 24 ++++++++---------- sound/core/seq/seq_midi_event.c | 42 ++++++------------------------- sound/core/seq/seq_virmidi.c | 4 +-- 5 files changed, 22 insertions(+), 56 deletions(-)
include/sound/seq_midi_event.h +2 -4
@@ -43,10 +43,8 @@ void snd_midi_event_free(struct snd_midi_event *dev);void snd_midi_event_reset_encode (struct snd_midi_event *dev);void snd_midi_event_reset_decode (struct snd_midi_event *dev);void snd_midi_event_no_status (struct snd_midi_event *dev,int on); -/* encode from byte stream - return number of written bytes if success */ -long snd_midi_event_encode (struct snd_midi_event *dev,unsigned char *buf,long count, -struct snd_seq_event *ev); -int snd_midi_event_encode_byte (struct snd_midi_event *dev,int c,struct snd_seq_event *ev); +bool snd_midi_event_encode_byte (struct snd_midi_event *dev,unsigned char c, +struct snd_seq_event *ev);/* decode from event to bytes - return number of written bytes if success */ long snd_midi_event_decode (struct snd_midi_event *dev,unsigned char *buf,long count,struct snd_seq_event *ev);
sound/core/seq/oss/seq_oss_midi.c +1 -1
@@ -637,7 +637,7 @@ snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struif ((mdev =get_mididev (dp, dev)) ==NULL )return -ENODEV ; -if (snd_midi_event_encode_byte (mdev->coder, c, ev) >0 ) { +if (snd_midi_event_encode_byte (mdev->coder, c, ev)) {snd_seq_oss_fill_addr (dp, ev, mdev->client, mdev->port);snd_use_lock_free (&mdev->use_lock);return 0 ;
sound/core/seq/seq_midi.c +10 -14
@@ -78,7 +78,7 @@ static void snd_midi_input_event(struct snd_rawmidi_substream *substream)struct seq_midisynth *msynth;struct snd_seq_event ev;char buf[16 ], *pbuf; -long res, count; +long res;if (substream ==NULL )return ;
@@ -94,19 +94,15 @@ static void snd_midi_input_event(struct snd_rawmidi_substream *substream)if (msynth->parser ==NULL )continue ; pbuf = buf; -while (res >0 ) { - count =snd_midi_event_encode (msynth->parser, pbuf, res, &ev); -if (count <0 ) -break ; - pbuf += count; - res -= count; -if (ev.type !=SNDRV_SEQ_EVENT_NONE ) { - ev.source.port = msynth->seq_port; - ev.dest.client =SNDRV_SEQ_ADDRESS_SUBSCRIBERS ; -snd_seq_kernel_client_dispatch (msynth->seq_client, &ev,1 ,0 ); -/* clear event and reset header */ -memset (&ev,0 ,sizeof (ev)); - } +while (res-- >0 ) { +if (!snd_midi_event_encode_byte (msynth->parser, + *pbuf++, &ev)) +continue ; + ev.source.port = msynth->seq_port; + ev.dest.client =SNDRV_SEQ_ADDRESS_SUBSCRIBERS ; +snd_seq_kernel_client_dispatch (msynth->seq_client, &ev,1 ,0 ); +/* clear event and reset header */ +memset (&ev,0 ,sizeof (ev)); } } }
sound/core/seq/seq_midi_event.c +7 -35
66 lines not on this page · raw message
sound/core/seq/seq_virmidi.c +2 -2
@@ -174,8 +174,8 @@ static void snd_vmidi_output_work(struct work_struct *work)while (READ_ONCE (vmidi->trigger)) {if (snd_rawmidi_transmit (substream, &input,1 ) !=1 )break ; -if (snd_midi_event_encode_byte (vmidi->parser, input, - &vmidi->event) <=0 ) +if (!snd_midi_event_encode_byte (vmidi->parser, input, + &vmidi->event))continue ;if (vmidi->event.type !=SNDRV_SEQ_EVENT_NONE ) { ret = snd_seq_kernel_client_dispatch(vmidi->client,
Signature · 1 line
2.18.0
PATCH 2/5 ALSA: seq: Remove dead codes
In reply to Takashi IwaiThere are a few functions that have been commented out for ages. And also there are functions that do nothing but placeholders. Let's kill them.
--- sound/core/seq/seq.c | 7 ----- sound/core/seq/seq_memory.c | 12 --------- sound/core/seq/seq_memory.h | 6 ----- sound/core/seq/seq_midi_emul.c | 12 --------- sound/core/seq/seq_midi_event.c | 45 --------------------------------- sound/core/seq/seq_virmidi.c | 33 ------------------------ 6 files changed, 115 deletions(-)
sound/core/seq/seq.c +0 -7
@@ -87,10 +87,6 @@ static int __init alsa_seq_init(void)if ((err =client_init_data ()) <0 ) goto error; -/* init memory, room for selected events */ -if ((err =snd_sequencer_memory_init ()) <0 ) - goto error; -/* init event queues */ if ((err =snd_seq_queues_init ()) <0 ) goto error;
@@ -126,9 +122,6 @@ static void __exit alsa_seq_exit(void)/* unregister sequencer device */ snd_sequencer_device_done (); -/* release event memory */ -snd_sequencer_memory_done (); -snd_seq_autoload_exit (); }
sound/core/seq/seq_memory.c +0 -12
@@ -504,18 +504,6 @@ int snd_seq_pool_delete(struct snd_seq_pool **ppool)return 0 ; } -/* initialize sequencer memory */ -int __init snd_sequencer_memory_init (void ) -{ -return 0 ; -} - -/* release sequencer memory */ -void __exit snd_sequencer_memory_done(void) -{ -} - -/* exported to seq_clientmgr.c */ void snd_seq_info_pool (struct snd_info_buffer *buffer,struct snd_seq_pool *pool,char *space)
sound/core/seq/seq_memory.h +0 -6
@@ -94,12 +94,6 @@ struct snd_seq_pool *snd_seq_pool_new(int poolsize);/* remove pool */ int snd_seq_pool_delete (struct snd_seq_pool **pool); -/* init memory */ -int snd_sequencer_memory_init (void ); - -/* release event memory */ -void snd_sequencer_memory_done (void ); -/* polling */ int snd_seq_pool_poll_wait (struct snd_seq_pool *pool,struct file *file,poll_table *wait);
sound/core/seq/seq_midi_emul.c +0 -12
@@ -728,15 +728,3 @@ void snd_midi_channel_free_set(struct snd_midi_channel_set *chset)kfree (chset); }EXPORT_SYMBOL (snd_midi_channel_free_set); - -static int __init alsa_seq_midi_emul_init (void ) -{ -return 0 ; -} - -static void __exit alsa_seq_midi_emul_exit(void) -{ -} - -module_init (alsa_seq_midi_emul_init) -module_exit (alsa_seq_midi_emul_exit)
sound/core/seq/seq_midi_event.c +0 -45
60 lines not on this page · raw message
sound/core/seq/seq_virmidi.c +0 -33
@@ -109,23 +109,6 @@ static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,return 0 ; } -/* -* receive an event from the remote virmidi port -* -* for rawmidi inputs, you can call this function from the event -* handler of a remote port which is attached to the virmidi via -* SNDRV_VIRMIDI_SEQ_ATTACH. -*/ -#if 0 -int snd_virmidi_receive (struct snd_rawmidi *rmidi,struct snd_seq_event *ev) -{ -struct snd_virmidi_dev *rdev; - - rdev = rmidi->private_data; -return snd_virmidi_dev_receive_event (rdev, ev, true); -} -#endif /* 0 */ -/* * event handler of virmidi port */
@@ -544,19 +527,3 @@ int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmireturn 0 ; }EXPORT_SYMBOL (snd_virmidi_new); - -/* -* ENTRY functions -*/ - -static int __init alsa_virmidi_init (void ) -{ -return 0 ; -} - -static void __exit alsa_virmidi_exit(void) -{ -} - -module_init (alsa_virmidi_init) -module_exit (alsa_virmidi_exit)
Signature · 1 line
2.18.0
PATCH 3/5 ALSA: seq: Fix leftovers at probe error path
In reply to Takashi IwaiThe sequencer core module doesn't call some destructors in the error path of the init code, which may leave some resources.
This patch mainly fix these leaks by calling the destructors appropriately at alsa_seq_init(). Also the patch brings a few cleanups along with it, namely:
- Expand the old "if ((err = xxx) < 0)" coding style
- Get rid of empty seq_queue_init() and its caller
- Change snd_seq_info_done() to void
Last but not least, a couple of functions lose __exit annotation since they are called also in alsa_seq_init().
No functional changes but minor code cleanups.
--- sound/core/seq/seq.c | 26 ++++++++++++++++---------- sound/core/seq/seq_clientmgr.c | 2 +- sound/core/seq/seq_info.c | 10 ++-------- sound/core/seq/seq_info.h | 6 +++--- sound/core/seq/seq_queue.c | 12 +----------- sound/core/seq/seq_queue.h | 3 --- 6 files changed, 23 insertions(+), 36 deletions(-)
sound/core/seq/seq.c +16 -10
@@ -84,26 +84,32 @@ static int __init alsa_seq_init(void) {int err; -if ((err =client_init_data ()) <0 ) - goto error; - -/* init event queues */ -if ((err =snd_seq_queues_init ()) <0 ) + err =client_init_data (); +if (err <0 ) goto error;/* register sequencer device */ -if ((err =snd_sequencer_device_init ()) <0 ) + err =snd_sequencer_device_init (); +if (err <0 ) goto error;/* register proc interface */ -if ((err =snd_seq_info_init ()) <0 ) - goto error; + err =snd_seq_info_init (); +if (err <0 ) + goto error_device;/* register our internal client */ -if ((err =snd_seq_system_client_init ()) <0 ) - goto error; + err =snd_seq_system_client_init (); +if (err <0 ) + goto error_info;snd_seq_autoload_init (); +return 0 ; + + error_info: +snd_seq_info_done (); + error_device: +snd_sequencer_device_done (); error:return err; }
sound/core/seq/seq_clientmgr.c +1 -1
@@ -2543,7 +2543,7 @@ int __init snd_sequencer_device_init(void)/* * unregister sequencer device */ -void __exit snd_sequencer_device_done(void) +void snd_sequencer_device_done (void ) {snd_unregister_device (&seq_dev);put_device (&seq_dev);
sound/core/seq/seq_info.c +2 -8
@@ -50,7 +50,7 @@ create_info_entry(char *name, void (*read)(struct snd_info_entry *,return entry; } -static void free_info_entries (void ) +void snd_seq_info_done (void ) {snd_info_free_entry (queues_entry);snd_info_free_entry (clients_entry);
@@ -70,12 +70,6 @@ int __init snd_seq_info_init(void)return 0 ; error: -free_info_entries (); +snd_seq_info_done ();return -ENOMEM ; } - -int __exit snd_seq_info_done(void) -{ -free_info_entries (); -return 0 ; -}
sound/core/seq/seq_info.h +3 -3
@@ -30,11 +30,11 @@ void snd_seq_info_queues_read(struct snd_info_entry *entry, struct snd_info_buff#ifdef CONFIG_SND_PROC_FS -int snd_seq_info_init (void ); -int snd_seq_info_done (void ); +int snd_seq_info_init (void ); +void snd_seq_info_done (void );#else static inline int snd_seq_info_init (void ) {return 0 ; } -static inline int snd_seq_info_done (void ) {return 0 ; } +static inline void snd_seq_info_done (void ) {}#endif #endif
sound/core/seq/seq_queue.c +1 -11
@@ -159,18 +159,8 @@ static void queue_delete(struct snd_seq_queue *q)/*----------------------------------------------------------------*/ -/* setup queues */ -int __init snd_seq_queues_init (void ) -{ -/* -memset(queue_list, 0, sizeof(queue_list)); -num_queues = 0; -*/ -return 0 ; -} -/* delete all existing queues */ -void __exit snd_seq_queues_delete(void) +void snd_seq_queues_delete (void ) {int i;
sound/core/seq/seq_queue.h +0 -3
@@ -63,9 +63,6 @@ struct snd_seq_queue {/* get the number of current queues */ int snd_seq_queue_get_cur_queues (void ); -/* init queues structure */ -int snd_seq_queues_init (void ); -/* delete queues */ void snd_seq_queues_delete (void );
Signature · 1 line
2.18.0
PATCH 4/5 ALSA: seq: Use no intrruptible mutex_lock
In reply to Takashi IwaiAll usages of mutex in ALSA sequencer core would take too long, hence we don't have to care about the user interruption that makes things complicated. Let's replace them with simpler mutex_lock().
--- sound/core/seq/seq_clientmgr.c | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-)
sound/core/seq/seq_clientmgr.c +7 -19
@@ -311,10 +311,9 @@ static int snd_seq_open(struct inode *inode, struct file *file)if (err <0 )return err; -if (mutex_lock_interruptible (®ister_mutex)) -return -ERESTARTSYS ; +mutex_lock (®ister_mutex); client =seq_create_client1 (-1 ,SNDRV_SEQ_DEFAULT_EVENTS ); -if (client ==NULL ) { +if (!client) {mutex_unlock (®ister_mutex);return -ENOMEM ;/* failure code */ }
@@ -1704,10 +1703,7 @@ static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,if (queue ==NULL )return -EINVAL ; -if (mutex_lock_interruptible (&queue->timer_mutex)) { -queuefree (queue); -return -ERESTARTSYS ; - } +mutex_lock (&queue->timer_mutex); tmr = queue->timer;memset (timer,0 ,sizeof (*timer)); timer->queue = queue->queue;
@@ -1741,10 +1737,7 @@ static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client, q =queueptr (timer->queue);if (q ==NULL )return -ENXIO ; -if (mutex_lock_interruptible (&q->timer_mutex)) { -queuefree (q); -return -ERESTARTSYS ; - } +mutex_lock (&q->timer_mutex); tmr = q->timer;snd_seq_queue_timer_close (timer->queue); tmr->type = timer->type;
@@ -2180,8 +2173,7 @@ int snd_seq_create_kernel_client(struct snd_card *card, int client_index,if (card ==NULL && client_index >=SNDRV_SEQ_GLOBAL_CLIENTS )return -EINVAL ; -if (mutex_lock_interruptible (®ister_mutex)) -return -ERESTARTSYS ; +mutex_lock (®ister_mutex);if (card) { client_index +=SNDRV_SEQ_GLOBAL_CLIENTS
@@ -2522,19 +2514,15 @@ int __init snd_sequencer_device_init(void)snd_device_initialize (&seq_dev,NULL );dev_set_name (&seq_dev,"seq" ); -if (mutex_lock_interruptible (®ister_mutex)) -return -ERESTARTSYS ; - +mutex_lock (®ister_mutex); err =snd_register_device (SNDRV_DEVICE_TYPE_SEQUENCER ,NULL ,0 , &snd_seq_f_ops,NULL , &seq_dev); +mutex_unlock (®ister_mutex);if (err <0 ) { -mutex_unlock (®ister_mutex);put_device (&seq_dev);return err; } -mutex_unlock (®ister_mutex); -return 0 ; }
Signature · 1 line
2.18.0
PATCH 5/5 ALSA: seq: Drop unused 64bit division macros
In reply to Takashi IwaiThe old ugly macros remained in the code without usage. Rip them off.
--- sound/core/seq/seq_queue.h | 24 ------------------------ 1 file changed, 24 deletions(-)
sound/core/seq/seq_queue.h +0 -24
@@ -109,28 +109,4 @@ int snd_seq_queue_is_used(int queueid, int client);int snd_seq_control_queue (struct snd_seq_event *ev,int atomic,int hop); -/* -* 64bit division - for sync stuff.. -*/ -#if defined(i386) || defined(i486) - -#define udiv_qrnnd (q, r, n1, n0, d) \ - __asm__ ("divl %4" \ - : "=a" ((u32)(q)), \ - "=d" ((u32)(r)) \ - : "0" ((u32)(n0)), \ - "1" ((u32)(n1)), \ - "rm" ((u32)(d))) - -#define u64_div (x,y,q) do {u32 __tmp; udiv_qrnnd(q, __tmp, (x)>>32, x, y);} while (0) -#define u64_mod (x,y,r) do {u32 __tmp; udiv_qrnnd(__tmp, q, (x)>>32, x, y);} while (0) -#define u64_divmod (x,y,q,r) udiv_qrnnd(q, r, (x)>>32, x, y) - -#else -#define u64_div (x,y,q) ((q) = (u32)((u64)(x) / (u64)(y))) -#define u64_mod (x,y,r) ((r) = (u32)((u64)(x) % (u64)(y))) -#define u64_divmod (x,y,q,r) (u64_div(x,y,q), u64_mod(x,y,r)) -#endif - -#endif
Signature · 1 line
2.18.0