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