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