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