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 int ch_ioctl(struct inode * inode, struct file * filp,
96 unsigned int cmd, unsigned long arg);
98 static long ch_ioctl_compat(struct file * filp,
99 unsigned int cmd, unsigned long arg);
102 static struct class * ch_sysfs_class;
105 struct list_head list;
108 struct scsi_device *device;
109 struct scsi_device **dt; /* ptrs to data transfer elements */
110 u_int firsts[CH_TYPES];
111 u_int counts[CH_TYPES];
112 u_int unit_attention;
117 static LIST_HEAD(ch_devlist);
118 static DEFINE_SPINLOCK(ch_devlist_lock);
119 static int ch_devcount;
121 static struct scsi_driver ch_template =
123 .owner = THIS_MODULE,
131 static const struct file_operations changer_fops =
133 .owner = THIS_MODULE,
135 .release = ch_release,
138 .compat_ioctl = ch_ioctl_compat,
142 static const struct {
148 /* Just filled in what looks right. Hav'nt checked any standard paper for
149 these errno assignments, so they may be wrong... */
151 .sense = ILLEGAL_REQUEST,
154 .errno = EBADSLT, /* Invalid element address */
156 .sense = ILLEGAL_REQUEST,
159 .errno = EBADE, /* Import or export element accessed */
161 .sense = ILLEGAL_REQUEST,
164 .errno = EXFULL, /* Medium destination element full */
166 .sense = ILLEGAL_REQUEST,
169 .errno = EBADE, /* Medium source element empty */
171 .sense = ILLEGAL_REQUEST,
174 .errno = EBADRQC, /* Invalid command operation code */
180 /* ------------------------------------------------------------------- */
182 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
186 /* Check to see if additional sense information is available */
187 if (scsi_sense_valid(sshdr) &&
189 for (i = 0; err[i].errno != 0; i++) {
190 if (err[i].sense == sshdr->sense_key &&
191 err[i].asc == sshdr->asc &&
192 err[i].ascq == sshdr->ascq) {
193 errno = -err[i].errno;
204 ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
205 void *buffer, unsigned buflength,
206 enum dma_data_direction direction)
208 int errno, retries = 0, timeout, result;
209 struct scsi_sense_hdr sshdr;
211 timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
212 ? timeout_init : timeout_move;
217 dprintk("command: ");
218 __scsi_print_command(cmd);
221 result = scsi_execute_req(ch->device, cmd, direction, buffer,
222 buflength, &sshdr, timeout * HZ,
225 dprintk("result: 0x%x\n",result);
226 if (driver_byte(result) & DRIVER_SENSE) {
228 scsi_print_sense_hdr(ch->name, &sshdr);
229 errno = ch_find_errno(&sshdr);
231 switch(sshdr.sense_key) {
233 ch->unit_attention = 1;
242 /* ------------------------------------------------------------------------ */
245 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
249 for (i = 0; i < CH_TYPES; i++) {
250 if (elem >= ch->firsts[i] &&
251 elem < ch->firsts[i] +
259 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
265 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
270 memset(cmd,0,sizeof(cmd));
271 cmd[0] = READ_ELEMENT_STATUS;
272 cmd[1] = (ch->device->lun << 5) |
273 (ch->voltags ? 0x10 : 0) |
274 ch_elem_to_typecode(ch,elem);
275 cmd[2] = (elem >> 8) & 0xff;
276 cmd[3] = elem & 0xff;
279 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
280 if (((buffer[16] << 8) | buffer[17]) != elem) {
281 dprintk("asked for element 0x%02x, got 0x%02x\n",
282 elem,(buffer[16] << 8) | buffer[17]);
286 memcpy(data,buffer+16,16);
290 vprintk("device has no volume tag support\n");
293 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
300 ch_init_elem(scsi_changer *ch)
305 vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
306 memset(cmd,0,sizeof(cmd));
307 cmd[0] = INITIALIZE_ELEMENT_STATUS;
308 cmd[1] = ch->device->lun << 5;
309 err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
310 vprintk("... finished\n");
315 ch_readconfig(scsi_changer *ch)
317 u_char cmd[10], data[16];
322 buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
326 memset(cmd,0,sizeof(cmd));
328 cmd[1] = ch->device->lun << 5;
331 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
334 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
337 ch->firsts[CHET_MT] =
338 (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
339 ch->counts[CHET_MT] =
340 (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
341 ch->firsts[CHET_ST] =
342 (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
343 ch->counts[CHET_ST] =
344 (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
345 ch->firsts[CHET_IE] =
346 (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
347 ch->counts[CHET_IE] =
348 (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
349 ch->firsts[CHET_DT] =
350 (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
351 ch->counts[CHET_DT] =
352 (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
353 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
355 ch->counts[CHET_MT]);
356 vprintk("type #2 (st): 0x%x+%d [storage]\n",
358 ch->counts[CHET_ST]);
359 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
361 ch->counts[CHET_IE]);
362 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
364 ch->counts[CHET_DT]);
366 vprintk("reading element address assigment page failed!\n");
369 /* vendor specific element types */
370 for (i = 0; i < 4; i++) {
371 if (0 == vendor_counts[i])
373 if (NULL == vendor_labels[i])
375 ch->firsts[CHET_V1+i] = vendor_firsts[i];
376 ch->counts[CHET_V1+i] = vendor_counts[i];
377 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
378 i+5,i+1,vendor_firsts[i],vendor_counts[i],
382 /* look up the devices of the data transfer elements */
383 ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
385 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
388 if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
391 vprintk("dt 0x%x: [insmod option] ",
392 elem+ch->firsts[CHET_DT]);
393 } else if (0 != ch_read_element_status
394 (ch,elem+ch->firsts[CHET_DT],data)) {
395 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
396 elem+ch->firsts[CHET_DT]);
398 vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
399 if (data[6] & 0x80) {
401 printk("not this SCSI bus\n");
403 } else if (0 == (data[6] & 0x30)) {
405 printk("ID/LUN unknown\n");
410 if (data[6] & 0x20) id = data[7];
411 if (data[6] & 0x10) lun = data[6] & 7;
416 printk("ID %i, LUN %i, ",id,lun);
418 scsi_device_lookup(ch->device->host,
422 /* should not happen */
424 printk("Huh? device not found!\n");
427 printk("name: %8.8s %16.16s %4.4s\n",
428 ch->dt[elem]->vendor,
440 /* ------------------------------------------------------------------------ */
443 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
447 dprintk("position: 0x%x\n",elem);
449 trans = ch->firsts[CHET_MT];
450 memset(cmd,0,sizeof(cmd));
451 cmd[0] = POSITION_TO_ELEMENT;
452 cmd[1] = ch->device->lun << 5;
453 cmd[2] = (trans >> 8) & 0xff;
454 cmd[3] = trans & 0xff;
455 cmd[4] = (elem >> 8) & 0xff;
456 cmd[5] = elem & 0xff;
457 cmd[8] = rotate ? 1 : 0;
458 return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
462 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
466 dprintk("move: 0x%x => 0x%x\n",src,dest);
468 trans = ch->firsts[CHET_MT];
469 memset(cmd,0,sizeof(cmd));
470 cmd[0] = MOVE_MEDIUM;
471 cmd[1] = ch->device->lun << 5;
472 cmd[2] = (trans >> 8) & 0xff;
473 cmd[3] = trans & 0xff;
474 cmd[4] = (src >> 8) & 0xff;
476 cmd[6] = (dest >> 8) & 0xff;
477 cmd[7] = dest & 0xff;
478 cmd[10] = rotate ? 1 : 0;
479 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
483 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
484 u_int dest1, u_int dest2, int rotate1, int rotate2)
488 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
491 trans = ch->firsts[CHET_MT];
492 memset(cmd,0,sizeof(cmd));
493 cmd[0] = EXCHANGE_MEDIUM;
494 cmd[1] = ch->device->lun << 5;
495 cmd[2] = (trans >> 8) & 0xff;
496 cmd[3] = trans & 0xff;
497 cmd[4] = (src >> 8) & 0xff;
499 cmd[6] = (dest1 >> 8) & 0xff;
500 cmd[7] = dest1 & 0xff;
501 cmd[8] = (dest2 >> 8) & 0xff;
502 cmd[9] = dest2 & 0xff;
503 cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
505 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
509 ch_check_voltag(char *tag)
513 for (i = 0; i < 32; i++) {
514 /* restrict to ascii */
515 if (tag[i] >= 0x7f || tag[i] < 0x20)
517 /* don't allow search wildcards */
525 ch_set_voltag(scsi_changer *ch, u_int elem,
526 int alternate, int clear, u_char *tag)
532 buffer = kzalloc(512, GFP_KERNEL);
536 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
537 clear ? "clear" : "set",
538 alternate ? "alternate" : "primary",
540 memset(cmd,0,sizeof(cmd));
541 cmd[0] = SEND_VOLUME_TAG;
542 cmd[1] = (ch->device->lun << 5) |
543 ch_elem_to_typecode(ch,elem);
544 cmd[2] = (elem >> 8) & 0xff;
545 cmd[3] = elem & 0xff;
547 ? (alternate ? 0x0d : 0x0c)
548 : (alternate ? 0x0b : 0x0a);
552 memcpy(buffer,tag,32);
553 ch_check_voltag(buffer);
555 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
560 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
566 mutex_lock(&ch->lock);
567 for (i = 0; i < ch->counts[type]; i++) {
568 if (0 != ch_read_element_status
569 (ch, ch->firsts[type]+i,data)) {
573 put_user(data[2], dest+i);
574 if (data[2] & CESTATUS_EXCEPT)
575 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
577 (int)data[4],(int)data[5]);
578 retval = ch_read_element_status
579 (ch, ch->firsts[type]+i,data);
583 mutex_unlock(&ch->lock);
587 /* ------------------------------------------------------------------------ */
590 ch_release(struct inode *inode, struct file *file)
592 scsi_changer *ch = file->private_data;
594 scsi_device_put(ch->device);
595 file->private_data = NULL;
600 ch_open(struct inode *inode, struct file *file)
602 scsi_changer *tmp, *ch;
603 int minor = iminor(inode);
605 spin_lock(&ch_devlist_lock);
607 list_for_each_entry(tmp,&ch_devlist,list) {
608 if (tmp->minor == minor)
611 if (NULL == ch || scsi_device_get(ch->device)) {
612 spin_unlock(&ch_devlist_lock);
615 spin_unlock(&ch_devlist_lock);
617 file->private_data = ch;
622 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
624 if (type >= CH_TYPES || unit >= ch->counts[type])
629 static int ch_ioctl(struct inode * inode, struct file * file,
630 unsigned int cmd, unsigned long arg)
632 scsi_changer *ch = file->private_data;
634 void __user *argp = (void __user *)arg;
639 struct changer_params params;
641 params.cp_curpicker = 0;
642 params.cp_npickers = ch->counts[CHET_MT];
643 params.cp_nslots = ch->counts[CHET_ST];
644 params.cp_nportals = ch->counts[CHET_IE];
645 params.cp_ndrives = ch->counts[CHET_DT];
647 if (copy_to_user(argp, ¶ms, sizeof(params)))
653 struct changer_vendor_params vparams;
655 memset(&vparams,0,sizeof(vparams));
656 if (ch->counts[CHET_V1]) {
657 vparams.cvp_n1 = ch->counts[CHET_V1];
658 strncpy(vparams.cvp_label1,vendor_labels[0],16);
660 if (ch->counts[CHET_V2]) {
661 vparams.cvp_n2 = ch->counts[CHET_V2];
662 strncpy(vparams.cvp_label2,vendor_labels[1],16);
664 if (ch->counts[CHET_V3]) {
665 vparams.cvp_n3 = ch->counts[CHET_V3];
666 strncpy(vparams.cvp_label3,vendor_labels[2],16);
668 if (ch->counts[CHET_V4]) {
669 vparams.cvp_n4 = ch->counts[CHET_V4];
670 strncpy(vparams.cvp_label4,vendor_labels[3],16);
672 if (copy_to_user(argp, &vparams, sizeof(vparams)))
679 struct changer_position pos;
681 if (copy_from_user(&pos, argp, sizeof (pos)))
684 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
685 dprintk("CHIOPOSITION: invalid parameter\n");
688 mutex_lock(&ch->lock);
689 retval = ch_position(ch,0,
690 ch->firsts[pos.cp_type] + pos.cp_unit,
691 pos.cp_flags & CP_INVERT);
692 mutex_unlock(&ch->lock);
698 struct changer_move mv;
700 if (copy_from_user(&mv, argp, sizeof (mv)))
703 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
704 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
705 dprintk("CHIOMOVE: invalid parameter\n");
709 mutex_lock(&ch->lock);
710 retval = ch_move(ch,0,
711 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
712 ch->firsts[mv.cm_totype] + mv.cm_tounit,
713 mv.cm_flags & CM_INVERT);
714 mutex_unlock(&ch->lock);
720 struct changer_exchange mv;
722 if (copy_from_user(&mv, argp, sizeof (mv)))
725 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
726 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
727 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
728 dprintk("CHIOEXCHANGE: invalid parameter\n");
732 mutex_lock(&ch->lock);
735 ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
736 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
737 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
738 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
739 mutex_unlock(&ch->lock);
745 struct changer_element_status ces;
747 if (copy_from_user(&ces, argp, sizeof (ces)))
749 if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
752 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
757 struct changer_get_element cge;
763 if (copy_from_user(&cge, argp, sizeof (cge)))
766 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
768 elem = ch->firsts[cge.cge_type] + cge.cge_unit;
770 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
773 mutex_lock(&ch->lock);
776 memset(cmd,0,sizeof(cmd));
777 cmd[0] = READ_ELEMENT_STATUS;
778 cmd[1] = (ch->device->lun << 5) |
779 (ch->voltags ? 0x10 : 0) |
780 ch_elem_to_typecode(ch,elem);
781 cmd[2] = (elem >> 8) & 0xff;
782 cmd[3] = elem & 0xff;
786 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
787 cge.cge_status = buffer[18];
789 if (buffer[18] & CESTATUS_EXCEPT) {
792 if (buffer[25] & 0x80) {
793 cge.cge_flags |= CGE_SRC;
794 if (buffer[25] & 0x40)
795 cge.cge_flags |= CGE_INVERT;
796 elem = (buffer[26]<<8) | buffer[27];
797 for (i = 0; i < 4; i++) {
798 if (elem >= ch->firsts[i] &&
799 elem < ch->firsts[i] + ch->counts[i]) {
801 cge.cge_srcunit = elem-ch->firsts[i];
805 if ((buffer[22] & 0x30) == 0x30) {
806 cge.cge_flags |= CGE_IDLUN;
807 cge.cge_id = buffer[23];
808 cge.cge_lun = buffer[22] & 7;
810 if (buffer[9] & 0x80) {
811 cge.cge_flags |= CGE_PVOLTAG;
812 memcpy(cge.cge_pvoltag,buffer+28,36);
814 if (buffer[9] & 0x40) {
815 cge.cge_flags |= CGE_AVOLTAG;
816 memcpy(cge.cge_avoltag,buffer+64,36);
818 } else if (ch->voltags) {
820 vprintk("device has no volume tag support\n");
824 mutex_unlock(&ch->lock);
826 if (copy_to_user(argp, &cge, sizeof (cge)))
833 mutex_lock(&ch->lock);
834 retval = ch_init_elem(ch);
835 mutex_unlock(&ch->lock);
841 struct changer_set_voltag csv;
844 if (copy_from_user(&csv, argp, sizeof(csv)))
847 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
848 dprintk("CHIOSVOLTAG: invalid parameter\n");
851 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
852 mutex_lock(&ch->lock);
853 retval = ch_set_voltag(ch, elem,
854 csv.csv_flags & CSV_AVOLTAG,
855 csv.csv_flags & CSV_CLEARTAG,
857 mutex_unlock(&ch->lock);
862 return scsi_ioctl(ch->device, cmd, argp);
869 struct changer_element_status32 {
871 compat_uptr_t ces_data;
873 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
875 static long ch_ioctl_compat(struct file * file,
876 unsigned int cmd, unsigned long arg)
878 scsi_changer *ch = file->private_data;
890 return ch_ioctl(NULL /* inode, unused */,
894 struct changer_element_status32 ces32;
895 unsigned char __user *data;
897 if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
899 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
902 data = compat_ptr(ces32.ces_data);
903 return ch_gstatus(ch, ces32.ces_type, data);
906 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
913 /* ------------------------------------------------------------------------ */
915 static int ch_probe(struct device *dev)
917 struct scsi_device *sd = to_scsi_device(dev);
920 if (sd->type != TYPE_MEDIUM_CHANGER)
923 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
927 ch->minor = ch_devcount;
928 sprintf(ch->name,"ch%d",ch->minor);
929 mutex_init(&ch->lock);
935 class_device_create(ch_sysfs_class, NULL,
936 MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
937 dev, "s%s", ch->name);
939 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
941 spin_lock(&ch_devlist_lock);
942 list_add_tail(&ch->list,&ch_devlist);
944 spin_unlock(&ch_devlist_lock);
948 static int ch_remove(struct device *dev)
950 struct scsi_device *sd = to_scsi_device(dev);
951 scsi_changer *tmp, *ch;
953 spin_lock(&ch_devlist_lock);
955 list_for_each_entry(tmp,&ch_devlist,list) {
956 if (tmp->device == sd)
961 spin_unlock(&ch_devlist_lock);
963 class_device_destroy(ch_sysfs_class,
964 MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
971 static int __init init_ch_module(void)
975 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
976 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
977 if (IS_ERR(ch_sysfs_class)) {
978 rc = PTR_ERR(ch_sysfs_class);
981 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
983 printk("Unable to get major %d for SCSI-Changer\n",
987 rc = scsi_register_driver(&ch_template.gendrv);
993 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
995 class_destroy(ch_sysfs_class);
999 static void __exit exit_ch_module(void)
1001 scsi_unregister_driver(&ch_template.gendrv);
1002 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1003 class_destroy(ch_sysfs_class);
1006 module_init(init_ch_module);
1007 module_exit(exit_ch_module);