44 *
55 * Copyright (C) 2010 OMICRON electronics GmbH
66 */
7- #include <linux/idr.h>
87#include <linux/device.h>
98#include <linux/err.h>
109#include <linux/init.h>
1615#include <linux/syscalls.h>
1716#include <linux/uaccess.h>
1817#include <linux/debugfs.h>
18+ #include <linux/xarray.h>
1919#include <uapi/linux/sched/types.h>
2020
2121#include "ptp_private.h"
@@ -34,7 +34,7 @@ const struct class ptp_class = {
3434
3535static dev_t ptp_devt ;
3636
37- static DEFINE_IDA (ptp_clocks_map );
37+ static DEFINE_XARRAY_ALLOC (ptp_clocks_map );
3838
3939/* time stamp event queue operations */
4040
@@ -204,7 +204,7 @@ static void ptp_clock_release(struct device *dev)
204204 bitmap_free (tsevq -> mask );
205205 kfree (tsevq );
206206 debugfs_remove (ptp -> debugfs_root );
207- ida_free (& ptp_clocks_map , ptp -> index );
207+ xa_erase (& ptp_clocks_map , ptp -> index );
208208 kfree (ptp );
209209}
210210
@@ -236,38 +236,42 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
236236{
237237 struct ptp_clock * ptp ;
238238 struct timestamp_event_queue * queue = NULL ;
239- int err = 0 , index , major = MAJOR (ptp_devt );
239+ int err , index , major = MAJOR (ptp_devt );
240240 char debugfsname [16 ];
241241 size_t size ;
242242
243243 if (info -> n_alarm > PTP_MAX_ALARMS )
244244 return ERR_PTR (- EINVAL );
245245
246246 /* Initialize a clock structure. */
247- err = - ENOMEM ;
248247 ptp = kzalloc (sizeof (struct ptp_clock ), GFP_KERNEL );
249- if (ptp == NULL )
248+ if (!ptp ) {
249+ err = - ENOMEM ;
250250 goto no_memory ;
251+ }
251252
252- index = ida_alloc_max (& ptp_clocks_map , MINORMASK , GFP_KERNEL );
253- if ( index < 0 ) {
254- err = index ;
253+ err = xa_alloc (& ptp_clocks_map , & index , ptp , xa_limit_31b ,
254+ GFP_KERNEL );
255+ if ( err )
255256 goto no_slot ;
256- }
257257
258258 ptp -> clock .ops = ptp_clock_ops ;
259259 ptp -> info = info ;
260260 ptp -> devid = MKDEV (major , index );
261261 ptp -> index = index ;
262262 INIT_LIST_HEAD (& ptp -> tsevqs );
263263 queue = kzalloc (sizeof (* queue ), GFP_KERNEL );
264- if (!queue )
264+ if (!queue ) {
265+ err = - ENOMEM ;
265266 goto no_memory_queue ;
267+ }
266268 list_add_tail (& queue -> qlist , & ptp -> tsevqs );
267269 spin_lock_init (& ptp -> tsevqs_lock );
268270 queue -> mask = bitmap_alloc (PTP_MAX_CHANNELS , GFP_KERNEL );
269- if (!queue -> mask )
271+ if (!queue -> mask ) {
272+ err = - ENOMEM ;
270273 goto no_memory_bitmap ;
274+ }
271275 bitmap_set (queue -> mask , 0 , PTP_MAX_CHANNELS );
272276 spin_lock_init (& queue -> lock );
273277 mutex_init (& ptp -> pincfg_mux );
@@ -381,7 +385,7 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
381385 list_del (& queue -> qlist );
382386 kfree (queue );
383387no_memory_queue :
384- ida_free (& ptp_clocks_map , index );
388+ xa_erase (& ptp_clocks_map , index );
385389no_slot :
386390 kfree (ptp );
387391no_memory :
@@ -514,7 +518,7 @@ static void __exit ptp_exit(void)
514518{
515519 class_unregister (& ptp_class );
516520 unregister_chrdev_region (ptp_devt , MINORMASK + 1 );
517- ida_destroy (& ptp_clocks_map );
521+ xa_destroy (& ptp_clocks_map );
518522}
519523
520524static int __init ptp_init (void )
0 commit comments