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