Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * SCSI Media Changer device driver for Linux 2.6
0004  *
0005  *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
0006  *
0007  */
0008 
0009 #define VERSION "0.25"
0010 
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/fs.h>
0014 #include <linux/kernel.h>
0015 #include <linux/mm.h>
0016 #include <linux/major.h>
0017 #include <linux/string.h>
0018 #include <linux/errno.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/blkdev.h>
0021 #include <linux/completion.h>
0022 #include <linux/compat.h>
0023 #include <linux/chio.h>         /* here are all the ioctls */
0024 #include <linux/mutex.h>
0025 #include <linux/idr.h>
0026 #include <linux/slab.h>
0027 
0028 #include <scsi/scsi.h>
0029 #include <scsi/scsi_cmnd.h>
0030 #include <scsi/scsi_driver.h>
0031 #include <scsi/scsi_ioctl.h>
0032 #include <scsi/scsi_host.h>
0033 #include <scsi/scsi_device.h>
0034 #include <scsi/scsi_eh.h>
0035 #include <scsi/scsi_dbg.h>
0036 
0037 #define CH_DT_MAX       16
0038 #define CH_TYPES        8
0039 #define CH_MAX_DEVS     128
0040 
0041 MODULE_DESCRIPTION("device driver for scsi media changer devices");
0042 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
0043 MODULE_LICENSE("GPL");
0044 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
0045 MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
0046 
0047 static int init = 1;
0048 module_param(init, int, 0444);
0049 MODULE_PARM_DESC(init, \
0050     "initialize element status on driver load (default: on)");
0051 
0052 static int timeout_move = 300;
0053 module_param(timeout_move, int, 0644);
0054 MODULE_PARM_DESC(timeout_move,"timeout for move commands "
0055          "(default: 300 seconds)");
0056 
0057 static int timeout_init = 3600;
0058 module_param(timeout_init, int, 0644);
0059 MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
0060          "(default: 3600 seconds)");
0061 
0062 static int verbose = 1;
0063 module_param(verbose, int, 0644);
0064 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
0065 
0066 static int debug;
0067 module_param(debug, int, 0644);
0068 MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
0069          "detailed sense codes on scsi errors (default: off)");
0070 
0071 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
0072 static int dt_lun[CH_DT_MAX];
0073 module_param_array(dt_id,  int, NULL, 0444);
0074 module_param_array(dt_lun, int, NULL, 0444);
0075 
0076 /* tell the driver about vendor-specific slots */
0077 static int vendor_firsts[CH_TYPES-4];
0078 static int vendor_counts[CH_TYPES-4];
0079 module_param_array(vendor_firsts, int, NULL, 0444);
0080 module_param_array(vendor_counts, int, NULL, 0444);
0081 
0082 static const char * vendor_labels[CH_TYPES-4] = {
0083     "v0", "v1", "v2", "v3"
0084 };
0085 // module_param_string_array(vendor_labels, NULL, 0444);
0086 
0087 #define ch_printk(prefix, ch, fmt, a...) \
0088     sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
0089 
0090 #define DPRINTK(fmt, arg...)                        \
0091 do {                                    \
0092     if (debug)                          \
0093         ch_printk(KERN_DEBUG, ch, fmt, ##arg);          \
0094 } while (0)
0095 #define VPRINTK(level, fmt, arg...)                 \
0096 do {                                    \
0097     if (verbose)                            \
0098         ch_printk(level, ch, fmt, ##arg);           \
0099 } while (0)
0100 
0101 /* ------------------------------------------------------------------- */
0102 
0103 #define MAX_RETRIES   1
0104 
0105 static struct class * ch_sysfs_class;
0106 
0107 typedef struct {
0108     struct kref         ref;
0109     struct list_head    list;
0110     int                 minor;
0111     char                name[8];
0112     struct scsi_device  *device;
0113     struct scsi_device  **dt;        /* ptrs to data transfer elements */
0114     u_int               firsts[CH_TYPES];
0115     u_int               counts[CH_TYPES];
0116     u_int               unit_attention;
0117     u_int           voltags;
0118     struct mutex        lock;
0119 } scsi_changer;
0120 
0121 static DEFINE_IDR(ch_index_idr);
0122 static DEFINE_SPINLOCK(ch_index_lock);
0123 
0124 static const struct {
0125     unsigned char  sense;
0126     unsigned char  asc;
0127     unsigned char  ascq;
0128     int        errno;
0129 } ch_err[] = {
0130 /* Just filled in what looks right. Hav'nt checked any standard paper for
0131    these errno assignments, so they may be wrong... */
0132     {
0133         .sense  = ILLEGAL_REQUEST,
0134         .asc    = 0x21,
0135         .ascq   = 0x01,
0136         .errno  = EBADSLT, /* Invalid element address */
0137     },{
0138         .sense  = ILLEGAL_REQUEST,
0139         .asc    = 0x28,
0140         .ascq   = 0x01,
0141         .errno  = EBADE,   /* Import or export element accessed */
0142     },{
0143         .sense  = ILLEGAL_REQUEST,
0144         .asc    = 0x3B,
0145         .ascq   = 0x0D,
0146         .errno  = EXFULL,  /* Medium destination element full */
0147     },{
0148         .sense  = ILLEGAL_REQUEST,
0149         .asc    = 0x3B,
0150         .ascq   = 0x0E,
0151         .errno  = EBADE,   /* Medium source element empty */
0152     },{
0153         .sense  = ILLEGAL_REQUEST,
0154         .asc    = 0x20,
0155         .ascq   = 0x00,
0156         .errno  = EBADRQC, /* Invalid command operation code */
0157     },{
0158             /* end of list */
0159     }
0160 };
0161 
0162 /* ------------------------------------------------------------------- */
0163 
0164 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
0165 {
0166     int i,errno = 0;
0167 
0168     /* Check to see if additional sense information is available */
0169     if (scsi_sense_valid(sshdr) &&
0170         sshdr->asc != 0) {
0171         for (i = 0; ch_err[i].errno != 0; i++) {
0172             if (ch_err[i].sense == sshdr->sense_key &&
0173                 ch_err[i].asc   == sshdr->asc &&
0174                 ch_err[i].ascq  == sshdr->ascq) {
0175                 errno = -ch_err[i].errno;
0176                 break;
0177             }
0178         }
0179     }
0180     if (errno == 0)
0181         errno = -EIO;
0182     return errno;
0183 }
0184 
0185 static int
0186 ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
0187        void *buffer, unsigned buflength,
0188        enum dma_data_direction direction)
0189 {
0190     int errno, retries = 0, timeout, result;
0191     struct scsi_sense_hdr sshdr;
0192 
0193     timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
0194         ? timeout_init : timeout_move;
0195 
0196  retry:
0197     errno = 0;
0198     result = scsi_execute_req(ch->device, cmd, direction, buffer,
0199                   buflength, &sshdr, timeout * HZ,
0200                   MAX_RETRIES, NULL);
0201     if (result < 0)
0202         return result;
0203     if (scsi_sense_valid(&sshdr)) {
0204         if (debug)
0205             scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
0206         errno = ch_find_errno(&sshdr);
0207 
0208         switch(sshdr.sense_key) {
0209         case UNIT_ATTENTION:
0210             ch->unit_attention = 1;
0211             if (retries++ < 3)
0212                 goto retry;
0213             break;
0214         }
0215     }
0216     return errno;
0217 }
0218 
0219 /* ------------------------------------------------------------------------ */
0220 
0221 static int
0222 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
0223 {
0224     int i;
0225 
0226     for (i = 0; i < CH_TYPES; i++) {
0227         if (elem >= ch->firsts[i]  &&
0228             elem <  ch->firsts[i] +
0229                 ch->counts[i])
0230             return i+1;
0231     }
0232     return 0;
0233 }
0234 
0235 static int
0236 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
0237 {
0238     u_char  cmd[12];
0239     u_char  *buffer;
0240     int     result;
0241 
0242     buffer = kmalloc(512, GFP_KERNEL);
0243     if(!buffer)
0244         return -ENOMEM;
0245 
0246  retry:
0247     memset(cmd,0,sizeof(cmd));
0248     cmd[0] = READ_ELEMENT_STATUS;
0249     cmd[1] = ((ch->device->lun & 0x7) << 5) |
0250         (ch->voltags ? 0x10 : 0) |
0251         ch_elem_to_typecode(ch,elem);
0252     cmd[2] = (elem >> 8) & 0xff;
0253     cmd[3] = elem        & 0xff;
0254     cmd[5] = 1;
0255     cmd[9] = 255;
0256     if (0 == (result = ch_do_scsi(ch, cmd, 12,
0257                       buffer, 256, DMA_FROM_DEVICE))) {
0258         if (((buffer[16] << 8) | buffer[17]) != elem) {
0259             DPRINTK("asked for element 0x%02x, got 0x%02x\n",
0260                 elem,(buffer[16] << 8) | buffer[17]);
0261             kfree(buffer);
0262             return -EIO;
0263         }
0264         memcpy(data,buffer+16,16);
0265     } else {
0266         if (ch->voltags) {
0267             ch->voltags = 0;
0268             VPRINTK(KERN_INFO, "device has no volume tag support\n");
0269             goto retry;
0270         }
0271         DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
0272     }
0273     kfree(buffer);
0274     return result;
0275 }
0276 
0277 static int
0278 ch_init_elem(scsi_changer *ch)
0279 {
0280     int err;
0281     u_char cmd[6];
0282 
0283     VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
0284     memset(cmd,0,sizeof(cmd));
0285     cmd[0] = INITIALIZE_ELEMENT_STATUS;
0286     cmd[1] = (ch->device->lun & 0x7) << 5;
0287     err = ch_do_scsi(ch, cmd, 6, NULL, 0, DMA_NONE);
0288     VPRINTK(KERN_INFO, "... finished\n");
0289     return err;
0290 }
0291 
0292 static int
0293 ch_readconfig(scsi_changer *ch)
0294 {
0295     u_char  cmd[10], data[16];
0296     u_char  *buffer;
0297     int     result,id,lun,i;
0298     u_int   elem;
0299 
0300     buffer = kzalloc(512, GFP_KERNEL);
0301     if (!buffer)
0302         return -ENOMEM;
0303 
0304     memset(cmd,0,sizeof(cmd));
0305     cmd[0] = MODE_SENSE;
0306     cmd[1] = (ch->device->lun & 0x7) << 5;
0307     cmd[2] = 0x1d;
0308     cmd[4] = 255;
0309     result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE);
0310     if (0 != result) {
0311         cmd[1] |= (1<<3);
0312         result  = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE);
0313     }
0314     if (0 == result) {
0315         ch->firsts[CHET_MT] =
0316             (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
0317         ch->counts[CHET_MT] =
0318             (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
0319         ch->firsts[CHET_ST] =
0320             (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
0321         ch->counts[CHET_ST] =
0322             (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
0323         ch->firsts[CHET_IE] =
0324             (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
0325         ch->counts[CHET_IE] =
0326             (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
0327         ch->firsts[CHET_DT] =
0328             (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
0329         ch->counts[CHET_DT] =
0330             (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
0331         VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
0332             ch->firsts[CHET_MT],
0333             ch->counts[CHET_MT]);
0334         VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
0335             ch->firsts[CHET_ST],
0336             ch->counts[CHET_ST]);
0337         VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
0338             ch->firsts[CHET_IE],
0339             ch->counts[CHET_IE]);
0340         VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
0341             ch->firsts[CHET_DT],
0342             ch->counts[CHET_DT]);
0343     } else {
0344         VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
0345     }
0346 
0347     /* vendor specific element types */
0348     for (i = 0; i < 4; i++) {
0349         if (0 == vendor_counts[i])
0350             continue;
0351         if (NULL == vendor_labels[i])
0352             continue;
0353         ch->firsts[CHET_V1+i] = vendor_firsts[i];
0354         ch->counts[CHET_V1+i] = vendor_counts[i];
0355         VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
0356             i+5,i+1,vendor_firsts[i],vendor_counts[i],
0357             vendor_labels[i]);
0358     }
0359 
0360     /* look up the devices of the data transfer elements */
0361     ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt),
0362              GFP_KERNEL);
0363 
0364     if (!ch->dt) {
0365         kfree(buffer);
0366         return -ENOMEM;
0367     }
0368 
0369     for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
0370         id  = -1;
0371         lun = 0;
0372         if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {
0373             id  = dt_id[elem];
0374             lun = dt_lun[elem];
0375             VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
0376                 elem+ch->firsts[CHET_DT]);
0377         } else if (0 != ch_read_element_status
0378                (ch,elem+ch->firsts[CHET_DT],data)) {
0379             VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
0380                 elem+ch->firsts[CHET_DT]);
0381         } else {
0382             VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
0383             if (data[6] & 0x80) {
0384                 VPRINTK(KERN_CONT, "not this SCSI bus\n");
0385                 ch->dt[elem] = NULL;
0386             } else if (0 == (data[6] & 0x30)) {
0387                 VPRINTK(KERN_CONT, "ID/LUN unknown\n");
0388                 ch->dt[elem] = NULL;
0389             } else {
0390                 id  = ch->device->id;
0391                 lun = 0;
0392                 if (data[6] & 0x20) id  = data[7];
0393                 if (data[6] & 0x10) lun = data[6] & 7;
0394             }
0395         }
0396         if (-1 != id) {
0397             VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
0398             ch->dt[elem] =
0399                 scsi_device_lookup(ch->device->host,
0400                            ch->device->channel,
0401                            id,lun);
0402             if (!ch->dt[elem]) {
0403                 /* should not happen */
0404                 VPRINTK(KERN_CONT, "Huh? device not found!\n");
0405             } else {
0406                 VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
0407                     ch->dt[elem]->vendor,
0408                     ch->dt[elem]->model,
0409                     ch->dt[elem]->rev);
0410             }
0411         }
0412     }
0413     ch->voltags = 1;
0414     kfree(buffer);
0415 
0416     return 0;
0417 }
0418 
0419 /* ------------------------------------------------------------------------ */
0420 
0421 static int
0422 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
0423 {
0424     u_char  cmd[10];
0425 
0426     DPRINTK("position: 0x%x\n",elem);
0427     if (0 == trans)
0428         trans = ch->firsts[CHET_MT];
0429     memset(cmd,0,sizeof(cmd));
0430     cmd[0]  = POSITION_TO_ELEMENT;
0431     cmd[1]  = (ch->device->lun & 0x7) << 5;
0432     cmd[2]  = (trans >> 8) & 0xff;
0433     cmd[3]  =  trans       & 0xff;
0434     cmd[4]  = (elem  >> 8) & 0xff;
0435     cmd[5]  =  elem        & 0xff;
0436     cmd[8]  = rotate ? 1 : 0;
0437     return ch_do_scsi(ch, cmd, 10, NULL, 0, DMA_NONE);
0438 }
0439 
0440 static int
0441 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
0442 {
0443     u_char  cmd[12];
0444 
0445     DPRINTK("move: 0x%x => 0x%x\n",src,dest);
0446     if (0 == trans)
0447         trans = ch->firsts[CHET_MT];
0448     memset(cmd,0,sizeof(cmd));
0449     cmd[0]  = MOVE_MEDIUM;
0450     cmd[1]  = (ch->device->lun & 0x7) << 5;
0451     cmd[2]  = (trans >> 8) & 0xff;
0452     cmd[3]  =  trans       & 0xff;
0453     cmd[4]  = (src   >> 8) & 0xff;
0454     cmd[5]  =  src         & 0xff;
0455     cmd[6]  = (dest  >> 8) & 0xff;
0456     cmd[7]  =  dest        & 0xff;
0457     cmd[10] = rotate ? 1 : 0;
0458     return ch_do_scsi(ch, cmd, 12, NULL,0, DMA_NONE);
0459 }
0460 
0461 static int
0462 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
0463         u_int dest1, u_int dest2, int rotate1, int rotate2)
0464 {
0465     u_char  cmd[12];
0466 
0467     DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
0468         src,dest1,dest2);
0469     if (0 == trans)
0470         trans = ch->firsts[CHET_MT];
0471     memset(cmd,0,sizeof(cmd));
0472     cmd[0]  = EXCHANGE_MEDIUM;
0473     cmd[1]  = (ch->device->lun & 0x7) << 5;
0474     cmd[2]  = (trans >> 8) & 0xff;
0475     cmd[3]  =  trans       & 0xff;
0476     cmd[4]  = (src   >> 8) & 0xff;
0477     cmd[5]  =  src         & 0xff;
0478     cmd[6]  = (dest1 >> 8) & 0xff;
0479     cmd[7]  =  dest1       & 0xff;
0480     cmd[8]  = (dest2 >> 8) & 0xff;
0481     cmd[9]  =  dest2       & 0xff;
0482     cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
0483 
0484     return ch_do_scsi(ch, cmd, 12, NULL, 0, DMA_NONE);
0485 }
0486 
0487 static void
0488 ch_check_voltag(char *tag)
0489 {
0490     int i;
0491 
0492     for (i = 0; i < 32; i++) {
0493         /* restrict to ascii */
0494         if (tag[i] >= 0x7f || tag[i] < 0x20)
0495             tag[i] = ' ';
0496         /* don't allow search wildcards */
0497         if (tag[i] == '?' ||
0498             tag[i] == '*')
0499             tag[i] = ' ';
0500     }
0501 }
0502 
0503 static int
0504 ch_set_voltag(scsi_changer *ch, u_int elem,
0505           int alternate, int clear, u_char *tag)
0506 {
0507     u_char  cmd[12];
0508     u_char  *buffer;
0509     int result;
0510 
0511     buffer = kzalloc(512, GFP_KERNEL);
0512     if (!buffer)
0513         return -ENOMEM;
0514 
0515     DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
0516         clear     ? "clear"     : "set",
0517         alternate ? "alternate" : "primary",
0518         elem, tag);
0519     memset(cmd,0,sizeof(cmd));
0520     cmd[0]  = SEND_VOLUME_TAG;
0521     cmd[1] = ((ch->device->lun & 0x7) << 5) |
0522         ch_elem_to_typecode(ch,elem);
0523     cmd[2] = (elem >> 8) & 0xff;
0524     cmd[3] = elem        & 0xff;
0525     cmd[5] = clear
0526         ? (alternate ? 0x0d : 0x0c)
0527         : (alternate ? 0x0b : 0x0a);
0528 
0529     cmd[9] = 255;
0530 
0531     memcpy(buffer,tag,32);
0532     ch_check_voltag(buffer);
0533 
0534     result = ch_do_scsi(ch, cmd, 12, buffer, 256, DMA_TO_DEVICE);
0535     kfree(buffer);
0536     return result;
0537 }
0538 
0539 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
0540 {
0541     int retval = 0;
0542     u_char data[16];
0543     unsigned int i;
0544 
0545     mutex_lock(&ch->lock);
0546     for (i = 0; i < ch->counts[type]; i++) {
0547         if (0 != ch_read_element_status
0548             (ch, ch->firsts[type]+i,data)) {
0549             retval = -EIO;
0550             break;
0551         }
0552         put_user(data[2], dest+i);
0553         if (data[2] & CESTATUS_EXCEPT)
0554             VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
0555                 ch->firsts[type]+i,
0556                 (int)data[4],(int)data[5]);
0557         retval = ch_read_element_status
0558             (ch, ch->firsts[type]+i,data);
0559         if (0 != retval)
0560             break;
0561     }
0562     mutex_unlock(&ch->lock);
0563     return retval;
0564 }
0565 
0566 /* ------------------------------------------------------------------------ */
0567 
0568 static void ch_destroy(struct kref *ref)
0569 {
0570     scsi_changer *ch = container_of(ref, scsi_changer, ref);
0571 
0572     ch->device = NULL;
0573     kfree(ch->dt);
0574     kfree(ch);
0575 }
0576 
0577 static int
0578 ch_release(struct inode *inode, struct file *file)
0579 {
0580     scsi_changer *ch = file->private_data;
0581 
0582     scsi_device_put(ch->device);
0583     file->private_data = NULL;
0584     kref_put(&ch->ref, ch_destroy);
0585     return 0;
0586 }
0587 
0588 static int
0589 ch_open(struct inode *inode, struct file *file)
0590 {
0591     scsi_changer *ch;
0592     int minor = iminor(inode);
0593 
0594     spin_lock(&ch_index_lock);
0595     ch = idr_find(&ch_index_idr, minor);
0596 
0597     if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
0598         spin_unlock(&ch_index_lock);
0599         return -ENXIO;
0600     }
0601     spin_unlock(&ch_index_lock);
0602     if (scsi_device_get(ch->device)) {
0603         kref_put(&ch->ref, ch_destroy);
0604         return -ENXIO;
0605     }
0606     /* Synchronize with ch_probe() */
0607     mutex_lock(&ch->lock);
0608     file->private_data = ch;
0609     mutex_unlock(&ch->lock);
0610     return 0;
0611 }
0612 
0613 static int
0614 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
0615 {
0616     if (type >= CH_TYPES  ||  unit >= ch->counts[type])
0617         return -1;
0618     return 0;
0619 }
0620 
0621 struct changer_element_status32 {
0622     int     ces_type;
0623     compat_uptr_t   ces_data;
0624 };
0625 #define CHIOGSTATUS32  _IOW('c', 8, struct changer_element_status32)
0626 
0627 static long ch_ioctl(struct file *file,
0628             unsigned int cmd, unsigned long arg)
0629 {
0630     scsi_changer *ch = file->private_data;
0631     int retval;
0632     void __user *argp = (void __user *)arg;
0633 
0634     retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
0635             file->f_flags & O_NDELAY);
0636     if (retval)
0637         return retval;
0638 
0639     switch (cmd) {
0640     case CHIOGPARAMS:
0641     {
0642         struct changer_params params;
0643 
0644         params.cp_curpicker = 0;
0645         params.cp_npickers  = ch->counts[CHET_MT];
0646         params.cp_nslots    = ch->counts[CHET_ST];
0647         params.cp_nportals  = ch->counts[CHET_IE];
0648         params.cp_ndrives   = ch->counts[CHET_DT];
0649 
0650         if (copy_to_user(argp, &params, sizeof(params)))
0651             return -EFAULT;
0652         return 0;
0653     }
0654     case CHIOGVPARAMS:
0655     {
0656         struct changer_vendor_params vparams;
0657 
0658         memset(&vparams,0,sizeof(vparams));
0659         if (ch->counts[CHET_V1]) {
0660             vparams.cvp_n1  = ch->counts[CHET_V1];
0661             strncpy(vparams.cvp_label1,vendor_labels[0],16);
0662         }
0663         if (ch->counts[CHET_V2]) {
0664             vparams.cvp_n2  = ch->counts[CHET_V2];
0665             strncpy(vparams.cvp_label2,vendor_labels[1],16);
0666         }
0667         if (ch->counts[CHET_V3]) {
0668             vparams.cvp_n3  = ch->counts[CHET_V3];
0669             strncpy(vparams.cvp_label3,vendor_labels[2],16);
0670         }
0671         if (ch->counts[CHET_V4]) {
0672             vparams.cvp_n4  = ch->counts[CHET_V4];
0673             strncpy(vparams.cvp_label4,vendor_labels[3],16);
0674         }
0675         if (copy_to_user(argp, &vparams, sizeof(vparams)))
0676             return -EFAULT;
0677         return 0;
0678     }
0679 
0680     case CHIOPOSITION:
0681     {
0682         struct changer_position pos;
0683 
0684         if (copy_from_user(&pos, argp, sizeof (pos)))
0685             return -EFAULT;
0686 
0687         if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
0688             DPRINTK("CHIOPOSITION: invalid parameter\n");
0689             return -EBADSLT;
0690         }
0691         mutex_lock(&ch->lock);
0692         retval = ch_position(ch,0,
0693                      ch->firsts[pos.cp_type] + pos.cp_unit,
0694                      pos.cp_flags & CP_INVERT);
0695         mutex_unlock(&ch->lock);
0696         return retval;
0697     }
0698 
0699     case CHIOMOVE:
0700     {
0701         struct changer_move mv;
0702 
0703         if (copy_from_user(&mv, argp, sizeof (mv)))
0704             return -EFAULT;
0705 
0706         if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
0707             0 != ch_checkrange(ch, mv.cm_totype,   mv.cm_tounit  )) {
0708             DPRINTK("CHIOMOVE: invalid parameter\n");
0709             return -EBADSLT;
0710         }
0711 
0712         mutex_lock(&ch->lock);
0713         retval = ch_move(ch,0,
0714                  ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
0715                  ch->firsts[mv.cm_totype]   + mv.cm_tounit,
0716                  mv.cm_flags & CM_INVERT);
0717         mutex_unlock(&ch->lock);
0718         return retval;
0719     }
0720 
0721     case CHIOEXCHANGE:
0722     {
0723         struct changer_exchange mv;
0724 
0725         if (copy_from_user(&mv, argp, sizeof (mv)))
0726             return -EFAULT;
0727 
0728         if (0 != ch_checkrange(ch, mv.ce_srctype,  mv.ce_srcunit ) ||
0729             0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
0730             0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
0731             DPRINTK("CHIOEXCHANGE: invalid parameter\n");
0732             return -EBADSLT;
0733         }
0734 
0735         mutex_lock(&ch->lock);
0736         retval = ch_exchange
0737             (ch,0,
0738              ch->firsts[mv.ce_srctype]  + mv.ce_srcunit,
0739              ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
0740              ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
0741              mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
0742         mutex_unlock(&ch->lock);
0743         return retval;
0744     }
0745 
0746     case CHIOGSTATUS:
0747     {
0748         struct changer_element_status ces;
0749 
0750         if (copy_from_user(&ces, argp, sizeof (ces)))
0751             return -EFAULT;
0752         if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
0753             return -EINVAL;
0754 
0755         return ch_gstatus(ch, ces.ces_type, ces.ces_data);
0756     }
0757 #ifdef CONFIG_COMPAT
0758     case CHIOGSTATUS32:
0759     {
0760         struct changer_element_status32 ces32;
0761 
0762         if (copy_from_user(&ces32, argp, sizeof(ces32)))
0763             return -EFAULT;
0764         if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
0765             return -EINVAL;
0766 
0767         return ch_gstatus(ch, ces32.ces_type,
0768                   compat_ptr(ces32.ces_data));
0769     }
0770 #endif
0771     case CHIOGELEM:
0772     {
0773         struct changer_get_element cge;
0774         u_char ch_cmd[12];
0775         u_char *buffer;
0776         unsigned int elem;
0777         int     result,i;
0778 
0779         if (copy_from_user(&cge, argp, sizeof (cge)))
0780             return -EFAULT;
0781 
0782         if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
0783             return -EINVAL;
0784         elem = ch->firsts[cge.cge_type] + cge.cge_unit;
0785 
0786         buffer = kmalloc(512, GFP_KERNEL);
0787         if (!buffer)
0788             return -ENOMEM;
0789         mutex_lock(&ch->lock);
0790 
0791     voltag_retry:
0792         memset(ch_cmd, 0, sizeof(ch_cmd));
0793         ch_cmd[0] = READ_ELEMENT_STATUS;
0794         ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
0795             (ch->voltags ? 0x10 : 0) |
0796             ch_elem_to_typecode(ch,elem);
0797         ch_cmd[2] = (elem >> 8) & 0xff;
0798         ch_cmd[3] = elem        & 0xff;
0799         ch_cmd[5] = 1;
0800         ch_cmd[9] = 255;
0801 
0802         result = ch_do_scsi(ch, ch_cmd, 12,
0803                     buffer, 256, DMA_FROM_DEVICE);
0804         if (!result) {
0805             cge.cge_status = buffer[18];
0806             cge.cge_flags = 0;
0807             if (buffer[18] & CESTATUS_EXCEPT) {
0808                 cge.cge_errno = EIO;
0809             }
0810             if (buffer[25] & 0x80) {
0811                 cge.cge_flags |= CGE_SRC;
0812                 if (buffer[25] & 0x40)
0813                     cge.cge_flags |= CGE_INVERT;
0814                 elem = (buffer[26]<<8) | buffer[27];
0815                 for (i = 0; i < 4; i++) {
0816                     if (elem >= ch->firsts[i] &&
0817                         elem <  ch->firsts[i] + ch->counts[i]) {
0818                         cge.cge_srctype = i;
0819                         cge.cge_srcunit = elem-ch->firsts[i];
0820                     }
0821                 }
0822             }
0823             if ((buffer[22] & 0x30) == 0x30) {
0824                 cge.cge_flags |= CGE_IDLUN;
0825                 cge.cge_id  = buffer[23];
0826                 cge.cge_lun = buffer[22] & 7;
0827             }
0828             if (buffer[9] & 0x80) {
0829                 cge.cge_flags |= CGE_PVOLTAG;
0830                 memcpy(cge.cge_pvoltag,buffer+28,36);
0831             }
0832             if (buffer[9] & 0x40) {
0833                 cge.cge_flags |= CGE_AVOLTAG;
0834                 memcpy(cge.cge_avoltag,buffer+64,36);
0835             }
0836         } else if (ch->voltags) {
0837             ch->voltags = 0;
0838             VPRINTK(KERN_INFO, "device has no volume tag support\n");
0839             goto voltag_retry;
0840         }
0841         kfree(buffer);
0842         mutex_unlock(&ch->lock);
0843 
0844         if (copy_to_user(argp, &cge, sizeof (cge)))
0845             return -EFAULT;
0846         return result;
0847     }
0848 
0849     case CHIOINITELEM:
0850     {
0851         mutex_lock(&ch->lock);
0852         retval = ch_init_elem(ch);
0853         mutex_unlock(&ch->lock);
0854         return retval;
0855     }
0856 
0857     case CHIOSVOLTAG:
0858     {
0859         struct changer_set_voltag csv;
0860         int elem;
0861 
0862         if (copy_from_user(&csv, argp, sizeof(csv)))
0863             return -EFAULT;
0864 
0865         if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
0866             DPRINTK("CHIOSVOLTAG: invalid parameter\n");
0867             return -EBADSLT;
0868         }
0869         elem = ch->firsts[csv.csv_type] + csv.csv_unit;
0870         mutex_lock(&ch->lock);
0871         retval = ch_set_voltag(ch, elem,
0872                        csv.csv_flags & CSV_AVOLTAG,
0873                        csv.csv_flags & CSV_CLEARTAG,
0874                        csv.csv_voltag);
0875         mutex_unlock(&ch->lock);
0876         return retval;
0877     }
0878 
0879     default:
0880         return scsi_ioctl(ch->device, file->f_mode, cmd, argp);
0881 
0882     }
0883 }
0884 
0885 /* ------------------------------------------------------------------------ */
0886 
0887 static int ch_probe(struct device *dev)
0888 {
0889     struct scsi_device *sd = to_scsi_device(dev);
0890     struct device *class_dev;
0891     int ret;
0892     scsi_changer *ch;
0893 
0894     if (sd->type != TYPE_MEDIUM_CHANGER)
0895         return -ENODEV;
0896 
0897     ch = kzalloc(sizeof(*ch), GFP_KERNEL);
0898     if (NULL == ch)
0899         return -ENOMEM;
0900 
0901     idr_preload(GFP_KERNEL);
0902     spin_lock(&ch_index_lock);
0903     ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
0904     spin_unlock(&ch_index_lock);
0905     idr_preload_end();
0906 
0907     if (ret < 0) {
0908         if (ret == -ENOSPC)
0909             ret = -ENODEV;
0910         goto free_ch;
0911     }
0912 
0913     ch->minor = ret;
0914     sprintf(ch->name,"ch%d",ch->minor);
0915     ret = scsi_device_get(sd);
0916     if (ret) {
0917         sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
0918                 ch->minor);
0919         goto remove_idr;
0920     }
0921 
0922     mutex_init(&ch->lock);
0923     kref_init(&ch->ref);
0924     ch->device = sd;
0925     class_dev = device_create(ch_sysfs_class, dev,
0926                   MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
0927                   "s%s", ch->name);
0928     if (IS_ERR(class_dev)) {
0929         sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
0930                 ch->minor);
0931         ret = PTR_ERR(class_dev);
0932         goto put_device;
0933     }
0934 
0935     mutex_lock(&ch->lock);
0936     ret = ch_readconfig(ch);
0937     if (ret) {
0938         mutex_unlock(&ch->lock);
0939         goto destroy_dev;
0940     }
0941     if (init)
0942         ch_init_elem(ch);
0943 
0944     mutex_unlock(&ch->lock);
0945     dev_set_drvdata(dev, ch);
0946     sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
0947 
0948     return 0;
0949 destroy_dev:
0950     device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
0951 put_device:
0952     scsi_device_put(sd);
0953 remove_idr:
0954     idr_remove(&ch_index_idr, ch->minor);
0955 free_ch:
0956     kfree(ch);
0957     return ret;
0958 }
0959 
0960 static int ch_remove(struct device *dev)
0961 {
0962     scsi_changer *ch = dev_get_drvdata(dev);
0963 
0964     spin_lock(&ch_index_lock);
0965     idr_remove(&ch_index_idr, ch->minor);
0966     dev_set_drvdata(dev, NULL);
0967     spin_unlock(&ch_index_lock);
0968 
0969     device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
0970     scsi_device_put(ch->device);
0971     kref_put(&ch->ref, ch_destroy);
0972     return 0;
0973 }
0974 
0975 static struct scsi_driver ch_template = {
0976     .gendrv         = {
0977         .name   = "ch",
0978         .owner  = THIS_MODULE,
0979         .probe  = ch_probe,
0980         .remove = ch_remove,
0981     },
0982 };
0983 
0984 static const struct file_operations changer_fops = {
0985     .owner      = THIS_MODULE,
0986     .open       = ch_open,
0987     .release    = ch_release,
0988     .unlocked_ioctl = ch_ioctl,
0989     .compat_ioctl   = compat_ptr_ioctl,
0990     .llseek     = noop_llseek,
0991 };
0992 
0993 static int __init init_ch_module(void)
0994 {
0995     int rc;
0996 
0997     printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
0998         ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
0999         if (IS_ERR(ch_sysfs_class)) {
1000         rc = PTR_ERR(ch_sysfs_class);
1001         return rc;
1002         }
1003     rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
1004     if (rc < 0) {
1005         printk("Unable to get major %d for SCSI-Changer\n",
1006                SCSI_CHANGER_MAJOR);
1007         goto fail1;
1008     }
1009     rc = scsi_register_driver(&ch_template.gendrv);
1010     if (rc < 0)
1011         goto fail2;
1012     return 0;
1013 
1014  fail2:
1015     unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1016  fail1:
1017     class_destroy(ch_sysfs_class);
1018     return rc;
1019 }
1020 
1021 static void __exit exit_ch_module(void)
1022 {
1023     scsi_unregister_driver(&ch_template.gendrv);
1024     unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1025     class_destroy(ch_sysfs_class);
1026     idr_destroy(&ch_index_idr);
1027 }
1028 
1029 module_init(init_ch_module);
1030 module_exit(exit_ch_module);