iwlwifi: introduce host commands callbacks
[linux/fpc-iii.git] / drivers / scsi / ch.c
blob7aad15436d24d25002cfacee35837401db5e5bee
1 /*
2 * SCSI Media Changer device driver for Linux 2.6
4 * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
6 */
8 #define VERSION "0.25"
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>
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>
35 #define CH_DT_MAX 16
36 #define CH_TYPES 8
37 #define CH_MAX_DEVS 128
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);
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)");
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)");
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)");
59 static int verbose = 1;
60 module_param(verbose, int, 0644);
61 MODULE_PARM_DESC(verbose,"be verbose (default: on)");
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)");
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);
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);
79 static const char * vendor_labels[CH_TYPES-4] = {
80 "v0", "v1", "v2", "v3"
82 // module_param_string_array(vendor_labels, NULL, 0444);
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)
89 /* ------------------------------------------------------------------- */
91 #define MAX_RETRIES 1
93 static struct class * ch_sysfs_class;
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;
108 static DEFINE_IDR(ch_index_idr);
109 static DEFINE_SPINLOCK(ch_index_lock);
111 static const struct {
112 unsigned char sense;
113 unsigned char asc;
114 unsigned char ascq;
115 int errno;
116 } err[] = {
117 /* Just filled in what looks right. Hav'nt checked any standard paper for
118 these errno assignments, so they may be wrong... */
120 .sense = ILLEGAL_REQUEST,
121 .asc = 0x21,
122 .ascq = 0x01,
123 .errno = EBADSLT, /* Invalid element address */
125 .sense = ILLEGAL_REQUEST,
126 .asc = 0x28,
127 .ascq = 0x01,
128 .errno = EBADE, /* Import or export element accessed */
130 .sense = ILLEGAL_REQUEST,
131 .asc = 0x3B,
132 .ascq = 0x0D,
133 .errno = EXFULL, /* Medium destination element full */
135 .sense = ILLEGAL_REQUEST,
136 .asc = 0x3B,
137 .ascq = 0x0E,
138 .errno = EBADE, /* Medium source element empty */
140 .sense = ILLEGAL_REQUEST,
141 .asc = 0x20,
142 .ascq = 0x00,
143 .errno = EBADRQC, /* Invalid command operation code */
145 /* end of list */
149 /* ------------------------------------------------------------------- */
151 static int ch_find_errno(struct scsi_sense_hdr *sshdr)
153 int i,errno = 0;
155 /* Check to see if additional sense information is available */
156 if (scsi_sense_valid(sshdr) &&
157 sshdr->asc != 0) {
158 for (i = 0; err[i].errno != 0; i++) {
159 if (err[i].sense == sshdr->sense_key &&
160 err[i].asc == sshdr->asc &&
161 err[i].ascq == sshdr->ascq) {
162 errno = -err[i].errno;
163 break;
167 if (errno == 0)
168 errno = -EIO;
169 return errno;
172 static int
173 ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
174 void *buffer, unsigned buflength,
175 enum dma_data_direction direction)
177 int errno, retries = 0, timeout, result;
178 struct scsi_sense_hdr sshdr;
180 timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
181 ? timeout_init : timeout_move;
183 retry:
184 errno = 0;
185 if (debug) {
186 dprintk("command: ");
187 __scsi_print_command(cmd);
190 result = scsi_execute_req(ch->device, cmd, direction, buffer,
191 buflength, &sshdr, timeout * HZ,
192 MAX_RETRIES);
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);
200 switch(sshdr.sense_key) {
201 case UNIT_ATTENTION:
202 ch->unit_attention = 1;
203 if (retries++ < 3)
204 goto retry;
205 break;
208 return errno;
211 /* ------------------------------------------------------------------------ */
213 static int
214 ch_elem_to_typecode(scsi_changer *ch, u_int elem)
216 int i;
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;
224 return 0;
227 static int
228 ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
230 u_char cmd[12];
231 u_char *buffer;
232 int result;
234 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
235 if(!buffer)
236 return -ENOMEM;
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;
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;
262 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
264 kfree(buffer);
265 return result;
268 static int
269 ch_init_elem(scsi_changer *ch)
271 int err;
272 u_char cmd[6];
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;
283 static int
284 ch_readconfig(scsi_changer *ch)
286 u_char cmd[10], data[16];
287 u_char *buffer;
288 int result,id,lun,i;
289 u_int elem;
291 buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
292 if (!buffer)
293 return -ENOMEM;
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);
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");
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]);
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;
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);
403 ch->voltags = 1;
404 kfree(buffer);
406 return 0;
409 /* ------------------------------------------------------------------------ */
411 static int
412 ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
414 u_char cmd[10];
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);
430 static int
431 ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
433 u_char cmd[12];
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);
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)
455 u_char cmd[12];
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);
474 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
477 static void
478 ch_check_voltag(char *tag)
480 int i;
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] = ' ';
493 static int
494 ch_set_voltag(scsi_changer *ch, u_int elem,
495 int alternate, int clear, u_char *tag)
497 u_char cmd[12];
498 u_char *buffer;
499 int result;
501 buffer = kzalloc(512, GFP_KERNEL);
502 if (!buffer)
503 return -ENOMEM;
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);
519 cmd[9] = 255;
521 memcpy(buffer,tag,32);
522 ch_check_voltag(buffer);
524 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
525 kfree(buffer);
526 return result;
529 static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
531 int retval = 0;
532 u_char data[16];
533 unsigned int i;
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;
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;
552 mutex_unlock(&ch->lock);
553 return retval;
556 /* ------------------------------------------------------------------------ */
558 static int
559 ch_release(struct inode *inode, struct file *file)
561 scsi_changer *ch = file->private_data;
563 scsi_device_put(ch->device);
564 file->private_data = NULL;
565 return 0;
568 static int
569 ch_open(struct inode *inode, struct file *file)
571 scsi_changer *ch;
572 int minor = iminor(inode);
574 spin_lock(&ch_index_lock);
575 ch = idr_find(&ch_index_idr, minor);
577 if (NULL == ch || scsi_device_get(ch->device)) {
578 spin_unlock(&ch_index_lock);
579 return -ENXIO;
581 spin_unlock(&ch_index_lock);
583 file->private_data = ch;
584 return 0;
587 static int
588 ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
590 if (type >= CH_TYPES || unit >= ch->counts[type])
591 return -1;
592 return 0;
595 static long ch_ioctl(struct file *file,
596 unsigned int cmd, unsigned long arg)
598 scsi_changer *ch = file->private_data;
599 int retval;
600 void __user *argp = (void __user *)arg;
602 switch (cmd) {
603 case CHIOGPARAMS:
605 struct changer_params params;
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];
613 if (copy_to_user(argp, &params, sizeof(params)))
614 return -EFAULT;
615 return 0;
617 case CHIOGVPARAMS:
619 struct changer_vendor_params vparams;
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);
626 if (ch->counts[CHET_V2]) {
627 vparams.cvp_n2 = ch->counts[CHET_V2];
628 strncpy(vparams.cvp_label2,vendor_labels[1],16);
630 if (ch->counts[CHET_V3]) {
631 vparams.cvp_n3 = ch->counts[CHET_V3];
632 strncpy(vparams.cvp_label3,vendor_labels[2],16);
634 if (ch->counts[CHET_V4]) {
635 vparams.cvp_n4 = ch->counts[CHET_V4];
636 strncpy(vparams.cvp_label4,vendor_labels[3],16);
638 if (copy_to_user(argp, &vparams, sizeof(vparams)))
639 return -EFAULT;
640 return 0;
643 case CHIOPOSITION:
645 struct changer_position pos;
647 if (copy_from_user(&pos, argp, sizeof (pos)))
648 return -EFAULT;
650 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
651 dprintk("CHIOPOSITION: invalid parameter\n");
652 return -EBADSLT;
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;
662 case CHIOMOVE:
664 struct changer_move mv;
666 if (copy_from_user(&mv, argp, sizeof (mv)))
667 return -EFAULT;
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;
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;
684 case CHIOEXCHANGE:
686 struct changer_exchange mv;
688 if (copy_from_user(&mv, argp, sizeof (mv)))
689 return -EFAULT;
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;
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;
709 case CHIOGSTATUS:
711 struct changer_element_status ces;
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;
718 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
721 case CHIOGELEM:
723 struct changer_get_element cge;
724 u_char cmd[12];
725 u_char *buffer;
726 unsigned int elem;
727 int result,i;
729 if (copy_from_user(&cge, argp, sizeof (cge)))
730 return -EFAULT;
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;
736 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
737 if (!buffer)
738 return -ENOMEM;
739 mutex_lock(&ch->lock);
741 voltag_retry:
742 memset(cmd,0,sizeof(cmd));
743 cmd[0] = READ_ELEMENT_STATUS;
744 cmd[1] = (ch->device->lun << 5) |
745 (ch->voltags ? 0x10 : 0) |
746 ch_elem_to_typecode(ch,elem);
747 cmd[2] = (elem >> 8) & 0xff;
748 cmd[3] = elem & 0xff;
749 cmd[5] = 1;
750 cmd[9] = 255;
752 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
753 cge.cge_status = buffer[18];
754 cge.cge_flags = 0;
755 if (buffer[18] & CESTATUS_EXCEPT) {
756 cge.cge_errno = EIO;
758 if (buffer[25] & 0x80) {
759 cge.cge_flags |= CGE_SRC;
760 if (buffer[25] & 0x40)
761 cge.cge_flags |= CGE_INVERT;
762 elem = (buffer[26]<<8) | buffer[27];
763 for (i = 0; i < 4; i++) {
764 if (elem >= ch->firsts[i] &&
765 elem < ch->firsts[i] + ch->counts[i]) {
766 cge.cge_srctype = i;
767 cge.cge_srcunit = elem-ch->firsts[i];
771 if ((buffer[22] & 0x30) == 0x30) {
772 cge.cge_flags |= CGE_IDLUN;
773 cge.cge_id = buffer[23];
774 cge.cge_lun = buffer[22] & 7;
776 if (buffer[9] & 0x80) {
777 cge.cge_flags |= CGE_PVOLTAG;
778 memcpy(cge.cge_pvoltag,buffer+28,36);
780 if (buffer[9] & 0x40) {
781 cge.cge_flags |= CGE_AVOLTAG;
782 memcpy(cge.cge_avoltag,buffer+64,36);
784 } else if (ch->voltags) {
785 ch->voltags = 0;
786 vprintk("device has no volume tag support\n");
787 goto voltag_retry;
789 kfree(buffer);
790 mutex_unlock(&ch->lock);
792 if (copy_to_user(argp, &cge, sizeof (cge)))
793 return -EFAULT;
794 return result;
797 case CHIOINITELEM:
799 mutex_lock(&ch->lock);
800 retval = ch_init_elem(ch);
801 mutex_unlock(&ch->lock);
802 return retval;
805 case CHIOSVOLTAG:
807 struct changer_set_voltag csv;
808 int elem;
810 if (copy_from_user(&csv, argp, sizeof(csv)))
811 return -EFAULT;
813 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
814 dprintk("CHIOSVOLTAG: invalid parameter\n");
815 return -EBADSLT;
817 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
818 mutex_lock(&ch->lock);
819 retval = ch_set_voltag(ch, elem,
820 csv.csv_flags & CSV_AVOLTAG,
821 csv.csv_flags & CSV_CLEARTAG,
822 csv.csv_voltag);
823 mutex_unlock(&ch->lock);
824 return retval;
827 default:
828 return scsi_ioctl(ch->device, cmd, argp);
833 #ifdef CONFIG_COMPAT
835 struct changer_element_status32 {
836 int ces_type;
837 compat_uptr_t ces_data;
839 #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
841 static long ch_ioctl_compat(struct file * file,
842 unsigned int cmd, unsigned long arg)
844 scsi_changer *ch = file->private_data;
846 switch (cmd) {
847 case CHIOGPARAMS:
848 case CHIOGVPARAMS:
849 case CHIOPOSITION:
850 case CHIOMOVE:
851 case CHIOEXCHANGE:
852 case CHIOGELEM:
853 case CHIOINITELEM:
854 case CHIOSVOLTAG:
855 /* compatible */
856 return ch_ioctl(file, cmd, arg);
857 case CHIOGSTATUS32:
859 struct changer_element_status32 ces32;
860 unsigned char __user *data;
862 if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
863 return -EFAULT;
864 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
865 return -EINVAL;
867 data = compat_ptr(ces32.ces_data);
868 return ch_gstatus(ch, ces32.ces_type, data);
870 default:
871 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
872 return -ENOIOCTLCMD;
876 #endif
878 /* ------------------------------------------------------------------------ */
880 static int ch_probe(struct device *dev)
882 struct scsi_device *sd = to_scsi_device(dev);
883 struct class_device *class_dev;
884 int minor, ret = -ENOMEM;
885 scsi_changer *ch;
887 if (sd->type != TYPE_MEDIUM_CHANGER)
888 return -ENODEV;
890 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
891 if (NULL == ch)
892 return -ENOMEM;
894 if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
895 goto free_ch;
897 spin_lock(&ch_index_lock);
898 ret = idr_get_new(&ch_index_idr, ch, &minor);
899 spin_unlock(&ch_index_lock);
901 if (ret)
902 goto free_ch;
904 if (minor > CH_MAX_DEVS) {
905 ret = -ENODEV;
906 goto remove_idr;
909 ch->minor = minor;
910 sprintf(ch->name,"ch%d",ch->minor);
912 class_dev = class_device_create(ch_sysfs_class, NULL,
913 MKDEV(SCSI_CHANGER_MAJOR, ch->minor),
914 dev, "s%s", ch->name);
915 if (IS_ERR(class_dev)) {
916 printk(KERN_WARNING "ch%d: class_device_create failed\n",
917 ch->minor);
918 ret = PTR_ERR(class_dev);
919 goto remove_idr;
922 mutex_init(&ch->lock);
923 ch->device = sd;
924 ch_readconfig(ch);
925 if (init)
926 ch_init_elem(ch);
928 dev_set_drvdata(dev, ch);
929 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
931 return 0;
932 remove_idr:
933 idr_remove(&ch_index_idr, minor);
934 free_ch:
935 kfree(ch);
936 return ret;
939 static int ch_remove(struct device *dev)
941 scsi_changer *ch = dev_get_drvdata(dev);
943 spin_lock(&ch_index_lock);
944 idr_remove(&ch_index_idr, ch->minor);
945 spin_unlock(&ch_index_lock);
947 class_device_destroy(ch_sysfs_class,
948 MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
949 kfree(ch->dt);
950 kfree(ch);
951 return 0;
954 static struct scsi_driver ch_template = {
955 .owner = THIS_MODULE,
956 .gendrv = {
957 .name = "ch",
958 .probe = ch_probe,
959 .remove = ch_remove,
963 static const struct file_operations changer_fops = {
964 .owner = THIS_MODULE,
965 .open = ch_open,
966 .release = ch_release,
967 .unlocked_ioctl = ch_ioctl,
968 #ifdef CONFIG_COMPAT
969 .compat_ioctl = ch_ioctl_compat,
970 #endif
973 static int __init init_ch_module(void)
975 int rc;
977 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
978 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
979 if (IS_ERR(ch_sysfs_class)) {
980 rc = PTR_ERR(ch_sysfs_class);
981 return rc;
983 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
984 if (rc < 0) {
985 printk("Unable to get major %d for SCSI-Changer\n",
986 SCSI_CHANGER_MAJOR);
987 goto fail1;
989 rc = scsi_register_driver(&ch_template.gendrv);
990 if (rc < 0)
991 goto fail2;
992 return 0;
994 fail2:
995 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
996 fail1:
997 class_destroy(ch_sysfs_class);
998 return rc;
1001 static void __exit exit_ch_module(void)
1003 scsi_unregister_driver(&ch_template.gendrv);
1004 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1005 class_destroy(ch_sysfs_class);
1006 idr_destroy(&ch_index_idr);
1009 module_init(init_ch_module);
1010 module_exit(exit_ch_module);
1013 * Local variables:
1014 * c-basic-offset: 8
1015 * End: