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