linux/drivers/scsi/ch.c
<<
>>
Prefs
   1/*
   2 * SCSI Media Changer device driver for Linux 2.6
   3 *
   4 *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
   5 *
   6 */
   7
   8#define VERSION "0.25"
   9
  10#include <linux/module.h>
  11#include <linux/init.h>
  12#include <linux/fs.h>
  13#include <linux/kernel.h>
  14#include <linux/mm.h>
  15#include <linux/major.h>
  16#include <linux/string.h>
  17#include <linux/errno.h>
  18#include <linux/interrupt.h>
  19#include <linux/blkdev.h>
  20#include <linux/completion.h>
  21#include <linux/compat.h>
  22#include <linux/chio.h>                 /* here are all the ioctls */
  23#include <linux/mutex.h>
  24#include <linux/idr.h>
  25
  26#include <scsi/scsi.h>
  27#include <scsi/scsi_cmnd.h>
  28#include <scsi/scsi_driver.h>
  29#include <scsi/scsi_ioctl.h>
  30#include <scsi/scsi_host.h>
  31#include <scsi/scsi_device.h>
  32#include <scsi/scsi_eh.h>
  33#include <scsi/scsi_dbg.h>
  34
  35#define CH_DT_MAX       16
  36#define CH_TYPES        8
  37#define CH_MAX_DEVS     128
  38
  39MODULE_DESCRIPTION("device driver for scsi media changer devices");
  40MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  41MODULE_LICENSE("GPL");
  42MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  43
  44static int init = 1;
  45module_param(init, int, 0444);
  46MODULE_PARM_DESC(init, \
  47    "initialize element status on driver load (default: on)");
  48
  49static int timeout_move = 300;
  50module_param(timeout_move, int, 0644);
  51MODULE_PARM_DESC(timeout_move,"timeout for move commands "
  52                 "(default: 300 seconds)");
  53
  54static int timeout_init = 3600;
  55module_param(timeout_init, int, 0644);
  56MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
  57                 "(default: 3600 seconds)");
  58
  59static int verbose = 1;
  60module_param(verbose, int, 0644);
  61MODULE_PARM_DESC(verbose,"be verbose (default: on)");
  62
  63static int debug = 0;
  64module_param(debug, int, 0644);
  65MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
  66                 "detailed sense codes on scsi errors (default: off)");
  67
  68static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
  69static int dt_lun[CH_DT_MAX];
  70module_param_array(dt_id,  int, NULL, 0444);
  71module_param_array(dt_lun, int, NULL, 0444);
  72
  73/* tell the driver about vendor-specific slots */
  74static int vendor_firsts[CH_TYPES-4];
  75static int vendor_counts[CH_TYPES-4];
  76module_param_array(vendor_firsts, int, NULL, 0444);
  77module_param_array(vendor_counts, int, NULL, 0444);
  78
  79static const char * vendor_labels[CH_TYPES-4] = {
  80        "v0", "v1", "v2", "v3"
  81};
  82// module_param_string_array(vendor_labels, NULL, 0444);
  83
  84#define dprintk(fmt, arg...)    if (debug) \
  85        printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg)
  86#define vprintk(fmt, arg...)    if (verbose) \
  87        printk(KERN_INFO "%s: " fmt, ch->name , ## arg)
  88
  89/* ------------------------------------------------------------------- */
  90
  91#define MAX_RETRIES   1
  92
  93static struct class * ch_sysfs_class;
  94
  95typedef struct {
  96        struct list_head    list;
  97        int                 minor;
  98        char                name[8];
  99        struct scsi_device  *device;
 100        struct scsi_device  **dt;        /* ptrs to data transfer elements */
 101        u_int               firsts[CH_TYPES];
 102        u_int               counts[CH_TYPES];
 103        u_int               unit_attention;
 104        u_int               voltags;
 105        struct mutex        lock;
 106} scsi_changer;
 107
 108static DEFINE_IDR(ch_index_idr);
 109static DEFINE_SPINLOCK(ch_index_lock);
 110
 111static const struct {
 112        unsigned char  sense;
 113        unsigned char  asc;
 114        unsigned char  ascq;
 115        int            errno;
 116} err[] = {
 117/* Just filled in what looks right. Hav'nt checked any standard paper for
 118   these errno assignments, so they may be wrong... */
 119        {
 120                .sense  = ILLEGAL_REQUEST,
 121                .asc    = 0x21,
 122                .ascq   = 0x01,
 123                .errno  = EBADSLT, /* Invalid element address */
 124        },{
 125                .sense  = ILLEGAL_REQUEST,
 126                .asc    = 0x28,
 127                .ascq   = 0x01,
 128                .errno  = EBADE,   /* Import or export element accessed */
 129        },{
 130                .sense  = ILLEGAL_REQUEST,
 131                .asc    = 0x3B,
 132                .ascq   = 0x0D,
 133                .errno  = EXFULL,  /* Medium destination element full */
 134        },{
 135                .sense  = ILLEGAL_REQUEST,
 136                .asc    = 0x3B,
 137                .ascq   = 0x0E,
 138                .errno  = EBADE,   /* Medium source element empty */
 139        },{
 140                .sense  = ILLEGAL_REQUEST,
 141                .asc    = 0x20,
 142                .ascq   = 0x00,
 143                .errno  = EBADRQC, /* Invalid command operation code */
 144        },{
 145                /* end of list */
 146        }
 147};
 148
 149/* ------------------------------------------------------------------- */
 150
 151static int ch_find_errno(struct scsi_sense_hdr *sshdr)
 152{
 153        int i,errno = 0;
 154
 155        /* Check to see if additional sense information is available */
 156        if (scsi_sense_valid(sshdr) &&
 157            sshdr->asc != 0) {
 158                for (i = 0; err[i].errno != 0; i++) {
 159                        if (err[i].sense == sshdr->sense_key &&
 160                            err[i].asc   == sshdr->asc &&
 161                            err[i].ascq  == sshdr->ascq) {
 162                                errno = -err[i].errno;
 163                                break;
 164                        }
 165                }
 166        }
 167        if (errno == 0)
 168                errno = -EIO;
 169        return errno;
 170}
 171
 172static int
 173ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
 174           void *buffer, unsigned buflength,
 175           enum dma_data_direction direction)
 176{
 177        int errno, retries = 0, timeout, result;
 178        struct scsi_sense_hdr sshdr;
 179
 180        timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
 181                ? timeout_init : timeout_move;
 182
 183 retry:
 184        errno = 0;
 185        if (debug) {
 186                dprintk("command: ");
 187                __scsi_print_command(cmd);
 188        }
 189
 190        result = scsi_execute_req(ch->device, cmd, direction, buffer,
 191                                  buflength, &sshdr, timeout * HZ,
 192                                  MAX_RETRIES);
 193
 194        dprintk("result: 0x%x\n",result);
 195        if (driver_byte(result) & DRIVER_SENSE) {
 196                if (debug)
 197                        scsi_print_sense_hdr(ch->name, &sshdr);
 198                errno = ch_find_errno(&sshdr);
 199
 200                switch(sshdr.sense_key) {
 201                case UNIT_ATTENTION:
 202                        ch->unit_attention = 1;
 203                        if (retries++ < 3)
 204                                goto retry;
 205                        break;
 206                }
 207        }
 208        return errno;
 209}
 210
 211/* ------------------------------------------------------------------------ */
 212
 213static int
 214ch_elem_to_typecode(scsi_changer *ch, u_int elem)
 215{
 216        int i;
 217
 218        for (i = 0; i < CH_TYPES; i++) {
 219                if (elem >= ch->firsts[i]  &&
 220                    elem <  ch->firsts[i] +
 221                    ch->counts[i])
 222                        return i+1;
 223        }
 224        return 0;
 225}
 226
 227static int
 228ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
 229{
 230        u_char  cmd[12];
 231        u_char  *buffer;
 232        int     result;
 233
 234        buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
 235        if(!buffer)
 236                return -ENOMEM;
 237
 238 retry:
 239        memset(cmd,0,sizeof(cmd));
 240        cmd[0] = READ_ELEMENT_STATUS;
 241        cmd[1] = (ch->device->lun << 5) |
 242                (ch->voltags ? 0x10 : 0) |
 243                ch_elem_to_typecode(ch,elem);
 244        cmd[2] = (elem >> 8) & 0xff;
 245        cmd[3] = elem        & 0xff;
 246        cmd[5] = 1;
 247        cmd[9] = 255;
 248        if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
 249                if (((buffer[16] << 8) | buffer[17]) != elem) {
 250                        dprintk("asked for element 0x%02x, got 0x%02x\n",
 251                                elem,(buffer[16] << 8) | buffer[17]);
 252                        kfree(buffer);
 253                        return -EIO;
 254                }
 255                memcpy(data,buffer+16,16);
 256        } else {
 257                if (ch->voltags) {
 258                        ch->voltags = 0;
 259                        vprintk("device has no volume tag support\n");
 260                        goto retry;
 261                }
 262                dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
 263        }
 264        kfree(buffer);
 265        return result;
 266}
 267
 268static int
 269ch_init_elem(scsi_changer *ch)
 270{
 271        int err;
 272        u_char cmd[6];
 273
 274        vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
 275        memset(cmd,0,sizeof(cmd));
 276        cmd[0] = INITIALIZE_ELEMENT_STATUS;
 277        cmd[1] = ch->device->lun << 5;
 278        err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
 279        vprintk("... finished\n");
 280        return err;
 281}
 282
 283static int
 284ch_readconfig(scsi_changer *ch)
 285{
 286        u_char  cmd[10], data[16];
 287        u_char  *buffer;
 288        int     result,id,lun,i;
 289        u_int   elem;
 290
 291        buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
 292        if (!buffer)
 293                return -ENOMEM;
 294
 295        memset(cmd,0,sizeof(cmd));
 296        cmd[0] = MODE_SENSE;
 297        cmd[1] = ch->device->lun << 5;
 298        cmd[2] = 0x1d;
 299        cmd[4] = 255;
 300        result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
 301        if (0 != result) {
 302                cmd[1] |= (1<<3);
 303                result  = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
 304        }
 305        if (0 == result) {
 306                ch->firsts[CHET_MT] =
 307                        (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
 308                ch->counts[CHET_MT] =
 309                        (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
 310                ch->firsts[CHET_ST] =
 311                        (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
 312                ch->counts[CHET_ST] =
 313                        (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
 314                ch->firsts[CHET_IE] =
 315                        (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
 316                ch->counts[CHET_IE] =
 317                        (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
 318                ch->firsts[CHET_DT] =
 319                        (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
 320                ch->counts[CHET_DT] =
 321                        (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
 322                vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
 323                        ch->firsts[CHET_MT],
 324                        ch->counts[CHET_MT]);
 325                vprintk("type #2 (st): 0x%x+%d [storage]\n",
 326                        ch->firsts[CHET_ST],
 327                        ch->counts[CHET_ST]);
 328                vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
 329                        ch->firsts[CHET_IE],
 330                        ch->counts[CHET_IE]);
 331                vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
 332                        ch->firsts[CHET_DT],
 333                        ch->counts[CHET_DT]);
 334        } else {
 335                vprintk("reading element address assigment page failed!\n");
 336        }
 337
 338        /* vendor specific element types */
 339        for (i = 0; i < 4; i++) {
 340                if (0 == vendor_counts[i])
 341                        continue;
 342                if (NULL == vendor_labels[i])
 343                        continue;
 344                ch->firsts[CHET_V1+i] = vendor_firsts[i];
 345                ch->counts[CHET_V1+i] = vendor_counts[i];
 346                vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
 347                        i+5,i+1,vendor_firsts[i],vendor_counts[i],
 348                        vendor_labels[i]);
 349        }
 350
 351        /* look up the devices of the data transfer elements */
 352        ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
 353                         GFP_KERNEL);
 354        for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
 355                id  = -1;
 356                lun = 0;
 357                if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {
 358                        id  = dt_id[elem];
 359                        lun = dt_lun[elem];
 360                        vprintk("dt 0x%x: [insmod option] ",
 361                                elem+ch->firsts[CHET_DT]);
 362                } else if (0 != ch_read_element_status
 363                           (ch,elem+ch->firsts[CHET_DT],data)) {
 364                        vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
 365                                elem+ch->firsts[CHET_DT]);
 366                } else {
 367                        vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
 368                        if (data[6] & 0x80) {
 369                                if (verbose)
 370                                        printk("not this SCSI bus\n");
 371                                ch->dt[elem] = NULL;
 372                        } else if (0 == (data[6] & 0x30)) {
 373                                if (verbose)
 374                                        printk("ID/LUN unknown\n");
 375                                ch->dt[elem] = NULL;
 376                        } else {
 377                                id  = ch->device->id;
 378                                lun = 0;
 379                                if (data[6] & 0x20) id  = data[7];
 380                                if (data[6] & 0x10) lun = data[6] & 7;
 381                        }
 382                }
 383                if (-1 != id) {
 384                        if (verbose)
 385                                printk("ID %i, LUN %i, ",id,lun);
 386                        ch->dt[elem] =
 387                                scsi_device_lookup(ch->device->host,
 388                                                   ch->device->channel,
 389                                                   id,lun);
 390                        if (!ch->dt[elem]) {
 391                                /* should not happen */
 392                                if (verbose)
 393                                        printk("Huh? device not found!\n");
 394                        } else {
 395                                if (verbose)
 396                                        printk("name: %8.8s %16.16s %4.4s\n",
 397                                               ch->dt[elem]->vendor,
 398                                               ch->dt[elem]->model,
 399                                               ch->dt[elem]->rev);
 400                        }
 401                }
 402        }
 403        ch->voltags = 1;
 404        kfree(buffer);
 405
 406        return 0;
 407}
 408
 409/* ------------------------------------------------------------------------ */
 410
 411static int
 412ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
 413{
 414        u_char  cmd[10];
 415
 416        dprintk("position: 0x%x\n",elem);
 417        if (0 == trans)
 418                trans = ch->firsts[CHET_MT];
 419        memset(cmd,0,sizeof(cmd));
 420        cmd[0]  = POSITION_TO_ELEMENT;
 421        cmd[1]  = ch->device->lun << 5;
 422        cmd[2]  = (trans >> 8) & 0xff;
 423        cmd[3]  =  trans       & 0xff;
 424        cmd[4]  = (elem  >> 8) & 0xff;
 425        cmd[5]  =  elem        & 0xff;
 426        cmd[8]  = rotate ? 1 : 0;
 427        return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
 428}
 429
 430static int
 431ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
 432{
 433        u_char  cmd[12];
 434
 435        dprintk("move: 0x%x => 0x%x\n",src,dest);
 436        if (0 == trans)
 437                trans = ch->firsts[CHET_MT];
 438        memset(cmd,0,sizeof(cmd));
 439        cmd[0]  = MOVE_MEDIUM;
 440        cmd[1]  = ch->device->lun << 5;
 441        cmd[2]  = (trans >> 8) & 0xff;
 442        cmd[3]  =  trans       & 0xff;
 443        cmd[4]  = (src   >> 8) & 0xff;
 444        cmd[5]  =  src         & 0xff;
 445        cmd[6]  = (dest  >> 8) & 0xff;
 446        cmd[7]  =  dest        & 0xff;
 447        cmd[10] = rotate ? 1 : 0;
 448        return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
 449}
 450
 451static int
 452ch_exchange(scsi_changer *ch, u_int trans, u_int src,
 453            u_int dest1, u_int dest2, int rotate1, int rotate2)
 454{
 455        u_char  cmd[12];
 456
 457        dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
 458                src,dest1,dest2);
 459        if (0 == trans)
 460                trans = ch->firsts[CHET_MT];
 461        memset(cmd,0,sizeof(cmd));
 462        cmd[0]  = EXCHANGE_MEDIUM;
 463        cmd[1]  = ch->device->lun << 5;
 464        cmd[2]  = (trans >> 8) & 0xff;
 465        cmd[3]  =  trans       & 0xff;
 466        cmd[4]  = (src   >> 8) & 0xff;
 467        cmd[5]  =  src         & 0xff;
 468        cmd[6]  = (dest1 >> 8) & 0xff;
 469        cmd[7]  =  dest1       & 0xff;
 470        cmd[8]  = (dest2 >> 8) & 0xff;
 471        cmd[9]  =  dest2       & 0xff;
 472        cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
 473
 474        return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
 475}
 476
 477static void
 478ch_check_voltag(char *tag)
 479{
 480        int i;
 481
 482        for (i = 0; i < 32; i++) {
 483                /* restrict to ascii */
 484                if (tag[i] >= 0x7f || tag[i] < 0x20)
 485                        tag[i] = ' ';
 486                /* don't allow search wildcards */
 487                if (tag[i] == '?' ||
 488                    tag[i] == '*')
 489                        tag[i] = ' ';
 490        }
 491}
 492
 493static int
 494ch_set_voltag(scsi_changer *ch, u_int elem,
 495              int alternate, int clear, u_char *tag)
 496{
 497        u_char  cmd[12];
 498        u_char  *buffer;
 499        int result;
 500
 501        buffer = kzalloc(512, GFP_KERNEL);
 502        if (!buffer)
 503                return -ENOMEM;
 504
 505        dprintk("%s %s voltag: 0x%x => \"%s\"\n",
 506                clear     ? "clear"     : "set",
 507                alternate ? "alternate" : "primary",
 508                elem, tag);
 509        memset(cmd,0,sizeof(cmd));
 510        cmd[0]  = SEND_VOLUME_TAG;
 511        cmd[1] = (ch->device->lun << 5) |
 512                ch_elem_to_typecode(ch,elem);
 513        cmd[2] = (elem >> 8) & 0xff;
 514        cmd[3] = elem        & 0xff;
 515        cmd[5] = clear
 516                ? (alternate ? 0x0d : 0x0c)
 517                : (alternate ? 0x0b : 0x0a);
 518
 519        cmd[9] = 255;
 520
 521        memcpy(buffer,tag,32);
 522        ch_check_voltag(buffer);
 523
 524        result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
 525        kfree(buffer);
 526        return result;
 527}
 528
 529static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
 530{
 531        int retval = 0;
 532        u_char data[16];
 533        unsigned int i;
 534
 535        mutex_lock(&ch->lock);
 536        for (i = 0; i < ch->counts[type]; i++) {
 537                if (0 != ch_read_element_status
 538                    (ch, ch->firsts[type]+i,data)) {
 539                        retval = -EIO;
 540                        break;
 541                }
 542                put_user(data[2], dest+i);
 543                if (data[2] & CESTATUS_EXCEPT)
 544                        vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
 545                                ch->firsts[type]+i,
 546                                (int)data[4],(int)data[5]);
 547                retval = ch_read_element_status
 548                        (ch, ch->firsts[type]+i,data);
 549                if (0 != retval)
 550                        break;
 551        }
 552        mutex_unlock(&ch->lock);
 553        return retval;
 554}
 555
 556/* ------------------------------------------------------------------------ */
 557
 558static int
 559ch_release(struct inode *inode, struct file *file)
 560{
 561        scsi_changer *ch = file->private_data;
 562
 563        scsi_device_put(ch->device);
 564        file->private_data = NULL;
 565        return 0;
 566}
 567
 568static int
 569ch_open(struct inode *inode, struct file *file)
 570{
 571        scsi_changer *ch;
 572        int minor = iminor(inode);
 573
 574        spin_lock(&ch_index_lock);
 575        ch = idr_find(&ch_index_idr, minor);
 576
 577        if (NULL == ch || scsi_device_get(ch->device)) {
 578                spin_unlock(&ch_index_lock);
 579                return -ENXIO;
 580        }
 581        spin_unlock(&ch_index_lock);
 582
 583        file->private_data = ch;
 584        return 0;
 585}
 586
 587static int
 588ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
 589{
 590        if (type >= CH_TYPES  ||  unit >= ch->counts[type])
 591                return -1;
 592        return 0;
 593}
 594
 595static long ch_ioctl(struct file *file,
 596                    unsigned int cmd, unsigned long arg)
 597{
 598        scsi_changer *ch = file->private_data;
 599        int retval;
 600        void __user *argp = (void __user *)arg;
 601
 602        switch (cmd) {
 603        case CHIOGPARAMS:
 604        {
 605                struct changer_params params;
 606
 607                params.cp_curpicker = 0;
 608                params.cp_npickers  = ch->counts[CHET_MT];
 609                params.cp_nslots    = ch->counts[CHET_ST];
 610                params.cp_nportals  = ch->counts[CHET_IE];
 611                params.cp_ndrives   = ch->counts[CHET_DT];
 612
 613                if (copy_to_user(argp, &params, sizeof(params)))
 614                        return -EFAULT;
 615                return 0;
 616        }
 617        case CHIOGVPARAMS:
 618        {
 619                struct changer_vendor_params vparams;
 620
 621                memset(&vparams,0,sizeof(vparams));
 622                if (ch->counts[CHET_V1]) {
 623                        vparams.cvp_n1  = ch->counts[CHET_V1];
 624                        strncpy(vparams.cvp_label1,vendor_labels[0],16);
 625                }
 626                if (ch->counts[CHET_V2]) {
 627                        vparams.cvp_n2  = ch->counts[CHET_V2];
 628                        strncpy(vparams.cvp_label2,vendor_labels[1],16);
 629                }
 630                if (ch->counts[CHET_V3]) {
 631                        vparams.cvp_n3  = ch->counts[CHET_V3];
 632                        strncpy(vparams.cvp_label3,vendor_labels[2],16);
 633                }
 634                if (ch->counts[CHET_V4]) {
 635                        vparams.cvp_n4  = ch->counts[CHET_V4];
 636                        strncpy(vparams.cvp_label4,vendor_labels[3],16);
 637                }
 638                if (copy_to_user(argp, &vparams, sizeof(vparams)))
 639                        return -EFAULT;
 640                return 0;
 641        }
 642
 643        case CHIOPOSITION:
 644        {
 645                struct changer_position pos;
 646
 647                if (copy_from_user(&pos, argp, sizeof (pos)))
 648                        return -EFAULT;
 649
 650                if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
 651                        dprintk("CHIOPOSITION: invalid parameter\n");
 652                        return -EBADSLT;
 653                }
 654                mutex_lock(&ch->lock);
 655                retval = ch_position(ch,0,
 656                                     ch->firsts[pos.cp_type] + pos.cp_unit,
 657                                     pos.cp_flags & CP_INVERT);
 658                mutex_unlock(&ch->lock);
 659                return retval;
 660        }
 661
 662        case CHIOMOVE:
 663        {
 664                struct changer_move mv;
 665
 666                if (copy_from_user(&mv, argp, sizeof (mv)))
 667                        return -EFAULT;
 668
 669                if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
 670                    0 != ch_checkrange(ch, mv.cm_totype,   mv.cm_tounit  )) {
 671                        dprintk("CHIOMOVE: invalid parameter\n");
 672                        return -EBADSLT;
 673                }
 674
 675                mutex_lock(&ch->lock);
 676                retval = ch_move(ch,0,
 677                                 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
 678                                 ch->firsts[mv.cm_totype]   + mv.cm_tounit,
 679                                 mv.cm_flags & CM_INVERT);
 680                mutex_unlock(&ch->lock);
 681                return retval;
 682        }
 683
 684        case CHIOEXCHANGE:
 685        {
 686                struct changer_exchange mv;
 687
 688                if (copy_from_user(&mv, argp, sizeof (mv)))
 689                        return -EFAULT;
 690
 691                if (0 != ch_checkrange(ch, mv.ce_srctype,  mv.ce_srcunit ) ||
 692                    0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
 693                    0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
 694                        dprintk("CHIOEXCHANGE: invalid parameter\n");
 695                        return -EBADSLT;
 696                }
 697
 698                mutex_lock(&ch->lock);
 699                retval = ch_exchange
 700                        (ch,0,
 701                         ch->firsts[mv.ce_srctype]  + mv.ce_srcunit,
 702                         ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
 703                         ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
 704                         mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
 705                mutex_unlock(&ch->lock);
 706                return retval;
 707        }
 708
 709        case CHIOGSTATUS:
 710        {
 711                struct changer_element_status ces;
 712
 713                if (copy_from_user(&ces, argp, sizeof (ces)))
 714                        return -EFAULT;
 715                if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
 716                        return -EINVAL;
 717
 718                return ch_gstatus(ch, ces.ces_type, ces.ces_data);
 719        }
 720
 721        case CHIOGELEM:
 722        {
 723                struct changer_get_element cge;
 724                u_char  cmd[12];
 725                u_char  *buffer;
 726                unsigned int elem;
 727                int     result,i;
 728
 729                if (copy_from_user(&cge, argp, sizeof (cge)))
 730                        return -EFAULT;
 731
 732                if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
 733                        return -EINVAL;
 734                elem = ch->firsts[cge.cge_type] + cge.cge_unit;
 735
 736                buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
 737                if (!buffer)
 738                        return -ENOMEM;
 739                mutex_lock(&ch->lock);
 740
 741        voltag_retry:
 742                memset(cmd,0,sizeof(cmd));
 743                cmd[0] = READ_ELEMENT_STATUS;
 744                cmd[1] = (ch->device->lun << 5) |
 745                        (ch->voltags ? 0x10 : 0) |
 746                        ch_elem_to_typecode(ch,elem);
 747                cmd[2] = (elem >> 8) & 0xff;
 748                cmd[3] = elem        & 0xff;
 749                cmd[5] = 1;
 750                cmd[9] = 255;
 751
 752                if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
 753                        cge.cge_status = buffer[18];
 754                        cge.cge_flags = 0;
 755                        if (buffer[18] & CESTATUS_EXCEPT) {
 756                                cge.cge_errno = EIO;
 757                        }
 758                        if (buffer[25] & 0x80) {
 759                                cge.cge_flags |= CGE_SRC;
 760                                if (buffer[25] & 0x40)
 761                                        cge.cge_flags |= CGE_INVERT;
 762                                elem = (buffer[26]<<8) | buffer[27];
 763                                for (i = 0; i < 4; i++) {
 764                                        if (elem >= ch->firsts[i] &&
 765                                            elem <  ch->firsts[i] + ch->counts[i]) {
 766                                                cge.cge_srctype = i;
 767                                                cge.cge_srcunit = elem-ch->firsts[i];
 768                                        }
 769                                }
 770                        }
 771                        if ((buffer[22] & 0x30) == 0x30) {
 772                                cge.cge_flags |= CGE_IDLUN;
 773                                cge.cge_id  = buffer[23];
 774                                cge.cge_lun = buffer[22] & 7;
 775                        }
 776                        if (buffer[9] & 0x80) {
 777                                cge.cge_flags |= CGE_PVOLTAG;
 778                                memcpy(cge.cge_pvoltag,buffer+28,36);
 779                        }
 780                        if (buffer[9] & 0x40) {
 781                                cge.cge_flags |= CGE_AVOLTAG;
 782                                memcpy(cge.cge_avoltag,buffer+64,36);
 783                        }
 784                } else if (ch->voltags) {
 785                        ch->voltags = 0;
 786                        vprintk("device has no volume tag support\n");
 787                        goto voltag_retry;
 788                }
 789                kfree(buffer);
 790                mutex_unlock(&ch->lock);
 791
 792                if (copy_to_user(argp, &cge, sizeof (cge)))
 793                        return -EFAULT;
 794                return result;
 795        }
 796
 797        case CHIOINITELEM:
 798        {
 799                mutex_lock(&ch->lock);
 800                retval = ch_init_elem(ch);
 801                mutex_unlock(&ch->lock);
 802                return retval;
 803        }
 804
 805        case CHIOSVOLTAG:
 806        {
 807                struct changer_set_voltag csv;
 808                int elem;
 809
 810                if (copy_from_user(&csv, argp, sizeof(csv)))
 811                        return -EFAULT;
 812
 813                if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
 814                        dprintk("CHIOSVOLTAG: invalid parameter\n");
 815                        return -EBADSLT;
 816                }
 817                elem = ch->firsts[csv.csv_type] + csv.csv_unit;
 818                mutex_lock(&ch->lock);
 819                retval = ch_set_voltag(ch, elem,
 820                                       csv.csv_flags & CSV_AVOLTAG,
 821                                       csv.csv_flags & CSV_CLEARTAG,
 822                                       csv.csv_voltag);
 823                mutex_unlock(&ch->lock);
 824                return retval;
 825        }
 826
 827        default:
 828                return scsi_ioctl(ch->device, cmd, argp);
 829
 830        }
 831}
 832
 833#ifdef CONFIG_COMPAT
 834
 835struct changer_element_status32 {
 836        int             ces_type;
 837        compat_uptr_t   ces_data;
 838};
 839#define CHIOGSTATUS32  _IOW('c', 8,struct changer_element_status32)
 840
 841static long ch_ioctl_compat(struct file * file,
 842                            unsigned int cmd, unsigned long arg)
 843{
 844        scsi_changer *ch = file->private_data;
 845
 846        switch (cmd) {
 847        case CHIOGPARAMS:
 848        case CHIOGVPARAMS:
 849        case CHIOPOSITION:
 850        case CHIOMOVE:
 851        case CHIOEXCHANGE:
 852        case CHIOGELEM:
 853        case CHIOINITELEM:
 854        case CHIOSVOLTAG:
 855                /* compatible */
 856                return ch_ioctl(file, cmd, arg);
 857        case CHIOGSTATUS32:
 858        {
 859                struct changer_element_status32 ces32;
 860                unsigned char __user *data;
 861
 862                if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
 863                        return -EFAULT;
 864                if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
 865                        return -EINVAL;
 866
 867                data = compat_ptr(ces32.ces_data);
 868                return ch_gstatus(ch, ces32.ces_type, data);
 869        }
 870        default:
 871                // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
 872                return -ENOIOCTLCMD;
 873
 874        }
 875}
 876#endif
 877
 878/* ------------------------------------------------------------------------ */
 879
 880static int ch_probe(struct device *dev)
 881{
 882        struct scsi_device *sd = to_scsi_device(dev);
 883        struct class_device *class_dev;
 884        int minor, ret = -ENOMEM;
 885        scsi_changer *ch;
 886
 887        if (sd->type != TYPE_MEDIUM_CHANGER)
 888                return -ENODEV;
 889
 890        ch = kzalloc(sizeof(*ch), GFP_KERNEL);
 891        if (NULL == ch)
 892                return -ENOMEM;
 893
 894        if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
 895                goto free_ch;
 896
 897        spin_lock(&ch_index_lock);
 898        ret = idr_get_new(&ch_index_idr, ch, &minor);
 899        spin_unlock(&ch_index_lock);
 900
 901        if (ret)
 902                goto free_ch;
 903
 904        if (minor > CH_MAX_DEVS) {
 905                ret = -ENODEV;
 906                goto remove_idr;
 907        }
 908
 909        ch->minor = minor;
 910        sprintf(ch->name,"ch%d",ch->minor);
 911
 912        class_dev = class_device_create(ch_sysfs_class, NULL,
 913                                        MKDEV(SCSI_CHANGER_MAJOR, ch->minor),
 914                                        dev, "s%s", ch->name);
 915        if (IS_ERR(class_dev)) {
 916                printk(KERN_WARNING "ch%d: class_device_create failed\n",
 917                       ch->minor);
 918                ret = PTR_ERR(class_dev);
 919                goto remove_idr;
 920        }
 921
 922        mutex_init(&ch->lock);
 923        ch->device = sd;
 924        ch_readconfig(ch);
 925        if (init)
 926                ch_init_elem(ch);
 927
 928        dev_set_drvdata(dev, ch);
 929        sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
 930
 931        return 0;
 932remove_idr:
 933        idr_remove(&ch_index_idr, minor);
 934free_ch:
 935        kfree(ch);
 936        return ret;
 937}
 938
 939static int ch_remove(struct device *dev)
 940{
 941        scsi_changer *ch = dev_get_drvdata(dev);
 942
 943        spin_lock(&ch_index_lock);
 944        idr_remove(&ch_index_idr, ch->minor);
 945        spin_unlock(&ch_index_lock);
 946
 947        class_device_destroy(ch_sysfs_class,
 948                             MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
 949        kfree(ch->dt);
 950        kfree(ch);
 951        return 0;
 952}
 953
 954static struct scsi_driver ch_template = {
 955        .owner          = THIS_MODULE,
 956        .gendrv         = {
 957                .name   = "ch",
 958                .probe  = ch_probe,
 959                .remove = ch_remove,
 960        },
 961};
 962
 963static const struct file_operations changer_fops = {
 964        .owner          = THIS_MODULE,
 965        .open           = ch_open,
 966        .release        = ch_release,
 967        .unlocked_ioctl = ch_ioctl,
 968#ifdef CONFIG_COMPAT
 969        .compat_ioctl   = ch_ioctl_compat,
 970#endif
 971};
 972
 973static int __init init_ch_module(void)
 974{
 975        int rc;
 976
 977        printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
 978        ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
 979        if (IS_ERR(ch_sysfs_class)) {
 980                rc = PTR_ERR(ch_sysfs_class);
 981                return rc;
 982        }
 983        rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
 984        if (rc < 0) {
 985                printk("Unable to get major %d for SCSI-Changer\n",
 986                       SCSI_CHANGER_MAJOR);
 987                goto fail1;
 988        }
 989        rc = scsi_register_driver(&ch_template.gendrv);
 990        if (rc < 0)
 991                goto fail2;
 992        return 0;
 993
 994 fail2:
 995        unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
 996 fail1:
 997        class_destroy(ch_sysfs_class);
 998        return rc;
 999}
1000
1001static void __exit exit_ch_module(void)
1002{
1003        scsi_unregister_driver(&ch_template.gendrv);
1004        unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1005        class_destroy(ch_sysfs_class);
1006        idr_destroy(&ch_index_idr);
1007}
1008
1009module_init(init_ch_module);
1010module_exit(exit_ch_module);
1011
1012/*
1013 * Local variables:
1014 * c-basic-offset: 8
1015 * End:
1016 */
1017
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.