@@ -90,7 +90,7 @@ enum ffs_state {
9090
9191	/* 
9292	 * We've got descriptors and strings.  We are or have called 
93- 	 * functionfs_ready_callback ().  functionfs_bind() may have 
93+ 	 * ffs_ready ().  functionfs_bind() may have 
9494	 * been called but we don't know. 
9595	 * 
9696	 * This is the only state in which operations on epfiles may 
@@ -103,7 +103,7 @@ enum ffs_state {
103103	 * we encounter an unrecoverable error.  The only 
104104	 * unrecoverable error is situation when after reading strings 
105105	 * from user space we fail to initialise epfiles or 
106- 	 * functionfs_ready_callback () returns with error (<0). 
106+ 	 * ffs_ready () returns with error (<0). 
107107	 * 
108108	 * In this state no open(2), read(2) or write(2) (both on ep0 
109109	 * as well as epfile) may succeed (at this point epfiles are 
@@ -361,6 +361,15 @@ ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
361361		   const  struct  file_operations  * fops ,
362362		   struct  dentry  * * dentry_p );
363363
364+ /* Devices management *******************************************************/ 
365+ 
366+ DEFINE_MUTEX (ffs_lock );
367+ 
368+ static  struct  ffs_dev  * ffs_find_dev (const  char  * name );
369+ static  void  * ffs_acquire_dev (const  char  * dev_name );
370+ static  void  ffs_release_dev (struct  ffs_data  * ffs_data );
371+ static  int  ffs_ready (struct  ffs_data  * ffs );
372+ static  void  ffs_closed (struct  ffs_data  * ffs );
364373
365374/* Misc helper functions ****************************************************/ 
366375
@@ -486,7 +495,7 @@ static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
486495			ffs -> state  =  FFS_ACTIVE ;
487496			mutex_unlock (& ffs -> mutex );
488497
489- 			ret  =  functionfs_ready_callback (ffs );
498+ 			ret  =  ffs_ready (ffs );
490499			if  (unlikely (ret  <  0 )) {
491500				ffs -> state  =  FFS_CLOSING ;
492501				return  ret ;
@@ -1218,7 +1227,7 @@ ffs_fs_mount(struct file_system_type *t, int flags,
12181227		return  ERR_PTR (- ENOMEM );
12191228	}
12201229
1221- 	ffs_dev  =  functionfs_acquire_dev_callback (dev_name );
1230+ 	ffs_dev  =  ffs_acquire_dev (dev_name );
12221231	if  (IS_ERR (ffs_dev )) {
12231232		ffs_data_put (ffs );
12241233		return  ERR_CAST (ffs_dev );
@@ -1228,7 +1237,7 @@ ffs_fs_mount(struct file_system_type *t, int flags,
12281237
12291238	rv  =  mount_nodev (t , flags , & data , ffs_sb_fill );
12301239	if  (IS_ERR (rv ) &&  data .ffs_data ) {
1231- 		functionfs_release_dev_callback (data .ffs_data );
1240+ 		ffs_release_dev (data .ffs_data );
12321241		ffs_data_put (data .ffs_data );
12331242	}
12341243	return  rv ;
@@ -1241,7 +1250,7 @@ ffs_fs_kill_sb(struct super_block *sb)
12411250
12421251	kill_litter_super (sb );
12431252	if  (sb -> s_fs_info ) {
1244- 		functionfs_release_dev_callback (sb -> s_fs_info );
1253+ 		ffs_release_dev (sb -> s_fs_info );
12451254		ffs_data_put (sb -> s_fs_info );
12461255	}
12471256}
@@ -1354,7 +1363,7 @@ static void ffs_data_clear(struct ffs_data *ffs)
13541363	ENTER ();
13551364
13561365	if  (test_and_clear_bit (FFS_FL_CALL_CLOSED_CALLBACK , & ffs -> flags ))
1357- 		functionfs_closed_callback (ffs );
1366+ 		ffs_closed (ffs );
13581367
13591368	BUG_ON (ffs -> gadget );
13601369
@@ -2466,6 +2475,221 @@ static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
24662475}
24672476
24682477
2478+ /* Devices management *******************************************************/ 
2479+ 
2480+ static  LIST_HEAD (ffs_devices );
2481+ 
2482+ static  struct  ffs_dev  * _ffs_find_dev (const  char  * name )
2483+ {
2484+ 	struct  ffs_dev  * dev ;
2485+ 
2486+ 	list_for_each_entry (dev , & ffs_devices , entry ) {
2487+ 		if  (!dev -> name  ||  !name )
2488+ 			continue ;
2489+ 		if  (strcmp (dev -> name , name ) ==  0 )
2490+ 			return  dev ;
2491+ 	}
2492+ 	
2493+ 	return  NULL ;
2494+ }
2495+ 
2496+ /* 
2497+  * ffs_lock must be taken by the caller of this function 
2498+  */ 
2499+ static  struct  ffs_dev  * ffs_get_single_dev (void )
2500+ {
2501+ 	struct  ffs_dev  * dev ;
2502+ 
2503+ 	if  (list_is_singular (& ffs_devices )) {
2504+ 		dev  =  list_first_entry (& ffs_devices , struct  ffs_dev , entry );
2505+ 		if  (dev -> single )
2506+ 			return  dev ;
2507+ 	}
2508+ 
2509+ 	return  NULL ;
2510+ }
2511+ 
2512+ /* 
2513+  * ffs_lock must be taken by the caller of this function 
2514+  */ 
2515+ static  struct  ffs_dev  * ffs_find_dev (const  char  * name )
2516+ {
2517+ 	struct  ffs_dev  * dev ;
2518+ 
2519+ 	dev  =  ffs_get_single_dev ();
2520+ 	if  (dev )
2521+ 		return  dev ;
2522+ 
2523+ 	return  _ffs_find_dev (name );
2524+ }
2525+ 
2526+ /* 
2527+  * ffs_lock must be taken by the caller of this function 
2528+  */ 
2529+ struct  ffs_dev  * ffs_alloc_dev (void )
2530+ {
2531+ 	struct  ffs_dev  * dev ;
2532+ 	int  ret ;
2533+ 
2534+ 	if  (ffs_get_single_dev ())
2535+ 			return  ERR_PTR (- EBUSY );
2536+ 
2537+ 	dev  =  kzalloc (sizeof (* dev ), GFP_KERNEL );
2538+ 	if  (!dev )
2539+ 		return  ERR_PTR (- ENOMEM );
2540+ 
2541+ 	if  (list_empty (& ffs_devices )) {
2542+ 		ret  =  functionfs_init ();
2543+ 		if  (ret ) {
2544+ 			kfree (dev );
2545+ 			return  ERR_PTR (ret );
2546+ 		}
2547+ 	}
2548+ 
2549+ 	list_add (& dev -> entry , & ffs_devices );
2550+ 
2551+ 	return  dev ;
2552+ }
2553+ 
2554+ /* 
2555+  * ffs_lock must be taken by the caller of this function 
2556+  * The caller is responsible for "name" being available whenever f_fs needs it 
2557+  */ 
2558+ static  int  _ffs_name_dev (struct  ffs_dev  * dev , const  char  * name )
2559+ {
2560+ 	struct  ffs_dev  * existing ;
2561+ 
2562+ 	existing  =  _ffs_find_dev (name );
2563+ 	if  (existing )
2564+ 		return  - EBUSY ;
2565+ 	
2566+ 	dev -> name  =  name ;
2567+ 
2568+ 	return  0 ;
2569+ }
2570+ 
2571+ /* 
2572+  * The caller is responsible for "name" being available whenever f_fs needs it 
2573+  */ 
2574+ int  ffs_name_dev (struct  ffs_dev  * dev , const  char  * name )
2575+ {
2576+ 	int  ret ;
2577+ 
2578+ 	ffs_dev_lock ();
2579+ 	ret  =  _ffs_name_dev (dev , name );
2580+ 	ffs_dev_unlock ();
2581+ 
2582+ 	return  ret ;
2583+ }
2584+ 
2585+ int  ffs_single_dev (struct  ffs_dev  * dev )
2586+ {
2587+ 	int  ret ;
2588+ 
2589+ 	ret  =  0 ;
2590+ 	ffs_dev_lock ();
2591+ 
2592+ 	if  (!list_is_singular (& ffs_devices ))
2593+ 		ret  =  - EBUSY ;
2594+ 	else 
2595+ 		dev -> single  =  true;
2596+ 
2597+ 	ffs_dev_unlock ();
2598+ 	return  ret ;
2599+ }
2600+ 
2601+ /* 
2602+  * ffs_lock must be taken by the caller of this function 
2603+  */ 
2604+ void  ffs_free_dev (struct  ffs_dev  * dev )
2605+ {
2606+ 	list_del (& dev -> entry );
2607+ 	kfree (dev );
2608+ 	if  (list_empty (& ffs_devices ))
2609+ 		functionfs_cleanup ();
2610+ }
2611+ 
2612+ static  void  * ffs_acquire_dev (const  char  * dev_name )
2613+ {
2614+ 	struct  ffs_dev  * ffs_dev ;
2615+ 
2616+ 	ENTER ();
2617+ 	ffs_dev_lock ();
2618+ 
2619+ 	ffs_dev  =  ffs_find_dev (dev_name );
2620+ 	if  (!ffs_dev )
2621+ 		ffs_dev  =  ERR_PTR (- ENODEV );
2622+ 	else  if  (ffs_dev -> mounted )
2623+ 		ffs_dev  =  ERR_PTR (- EBUSY );
2624+ 	else 
2625+ 		ffs_dev -> mounted  =  true;
2626+ 
2627+ 	ffs_dev_unlock ();
2628+ 	return  ffs_dev ;
2629+ }
2630+ 
2631+ static  void  ffs_release_dev (struct  ffs_data  * ffs_data )
2632+ {
2633+ 	struct  ffs_dev  * ffs_dev ;
2634+ 
2635+ 	ENTER ();
2636+ 	ffs_dev_lock ();
2637+ 
2638+ 	ffs_dev  =  ffs_data -> private_data ;
2639+ 	if  (ffs_dev )
2640+ 		ffs_dev -> mounted  =  false;
2641+ 
2642+ 	ffs_dev_unlock ();
2643+ }
2644+ 
2645+ static  int  ffs_ready (struct  ffs_data  * ffs )
2646+ {
2647+ 	struct  ffs_dev  * ffs_obj ;
2648+ 	int  ret  =  0 ;
2649+ 
2650+ 	ENTER ();
2651+ 	ffs_dev_lock ();
2652+ 
2653+ 	ffs_obj  =  ffs -> private_data ;
2654+ 	if  (!ffs_obj ) {
2655+ 		ret  =  - EINVAL ;
2656+ 		goto done ;
2657+ 	}
2658+ 	if  (WARN_ON (ffs_obj -> desc_ready )) {
2659+ 		ret  =  - EBUSY ;
2660+ 		goto done ;
2661+ 	}
2662+ 
2663+ 	ffs_obj -> desc_ready  =  true;
2664+ 	ffs_obj -> ffs_data  =  ffs ;
2665+ 
2666+ 	if  (ffs_obj -> ffs_ready_callback )
2667+ 		ret  =  ffs_obj -> ffs_ready_callback (ffs );
2668+ 
2669+ done :
2670+ 	ffs_dev_unlock ();
2671+ 	return  ret ;
2672+ }
2673+ 
2674+ static  void  ffs_closed (struct  ffs_data  * ffs )
2675+ {
2676+ 	struct  ffs_dev  * ffs_obj ;
2677+ 
2678+ 	ENTER ();
2679+ 	ffs_dev_lock ();
2680+ 
2681+ 	ffs_obj  =  ffs -> private_data ;
2682+ 	if  (!ffs_obj )
2683+ 		goto done ;
2684+ 
2685+ 	ffs_obj -> desc_ready  =  false;
2686+ 
2687+ 	if  (ffs_obj -> ffs_closed_callback )
2688+ 		ffs_obj -> ffs_closed_callback (ffs );
2689+ done :
2690+ 	ffs_dev_unlock ();
2691+ }
2692+ 
24692693/* Misc helper functions ****************************************************/ 
24702694
24712695static  int  ffs_mutex_lock (struct  mutex  * mutex , unsigned  nonblock )
0 commit comments