LCOV - code coverage report
Current view: top level - block - bsg.c (source / functions) Hit Total Coverage
Test: Real Lines: 14 190 7.4 %
Date: 2020-10-17 15:46:16 Functions: 0 20 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  * bsg.c - block layer implementation of the sg v4 interface
       4                 :            :  */
       5                 :            : #include <linux/module.h>
       6                 :            : #include <linux/init.h>
       7                 :            : #include <linux/file.h>
       8                 :            : #include <linux/blkdev.h>
       9                 :            : #include <linux/cdev.h>
      10                 :            : #include <linux/jiffies.h>
      11                 :            : #include <linux/percpu.h>
      12                 :            : #include <linux/idr.h>
      13                 :            : #include <linux/bsg.h>
      14                 :            : #include <linux/slab.h>
      15                 :            : 
      16                 :            : #include <scsi/scsi.h>
      17                 :            : #include <scsi/scsi_ioctl.h>
      18                 :            : #include <scsi/scsi_cmnd.h>
      19                 :            : #include <scsi/scsi_device.h>
      20                 :            : #include <scsi/scsi_driver.h>
      21                 :            : #include <scsi/sg.h>
      22                 :            : 
      23                 :            : #define BSG_DESCRIPTION "Block layer SCSI generic (bsg) driver"
      24                 :            : #define BSG_VERSION     "0.4"
      25                 :            : 
      26                 :            : #define bsg_dbg(bd, fmt, ...) \
      27                 :            :         pr_debug("%s: " fmt, (bd)->name, ##__VA_ARGS__)
      28                 :            : 
      29                 :            : struct bsg_device {
      30                 :            :         struct request_queue *queue;
      31                 :            :         spinlock_t lock;
      32                 :            :         struct hlist_node dev_list;
      33                 :            :         refcount_t ref_count;
      34                 :            :         char name[20];
      35                 :            :         int max_queue;
      36                 :            : };
      37                 :            : 
      38                 :            : #define BSG_DEFAULT_CMDS        64
      39                 :            : #define BSG_MAX_DEVS            32768
      40                 :            : 
      41                 :            : static DEFINE_MUTEX(bsg_mutex);
      42                 :            : static DEFINE_IDR(bsg_minor_idr);
      43                 :            : 
      44                 :            : #define BSG_LIST_ARRAY_SIZE     8
      45                 :            : static struct hlist_head bsg_device_list[BSG_LIST_ARRAY_SIZE];
      46                 :            : 
      47                 :            : static struct class *bsg_class;
      48                 :            : static int bsg_major;
      49                 :            : 
      50                 :            : static inline struct hlist_head *bsg_dev_idx_hash(int index)
      51                 :            : {
      52                 :          0 :         return &bsg_device_list[index & (BSG_LIST_ARRAY_SIZE - 1)];
      53                 :            : }
      54                 :            : 
      55                 :            : #define uptr64(val) ((void __user *)(uintptr_t)(val))
      56                 :            : 
      57                 :          0 : static int bsg_scsi_check_proto(struct sg_io_v4 *hdr)
      58                 :            : {
      59                 :          0 :         if (hdr->protocol != BSG_PROTOCOL_SCSI  ||
      60                 :          0 :             hdr->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
      61                 :            :                 return -EINVAL;
      62                 :          0 :         return 0;
      63                 :            : }
      64                 :            : 
      65                 :          0 : static int bsg_scsi_fill_hdr(struct request *rq, struct sg_io_v4 *hdr,
      66                 :            :                 fmode_t mode)
      67                 :            : {
      68                 :            :         struct scsi_request *sreq = scsi_req(rq);
      69                 :            : 
      70                 :          0 :         if (hdr->dout_xfer_len && hdr->din_xfer_len) {
      71                 :          0 :                 pr_warn_once("BIDI support in bsg has been removed.\n");
      72                 :            :                 return -EOPNOTSUPP;
      73                 :            :         }
      74                 :            : 
      75                 :          0 :         sreq->cmd_len = hdr->request_len;
      76                 :          0 :         if (sreq->cmd_len > BLK_MAX_CDB) {
      77                 :          0 :                 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
      78                 :          0 :                 if (!sreq->cmd)
      79                 :            :                         return -ENOMEM;
      80                 :            :         }
      81                 :            : 
      82                 :          0 :         if (copy_from_user(sreq->cmd, uptr64(hdr->request), sreq->cmd_len))
      83                 :            :                 return -EFAULT;
      84                 :          0 :         if (blk_verify_command(sreq->cmd, mode))
      85                 :            :                 return -EPERM;
      86                 :          0 :         return 0;
      87                 :            : }
      88                 :            : 
      89                 :          0 : static int bsg_scsi_complete_rq(struct request *rq, struct sg_io_v4 *hdr)
      90                 :            : {
      91                 :            :         struct scsi_request *sreq = scsi_req(rq);
      92                 :            :         int ret = 0;
      93                 :            : 
      94                 :            :         /*
      95                 :            :          * fill in all the output members
      96                 :            :          */
      97                 :          0 :         hdr->device_status = sreq->result & 0xff;
      98                 :          0 :         hdr->transport_status = host_byte(sreq->result);
      99                 :          0 :         hdr->driver_status = driver_byte(sreq->result);
     100                 :          0 :         hdr->info = 0;
     101                 :          0 :         if (hdr->device_status || hdr->transport_status || hdr->driver_status)
     102                 :          0 :                 hdr->info |= SG_INFO_CHECK;
     103                 :          0 :         hdr->response_len = 0;
     104                 :            : 
     105                 :          0 :         if (sreq->sense_len && hdr->response) {
     106                 :          0 :                 int len = min_t(unsigned int, hdr->max_response_len,
     107                 :            :                                         sreq->sense_len);
     108                 :            : 
     109                 :          0 :                 if (copy_to_user(uptr64(hdr->response), sreq->sense, len))
     110                 :            :                         ret = -EFAULT;
     111                 :            :                 else
     112                 :          0 :                         hdr->response_len = len;
     113                 :            :         }
     114                 :            : 
     115                 :          0 :         if (rq_data_dir(rq) == READ)
     116                 :          0 :                 hdr->din_resid = sreq->resid_len;
     117                 :            :         else
     118                 :          0 :                 hdr->dout_resid = sreq->resid_len;
     119                 :            : 
     120                 :          0 :         return ret;
     121                 :            : }
     122                 :            : 
     123                 :          0 : static void bsg_scsi_free_rq(struct request *rq)
     124                 :            : {
     125                 :            :         scsi_req_free_cmd(scsi_req(rq));
     126                 :          0 : }
     127                 :            : 
     128                 :            : static const struct bsg_ops bsg_scsi_ops = {
     129                 :            :         .check_proto            = bsg_scsi_check_proto,
     130                 :            :         .fill_hdr               = bsg_scsi_fill_hdr,
     131                 :            :         .complete_rq            = bsg_scsi_complete_rq,
     132                 :            :         .free_rq                = bsg_scsi_free_rq,
     133                 :            : };
     134                 :            : 
     135                 :          0 : static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
     136                 :            : {
     137                 :            :         struct request *rq;
     138                 :            :         struct bio *bio;
     139                 :            :         struct sg_io_v4 hdr;
     140                 :            :         int ret;
     141                 :            : 
     142                 :          0 :         if (copy_from_user(&hdr, uarg, sizeof(hdr)))
     143                 :            :                 return -EFAULT;
     144                 :            : 
     145                 :          0 :         if (!q->bsg_dev.class_dev)
     146                 :            :                 return -ENXIO;
     147                 :            : 
     148                 :          0 :         if (hdr.guard != 'Q')
     149                 :            :                 return -EINVAL;
     150                 :          0 :         ret = q->bsg_dev.ops->check_proto(&hdr);
     151                 :          0 :         if (ret)
     152                 :            :                 return ret;
     153                 :            : 
     154                 :          0 :         rq = blk_get_request(q, hdr.dout_xfer_len ?
     155                 :            :                         REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
     156                 :          0 :         if (IS_ERR(rq))
     157                 :          0 :                 return PTR_ERR(rq);
     158                 :            : 
     159                 :          0 :         ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
     160                 :          0 :         if (ret)
     161                 :            :                 return ret;
     162                 :            : 
     163                 :          0 :         rq->timeout = msecs_to_jiffies(hdr.timeout);
     164                 :          0 :         if (!rq->timeout)
     165                 :          0 :                 rq->timeout = q->sg_timeout;
     166                 :          0 :         if (!rq->timeout)
     167                 :          0 :                 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
     168                 :          0 :         if (rq->timeout < BLK_MIN_SG_TIMEOUT)
     169                 :          0 :                 rq->timeout = BLK_MIN_SG_TIMEOUT;
     170                 :            : 
     171                 :          0 :         if (hdr.dout_xfer_len) {
     172                 :          0 :                 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
     173                 :            :                                 hdr.dout_xfer_len, GFP_KERNEL);
     174                 :          0 :         } else if (hdr.din_xfer_len) {
     175                 :          0 :                 ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
     176                 :            :                                 hdr.din_xfer_len, GFP_KERNEL);
     177                 :            :         }
     178                 :            : 
     179                 :          0 :         if (ret)
     180                 :            :                 goto out_free_rq;
     181                 :            : 
     182                 :          0 :         bio = rq->bio;
     183                 :            : 
     184                 :          0 :         blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
     185                 :          0 :         ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
     186                 :          0 :         blk_rq_unmap_user(bio);
     187                 :            : 
     188                 :            : out_free_rq:
     189                 :          0 :         rq->q->bsg_dev.ops->free_rq(rq);
     190                 :          0 :         blk_put_request(rq);
     191                 :          0 :         if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
     192                 :            :                 return -EFAULT;
     193                 :          0 :         return ret;
     194                 :            : }
     195                 :            : 
     196                 :          0 : static struct bsg_device *bsg_alloc_device(void)
     197                 :            : {
     198                 :            :         struct bsg_device *bd;
     199                 :            : 
     200                 :          0 :         bd = kzalloc(sizeof(struct bsg_device), GFP_KERNEL);
     201                 :          0 :         if (unlikely(!bd))
     202                 :            :                 return NULL;
     203                 :            : 
     204                 :          0 :         spin_lock_init(&bd->lock);
     205                 :          0 :         bd->max_queue = BSG_DEFAULT_CMDS;
     206                 :            :         INIT_HLIST_NODE(&bd->dev_list);
     207                 :          0 :         return bd;
     208                 :            : }
     209                 :            : 
     210                 :          0 : static int bsg_put_device(struct bsg_device *bd)
     211                 :            : {
     212                 :          0 :         struct request_queue *q = bd->queue;
     213                 :            : 
     214                 :          0 :         mutex_lock(&bsg_mutex);
     215                 :            : 
     216                 :          0 :         if (!refcount_dec_and_test(&bd->ref_count)) {
     217                 :          0 :                 mutex_unlock(&bsg_mutex);
     218                 :          0 :                 return 0;
     219                 :            :         }
     220                 :            : 
     221                 :            :         hlist_del(&bd->dev_list);
     222                 :          0 :         mutex_unlock(&bsg_mutex);
     223                 :            : 
     224                 :            :         bsg_dbg(bd, "tearing down\n");
     225                 :            : 
     226                 :            :         /*
     227                 :            :          * close can always block
     228                 :            :          */
     229                 :          0 :         kfree(bd);
     230                 :          0 :         blk_put_queue(q);
     231                 :          0 :         return 0;
     232                 :            : }
     233                 :            : 
     234                 :          0 : static struct bsg_device *bsg_add_device(struct inode *inode,
     235                 :            :                                          struct request_queue *rq,
     236                 :            :                                          struct file *file)
     237                 :            : {
     238                 :            :         struct bsg_device *bd;
     239                 :            :         unsigned char buf[32];
     240                 :            : 
     241                 :            :         lockdep_assert_held(&bsg_mutex);
     242                 :            : 
     243                 :          0 :         if (!blk_get_queue(rq))
     244                 :            :                 return ERR_PTR(-ENXIO);
     245                 :            : 
     246                 :          0 :         bd = bsg_alloc_device();
     247                 :          0 :         if (!bd) {
     248                 :          0 :                 blk_put_queue(rq);
     249                 :          0 :                 return ERR_PTR(-ENOMEM);
     250                 :            :         }
     251                 :            : 
     252                 :          0 :         bd->queue = rq;
     253                 :            : 
     254                 :            :         refcount_set(&bd->ref_count, 1);
     255                 :          0 :         hlist_add_head(&bd->dev_list, bsg_dev_idx_hash(iminor(inode)));
     256                 :            : 
     257                 :          0 :         strncpy(bd->name, dev_name(rq->bsg_dev.class_dev), sizeof(bd->name) - 1);
     258                 :            :         bsg_dbg(bd, "bound to <%s>, max queue %d\n",
     259                 :            :                 format_dev_t(buf, inode->i_rdev), bd->max_queue);
     260                 :            : 
     261                 :          0 :         return bd;
     262                 :            : }
     263                 :            : 
     264                 :          0 : static struct bsg_device *__bsg_get_device(int minor, struct request_queue *q)
     265                 :            : {
     266                 :            :         struct bsg_device *bd;
     267                 :            : 
     268                 :            :         lockdep_assert_held(&bsg_mutex);
     269                 :            : 
     270                 :          0 :         hlist_for_each_entry(bd, bsg_dev_idx_hash(minor), dev_list) {
     271                 :          0 :                 if (bd->queue == q) {
     272                 :          0 :                         refcount_inc(&bd->ref_count);
     273                 :          0 :                         goto found;
     274                 :            :                 }
     275                 :            :         }
     276                 :            :         bd = NULL;
     277                 :            : found:
     278                 :          0 :         return bd;
     279                 :            : }
     280                 :            : 
     281                 :          0 : static struct bsg_device *bsg_get_device(struct inode *inode, struct file *file)
     282                 :            : {
     283                 :            :         struct bsg_device *bd;
     284                 :            :         struct bsg_class_device *bcd;
     285                 :            : 
     286                 :            :         /*
     287                 :            :          * find the class device
     288                 :            :          */
     289                 :          0 :         mutex_lock(&bsg_mutex);
     290                 :          0 :         bcd = idr_find(&bsg_minor_idr, iminor(inode));
     291                 :            : 
     292                 :          0 :         if (!bcd) {
     293                 :            :                 bd = ERR_PTR(-ENODEV);
     294                 :            :                 goto out_unlock;
     295                 :            :         }
     296                 :            : 
     297                 :          0 :         bd = __bsg_get_device(iminor(inode), bcd->queue);
     298                 :          0 :         if (!bd)
     299                 :          0 :                 bd = bsg_add_device(inode, bcd->queue, file);
     300                 :            : 
     301                 :            : out_unlock:
     302                 :          0 :         mutex_unlock(&bsg_mutex);
     303                 :          0 :         return bd;
     304                 :            : }
     305                 :            : 
     306                 :          0 : static int bsg_open(struct inode *inode, struct file *file)
     307                 :            : {
     308                 :            :         struct bsg_device *bd;
     309                 :            : 
     310                 :          0 :         bd = bsg_get_device(inode, file);
     311                 :            : 
     312                 :          0 :         if (IS_ERR(bd))
     313                 :          0 :                 return PTR_ERR(bd);
     314                 :            : 
     315                 :          0 :         file->private_data = bd;
     316                 :          0 :         return 0;
     317                 :            : }
     318                 :            : 
     319                 :          0 : static int bsg_release(struct inode *inode, struct file *file)
     320                 :            : {
     321                 :          0 :         struct bsg_device *bd = file->private_data;
     322                 :            : 
     323                 :          0 :         file->private_data = NULL;
     324                 :          0 :         return bsg_put_device(bd);
     325                 :            : }
     326                 :            : 
     327                 :          0 : static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
     328                 :            : {
     329                 :          0 :         return put_user(bd->max_queue, uarg);
     330                 :            : }
     331                 :            : 
     332                 :          0 : static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
     333                 :            : {
     334                 :            :         int queue;
     335                 :            : 
     336                 :          0 :         if (get_user(queue, uarg))
     337                 :            :                 return -EFAULT;
     338                 :          0 :         if (queue < 1)
     339                 :            :                 return -EINVAL;
     340                 :            : 
     341                 :            :         spin_lock_irq(&bd->lock);
     342                 :          0 :         bd->max_queue = queue;
     343                 :            :         spin_unlock_irq(&bd->lock);
     344                 :          0 :         return 0;
     345                 :            : }
     346                 :            : 
     347                 :          0 : static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
     348                 :            : {
     349                 :          0 :         struct bsg_device *bd = file->private_data;
     350                 :          0 :         void __user *uarg = (void __user *) arg;
     351                 :            : 
     352                 :          0 :         switch (cmd) {
     353                 :            :         /*
     354                 :            :          * Our own ioctls
     355                 :            :          */
     356                 :            :         case SG_GET_COMMAND_Q:
     357                 :          0 :                 return bsg_get_command_q(bd, uarg);
     358                 :            :         case SG_SET_COMMAND_Q:
     359                 :          0 :                 return bsg_set_command_q(bd, uarg);
     360                 :            : 
     361                 :            :         /*
     362                 :            :          * SCSI/sg ioctls
     363                 :            :          */
     364                 :            :         case SG_GET_VERSION_NUM:
     365                 :            :         case SCSI_IOCTL_GET_IDLUN:
     366                 :            :         case SCSI_IOCTL_GET_BUS_NUMBER:
     367                 :            :         case SG_SET_TIMEOUT:
     368                 :            :         case SG_GET_TIMEOUT:
     369                 :            :         case SG_GET_RESERVED_SIZE:
     370                 :            :         case SG_SET_RESERVED_SIZE:
     371                 :            :         case SG_EMULATED_HOST:
     372                 :            :         case SCSI_IOCTL_SEND_COMMAND:
     373                 :          0 :                 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
     374                 :            :         case SG_IO:
     375                 :          0 :                 return bsg_sg_io(bd->queue, file->f_mode, uarg);
     376                 :            :         default:
     377                 :            :                 return -ENOTTY;
     378                 :            :         }
     379                 :            : }
     380                 :            : 
     381                 :            : static const struct file_operations bsg_fops = {
     382                 :            :         .open           =       bsg_open,
     383                 :            :         .release        =       bsg_release,
     384                 :            :         .unlocked_ioctl =       bsg_ioctl,
     385                 :            :         .owner          =       THIS_MODULE,
     386                 :            :         .llseek         =       default_llseek,
     387                 :            : };
     388                 :            : 
     389                 :          0 : void bsg_unregister_queue(struct request_queue *q)
     390                 :            : {
     391                 :            :         struct bsg_class_device *bcd = &q->bsg_dev;
     392                 :            : 
     393                 :          0 :         if (!bcd->class_dev)
     394                 :          0 :                 return;
     395                 :            : 
     396                 :          0 :         mutex_lock(&bsg_mutex);
     397                 :          0 :         idr_remove(&bsg_minor_idr, bcd->minor);
     398                 :          0 :         if (q->kobj.sd)
     399                 :          0 :                 sysfs_remove_link(&q->kobj, "bsg");
     400                 :          0 :         device_unregister(bcd->class_dev);
     401                 :          0 :         bcd->class_dev = NULL;
     402                 :          0 :         mutex_unlock(&bsg_mutex);
     403                 :            : }
     404                 :            : EXPORT_SYMBOL_GPL(bsg_unregister_queue);
     405                 :            : 
     406                 :          0 : int bsg_register_queue(struct request_queue *q, struct device *parent,
     407                 :            :                 const char *name, const struct bsg_ops *ops)
     408                 :            : {
     409                 :            :         struct bsg_class_device *bcd;
     410                 :            :         dev_t dev;
     411                 :            :         int ret;
     412                 :            :         struct device *class_dev = NULL;
     413                 :            : 
     414                 :            :         /*
     415                 :            :          * we need a proper transport to send commands, not a stacked device
     416                 :            :          */
     417                 :          0 :         if (!queue_is_mq(q))
     418                 :            :                 return 0;
     419                 :            : 
     420                 :          0 :         bcd = &q->bsg_dev;
     421                 :          0 :         memset(bcd, 0, sizeof(*bcd));
     422                 :            : 
     423                 :          0 :         mutex_lock(&bsg_mutex);
     424                 :            : 
     425                 :          0 :         ret = idr_alloc(&bsg_minor_idr, bcd, 0, BSG_MAX_DEVS, GFP_KERNEL);
     426                 :          0 :         if (ret < 0) {
     427                 :          0 :                 if (ret == -ENOSPC) {
     428                 :          0 :                         printk(KERN_ERR "bsg: too many bsg devices\n");
     429                 :            :                         ret = -EINVAL;
     430                 :            :                 }
     431                 :            :                 goto unlock;
     432                 :            :         }
     433                 :            : 
     434                 :          0 :         bcd->minor = ret;
     435                 :          0 :         bcd->queue = q;
     436                 :          0 :         bcd->ops = ops;
     437                 :          0 :         dev = MKDEV(bsg_major, bcd->minor);
     438                 :          0 :         class_dev = device_create(bsg_class, parent, dev, NULL, "%s", name);
     439                 :          0 :         if (IS_ERR(class_dev)) {
     440                 :            :                 ret = PTR_ERR(class_dev);
     441                 :          0 :                 goto idr_remove;
     442                 :            :         }
     443                 :          0 :         bcd->class_dev = class_dev;
     444                 :            : 
     445                 :          0 :         if (q->kobj.sd) {
     446                 :          0 :                 ret = sysfs_create_link(&q->kobj, &bcd->class_dev->kobj, "bsg");
     447                 :          0 :                 if (ret)
     448                 :            :                         goto unregister_class_dev;
     449                 :            :         }
     450                 :            : 
     451                 :          0 :         mutex_unlock(&bsg_mutex);
     452                 :          0 :         return 0;
     453                 :            : 
     454                 :            : unregister_class_dev:
     455                 :          0 :         device_unregister(class_dev);
     456                 :            : idr_remove:
     457                 :          0 :         idr_remove(&bsg_minor_idr, bcd->minor);
     458                 :            : unlock:
     459                 :          0 :         mutex_unlock(&bsg_mutex);
     460                 :          0 :         return ret;
     461                 :            : }
     462                 :            : 
     463                 :          0 : int bsg_scsi_register_queue(struct request_queue *q, struct device *parent)
     464                 :            : {
     465                 :          0 :         if (!blk_queue_scsi_passthrough(q)) {
     466                 :          0 :                 WARN_ONCE(true, "Attempt to register a non-SCSI queue\n");
     467                 :            :                 return -EINVAL;
     468                 :            :         }
     469                 :            : 
     470                 :          0 :         return bsg_register_queue(q, parent, dev_name(parent), &bsg_scsi_ops);
     471                 :            : }
     472                 :            : EXPORT_SYMBOL_GPL(bsg_scsi_register_queue);
     473                 :            : 
     474                 :            : static struct cdev bsg_cdev;
     475                 :            : 
     476                 :          0 : static char *bsg_devnode(struct device *dev, umode_t *mode)
     477                 :            : {
     478                 :          0 :         return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
     479                 :            : }
     480                 :            : 
     481                 :          3 : static int __init bsg_init(void)
     482                 :            : {
     483                 :            :         int ret, i;
     484                 :            :         dev_t devid;
     485                 :            : 
     486                 :          3 :         for (i = 0; i < BSG_LIST_ARRAY_SIZE; i++)
     487                 :          3 :                 INIT_HLIST_HEAD(&bsg_device_list[i]);
     488                 :            : 
     489                 :          3 :         bsg_class = class_create(THIS_MODULE, "bsg");
     490                 :          3 :         if (IS_ERR(bsg_class))
     491                 :          0 :                 return PTR_ERR(bsg_class);
     492                 :          3 :         bsg_class->devnode = bsg_devnode;
     493                 :            : 
     494                 :          3 :         ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
     495                 :          3 :         if (ret)
     496                 :            :                 goto destroy_bsg_class;
     497                 :            : 
     498                 :          3 :         bsg_major = MAJOR(devid);
     499                 :            : 
     500                 :          3 :         cdev_init(&bsg_cdev, &bsg_fops);
     501                 :          3 :         ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);
     502                 :          3 :         if (ret)
     503                 :            :                 goto unregister_chrdev;
     504                 :            : 
     505                 :          3 :         printk(KERN_INFO BSG_DESCRIPTION " version " BSG_VERSION
     506                 :            :                " loaded (major %d)\n", bsg_major);
     507                 :          3 :         return 0;
     508                 :            : unregister_chrdev:
     509                 :          0 :         unregister_chrdev_region(MKDEV(bsg_major, 0), BSG_MAX_DEVS);
     510                 :            : destroy_bsg_class:
     511                 :          0 :         class_destroy(bsg_class);
     512                 :          0 :         return ret;
     513                 :            : }
     514                 :            : 
     515                 :            : MODULE_AUTHOR("Jens Axboe");
     516                 :            : MODULE_DESCRIPTION(BSG_DESCRIPTION);
     517                 :            : MODULE_LICENSE("GPL");
     518                 :            : 
     519                 :            : device_initcall(bsg_init);
    

Generated by: LCOV version 1.14