lockd: handle fl_grant callbacks
[pv_ops_mirror.git] / drivers / scsi / ch.c
blob2a2cc6cf1182a9cdaa2ebf0df1c2b9e15b4b37e3
1 /*
2 * SCSI Media Changer device driver for Linux 2.6
4 * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
6 */
8 #define VERSION "0.25"
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/fs.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.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>
34 #define CH_DT_MAX 16
35 #define CH_TYPES 8
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);
42 static int init = 1;
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)");
61 static int debug = 0;
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 /* ------------------------------------------------------------------- */
89 #define MAX_RETRIES 1
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);
97 #ifdef CONFIG_COMPAT
98 static long ch_ioctl_compat(struct file * filp,
99 unsigned int cmd, unsigned long arg);
100 #endif
102 static struct class * ch_sysfs_class;
104 typedef struct {
105 struct list_head list;
106 int minor;
107 char name[8];
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;
113 u_int voltags;
114 struct mutex lock;
115 } scsi_changer;
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,
124 .gendrv = {
125 .name = "ch",
126 .probe = ch_probe,
127 .remove = ch_remove,
131 static const struct file_operations changer_fops =
133 .owner = THIS_MODULE,
134 .open = ch_open,
135 .release = ch_release,
136 .ioctl = ch_ioctl,
137 #ifdef CONFIG_COMPAT
138 .compat_ioctl = ch_ioctl_compat,
139 #endif
142 static const struct {
143 unsigned char sense;
144 unsigned char asc;
145 unsigned char ascq;
146 int errno;
147 } err[] = {
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,
152 .asc = 0x21,
153 .ascq = 0x01,
154 .errno = EBADSLT, /* Invalid element address */
156 .sense = ILLEGAL_REQUEST,
157 .asc = 0x28,
158 .ascq = 0x01,
159 .errno = EBADE, /* Import or export element accessed */
161 .sense = ILLEGAL_REQUEST,
162 .asc = 0x3B,
163 .ascq = 0x0D,
164 .errno = EXFULL, /* Medium destination element full */
166 .sense = ILLEGAL_REQUEST,
167 .asc = 0x3B,
168 .ascq = 0x0E,
169 .errno = EBADE, /* Medium source element empty */
171 .sense = ILLEGAL_REQUEST,
172 .asc = 0x20,
173 .ascq = 0x00,
174 .errno = EBADRQC, /* Invalid command operation code */
176 /* end of list */
180 /* ------------------------------------------------------------------- */
182 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
184 int i,errno = 0;
186 /* Check to see if additional sense information is available */
187 if (scsi_sense_valid(sshdr) &&
188 sshdr->asc != 0) {
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;
194 break;
198 if (errno == 0)
199 errno = -EIO;
200 return errno;
203 static int
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;
214 retry:
215 errno = 0;
216 if (debug) {
217 dprintk("command: ");
218 __scsi_print_command(cmd);
221 result = scsi_execute_req(ch->device, cmd, direction, buffer,
222 buflength, &sshdr, timeout * HZ,
223 MAX_RETRIES);
225 dprintk("result: 0x%x\n",result);
226 if (driver_byte(result) & DRIVER_SENSE) {
227 if (debug)
228 scsi_print_sense_hdr(ch->name, &sshdr);
229 errno = ch_find_errno(&sshdr);
231 switch(sshdr.sense_key) {
232 case UNIT_ATTENTION:
233 ch->unit_attention = 1;
234 if (retries++ < 3)
235 goto retry;
236 break;
239 return errno;
242 /* ------------------------------------------------------------------------ */
244 static int
245 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
247 int i;
249 for (i = 0; i < CH_TYPES; i++) {
250 if (elem >= ch->firsts[i] &&
251 elem < ch->firsts[i] +
252 ch->counts[i])
253 return i+1;
255 return 0;
258 static int
259 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
261 u_char cmd[12];
262 u_char *buffer;
263 int result;
265 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
266 if(!buffer)
267 return -ENOMEM;
269 retry:
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;
277 cmd[5] = 1;
278 cmd[9] = 255;
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]);
283 kfree(buffer);
284 return -EIO;
286 memcpy(data,buffer+16,16);
287 } else {
288 if (ch->voltags) {
289 ch->voltags = 0;
290 vprintk("device has no volume tag support\n");
291 goto retry;
293 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
295 kfree(buffer);
296 return result;
299 static int
300 ch_init_elem(scsi_changer *ch)
302 int err;
303 u_char cmd[6];
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");
311 return err;
314 static int
315 ch_readconfig(scsi_changer *ch)
317 u_char cmd[10], data[16];
318 u_char *buffer;
319 int result,id,lun,i;
320 u_int elem;
322 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
323 if (!buffer)
324 return -ENOMEM;
325 memset(buffer,0,512);
327 memset(cmd,0,sizeof(cmd));
328 cmd[0] = MODE_SENSE;
329 cmd[1] = ch->device->lun << 5;
330 cmd[2] = 0x1d;
331 cmd[4] = 255;
332 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
333 if (0 != result) {
334 cmd[1] |= (1<<3);
335 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
337 if (0 == result) {
338 ch->firsts[CHET_MT] =
339 (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
340 ch->counts[CHET_MT] =
341 (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
342 ch->firsts[CHET_ST] =
343 (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
344 ch->counts[CHET_ST] =
345 (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
346 ch->firsts[CHET_IE] =
347 (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
348 ch->counts[CHET_IE] =
349 (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
350 ch->firsts[CHET_DT] =
351 (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
352 ch->counts[CHET_DT] =
353 (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
354 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
355 ch->firsts[CHET_MT],
356 ch->counts[CHET_MT]);
357 vprintk("type #2 (st): 0x%x+%d [storage]\n",
358 ch->firsts[CHET_ST],
359 ch->counts[CHET_ST]);
360 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
361 ch->firsts[CHET_IE],
362 ch->counts[CHET_IE]);
363 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
364 ch->firsts[CHET_DT],
365 ch->counts[CHET_DT]);
366 } else {
367 vprintk("reading element address assigment page failed!\n");
370 /* vendor specific element types */
371 for (i = 0; i < 4; i++) {
372 if (0 == vendor_counts[i])
373 continue;
374 if (NULL == vendor_labels[i])
375 continue;
376 ch->firsts[CHET_V1+i] = vendor_firsts[i];
377 ch->counts[CHET_V1+i] = vendor_counts[i];
378 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
379 i+5,i+1,vendor_firsts[i],vendor_counts[i],
380 vendor_labels[i]);
383 /* look up the devices of the data transfer elements */
384 ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
385 GFP_KERNEL);
386 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
387 id = -1;
388 lun = 0;
389 if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
390 id = dt_id[elem];
391 lun = dt_lun[elem];
392 vprintk("dt 0x%x: [insmod option] ",
393 elem+ch->firsts[CHET_DT]);
394 } else if (0 != ch_read_element_status
395 (ch,elem+ch->firsts[CHET_DT],data)) {
396 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
397 elem+ch->firsts[CHET_DT]);
398 } else {
399 vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
400 if (data[6] & 0x80) {
401 if (verbose)
402 printk("not this SCSI bus\n");
403 ch->dt[elem] = NULL;
404 } else if (0 == (data[6] & 0x30)) {
405 if (verbose)
406 printk("ID/LUN unknown\n");
407 ch->dt[elem] = NULL;
408 } else {
409 id = ch->device->id;
410 lun = 0;
411 if (data[6] & 0x20) id = data[7];
412 if (data[6] & 0x10) lun = data[6] & 7;
415 if (-1 != id) {
416 if (verbose)
417 printk("ID %i, LUN %i, ",id,lun);
418 ch->dt[elem] =
419 scsi_device_lookup(ch->device->host,
420 ch->device->channel,
421 id,lun);
422 if (!ch->dt[elem]) {
423 /* should not happen */
424 if (verbose)
425 printk("Huh? device not found!\n");
426 } else {
427 if (verbose)
428 printk("name: %8.8s %16.16s %4.4s\n",
429 ch->dt[elem]->vendor,
430 ch->dt[elem]->model,
431 ch->dt[elem]->rev);
435 ch->voltags = 1;
436 kfree(buffer);
438 return 0;
441 /* ------------------------------------------------------------------------ */
443 static int
444 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
446 u_char cmd[10];
448 dprintk("position: 0x%x\n",elem);
449 if (0 == trans)
450 trans = ch->firsts[CHET_MT];
451 memset(cmd,0,sizeof(cmd));
452 cmd[0] = POSITION_TO_ELEMENT;
453 cmd[1] = ch->device->lun << 5;
454 cmd[2] = (trans >> 8) & 0xff;
455 cmd[3] = trans & 0xff;
456 cmd[4] = (elem >> 8) & 0xff;
457 cmd[5] = elem & 0xff;
458 cmd[8] = rotate ? 1 : 0;
459 return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
462 static int
463 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
465 u_char cmd[12];
467 dprintk("move: 0x%x => 0x%x\n",src,dest);
468 if (0 == trans)
469 trans = ch->firsts[CHET_MT];
470 memset(cmd,0,sizeof(cmd));
471 cmd[0] = MOVE_MEDIUM;
472 cmd[1] = ch->device->lun << 5;
473 cmd[2] = (trans >> 8) & 0xff;
474 cmd[3] = trans & 0xff;
475 cmd[4] = (src >> 8) & 0xff;
476 cmd[5] = src & 0xff;
477 cmd[6] = (dest >> 8) & 0xff;
478 cmd[7] = dest & 0xff;
479 cmd[10] = rotate ? 1 : 0;
480 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
483 static int
484 ch_exchange(scsi_changer *ch, u_int trans, u_int src,
485 u_int dest1, u_int dest2, int rotate1, int rotate2)
487 u_char cmd[12];
489 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
490 src,dest1,dest2);
491 if (0 == trans)
492 trans = ch->firsts[CHET_MT];
493 memset(cmd,0,sizeof(cmd));
494 cmd[0] = EXCHANGE_MEDIUM;
495 cmd[1] = ch->device->lun << 5;
496 cmd[2] = (trans >> 8) & 0xff;
497 cmd[3] = trans & 0xff;
498 cmd[4] = (src >> 8) & 0xff;
499 cmd[5] = src & 0xff;
500 cmd[6] = (dest1 >> 8) & 0xff;
501 cmd[7] = dest1 & 0xff;
502 cmd[8] = (dest2 >> 8) & 0xff;
503 cmd[9] = dest2 & 0xff;
504 cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
506 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
509 static void
510 ch_check_voltag(char *tag)
512 int i;
514 for (i = 0; i < 32; i++) {
515 /* restrict to ascii */
516 if (tag[i] >= 0x7f || tag[i] < 0x20)
517 tag[i] = ' ';
518 /* don't allow search wildcards */
519 if (tag[i] == '?' ||
520 tag[i] == '*')
521 tag[i] = ' ';
525 static int
526 ch_set_voltag(scsi_changer *ch, u_int elem,
527 int alternate, int clear, u_char *tag)
529 u_char cmd[12];
530 u_char *buffer;
531 int result;
533 buffer = kmalloc(512, GFP_KERNEL);
534 if (!buffer)
535 return -ENOMEM;
536 memset(buffer,0,512);
538 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
539 clear ? "clear" : "set",
540 alternate ? "alternate" : "primary",
541 elem, tag);
542 memset(cmd,0,sizeof(cmd));
543 cmd[0] = SEND_VOLUME_TAG;
544 cmd[1] = (ch->device->lun << 5) |
545 ch_elem_to_typecode(ch,elem);
546 cmd[2] = (elem >> 8) & 0xff;
547 cmd[3] = elem & 0xff;
548 cmd[5] = clear
549 ? (alternate ? 0x0d : 0x0c)
550 : (alternate ? 0x0b : 0x0a);
552 cmd[9] = 255;
554 memcpy(buffer,tag,32);
555 ch_check_voltag(buffer);
557 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
558 kfree(buffer);
559 return result;
562 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
564 int retval = 0;
565 u_char data[16];
566 unsigned int i;
568 mutex_lock(&ch->lock);
569 for (i = 0; i < ch->counts[type]; i++) {
570 if (0 != ch_read_element_status
571 (ch, ch->firsts[type]+i,data)) {
572 retval = -EIO;
573 break;
575 put_user(data[2], dest+i);
576 if (data[2] & CESTATUS_EXCEPT)
577 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
578 ch->firsts[type]+i,
579 (int)data[4],(int)data[5]);
580 retval = ch_read_element_status
581 (ch, ch->firsts[type]+i,data);
582 if (0 != retval)
583 break;
585 mutex_unlock(&ch->lock);
586 return retval;
589 /* ------------------------------------------------------------------------ */
591 static int
592 ch_release(struct inode *inode, struct file *file)
594 scsi_changer *ch = file->private_data;
596 scsi_device_put(ch->device);
597 file->private_data = NULL;
598 return 0;
601 static int
602 ch_open(struct inode *inode, struct file *file)
604 scsi_changer *tmp, *ch;
605 int minor = iminor(inode);
607 spin_lock(&ch_devlist_lock);
608 ch = NULL;
609 list_for_each_entry(tmp,&ch_devlist,list) {
610 if (tmp->minor == minor)
611 ch = tmp;
613 if (NULL == ch || scsi_device_get(ch->device)) {
614 spin_unlock(&ch_devlist_lock);
615 return -ENXIO;
617 spin_unlock(&ch_devlist_lock);
619 file->private_data = ch;
620 return 0;
623 static int
624 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
626 if (type >= CH_TYPES || unit >= ch->counts[type])
627 return -1;
628 return 0;
631 static int ch_ioctl(struct inode * inode, struct file * file,
632 unsigned int cmd, unsigned long arg)
634 scsi_changer *ch = file->private_data;
635 int retval;
636 void __user *argp = (void __user *)arg;
638 switch (cmd) {
639 case CHIOGPARAMS:
641 struct changer_params params;
643 params.cp_curpicker = 0;
644 params.cp_npickers = ch->counts[CHET_MT];
645 params.cp_nslots = ch->counts[CHET_ST];
646 params.cp_nportals = ch->counts[CHET_IE];
647 params.cp_ndrives = ch->counts[CHET_DT];
649 if (copy_to_user(argp, &params, sizeof(params)))
650 return -EFAULT;
651 return 0;
653 case CHIOGVPARAMS:
655 struct changer_vendor_params vparams;
657 memset(&vparams,0,sizeof(vparams));
658 if (ch->counts[CHET_V1]) {
659 vparams.cvp_n1 = ch->counts[CHET_V1];
660 strncpy(vparams.cvp_label1,vendor_labels[0],16);
662 if (ch->counts[CHET_V2]) {
663 vparams.cvp_n2 = ch->counts[CHET_V2];
664 strncpy(vparams.cvp_label2,vendor_labels[1],16);
666 if (ch->counts[CHET_V3]) {
667 vparams.cvp_n3 = ch->counts[CHET_V3];
668 strncpy(vparams.cvp_label3,vendor_labels[2],16);
670 if (ch->counts[CHET_V4]) {
671 vparams.cvp_n4 = ch->counts[CHET_V4];
672 strncpy(vparams.cvp_label4,vendor_labels[3],16);
674 if (copy_to_user(argp, &vparams, sizeof(vparams)))
675 return -EFAULT;
676 return 0;
679 case CHIOPOSITION:
681 struct changer_position pos;
683 if (copy_from_user(&pos, argp, sizeof (pos)))
684 return -EFAULT;
686 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
687 dprintk("CHIOPOSITION: invalid parameter\n");
688 return -EBADSLT;
690 mutex_lock(&ch->lock);
691 retval = ch_position(ch,0,
692 ch->firsts[pos.cp_type] + pos.cp_unit,
693 pos.cp_flags & CP_INVERT);
694 mutex_unlock(&ch->lock);
695 return retval;
698 case CHIOMOVE:
700 struct changer_move mv;
702 if (copy_from_user(&mv, argp, sizeof (mv)))
703 return -EFAULT;
705 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
706 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
707 dprintk("CHIOMOVE: invalid parameter\n");
708 return -EBADSLT;
711 mutex_lock(&ch->lock);
712 retval = ch_move(ch,0,
713 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
714 ch->firsts[mv.cm_totype] + mv.cm_tounit,
715 mv.cm_flags & CM_INVERT);
716 mutex_unlock(&ch->lock);
717 return retval;
720 case CHIOEXCHANGE:
722 struct changer_exchange mv;
724 if (copy_from_user(&mv, argp, sizeof (mv)))
725 return -EFAULT;
727 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
728 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
729 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
730 dprintk("CHIOEXCHANGE: invalid parameter\n");
731 return -EBADSLT;
734 mutex_lock(&ch->lock);
735 retval = ch_exchange
736 (ch,0,
737 ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
738 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
739 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
740 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
741 mutex_unlock(&ch->lock);
742 return retval;
745 case CHIOGSTATUS:
747 struct changer_element_status ces;
749 if (copy_from_user(&ces, argp, sizeof (ces)))
750 return -EFAULT;
751 if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
752 return -EINVAL;
754 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
757 case CHIOGELEM:
759 struct changer_get_element cge;
760 u_char cmd[12];
761 u_char *buffer;
762 unsigned int elem;
763 int result,i;
765 if (copy_from_user(&cge, argp, sizeof (cge)))
766 return -EFAULT;
768 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
769 return -EINVAL;
770 elem = ch->firsts[cge.cge_type] + cge.cge_unit;
772 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
773 if (!buffer)
774 return -ENOMEM;
775 mutex_lock(&ch->lock);
777 voltag_retry:
778 memset(cmd,0,sizeof(cmd));
779 cmd[0] = READ_ELEMENT_STATUS;
780 cmd[1] = (ch->device->lun << 5) |
781 (ch->voltags ? 0x10 : 0) |
782 ch_elem_to_typecode(ch,elem);
783 cmd[2] = (elem >> 8) & 0xff;
784 cmd[3] = elem & 0xff;
785 cmd[5] = 1;
786 cmd[9] = 255;
788 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
789 cge.cge_status = buffer[18];
790 cge.cge_flags = 0;
791 if (buffer[18] & CESTATUS_EXCEPT) {
792 cge.cge_errno = EIO;
794 if (buffer[25] & 0x80) {
795 cge.cge_flags |= CGE_SRC;
796 if (buffer[25] & 0x40)
797 cge.cge_flags |= CGE_INVERT;
798 elem = (buffer[26]<<8) | buffer[27];
799 for (i = 0; i < 4; i++) {
800 if (elem >= ch->firsts[i] &&
801 elem < ch->firsts[i] + ch->counts[i]) {
802 cge.cge_srctype = i;
803 cge.cge_srcunit = elem-ch->firsts[i];
807 if ((buffer[22] & 0x30) == 0x30) {
808 cge.cge_flags |= CGE_IDLUN;
809 cge.cge_id = buffer[23];
810 cge.cge_lun = buffer[22] & 7;
812 if (buffer[9] & 0x80) {
813 cge.cge_flags |= CGE_PVOLTAG;
814 memcpy(cge.cge_pvoltag,buffer+28,36);
816 if (buffer[9] & 0x40) {
817 cge.cge_flags |= CGE_AVOLTAG;
818 memcpy(cge.cge_avoltag,buffer+64,36);
820 } else if (ch->voltags) {
821 ch->voltags = 0;
822 vprintk("device has no volume tag support\n");
823 goto voltag_retry;
825 kfree(buffer);
826 mutex_unlock(&ch->lock);
828 if (copy_to_user(argp, &cge, sizeof (cge)))
829 return -EFAULT;
830 return result;
833 case CHIOINITELEM:
835 mutex_lock(&ch->lock);
836 retval = ch_init_elem(ch);
837 mutex_unlock(&ch->lock);
838 return retval;
841 case CHIOSVOLTAG:
843 struct changer_set_voltag csv;
844 int elem;
846 if (copy_from_user(&csv, argp, sizeof(csv)))
847 return -EFAULT;
849 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
850 dprintk("CHIOSVOLTAG: invalid parameter\n");
851 return -EBADSLT;
853 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
854 mutex_lock(&ch->lock);
855 retval = ch_set_voltag(ch, elem,
856 csv.csv_flags & CSV_AVOLTAG,
857 csv.csv_flags & CSV_CLEARTAG,
858 csv.csv_voltag);
859 mutex_unlock(&ch->lock);
860 return retval;
863 default:
864 return scsi_ioctl(ch->device, cmd, argp);
869 #ifdef CONFIG_COMPAT
871 struct changer_element_status32 {
872 int ces_type;
873 compat_uptr_t ces_data;
875 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
877 static long ch_ioctl_compat(struct file * file,
878 unsigned int cmd, unsigned long arg)
880 scsi_changer *ch = file->private_data;
882 switch (cmd) {
883 case CHIOGPARAMS:
884 case CHIOGVPARAMS:
885 case CHIOPOSITION:
886 case CHIOMOVE:
887 case CHIOEXCHANGE:
888 case CHIOGELEM:
889 case CHIOINITELEM:
890 case CHIOSVOLTAG:
891 /* compatible */
892 return ch_ioctl(NULL /* inode, unused */,
893 file, cmd, arg);
894 case CHIOGSTATUS32:
896 struct changer_element_status32 ces32;
897 unsigned char __user *data;
899 if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
900 return -EFAULT;
901 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
902 return -EINVAL;
904 data = compat_ptr(ces32.ces_data);
905 return ch_gstatus(ch, ces32.ces_type, data);
907 default:
908 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
909 return -ENOIOCTLCMD;
913 #endif
915 /* ------------------------------------------------------------------------ */
917 static int ch_probe(struct device *dev)
919 struct scsi_device *sd = to_scsi_device(dev);
920 scsi_changer *ch;
922 if (sd->type != TYPE_MEDIUM_CHANGER)
923 return -ENODEV;
925 ch = kmalloc(sizeof(*ch), GFP_KERNEL);
926 if (NULL == ch)
927 return -ENOMEM;
929 memset(ch,0,sizeof(*ch));
930 ch->minor = ch_devcount;
931 sprintf(ch->name,"ch%d",ch->minor);
932 mutex_init(&ch->lock);
933 ch->device = sd;
934 ch_readconfig(ch);
935 if (init)
936 ch_init_elem(ch);
938 class_device_create(ch_sysfs_class, NULL,
939 MKDEV(SCSI_CHANGER_MAJOR,ch->minor),
940 dev, "s%s", ch->name);
942 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
944 spin_lock(&ch_devlist_lock);
945 list_add_tail(&ch->list,&ch_devlist);
946 ch_devcount++;
947 spin_unlock(&ch_devlist_lock);
948 return 0;
951 static int ch_remove(struct device *dev)
953 struct scsi_device *sd = to_scsi_device(dev);
954 scsi_changer *tmp, *ch;
956 spin_lock(&ch_devlist_lock);
957 ch = NULL;
958 list_for_each_entry(tmp,&ch_devlist,list) {
959 if (tmp->device == sd)
960 ch = tmp;
962 BUG_ON(NULL == ch);
963 list_del(&ch->list);
964 spin_unlock(&ch_devlist_lock);
966 class_device_destroy(ch_sysfs_class,
967 MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
968 kfree(ch->dt);
969 kfree(ch);
970 ch_devcount--;
971 return 0;
974 static int __init init_ch_module(void)
976 int rc;
978 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
979 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
980 if (IS_ERR(ch_sysfs_class)) {
981 rc = PTR_ERR(ch_sysfs_class);
982 return rc;
984 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
985 if (rc < 0) {
986 printk("Unable to get major %d for SCSI-Changer\n",
987 SCSI_CHANGER_MAJOR);
988 goto fail1;
990 rc = scsi_register_driver(&ch_template.gendrv);
991 if (rc < 0)
992 goto fail2;
993 return 0;
995 fail2:
996 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
997 fail1:
998 class_destroy(ch_sysfs_class);
999 return rc;
1002 static void __exit exit_ch_module(void)
1004 scsi_unregister_driver(&ch_template.gendrv);
1005 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1006 class_destroy(ch_sysfs_class);
1009 module_init(init_ch_module);
1010 module_exit(exit_ch_module);
1013 * Local variables:
1014 * c-basic-offset: 8
1015 * End: