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>
24 #include <linux/idr.h>
25 #include <linux/smp_lock.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_driver.h>
30 #include <scsi/scsi_ioctl.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_dbg.h>
38 #define CH_MAX_DEVS 128
40 MODULE_DESCRIPTION("device driver for scsi media changer devices");
41 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
42 MODULE_LICENSE("GPL");
43 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR
);
46 module_param(init
, int, 0444);
47 MODULE_PARM_DESC(init
, \
48 "initialize element status on driver load (default: on)");
50 static int timeout_move
= 300;
51 module_param(timeout_move
, int, 0644);
52 MODULE_PARM_DESC(timeout_move
,"timeout for move commands "
53 "(default: 300 seconds)");
55 static int timeout_init
= 3600;
56 module_param(timeout_init
, int, 0644);
57 MODULE_PARM_DESC(timeout_init
,"timeout for INITIALIZE ELEMENT STATUS "
58 "(default: 3600 seconds)");
60 static int verbose
= 1;
61 module_param(verbose
, int, 0644);
62 MODULE_PARM_DESC(verbose
,"be verbose (default: on)");
65 module_param(debug
, int, 0644);
66 MODULE_PARM_DESC(debug
,"enable/disable debug messages, also prints more "
67 "detailed sense codes on scsi errors (default: off)");
69 static int dt_id
[CH_DT_MAX
] = { [ 0 ... (CH_DT_MAX
-1) ] = -1 };
70 static int dt_lun
[CH_DT_MAX
];
71 module_param_array(dt_id
, int, NULL
, 0444);
72 module_param_array(dt_lun
, int, NULL
, 0444);
74 /* tell the driver about vendor-specific slots */
75 static int vendor_firsts
[CH_TYPES
-4];
76 static int vendor_counts
[CH_TYPES
-4];
77 module_param_array(vendor_firsts
, int, NULL
, 0444);
78 module_param_array(vendor_counts
, int, NULL
, 0444);
80 static const char * vendor_labels
[CH_TYPES
-4] = {
81 "v0", "v1", "v2", "v3"
83 // module_param_string_array(vendor_labels, NULL, 0444);
85 #define dprintk(fmt, arg...) if (debug) \
86 printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg)
87 #define vprintk(fmt, arg...) if (verbose) \
88 printk(KERN_INFO "%s: " fmt, ch->name , ## arg)
90 /* ------------------------------------------------------------------- */
94 static struct class * ch_sysfs_class
;
97 struct list_head list
;
100 struct scsi_device
*device
;
101 struct scsi_device
**dt
; /* ptrs to data transfer elements */
102 u_int firsts
[CH_TYPES
];
103 u_int counts
[CH_TYPES
];
104 u_int unit_attention
;
109 static DEFINE_IDR(ch_index_idr
);
110 static DEFINE_SPINLOCK(ch_index_lock
);
112 static const struct {
118 /* Just filled in what looks right. Hav'nt checked any standard paper for
119 these errno assignments, so they may be wrong... */
121 .sense
= ILLEGAL_REQUEST
,
124 .errno
= EBADSLT
, /* Invalid element address */
126 .sense
= ILLEGAL_REQUEST
,
129 .errno
= EBADE
, /* Import or export element accessed */
131 .sense
= ILLEGAL_REQUEST
,
134 .errno
= EXFULL
, /* Medium destination element full */
136 .sense
= ILLEGAL_REQUEST
,
139 .errno
= EBADE
, /* Medium source element empty */
141 .sense
= ILLEGAL_REQUEST
,
144 .errno
= EBADRQC
, /* Invalid command operation code */
150 /* ------------------------------------------------------------------- */
152 static int ch_find_errno(struct scsi_sense_hdr
*sshdr
)
156 /* Check to see if additional sense information is available */
157 if (scsi_sense_valid(sshdr
) &&
159 for (i
= 0; ch_err
[i
].errno
!= 0; i
++) {
160 if (ch_err
[i
].sense
== sshdr
->sense_key
&&
161 ch_err
[i
].asc
== sshdr
->asc
&&
162 ch_err
[i
].ascq
== sshdr
->ascq
) {
163 errno
= -ch_err
[i
].errno
;
174 ch_do_scsi(scsi_changer
*ch
, unsigned char *cmd
,
175 void *buffer
, unsigned buflength
,
176 enum dma_data_direction direction
)
178 int errno
, retries
= 0, timeout
, result
;
179 struct scsi_sense_hdr sshdr
;
181 timeout
= (cmd
[0] == INITIALIZE_ELEMENT_STATUS
)
182 ? timeout_init
: timeout_move
;
187 dprintk("command: ");
188 __scsi_print_command(cmd
);
191 result
= scsi_execute_req(ch
->device
, cmd
, direction
, buffer
,
192 buflength
, &sshdr
, timeout
* HZ
,
195 dprintk("result: 0x%x\n",result
);
196 if (driver_byte(result
) & DRIVER_SENSE
) {
198 scsi_print_sense_hdr(ch
->name
, &sshdr
);
199 errno
= ch_find_errno(&sshdr
);
201 switch(sshdr
.sense_key
) {
203 ch
->unit_attention
= 1;
212 /* ------------------------------------------------------------------------ */
215 ch_elem_to_typecode(scsi_changer
*ch
, u_int elem
)
219 for (i
= 0; i
< CH_TYPES
; i
++) {
220 if (elem
>= ch
->firsts
[i
] &&
221 elem
< ch
->firsts
[i
] +
229 ch_read_element_status(scsi_changer
*ch
, u_int elem
, char *data
)
235 buffer
= kmalloc(512, GFP_KERNEL
| GFP_DMA
);
240 memset(cmd
,0,sizeof(cmd
));
241 cmd
[0] = READ_ELEMENT_STATUS
;
242 cmd
[1] = (ch
->device
->lun
<< 5) |
243 (ch
->voltags
? 0x10 : 0) |
244 ch_elem_to_typecode(ch
,elem
);
245 cmd
[2] = (elem
>> 8) & 0xff;
246 cmd
[3] = elem
& 0xff;
249 if (0 == (result
= ch_do_scsi(ch
, cmd
, buffer
, 256, DMA_FROM_DEVICE
))) {
250 if (((buffer
[16] << 8) | buffer
[17]) != elem
) {
251 dprintk("asked for element 0x%02x, got 0x%02x\n",
252 elem
,(buffer
[16] << 8) | buffer
[17]);
256 memcpy(data
,buffer
+16,16);
260 vprintk("device has no volume tag support\n");
263 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem
);
270 ch_init_elem(scsi_changer
*ch
)
275 vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
276 memset(cmd
,0,sizeof(cmd
));
277 cmd
[0] = INITIALIZE_ELEMENT_STATUS
;
278 cmd
[1] = ch
->device
->lun
<< 5;
279 err
= ch_do_scsi(ch
, cmd
, NULL
, 0, DMA_NONE
);
280 vprintk("... finished\n");
285 ch_readconfig(scsi_changer
*ch
)
287 u_char cmd
[10], data
[16];
292 buffer
= kzalloc(512, GFP_KERNEL
| GFP_DMA
);
296 memset(cmd
,0,sizeof(cmd
));
298 cmd
[1] = ch
->device
->lun
<< 5;
301 result
= ch_do_scsi(ch
, cmd
, buffer
, 255, DMA_FROM_DEVICE
);
304 result
= ch_do_scsi(ch
, cmd
, buffer
, 255, DMA_FROM_DEVICE
);
307 ch
->firsts
[CHET_MT
] =
308 (buffer
[buffer
[3]+ 6] << 8) | buffer
[buffer
[3]+ 7];
309 ch
->counts
[CHET_MT
] =
310 (buffer
[buffer
[3]+ 8] << 8) | buffer
[buffer
[3]+ 9];
311 ch
->firsts
[CHET_ST
] =
312 (buffer
[buffer
[3]+10] << 8) | buffer
[buffer
[3]+11];
313 ch
->counts
[CHET_ST
] =
314 (buffer
[buffer
[3]+12] << 8) | buffer
[buffer
[3]+13];
315 ch
->firsts
[CHET_IE
] =
316 (buffer
[buffer
[3]+14] << 8) | buffer
[buffer
[3]+15];
317 ch
->counts
[CHET_IE
] =
318 (buffer
[buffer
[3]+16] << 8) | buffer
[buffer
[3]+17];
319 ch
->firsts
[CHET_DT
] =
320 (buffer
[buffer
[3]+18] << 8) | buffer
[buffer
[3]+19];
321 ch
->counts
[CHET_DT
] =
322 (buffer
[buffer
[3]+20] << 8) | buffer
[buffer
[3]+21];
323 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
325 ch
->counts
[CHET_MT
]);
326 vprintk("type #2 (st): 0x%x+%d [storage]\n",
328 ch
->counts
[CHET_ST
]);
329 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
331 ch
->counts
[CHET_IE
]);
332 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
334 ch
->counts
[CHET_DT
]);
336 vprintk("reading element address assigment page failed!\n");
339 /* vendor specific element types */
340 for (i
= 0; i
< 4; i
++) {
341 if (0 == vendor_counts
[i
])
343 if (NULL
== vendor_labels
[i
])
345 ch
->firsts
[CHET_V1
+i
] = vendor_firsts
[i
];
346 ch
->counts
[CHET_V1
+i
] = vendor_counts
[i
];
347 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
348 i
+5,i
+1,vendor_firsts
[i
],vendor_counts
[i
],
352 /* look up the devices of the data transfer elements */
353 ch
->dt
= kmalloc(ch
->counts
[CHET_DT
]*sizeof(struct scsi_device
),
355 for (elem
= 0; elem
< ch
->counts
[CHET_DT
]; elem
++) {
358 if (elem
< CH_DT_MAX
&& -1 != dt_id
[elem
]) {
361 vprintk("dt 0x%x: [insmod option] ",
362 elem
+ch
->firsts
[CHET_DT
]);
363 } else if (0 != ch_read_element_status
364 (ch
,elem
+ch
->firsts
[CHET_DT
],data
)) {
365 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
366 elem
+ch
->firsts
[CHET_DT
]);
368 vprintk("dt 0x%x: ",elem
+ch
->firsts
[CHET_DT
]);
369 if (data
[6] & 0x80) {
371 printk("not this SCSI bus\n");
373 } else if (0 == (data
[6] & 0x30)) {
375 printk("ID/LUN unknown\n");
380 if (data
[6] & 0x20) id
= data
[7];
381 if (data
[6] & 0x10) lun
= data
[6] & 7;
386 printk("ID %i, LUN %i, ",id
,lun
);
388 scsi_device_lookup(ch
->device
->host
,
392 /* should not happen */
394 printk("Huh? device not found!\n");
397 printk("name: %8.8s %16.16s %4.4s\n",
398 ch
->dt
[elem
]->vendor
,
410 /* ------------------------------------------------------------------------ */
413 ch_position(scsi_changer
*ch
, u_int trans
, u_int elem
, int rotate
)
417 dprintk("position: 0x%x\n",elem
);
419 trans
= ch
->firsts
[CHET_MT
];
420 memset(cmd
,0,sizeof(cmd
));
421 cmd
[0] = POSITION_TO_ELEMENT
;
422 cmd
[1] = ch
->device
->lun
<< 5;
423 cmd
[2] = (trans
>> 8) & 0xff;
424 cmd
[3] = trans
& 0xff;
425 cmd
[4] = (elem
>> 8) & 0xff;
426 cmd
[5] = elem
& 0xff;
427 cmd
[8] = rotate
? 1 : 0;
428 return ch_do_scsi(ch
, cmd
, NULL
, 0, DMA_NONE
);
432 ch_move(scsi_changer
*ch
, u_int trans
, u_int src
, u_int dest
, int rotate
)
436 dprintk("move: 0x%x => 0x%x\n",src
,dest
);
438 trans
= ch
->firsts
[CHET_MT
];
439 memset(cmd
,0,sizeof(cmd
));
440 cmd
[0] = MOVE_MEDIUM
;
441 cmd
[1] = ch
->device
->lun
<< 5;
442 cmd
[2] = (trans
>> 8) & 0xff;
443 cmd
[3] = trans
& 0xff;
444 cmd
[4] = (src
>> 8) & 0xff;
446 cmd
[6] = (dest
>> 8) & 0xff;
447 cmd
[7] = dest
& 0xff;
448 cmd
[10] = rotate
? 1 : 0;
449 return ch_do_scsi(ch
, cmd
, NULL
,0, DMA_NONE
);
453 ch_exchange(scsi_changer
*ch
, u_int trans
, u_int src
,
454 u_int dest1
, u_int dest2
, int rotate1
, int rotate2
)
458 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
461 trans
= ch
->firsts
[CHET_MT
];
462 memset(cmd
,0,sizeof(cmd
));
463 cmd
[0] = EXCHANGE_MEDIUM
;
464 cmd
[1] = ch
->device
->lun
<< 5;
465 cmd
[2] = (trans
>> 8) & 0xff;
466 cmd
[3] = trans
& 0xff;
467 cmd
[4] = (src
>> 8) & 0xff;
469 cmd
[6] = (dest1
>> 8) & 0xff;
470 cmd
[7] = dest1
& 0xff;
471 cmd
[8] = (dest2
>> 8) & 0xff;
472 cmd
[9] = dest2
& 0xff;
473 cmd
[10] = (rotate1
? 1 : 0) | (rotate2
? 2 : 0);
475 return ch_do_scsi(ch
, cmd
, NULL
,0, DMA_NONE
);
479 ch_check_voltag(char *tag
)
483 for (i
= 0; i
< 32; i
++) {
484 /* restrict to ascii */
485 if (tag
[i
] >= 0x7f || tag
[i
] < 0x20)
487 /* don't allow search wildcards */
495 ch_set_voltag(scsi_changer
*ch
, u_int elem
,
496 int alternate
, int clear
, u_char
*tag
)
502 buffer
= kzalloc(512, GFP_KERNEL
);
506 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
507 clear
? "clear" : "set",
508 alternate
? "alternate" : "primary",
510 memset(cmd
,0,sizeof(cmd
));
511 cmd
[0] = SEND_VOLUME_TAG
;
512 cmd
[1] = (ch
->device
->lun
<< 5) |
513 ch_elem_to_typecode(ch
,elem
);
514 cmd
[2] = (elem
>> 8) & 0xff;
515 cmd
[3] = elem
& 0xff;
517 ? (alternate
? 0x0d : 0x0c)
518 : (alternate
? 0x0b : 0x0a);
522 memcpy(buffer
,tag
,32);
523 ch_check_voltag(buffer
);
525 result
= ch_do_scsi(ch
, cmd
, buffer
, 256, DMA_TO_DEVICE
);
530 static int ch_gstatus(scsi_changer
*ch
, int type
, unsigned char __user
*dest
)
536 mutex_lock(&ch
->lock
);
537 for (i
= 0; i
< ch
->counts
[type
]; i
++) {
538 if (0 != ch_read_element_status
539 (ch
, ch
->firsts
[type
]+i
,data
)) {
543 put_user(data
[2], dest
+i
);
544 if (data
[2] & CESTATUS_EXCEPT
)
545 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
547 (int)data
[4],(int)data
[5]);
548 retval
= ch_read_element_status
549 (ch
, ch
->firsts
[type
]+i
,data
);
553 mutex_unlock(&ch
->lock
);
557 /* ------------------------------------------------------------------------ */
560 ch_release(struct inode
*inode
, struct file
*file
)
562 scsi_changer
*ch
= file
->private_data
;
564 scsi_device_put(ch
->device
);
565 file
->private_data
= NULL
;
570 ch_open(struct inode
*inode
, struct file
*file
)
573 int minor
= iminor(inode
);
576 spin_lock(&ch_index_lock
);
577 ch
= idr_find(&ch_index_idr
, minor
);
579 if (NULL
== ch
|| scsi_device_get(ch
->device
)) {
580 spin_unlock(&ch_index_lock
);
584 spin_unlock(&ch_index_lock
);
586 file
->private_data
= ch
;
592 ch_checkrange(scsi_changer
*ch
, unsigned int type
, unsigned int unit
)
594 if (type
>= CH_TYPES
|| unit
>= ch
->counts
[type
])
599 static long ch_ioctl(struct file
*file
,
600 unsigned int cmd
, unsigned long arg
)
602 scsi_changer
*ch
= file
->private_data
;
604 void __user
*argp
= (void __user
*)arg
;
609 struct changer_params params
;
611 params
.cp_curpicker
= 0;
612 params
.cp_npickers
= ch
->counts
[CHET_MT
];
613 params
.cp_nslots
= ch
->counts
[CHET_ST
];
614 params
.cp_nportals
= ch
->counts
[CHET_IE
];
615 params
.cp_ndrives
= ch
->counts
[CHET_DT
];
617 if (copy_to_user(argp
, ¶ms
, sizeof(params
)))
623 struct changer_vendor_params vparams
;
625 memset(&vparams
,0,sizeof(vparams
));
626 if (ch
->counts
[CHET_V1
]) {
627 vparams
.cvp_n1
= ch
->counts
[CHET_V1
];
628 strncpy(vparams
.cvp_label1
,vendor_labels
[0],16);
630 if (ch
->counts
[CHET_V2
]) {
631 vparams
.cvp_n2
= ch
->counts
[CHET_V2
];
632 strncpy(vparams
.cvp_label2
,vendor_labels
[1],16);
634 if (ch
->counts
[CHET_V3
]) {
635 vparams
.cvp_n3
= ch
->counts
[CHET_V3
];
636 strncpy(vparams
.cvp_label3
,vendor_labels
[2],16);
638 if (ch
->counts
[CHET_V4
]) {
639 vparams
.cvp_n4
= ch
->counts
[CHET_V4
];
640 strncpy(vparams
.cvp_label4
,vendor_labels
[3],16);
642 if (copy_to_user(argp
, &vparams
, sizeof(vparams
)))
649 struct changer_position pos
;
651 if (copy_from_user(&pos
, argp
, sizeof (pos
)))
654 if (0 != ch_checkrange(ch
, pos
.cp_type
, pos
.cp_unit
)) {
655 dprintk("CHIOPOSITION: invalid parameter\n");
658 mutex_lock(&ch
->lock
);
659 retval
= ch_position(ch
,0,
660 ch
->firsts
[pos
.cp_type
] + pos
.cp_unit
,
661 pos
.cp_flags
& CP_INVERT
);
662 mutex_unlock(&ch
->lock
);
668 struct changer_move mv
;
670 if (copy_from_user(&mv
, argp
, sizeof (mv
)))
673 if (0 != ch_checkrange(ch
, mv
.cm_fromtype
, mv
.cm_fromunit
) ||
674 0 != ch_checkrange(ch
, mv
.cm_totype
, mv
.cm_tounit
)) {
675 dprintk("CHIOMOVE: invalid parameter\n");
679 mutex_lock(&ch
->lock
);
680 retval
= ch_move(ch
,0,
681 ch
->firsts
[mv
.cm_fromtype
] + mv
.cm_fromunit
,
682 ch
->firsts
[mv
.cm_totype
] + mv
.cm_tounit
,
683 mv
.cm_flags
& CM_INVERT
);
684 mutex_unlock(&ch
->lock
);
690 struct changer_exchange mv
;
692 if (copy_from_user(&mv
, argp
, sizeof (mv
)))
695 if (0 != ch_checkrange(ch
, mv
.ce_srctype
, mv
.ce_srcunit
) ||
696 0 != ch_checkrange(ch
, mv
.ce_fdsttype
, mv
.ce_fdstunit
) ||
697 0 != ch_checkrange(ch
, mv
.ce_sdsttype
, mv
.ce_sdstunit
)) {
698 dprintk("CHIOEXCHANGE: invalid parameter\n");
702 mutex_lock(&ch
->lock
);
705 ch
->firsts
[mv
.ce_srctype
] + mv
.ce_srcunit
,
706 ch
->firsts
[mv
.ce_fdsttype
] + mv
.ce_fdstunit
,
707 ch
->firsts
[mv
.ce_sdsttype
] + mv
.ce_sdstunit
,
708 mv
.ce_flags
& CE_INVERT1
, mv
.ce_flags
& CE_INVERT2
);
709 mutex_unlock(&ch
->lock
);
715 struct changer_element_status ces
;
717 if (copy_from_user(&ces
, argp
, sizeof (ces
)))
719 if (ces
.ces_type
< 0 || ces
.ces_type
>= CH_TYPES
)
722 return ch_gstatus(ch
, ces
.ces_type
, ces
.ces_data
);
727 struct changer_get_element cge
;
733 if (copy_from_user(&cge
, argp
, sizeof (cge
)))
736 if (0 != ch_checkrange(ch
, cge
.cge_type
, cge
.cge_unit
))
738 elem
= ch
->firsts
[cge
.cge_type
] + cge
.cge_unit
;
740 buffer
= kmalloc(512, GFP_KERNEL
| GFP_DMA
);
743 mutex_lock(&ch
->lock
);
746 memset(ch_cmd
, 0, sizeof(ch_cmd
));
747 ch_cmd
[0] = READ_ELEMENT_STATUS
;
748 ch_cmd
[1] = (ch
->device
->lun
<< 5) |
749 (ch
->voltags
? 0x10 : 0) |
750 ch_elem_to_typecode(ch
,elem
);
751 ch_cmd
[2] = (elem
>> 8) & 0xff;
752 ch_cmd
[3] = elem
& 0xff;
756 result
= ch_do_scsi(ch
, ch_cmd
, buffer
, 256, DMA_FROM_DEVICE
);
758 cge
.cge_status
= buffer
[18];
760 if (buffer
[18] & CESTATUS_EXCEPT
) {
763 if (buffer
[25] & 0x80) {
764 cge
.cge_flags
|= CGE_SRC
;
765 if (buffer
[25] & 0x40)
766 cge
.cge_flags
|= CGE_INVERT
;
767 elem
= (buffer
[26]<<8) | buffer
[27];
768 for (i
= 0; i
< 4; i
++) {
769 if (elem
>= ch
->firsts
[i
] &&
770 elem
< ch
->firsts
[i
] + ch
->counts
[i
]) {
772 cge
.cge_srcunit
= elem
-ch
->firsts
[i
];
776 if ((buffer
[22] & 0x30) == 0x30) {
777 cge
.cge_flags
|= CGE_IDLUN
;
778 cge
.cge_id
= buffer
[23];
779 cge
.cge_lun
= buffer
[22] & 7;
781 if (buffer
[9] & 0x80) {
782 cge
.cge_flags
|= CGE_PVOLTAG
;
783 memcpy(cge
.cge_pvoltag
,buffer
+28,36);
785 if (buffer
[9] & 0x40) {
786 cge
.cge_flags
|= CGE_AVOLTAG
;
787 memcpy(cge
.cge_avoltag
,buffer
+64,36);
789 } else if (ch
->voltags
) {
791 vprintk("device has no volume tag support\n");
795 mutex_unlock(&ch
->lock
);
797 if (copy_to_user(argp
, &cge
, sizeof (cge
)))
804 mutex_lock(&ch
->lock
);
805 retval
= ch_init_elem(ch
);
806 mutex_unlock(&ch
->lock
);
812 struct changer_set_voltag csv
;
815 if (copy_from_user(&csv
, argp
, sizeof(csv
)))
818 if (0 != ch_checkrange(ch
, csv
.csv_type
, csv
.csv_unit
)) {
819 dprintk("CHIOSVOLTAG: invalid parameter\n");
822 elem
= ch
->firsts
[csv
.csv_type
] + csv
.csv_unit
;
823 mutex_lock(&ch
->lock
);
824 retval
= ch_set_voltag(ch
, elem
,
825 csv
.csv_flags
& CSV_AVOLTAG
,
826 csv
.csv_flags
& CSV_CLEARTAG
,
828 mutex_unlock(&ch
->lock
);
833 return scsi_ioctl(ch
->device
, cmd
, argp
);
840 struct changer_element_status32
{
842 compat_uptr_t ces_data
;
844 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
846 static long ch_ioctl_compat(struct file
* file
,
847 unsigned int cmd
, unsigned long arg
)
849 scsi_changer
*ch
= file
->private_data
;
861 return ch_ioctl(file
, cmd
, arg
);
864 struct changer_element_status32 ces32
;
865 unsigned char __user
*data
;
867 if (copy_from_user(&ces32
, (void __user
*)arg
, sizeof (ces32
)))
869 if (ces32
.ces_type
< 0 || ces32
.ces_type
>= CH_TYPES
)
872 data
= compat_ptr(ces32
.ces_data
);
873 return ch_gstatus(ch
, ces32
.ces_type
, data
);
876 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
883 /* ------------------------------------------------------------------------ */
885 static int ch_probe(struct device
*dev
)
887 struct scsi_device
*sd
= to_scsi_device(dev
);
888 struct device
*class_dev
;
889 int minor
, ret
= -ENOMEM
;
892 if (sd
->type
!= TYPE_MEDIUM_CHANGER
)
895 ch
= kzalloc(sizeof(*ch
), GFP_KERNEL
);
899 if (!idr_pre_get(&ch_index_idr
, GFP_KERNEL
))
902 spin_lock(&ch_index_lock
);
903 ret
= idr_get_new(&ch_index_idr
, ch
, &minor
);
904 spin_unlock(&ch_index_lock
);
909 if (minor
> CH_MAX_DEVS
) {
915 sprintf(ch
->name
,"ch%d",ch
->minor
);
917 class_dev
= device_create_drvdata(ch_sysfs_class
, dev
,
918 MKDEV(SCSI_CHANGER_MAJOR
, ch
->minor
),
919 ch
, "s%s", ch
->name
);
920 if (IS_ERR(class_dev
)) {
921 printk(KERN_WARNING
"ch%d: device_create failed\n",
923 ret
= PTR_ERR(class_dev
);
927 mutex_init(&ch
->lock
);
933 dev_set_drvdata(dev
, ch
);
934 sdev_printk(KERN_INFO
, sd
, "Attached scsi changer %s\n", ch
->name
);
938 idr_remove(&ch_index_idr
, minor
);
944 static int ch_remove(struct device
*dev
)
946 scsi_changer
*ch
= dev_get_drvdata(dev
);
948 spin_lock(&ch_index_lock
);
949 idr_remove(&ch_index_idr
, ch
->minor
);
950 spin_unlock(&ch_index_lock
);
952 device_destroy(ch_sysfs_class
, MKDEV(SCSI_CHANGER_MAJOR
,ch
->minor
));
958 static struct scsi_driver ch_template
= {
959 .owner
= THIS_MODULE
,
967 static const struct file_operations changer_fops
= {
968 .owner
= THIS_MODULE
,
970 .release
= ch_release
,
971 .unlocked_ioctl
= ch_ioctl
,
973 .compat_ioctl
= ch_ioctl_compat
,
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
);
1010 idr_destroy(&ch_index_idr
);
1013 module_init(init_ch_module
);
1014 module_exit(exit_ch_module
);