2 * SCSI Media Changer device driver for Linux 2.6
4 * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
10 #include <linux/module.h>
11 #include <linux/init.h>
13 #include <linux/kernel.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>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_driver.h>
28 #include <scsi/scsi_ioctl.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_dbg.h>
37 MODULE_DESCRIPTION("device driver for scsi media changer devices");
38 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
39 MODULE_LICENSE("GPL");
40 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
43 module_param(init, int, 0444);
44 MODULE_PARM_DESC(init, \
45 "initialize element status on driver load (default: on)");
47 static int timeout_move = 300;
48 module_param(timeout_move, int, 0644);
49 MODULE_PARM_DESC(timeout_move,"timeout for move commands "
50 "(default: 300 seconds)");
52 static int timeout_init = 3600;
53 module_param(timeout_init, int, 0644);
54 MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
55 "(default: 3600 seconds)");
57 static int verbose = 1;
58 module_param(verbose, int, 0644);
59 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
64 "detailed sense codes on scsi errors (default: off)");
66 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
67 static int dt_lun[CH_DT_MAX];
68 module_param_array(dt_id, int, NULL, 0444);
69 module_param_array(dt_lun, int, NULL, 0444);
71 /* tell the driver about vendor-specific slots */
72 static int vendor_firsts[CH_TYPES-4];
73 static int vendor_counts[CH_TYPES-4];
74 module_param_array(vendor_firsts, int, NULL, 0444);
75 module_param_array(vendor_counts, int, NULL, 0444);
77 static const char * vendor_labels[CH_TYPES-4] = {
78 "v0", "v1", "v2", "v3"
80 // module_param_string_array(vendor_labels, NULL, 0444);
82 #define dprintk(fmt, arg...) if (debug) \
83 printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg)
84 #define vprintk(fmt, arg...) if (verbose) \
85 printk(KERN_INFO "%s: " fmt, ch->name , ## arg)
87 /* ------------------------------------------------------------------- */
91 static int ch_probe(struct device *);
92 static int ch_remove(struct device *);
93 static int ch_open(struct inode * inode, struct file * filp);
94 static int ch_release(struct inode * inode, struct file * filp);
95 static long ch_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
97 static long ch_ioctl_compat(struct file * filp,
98 unsigned int cmd, unsigned long arg);
101 static struct class * ch_sysfs_class;
104 struct list_head list;
107 struct scsi_device *device;
108 struct scsi_device **dt; /* ptrs to data transfer elements */
109 u_int firsts[CH_TYPES];
110 u_int counts[CH_TYPES];
111 u_int unit_attention;
116 static LIST_HEAD(ch_devlist);
117 static DEFINE_SPINLOCK(ch_devlist_lock);
118 static int ch_devcount;
120 static struct scsi_driver ch_template =
122 .owner = THIS_MODULE,
130 static const struct file_operations changer_fops =
132 .owner = THIS_MODULE,
134 .release = ch_release,
135 .unlocked_ioctl = ch_ioctl,
137 .compat_ioctl = ch_ioctl_compat,
141 static const struct {
147 /* Just filled in what looks right. Hav'nt checked any standard paper for
148 these errno assignments, so they may be wrong... */
150 .sense = ILLEGAL_REQUEST,
153 .errno = EBADSLT, /* Invalid element address */
155 .sense = ILLEGAL_REQUEST,
158 .errno = EBADE, /* Import or export element accessed */
160 .sense = ILLEGAL_REQUEST,
163 .errno = EXFULL, /* Medium destination element full */
165 .sense = ILLEGAL_REQUEST,
168 .errno = EBADE, /* Medium source element empty */
170 .sense = ILLEGAL_REQUEST,
173 .errno = EBADRQC, /* Invalid command operation code */
179 /* ------------------------------------------------------------------- */
181 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
185 /* Check to see if additional sense information is available */
186 if (scsi_sense_valid(sshdr) &&
188 for (i = 0; err[i].errno != 0; i++) {
189 if (err[i].sense == sshdr->sense_key &&
190 err[i].asc == sshdr->asc &&
191 err[i].ascq == sshdr->ascq) {
192 errno = -err[i].errno;
203 ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
204 void *buffer, unsigned buflength,
205 enum dma_data_direction direction)
207 int errno, retries = 0, timeout, result;
208 struct scsi_sense_hdr sshdr;
210 timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
211 ? timeout_init : timeout_move;
216 dprintk("command: ");
217 __scsi_print_command(cmd);
220 result = scsi_execute_req(ch->device, cmd, direction, buffer,
221 buflength, &sshdr, timeout * HZ,
224 dprintk("result: 0x%x\n",result);
225 if (driver_byte(result) & DRIVER_SENSE) {
227 scsi_print_sense_hdr(ch->name, &sshdr);
228 errno = ch_find_errno(&sshdr);
230 switch(sshdr.sense_key) {
232 ch->unit_attention = 1;
241 /* ------------------------------------------------------------------------ */
244 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
248 for (i = 0; i < CH_TYPES; i++) {
249 if (elem >= ch->firsts[i] &&
250 elem < ch->firsts[i] +
258 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
264 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
269 memset(cmd,0,sizeof(cmd));
270 cmd[0] = READ_ELEMENT_STATUS;
271 cmd[1] = (ch->device->lun << 5) |
272 (ch->voltags ? 0x10 : 0) |
273 ch_elem_to_typecode(ch,elem);
274 cmd[2] = (elem >> 8) & 0xff;
275 cmd[3] = elem & 0xff;
278 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
279 if (((buffer[16] << 8) | buffer[17]) != elem) {
280 dprintk("asked for element 0x%02x, got 0x%02x\n",
281 elem,(buffer[16] << 8) | buffer[17]);
285 memcpy(data,buffer+16,16);
289 vprintk("device has no volume tag support\n");
292 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
299 ch_init_elem(scsi_changer *ch)
304 vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
305 memset(cmd,0,sizeof(cmd));
306 cmd[0] = INITIALIZE_ELEMENT_STATUS;
307 cmd[1] = ch->device->lun << 5;
308 err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
309 vprintk("... finished\n");
314 ch_readconfig(scsi_changer *ch)
316 u_char cmd[10], data[16];
321 buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
325 memset(cmd,0,sizeof(cmd));
327 cmd[1] = ch->device->lun << 5;
330 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
333 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
336 ch->firsts[CHET_MT] =
337 (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
338 ch->counts[CHET_MT] =
339 (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
340 ch->firsts[CHET_ST] =
341 (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
342 ch->counts[CHET_ST] =
343 (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
344 ch->firsts[CHET_IE] =
345 (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
346 ch->counts[CHET_IE] =
347 (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
348 ch->firsts[CHET_DT] =
349 (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
350 ch->counts[CHET_DT] =
351 (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
352 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
354 ch->counts[CHET_MT]);
355 vprintk("type #2 (st): 0x%x+%d [storage]\n",
357 ch->counts[CHET_ST]);
358 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
360 ch->counts[CHET_IE]);
361 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
363 ch->counts[CHET_DT]);
365 vprintk("reading element address assigment page failed!\n");
368 /* vendor specific element types */
369 for (i = 0; i < 4; i++) {
370 if (0 == vendor_counts[i])
372 if (NULL == vendor_labels[i])
374 ch->firsts[CHET_V1+i] = vendor_firsts[i];
375 ch->counts[CHET_V1+i] = vendor_counts[i];
376 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
377 i+5,i+1,vendor_firsts[i],vendor_counts[i],
381 /* look up the devices of the data transfer elements */
382 ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
384 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
387 if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
390 vprintk("dt 0x%x: [insmod option] ",
391 elem+ch->firsts[CHET_DT]);
392 } else if (0 != ch_read_element_status
393 (ch,elem+ch->firsts[CHET_DT],data)) {
394 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
395 elem+ch->firsts[CHET_DT]);
397 vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
398 if (data[6] & 0x80) {
400 printk("not this SCSI bus\n");
402 } else if (0 == (data[6] & 0x30)) {
404 printk("ID/LUN unknown\n");
409 if (data[6] & 0x20) id = data[7];
410 if (data[6] & 0x10) lun = data[6] & 7;
415 printk("ID %i, LUN %i, ",id,lun);
417 scsi_device_lookup(ch->device->host,
421 /* should not happen */
423 printk("Huh? device not found!\n");
426 printk("name: %8.8s %16.16s %4.4s\n",
427 ch->dt[elem]->vendor,
439 /* ------------------------------------------------------------------------ */
442 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
446 dprintk("position: 0x%x\n",elem);
448 trans = ch->firsts[CHET_MT];
449 memset(cmd,0,sizeof(cmd));
450 cmd[0] = POSITION_TO_ELEMENT;
451 cmd[1] = ch->device->lun << 5;
452 cmd[2] = (trans >> 8) & 0xff;
453 cmd[3] = trans & 0xff;
454 cmd[4] = (elem >> 8) & 0xff;
455 cmd[5] = elem & 0xff;
456 cmd[8] = rotate ? 1 : 0;
457 return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
461 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
465 dprintk("move: 0x%x => 0x%x\n",src,dest);
467 trans = ch->firsts[CHET_MT];
468 memset(cmd,0,sizeof(cmd));
469 cmd[0] = MOVE_MEDIUM;
470 cmd[1] = ch->device->lun << 5;
471 cmd[2] = (trans >> 8) & 0xff;
472 cmd[3] = trans & 0xff;
473 cmd[4] = (src >> 8) & 0xff;
475 cmd[6] = (dest >> 8) & 0xff;
476 cmd[7] = dest & 0xff;
477 cmd[10] = rotate ? 1 : 0;
478 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
482 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
483 u_int dest1, u_int dest2, int rotate1, int rotate2)
487 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
490 trans = ch->firsts[CHET_MT];
491 memset(cmd,0,sizeof(cmd));
492 cmd[0] = EXCHANGE_MEDIUM;
493 cmd[1] = ch->device->lun << 5;
494 cmd[2] = (trans >> 8) & 0xff;
495 cmd[3] = trans & 0xff;
496 cmd[4] = (src >> 8) & 0xff;
498 cmd[6] = (dest1 >> 8) & 0xff;
499 cmd[7] = dest1 & 0xff;
500 cmd[8] = (dest2 >> 8) & 0xff;
501 cmd[9] = dest2 & 0xff;
502 cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
504 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
508 ch_check_voltag(char *tag)
512 for (i = 0; i < 32; i++) {
513 /* restrict to ascii */
514 if (tag[i] >= 0x7f || tag[i] < 0x20)
516 /* don't allow search wildcards */
524 ch_set_voltag(scsi_changer *ch, u_int elem,
525 int alternate, int clear, u_char *tag)
531 buffer = kzalloc(512, GFP_KERNEL);
535 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
536 clear ? "clear" : "set",
537 alternate ? "alternate" : "primary",
539 memset(cmd,0,sizeof(cmd));
540 cmd[0] = SEND_VOLUME_TAG;
541 cmd[1] = (ch->device->lun << 5) |
542 ch_elem_to_typecode(ch,elem);
543 cmd[2] = (elem >> 8) & 0xff;
544 cmd[3] = elem & 0xff;
546 ? (alternate ? 0x0d : 0x0c)
547 : (alternate ? 0x0b : 0x0a);
551 memcpy(buffer,tag,32);
552 ch_check_voltag(buffer);
554 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
559 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
565 mutex_lock(&ch->lock);
566 for (i = 0; i < ch->counts[type]; i++) {
567 if (0 != ch_read_element_status
568 (ch, ch->firsts[type]+i,data)) {
572 put_user(data[2], dest+i);
573 if (data[2] & CESTATUS_EXCEPT)
574 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
576 (int)data[4],(int)data[5]);
577 retval = ch_read_element_status
578 (ch, ch->firsts[type]+i,data);
582 mutex_unlock(&ch->lock);
586 /* ------------------------------------------------------------------------ */
589 ch_release(struct inode *inode, struct file *file)
591 scsi_changer *ch = file->private_data;
593 scsi_device_put(ch->device);
594 file->private_data = NULL;
599 ch_open(struct inode *inode, struct file *file)
601 scsi_changer *tmp, *ch;
602 int minor = iminor(inode);
604 spin_lock(&ch_devlist_lock);
606 list_for_each_entry(tmp,&ch_devlist,list) {
607 if (tmp->minor == minor)
610 if (NULL == ch || scsi_device_get(ch->device)) {
611 spin_unlock(&ch_devlist_lock);
614 spin_unlock(&ch_devlist_lock);
616 file->private_data = ch;
621 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
623 if (type >= CH_TYPES || unit >= ch->counts[type])
628 static long ch_ioctl(struct file *file,
629 unsigned int cmd, unsigned long arg)
631 scsi_changer *ch = file->private_data;
633 void __user *argp = (void __user *)arg;
638 struct changer_params params;
640 params.cp_curpicker = 0;
641 params.cp_npickers = ch->counts[CHET_MT];
642 params.cp_nslots = ch->counts[CHET_ST];
643 params.cp_nportals = ch->counts[CHET_IE];
644 params.cp_ndrives = ch->counts[CHET_DT];
646 if (copy_to_user(argp, ¶ms, sizeof(params)))
652 struct changer_vendor_params vparams;
654 memset(&vparams,0,sizeof(vparams));
655 if (ch->counts[CHET_V1]) {
656 vparams.cvp_n1 = ch->counts[CHET_V1];
657 strncpy(vparams.cvp_label1,vendor_labels[0],16);
659 if (ch->counts[CHET_V2]) {
660 vparams.cvp_n2 = ch->counts[CHET_V2];
661 strncpy(vparams.cvp_label2,vendor_labels[1],16);
663 if (ch->counts[CHET_V3]) {
664 vparams.cvp_n3 = ch->counts[CHET_V3];
665 strncpy(vparams.cvp_label3,vendor_labels[2],16);
667 if (ch->counts[CHET_V4]) {
668 vparams.cvp_n4 = ch->counts[CHET_V4];
669 strncpy(vparams.cvp_label4,vendor_labels[3],16);
671 if (copy_to_user(argp, &vparams, sizeof(vparams)))
678 struct changer_position pos;
680 if (copy_from_user(&pos, argp, sizeof (pos)))
683 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
684 dprintk("CHIOPOSITION: invalid parameter\n");
687 mutex_lock(&ch->lock);
688 retval = ch_position(ch,0,
689 ch->firsts[pos.cp_type] + pos.cp_unit,
690 pos.cp_flags & CP_INVERT);
691 mutex_unlock(&ch->lock);
697 struct changer_move mv;
699 if (copy_from_user(&mv, argp, sizeof (mv)))
702 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
703 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
704 dprintk("CHIOMOVE: invalid parameter\n");
708 mutex_lock(&ch->lock);
709 retval = ch_move(ch,0,
710 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
711 ch->firsts[mv.cm_totype] + mv.cm_tounit,
712 mv.cm_flags & CM_INVERT);
713 mutex_unlock(&ch->lock);
719 struct changer_exchange mv;
721 if (copy_from_user(&mv, argp, sizeof (mv)))
724 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
725 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
726 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
727 dprintk("CHIOEXCHANGE: invalid parameter\n");
731 mutex_lock(&ch->lock);
734 ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
735 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
736 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
737 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
738 mutex_unlock(&ch->lock);
744 struct changer_element_status ces;
746 if (copy_from_user(&ces, argp, sizeof (ces)))
748 if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
751 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
756 struct changer_get_element cge;
762 if (copy_from_user(&cge, argp, sizeof (cge)))
765 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
767 elem = ch->firsts[cge.cge_type] + cge.cge_unit;
769 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
772 mutex_lock(&ch->lock);
775 memset(cmd,0,sizeof(cmd));
776 cmd[0] = READ_ELEMENT_STATUS;
777 cmd[1] = (ch->device->lun << 5) |
778 (ch->voltags ? 0x10 : 0) |
779 ch_elem_to_typecode(ch,elem);
780 cmd[2] = (elem >> 8) & 0xff;
781 cmd[3] = elem & 0xff;
785 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
786 cge.cge_status = buffer[18];
788 if (buffer[18] & CESTATUS_EXCEPT) {
791 if (buffer[25] & 0x80) {
792 cge.cge_flags |= CGE_SRC;
793 if (buffer[25] & 0x40)
794 cge.cge_flags |= CGE_INVERT;
795 elem = (buffer[26]<<8) | buffer[27];
796 for (i = 0; i < 4; i++) {
797 if (elem >= ch->firsts[i] &&
798 elem < ch->firsts[i] + ch->counts[i]) {
800 cge.cge_srcunit = elem-ch->firsts[i];
804 if ((buffer[22] & 0x30) == 0x30) {
805 cge.cge_flags |= CGE_IDLUN;
806 cge.cge_id = buffer[23];
807 cge.cge_lun = buffer[22] & 7;
809 if (buffer[9] & 0x80) {
810 cge.cge_flags |= CGE_PVOLTAG;
811 memcpy(cge.cge_pvoltag,buffer+28,36);
813 if (buffer[9] & 0x40) {
814 cge.cge_flags |= CGE_AVOLTAG;
815 memcpy(cge.cge_avoltag,buffer+64,36);
817 } else if (ch->voltags) {
819 vprintk("device has no volume tag support\n");
823 mutex_unlock(&ch->lock);
825 if (copy_to_user(argp, &cge, sizeof (cge)))
832 mutex_lock(&ch->lock);
833 retval = ch_init_elem(ch);
834 mutex_unlock(&ch->lock);
840 struct changer_set_voltag csv;
843 if (copy_from_user(&csv, argp, sizeof(csv)))
846 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
847 dprintk("CHIOSVOLTAG: invalid parameter\n");
850 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
851 mutex_lock(&ch->lock);
852 retval = ch_set_voltag(ch, elem,
853 csv.csv_flags & CSV_AVOLTAG,
854 csv.csv_flags & CSV_CLEARTAG,
856 mutex_unlock(&ch->lock);
861 return scsi_ioctl(ch->device, cmd, argp);
868 struct changer_element_status32 {
870 compat_uptr_t ces_data;
872 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
874 static long ch_ioctl_compat(struct file * file,
875 unsigned int cmd, unsigned long arg)
877 scsi_changer *ch = file->private_data;
889 return ch_ioctl(file, cmd, arg);
892 struct changer_element_status32 ces32;
893 unsigned char __user *data;
895 if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
897 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
900 data = compat_ptr(ces32.ces_data);
901 return ch_gstatus(ch, ces32.ces_type, data);
904 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
911 /* ------------------------------------------------------------------------ */
913 static int ch_probe(struct device *dev)
915 struct scsi_device *sd = to_scsi_device(dev);
916 struct class_device *class_dev;
919 if (sd->type != TYPE_MEDIUM_CHANGER)
922 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
926 ch->minor = ch_devcount;
927 sprintf(ch->name,"ch%d",ch->minor);
929 class_dev = class_device_create(ch_sysfs_class, NULL,
930 MKDEV(SCSI_CHANGER_MAJOR, ch->minor),
931 dev, "s%s", ch->name);
932 if (IS_ERR(class_dev)) {
933 printk(KERN_WARNING "ch%d: class_device_create failed\n",
936 return PTR_ERR(class_dev);
939 mutex_init(&ch->lock);
945 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
947 spin_lock(&ch_devlist_lock);
948 list_add_tail(&ch->list,&ch_devlist);
950 spin_unlock(&ch_devlist_lock);
954 static int ch_remove(struct device *dev)
956 struct scsi_device *sd = to_scsi_device(dev);
957 scsi_changer *tmp, *ch;
959 spin_lock(&ch_devlist_lock);
961 list_for_each_entry(tmp,&ch_devlist,list) {
962 if (tmp->device == sd)
967 spin_unlock(&ch_devlist_lock);
969 class_device_destroy(ch_sysfs_class,
970 MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
977 static int __init init_ch_module(void)
981 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
982 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
983 if (IS_ERR(ch_sysfs_class)) {
984 rc = PTR_ERR(ch_sysfs_class);
987 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
989 printk("Unable to get major %d for SCSI-Changer\n",
993 rc = scsi_register_driver(&ch_template.gendrv);
999 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1001 class_destroy(ch_sysfs_class);
1005 static void __exit exit_ch_module(void)
1007 scsi_unregister_driver(&ch_template.gendrv);
1008 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1009 class_destroy(ch_sysfs_class);
1012 module_init(init_ch_module);
1013 module_exit(exit_ch_module);