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