@@ -88,12 +88,11 @@ static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_l
8888
8989/*
9090 * create a timer instance with the given owner string.
91- * when timer is not NULL, increments the module counter
9291 */
93- static struct snd_timer_instance * snd_timer_instance_new (char * owner ,
94- struct snd_timer * timer )
92+ struct snd_timer_instance * snd_timer_instance_new (const char * owner )
9593{
9694 struct snd_timer_instance * timeri ;
95+
9796 timeri = kzalloc (sizeof (* timeri ), GFP_KERNEL );
9897 if (timeri == NULL )
9998 return NULL ;
@@ -108,15 +107,20 @@ static struct snd_timer_instance *snd_timer_instance_new(char *owner,
108107 INIT_LIST_HEAD (& timeri -> slave_list_head );
109108 INIT_LIST_HEAD (& timeri -> slave_active_head );
110109
111- timeri -> timer = timer ;
112- if (timer && !try_module_get (timer -> module )) {
110+ return timeri ;
111+ }
112+ EXPORT_SYMBOL (snd_timer_instance_new );
113+
114+ void snd_timer_instance_free (struct snd_timer_instance * timeri )
115+ {
116+ if (timeri ) {
117+ if (timeri -> private_free )
118+ timeri -> private_free (timeri );
113119 kfree (timeri -> owner );
114120 kfree (timeri );
115- return NULL ;
116121 }
117-
118- return timeri ;
119122}
123+ EXPORT_SYMBOL (snd_timer_instance_free );
120124
121125/*
122126 * find a timer instance from the given timer id
@@ -236,12 +240,11 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
236240 * open a timer instance
237241 * when opening a master, the slave id must be here given.
238242 */
239- int snd_timer_open (struct snd_timer_instance * * ti ,
240- char * owner , struct snd_timer_id * tid ,
243+ int snd_timer_open (struct snd_timer_instance * timeri ,
244+ struct snd_timer_id * tid ,
241245 unsigned int slave_id )
242246{
243247 struct snd_timer * timer ;
244- struct snd_timer_instance * timeri = NULL ;
245248 struct device * card_dev_to_put = NULL ;
246249 int err ;
247250
@@ -259,22 +262,14 @@ int snd_timer_open(struct snd_timer_instance **ti,
259262 err = - EBUSY ;
260263 goto unlock ;
261264 }
262- timeri = snd_timer_instance_new (owner , NULL );
263- if (!timeri ) {
264- err = - ENOMEM ;
265- goto unlock ;
266- }
267265 timeri -> slave_class = tid -> dev_sclass ;
268266 timeri -> slave_id = tid -> device ;
269267 timeri -> flags |= SNDRV_TIMER_IFLG_SLAVE ;
270268 list_add_tail (& timeri -> open_list , & snd_timer_slave_list );
271269 num_slaves ++ ;
272270 err = snd_timer_check_slave (timeri );
273- if (err < 0 ) {
274- snd_timer_close_locked (timeri , & card_dev_to_put );
275- timeri = NULL ;
276- }
277- goto unlock ;
271+ if (err < 0 )
272+ goto close ;
278273 }
279274
280275 /* open a master instance */
@@ -304,45 +299,40 @@ int snd_timer_open(struct snd_timer_instance **ti,
304299 err = - EBUSY ;
305300 goto unlock ;
306301 }
307- timeri = snd_timer_instance_new (owner , timer );
308- if (!timeri ) {
309- err = - ENOMEM ;
302+ if (!try_module_get (timer -> module )) {
303+ err = - EBUSY ;
310304 goto unlock ;
311305 }
312306 /* take a card refcount for safe disconnection */
313- if (timer -> card )
307+ if (timer -> card ) {
314308 get_device (& timer -> card -> card_dev );
315- timeri -> slave_class = tid -> dev_sclass ;
316- timeri -> slave_id = slave_id ;
309+ card_dev_to_put = & timer -> card -> card_dev ;
310+ }
317311
318312 if (list_empty (& timer -> open_list_head ) && timer -> hw .open ) {
319313 err = timer -> hw .open (timer );
320314 if (err ) {
321- kfree (timeri -> owner );
322- kfree (timeri );
323- timeri = NULL ;
324-
325- if (timer -> card )
326- card_dev_to_put = & timer -> card -> card_dev ;
327315 module_put (timer -> module );
328316 goto unlock ;
329317 }
330318 }
331319
320+ timeri -> timer = timer ;
321+ timeri -> slave_class = tid -> dev_sclass ;
322+ timeri -> slave_id = slave_id ;
323+
332324 list_add_tail (& timeri -> open_list , & timer -> open_list_head );
333325 timer -> num_instances ++ ;
334326 err = snd_timer_check_master (timeri );
335- if (err < 0 ) {
327+ close :
328+ if (err < 0 )
336329 snd_timer_close_locked (timeri , & card_dev_to_put );
337- timeri = NULL ;
338- }
339330
340331 unlock :
341332 mutex_unlock (& register_mutex );
342333 /* put_device() is called after unlock for avoiding deadlock */
343- if (card_dev_to_put )
334+ if (err < 0 && card_dev_to_put )
344335 put_device (card_dev_to_put );
345- * ti = timeri ;
346336 return err ;
347337}
348338EXPORT_SYMBOL (snd_timer_open );
@@ -363,9 +353,11 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
363353 spin_unlock_irq (& timer -> lock );
364354 }
365355
366- list_del (& timeri -> open_list );
367- if (timeri -> flags & SNDRV_TIMER_IFLG_SLAVE )
368- num_slaves -- ;
356+ if (!list_empty (& timeri -> open_list )) {
357+ list_del_init (& timeri -> open_list );
358+ if (timeri -> flags & SNDRV_TIMER_IFLG_SLAVE )
359+ num_slaves -- ;
360+ }
369361
370362 /* force to stop the timer */
371363 snd_timer_stop (timeri );
@@ -384,6 +376,7 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
384376 /* remove slave links */
385377 spin_lock_irq (& slave_active_lock );
386378 spin_lock (& timer -> lock );
379+ timeri -> timer = NULL ;
387380 list_for_each_entry_safe (slave , tmp , & timeri -> slave_list_head ,
388381 open_list ) {
389382 list_move_tail (& slave -> open_list , & snd_timer_slave_list );
@@ -401,11 +394,6 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
401394 timer = NULL ;
402395 }
403396
404- if (timeri -> private_free )
405- timeri -> private_free (timeri );
406- kfree (timeri -> owner );
407- kfree (timeri );
408-
409397 if (timer ) {
410398 if (list_empty (& timer -> open_list_head ) && timer -> hw .close )
411399 timer -> hw .close (timer );
@@ -1480,8 +1468,10 @@ static int snd_timer_user_release(struct inode *inode, struct file *file)
14801468 tu = file -> private_data ;
14811469 file -> private_data = NULL ;
14821470 mutex_lock (& tu -> ioctl_lock );
1483- if (tu -> timeri )
1471+ if (tu -> timeri ) {
14841472 snd_timer_close (tu -> timeri );
1473+ snd_timer_instance_free (tu -> timeri );
1474+ }
14851475 mutex_unlock (& tu -> ioctl_lock );
14861476 kfree (tu -> queue );
14871477 kfree (tu -> tqueue );
@@ -1722,6 +1712,7 @@ static int snd_timer_user_tselect(struct file *file,
17221712 tu = file -> private_data ;
17231713 if (tu -> timeri ) {
17241714 snd_timer_close (tu -> timeri );
1715+ snd_timer_instance_free (tu -> timeri );
17251716 tu -> timeri = NULL ;
17261717 }
17271718 if (copy_from_user (& tselect , _tselect , sizeof (tselect ))) {
@@ -1731,9 +1722,11 @@ static int snd_timer_user_tselect(struct file *file,
17311722 sprintf (str , "application %i" , current -> pid );
17321723 if (tselect .id .dev_class != SNDRV_TIMER_CLASS_SLAVE )
17331724 tselect .id .dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION ;
1734- err = snd_timer_open (& tu -> timeri , str , & tselect .id , current -> pid );
1735- if (err < 0 )
1725+ tu -> timeri = snd_timer_instance_new (str );
1726+ if (!tu -> timeri ) {
1727+ err = - ENOMEM ;
17361728 goto __err ;
1729+ }
17371730
17381731 tu -> timeri -> flags |= SNDRV_TIMER_IFLG_FAST ;
17391732 tu -> timeri -> callback = tu -> tread
@@ -1742,6 +1735,12 @@ static int snd_timer_user_tselect(struct file *file,
17421735 tu -> timeri -> callback_data = (void * )tu ;
17431736 tu -> timeri -> disconnect = snd_timer_user_disconnect ;
17441737
1738+ err = snd_timer_open (tu -> timeri , & tselect .id , current -> pid );
1739+ if (err < 0 ) {
1740+ snd_timer_instance_free (tu -> timeri );
1741+ tu -> timeri = NULL ;
1742+ }
1743+
17451744 __err :
17461745 return err ;
17471746}
0 commit comments