xref: /linux/drivers/scsi/ch.c (revision 6fba331225f5040e93d0947d1027e9712238291f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SCSI Media Changer device driver for Linux 2.6
4  *
5  *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
6  *
7  */
8 
9 #define VERSION "0.25"
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/fs.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/major.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/blkdev.h>
21 #include <linux/completion.h>
22 #include <linux/compat.h>
23 #include <linux/chio.h>			/* here are all the ioctls */
24 #include <linux/mutex.h>
25 #include <linux/idr.h>
26 #include <linux/slab.h>
27 
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_eh.h>
35 #include <scsi/scsi_dbg.h>
36 
37 #define CH_DT_MAX       16
38 #define CH_TYPES        8
39 #define CH_MAX_DEVS     128
40 
41 MODULE_DESCRIPTION("device driver for scsi media changer devices");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
43 MODULE_LICENSE("GPL");
44 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
45 MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
46 
47 static int init = 1;
48 module_param(init, int, 0444);
49 MODULE_PARM_DESC(init, \
50     "initialize element status on driver load (default: on)");
51 
52 static int timeout_move = 300;
53 module_param(timeout_move, int, 0644);
54 MODULE_PARM_DESC(timeout_move,"timeout for move commands "
55 		 "(default: 300 seconds)");
56 
57 static int timeout_init = 3600;
58 module_param(timeout_init, int, 0644);
59 MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
60 		 "(default: 3600 seconds)");
61 
62 static int verbose = 1;
63 module_param(verbose, int, 0644);
64 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
65 
66 static int debug;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
69 		 "detailed sense codes on scsi errors (default: off)");
70 
71 static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
72 static int dt_lun[CH_DT_MAX];
73 module_param_array(dt_id,  int, NULL, 0444);
74 module_param_array(dt_lun, int, NULL, 0444);
75 
76 /* tell the driver about vendor-specific slots */
77 static int vendor_firsts[CH_TYPES-4];
78 static int vendor_counts[CH_TYPES-4];
79 module_param_array(vendor_firsts, int, NULL, 0444);
80 module_param_array(vendor_counts, int, NULL, 0444);
81 
82 static const char * vendor_labels[CH_TYPES-4] = {
83 	"v0", "v1", "v2", "v3"
84 };
85 // module_param_string_array(vendor_labels, NULL, 0444);
86 
87 #define ch_printk(prefix, ch, fmt, a...) \
88 	sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
89 
90 #define DPRINTK(fmt, arg...)						\
91 do {									\
92 	if (debug)							\
93 		ch_printk(KERN_DEBUG, ch, fmt, ##arg);			\
94 } while (0)
95 #define VPRINTK(level, fmt, arg...)					\
96 do {									\
97 	if (verbose)							\
98 		ch_printk(level, ch, fmt, ##arg);			\
99 } while (0)
100 
101 /* ------------------------------------------------------------------- */
102 
103 #define MAX_RETRIES   1
104 
105 static const struct class ch_sysfs_class = {
106 	.name = "scsi_changer",
107 };
108 
109 typedef struct {
110 	struct kref         ref;
111 	struct list_head    list;
112 	int                 minor;
113 	char                name[8];
114 	struct scsi_device  *device;
115 	u_int               firsts[CH_TYPES];
116 	u_int               counts[CH_TYPES];
117 	u_int		    voltags;
118 	struct mutex	    lock;
119 } scsi_changer;
120 
121 static DEFINE_IDR(ch_index_idr);
122 static DEFINE_SPINLOCK(ch_index_lock);
123 
124 static const struct {
125 	u8	sense_key;
126 	u16	sense_code;
127 	int	errno;
128 } ch_err[] = {
129 /* Just filled in what looks right. Hav'nt checked any standard paper for
130    these errno assignments, so they may be wrong... */
131 	{
132 		.sense_key	= ILLEGAL_REQUEST,
133 		.sense_code	= INVALID_ELEMENT_ADDRESS,
134 		.errno		= EBADSLT,
135 	},{
136 		.sense_key	= ILLEGAL_REQUEST,
137 		.sense_code	= IMPORT_OR_EXPORT_ELEMENT_ACCESSED,
138 		.errno		= EBADE,
139 	},{
140 		.sense_key	= ILLEGAL_REQUEST,
141 		.sense_code	= MEDIUM_DESTINATION_ELEMENT_FULL,
142 		.errno		= EXFULL,
143 	},{
144 		.sense_key	= ILLEGAL_REQUEST,
145 		.sense_code	= MEDIUM_SOURCE_ELEMENT_EMPTY,
146 		.errno		= EBADE,
147 	},{
148 		.sense_key	= ILLEGAL_REQUEST,
149 		.sense_code	= INVALID_COMMAND_OP_CODE,
150 		.errno		= EBADRQC,
151 	},{
152 	        /* end of list */
153 	}
154 };
155 
156 /* ------------------------------------------------------------------- */
157 
158 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
159 {
160 	int i,errno = 0;
161 
162 	/* Check to see if additional sense information is available */
163 	if (scsi_sense_valid(sshdr) && scsi_sense_asc(sshdr) != 0) {
164 		for (i = 0; ch_err[i].errno != 0; i++) {
165 			if (ch_err[i].sense_key == sshdr->sense_key &&
166 			    ch_err[i].sense_code == sshdr->sense_code) {
167 				errno = -ch_err[i].errno;
168 				break;
169 			}
170 		}
171 	}
172 	if (errno == 0)
173 		errno = -EIO;
174 	return errno;
175 }
176 
177 static int
178 ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
179 	   void *buffer, unsigned int buflength, enum req_op op)
180 {
181 	int errno = 0, timeout, result;
182 	struct scsi_sense_hdr sshdr;
183 	struct scsi_failure failure_defs[] = {
184 		{
185 			.sense_key = UNIT_ATTENTION,
186 			.sense_code = SCMD_FAILURE_SENSE_CODE_ANY,
187 			.allowed = 3,
188 			.result = SAM_STAT_CHECK_CONDITION,
189 		},
190 		{}
191 	};
192 	struct scsi_failures failures = {
193 		.failure_definitions = failure_defs,
194 	};
195 	const struct scsi_exec_args exec_args = {
196 		.sshdr = &sshdr,
197 		.failures = &failures,
198 	};
199 
200 	timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
201 		? timeout_init : timeout_move;
202 
203 	result = scsi_execute_cmd(ch->device, cmd, op, buffer, buflength,
204 				  timeout * HZ, MAX_RETRIES, &exec_args);
205 	if (result < 0)
206 		return result;
207 	if (scsi_sense_valid(&sshdr)) {
208 		if (debug)
209 			scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
210 		errno = ch_find_errno(&sshdr);
211 	}
212 	return errno;
213 }
214 
215 /* ------------------------------------------------------------------------ */
216 
217 static int
218 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
219 {
220 	int i;
221 
222 	for (i = 0; i < CH_TYPES; i++) {
223 		if (elem >= ch->firsts[i]  &&
224 		    elem <  ch->firsts[i] +
225 	            ch->counts[i])
226 			return i+1;
227 	}
228 	return 0;
229 }
230 
231 static int
232 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
233 {
234 	u_char  cmd[12];
235 	u_char  *buffer;
236 	int     result;
237 
238 	buffer = kmalloc(512, GFP_KERNEL);
239 	if(!buffer)
240 		return -ENOMEM;
241 
242  retry:
243 	memset(cmd,0,sizeof(cmd));
244 	cmd[0] = READ_ELEMENT_STATUS;
245 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
246 		(ch->voltags ? 0x10 : 0) |
247 		ch_elem_to_typecode(ch,elem);
248 	cmd[2] = (elem >> 8) & 0xff;
249 	cmd[3] = elem        & 0xff;
250 	cmd[5] = 1;
251 	cmd[9] = 255;
252 	if (0 == (result = ch_do_scsi(ch, cmd, 12,
253 				      buffer, 256, REQ_OP_DRV_IN))) {
254 		if (((buffer[16] << 8) | buffer[17]) != elem) {
255 			DPRINTK("asked for element 0x%02x, got 0x%02x\n",
256 				elem,(buffer[16] << 8) | buffer[17]);
257 			kfree(buffer);
258 			return -EIO;
259 		}
260 		memcpy(data,buffer+16,16);
261 	} else {
262 		if (ch->voltags) {
263 			ch->voltags = 0;
264 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
265 			goto retry;
266 		}
267 		DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
268 	}
269 	kfree(buffer);
270 	return result;
271 }
272 
273 static int
274 ch_init_elem(scsi_changer *ch)
275 {
276 	int err;
277 	u_char cmd[6];
278 
279 	VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
280 	memset(cmd,0,sizeof(cmd));
281 	cmd[0] = INITIALIZE_ELEMENT_STATUS;
282 	cmd[1] = (ch->device->lun & 0x7) << 5;
283 	err = ch_do_scsi(ch, cmd, 6, NULL, 0, REQ_OP_DRV_IN);
284 	VPRINTK(KERN_INFO, "... finished\n");
285 	return err;
286 }
287 
288 static int
289 ch_readconfig(scsi_changer *ch)
290 {
291 	u_char  cmd[10], data[16];
292 	u_char  *buffer;
293 	int     result,id,lun,i;
294 	u_int   elem;
295 
296 	buffer = kzalloc(512, GFP_KERNEL);
297 	if (!buffer)
298 		return -ENOMEM;
299 
300 	memset(cmd,0,sizeof(cmd));
301 	cmd[0] = MODE_SENSE;
302 	cmd[1] = (ch->device->lun & 0x7) << 5;
303 	cmd[2] = 0x1d;
304 	cmd[4] = 255;
305 	result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
306 	if (0 != result) {
307 		cmd[1] |= (1<<3);
308 		result  = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
309 	}
310 	if (0 == result) {
311 		ch->firsts[CHET_MT] =
312 			(buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
313 		ch->counts[CHET_MT] =
314 			(buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
315 		ch->firsts[CHET_ST] =
316 			(buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
317 		ch->counts[CHET_ST] =
318 			(buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
319 		ch->firsts[CHET_IE] =
320 			(buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
321 		ch->counts[CHET_IE] =
322 			(buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
323 		ch->firsts[CHET_DT] =
324 			(buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
325 		ch->counts[CHET_DT] =
326 			(buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
327 		VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
328 			ch->firsts[CHET_MT],
329 			ch->counts[CHET_MT]);
330 		VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
331 			ch->firsts[CHET_ST],
332 			ch->counts[CHET_ST]);
333 		VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
334 			ch->firsts[CHET_IE],
335 			ch->counts[CHET_IE]);
336 		VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
337 			ch->firsts[CHET_DT],
338 			ch->counts[CHET_DT]);
339 	} else {
340 		VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
341 	}
342 
343 	/* vendor specific element types */
344 	for (i = 0; i < 4; i++) {
345 		if (0 == vendor_counts[i])
346 			continue;
347 		if (NULL == vendor_labels[i])
348 			continue;
349 		ch->firsts[CHET_V1+i] = vendor_firsts[i];
350 		ch->counts[CHET_V1+i] = vendor_counts[i];
351 		VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
352 			i+5,i+1,vendor_firsts[i],vendor_counts[i],
353 			vendor_labels[i]);
354 	}
355 
356 	/* report the devices of the data transfer elements */
357 	for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
358 		struct scsi_device *sdev;
359 
360 		id  = -1;
361 		lun = 0;
362 		if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {
363 			id  = dt_id[elem];
364 			lun = dt_lun[elem];
365 			VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
366 				elem+ch->firsts[CHET_DT]);
367 		} else if (0 != ch_read_element_status
368 			   (ch,elem+ch->firsts[CHET_DT],data)) {
369 			VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
370 				elem+ch->firsts[CHET_DT]);
371 		} else {
372 			VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
373 			if (data[6] & 0x80) {
374 				VPRINTK(KERN_CONT, "not this SCSI bus\n");
375 			} else if (0 == (data[6] & 0x30)) {
376 				VPRINTK(KERN_CONT, "ID/LUN unknown\n");
377 			} else {
378 				id  = ch->device->id;
379 				lun = 0;
380 				if (data[6] & 0x20) id  = data[7];
381 				if (data[6] & 0x10) lun = data[6] & 7;
382 			}
383 		}
384 		if (-1 != id) {
385 			VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
386 			sdev = scsi_device_lookup(ch->device->host,
387 						  ch->device->channel,
388 						  id, lun);
389 			if (!sdev) {
390 				/* should not happen */
391 				VPRINTK(KERN_CONT, "Huh? device not found!\n");
392 			} else {
393 				VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
394 					sdev->vendor, sdev->model, sdev->rev);
395 				scsi_device_put(sdev);
396 			}
397 		}
398 	}
399 	ch->voltags = 1;
400 	kfree(buffer);
401 
402 	return 0;
403 }
404 
405 /* ------------------------------------------------------------------------ */
406 
407 static int
408 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
409 {
410 	u_char  cmd[10];
411 
412 	DPRINTK("position: 0x%x\n",elem);
413 	if (0 == trans)
414 		trans = ch->firsts[CHET_MT];
415 	memset(cmd,0,sizeof(cmd));
416 	cmd[0]  = POSITION_TO_ELEMENT;
417 	cmd[1]  = (ch->device->lun & 0x7) << 5;
418 	cmd[2]  = (trans >> 8) & 0xff;
419 	cmd[3]  =  trans       & 0xff;
420 	cmd[4]  = (elem  >> 8) & 0xff;
421 	cmd[5]  =  elem        & 0xff;
422 	cmd[8]  = rotate ? 1 : 0;
423 	return ch_do_scsi(ch, cmd, 10, NULL, 0, REQ_OP_DRV_IN);
424 }
425 
426 static int
427 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
428 {
429 	u_char  cmd[12];
430 
431 	DPRINTK("move: 0x%x => 0x%x\n",src,dest);
432 	if (0 == trans)
433 		trans = ch->firsts[CHET_MT];
434 	memset(cmd,0,sizeof(cmd));
435 	cmd[0]  = MOVE_MEDIUM;
436 	cmd[1]  = (ch->device->lun & 0x7) << 5;
437 	cmd[2]  = (trans >> 8) & 0xff;
438 	cmd[3]  =  trans       & 0xff;
439 	cmd[4]  = (src   >> 8) & 0xff;
440 	cmd[5]  =  src         & 0xff;
441 	cmd[6]  = (dest  >> 8) & 0xff;
442 	cmd[7]  =  dest        & 0xff;
443 	cmd[10] = rotate ? 1 : 0;
444 	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
445 }
446 
447 static int
448 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
449 	    u_int dest1, u_int dest2, int rotate1, int rotate2)
450 {
451 	u_char  cmd[12];
452 
453 	DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
454 		src,dest1,dest2);
455 	if (0 == trans)
456 		trans = ch->firsts[CHET_MT];
457 	memset(cmd,0,sizeof(cmd));
458 	cmd[0]  = EXCHANGE_MEDIUM;
459 	cmd[1]  = (ch->device->lun & 0x7) << 5;
460 	cmd[2]  = (trans >> 8) & 0xff;
461 	cmd[3]  =  trans       & 0xff;
462 	cmd[4]  = (src   >> 8) & 0xff;
463 	cmd[5]  =  src         & 0xff;
464 	cmd[6]  = (dest1 >> 8) & 0xff;
465 	cmd[7]  =  dest1       & 0xff;
466 	cmd[8]  = (dest2 >> 8) & 0xff;
467 	cmd[9]  =  dest2       & 0xff;
468 	cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
469 
470 	return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
471 }
472 
473 static void
474 ch_check_voltag(char *tag)
475 {
476 	int i;
477 
478 	for (i = 0; i < 32; i++) {
479 		/* restrict to ascii */
480 		if (tag[i] >= 0x7f || tag[i] < 0x20)
481 			tag[i] = ' ';
482 		/* don't allow search wildcards */
483 		if (tag[i] == '?' ||
484 		    tag[i] == '*')
485 			tag[i] = ' ';
486 	}
487 }
488 
489 static int
490 ch_set_voltag(scsi_changer *ch, u_int elem,
491 	      int alternate, int clear, u_char *tag)
492 {
493 	u_char  cmd[12];
494 	u_char  *buffer;
495 	int result;
496 
497 	buffer = kzalloc(512, GFP_KERNEL);
498 	if (!buffer)
499 		return -ENOMEM;
500 
501 	DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
502 		clear     ? "clear"     : "set",
503 		alternate ? "alternate" : "primary",
504 		elem, tag);
505 	memset(cmd,0,sizeof(cmd));
506 	cmd[0]  = SEND_VOLUME_TAG;
507 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
508 		ch_elem_to_typecode(ch,elem);
509 	cmd[2] = (elem >> 8) & 0xff;
510 	cmd[3] = elem        & 0xff;
511 	cmd[5] = clear
512 		? (alternate ? 0x0d : 0x0c)
513 		: (alternate ? 0x0b : 0x0a);
514 
515 	cmd[9] = 255;
516 
517 	memcpy(buffer,tag,32);
518 	ch_check_voltag(buffer);
519 
520 	result = ch_do_scsi(ch, cmd, 12, buffer, 256, REQ_OP_DRV_OUT);
521 	kfree(buffer);
522 	return result;
523 }
524 
525 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
526 {
527 	int retval = 0;
528 	u_char data[16];
529 	unsigned int i;
530 
531 	mutex_lock(&ch->lock);
532 	for (i = 0; i < ch->counts[type]; i++) {
533 		if (0 != ch_read_element_status
534 		    (ch, ch->firsts[type]+i,data)) {
535 			retval = -EIO;
536 			break;
537 		}
538 		put_user(data[2], dest+i);
539 		if (data[2] & CESTATUS_EXCEPT)
540 			VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
541 				ch->firsts[type]+i,
542 				(int)data[4],(int)data[5]);
543 		retval = ch_read_element_status
544 			(ch, ch->firsts[type]+i,data);
545 		if (0 != retval)
546 			break;
547 	}
548 	mutex_unlock(&ch->lock);
549 	return retval;
550 }
551 
552 /* ------------------------------------------------------------------------ */
553 
554 static void ch_destroy(struct kref *ref)
555 {
556 	scsi_changer *ch = container_of(ref, scsi_changer, ref);
557 
558 	ch->device = NULL;
559 	kfree(ch);
560 }
561 
562 static int
563 ch_release(struct inode *inode, struct file *file)
564 {
565 	scsi_changer *ch = file->private_data;
566 
567 	scsi_device_put(ch->device);
568 	file->private_data = NULL;
569 	kref_put(&ch->ref, ch_destroy);
570 	return 0;
571 }
572 
573 static int
574 ch_open(struct inode *inode, struct file *file)
575 {
576 	scsi_changer *ch;
577 	int minor = iminor(inode);
578 
579 	spin_lock(&ch_index_lock);
580 	ch = idr_find(&ch_index_idr, minor);
581 
582 	if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
583 		spin_unlock(&ch_index_lock);
584 		return -ENXIO;
585 	}
586 	spin_unlock(&ch_index_lock);
587 	if (scsi_device_get(ch->device)) {
588 		kref_put(&ch->ref, ch_destroy);
589 		return -ENXIO;
590 	}
591 	/* Synchronize with ch_probe() */
592 	mutex_lock(&ch->lock);
593 	file->private_data = ch;
594 	mutex_unlock(&ch->lock);
595 	return 0;
596 }
597 
598 static int
599 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
600 {
601 	if (type >= CH_TYPES  ||  unit >= ch->counts[type])
602 		return -1;
603 	return 0;
604 }
605 
606 struct changer_element_status32 {
607 	int		ces_type;
608 	compat_uptr_t	ces_data;
609 };
610 #define CHIOGSTATUS32  _IOW('c', 8, struct changer_element_status32)
611 
612 static long ch_ioctl(struct file *file,
613 		    unsigned int cmd, unsigned long arg)
614 {
615 	scsi_changer *ch = file->private_data;
616 	int retval;
617 	void __user *argp = (void __user *)arg;
618 
619 	retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
620 			file->f_flags & O_NDELAY);
621 	if (retval)
622 		return retval;
623 
624 	switch (cmd) {
625 	case CHIOGPARAMS:
626 	{
627 		struct changer_params params;
628 
629 		params.cp_curpicker = 0;
630 		params.cp_npickers  = ch->counts[CHET_MT];
631 		params.cp_nslots    = ch->counts[CHET_ST];
632 		params.cp_nportals  = ch->counts[CHET_IE];
633 		params.cp_ndrives   = ch->counts[CHET_DT];
634 
635 		if (copy_to_user(argp, &params, sizeof(params)))
636 			return -EFAULT;
637 		return 0;
638 	}
639 	case CHIOGVPARAMS:
640 	{
641 		struct changer_vendor_params vparams;
642 
643 		memset(&vparams,0,sizeof(vparams));
644 		if (ch->counts[CHET_V1]) {
645 			vparams.cvp_n1  = ch->counts[CHET_V1];
646 			strscpy(vparams.cvp_label1, vendor_labels[0],
647 				sizeof(vparams.cvp_label1));
648 		}
649 		if (ch->counts[CHET_V2]) {
650 			vparams.cvp_n2  = ch->counts[CHET_V2];
651 			strscpy(vparams.cvp_label2, vendor_labels[1],
652 				sizeof(vparams.cvp_label2));
653 		}
654 		if (ch->counts[CHET_V3]) {
655 			vparams.cvp_n3  = ch->counts[CHET_V3];
656 			strscpy(vparams.cvp_label3, vendor_labels[2],
657 				sizeof(vparams.cvp_label3));
658 		}
659 		if (ch->counts[CHET_V4]) {
660 			vparams.cvp_n4  = ch->counts[CHET_V4];
661 			strscpy(vparams.cvp_label4, vendor_labels[3],
662 				sizeof(vparams.cvp_label4));
663 		}
664 		if (copy_to_user(argp, &vparams, sizeof(vparams)))
665 			return -EFAULT;
666 		return 0;
667 	}
668 
669 	case CHIOPOSITION:
670 	{
671 		struct changer_position pos;
672 
673 		if (copy_from_user(&pos, argp, sizeof (pos)))
674 			return -EFAULT;
675 
676 		if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
677 			DPRINTK("CHIOPOSITION: invalid parameter\n");
678 			return -EBADSLT;
679 		}
680 		mutex_lock(&ch->lock);
681 		retval = ch_position(ch,0,
682 				     ch->firsts[pos.cp_type] + pos.cp_unit,
683 				     pos.cp_flags & CP_INVERT);
684 		mutex_unlock(&ch->lock);
685 		return retval;
686 	}
687 
688 	case CHIOMOVE:
689 	{
690 		struct changer_move mv;
691 
692 		if (copy_from_user(&mv, argp, sizeof (mv)))
693 			return -EFAULT;
694 
695 		if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
696 		    0 != ch_checkrange(ch, mv.cm_totype,   mv.cm_tounit  )) {
697 			DPRINTK("CHIOMOVE: invalid parameter\n");
698 			return -EBADSLT;
699 		}
700 
701 		mutex_lock(&ch->lock);
702 		retval = ch_move(ch,0,
703 				 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
704 				 ch->firsts[mv.cm_totype]   + mv.cm_tounit,
705 				 mv.cm_flags & CM_INVERT);
706 		mutex_unlock(&ch->lock);
707 		return retval;
708 	}
709 
710 	case CHIOEXCHANGE:
711 	{
712 		struct changer_exchange mv;
713 
714 		if (copy_from_user(&mv, argp, sizeof (mv)))
715 			return -EFAULT;
716 
717 		if (0 != ch_checkrange(ch, mv.ce_srctype,  mv.ce_srcunit ) ||
718 		    0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
719 		    0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
720 			DPRINTK("CHIOEXCHANGE: invalid parameter\n");
721 			return -EBADSLT;
722 		}
723 
724 		mutex_lock(&ch->lock);
725 		retval = ch_exchange
726 			(ch,0,
727 			 ch->firsts[mv.ce_srctype]  + mv.ce_srcunit,
728 			 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
729 			 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
730 			 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
731 		mutex_unlock(&ch->lock);
732 		return retval;
733 	}
734 
735 	case CHIOGSTATUS:
736 	{
737 		struct changer_element_status ces;
738 
739 		if (copy_from_user(&ces, argp, sizeof (ces)))
740 			return -EFAULT;
741 		if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
742 			return -EINVAL;
743 
744 		return ch_gstatus(ch, ces.ces_type, ces.ces_data);
745 	}
746 #ifdef CONFIG_COMPAT
747 	case CHIOGSTATUS32:
748 	{
749 		struct changer_element_status32 ces32;
750 
751 		if (copy_from_user(&ces32, argp, sizeof(ces32)))
752 			return -EFAULT;
753 		if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
754 			return -EINVAL;
755 
756 		return ch_gstatus(ch, ces32.ces_type,
757 				  compat_ptr(ces32.ces_data));
758 	}
759 #endif
760 	case CHIOGELEM:
761 	{
762 		struct changer_get_element cge;
763 		u_char ch_cmd[12];
764 		u_char *buffer;
765 		unsigned int elem;
766 		int     result,i;
767 
768 		if (copy_from_user(&cge, argp, sizeof (cge)))
769 			return -EFAULT;
770 
771 		if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
772 			return -EINVAL;
773 		elem = ch->firsts[cge.cge_type] + cge.cge_unit;
774 
775 		buffer = kmalloc(512, GFP_KERNEL);
776 		if (!buffer)
777 			return -ENOMEM;
778 		mutex_lock(&ch->lock);
779 
780 	voltag_retry:
781 		memset(ch_cmd, 0, sizeof(ch_cmd));
782 		ch_cmd[0] = READ_ELEMENT_STATUS;
783 		ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
784 			(ch->voltags ? 0x10 : 0) |
785 			ch_elem_to_typecode(ch,elem);
786 		ch_cmd[2] = (elem >> 8) & 0xff;
787 		ch_cmd[3] = elem        & 0xff;
788 		ch_cmd[5] = 1;
789 		ch_cmd[9] = 255;
790 
791 		result = ch_do_scsi(ch, ch_cmd, 12, buffer, 256, REQ_OP_DRV_IN);
792 		if (!result) {
793 			cge.cge_status = buffer[18];
794 			cge.cge_flags = 0;
795 			if (buffer[18] & CESTATUS_EXCEPT) {
796 				cge.cge_errno = EIO;
797 			}
798 			if (buffer[25] & 0x80) {
799 				cge.cge_flags |= CGE_SRC;
800 				if (buffer[25] & 0x40)
801 					cge.cge_flags |= CGE_INVERT;
802 				elem = (buffer[26]<<8) | buffer[27];
803 				for (i = 0; i < 4; i++) {
804 					if (elem >= ch->firsts[i] &&
805 					    elem <  ch->firsts[i] + ch->counts[i]) {
806 						cge.cge_srctype = i;
807 						cge.cge_srcunit = elem-ch->firsts[i];
808 					}
809 				}
810 			}
811 			if ((buffer[22] & 0x30) == 0x30) {
812 				cge.cge_flags |= CGE_IDLUN;
813 				cge.cge_id  = buffer[23];
814 				cge.cge_lun = buffer[22] & 7;
815 			}
816 			if (buffer[9] & 0x80) {
817 				cge.cge_flags |= CGE_PVOLTAG;
818 				memcpy(cge.cge_pvoltag,buffer+28,36);
819 			}
820 			if (buffer[9] & 0x40) {
821 				cge.cge_flags |= CGE_AVOLTAG;
822 				memcpy(cge.cge_avoltag,buffer+64,36);
823 			}
824 		} else if (ch->voltags) {
825 			ch->voltags = 0;
826 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
827 			goto voltag_retry;
828 		}
829 		kfree(buffer);
830 		mutex_unlock(&ch->lock);
831 
832 		if (copy_to_user(argp, &cge, sizeof (cge)))
833 			return -EFAULT;
834 		return result;
835 	}
836 
837 	case CHIOINITELEM:
838 	{
839 		mutex_lock(&ch->lock);
840 		retval = ch_init_elem(ch);
841 		mutex_unlock(&ch->lock);
842 		return retval;
843 	}
844 
845 	case CHIOSVOLTAG:
846 	{
847 		struct changer_set_voltag csv;
848 		int elem;
849 
850 		if (copy_from_user(&csv, argp, sizeof(csv)))
851 			return -EFAULT;
852 
853 		if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
854 			DPRINTK("CHIOSVOLTAG: invalid parameter\n");
855 			return -EBADSLT;
856 		}
857 		elem = ch->firsts[csv.csv_type] + csv.csv_unit;
858 		mutex_lock(&ch->lock);
859 		retval = ch_set_voltag(ch, elem,
860 				       csv.csv_flags & CSV_AVOLTAG,
861 				       csv.csv_flags & CSV_CLEARTAG,
862 				       csv.csv_voltag);
863 		mutex_unlock(&ch->lock);
864 		return retval;
865 	}
866 
867 	default:
868 		return scsi_ioctl(ch->device, file->f_mode & FMODE_WRITE, cmd,
869 				  argp);
870 
871 	}
872 }
873 
874 /* ------------------------------------------------------------------------ */
875 
876 static int ch_probe(struct scsi_device *sd)
877 {
878 	struct device *dev = &sd->sdev_gendev;
879 	struct device *class_dev;
880 	int ret;
881 	scsi_changer *ch;
882 
883 	if (sd->type != TYPE_MEDIUM_CHANGER)
884 		return -ENODEV;
885 
886 	ch = kzalloc_obj(*ch);
887 	if (NULL == ch)
888 		return -ENOMEM;
889 
890 	idr_preload(GFP_KERNEL);
891 	spin_lock(&ch_index_lock);
892 	ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
893 	spin_unlock(&ch_index_lock);
894 	idr_preload_end();
895 
896 	if (ret < 0) {
897 		if (ret == -ENOSPC)
898 			ret = -ENODEV;
899 		goto free_ch;
900 	}
901 
902 	ch->minor = ret;
903 	sprintf(ch->name,"ch%d",ch->minor);
904 	ret = scsi_device_get(sd);
905 	if (ret) {
906 		sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
907 			    ch->minor);
908 		goto remove_idr;
909 	}
910 
911 	mutex_init(&ch->lock);
912 	kref_init(&ch->ref);
913 	ch->device = sd;
914 	class_dev = device_create(&ch_sysfs_class, dev,
915 				  MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
916 				  "s%s", ch->name);
917 	if (IS_ERR(class_dev)) {
918 		sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
919 			    ch->minor);
920 		ret = PTR_ERR(class_dev);
921 		goto put_device;
922 	}
923 
924 	mutex_lock(&ch->lock);
925 	ret = ch_readconfig(ch);
926 	if (ret) {
927 		mutex_unlock(&ch->lock);
928 		goto destroy_dev;
929 	}
930 	if (init)
931 		ch_init_elem(ch);
932 
933 	mutex_unlock(&ch->lock);
934 	dev_set_drvdata(dev, ch);
935 	sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
936 
937 	return 0;
938 destroy_dev:
939 	device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
940 put_device:
941 	scsi_device_put(sd);
942 remove_idr:
943 	idr_remove(&ch_index_idr, ch->minor);
944 free_ch:
945 	kfree(ch);
946 	return ret;
947 }
948 
949 static void ch_remove(struct scsi_device *sd)
950 {
951 	struct device *dev = &sd->sdev_gendev;
952 	scsi_changer *ch = dev_get_drvdata(dev);
953 
954 	spin_lock(&ch_index_lock);
955 	idr_remove(&ch_index_idr, ch->minor);
956 	dev_set_drvdata(dev, NULL);
957 	spin_unlock(&ch_index_lock);
958 
959 	device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
960 	scsi_device_put(ch->device);
961 	kref_put(&ch->ref, ch_destroy);
962 }
963 
964 static struct scsi_driver ch_template = {
965 	.probe = ch_probe,
966 	.remove = ch_remove,
967 	.gendrv = {
968 		.name	= "ch",
969 		.owner	= THIS_MODULE,
970 	},
971 };
972 
973 static const struct file_operations changer_fops = {
974 	.owner		= THIS_MODULE,
975 	.open		= ch_open,
976 	.release	= ch_release,
977 	.unlocked_ioctl	= ch_ioctl,
978 	.compat_ioctl	= compat_ptr_ioctl,
979 	.llseek		= noop_llseek,
980 };
981 
982 static int __init init_ch_module(void)
983 {
984 	int rc;
985 
986 	printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
987 	rc = class_register(&ch_sysfs_class);
988 	if (rc)
989 		return rc;
990 	rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
991 	if (rc < 0) {
992 		printk("Unable to get major %d for SCSI-Changer\n",
993 		       SCSI_CHANGER_MAJOR);
994 		goto fail1;
995 	}
996 	rc = scsi_register_driver(&ch_template);
997 	if (rc < 0)
998 		goto fail2;
999 	return 0;
1000 
1001  fail2:
1002 	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1003  fail1:
1004 	class_unregister(&ch_sysfs_class);
1005 	return rc;
1006 }
1007 
1008 static void __exit exit_ch_module(void)
1009 {
1010 	scsi_unregister_driver(&ch_template);
1011 	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1012 	class_unregister(&ch_sysfs_class);
1013 	idr_destroy(&ch_index_idr);
1014 }
1015 
1016 module_init(init_ch_module);
1017 module_exit(exit_ch_module);
1018