blob: aa2011b6468387609993e2ab7643b8e40740c403 [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>
FUJITA Tomonorida707c52008-01-24 17:24:50 +090024#include <linux/idr.h>
Jonathan Corbetb0061a02008-05-16 14:12:52 -060025#include <linux/smp_lock.h>
Gerd Knorr daa6eda2005-05-10 10:59:13 +020026
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
FUJITA Tomonorida707c52008-01-24 17:24:50 +090038#define CH_MAX_DEVS 128
Gerd Knorr daa6eda2005-05-10 10:59:13 +020039
40MODULE_DESCRIPTION("device driver for scsi media changer devices");
41MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
42MODULE_LICENSE("GPL");
Rene Hermanf018fa52006-03-08 00:14:20 -080043MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
Gerd Knorr daa6eda2005-05-10 10:59:13 +020044
45static int init = 1;
46module_param(init, int, 0444);
47MODULE_PARM_DESC(init, \
48 "initialize element status on driver load (default: on)");
49
50static int timeout_move = 300;
51module_param(timeout_move, int, 0644);
52MODULE_PARM_DESC(timeout_move,"timeout for move commands "
53 "(default: 300 seconds)");
54
55static int timeout_init = 3600;
56module_param(timeout_init, int, 0644);
57MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
58 "(default: 3600 seconds)");
59
60static int verbose = 1;
61module_param(verbose, int, 0644);
62MODULE_PARM_DESC(verbose,"be verbose (default: on)");
63
64static int debug = 0;
65module_param(debug, int, 0644);
66MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
67 "detailed sense codes on scsi errors (default: off)");
68
69static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
70static int dt_lun[CH_DT_MAX];
71module_param_array(dt_id, int, NULL, 0444);
72module_param_array(dt_lun, int, NULL, 0444);
73
74/* tell the driver about vendor-specific slots */
75static int vendor_firsts[CH_TYPES-4];
76static int vendor_counts[CH_TYPES-4];
77module_param_array(vendor_firsts, int, NULL, 0444);
78module_param_array(vendor_counts, int, NULL, 0444);
79
Arjan van de Ven0ad78202005-11-28 16:22:25 +010080static const char * vendor_labels[CH_TYPES-4] = {
Gerd Knorr daa6eda2005-05-10 10:59:13 +020081 "v0", "v1", "v2", "v3"
82};
83// module_param_string_array(vendor_labels, NULL, 0444);
84
85#define dprintk(fmt, arg...) if (debug) \
86 printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg)
87#define vprintk(fmt, arg...) if (verbose) \
88 printk(KERN_INFO "%s: " fmt, ch->name , ## arg)
89
90/* ------------------------------------------------------------------- */
91
92#define MAX_RETRIES 1
93
Gerd Knorr 21feb5c2005-05-12 10:25:26 +020094static struct class * ch_sysfs_class;
Gerd Knorr daa6eda2005-05-10 10:59:13 +020095
96typedef struct {
97 struct list_head list;
98 int minor;
99 char name[8];
100 struct scsi_device *device;
101 struct scsi_device **dt; /* ptrs to data transfer elements */
102 u_int firsts[CH_TYPES];
103 u_int counts[CH_TYPES];
104 u_int unit_attention;
105 u_int voltags;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100106 struct mutex lock;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200107} scsi_changer;
108
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900109static DEFINE_IDR(ch_index_idr);
110static DEFINE_SPINLOCK(ch_index_lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200111
Arjan van de Ven0ad78202005-11-28 16:22:25 +0100112static const struct {
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200113 unsigned char sense;
114 unsigned char asc;
115 unsigned char ascq;
116 int errno;
Harvey Harrisond70d4662008-03-31 22:05:30 -0700117} ch_err[] = {
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200118/* Just filled in what looks right. Hav'nt checked any standard paper for
119 these errno assignments, so they may be wrong... */
120 {
121 .sense = ILLEGAL_REQUEST,
122 .asc = 0x21,
123 .ascq = 0x01,
124 .errno = EBADSLT, /* Invalid element address */
125 },{
126 .sense = ILLEGAL_REQUEST,
127 .asc = 0x28,
128 .ascq = 0x01,
129 .errno = EBADE, /* Import or export element accessed */
130 },{
131 .sense = ILLEGAL_REQUEST,
132 .asc = 0x3B,
133 .ascq = 0x0D,
134 .errno = EXFULL, /* Medium destination element full */
135 },{
136 .sense = ILLEGAL_REQUEST,
137 .asc = 0x3B,
138 .ascq = 0x0E,
139 .errno = EBADE, /* Medium source element empty */
140 },{
141 .sense = ILLEGAL_REQUEST,
142 .asc = 0x20,
143 .ascq = 0x00,
144 .errno = EBADRQC, /* Invalid command operation code */
145 },{
146 /* end of list */
147 }
148};
149
150/* ------------------------------------------------------------------- */
151
James Bottomley84743bb2005-06-12 22:37:10 -0500152static int ch_find_errno(struct scsi_sense_hdr *sshdr)
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200153{
154 int i,errno = 0;
155
156 /* Check to see if additional sense information is available */
James Bottomley84743bb2005-06-12 22:37:10 -0500157 if (scsi_sense_valid(sshdr) &&
158 sshdr->asc != 0) {
Harvey Harrisond70d4662008-03-31 22:05:30 -0700159 for (i = 0; ch_err[i].errno != 0; i++) {
160 if (ch_err[i].sense == sshdr->sense_key &&
161 ch_err[i].asc == sshdr->asc &&
162 ch_err[i].ascq == sshdr->ascq) {
163 errno = -ch_err[i].errno;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200164 break;
165 }
166 }
167 }
168 if (errno == 0)
169 errno = -EIO;
170 return errno;
171}
172
173static int
174ch_do_scsi(scsi_changer *ch, unsigned char *cmd,
175 void *buffer, unsigned buflength,
176 enum dma_data_direction direction)
177{
James Bottomley84743bb2005-06-12 22:37:10 -0500178 int errno, retries = 0, timeout, result;
179 struct scsi_sense_hdr sshdr;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900180
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200181 timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
182 ? timeout_init : timeout_move;
183
184 retry:
185 errno = 0;
186 if (debug) {
187 dprintk("command: ");
188 __scsi_print_command(cmd);
189 }
190
James Bottomley84743bb2005-06-12 22:37:10 -0500191 result = scsi_execute_req(ch->device, cmd, direction, buffer,
192 buflength, &sshdr, timeout * HZ,
193 MAX_RETRIES);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200194
James Bottomley84743bb2005-06-12 22:37:10 -0500195 dprintk("result: 0x%x\n",result);
196 if (driver_byte(result) & DRIVER_SENSE) {
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200197 if (debug)
James Bottomley84743bb2005-06-12 22:37:10 -0500198 scsi_print_sense_hdr(ch->name, &sshdr);
199 errno = ch_find_errno(&sshdr);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200200
James Bottomley84743bb2005-06-12 22:37:10 -0500201 switch(sshdr.sense_key) {
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200202 case UNIT_ATTENTION:
203 ch->unit_attention = 1;
204 if (retries++ < 3)
205 goto retry;
206 break;
207 }
208 }
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200209 return errno;
210}
211
212/* ------------------------------------------------------------------------ */
213
214static int
215ch_elem_to_typecode(scsi_changer *ch, u_int elem)
216{
217 int i;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900218
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200219 for (i = 0; i < CH_TYPES; i++) {
220 if (elem >= ch->firsts[i] &&
221 elem < ch->firsts[i] +
222 ch->counts[i])
223 return i+1;
224 }
225 return 0;
226}
227
228static int
229ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
230{
231 u_char cmd[12];
232 u_char *buffer;
233 int result;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900234
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200235 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
236 if(!buffer)
237 return -ENOMEM;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900238
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200239 retry:
240 memset(cmd,0,sizeof(cmd));
241 cmd[0] = READ_ELEMENT_STATUS;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900242 cmd[1] = (ch->device->lun << 5) |
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200243 (ch->voltags ? 0x10 : 0) |
244 ch_elem_to_typecode(ch,elem);
245 cmd[2] = (elem >> 8) & 0xff;
246 cmd[3] = elem & 0xff;
247 cmd[5] = 1;
248 cmd[9] = 255;
249 if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) {
250 if (((buffer[16] << 8) | buffer[17]) != elem) {
251 dprintk("asked for element 0x%02x, got 0x%02x\n",
252 elem,(buffer[16] << 8) | buffer[17]);
253 kfree(buffer);
254 return -EIO;
255 }
256 memcpy(data,buffer+16,16);
257 } else {
258 if (ch->voltags) {
259 ch->voltags = 0;
260 vprintk("device has no volume tag support\n");
261 goto retry;
262 }
263 dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem);
264 }
265 kfree(buffer);
266 return result;
267}
268
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900269static int
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200270ch_init_elem(scsi_changer *ch)
271{
272 int err;
273 u_char cmd[6];
274
275 vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n");
276 memset(cmd,0,sizeof(cmd));
277 cmd[0] = INITIALIZE_ELEMENT_STATUS;
278 cmd[1] = ch->device->lun << 5;
279 err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
280 vprintk("... finished\n");
281 return err;
282}
283
284static int
285ch_readconfig(scsi_changer *ch)
286{
287 u_char cmd[10], data[16];
288 u_char *buffer;
289 int result,id,lun,i;
290 u_int elem;
291
vignesh.babu@wipro.com4530a162007-04-16 11:35:33 +0530292 buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200293 if (!buffer)
294 return -ENOMEM;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900295
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200296 memset(cmd,0,sizeof(cmd));
297 cmd[0] = MODE_SENSE;
298 cmd[1] = ch->device->lun << 5;
299 cmd[2] = 0x1d;
300 cmd[4] = 255;
301 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
302 if (0 != result) {
303 cmd[1] |= (1<<3);
304 result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE);
305 }
306 if (0 == result) {
307 ch->firsts[CHET_MT] =
308 (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
309 ch->counts[CHET_MT] =
310 (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
311 ch->firsts[CHET_ST] =
312 (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
313 ch->counts[CHET_ST] =
314 (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
315 ch->firsts[CHET_IE] =
316 (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
317 ch->counts[CHET_IE] =
318 (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
319 ch->firsts[CHET_DT] =
320 (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
321 ch->counts[CHET_DT] =
322 (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
323 vprintk("type #1 (mt): 0x%x+%d [medium transport]\n",
324 ch->firsts[CHET_MT],
325 ch->counts[CHET_MT]);
326 vprintk("type #2 (st): 0x%x+%d [storage]\n",
327 ch->firsts[CHET_ST],
328 ch->counts[CHET_ST]);
329 vprintk("type #3 (ie): 0x%x+%d [import/export]\n",
330 ch->firsts[CHET_IE],
331 ch->counts[CHET_IE]);
332 vprintk("type #4 (dt): 0x%x+%d [data transfer]\n",
333 ch->firsts[CHET_DT],
334 ch->counts[CHET_DT]);
335 } else {
336 vprintk("reading element address assigment page failed!\n");
337 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900338
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200339 /* vendor specific element types */
340 for (i = 0; i < 4; i++) {
341 if (0 == vendor_counts[i])
342 continue;
343 if (NULL == vendor_labels[i])
344 continue;
345 ch->firsts[CHET_V1+i] = vendor_firsts[i];
346 ch->counts[CHET_V1+i] = vendor_counts[i];
347 vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
348 i+5,i+1,vendor_firsts[i],vendor_counts[i],
349 vendor_labels[i]);
350 }
351
352 /* look up the devices of the data transfer elements */
353 ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device),
354 GFP_KERNEL);
355 for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
356 id = -1;
357 lun = 0;
358 if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
359 id = dt_id[elem];
360 lun = dt_lun[elem];
361 vprintk("dt 0x%x: [insmod option] ",
362 elem+ch->firsts[CHET_DT]);
363 } else if (0 != ch_read_element_status
364 (ch,elem+ch->firsts[CHET_DT],data)) {
365 vprintk("dt 0x%x: READ ELEMENT STATUS failed\n",
366 elem+ch->firsts[CHET_DT]);
367 } else {
368 vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]);
369 if (data[6] & 0x80) {
370 if (verbose)
371 printk("not this SCSI bus\n");
372 ch->dt[elem] = NULL;
373 } else if (0 == (data[6] & 0x30)) {
374 if (verbose)
375 printk("ID/LUN unknown\n");
376 ch->dt[elem] = NULL;
377 } else {
378 id = ch->device->id;
379 lun = 0;
380 if (data[6] & 0x20) id = data[7];
381 if (data[6] & 0x10) lun = data[6] & 7;
382 }
383 }
384 if (-1 != id) {
385 if (verbose)
386 printk("ID %i, LUN %i, ",id,lun);
387 ch->dt[elem] =
388 scsi_device_lookup(ch->device->host,
389 ch->device->channel,
390 id,lun);
391 if (!ch->dt[elem]) {
392 /* should not happen */
393 if (verbose)
394 printk("Huh? device not found!\n");
395 } else {
396 if (verbose)
397 printk("name: %8.8s %16.16s %4.4s\n",
398 ch->dt[elem]->vendor,
399 ch->dt[elem]->model,
400 ch->dt[elem]->rev);
401 }
402 }
403 }
404 ch->voltags = 1;
405 kfree(buffer);
406
407 return 0;
408}
409
410/* ------------------------------------------------------------------------ */
411
412static int
413ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
414{
415 u_char cmd[10];
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900416
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200417 dprintk("position: 0x%x\n",elem);
418 if (0 == trans)
419 trans = ch->firsts[CHET_MT];
420 memset(cmd,0,sizeof(cmd));
421 cmd[0] = POSITION_TO_ELEMENT;
422 cmd[1] = ch->device->lun << 5;
423 cmd[2] = (trans >> 8) & 0xff;
424 cmd[3] = trans & 0xff;
425 cmd[4] = (elem >> 8) & 0xff;
426 cmd[5] = elem & 0xff;
427 cmd[8] = rotate ? 1 : 0;
428 return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE);
429}
430
431static int
432ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
433{
434 u_char cmd[12];
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900435
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200436 dprintk("move: 0x%x => 0x%x\n",src,dest);
437 if (0 == trans)
438 trans = ch->firsts[CHET_MT];
439 memset(cmd,0,sizeof(cmd));
440 cmd[0] = MOVE_MEDIUM;
441 cmd[1] = ch->device->lun << 5;
442 cmd[2] = (trans >> 8) & 0xff;
443 cmd[3] = trans & 0xff;
444 cmd[4] = (src >> 8) & 0xff;
445 cmd[5] = src & 0xff;
446 cmd[6] = (dest >> 8) & 0xff;
447 cmd[7] = dest & 0xff;
448 cmd[10] = rotate ? 1 : 0;
449 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
450}
451
452static int
453ch_exchange(scsi_changer *ch, u_int trans, u_int src,
454 u_int dest1, u_int dest2, int rotate1, int rotate2)
455{
456 u_char cmd[12];
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900457
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200458 dprintk("exchange: 0x%x => 0x%x => 0x%x\n",
459 src,dest1,dest2);
460 if (0 == trans)
461 trans = ch->firsts[CHET_MT];
462 memset(cmd,0,sizeof(cmd));
463 cmd[0] = EXCHANGE_MEDIUM;
464 cmd[1] = ch->device->lun << 5;
465 cmd[2] = (trans >> 8) & 0xff;
466 cmd[3] = trans & 0xff;
467 cmd[4] = (src >> 8) & 0xff;
468 cmd[5] = src & 0xff;
469 cmd[6] = (dest1 >> 8) & 0xff;
470 cmd[7] = dest1 & 0xff;
471 cmd[8] = (dest2 >> 8) & 0xff;
472 cmd[9] = dest2 & 0xff;
473 cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900474
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200475 return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE);
476}
477
478static void
479ch_check_voltag(char *tag)
480{
481 int i;
482
483 for (i = 0; i < 32; i++) {
484 /* restrict to ascii */
485 if (tag[i] >= 0x7f || tag[i] < 0x20)
486 tag[i] = ' ';
487 /* don't allow search wildcards */
488 if (tag[i] == '?' ||
489 tag[i] == '*')
490 tag[i] = ' ';
491 }
492}
493
494static int
495ch_set_voltag(scsi_changer *ch, u_int elem,
496 int alternate, int clear, u_char *tag)
497{
498 u_char cmd[12];
499 u_char *buffer;
500 int result;
501
vignesh.babu@wipro.com4530a162007-04-16 11:35:33 +0530502 buffer = kzalloc(512, GFP_KERNEL);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200503 if (!buffer)
504 return -ENOMEM;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200505
506 dprintk("%s %s voltag: 0x%x => \"%s\"\n",
507 clear ? "clear" : "set",
508 alternate ? "alternate" : "primary",
509 elem, tag);
510 memset(cmd,0,sizeof(cmd));
511 cmd[0] = SEND_VOLUME_TAG;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900512 cmd[1] = (ch->device->lun << 5) |
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200513 ch_elem_to_typecode(ch,elem);
514 cmd[2] = (elem >> 8) & 0xff;
515 cmd[3] = elem & 0xff;
516 cmd[5] = clear
517 ? (alternate ? 0x0d : 0x0c)
518 : (alternate ? 0x0b : 0x0a);
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900519
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200520 cmd[9] = 255;
521
522 memcpy(buffer,tag,32);
523 ch_check_voltag(buffer);
524
525 result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE);
526 kfree(buffer);
527 return result;
528}
529
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100530static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200531{
532 int retval = 0;
533 u_char data[16];
534 unsigned int i;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900535
Arjan van de Ven0b950672006-01-11 13:16:10 +0100536 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200537 for (i = 0; i < ch->counts[type]; i++) {
538 if (0 != ch_read_element_status
539 (ch, ch->firsts[type]+i,data)) {
540 retval = -EIO;
541 break;
542 }
543 put_user(data[2], dest+i);
544 if (data[2] & CESTATUS_EXCEPT)
545 vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n",
546 ch->firsts[type]+i,
547 (int)data[4],(int)data[5]);
548 retval = ch_read_element_status
549 (ch, ch->firsts[type]+i,data);
550 if (0 != retval)
551 break;
552 }
Arjan van de Ven0b950672006-01-11 13:16:10 +0100553 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200554 return retval;
555}
556
557/* ------------------------------------------------------------------------ */
558
559static int
560ch_release(struct inode *inode, struct file *file)
561{
562 scsi_changer *ch = file->private_data;
563
564 scsi_device_put(ch->device);
565 file->private_data = NULL;
566 return 0;
567}
568
569static int
570ch_open(struct inode *inode, struct file *file)
571{
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900572 scsi_changer *ch;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200573 int minor = iminor(inode);
574
Jonathan Corbetb0061a02008-05-16 14:12:52 -0600575 lock_kernel();
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900576 spin_lock(&ch_index_lock);
577 ch = idr_find(&ch_index_idr, minor);
578
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200579 if (NULL == ch || scsi_device_get(ch->device)) {
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900580 spin_unlock(&ch_index_lock);
Jonathan Corbetb0061a02008-05-16 14:12:52 -0600581 unlock_kernel();
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200582 return -ENXIO;
583 }
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900584 spin_unlock(&ch_index_lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200585
586 file->private_data = ch;
Jonathan Corbetb0061a02008-05-16 14:12:52 -0600587 unlock_kernel();
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200588 return 0;
589}
590
591static int
592ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
593{
594 if (type >= CH_TYPES || unit >= ch->counts[type])
595 return -1;
596 return 0;
597}
598
Mathieu Segaudf7fea182008-01-14 15:43:18 +0100599static long ch_ioctl(struct file *file,
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200600 unsigned int cmd, unsigned long arg)
601{
602 scsi_changer *ch = file->private_data;
603 int retval;
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100604 void __user *argp = (void __user *)arg;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900605
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200606 switch (cmd) {
607 case CHIOGPARAMS:
608 {
609 struct changer_params params;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900610
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200611 params.cp_curpicker = 0;
612 params.cp_npickers = ch->counts[CHET_MT];
613 params.cp_nslots = ch->counts[CHET_ST];
614 params.cp_nportals = ch->counts[CHET_IE];
615 params.cp_ndrives = ch->counts[CHET_DT];
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900616
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100617 if (copy_to_user(argp, &params, sizeof(params)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200618 return -EFAULT;
619 return 0;
620 }
621 case CHIOGVPARAMS:
622 {
623 struct changer_vendor_params vparams;
624
625 memset(&vparams,0,sizeof(vparams));
626 if (ch->counts[CHET_V1]) {
627 vparams.cvp_n1 = ch->counts[CHET_V1];
628 strncpy(vparams.cvp_label1,vendor_labels[0],16);
629 }
630 if (ch->counts[CHET_V2]) {
631 vparams.cvp_n2 = ch->counts[CHET_V2];
632 strncpy(vparams.cvp_label2,vendor_labels[1],16);
633 }
634 if (ch->counts[CHET_V3]) {
635 vparams.cvp_n3 = ch->counts[CHET_V3];
636 strncpy(vparams.cvp_label3,vendor_labels[2],16);
637 }
638 if (ch->counts[CHET_V4]) {
639 vparams.cvp_n4 = ch->counts[CHET_V4];
640 strncpy(vparams.cvp_label4,vendor_labels[3],16);
641 }
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100642 if (copy_to_user(argp, &vparams, sizeof(vparams)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200643 return -EFAULT;
644 return 0;
645 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900646
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200647 case CHIOPOSITION:
648 {
649 struct changer_position pos;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900650
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100651 if (copy_from_user(&pos, argp, sizeof (pos)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200652 return -EFAULT;
653
654 if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
655 dprintk("CHIOPOSITION: invalid parameter\n");
656 return -EBADSLT;
657 }
Arjan van de Ven0b950672006-01-11 13:16:10 +0100658 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200659 retval = ch_position(ch,0,
660 ch->firsts[pos.cp_type] + pos.cp_unit,
661 pos.cp_flags & CP_INVERT);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100662 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200663 return retval;
664 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900665
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200666 case CHIOMOVE:
667 {
668 struct changer_move mv;
669
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100670 if (copy_from_user(&mv, argp, sizeof (mv)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200671 return -EFAULT;
672
673 if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
674 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
675 dprintk("CHIOMOVE: invalid parameter\n");
676 return -EBADSLT;
677 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900678
Arjan van de Ven0b950672006-01-11 13:16:10 +0100679 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200680 retval = ch_move(ch,0,
681 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
682 ch->firsts[mv.cm_totype] + mv.cm_tounit,
683 mv.cm_flags & CM_INVERT);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100684 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200685 return retval;
686 }
687
688 case CHIOEXCHANGE:
689 {
690 struct changer_exchange mv;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900691
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100692 if (copy_from_user(&mv, argp, sizeof (mv)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200693 return -EFAULT;
694
695 if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
696 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
697 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
698 dprintk("CHIOEXCHANGE: invalid parameter\n");
699 return -EBADSLT;
700 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900701
Arjan van de Ven0b950672006-01-11 13:16:10 +0100702 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200703 retval = ch_exchange
704 (ch,0,
705 ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
706 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
707 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
708 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100709 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200710 return retval;
711 }
712
713 case CHIOGSTATUS:
714 {
715 struct changer_element_status ces;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900716
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100717 if (copy_from_user(&ces, argp, sizeof (ces)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200718 return -EFAULT;
719 if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
720 return -EINVAL;
721
722 return ch_gstatus(ch, ces.ces_type, ces.ces_data);
723 }
724
725 case CHIOGELEM:
726 {
727 struct changer_get_element cge;
Harvey Harrisond70d4662008-03-31 22:05:30 -0700728 u_char ch_cmd[12];
729 u_char *buffer;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200730 unsigned int elem;
731 int result,i;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900732
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100733 if (copy_from_user(&cge, argp, sizeof (cge)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200734 return -EFAULT;
735
736 if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
737 return -EINVAL;
738 elem = ch->firsts[cge.cge_type] + cge.cge_unit;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900739
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200740 buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
741 if (!buffer)
742 return -ENOMEM;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100743 mutex_lock(&ch->lock);
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900744
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200745 voltag_retry:
Harvey Harrisond70d4662008-03-31 22:05:30 -0700746 memset(ch_cmd, 0, sizeof(ch_cmd));
747 ch_cmd[0] = READ_ELEMENT_STATUS;
748 ch_cmd[1] = (ch->device->lun << 5) |
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200749 (ch->voltags ? 0x10 : 0) |
750 ch_elem_to_typecode(ch,elem);
Harvey Harrisond70d4662008-03-31 22:05:30 -0700751 ch_cmd[2] = (elem >> 8) & 0xff;
752 ch_cmd[3] = elem & 0xff;
753 ch_cmd[5] = 1;
754 ch_cmd[9] = 255;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900755
Harvey Harrisond70d4662008-03-31 22:05:30 -0700756 result = ch_do_scsi(ch, ch_cmd, buffer, 256, DMA_FROM_DEVICE);
757 if (!result) {
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200758 cge.cge_status = buffer[18];
759 cge.cge_flags = 0;
760 if (buffer[18] & CESTATUS_EXCEPT) {
761 cge.cge_errno = EIO;
762 }
763 if (buffer[25] & 0x80) {
764 cge.cge_flags |= CGE_SRC;
765 if (buffer[25] & 0x40)
766 cge.cge_flags |= CGE_INVERT;
767 elem = (buffer[26]<<8) | buffer[27];
768 for (i = 0; i < 4; i++) {
769 if (elem >= ch->firsts[i] &&
770 elem < ch->firsts[i] + ch->counts[i]) {
771 cge.cge_srctype = i;
772 cge.cge_srcunit = elem-ch->firsts[i];
773 }
774 }
775 }
776 if ((buffer[22] & 0x30) == 0x30) {
777 cge.cge_flags |= CGE_IDLUN;
778 cge.cge_id = buffer[23];
779 cge.cge_lun = buffer[22] & 7;
780 }
781 if (buffer[9] & 0x80) {
782 cge.cge_flags |= CGE_PVOLTAG;
783 memcpy(cge.cge_pvoltag,buffer+28,36);
784 }
785 if (buffer[9] & 0x40) {
786 cge.cge_flags |= CGE_AVOLTAG;
787 memcpy(cge.cge_avoltag,buffer+64,36);
788 }
789 } else if (ch->voltags) {
790 ch->voltags = 0;
791 vprintk("device has no volume tag support\n");
792 goto voltag_retry;
793 }
794 kfree(buffer);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100795 mutex_unlock(&ch->lock);
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900796
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100797 if (copy_to_user(argp, &cge, sizeof (cge)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200798 return -EFAULT;
799 return result;
800 }
801
802 case CHIOINITELEM:
803 {
Arjan van de Ven0b950672006-01-11 13:16:10 +0100804 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200805 retval = ch_init_elem(ch);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100806 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200807 return retval;
808 }
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900809
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200810 case CHIOSVOLTAG:
811 {
812 struct changer_set_voltag csv;
813 int elem;
814
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100815 if (copy_from_user(&csv, argp, sizeof(csv)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200816 return -EFAULT;
817
818 if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
819 dprintk("CHIOSVOLTAG: invalid parameter\n");
820 return -EBADSLT;
821 }
822 elem = ch->firsts[csv.csv_type] + csv.csv_unit;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100823 mutex_lock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200824 retval = ch_set_voltag(ch, elem,
825 csv.csv_flags & CSV_AVOLTAG,
826 csv.csv_flags & CSV_CLEARTAG,
827 csv.csv_voltag);
Arjan van de Ven0b950672006-01-11 13:16:10 +0100828 mutex_unlock(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200829 return retval;
830 }
831
832 default:
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100833 return scsi_ioctl(ch->device, cmd, argp);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200834
835 }
836}
837
838#ifdef CONFIG_COMPAT
839
840struct changer_element_status32 {
841 int ces_type;
842 compat_uptr_t ces_data;
843};
844#define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32)
845
846static long ch_ioctl_compat(struct file * file,
847 unsigned int cmd, unsigned long arg)
848{
849 scsi_changer *ch = file->private_data;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900850
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200851 switch (cmd) {
852 case CHIOGPARAMS:
853 case CHIOGVPARAMS:
854 case CHIOPOSITION:
855 case CHIOMOVE:
856 case CHIOEXCHANGE:
857 case CHIOGELEM:
858 case CHIOINITELEM:
859 case CHIOSVOLTAG:
860 /* compatible */
Mathieu Segaudf7fea182008-01-14 15:43:18 +0100861 return ch_ioctl(file, cmd, arg);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200862 case CHIOGSTATUS32:
863 {
864 struct changer_element_status32 ces32;
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100865 unsigned char __user *data;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900866
viro@ZenIV.linux.org.ukfe08ac32005-09-09 22:03:44 +0100867 if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200868 return -EFAULT;
869 if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
870 return -EINVAL;
871
872 data = compat_ptr(ces32.ces_data);
873 return ch_gstatus(ch, ces32.ces_type, data);
874 }
875 default:
876 // return scsi_ioctl_compat(ch->device, cmd, (void*)arg);
877 return -ENOIOCTLCMD;
878
879 }
880}
881#endif
882
883/* ------------------------------------------------------------------------ */
884
885static int ch_probe(struct device *dev)
886{
887 struct scsi_device *sd = to_scsi_device(dev);
Tony Jonesee959b02008-02-22 00:13:36 +0100888 struct device *class_dev;
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900889 int minor, ret = -ENOMEM;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200890 scsi_changer *ch;
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900891
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200892 if (sd->type != TYPE_MEDIUM_CHANGER)
893 return -ENODEV;
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900894
vignesh.babu@wipro.com4530a162007-04-16 11:35:33 +0530895 ch = kzalloc(sizeof(*ch), GFP_KERNEL);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200896 if (NULL == ch)
897 return -ENOMEM;
898
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900899 if (!idr_pre_get(&ch_index_idr, GFP_KERNEL))
900 goto free_ch;
901
902 spin_lock(&ch_index_lock);
903 ret = idr_get_new(&ch_index_idr, ch, &minor);
904 spin_unlock(&ch_index_lock);
905
906 if (ret)
907 goto free_ch;
908
909 if (minor > CH_MAX_DEVS) {
910 ret = -ENODEV;
911 goto remove_idr;
912 }
913
914 ch->minor = minor;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200915 sprintf(ch->name,"ch%d",ch->minor);
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900916
Greg Kroah-Hartman24b42562008-05-16 17:55:12 -0700917 class_dev = device_create_drvdata(ch_sysfs_class, dev,
918 MKDEV(SCSI_CHANGER_MAJOR, ch->minor),
919 ch, "s%s", ch->name);
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900920 if (IS_ERR(class_dev)) {
Tony Jonesee959b02008-02-22 00:13:36 +0100921 printk(KERN_WARNING "ch%d: device_create failed\n",
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900922 ch->minor);
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900923 ret = PTR_ERR(class_dev);
924 goto remove_idr;
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900925 }
926
Arjan van de Ven0b950672006-01-11 13:16:10 +0100927 mutex_init(&ch->lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200928 ch->device = sd;
929 ch_readconfig(ch);
930 if (init)
931 ch_init_elem(ch);
932
Jeff Garzik017560f2005-10-24 18:04:36 -0400933 sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
FUJITA Tomonoria3d2c2e2008-01-23 23:34:35 +0900934
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200935 return 0;
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900936remove_idr:
937 idr_remove(&ch_index_idr, minor);
938free_ch:
939 kfree(ch);
940 return ret;
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200941}
942
943static int ch_remove(struct device *dev)
944{
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900945 scsi_changer *ch = dev_get_drvdata(dev);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200946
FUJITA Tomonorida707c52008-01-24 17:24:50 +0900947 spin_lock(&ch_index_lock);
948 idr_remove(&ch_index_idr, ch->minor);
949 spin_unlock(&ch_index_lock);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200950
Tony Jonesee959b02008-02-22 00:13:36 +0100951 device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200952 kfree(ch->dt);
953 kfree(ch);
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200954 return 0;
955}
956
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900957static struct scsi_driver ch_template = {
958 .owner = THIS_MODULE,
959 .gendrv = {
960 .name = "ch",
961 .probe = ch_probe,
962 .remove = ch_remove,
963 },
964};
965
966static const struct file_operations changer_fops = {
967 .owner = THIS_MODULE,
968 .open = ch_open,
969 .release = ch_release,
970 .unlocked_ioctl = ch_ioctl,
971#ifdef CONFIG_COMPAT
972 .compat_ioctl = ch_ioctl_compat,
973#endif
974};
975
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200976static int __init init_ch_module(void)
977{
978 int rc;
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +0900979
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200980 printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
Gerd Knorr 21feb5c2005-05-12 10:25:26 +0200981 ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
Gerd Knorr daa6eda2005-05-10 10:59:13 +0200982 if (IS_ERR(ch_sysfs_class)) {
983 rc = PTR_ERR(ch_sysfs_class);
984 return rc;
985 }
986 rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
987 if (rc < 0) {
988 printk("Unable to get major %d for SCSI-Changer\n",
989 SCSI_CHANGER_MAJOR);
990 goto fail1;
991 }
992 rc = scsi_register_driver(&ch_template.gendrv);
993 if (rc < 0)
994 goto fail2;
995 return 0;
996
997 fail2:
998 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
999 fail1:
Gerd Knorr 21feb5c2005-05-12 10:25:26 +02001000 class_destroy(ch_sysfs_class);
Gerd Knorr daa6eda2005-05-10 10:59:13 +02001001 return rc;
1002}
1003
FUJITA Tomonori5aa22af2008-01-24 17:24:52 +09001004static void __exit exit_ch_module(void)
Gerd Knorr daa6eda2005-05-10 10:59:13 +02001005{
1006 scsi_unregister_driver(&ch_template.gendrv);
1007 unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
Gerd Knorr 21feb5c2005-05-12 10:25:26 +02001008 class_destroy(ch_sysfs_class);
FUJITA Tomonorida707c52008-01-24 17:24:50 +09001009 idr_destroy(&ch_index_idr);
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 */