2 * SCSI Media Changer device driver for Linux 2.6
4 * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
17 #include <linux/major.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/blkdev.h>
22 #include <linux/completion.h>
23 #include <linux/devfs_fs_kernel.h>
24 #include <linux/ioctl32.h>
25 #include <linux/compat.h>
26 #include <linux/chio.h> /* here are all the ioctls */
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_request.h>
35 #include <scsi/scsi_dbg.h>
40 MODULE_DESCRIPTION("device driver for scsi media changer devices");
41 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
42 MODULE_LICENSE("GPL");
45 module_param(init, int, 0444);
46 MODULE_PARM_DESC(init, \
47 "initialize element status on driver load (default: on)");
49 static int timeout_move = 300;
50 module_param(timeout_move, int, 0644);
51 MODULE_PARM_DESC(timeout_move,"timeout for move commands "
52 "(default: 300 seconds)");
54 static int timeout_init = 3600;
55 module_param(timeout_init, int, 0644);
56 MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
57 "(default: 3600 seconds)");
59 static int verbose = 1;
60 module_param(verbose, int, 0644);
61 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
64 module_param(debug, int, 0644);
65 MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
66 "detailed sense codes on scsi errors (default: off)");
68 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
69 static int dt_lun[CH_DT_MAX];
70 module_param_array(dt_id, int, NULL, 0444);
71 module_param_array(dt_lun, int, NULL, 0444);
73 /* tell the driver about vendor-specific slots */
74 static int vendor_firsts[CH_TYPES-4];
75 static int vendor_counts[CH_TYPES-4];
76 module_param_array(vendor_firsts, int, NULL, 0444);
77 module_param_array(vendor_counts, int, NULL, 0444);
79 static char *vendor_labels[CH_TYPES-4] = {
80 "v0", "v1", "v2", "v3"
82 // module_param_string_array(vendor_labels, NULL, 0444);
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)
89 /* ------------------------------------------------------------------- */
93 static int ch_probe(struct device *);
94 static int ch_remove(struct device *);
95 static int ch_open(struct inode * inode, struct file * filp);
96 static int ch_release(struct inode * inode, struct file * filp);
97 static int ch_ioctl(struct inode * inode, struct file * filp,
98 unsigned int cmd, unsigned long arg);
100 static long ch_ioctl_compat(struct file * filp,
101 unsigned int cmd, unsigned long arg);
104 static struct class * ch_sysfs_class;
107 struct list_head list;
110 struct scsi_device *device;
111 struct scsi_device **dt; /* ptrs to data transfer elements */
112 u_int firsts[CH_TYPES];
113 u_int counts[CH_TYPES];
114 u_int unit_attention;
116 struct semaphore lock;
119 static LIST_HEAD(ch_devlist);
120 static spinlock_t ch_devlist_lock = SPIN_LOCK_UNLOCKED;
121 static int ch_devcount;
123 static struct scsi_driver ch_template =
125 .owner = THIS_MODULE,
133 static struct file_operations changer_fops =
135 .owner = THIS_MODULE,
137 .release = ch_release,
140 .compat_ioctl = ch_ioctl_compat,
150 /* Just filled in what looks right. Hav'nt checked any standard paper for
151 these errno assignments, so they may be wrong... */
153 .sense = ILLEGAL_REQUEST,
156 .errno = EBADSLT, /* Invalid element address */
158 .sense = ILLEGAL_REQUEST,
161 .errno = EBADE, /* Import or export element accessed */
163 .sense = ILLEGAL_REQUEST,
166 .errno = EXFULL, /* Medium destination element full */
168 .sense = ILLEGAL_REQUEST,
171 .errno = EBADE, /* Medium source element empty */
173 .sense = ILLEGAL_REQUEST,
176 .errno = EBADRQC, /* Invalid command operation code */
182 /* ------------------------------------------------------------------- */
184 static int ch_find_errno(unsigned char *sense_buffer)
188 /* Check to see if additional sense information is available */
189 if (sense_buffer[7] > 5 &&
190 sense_buffer[12] != 0) {
191 for (i = 0; err[i].errno != 0; i++) {
192 if (err[i].sense == sense_buffer[ 2] &&
193 err[i].asc == sense_buffer[12] &&
194 err[i].ascq == sense_buffer[13]) {
195 errno = -err[i].errno;
206 ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
207 void *buffer, unsigned buflength,
208 enum dma_data_direction direction)
210 int errno, retries = 0, timeout;
211 struct scsi_request *sr;
213 sr = scsi_allocate_request(ch->device, GFP_KERNEL);
217 timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
218 ? timeout_init : timeout_move;
223 dprintk("command: ");
224 __scsi_print_command(cmd);
227 scsi_wait_req(sr, cmd, buffer, buflength,
228 timeout * HZ, MAX_RETRIES);
230 dprintk("result: 0x%x\n",sr->sr_result);
231 if (driver_byte(sr->sr_result) & DRIVER_SENSE) {
233 scsi_print_req_sense(ch->name, sr);
234 errno = ch_find_errno(sr->sr_sense_buffer);
236 switch(sr->sr_sense_buffer[2] & 0xf) {
238 ch->unit_attention = 1;
244 scsi_release_request(sr);
248 /* ------------------------------------------------------------------------ */
251 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
255 for (i = 0; i < CH_TYPES; i++) {
256 if (elem >= ch->firsts[i] &&
257 elem < ch->firsts[i] +
265 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
271 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
276 memset(cmd,0,sizeof(cmd));
277 cmd[0] = READ_ELEMENT_STATUS;
278 cmd[1] = (ch->device->lun << 5) |
279 (ch->voltags ? 0x10 : 0) |
280 ch_elem_to_typecode(ch,elem);
281 cmd[2] = (elem >> 8) & 0xff;
282 cmd[3] = elem & 0xff;
285 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
286 if (((buffer[16] << 8) | buffer[17]) != elem) {
287 dprintk("asked for element 0x%02x, got 0x%02x\n",
288 elem,(buffer[16] << 8) | buffer[17]);
292 memcpy(data,buffer+16,16);
296 vprintk("device has no volume tag support\n");
299 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
306 ch_init_elem(scsi_changer *ch)
311 vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
312 memset(cmd,0,sizeof(cmd));
313 cmd[0] = INITIALIZE_ELEMENT_STATUS;
314 cmd[1] = ch->device->lun << 5;
315 err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
316 vprintk("... finished\n");
321 ch_readconfig(scsi_changer *ch)
323 u_char cmd[10], data[16];
328 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
331 memset(buffer,0,512);
333 memset(cmd,0,sizeof(cmd));
335 cmd[1] = ch->device->lun << 5;
338 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
341 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
344 ch->firsts[CHET_MT] =
345 (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
346 ch->counts[CHET_MT] =
347 (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
348 ch->firsts[CHET_ST] =
349 (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
350 ch->counts[CHET_ST] =
351 (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
352 ch->firsts[CHET_IE] =
353 (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
354 ch->counts[CHET_IE] =
355 (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
356 ch->firsts[CHET_DT] =
357 (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
358 ch->counts[CHET_DT] =
359 (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
360 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
362 ch->counts[CHET_MT]);
363 vprintk("type #2 (st): 0x%x+%d [storage]\n",
365 ch->counts[CHET_ST]);
366 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
368 ch->counts[CHET_IE]);
369 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
371 ch->counts[CHET_DT]);
373 vprintk("reading element address assigment page failed!\n");
376 /* vendor specific element types */
377 for (i = 0; i < 4; i++) {
378 if (0 == vendor_counts[i])
380 if (NULL == vendor_labels[i])
382 ch->firsts[CHET_V1+i] = vendor_firsts[i];
383 ch->counts[CHET_V1+i] = vendor_counts[i];
384 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
385 i+5,i+1,vendor_firsts[i],vendor_counts[i],
389 /* look up the devices of the data transfer elements */
390 ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
392 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
395 if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
398 vprintk("dt 0x%x: [insmod option] ",
399 elem+ch->firsts[CHET_DT]);
400 } else if (0 != ch_read_element_status
401 (ch,elem+ch->firsts[CHET_DT],data)) {
402 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
403 elem+ch->firsts[CHET_DT]);
405 vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
406 if (data[6] & 0x80) {
408 printk("not this SCSI bus\n");
410 } else if (0 == (data[6] & 0x30)) {
412 printk("ID/LUN unknown\n");
417 if (data[6] & 0x20) id = data[7];
418 if (data[6] & 0x10) lun = data[6] & 7;
423 printk("ID %i, LUN %i, ",id,lun);
425 scsi_device_lookup(ch->device->host,
429 /* should not happen */
431 printk("Huh? device not found!\n");
434 printk("name: %8.8s %16.16s %4.4s\n",
435 ch->dt[elem]->vendor,
447 /* ------------------------------------------------------------------------ */
450 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
454 dprintk("position: 0x%x\n",elem);
456 trans = ch->firsts[CHET_MT];
457 memset(cmd,0,sizeof(cmd));
458 cmd[0] = POSITION_TO_ELEMENT;
459 cmd[1] = ch->device->lun << 5;
460 cmd[2] = (trans >> 8) & 0xff;
461 cmd[3] = trans & 0xff;
462 cmd[4] = (elem >> 8) & 0xff;
463 cmd[5] = elem & 0xff;
464 cmd[8] = rotate ? 1 : 0;
465 return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
469 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
473 dprintk("move: 0x%x => 0x%x\n",src,dest);
475 trans = ch->firsts[CHET_MT];
476 memset(cmd,0,sizeof(cmd));
477 cmd[0] = MOVE_MEDIUM;
478 cmd[1] = ch->device->lun << 5;
479 cmd[2] = (trans >> 8) & 0xff;
480 cmd[3] = trans & 0xff;
481 cmd[4] = (src >> 8) & 0xff;
483 cmd[6] = (dest >> 8) & 0xff;
484 cmd[7] = dest & 0xff;
485 cmd[10] = rotate ? 1 : 0;
486 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
490 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
491 u_int dest1, u_int dest2, int rotate1, int rotate2)
495 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
498 trans = ch->firsts[CHET_MT];
499 memset(cmd,0,sizeof(cmd));
500 cmd[0] = EXCHANGE_MEDIUM;
501 cmd[1] = ch->device->lun << 5;
502 cmd[2] = (trans >> 8) & 0xff;
503 cmd[3] = trans & 0xff;
504 cmd[4] = (src >> 8) & 0xff;
506 cmd[6] = (dest1 >> 8) & 0xff;
507 cmd[7] = dest1 & 0xff;
508 cmd[8] = (dest2 >> 8) & 0xff;
509 cmd[9] = dest2 & 0xff;
510 cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
512 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
516 ch_check_voltag(char *tag)
520 for (i = 0; i < 32; i++) {
521 /* restrict to ascii */
522 if (tag[i] >= 0x7f || tag[i] < 0x20)
524 /* don't allow search wildcards */
532 ch_set_voltag(scsi_changer *ch, u_int elem,
533 int alternate, int clear, u_char *tag)
539 buffer = kmalloc(512, GFP_KERNEL);
542 memset(buffer,0,512);
544 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
545 clear ? "clear" : "set",
546 alternate ? "alternate" : "primary",
548 memset(cmd,0,sizeof(cmd));
549 cmd[0] = SEND_VOLUME_TAG;
550 cmd[1] = (ch->device->lun << 5) |
551 ch_elem_to_typecode(ch,elem);
552 cmd[2] = (elem >> 8) & 0xff;
553 cmd[3] = elem & 0xff;
555 ? (alternate ? 0x0d : 0x0c)
556 : (alternate ? 0x0b : 0x0a);
560 memcpy(buffer,tag,32);
561 ch_check_voltag(buffer);
563 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
568 static int ch_gstatus(scsi_changer *ch, int type, unsigned char *dest)
575 for (i = 0; i < ch->counts[type]; i++) {
576 if (0 != ch_read_element_status
577 (ch, ch->firsts[type]+i,data)) {
581 put_user(data[2], dest+i);
582 if (data[2] & CESTATUS_EXCEPT)
583 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
585 (int)data[4],(int)data[5]);
586 retval = ch_read_element_status
587 (ch, ch->firsts[type]+i,data);
595 /* ------------------------------------------------------------------------ */
598 ch_release(struct inode *inode, struct file *file)
600 scsi_changer *ch = file->private_data;
602 scsi_device_put(ch->device);
603 file->private_data = NULL;
608 ch_open(struct inode *inode, struct file *file)
610 scsi_changer *tmp, *ch;
611 int minor = iminor(inode);
613 spin_lock(&ch_devlist_lock);
615 list_for_each_entry(tmp,&ch_devlist,list) {
616 if (tmp->minor == minor)
619 if (NULL == ch || scsi_device_get(ch->device)) {
620 spin_unlock(&ch_devlist_lock);
623 spin_unlock(&ch_devlist_lock);
625 file->private_data = ch;
630 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
632 if (type >= CH_TYPES || unit >= ch->counts[type])
637 static int ch_ioctl(struct inode * inode, struct file * file,
638 unsigned int cmd, unsigned long arg)
640 scsi_changer *ch = file->private_data;
646 struct changer_params params;
648 params.cp_curpicker = 0;
649 params.cp_npickers = ch->counts[CHET_MT];
650 params.cp_nslots = ch->counts[CHET_ST];
651 params.cp_nportals = ch->counts[CHET_IE];
652 params.cp_ndrives = ch->counts[CHET_DT];
654 if (copy_to_user((void *) arg, ¶ms, sizeof(params)))
660 struct changer_vendor_params vparams;
662 memset(&vparams,0,sizeof(vparams));
663 if (ch->counts[CHET_V1]) {
664 vparams.cvp_n1 = ch->counts[CHET_V1];
665 strncpy(vparams.cvp_label1,vendor_labels[0],16);
667 if (ch->counts[CHET_V2]) {
668 vparams.cvp_n2 = ch->counts[CHET_V2];
669 strncpy(vparams.cvp_label2,vendor_labels[1],16);
671 if (ch->counts[CHET_V3]) {
672 vparams.cvp_n3 = ch->counts[CHET_V3];
673 strncpy(vparams.cvp_label3,vendor_labels[2],16);
675 if (ch->counts[CHET_V4]) {
676 vparams.cvp_n4 = ch->counts[CHET_V4];
677 strncpy(vparams.cvp_label4,vendor_labels[3],16);
679 if (copy_to_user((void *) arg, &vparams, sizeof(vparams)))
686 struct changer_position pos;
688 if (copy_from_user(&pos, (void*)arg, sizeof (pos)))
691 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
692 dprintk("CHIOPOSITION: invalid parameter\n");
696 retval = ch_position(ch,0,
697 ch->firsts[pos.cp_type] + pos.cp_unit,
698 pos.cp_flags & CP_INVERT);
705 struct changer_move mv;
707 if (copy_from_user(&mv, (void*)arg, sizeof (mv)))
710 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
711 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
712 dprintk("CHIOMOVE: invalid parameter\n");
717 retval = ch_move(ch,0,
718 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
719 ch->firsts[mv.cm_totype] + mv.cm_tounit,
720 mv.cm_flags & CM_INVERT);
727 struct changer_exchange mv;
729 if (copy_from_user(&mv, (void*)arg, sizeof (mv)))
732 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
733 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
734 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
735 dprintk("CHIOEXCHANGE: invalid parameter\n");
742 ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
743 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
744 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
745 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
752 struct changer_element_status ces;
754 if (copy_from_user(&ces, (void*)arg, sizeof (ces)))
756 if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
759 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
764 struct changer_get_element cge;
770 if (copy_from_user(&cge, (void*)arg, sizeof (cge)))
773 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
775 elem = ch->firsts[cge.cge_type] + cge.cge_unit;
777 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
783 memset(cmd,0,sizeof(cmd));
784 cmd[0] = READ_ELEMENT_STATUS;
785 cmd[1] = (ch->device->lun << 5) |
786 (ch->voltags ? 0x10 : 0) |
787 ch_elem_to_typecode(ch,elem);
788 cmd[2] = (elem >> 8) & 0xff;
789 cmd[3] = elem & 0xff;
793 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
794 cge.cge_status = buffer[18];
796 if (buffer[18] & CESTATUS_EXCEPT) {
799 if (buffer[25] & 0x80) {
800 cge.cge_flags |= CGE_SRC;
801 if (buffer[25] & 0x40)
802 cge.cge_flags |= CGE_INVERT;
803 elem = (buffer[26]<<8) | buffer[27];
804 for (i = 0; i < 4; i++) {
805 if (elem >= ch->firsts[i] &&
806 elem < ch->firsts[i] + ch->counts[i]) {
808 cge.cge_srcunit = elem-ch->firsts[i];
812 if ((buffer[22] & 0x30) == 0x30) {
813 cge.cge_flags |= CGE_IDLUN;
814 cge.cge_id = buffer[23];
815 cge.cge_lun = buffer[22] & 7;
817 if (buffer[9] & 0x80) {
818 cge.cge_flags |= CGE_PVOLTAG;
819 memcpy(cge.cge_pvoltag,buffer+28,36);
821 if (buffer[9] & 0x40) {
822 cge.cge_flags |= CGE_AVOLTAG;
823 memcpy(cge.cge_avoltag,buffer+64,36);
825 } else if (ch->voltags) {
827 vprintk("device has no volume tag support\n");
833 if (copy_to_user((void*)arg, &cge, sizeof (cge)))
841 retval = ch_init_elem(ch);
848 struct changer_set_voltag csv;
851 if (copy_from_user(&csv, (void*)arg, sizeof(csv)))
854 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
855 dprintk("CHIOSVOLTAG: invalid parameter\n");
858 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
860 retval = ch_set_voltag(ch, elem,
861 csv.csv_flags & CSV_AVOLTAG,
862 csv.csv_flags & CSV_CLEARTAG,
869 return scsi_ioctl(ch->device, cmd, (void*)arg);
876 struct changer_element_status32 {
878 compat_uptr_t ces_data;
880 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
882 static long ch_ioctl_compat(struct file * file,
883 unsigned int cmd, unsigned long arg)
885 scsi_changer *ch = file->private_data;
897 return ch_ioctl(NULL /* inode, unused */,
901 struct changer_element_status32 ces32;
904 if (copy_from_user(&ces32, (void*)arg, sizeof (ces32)))
906 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
909 data = compat_ptr(ces32.ces_data);
910 return ch_gstatus(ch, ces32.ces_type, data);
913 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
920 /* ------------------------------------------------------------------------ */
922 static int ch_probe(struct device *dev)
924 struct scsi_device *sd = to_scsi_device(dev);
927 if (sd->type != TYPE_MEDIUM_CHANGER)
930 ch = kmalloc(sizeof(*ch), GFP_KERNEL);
934 memset(ch,0,sizeof(*ch));
935 ch->minor = ch_devcount;
936 sprintf(ch->name,"ch%d",ch->minor);
937 init_MUTEX(&ch->lock);
943 devfs_mk_cdev(MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
944 S_IFCHR | S_IRUGO | S_IWUGO, ch->name);
945 class_device_create(ch_sysfs_class,
946 MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
947 dev, "s%s", ch->name);
949 printk(KERN_INFO "Attached scsi changer %s "
950 "at scsi%d, channel %d, id %d, lun %d\n",
951 ch->name, sd->host->host_no, sd->channel, sd->id, sd->lun);
953 spin_lock(&ch_devlist_lock);
954 list_add_tail(&ch->list,&ch_devlist);
956 spin_unlock(&ch_devlist_lock);
960 static int ch_remove(struct device *dev)
962 struct scsi_device *sd = to_scsi_device(dev);
963 scsi_changer *tmp, *ch;
965 spin_lock(&ch_devlist_lock);
967 list_for_each_entry(tmp,&ch_devlist,list) {
968 if (tmp->device == sd)
973 spin_unlock(&ch_devlist_lock);
975 class_device_destroy(ch_sysfs_class,
976 MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
977 devfs_remove(ch->name);
984 static int __init init_ch_module(void)
988 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
989 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
990 if (IS_ERR(ch_sysfs_class)) {
991 rc = PTR_ERR(ch_sysfs_class);
994 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
996 printk("Unable to get major %d for SCSI-Changer\n",
1000 rc = scsi_register_driver(&ch_template.gendrv);
1006 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1008 class_destroy(ch_sysfs_class);
1012 static void __exit exit_ch_module(void)
1014 scsi_unregister_driver(&ch_template.gendrv);
1015 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1016 class_destroy(ch_sysfs_class);
1019 module_init(init_ch_module);
1020 module_exit(exit_ch_module);