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