]> Git Repo - J-linux.git/blob - drivers/fpga/fpga-mgr.c
fpga: fpga-mgr: wrap the write_init() op
[J-linux.git] / drivers / fpga / fpga-mgr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * FPGA Manager Core
4  *
5  *  Copyright (C) 2013-2015 Altera Corporation
6  *  Copyright (C) 2017 Intel Corporation
7  *
8  * With code from the mailing list:
9  * Copyright (C) 2013 Xilinx, Inc.
10  */
11 #include <linux/firmware.h>
12 #include <linux/fpga/fpga-mgr.h>
13 #include <linux/idr.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/scatterlist.h>
19 #include <linux/highmem.h>
20
21 static DEFINE_IDA(fpga_mgr_ida);
22 static struct class *fpga_mgr_class;
23
24 struct fpga_mgr_devres {
25         struct fpga_manager *mgr;
26 };
27
28 static inline int fpga_mgr_write_init(struct fpga_manager *mgr,
29                                       struct fpga_image_info *info,
30                                       const char *buf, size_t count)
31 {
32         if (mgr->mops->write_init)
33                 return  mgr->mops->write_init(mgr, info, buf, count);
34         return 0;
35 }
36
37 /**
38  * fpga_image_info_alloc - Allocate an FPGA image info struct
39  * @dev: owning device
40  *
41  * Return: struct fpga_image_info or NULL
42  */
43 struct fpga_image_info *fpga_image_info_alloc(struct device *dev)
44 {
45         struct fpga_image_info *info;
46
47         get_device(dev);
48
49         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
50         if (!info) {
51                 put_device(dev);
52                 return NULL;
53         }
54
55         info->dev = dev;
56
57         return info;
58 }
59 EXPORT_SYMBOL_GPL(fpga_image_info_alloc);
60
61 /**
62  * fpga_image_info_free - Free an FPGA image info struct
63  * @info: FPGA image info struct to free
64  */
65 void fpga_image_info_free(struct fpga_image_info *info)
66 {
67         struct device *dev;
68
69         if (!info)
70                 return;
71
72         dev = info->dev;
73         if (info->firmware_name)
74                 devm_kfree(dev, info->firmware_name);
75
76         devm_kfree(dev, info);
77         put_device(dev);
78 }
79 EXPORT_SYMBOL_GPL(fpga_image_info_free);
80
81 /*
82  * Call the low level driver's write_init function.  This will do the
83  * device-specific things to get the FPGA into the state where it is ready to
84  * receive an FPGA image. The low level driver only gets to see the first
85  * initial_header_size bytes in the buffer.
86  */
87 static int fpga_mgr_write_init_buf(struct fpga_manager *mgr,
88                                    struct fpga_image_info *info,
89                                    const char *buf, size_t count)
90 {
91         int ret;
92
93         mgr->state = FPGA_MGR_STATE_WRITE_INIT;
94         if (!mgr->mops->initial_header_size)
95                 ret = fpga_mgr_write_init(mgr, info, NULL, 0);
96         else
97                 ret = fpga_mgr_write_init(
98                     mgr, info, buf, min(mgr->mops->initial_header_size, count));
99
100         if (ret) {
101                 dev_err(&mgr->dev, "Error preparing FPGA for writing\n");
102                 mgr->state = FPGA_MGR_STATE_WRITE_INIT_ERR;
103                 return ret;
104         }
105
106         return 0;
107 }
108
109 static int fpga_mgr_write_init_sg(struct fpga_manager *mgr,
110                                   struct fpga_image_info *info,
111                                   struct sg_table *sgt)
112 {
113         struct sg_mapping_iter miter;
114         size_t len;
115         char *buf;
116         int ret;
117
118         if (!mgr->mops->initial_header_size)
119                 return fpga_mgr_write_init_buf(mgr, info, NULL, 0);
120
121         /*
122          * First try to use miter to map the first fragment to access the
123          * header, this is the typical path.
124          */
125         sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
126         if (sg_miter_next(&miter) &&
127             miter.length >= mgr->mops->initial_header_size) {
128                 ret = fpga_mgr_write_init_buf(mgr, info, miter.addr,
129                                               miter.length);
130                 sg_miter_stop(&miter);
131                 return ret;
132         }
133         sg_miter_stop(&miter);
134
135         /* Otherwise copy the fragments into temporary memory. */
136         buf = kmalloc(mgr->mops->initial_header_size, GFP_KERNEL);
137         if (!buf)
138                 return -ENOMEM;
139
140         len = sg_copy_to_buffer(sgt->sgl, sgt->nents, buf,
141                                 mgr->mops->initial_header_size);
142         ret = fpga_mgr_write_init_buf(mgr, info, buf, len);
143
144         kfree(buf);
145
146         return ret;
147 }
148
149 /*
150  * After all the FPGA image has been written, do the device specific steps to
151  * finish and set the FPGA into operating mode.
152  */
153 static int fpga_mgr_write_complete(struct fpga_manager *mgr,
154                                    struct fpga_image_info *info)
155 {
156         int ret;
157
158         mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE;
159         ret = mgr->mops->write_complete(mgr, info);
160         if (ret) {
161                 dev_err(&mgr->dev, "Error after writing image data to FPGA\n");
162                 mgr->state = FPGA_MGR_STATE_WRITE_COMPLETE_ERR;
163                 return ret;
164         }
165         mgr->state = FPGA_MGR_STATE_OPERATING;
166
167         return 0;
168 }
169
170 /**
171  * fpga_mgr_buf_load_sg - load fpga from image in buffer from a scatter list
172  * @mgr:        fpga manager
173  * @info:       fpga image specific information
174  * @sgt:        scatterlist table
175  *
176  * Step the low level fpga manager through the device-specific steps of getting
177  * an FPGA ready to be configured, writing the image to it, then doing whatever
178  * post-configuration steps necessary.  This code assumes the caller got the
179  * mgr pointer from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is
180  * not an error code.
181  *
182  * This is the preferred entry point for FPGA programming, it does not require
183  * any contiguous kernel memory.
184  *
185  * Return: 0 on success, negative error code otherwise.
186  */
187 static int fpga_mgr_buf_load_sg(struct fpga_manager *mgr,
188                                 struct fpga_image_info *info,
189                                 struct sg_table *sgt)
190 {
191         int ret;
192
193         ret = fpga_mgr_write_init_sg(mgr, info, sgt);
194         if (ret)
195                 return ret;
196
197         /* Write the FPGA image to the FPGA. */
198         mgr->state = FPGA_MGR_STATE_WRITE;
199         if (mgr->mops->write_sg) {
200                 ret = mgr->mops->write_sg(mgr, sgt);
201         } else {
202                 struct sg_mapping_iter miter;
203
204                 sg_miter_start(&miter, sgt->sgl, sgt->nents, SG_MITER_FROM_SG);
205                 while (sg_miter_next(&miter)) {
206                         ret = mgr->mops->write(mgr, miter.addr, miter.length);
207                         if (ret)
208                                 break;
209                 }
210                 sg_miter_stop(&miter);
211         }
212
213         if (ret) {
214                 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
215                 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
216                 return ret;
217         }
218
219         return fpga_mgr_write_complete(mgr, info);
220 }
221
222 static int fpga_mgr_buf_load_mapped(struct fpga_manager *mgr,
223                                     struct fpga_image_info *info,
224                                     const char *buf, size_t count)
225 {
226         int ret;
227
228         ret = fpga_mgr_write_init_buf(mgr, info, buf, count);
229         if (ret)
230                 return ret;
231
232         /*
233          * Write the FPGA image to the FPGA.
234          */
235         mgr->state = FPGA_MGR_STATE_WRITE;
236         ret = mgr->mops->write(mgr, buf, count);
237         if (ret) {
238                 dev_err(&mgr->dev, "Error while writing image data to FPGA\n");
239                 mgr->state = FPGA_MGR_STATE_WRITE_ERR;
240                 return ret;
241         }
242
243         return fpga_mgr_write_complete(mgr, info);
244 }
245
246 /**
247  * fpga_mgr_buf_load - load fpga from image in buffer
248  * @mgr:        fpga manager
249  * @info:       fpga image info
250  * @buf:        buffer contain fpga image
251  * @count:      byte count of buf
252  *
253  * Step the low level fpga manager through the device-specific steps of getting
254  * an FPGA ready to be configured, writing the image to it, then doing whatever
255  * post-configuration steps necessary.  This code assumes the caller got the
256  * mgr pointer from of_fpga_mgr_get() and checked that it is not an error code.
257  *
258  * Return: 0 on success, negative error code otherwise.
259  */
260 static int fpga_mgr_buf_load(struct fpga_manager *mgr,
261                              struct fpga_image_info *info,
262                              const char *buf, size_t count)
263 {
264         struct page **pages;
265         struct sg_table sgt;
266         const void *p;
267         int nr_pages;
268         int index;
269         int rc;
270
271         /*
272          * This is just a fast path if the caller has already created a
273          * contiguous kernel buffer and the driver doesn't require SG, non-SG
274          * drivers will still work on the slow path.
275          */
276         if (mgr->mops->write)
277                 return fpga_mgr_buf_load_mapped(mgr, info, buf, count);
278
279         /*
280          * Convert the linear kernel pointer into a sg_table of pages for use
281          * by the driver.
282          */
283         nr_pages = DIV_ROUND_UP((unsigned long)buf + count, PAGE_SIZE) -
284                    (unsigned long)buf / PAGE_SIZE;
285         pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
286         if (!pages)
287                 return -ENOMEM;
288
289         p = buf - offset_in_page(buf);
290         for (index = 0; index < nr_pages; index++) {
291                 if (is_vmalloc_addr(p))
292                         pages[index] = vmalloc_to_page(p);
293                 else
294                         pages[index] = kmap_to_page((void *)p);
295                 if (!pages[index]) {
296                         kfree(pages);
297                         return -EFAULT;
298                 }
299                 p += PAGE_SIZE;
300         }
301
302         /*
303          * The temporary pages list is used to code share the merging algorithm
304          * in sg_alloc_table_from_pages
305          */
306         rc = sg_alloc_table_from_pages(&sgt, pages, index, offset_in_page(buf),
307                                        count, GFP_KERNEL);
308         kfree(pages);
309         if (rc)
310                 return rc;
311
312         rc = fpga_mgr_buf_load_sg(mgr, info, &sgt);
313         sg_free_table(&sgt);
314
315         return rc;
316 }
317
318 /**
319  * fpga_mgr_firmware_load - request firmware and load to fpga
320  * @mgr:        fpga manager
321  * @info:       fpga image specific information
322  * @image_name: name of image file on the firmware search path
323  *
324  * Request an FPGA image using the firmware class, then write out to the FPGA.
325  * Update the state before each step to provide info on what step failed if
326  * there is a failure.  This code assumes the caller got the mgr pointer
327  * from of_fpga_mgr_get() or fpga_mgr_get() and checked that it is not an error
328  * code.
329  *
330  * Return: 0 on success, negative error code otherwise.
331  */
332 static int fpga_mgr_firmware_load(struct fpga_manager *mgr,
333                                   struct fpga_image_info *info,
334                                   const char *image_name)
335 {
336         struct device *dev = &mgr->dev;
337         const struct firmware *fw;
338         int ret;
339
340         dev_info(dev, "writing %s to %s\n", image_name, mgr->name);
341
342         mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ;
343
344         ret = request_firmware(&fw, image_name, dev);
345         if (ret) {
346                 mgr->state = FPGA_MGR_STATE_FIRMWARE_REQ_ERR;
347                 dev_err(dev, "Error requesting firmware %s\n", image_name);
348                 return ret;
349         }
350
351         ret = fpga_mgr_buf_load(mgr, info, fw->data, fw->size);
352
353         release_firmware(fw);
354
355         return ret;
356 }
357
358 /**
359  * fpga_mgr_load - load FPGA from scatter/gather table, buffer, or firmware
360  * @mgr:        fpga manager
361  * @info:       fpga image information.
362  *
363  * Load the FPGA from an image which is indicated in @info.  If successful, the
364  * FPGA ends up in operating mode.
365  *
366  * Return: 0 on success, negative error code otherwise.
367  */
368 int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info)
369 {
370         if (info->sgt)
371                 return fpga_mgr_buf_load_sg(mgr, info, info->sgt);
372         if (info->buf && info->count)
373                 return fpga_mgr_buf_load(mgr, info, info->buf, info->count);
374         if (info->firmware_name)
375                 return fpga_mgr_firmware_load(mgr, info, info->firmware_name);
376         return -EINVAL;
377 }
378 EXPORT_SYMBOL_GPL(fpga_mgr_load);
379
380 static const char * const state_str[] = {
381         [FPGA_MGR_STATE_UNKNOWN] =              "unknown",
382         [FPGA_MGR_STATE_POWER_OFF] =            "power off",
383         [FPGA_MGR_STATE_POWER_UP] =             "power up",
384         [FPGA_MGR_STATE_RESET] =                "reset",
385
386         /* requesting FPGA image from firmware */
387         [FPGA_MGR_STATE_FIRMWARE_REQ] =         "firmware request",
388         [FPGA_MGR_STATE_FIRMWARE_REQ_ERR] =     "firmware request error",
389
390         /* Preparing FPGA to receive image */
391         [FPGA_MGR_STATE_WRITE_INIT] =           "write init",
392         [FPGA_MGR_STATE_WRITE_INIT_ERR] =       "write init error",
393
394         /* Writing image to FPGA */
395         [FPGA_MGR_STATE_WRITE] =                "write",
396         [FPGA_MGR_STATE_WRITE_ERR] =            "write error",
397
398         /* Finishing configuration after image has been written */
399         [FPGA_MGR_STATE_WRITE_COMPLETE] =       "write complete",
400         [FPGA_MGR_STATE_WRITE_COMPLETE_ERR] =   "write complete error",
401
402         /* FPGA reports to be in normal operating mode */
403         [FPGA_MGR_STATE_OPERATING] =            "operating",
404 };
405
406 static ssize_t name_show(struct device *dev,
407                          struct device_attribute *attr, char *buf)
408 {
409         struct fpga_manager *mgr = to_fpga_manager(dev);
410
411         return sprintf(buf, "%s\n", mgr->name);
412 }
413
414 static ssize_t state_show(struct device *dev,
415                           struct device_attribute *attr, char *buf)
416 {
417         struct fpga_manager *mgr = to_fpga_manager(dev);
418
419         return sprintf(buf, "%s\n", state_str[mgr->state]);
420 }
421
422 static ssize_t status_show(struct device *dev,
423                            struct device_attribute *attr, char *buf)
424 {
425         struct fpga_manager *mgr = to_fpga_manager(dev);
426         u64 status;
427         int len = 0;
428
429         if (!mgr->mops->status)
430                 return -ENOENT;
431
432         status = mgr->mops->status(mgr);
433
434         if (status & FPGA_MGR_STATUS_OPERATION_ERR)
435                 len += sprintf(buf + len, "reconfig operation error\n");
436         if (status & FPGA_MGR_STATUS_CRC_ERR)
437                 len += sprintf(buf + len, "reconfig CRC error\n");
438         if (status & FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR)
439                 len += sprintf(buf + len, "reconfig incompatible image\n");
440         if (status & FPGA_MGR_STATUS_IP_PROTOCOL_ERR)
441                 len += sprintf(buf + len, "reconfig IP protocol error\n");
442         if (status & FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR)
443                 len += sprintf(buf + len, "reconfig fifo overflow error\n");
444
445         return len;
446 }
447
448 static DEVICE_ATTR_RO(name);
449 static DEVICE_ATTR_RO(state);
450 static DEVICE_ATTR_RO(status);
451
452 static struct attribute *fpga_mgr_attrs[] = {
453         &dev_attr_name.attr,
454         &dev_attr_state.attr,
455         &dev_attr_status.attr,
456         NULL,
457 };
458 ATTRIBUTE_GROUPS(fpga_mgr);
459
460 static struct fpga_manager *__fpga_mgr_get(struct device *dev)
461 {
462         struct fpga_manager *mgr;
463
464         mgr = to_fpga_manager(dev);
465
466         if (!try_module_get(dev->parent->driver->owner))
467                 goto err_dev;
468
469         return mgr;
470
471 err_dev:
472         put_device(dev);
473         return ERR_PTR(-ENODEV);
474 }
475
476 static int fpga_mgr_dev_match(struct device *dev, const void *data)
477 {
478         return dev->parent == data;
479 }
480
481 /**
482  * fpga_mgr_get - Given a device, get a reference to an fpga mgr.
483  * @dev:        parent device that fpga mgr was registered with
484  *
485  * Return: fpga manager struct or IS_ERR() condition containing error code.
486  */
487 struct fpga_manager *fpga_mgr_get(struct device *dev)
488 {
489         struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
490                                                    fpga_mgr_dev_match);
491         if (!mgr_dev)
492                 return ERR_PTR(-ENODEV);
493
494         return __fpga_mgr_get(mgr_dev);
495 }
496 EXPORT_SYMBOL_GPL(fpga_mgr_get);
497
498 /**
499  * of_fpga_mgr_get - Given a device node, get a reference to an fpga mgr.
500  *
501  * @node:       device node
502  *
503  * Return: fpga manager struct or IS_ERR() condition containing error code.
504  */
505 struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
506 {
507         struct device *dev;
508
509         dev = class_find_device_by_of_node(fpga_mgr_class, node);
510         if (!dev)
511                 return ERR_PTR(-ENODEV);
512
513         return __fpga_mgr_get(dev);
514 }
515 EXPORT_SYMBOL_GPL(of_fpga_mgr_get);
516
517 /**
518  * fpga_mgr_put - release a reference to an fpga manager
519  * @mgr:        fpga manager structure
520  */
521 void fpga_mgr_put(struct fpga_manager *mgr)
522 {
523         module_put(mgr->dev.parent->driver->owner);
524         put_device(&mgr->dev);
525 }
526 EXPORT_SYMBOL_GPL(fpga_mgr_put);
527
528 /**
529  * fpga_mgr_lock - Lock FPGA manager for exclusive use
530  * @mgr:        fpga manager
531  *
532  * Given a pointer to FPGA Manager (from fpga_mgr_get() or
533  * of_fpga_mgr_put()) attempt to get the mutex. The user should call
534  * fpga_mgr_lock() and verify that it returns 0 before attempting to
535  * program the FPGA.  Likewise, the user should call fpga_mgr_unlock
536  * when done programming the FPGA.
537  *
538  * Return: 0 for success or -EBUSY
539  */
540 int fpga_mgr_lock(struct fpga_manager *mgr)
541 {
542         if (!mutex_trylock(&mgr->ref_mutex)) {
543                 dev_err(&mgr->dev, "FPGA manager is in use.\n");
544                 return -EBUSY;
545         }
546
547         return 0;
548 }
549 EXPORT_SYMBOL_GPL(fpga_mgr_lock);
550
551 /**
552  * fpga_mgr_unlock - Unlock FPGA manager after done programming
553  * @mgr:        fpga manager
554  */
555 void fpga_mgr_unlock(struct fpga_manager *mgr)
556 {
557         mutex_unlock(&mgr->ref_mutex);
558 }
559 EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
560
561 /**
562  * fpga_mgr_create - create and initialize an FPGA manager struct
563  * @parent:     fpga manager device from pdev
564  * @name:       fpga manager name
565  * @mops:       pointer to structure of fpga manager ops
566  * @priv:       fpga manager private data
567  *
568  * The caller of this function is responsible for freeing the struct with
569  * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
570  *
571  * Return: pointer to struct fpga_manager or NULL
572  */
573 struct fpga_manager *fpga_mgr_create(struct device *parent, const char *name,
574                                      const struct fpga_manager_ops *mops,
575                                      void *priv)
576 {
577         struct fpga_manager *mgr;
578         int id, ret;
579
580         if (!mops || !mops->write_complete || !mops->state ||
581             (!mops->write && !mops->write_sg) ||
582             (mops->write && mops->write_sg)) {
583                 dev_err(parent, "Attempt to register without fpga_manager_ops\n");
584                 return NULL;
585         }
586
587         if (!name || !strlen(name)) {
588                 dev_err(parent, "Attempt to register with no name!\n");
589                 return NULL;
590         }
591
592         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
593         if (!mgr)
594                 return NULL;
595
596         id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
597         if (id < 0)
598                 goto error_kfree;
599
600         mutex_init(&mgr->ref_mutex);
601
602         mgr->name = name;
603         mgr->mops = mops;
604         mgr->priv = priv;
605
606         device_initialize(&mgr->dev);
607         mgr->dev.class = fpga_mgr_class;
608         mgr->dev.groups = mops->groups;
609         mgr->dev.parent = parent;
610         mgr->dev.of_node = parent->of_node;
611         mgr->dev.id = id;
612
613         ret = dev_set_name(&mgr->dev, "fpga%d", id);
614         if (ret)
615                 goto error_device;
616
617         return mgr;
618
619 error_device:
620         ida_simple_remove(&fpga_mgr_ida, id);
621 error_kfree:
622         kfree(mgr);
623
624         return NULL;
625 }
626 EXPORT_SYMBOL_GPL(fpga_mgr_create);
627
628 /**
629  * fpga_mgr_free - free an FPGA manager created with fpga_mgr_create()
630  * @mgr:        fpga manager struct
631  */
632 void fpga_mgr_free(struct fpga_manager *mgr)
633 {
634         ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
635         kfree(mgr);
636 }
637 EXPORT_SYMBOL_GPL(fpga_mgr_free);
638
639 static void devm_fpga_mgr_release(struct device *dev, void *res)
640 {
641         struct fpga_mgr_devres *dr = res;
642
643         fpga_mgr_free(dr->mgr);
644 }
645
646 /**
647  * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
648  * @parent:     fpga manager device from pdev
649  * @name:       fpga manager name
650  * @mops:       pointer to structure of fpga manager ops
651  * @priv:       fpga manager private data
652  *
653  * This function is intended for use in an FPGA manager driver's probe function.
654  * After the manager driver creates the manager struct with
655  * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
656  * manager driver's remove function should call fpga_mgr_unregister().  The
657  * manager struct allocated with this function will be freed automatically on
658  * driver detach.  This includes the case of a probe function returning error
659  * before calling fpga_mgr_register(), the struct will still get cleaned up.
660  *
661  * Return: pointer to struct fpga_manager or NULL
662  */
663 struct fpga_manager *devm_fpga_mgr_create(struct device *parent, const char *name,
664                                           const struct fpga_manager_ops *mops,
665                                           void *priv)
666 {
667         struct fpga_mgr_devres *dr;
668
669         dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
670         if (!dr)
671                 return NULL;
672
673         dr->mgr = fpga_mgr_create(parent, name, mops, priv);
674         if (!dr->mgr) {
675                 devres_free(dr);
676                 return NULL;
677         }
678
679         devres_add(parent, dr);
680
681         return dr->mgr;
682 }
683 EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
684
685 /**
686  * fpga_mgr_register - register an FPGA manager
687  * @mgr: fpga manager struct
688  *
689  * Return: 0 on success, negative error code otherwise.
690  */
691 int fpga_mgr_register(struct fpga_manager *mgr)
692 {
693         int ret;
694
695         /*
696          * Initialize framework state by requesting low level driver read state
697          * from device.  FPGA may be in reset mode or may have been programmed
698          * by bootloader or EEPROM.
699          */
700         mgr->state = mgr->mops->state(mgr);
701
702         ret = device_add(&mgr->dev);
703         if (ret)
704                 goto error_device;
705
706         dev_info(&mgr->dev, "%s registered\n", mgr->name);
707
708         return 0;
709
710 error_device:
711         ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
712
713         return ret;
714 }
715 EXPORT_SYMBOL_GPL(fpga_mgr_register);
716
717 /**
718  * fpga_mgr_unregister - unregister an FPGA manager
719  * @mgr: fpga manager struct
720  *
721  * This function is intended for use in an FPGA manager driver's remove function.
722  */
723 void fpga_mgr_unregister(struct fpga_manager *mgr)
724 {
725         dev_info(&mgr->dev, "%s %s\n", __func__, mgr->name);
726
727         /*
728          * If the low level driver provides a method for putting fpga into
729          * a desired state upon unregister, do it.
730          */
731         if (mgr->mops->fpga_remove)
732                 mgr->mops->fpga_remove(mgr);
733
734         device_unregister(&mgr->dev);
735 }
736 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
737
738 static int fpga_mgr_devres_match(struct device *dev, void *res,
739                                  void *match_data)
740 {
741         struct fpga_mgr_devres *dr = res;
742
743         return match_data == dr->mgr;
744 }
745
746 static void devm_fpga_mgr_unregister(struct device *dev, void *res)
747 {
748         struct fpga_mgr_devres *dr = res;
749
750         fpga_mgr_unregister(dr->mgr);
751 }
752
753 /**
754  * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
755  * @dev: managing device for this FPGA manager
756  * @mgr: fpga manager struct
757  *
758  * This is the devres variant of fpga_mgr_register() for which the unregister
759  * function will be called automatically when the managing device is detached.
760  */
761 int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
762 {
763         struct fpga_mgr_devres *dr;
764         int ret;
765
766         /*
767          * Make sure that the struct fpga_manager * that is passed in is
768          * managed itself.
769          */
770         if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
771                                  fpga_mgr_devres_match, mgr)))
772                 return -EINVAL;
773
774         dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
775         if (!dr)
776                 return -ENOMEM;
777
778         ret = fpga_mgr_register(mgr);
779         if (ret) {
780                 devres_free(dr);
781                 return ret;
782         }
783
784         dr->mgr = mgr;
785         devres_add(dev, dr);
786
787         return 0;
788 }
789 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
790
791 static void fpga_mgr_dev_release(struct device *dev)
792 {
793 }
794
795 static int __init fpga_mgr_class_init(void)
796 {
797         pr_info("FPGA manager framework\n");
798
799         fpga_mgr_class = class_create(THIS_MODULE, "fpga_manager");
800         if (IS_ERR(fpga_mgr_class))
801                 return PTR_ERR(fpga_mgr_class);
802
803         fpga_mgr_class->dev_groups = fpga_mgr_groups;
804         fpga_mgr_class->dev_release = fpga_mgr_dev_release;
805
806         return 0;
807 }
808
809 static void __exit fpga_mgr_class_exit(void)
810 {
811         class_destroy(fpga_mgr_class);
812         ida_destroy(&fpga_mgr_ida);
813 }
814
815 MODULE_AUTHOR("Alan Tull <[email protected]>");
816 MODULE_DESCRIPTION("FPGA manager framework");
817 MODULE_LICENSE("GPL v2");
818
819 subsys_initcall(fpga_mgr_class_init);
820 module_exit(fpga_mgr_class_exit);
This page took 0.070646 seconds and 4 git commands to generate.