Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Provides user-space access to the SSAM EC via the /dev/surface/aggregator
0004  * misc device. Intended for debugging and development.
0005  *
0006  * Copyright (C) 2020-2022 Maximilian Luz <luzmaximilian@gmail.com>
0007  */
0008 
0009 #include <linux/fs.h>
0010 #include <linux/ioctl.h>
0011 #include <linux/kernel.h>
0012 #include <linux/kfifo.h>
0013 #include <linux/kref.h>
0014 #include <linux/miscdevice.h>
0015 #include <linux/module.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/poll.h>
0018 #include <linux/rwsem.h>
0019 #include <linux/slab.h>
0020 #include <linux/uaccess.h>
0021 #include <linux/vmalloc.h>
0022 
0023 #include <linux/surface_aggregator/cdev.h>
0024 #include <linux/surface_aggregator/controller.h>
0025 #include <linux/surface_aggregator/serial_hub.h>
0026 
0027 #define SSAM_CDEV_DEVICE_NAME   "surface_aggregator_cdev"
0028 
0029 
0030 /* -- Main structures. ------------------------------------------------------ */
0031 
0032 enum ssam_cdev_device_state {
0033     SSAM_CDEV_DEVICE_SHUTDOWN_BIT = BIT(0),
0034 };
0035 
0036 struct ssam_cdev {
0037     struct kref kref;
0038     struct rw_semaphore lock;
0039 
0040     struct device *dev;
0041     struct ssam_controller *ctrl;
0042     struct miscdevice mdev;
0043     unsigned long flags;
0044 
0045     struct rw_semaphore client_lock;  /* Guards client list. */
0046     struct list_head client_list;
0047 };
0048 
0049 struct ssam_cdev_client;
0050 
0051 struct ssam_cdev_notifier {
0052     struct ssam_cdev_client *client;
0053     struct ssam_event_notifier nf;
0054 };
0055 
0056 struct ssam_cdev_client {
0057     struct ssam_cdev *cdev;
0058     struct list_head node;
0059 
0060     struct mutex notifier_lock; /* Guards notifier access for registration */
0061     struct ssam_cdev_notifier *notifier[SSH_NUM_EVENTS];
0062 
0063     struct mutex read_lock;     /* Guards FIFO buffer read access */
0064     struct mutex write_lock;    /* Guards FIFO buffer write access */
0065     DECLARE_KFIFO(buffer, u8, 4096);
0066 
0067     wait_queue_head_t waitq;
0068     struct fasync_struct *fasync;
0069 };
0070 
0071 static void __ssam_cdev_release(struct kref *kref)
0072 {
0073     kfree(container_of(kref, struct ssam_cdev, kref));
0074 }
0075 
0076 static struct ssam_cdev *ssam_cdev_get(struct ssam_cdev *cdev)
0077 {
0078     if (cdev)
0079         kref_get(&cdev->kref);
0080 
0081     return cdev;
0082 }
0083 
0084 static void ssam_cdev_put(struct ssam_cdev *cdev)
0085 {
0086     if (cdev)
0087         kref_put(&cdev->kref, __ssam_cdev_release);
0088 }
0089 
0090 
0091 /* -- Notifier handling. ---------------------------------------------------- */
0092 
0093 static u32 ssam_cdev_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
0094 {
0095     struct ssam_cdev_notifier *cdev_nf = container_of(nf, struct ssam_cdev_notifier, nf);
0096     struct ssam_cdev_client *client = cdev_nf->client;
0097     struct ssam_cdev_event event;
0098     size_t n = struct_size(&event, data, in->length);
0099 
0100     /* Translate event. */
0101     event.target_category = in->target_category;
0102     event.target_id = in->target_id;
0103     event.command_id = in->command_id;
0104     event.instance_id = in->instance_id;
0105     event.length = in->length;
0106 
0107     mutex_lock(&client->write_lock);
0108 
0109     /* Make sure we have enough space. */
0110     if (kfifo_avail(&client->buffer) < n) {
0111         dev_warn(client->cdev->dev,
0112              "buffer full, dropping event (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
0113              in->target_category, in->target_id, in->command_id, in->instance_id);
0114         mutex_unlock(&client->write_lock);
0115         return 0;
0116     }
0117 
0118     /* Copy event header and payload. */
0119     kfifo_in(&client->buffer, (const u8 *)&event, struct_size(&event, data, 0));
0120     kfifo_in(&client->buffer, &in->data[0], in->length);
0121 
0122     mutex_unlock(&client->write_lock);
0123 
0124     /* Notify waiting readers. */
0125     kill_fasync(&client->fasync, SIGIO, POLL_IN);
0126     wake_up_interruptible(&client->waitq);
0127 
0128     /*
0129      * Don't mark events as handled, this is the job of a proper driver and
0130      * not the debugging interface.
0131      */
0132     return 0;
0133 }
0134 
0135 static int ssam_cdev_notifier_register(struct ssam_cdev_client *client, u8 tc, int priority)
0136 {
0137     const u16 rqid = ssh_tc_to_rqid(tc);
0138     const u16 event = ssh_rqid_to_event(rqid);
0139     struct ssam_cdev_notifier *nf;
0140     int status;
0141 
0142     lockdep_assert_held_read(&client->cdev->lock);
0143 
0144     /* Validate notifier target category. */
0145     if (!ssh_rqid_is_event(rqid))
0146         return -EINVAL;
0147 
0148     mutex_lock(&client->notifier_lock);
0149 
0150     /* Check if the notifier has already been registered. */
0151     if (client->notifier[event]) {
0152         mutex_unlock(&client->notifier_lock);
0153         return -EEXIST;
0154     }
0155 
0156     /* Allocate new notifier. */
0157     nf = kzalloc(sizeof(*nf), GFP_KERNEL);
0158     if (!nf) {
0159         mutex_unlock(&client->notifier_lock);
0160         return -ENOMEM;
0161     }
0162 
0163     /*
0164      * Create a dummy notifier with the minimal required fields for
0165      * observer registration. Note that we can skip fully specifying event
0166      * and registry here as we do not need any matching and use silent
0167      * registration, which does not enable the corresponding event.
0168      */
0169     nf->client = client;
0170     nf->nf.base.fn = ssam_cdev_notifier;
0171     nf->nf.base.priority = priority;
0172     nf->nf.event.id.target_category = tc;
0173     nf->nf.event.mask = 0;  /* Do not do any matching. */
0174     nf->nf.flags = SSAM_EVENT_NOTIFIER_OBSERVER;
0175 
0176     /* Register notifier. */
0177     status = ssam_notifier_register(client->cdev->ctrl, &nf->nf);
0178     if (status)
0179         kfree(nf);
0180     else
0181         client->notifier[event] = nf;
0182 
0183     mutex_unlock(&client->notifier_lock);
0184     return status;
0185 }
0186 
0187 static int ssam_cdev_notifier_unregister(struct ssam_cdev_client *client, u8 tc)
0188 {
0189     const u16 rqid = ssh_tc_to_rqid(tc);
0190     const u16 event = ssh_rqid_to_event(rqid);
0191     int status;
0192 
0193     lockdep_assert_held_read(&client->cdev->lock);
0194 
0195     /* Validate notifier target category. */
0196     if (!ssh_rqid_is_event(rqid))
0197         return -EINVAL;
0198 
0199     mutex_lock(&client->notifier_lock);
0200 
0201     /* Check if the notifier is currently registered. */
0202     if (!client->notifier[event]) {
0203         mutex_unlock(&client->notifier_lock);
0204         return -ENOENT;
0205     }
0206 
0207     /* Unregister and free notifier. */
0208     status = ssam_notifier_unregister(client->cdev->ctrl, &client->notifier[event]->nf);
0209     kfree(client->notifier[event]);
0210     client->notifier[event] = NULL;
0211 
0212     mutex_unlock(&client->notifier_lock);
0213     return status;
0214 }
0215 
0216 static void ssam_cdev_notifier_unregister_all(struct ssam_cdev_client *client)
0217 {
0218     int i;
0219 
0220     down_read(&client->cdev->lock);
0221 
0222     /*
0223      * This function may be used during shutdown, thus we need to test for
0224      * cdev->ctrl instead of the SSAM_CDEV_DEVICE_SHUTDOWN_BIT bit.
0225      */
0226     if (client->cdev->ctrl) {
0227         for (i = 0; i < SSH_NUM_EVENTS; i++)
0228             ssam_cdev_notifier_unregister(client, i + 1);
0229 
0230     } else {
0231         int count = 0;
0232 
0233         /*
0234          * Device has been shut down. Any notifier remaining is a bug,
0235          * so warn about that as this would otherwise hardly be
0236          * noticeable. Nevertheless, free them as well.
0237          */
0238         mutex_lock(&client->notifier_lock);
0239         for (i = 0; i < SSH_NUM_EVENTS; i++) {
0240             count += !!(client->notifier[i]);
0241             kfree(client->notifier[i]);
0242             client->notifier[i] = NULL;
0243         }
0244         mutex_unlock(&client->notifier_lock);
0245 
0246         WARN_ON(count > 0);
0247     }
0248 
0249     up_read(&client->cdev->lock);
0250 }
0251 
0252 
0253 /* -- IOCTL functions. ------------------------------------------------------ */
0254 
0255 static long ssam_cdev_request(struct ssam_cdev_client *client, struct ssam_cdev_request __user *r)
0256 {
0257     struct ssam_cdev_request rqst;
0258     struct ssam_request spec = {};
0259     struct ssam_response rsp = {};
0260     const void __user *plddata;
0261     void __user *rspdata;
0262     int status = 0, ret = 0, tmp;
0263 
0264     lockdep_assert_held_read(&client->cdev->lock);
0265 
0266     ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
0267     if (ret)
0268         goto out;
0269 
0270     plddata = u64_to_user_ptr(rqst.payload.data);
0271     rspdata = u64_to_user_ptr(rqst.response.data);
0272 
0273     /* Setup basic request fields. */
0274     spec.target_category = rqst.target_category;
0275     spec.target_id = rqst.target_id;
0276     spec.command_id = rqst.command_id;
0277     spec.instance_id = rqst.instance_id;
0278     spec.flags = 0;
0279     spec.length = rqst.payload.length;
0280     spec.payload = NULL;
0281 
0282     if (rqst.flags & SSAM_CDEV_REQUEST_HAS_RESPONSE)
0283         spec.flags |= SSAM_REQUEST_HAS_RESPONSE;
0284 
0285     if (rqst.flags & SSAM_CDEV_REQUEST_UNSEQUENCED)
0286         spec.flags |= SSAM_REQUEST_UNSEQUENCED;
0287 
0288     rsp.capacity = rqst.response.length;
0289     rsp.length = 0;
0290     rsp.pointer = NULL;
0291 
0292     /* Get request payload from user-space. */
0293     if (spec.length) {
0294         if (!plddata) {
0295             ret = -EINVAL;
0296             goto out;
0297         }
0298 
0299         /*
0300          * Note: spec.length is limited to U16_MAX bytes via struct
0301          * ssam_cdev_request. This is slightly larger than the
0302          * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
0303          * underlying protocol (note that nothing remotely this size
0304          * should ever be allocated in any normal case). This size is
0305          * validated later in ssam_request_sync(), for allocation the
0306          * bound imposed by u16 should be enough.
0307          */
0308         spec.payload = kzalloc(spec.length, GFP_KERNEL);
0309         if (!spec.payload) {
0310             ret = -ENOMEM;
0311             goto out;
0312         }
0313 
0314         if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
0315             ret = -EFAULT;
0316             goto out;
0317         }
0318     }
0319 
0320     /* Allocate response buffer. */
0321     if (rsp.capacity) {
0322         if (!rspdata) {
0323             ret = -EINVAL;
0324             goto out;
0325         }
0326 
0327         /*
0328          * Note: rsp.capacity is limited to U16_MAX bytes via struct
0329          * ssam_cdev_request. This is slightly larger than the
0330          * theoretical maximum (SSH_COMMAND_MAX_PAYLOAD_SIZE) of the
0331          * underlying protocol (note that nothing remotely this size
0332          * should ever be allocated in any normal case). In later use,
0333          * this capacity does not have to be strictly bounded, as it
0334          * is only used as an output buffer to be written to. For
0335          * allocation the bound imposed by u16 should be enough.
0336          */
0337         rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
0338         if (!rsp.pointer) {
0339             ret = -ENOMEM;
0340             goto out;
0341         }
0342     }
0343 
0344     /* Perform request. */
0345     status = ssam_request_sync(client->cdev->ctrl, &spec, &rsp);
0346     if (status)
0347         goto out;
0348 
0349     /* Copy response to user-space. */
0350     if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
0351         ret = -EFAULT;
0352 
0353 out:
0354     /* Always try to set response-length and status. */
0355     tmp = put_user(rsp.length, &r->response.length);
0356     if (tmp)
0357         ret = tmp;
0358 
0359     tmp = put_user(status, &r->status);
0360     if (tmp)
0361         ret = tmp;
0362 
0363     /* Cleanup. */
0364     kfree(spec.payload);
0365     kfree(rsp.pointer);
0366 
0367     return ret;
0368 }
0369 
0370 static long ssam_cdev_notif_register(struct ssam_cdev_client *client,
0371                      const struct ssam_cdev_notifier_desc __user *d)
0372 {
0373     struct ssam_cdev_notifier_desc desc;
0374     long ret;
0375 
0376     lockdep_assert_held_read(&client->cdev->lock);
0377 
0378     ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
0379     if (ret)
0380         return ret;
0381 
0382     return ssam_cdev_notifier_register(client, desc.target_category, desc.priority);
0383 }
0384 
0385 static long ssam_cdev_notif_unregister(struct ssam_cdev_client *client,
0386                        const struct ssam_cdev_notifier_desc __user *d)
0387 {
0388     struct ssam_cdev_notifier_desc desc;
0389     long ret;
0390 
0391     lockdep_assert_held_read(&client->cdev->lock);
0392 
0393     ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
0394     if (ret)
0395         return ret;
0396 
0397     return ssam_cdev_notifier_unregister(client, desc.target_category);
0398 }
0399 
0400 static long ssam_cdev_event_enable(struct ssam_cdev_client *client,
0401                    const struct ssam_cdev_event_desc __user *d)
0402 {
0403     struct ssam_cdev_event_desc desc;
0404     struct ssam_event_registry reg;
0405     struct ssam_event_id id;
0406     long ret;
0407 
0408     lockdep_assert_held_read(&client->cdev->lock);
0409 
0410     /* Read descriptor from user-space. */
0411     ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
0412     if (ret)
0413         return ret;
0414 
0415     /* Translate descriptor. */
0416     reg.target_category = desc.reg.target_category;
0417     reg.target_id = desc.reg.target_id;
0418     reg.cid_enable = desc.reg.cid_enable;
0419     reg.cid_disable = desc.reg.cid_disable;
0420 
0421     id.target_category = desc.id.target_category;
0422     id.instance = desc.id.instance;
0423 
0424     /* Disable event. */
0425     return ssam_controller_event_enable(client->cdev->ctrl, reg, id, desc.flags);
0426 }
0427 
0428 static long ssam_cdev_event_disable(struct ssam_cdev_client *client,
0429                     const struct ssam_cdev_event_desc __user *d)
0430 {
0431     struct ssam_cdev_event_desc desc;
0432     struct ssam_event_registry reg;
0433     struct ssam_event_id id;
0434     long ret;
0435 
0436     lockdep_assert_held_read(&client->cdev->lock);
0437 
0438     /* Read descriptor from user-space. */
0439     ret = copy_struct_from_user(&desc, sizeof(desc), d, sizeof(*d));
0440     if (ret)
0441         return ret;
0442 
0443     /* Translate descriptor. */
0444     reg.target_category = desc.reg.target_category;
0445     reg.target_id = desc.reg.target_id;
0446     reg.cid_enable = desc.reg.cid_enable;
0447     reg.cid_disable = desc.reg.cid_disable;
0448 
0449     id.target_category = desc.id.target_category;
0450     id.instance = desc.id.instance;
0451 
0452     /* Disable event. */
0453     return ssam_controller_event_disable(client->cdev->ctrl, reg, id, desc.flags);
0454 }
0455 
0456 
0457 /* -- File operations. ------------------------------------------------------ */
0458 
0459 static int ssam_cdev_device_open(struct inode *inode, struct file *filp)
0460 {
0461     struct miscdevice *mdev = filp->private_data;
0462     struct ssam_cdev_client *client;
0463     struct ssam_cdev *cdev = container_of(mdev, struct ssam_cdev, mdev);
0464 
0465     /* Initialize client */
0466     client = vzalloc(sizeof(*client));
0467     if (!client)
0468         return -ENOMEM;
0469 
0470     client->cdev = ssam_cdev_get(cdev);
0471 
0472     INIT_LIST_HEAD(&client->node);
0473 
0474     mutex_init(&client->notifier_lock);
0475 
0476     mutex_init(&client->read_lock);
0477     mutex_init(&client->write_lock);
0478     INIT_KFIFO(client->buffer);
0479     init_waitqueue_head(&client->waitq);
0480 
0481     filp->private_data = client;
0482 
0483     /* Attach client. */
0484     down_write(&cdev->client_lock);
0485 
0486     if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
0487         up_write(&cdev->client_lock);
0488         mutex_destroy(&client->write_lock);
0489         mutex_destroy(&client->read_lock);
0490         mutex_destroy(&client->notifier_lock);
0491         ssam_cdev_put(client->cdev);
0492         vfree(client);
0493         return -ENODEV;
0494     }
0495     list_add_tail(&client->node, &cdev->client_list);
0496 
0497     up_write(&cdev->client_lock);
0498 
0499     stream_open(inode, filp);
0500     return 0;
0501 }
0502 
0503 static int ssam_cdev_device_release(struct inode *inode, struct file *filp)
0504 {
0505     struct ssam_cdev_client *client = filp->private_data;
0506 
0507     /* Force-unregister all remaining notifiers of this client. */
0508     ssam_cdev_notifier_unregister_all(client);
0509 
0510     /* Detach client. */
0511     down_write(&client->cdev->client_lock);
0512     list_del(&client->node);
0513     up_write(&client->cdev->client_lock);
0514 
0515     /* Free client. */
0516     mutex_destroy(&client->write_lock);
0517     mutex_destroy(&client->read_lock);
0518 
0519     mutex_destroy(&client->notifier_lock);
0520 
0521     ssam_cdev_put(client->cdev);
0522     vfree(client);
0523 
0524     return 0;
0525 }
0526 
0527 static long __ssam_cdev_device_ioctl(struct ssam_cdev_client *client, unsigned int cmd,
0528                      unsigned long arg)
0529 {
0530     lockdep_assert_held_read(&client->cdev->lock);
0531 
0532     switch (cmd) {
0533     case SSAM_CDEV_REQUEST:
0534         return ssam_cdev_request(client, (struct ssam_cdev_request __user *)arg);
0535 
0536     case SSAM_CDEV_NOTIF_REGISTER:
0537         return ssam_cdev_notif_register(client,
0538                         (struct ssam_cdev_notifier_desc __user *)arg);
0539 
0540     case SSAM_CDEV_NOTIF_UNREGISTER:
0541         return ssam_cdev_notif_unregister(client,
0542                           (struct ssam_cdev_notifier_desc __user *)arg);
0543 
0544     case SSAM_CDEV_EVENT_ENABLE:
0545         return ssam_cdev_event_enable(client, (struct ssam_cdev_event_desc __user *)arg);
0546 
0547     case SSAM_CDEV_EVENT_DISABLE:
0548         return ssam_cdev_event_disable(client, (struct ssam_cdev_event_desc __user *)arg);
0549 
0550     default:
0551         return -ENOTTY;
0552     }
0553 }
0554 
0555 static long ssam_cdev_device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0556 {
0557     struct ssam_cdev_client *client = file->private_data;
0558     long status;
0559 
0560     /* Ensure that controller is valid for as long as we need it. */
0561     if (down_read_killable(&client->cdev->lock))
0562         return -ERESTARTSYS;
0563 
0564     if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags)) {
0565         up_read(&client->cdev->lock);
0566         return -ENODEV;
0567     }
0568 
0569     status = __ssam_cdev_device_ioctl(client, cmd, arg);
0570 
0571     up_read(&client->cdev->lock);
0572     return status;
0573 }
0574 
0575 static ssize_t ssam_cdev_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
0576 {
0577     struct ssam_cdev_client *client = file->private_data;
0578     struct ssam_cdev *cdev = client->cdev;
0579     unsigned int copied;
0580     int status = 0;
0581 
0582     if (down_read_killable(&cdev->lock))
0583         return -ERESTARTSYS;
0584 
0585     /* Make sure we're not shut down. */
0586     if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
0587         up_read(&cdev->lock);
0588         return -ENODEV;
0589     }
0590 
0591     do {
0592         /* Check availability, wait if necessary. */
0593         if (kfifo_is_empty(&client->buffer)) {
0594             up_read(&cdev->lock);
0595 
0596             if (file->f_flags & O_NONBLOCK)
0597                 return -EAGAIN;
0598 
0599             status = wait_event_interruptible(client->waitq,
0600                               !kfifo_is_empty(&client->buffer) ||
0601                               test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT,
0602                                    &cdev->flags));
0603             if (status < 0)
0604                 return status;
0605 
0606             if (down_read_killable(&cdev->lock))
0607                 return -ERESTARTSYS;
0608 
0609             /* Need to check that we're not shut down again. */
0610             if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags)) {
0611                 up_read(&cdev->lock);
0612                 return -ENODEV;
0613             }
0614         }
0615 
0616         /* Try to read from FIFO. */
0617         if (mutex_lock_interruptible(&client->read_lock)) {
0618             up_read(&cdev->lock);
0619             return -ERESTARTSYS;
0620         }
0621 
0622         status = kfifo_to_user(&client->buffer, buf, count, &copied);
0623         mutex_unlock(&client->read_lock);
0624 
0625         if (status < 0) {
0626             up_read(&cdev->lock);
0627             return status;
0628         }
0629 
0630         /* We might not have gotten anything, check this here. */
0631         if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
0632             up_read(&cdev->lock);
0633             return -EAGAIN;
0634         }
0635     } while (copied == 0);
0636 
0637     up_read(&cdev->lock);
0638     return copied;
0639 }
0640 
0641 static __poll_t ssam_cdev_poll(struct file *file, struct poll_table_struct *pt)
0642 {
0643     struct ssam_cdev_client *client = file->private_data;
0644     __poll_t events = 0;
0645 
0646     if (test_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &client->cdev->flags))
0647         return EPOLLHUP | EPOLLERR;
0648 
0649     poll_wait(file, &client->waitq, pt);
0650 
0651     if (!kfifo_is_empty(&client->buffer))
0652         events |= EPOLLIN | EPOLLRDNORM;
0653 
0654     return events;
0655 }
0656 
0657 static int ssam_cdev_fasync(int fd, struct file *file, int on)
0658 {
0659     struct ssam_cdev_client *client = file->private_data;
0660 
0661     return fasync_helper(fd, file, on, &client->fasync);
0662 }
0663 
0664 static const struct file_operations ssam_controller_fops = {
0665     .owner          = THIS_MODULE,
0666     .open           = ssam_cdev_device_open,
0667     .release        = ssam_cdev_device_release,
0668     .read           = ssam_cdev_read,
0669     .poll           = ssam_cdev_poll,
0670     .fasync         = ssam_cdev_fasync,
0671     .unlocked_ioctl = ssam_cdev_device_ioctl,
0672     .compat_ioctl   = ssam_cdev_device_ioctl,
0673     .llseek         = no_llseek,
0674 };
0675 
0676 
0677 /* -- Device and driver setup ----------------------------------------------- */
0678 
0679 static int ssam_dbg_device_probe(struct platform_device *pdev)
0680 {
0681     struct ssam_controller *ctrl;
0682     struct ssam_cdev *cdev;
0683     int status;
0684 
0685     ctrl = ssam_client_bind(&pdev->dev);
0686     if (IS_ERR(ctrl))
0687         return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
0688 
0689     cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
0690     if (!cdev)
0691         return -ENOMEM;
0692 
0693     kref_init(&cdev->kref);
0694     init_rwsem(&cdev->lock);
0695     cdev->ctrl = ctrl;
0696     cdev->dev = &pdev->dev;
0697 
0698     cdev->mdev.parent   = &pdev->dev;
0699     cdev->mdev.minor    = MISC_DYNAMIC_MINOR;
0700     cdev->mdev.name     = "surface_aggregator";
0701     cdev->mdev.nodename = "surface/aggregator";
0702     cdev->mdev.fops     = &ssam_controller_fops;
0703 
0704     init_rwsem(&cdev->client_lock);
0705     INIT_LIST_HEAD(&cdev->client_list);
0706 
0707     status = misc_register(&cdev->mdev);
0708     if (status) {
0709         kfree(cdev);
0710         return status;
0711     }
0712 
0713     platform_set_drvdata(pdev, cdev);
0714     return 0;
0715 }
0716 
0717 static int ssam_dbg_device_remove(struct platform_device *pdev)
0718 {
0719     struct ssam_cdev *cdev = platform_get_drvdata(pdev);
0720     struct ssam_cdev_client *client;
0721 
0722     /*
0723      * Mark device as shut-down. Prevent new clients from being added and
0724      * new operations from being executed.
0725      */
0726     set_bit(SSAM_CDEV_DEVICE_SHUTDOWN_BIT, &cdev->flags);
0727 
0728     down_write(&cdev->client_lock);
0729 
0730     /* Remove all notifiers registered by us. */
0731     list_for_each_entry(client, &cdev->client_list, node) {
0732         ssam_cdev_notifier_unregister_all(client);
0733     }
0734 
0735     /* Wake up async clients. */
0736     list_for_each_entry(client, &cdev->client_list, node) {
0737         kill_fasync(&client->fasync, SIGIO, POLL_HUP);
0738     }
0739 
0740     /* Wake up blocking clients. */
0741     list_for_each_entry(client, &cdev->client_list, node) {
0742         wake_up_interruptible(&client->waitq);
0743     }
0744 
0745     up_write(&cdev->client_lock);
0746 
0747     /*
0748      * The controller is only guaranteed to be valid for as long as the
0749      * driver is bound. Remove controller so that any lingering open files
0750      * cannot access it any more after we're gone.
0751      */
0752     down_write(&cdev->lock);
0753     cdev->ctrl = NULL;
0754     cdev->dev = NULL;
0755     up_write(&cdev->lock);
0756 
0757     misc_deregister(&cdev->mdev);
0758 
0759     ssam_cdev_put(cdev);
0760     return 0;
0761 }
0762 
0763 static struct platform_device *ssam_cdev_device;
0764 
0765 static struct platform_driver ssam_cdev_driver = {
0766     .probe = ssam_dbg_device_probe,
0767     .remove = ssam_dbg_device_remove,
0768     .driver = {
0769         .name = SSAM_CDEV_DEVICE_NAME,
0770         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0771     },
0772 };
0773 
0774 static int __init ssam_debug_init(void)
0775 {
0776     int status;
0777 
0778     ssam_cdev_device = platform_device_alloc(SSAM_CDEV_DEVICE_NAME,
0779                          PLATFORM_DEVID_NONE);
0780     if (!ssam_cdev_device)
0781         return -ENOMEM;
0782 
0783     status = platform_device_add(ssam_cdev_device);
0784     if (status)
0785         goto err_device;
0786 
0787     status = platform_driver_register(&ssam_cdev_driver);
0788     if (status)
0789         goto err_driver;
0790 
0791     return 0;
0792 
0793 err_driver:
0794     platform_device_del(ssam_cdev_device);
0795 err_device:
0796     platform_device_put(ssam_cdev_device);
0797     return status;
0798 }
0799 module_init(ssam_debug_init);
0800 
0801 static void __exit ssam_debug_exit(void)
0802 {
0803     platform_driver_unregister(&ssam_cdev_driver);
0804     platform_device_unregister(ssam_cdev_device);
0805 }
0806 module_exit(ssam_debug_exit);
0807 
0808 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
0809 MODULE_DESCRIPTION("User-space interface for Surface System Aggregator Module");
0810 MODULE_LICENSE("GPL");