Lines Matching +full:4 +full:- +full:ch

1 // SPDX-License-Identifier: GPL-2.0-only
5 * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
71 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
76 /* tell the driver about vendor-specific slots */
77 static int vendor_firsts[CH_TYPES-4];
78 static int vendor_counts[CH_TYPES-4];
82 static const char * vendor_labels[CH_TYPES-4] = {
87 #define ch_printk(prefix, ch, fmt, a...) \ argument
88 sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
93 ch_printk(KERN_DEBUG, ch, fmt, ##arg); \
98 ch_printk(level, ch, fmt, ##arg); \
101 /* ------------------------------------------------------------------- */
162 /* ------------------------------------------------------------------- */
170 sshdr->asc != 0) { in ch_find_errno()
172 if (ch_err[i].sense == sshdr->sense_key && in ch_find_errno()
173 ch_err[i].asc == sshdr->asc && in ch_find_errno()
174 ch_err[i].ascq == sshdr->ascq) { in ch_find_errno()
175 errno = -ch_err[i].errno; in ch_find_errno()
181 errno = -EIO; in ch_find_errno()
186 ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len, in ch_do_scsi() argument
198 result = scsi_execute_req(ch->device, cmd, direction, buffer, in ch_do_scsi()
205 scsi_print_sense_hdr(ch->device, ch->name, &sshdr); in ch_do_scsi()
210 ch->unit_attention = 1; in ch_do_scsi()
219 /* ------------------------------------------------------------------------ */
222 ch_elem_to_typecode(scsi_changer *ch, u_int elem) in ch_elem_to_typecode() argument
227 if (elem >= ch->firsts[i] && in ch_elem_to_typecode()
228 elem < ch->firsts[i] + in ch_elem_to_typecode()
229 ch->counts[i]) in ch_elem_to_typecode()
236 ch_read_element_status(scsi_changer *ch, u_int elem, char *data) in ch_read_element_status() argument
244 return -ENOMEM; in ch_read_element_status()
249 cmd[1] = ((ch->device->lun & 0x7) << 5) | in ch_read_element_status()
250 (ch->voltags ? 0x10 : 0) | in ch_read_element_status()
251 ch_elem_to_typecode(ch,elem); in ch_read_element_status()
256 if (0 == (result = ch_do_scsi(ch, cmd, 12, in ch_read_element_status()
262 return -EIO; in ch_read_element_status()
266 if (ch->voltags) { in ch_read_element_status()
267 ch->voltags = 0; in ch_read_element_status()
278 ch_init_elem(scsi_changer *ch) in ch_init_elem() argument
286 cmd[1] = (ch->device->lun & 0x7) << 5; in ch_init_elem()
287 err = ch_do_scsi(ch, cmd, 6, NULL, 0, DMA_NONE); in ch_init_elem()
293 ch_readconfig(scsi_changer *ch) in ch_readconfig() argument
302 return -ENOMEM; in ch_readconfig()
306 cmd[1] = (ch->device->lun & 0x7) << 5; in ch_readconfig()
308 cmd[4] = 255; in ch_readconfig()
309 result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE); in ch_readconfig()
312 result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE); in ch_readconfig()
315 ch->firsts[CHET_MT] = in ch_readconfig()
317 ch->counts[CHET_MT] = in ch_readconfig()
319 ch->firsts[CHET_ST] = in ch_readconfig()
321 ch->counts[CHET_ST] = in ch_readconfig()
323 ch->firsts[CHET_IE] = in ch_readconfig()
325 ch->counts[CHET_IE] = in ch_readconfig()
327 ch->firsts[CHET_DT] = in ch_readconfig()
329 ch->counts[CHET_DT] = in ch_readconfig()
332 ch->firsts[CHET_MT], in ch_readconfig()
333 ch->counts[CHET_MT]); in ch_readconfig()
335 ch->firsts[CHET_ST], in ch_readconfig()
336 ch->counts[CHET_ST]); in ch_readconfig()
338 ch->firsts[CHET_IE], in ch_readconfig()
339 ch->counts[CHET_IE]); in ch_readconfig()
340 VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n", in ch_readconfig()
341 ch->firsts[CHET_DT], in ch_readconfig()
342 ch->counts[CHET_DT]); in ch_readconfig()
348 for (i = 0; i < 4; i++) { in ch_readconfig()
353 ch->firsts[CHET_V1+i] = vendor_firsts[i]; in ch_readconfig()
354 ch->counts[CHET_V1+i] = vendor_counts[i]; in ch_readconfig()
361 ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt), in ch_readconfig()
364 if (!ch->dt) { in ch_readconfig()
366 return -ENOMEM; in ch_readconfig()
369 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) { in ch_readconfig()
370 id = -1; in ch_readconfig()
372 if (elem < CH_DT_MAX && -1 != dt_id[elem]) { in ch_readconfig()
376 elem+ch->firsts[CHET_DT]); in ch_readconfig()
378 (ch,elem+ch->firsts[CHET_DT],data)) { in ch_readconfig()
380 elem+ch->firsts[CHET_DT]); in ch_readconfig()
382 VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]); in ch_readconfig()
385 ch->dt[elem] = NULL; in ch_readconfig()
388 ch->dt[elem] = NULL; in ch_readconfig()
390 id = ch->device->id; in ch_readconfig()
396 if (-1 != id) { in ch_readconfig()
398 ch->dt[elem] = in ch_readconfig()
399 scsi_device_lookup(ch->device->host, in ch_readconfig()
400 ch->device->channel, in ch_readconfig()
402 if (!ch->dt[elem]) { in ch_readconfig()
407 ch->dt[elem]->vendor, in ch_readconfig()
408 ch->dt[elem]->model, in ch_readconfig()
409 ch->dt[elem]->rev); in ch_readconfig()
413 ch->voltags = 1; in ch_readconfig()
419 /* ------------------------------------------------------------------------ */
422 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate) in ch_position() argument
428 trans = ch->firsts[CHET_MT]; in ch_position()
431 cmd[1] = (ch->device->lun & 0x7) << 5; in ch_position()
434 cmd[4] = (elem >> 8) & 0xff; in ch_position()
437 return ch_do_scsi(ch, cmd, 10, NULL, 0, DMA_NONE); in ch_position()
441 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate) in ch_move() argument
447 trans = ch->firsts[CHET_MT]; in ch_move()
450 cmd[1] = (ch->device->lun & 0x7) << 5; in ch_move()
453 cmd[4] = (src >> 8) & 0xff; in ch_move()
458 return ch_do_scsi(ch, cmd, 12, NULL,0, DMA_NONE); in ch_move()
462 ch_exchange(scsi_changer *ch, u_int trans, u_int src, in ch_exchange() argument
470 trans = ch->firsts[CHET_MT]; in ch_exchange()
473 cmd[1] = (ch->device->lun & 0x7) << 5; in ch_exchange()
476 cmd[4] = (src >> 8) & 0xff; in ch_exchange()
484 return ch_do_scsi(ch, cmd, 12, NULL, 0, DMA_NONE); in ch_exchange()
504 ch_set_voltag(scsi_changer *ch, u_int elem, in ch_set_voltag() argument
513 return -ENOMEM; in ch_set_voltag()
521 cmd[1] = ((ch->device->lun & 0x7) << 5) | in ch_set_voltag()
522 ch_elem_to_typecode(ch,elem); in ch_set_voltag()
534 result = ch_do_scsi(ch, cmd, 12, buffer, 256, DMA_TO_DEVICE); in ch_set_voltag()
539 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) in ch_gstatus() argument
545 mutex_lock(&ch->lock); in ch_gstatus()
546 for (i = 0; i < ch->counts[type]; i++) { in ch_gstatus()
548 (ch, ch->firsts[type]+i,data)) { in ch_gstatus()
549 retval = -EIO; in ch_gstatus()
555 ch->firsts[type]+i, in ch_gstatus()
556 (int)data[4],(int)data[5]); in ch_gstatus()
558 (ch, ch->firsts[type]+i,data); in ch_gstatus()
562 mutex_unlock(&ch->lock); in ch_gstatus()
566 /* ------------------------------------------------------------------------ */
570 scsi_changer *ch = container_of(ref, scsi_changer, ref); in ch_destroy() local
572 ch->device = NULL; in ch_destroy()
573 kfree(ch->dt); in ch_destroy()
574 kfree(ch); in ch_destroy()
580 scsi_changer *ch = file->private_data; in ch_release() local
582 scsi_device_put(ch->device); in ch_release()
583 file->private_data = NULL; in ch_release()
584 kref_put(&ch->ref, ch_destroy); in ch_release()
591 scsi_changer *ch; in ch_open() local
595 ch = idr_find(&ch_index_idr, minor); in ch_open()
597 if (ch == NULL || !kref_get_unless_zero(&ch->ref)) { in ch_open()
599 return -ENXIO; in ch_open()
602 if (scsi_device_get(ch->device)) { in ch_open()
603 kref_put(&ch->ref, ch_destroy); in ch_open()
604 return -ENXIO; in ch_open()
607 mutex_lock(&ch->lock); in ch_open()
608 file->private_data = ch; in ch_open()
609 mutex_unlock(&ch->lock); in ch_open()
614 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit) in ch_checkrange() argument
616 if (type >= CH_TYPES || unit >= ch->counts[type]) in ch_checkrange()
617 return -1; in ch_checkrange()
630 scsi_changer *ch = file->private_data; in ch_ioctl() local
634 retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd, in ch_ioctl()
635 file->f_flags & O_NDELAY); in ch_ioctl()
645 params.cp_npickers = ch->counts[CHET_MT]; in ch_ioctl()
646 params.cp_nslots = ch->counts[CHET_ST]; in ch_ioctl()
647 params.cp_nportals = ch->counts[CHET_IE]; in ch_ioctl()
648 params.cp_ndrives = ch->counts[CHET_DT]; in ch_ioctl()
651 return -EFAULT; in ch_ioctl()
659 if (ch->counts[CHET_V1]) { in ch_ioctl()
660 vparams.cvp_n1 = ch->counts[CHET_V1]; in ch_ioctl()
663 if (ch->counts[CHET_V2]) { in ch_ioctl()
664 vparams.cvp_n2 = ch->counts[CHET_V2]; in ch_ioctl()
667 if (ch->counts[CHET_V3]) { in ch_ioctl()
668 vparams.cvp_n3 = ch->counts[CHET_V3]; in ch_ioctl()
671 if (ch->counts[CHET_V4]) { in ch_ioctl()
672 vparams.cvp_n4 = ch->counts[CHET_V4]; in ch_ioctl()
676 return -EFAULT; in ch_ioctl()
685 return -EFAULT; in ch_ioctl()
687 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) { in ch_ioctl()
689 return -EBADSLT; in ch_ioctl()
691 mutex_lock(&ch->lock); in ch_ioctl()
692 retval = ch_position(ch,0, in ch_ioctl()
693 ch->firsts[pos.cp_type] + pos.cp_unit, in ch_ioctl()
695 mutex_unlock(&ch->lock); in ch_ioctl()
704 return -EFAULT; in ch_ioctl()
706 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) || in ch_ioctl()
707 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) { in ch_ioctl()
709 return -EBADSLT; in ch_ioctl()
712 mutex_lock(&ch->lock); in ch_ioctl()
713 retval = ch_move(ch,0, in ch_ioctl()
714 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit, in ch_ioctl()
715 ch->firsts[mv.cm_totype] + mv.cm_tounit, in ch_ioctl()
717 mutex_unlock(&ch->lock); in ch_ioctl()
726 return -EFAULT; in ch_ioctl()
728 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) || in ch_ioctl()
729 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) || in ch_ioctl()
730 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) { in ch_ioctl()
732 return -EBADSLT; in ch_ioctl()
735 mutex_lock(&ch->lock); in ch_ioctl()
737 (ch,0, in ch_ioctl()
738 ch->firsts[mv.ce_srctype] + mv.ce_srcunit, in ch_ioctl()
739 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit, in ch_ioctl()
740 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit, in ch_ioctl()
742 mutex_unlock(&ch->lock); in ch_ioctl()
751 return -EFAULT; in ch_ioctl()
753 return -EINVAL; in ch_ioctl()
755 return ch_gstatus(ch, ces.ces_type, ces.ces_data); in ch_ioctl()
763 return -EFAULT; in ch_ioctl()
765 return -EINVAL; in ch_ioctl()
767 return ch_gstatus(ch, ces32.ces_type, in ch_ioctl()
780 return -EFAULT; in ch_ioctl()
782 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit)) in ch_ioctl()
783 return -EINVAL; in ch_ioctl()
784 elem = ch->firsts[cge.cge_type] + cge.cge_unit; in ch_ioctl()
788 return -ENOMEM; in ch_ioctl()
789 mutex_lock(&ch->lock); in ch_ioctl()
794 ch_cmd[1] = ((ch->device->lun & 0x7) << 5) | in ch_ioctl()
795 (ch->voltags ? 0x10 : 0) | in ch_ioctl()
796 ch_elem_to_typecode(ch,elem); in ch_ioctl()
802 result = ch_do_scsi(ch, ch_cmd, 12, in ch_ioctl()
815 for (i = 0; i < 4; i++) { in ch_ioctl()
816 if (elem >= ch->firsts[i] && in ch_ioctl()
817 elem < ch->firsts[i] + ch->counts[i]) { in ch_ioctl()
819 cge.cge_srcunit = elem-ch->firsts[i]; in ch_ioctl()
836 } else if (ch->voltags) { in ch_ioctl()
837 ch->voltags = 0; in ch_ioctl()
842 mutex_unlock(&ch->lock); in ch_ioctl()
845 return -EFAULT; in ch_ioctl()
851 mutex_lock(&ch->lock); in ch_ioctl()
852 retval = ch_init_elem(ch); in ch_ioctl()
853 mutex_unlock(&ch->lock); in ch_ioctl()
863 return -EFAULT; in ch_ioctl()
865 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) { in ch_ioctl()
867 return -EBADSLT; in ch_ioctl()
869 elem = ch->firsts[csv.csv_type] + csv.csv_unit; in ch_ioctl()
870 mutex_lock(&ch->lock); in ch_ioctl()
871 retval = ch_set_voltag(ch, elem, in ch_ioctl()
875 mutex_unlock(&ch->lock); in ch_ioctl()
880 return scsi_ioctl(ch->device, file->f_mode, cmd, argp); in ch_ioctl()
885 /* ------------------------------------------------------------------------ */
892 scsi_changer *ch; in ch_probe() local
894 if (sd->type != TYPE_MEDIUM_CHANGER) in ch_probe()
895 return -ENODEV; in ch_probe()
897 ch = kzalloc(sizeof(*ch), GFP_KERNEL); in ch_probe()
898 if (NULL == ch) in ch_probe()
899 return -ENOMEM; in ch_probe()
903 ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT); in ch_probe()
908 if (ret == -ENOSPC) in ch_probe()
909 ret = -ENODEV; in ch_probe()
913 ch->minor = ret; in ch_probe()
914 sprintf(ch->name,"ch%d",ch->minor); in ch_probe()
917 sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n", in ch_probe()
918 ch->minor); in ch_probe()
922 mutex_init(&ch->lock); in ch_probe()
923 kref_init(&ch->ref); in ch_probe()
924 ch->device = sd; in ch_probe()
926 MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch, in ch_probe()
927 "s%s", ch->name); in ch_probe()
929 sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n", in ch_probe()
930 ch->minor); in ch_probe()
935 mutex_lock(&ch->lock); in ch_probe()
936 ret = ch_readconfig(ch); in ch_probe()
938 mutex_unlock(&ch->lock); in ch_probe()
942 ch_init_elem(ch); in ch_probe()
944 mutex_unlock(&ch->lock); in ch_probe()
945 dev_set_drvdata(dev, ch); in ch_probe()
946 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name); in ch_probe()
950 device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor)); in ch_probe()
954 idr_remove(&ch_index_idr, ch->minor); in ch_probe()
956 kfree(ch); in ch_probe()
962 scsi_changer *ch = dev_get_drvdata(dev); in ch_remove() local
965 idr_remove(&ch_index_idr, ch->minor); in ch_remove()
969 device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor)); in ch_remove()
970 scsi_device_put(ch->device); in ch_remove()
971 kref_put(&ch->ref, ch_destroy); in ch_remove()
977 .name = "ch",
1003 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops); in init_ch_module()
1005 printk("Unable to get major %d for SCSI-Changer\n", in init_ch_module()
1015 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); in init_ch_module()
1024 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); in exit_ch_module()