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