xref: /openbmc/linux/drivers/scsi/ch.c (revision ced202f7)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2daa6eda6SGerd Knorr  /*
3daa6eda6SGerd Knorr   * SCSI Media Changer device driver for Linux 2.6
4daa6eda6SGerd Knorr   *
5daa6eda6SGerd Knorr   *     (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
6daa6eda6SGerd Knorr   *
7daa6eda6SGerd Knorr   */
8daa6eda6SGerd Knorr  
9daa6eda6SGerd Knorr  #define VERSION "0.25"
10daa6eda6SGerd Knorr  
11daa6eda6SGerd Knorr  #include <linux/module.h>
12daa6eda6SGerd Knorr  #include <linux/init.h>
13daa6eda6SGerd Knorr  #include <linux/fs.h>
14daa6eda6SGerd Knorr  #include <linux/kernel.h>
15daa6eda6SGerd Knorr  #include <linux/mm.h>
16daa6eda6SGerd Knorr  #include <linux/major.h>
17daa6eda6SGerd Knorr  #include <linux/string.h>
18daa6eda6SGerd Knorr  #include <linux/errno.h>
19daa6eda6SGerd Knorr  #include <linux/interrupt.h>
20daa6eda6SGerd Knorr  #include <linux/blkdev.h>
21daa6eda6SGerd Knorr  #include <linux/completion.h>
22daa6eda6SGerd Knorr  #include <linux/compat.h>
23daa6eda6SGerd Knorr  #include <linux/chio.h>			/* here are all the ioctls */
240b950672SArjan van de Ven #include <linux/mutex.h>
25da707c54SFUJITA Tomonori #include <linux/idr.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
27daa6eda6SGerd Knorr  
28daa6eda6SGerd Knorr  #include <scsi/scsi.h>
29daa6eda6SGerd Knorr  #include <scsi/scsi_cmnd.h>
30daa6eda6SGerd Knorr  #include <scsi/scsi_driver.h>
31daa6eda6SGerd Knorr  #include <scsi/scsi_ioctl.h>
32daa6eda6SGerd Knorr  #include <scsi/scsi_host.h>
33daa6eda6SGerd Knorr  #include <scsi/scsi_device.h>
3484743bbcSJames Bottomley #include <scsi/scsi_eh.h>
35daa6eda6SGerd Knorr  #include <scsi/scsi_dbg.h>
36daa6eda6SGerd Knorr  
37daa6eda6SGerd Knorr  #define CH_DT_MAX       16
38daa6eda6SGerd Knorr  #define CH_TYPES        8
39da707c54SFUJITA Tomonori #define CH_MAX_DEVS     128
40daa6eda6SGerd Knorr  
41daa6eda6SGerd Knorr  MODULE_DESCRIPTION("device driver for scsi media changer devices");
42daa6eda6SGerd Knorr  MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
43daa6eda6SGerd Knorr  MODULE_LICENSE("GPL");
44f018fa55SRene Herman MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
4595f6c83fSScott James Remnant MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
46daa6eda6SGerd Knorr  
47daa6eda6SGerd Knorr  static int init = 1;
48daa6eda6SGerd Knorr  module_param(init, int, 0444);
49daa6eda6SGerd Knorr  MODULE_PARM_DESC(init, \
50daa6eda6SGerd Knorr      "initialize element status on driver load (default: on)");
51daa6eda6SGerd Knorr  
52daa6eda6SGerd Knorr  static int timeout_move = 300;
53daa6eda6SGerd Knorr  module_param(timeout_move, int, 0644);
54daa6eda6SGerd Knorr  MODULE_PARM_DESC(timeout_move,"timeout for move commands "
55daa6eda6SGerd Knorr  		 "(default: 300 seconds)");
56daa6eda6SGerd Knorr  
57daa6eda6SGerd Knorr  static int timeout_init = 3600;
58daa6eda6SGerd Knorr  module_param(timeout_init, int, 0644);
59daa6eda6SGerd Knorr  MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
60daa6eda6SGerd Knorr  		 "(default: 3600 seconds)");
61daa6eda6SGerd Knorr  
62daa6eda6SGerd Knorr  static int verbose = 1;
63daa6eda6SGerd Knorr  module_param(verbose, int, 0644);
64daa6eda6SGerd Knorr  MODULE_PARM_DESC(verbose,"be verbose (default: on)");
65daa6eda6SGerd Knorr  
66daa6eda6SGerd Knorr  static int debug = 0;
67daa6eda6SGerd Knorr  module_param(debug, int, 0644);
68daa6eda6SGerd Knorr  MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
69daa6eda6SGerd Knorr  		 "detailed sense codes on scsi errors (default: off)");
70daa6eda6SGerd Knorr  
71daa6eda6SGerd Knorr  static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
72daa6eda6SGerd Knorr  static int dt_lun[CH_DT_MAX];
73daa6eda6SGerd Knorr  module_param_array(dt_id,  int, NULL, 0444);
74daa6eda6SGerd Knorr  module_param_array(dt_lun, int, NULL, 0444);
75daa6eda6SGerd Knorr  
76daa6eda6SGerd Knorr  /* tell the driver about vendor-specific slots */
77daa6eda6SGerd Knorr  static int vendor_firsts[CH_TYPES-4];
78daa6eda6SGerd Knorr  static int vendor_counts[CH_TYPES-4];
79daa6eda6SGerd Knorr  module_param_array(vendor_firsts, int, NULL, 0444);
80daa6eda6SGerd Knorr  module_param_array(vendor_counts, int, NULL, 0444);
81daa6eda6SGerd Knorr  
820ad78200SArjan van de Ven static const char * vendor_labels[CH_TYPES-4] = {
83daa6eda6SGerd Knorr  	"v0", "v1", "v2", "v3"
84daa6eda6SGerd Knorr  };
85daa6eda6SGerd Knorr  // module_param_string_array(vendor_labels, NULL, 0444);
86daa6eda6SGerd Knorr  
8728c31729SHannes Reinecke #define ch_printk(prefix, ch, fmt, a...) \
8822e0d994SHannes Reinecke 	sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
8928c31729SHannes Reinecke 
9087da3235SJoe Perches #define DPRINTK(fmt, arg...)						\
9187da3235SJoe Perches do {									\
9287da3235SJoe Perches 	if (debug)							\
9328c31729SHannes Reinecke 		ch_printk(KERN_DEBUG, ch, fmt, ##arg);			\
9487da3235SJoe Perches } while (0)
9587da3235SJoe Perches #define VPRINTK(level, fmt, arg...)					\
9687da3235SJoe Perches do {									\
9787da3235SJoe Perches 	if (verbose)							\
9828c31729SHannes Reinecke 		ch_printk(level, ch, fmt, ##arg);			\
9987da3235SJoe Perches } while (0)
100daa6eda6SGerd Knorr  
101daa6eda6SGerd Knorr  /* ------------------------------------------------------------------- */
102daa6eda6SGerd Knorr  
103daa6eda6SGerd Knorr  #define MAX_RETRIES   1
104daa6eda6SGerd Knorr  
10521feb5ccSGerd Knorr  static struct class * ch_sysfs_class;
106daa6eda6SGerd Knorr  
107daa6eda6SGerd Knorr  typedef struct {
108085e5676SHannes Reinecke 	struct kref         ref;
109daa6eda6SGerd Knorr  	struct list_head    list;
110daa6eda6SGerd Knorr  	int                 minor;
111daa6eda6SGerd Knorr  	char                name[8];
112daa6eda6SGerd Knorr  	struct scsi_device  *device;
113daa6eda6SGerd Knorr  	struct scsi_device  **dt;        /* ptrs to data transfer elements */
114daa6eda6SGerd Knorr  	u_int               firsts[CH_TYPES];
115daa6eda6SGerd Knorr  	u_int               counts[CH_TYPES];
116daa6eda6SGerd Knorr  	u_int               unit_attention;
117daa6eda6SGerd Knorr  	u_int		    voltags;
1180b950672SArjan van de Ven 	struct mutex	    lock;
119daa6eda6SGerd Knorr  } scsi_changer;
120daa6eda6SGerd Knorr  
121da707c54SFUJITA Tomonori static DEFINE_IDR(ch_index_idr);
122da707c54SFUJITA Tomonori static DEFINE_SPINLOCK(ch_index_lock);
123daa6eda6SGerd Knorr  
1240ad78200SArjan van de Ven static const struct {
125daa6eda6SGerd Knorr  	unsigned char  sense;
126daa6eda6SGerd Knorr  	unsigned char  asc;
127daa6eda6SGerd Knorr  	unsigned char  ascq;
128daa6eda6SGerd Knorr  	int	       errno;
129d70d4667SHarvey Harrison } ch_err[] = {
130daa6eda6SGerd Knorr  /* Just filled in what looks right. Hav'nt checked any standard paper for
131daa6eda6SGerd Knorr     these errno assignments, so they may be wrong... */
132daa6eda6SGerd Knorr  	{
133daa6eda6SGerd Knorr  		.sense  = ILLEGAL_REQUEST,
134daa6eda6SGerd Knorr  		.asc    = 0x21,
135daa6eda6SGerd Knorr  		.ascq   = 0x01,
136daa6eda6SGerd Knorr  		.errno  = EBADSLT, /* Invalid element address */
137daa6eda6SGerd Knorr  	},{
138daa6eda6SGerd Knorr  		.sense  = ILLEGAL_REQUEST,
139daa6eda6SGerd Knorr  		.asc    = 0x28,
140daa6eda6SGerd Knorr  		.ascq   = 0x01,
141daa6eda6SGerd Knorr  		.errno  = EBADE,   /* Import or export element accessed */
142daa6eda6SGerd Knorr  	},{
143daa6eda6SGerd Knorr  		.sense  = ILLEGAL_REQUEST,
144daa6eda6SGerd Knorr  		.asc    = 0x3B,
145daa6eda6SGerd Knorr  		.ascq   = 0x0D,
146daa6eda6SGerd Knorr  		.errno  = EXFULL,  /* Medium destination element full */
147daa6eda6SGerd Knorr  	},{
148daa6eda6SGerd Knorr  		.sense  = ILLEGAL_REQUEST,
149daa6eda6SGerd Knorr  		.asc    = 0x3B,
150daa6eda6SGerd Knorr  		.ascq   = 0x0E,
151daa6eda6SGerd Knorr  		.errno  = EBADE,   /* Medium source element empty */
152daa6eda6SGerd Knorr  	},{
153daa6eda6SGerd Knorr  		.sense  = ILLEGAL_REQUEST,
154daa6eda6SGerd Knorr  		.asc    = 0x20,
155daa6eda6SGerd Knorr  		.ascq   = 0x00,
156daa6eda6SGerd Knorr  		.errno  = EBADRQC, /* Invalid command operation code */
157daa6eda6SGerd Knorr  	},{
158daa6eda6SGerd Knorr  	        /* end of list */
159daa6eda6SGerd Knorr  	}
160daa6eda6SGerd Knorr  };
161daa6eda6SGerd Knorr  
162daa6eda6SGerd Knorr  /* ------------------------------------------------------------------- */
163daa6eda6SGerd Knorr  
16484743bbcSJames Bottomley static int ch_find_errno(struct scsi_sense_hdr *sshdr)
165daa6eda6SGerd Knorr  {
166daa6eda6SGerd Knorr  	int i,errno = 0;
167daa6eda6SGerd Knorr  
168daa6eda6SGerd Knorr  	/* Check to see if additional sense information is available */
16984743bbcSJames Bottomley 	if (scsi_sense_valid(sshdr) &&
17084743bbcSJames Bottomley 	    sshdr->asc != 0) {
171d70d4667SHarvey Harrison 		for (i = 0; ch_err[i].errno != 0; i++) {
172d70d4667SHarvey Harrison 			if (ch_err[i].sense == sshdr->sense_key &&
173d70d4667SHarvey Harrison 			    ch_err[i].asc   == sshdr->asc &&
174d70d4667SHarvey Harrison 			    ch_err[i].ascq  == sshdr->ascq) {
175d70d4667SHarvey Harrison 				errno = -ch_err[i].errno;
176daa6eda6SGerd Knorr  				break;
177daa6eda6SGerd Knorr  			}
178daa6eda6SGerd Knorr  		}
179daa6eda6SGerd Knorr  	}
180daa6eda6SGerd Knorr  	if (errno == 0)
181daa6eda6SGerd Knorr  		errno = -EIO;
182daa6eda6SGerd Knorr  	return errno;
183daa6eda6SGerd Knorr  }
184daa6eda6SGerd Knorr  
185daa6eda6SGerd Knorr  static int
186a9a47bf5SHannes Reinecke ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
187daa6eda6SGerd Knorr  	   void *buffer, unsigned buflength,
188daa6eda6SGerd Knorr  	   enum dma_data_direction direction)
189daa6eda6SGerd Knorr  {
19084743bbcSJames Bottomley 	int errno, retries = 0, timeout, result;
19184743bbcSJames Bottomley 	struct scsi_sense_hdr sshdr;
192daa6eda6SGerd Knorr  
193daa6eda6SGerd Knorr  	timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
194daa6eda6SGerd Knorr  		? timeout_init : timeout_move;
195daa6eda6SGerd Knorr  
196daa6eda6SGerd Knorr   retry:
197daa6eda6SGerd Knorr  	errno = 0;
19884743bbcSJames Bottomley 	result = scsi_execute_req(ch->device, cmd, direction, buffer,
19984743bbcSJames Bottomley 				  buflength, &sshdr, timeout * HZ,
200f4f4e47eSFUJITA Tomonori 				  MAX_RETRIES, NULL);
201*ced202f7SHannes Reinecke 	if (result < 0)
202*ced202f7SHannes Reinecke 		return result;
203c65be1a6SJohannes Thumshirn 	if (driver_byte(result) == DRIVER_SENSE) {
204daa6eda6SGerd Knorr  		if (debug)
205d811b848SHannes Reinecke 			scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
20684743bbcSJames Bottomley 		errno = ch_find_errno(&sshdr);
207daa6eda6SGerd Knorr  
20884743bbcSJames Bottomley 		switch(sshdr.sense_key) {
209daa6eda6SGerd Knorr  		case UNIT_ATTENTION:
210daa6eda6SGerd Knorr  			ch->unit_attention = 1;
211daa6eda6SGerd Knorr  			if (retries++ < 3)
212daa6eda6SGerd Knorr  				goto retry;
213daa6eda6SGerd Knorr  			break;
214daa6eda6SGerd Knorr  		}
215daa6eda6SGerd Knorr  	}
216daa6eda6SGerd Knorr  	return errno;
217daa6eda6SGerd Knorr  }
218daa6eda6SGerd Knorr  
219daa6eda6SGerd Knorr  /* ------------------------------------------------------------------------ */
220daa6eda6SGerd Knorr  
221daa6eda6SGerd Knorr  static int
222daa6eda6SGerd Knorr  ch_elem_to_typecode(scsi_changer *ch, u_int elem)
223daa6eda6SGerd Knorr  {
224daa6eda6SGerd Knorr  	int i;
225daa6eda6SGerd Knorr  
226daa6eda6SGerd Knorr  	for (i = 0; i < CH_TYPES; i++) {
227daa6eda6SGerd Knorr  		if (elem >= ch->firsts[i]  &&
228daa6eda6SGerd Knorr  		    elem <  ch->firsts[i] +
229daa6eda6SGerd Knorr  	            ch->counts[i])
230daa6eda6SGerd Knorr  			return i+1;
231daa6eda6SGerd Knorr  	}
232daa6eda6SGerd Knorr  	return 0;
233daa6eda6SGerd Knorr  }
234daa6eda6SGerd Knorr  
235daa6eda6SGerd Knorr  static int
236daa6eda6SGerd Knorr  ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
237daa6eda6SGerd Knorr  {
238daa6eda6SGerd Knorr  	u_char  cmd[12];
239daa6eda6SGerd Knorr  	u_char  *buffer;
240daa6eda6SGerd Knorr  	int     result;
241daa6eda6SGerd Knorr  
242daa6eda6SGerd Knorr  	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
243daa6eda6SGerd Knorr  	if(!buffer)
244daa6eda6SGerd Knorr  		return -ENOMEM;
245daa6eda6SGerd Knorr  
246daa6eda6SGerd Knorr   retry:
247daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
248daa6eda6SGerd Knorr  	cmd[0] = READ_ELEMENT_STATUS;
2499cb78c16SHannes Reinecke 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
250daa6eda6SGerd Knorr  		(ch->voltags ? 0x10 : 0) |
251daa6eda6SGerd Knorr  		ch_elem_to_typecode(ch,elem);
252daa6eda6SGerd Knorr  	cmd[2] = (elem >> 8) & 0xff;
253daa6eda6SGerd Knorr  	cmd[3] = elem        & 0xff;
254daa6eda6SGerd Knorr  	cmd[5] = 1;
255daa6eda6SGerd Knorr  	cmd[9] = 255;
256a9a47bf5SHannes Reinecke 	if (0 == (result = ch_do_scsi(ch, cmd, 12,
257a9a47bf5SHannes Reinecke 				      buffer, 256, DMA_FROM_DEVICE))) {
258daa6eda6SGerd Knorr  		if (((buffer[16] << 8) | buffer[17]) != elem) {
25987da3235SJoe Perches 			DPRINTK("asked for element 0x%02x, got 0x%02x\n",
260daa6eda6SGerd Knorr  				elem,(buffer[16] << 8) | buffer[17]);
261daa6eda6SGerd Knorr  			kfree(buffer);
262daa6eda6SGerd Knorr  			return -EIO;
263daa6eda6SGerd Knorr  		}
264daa6eda6SGerd Knorr  		memcpy(data,buffer+16,16);
265daa6eda6SGerd Knorr  	} else {
266daa6eda6SGerd Knorr  		if (ch->voltags) {
267daa6eda6SGerd Knorr  			ch->voltags = 0;
26887da3235SJoe Perches 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
269daa6eda6SGerd Knorr  			goto retry;
270daa6eda6SGerd Knorr  		}
27187da3235SJoe Perches 		DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
272daa6eda6SGerd Knorr  	}
273daa6eda6SGerd Knorr  	kfree(buffer);
274daa6eda6SGerd Knorr  	return result;
275daa6eda6SGerd Knorr  }
276daa6eda6SGerd Knorr  
277daa6eda6SGerd Knorr  static int
278daa6eda6SGerd Knorr  ch_init_elem(scsi_changer *ch)
279daa6eda6SGerd Knorr  {
280daa6eda6SGerd Knorr  	int err;
281daa6eda6SGerd Knorr  	u_char cmd[6];
282daa6eda6SGerd Knorr  
28387da3235SJoe Perches 	VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
284daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
285daa6eda6SGerd Knorr  	cmd[0] = INITIALIZE_ELEMENT_STATUS;
2869cb78c16SHannes Reinecke 	cmd[1] = (ch->device->lun & 0x7) << 5;
287a9a47bf5SHannes Reinecke 	err = ch_do_scsi(ch, cmd, 6, NULL, 0, DMA_NONE);
28887da3235SJoe Perches 	VPRINTK(KERN_INFO, "... finished\n");
289daa6eda6SGerd Knorr  	return err;
290daa6eda6SGerd Knorr  }
291daa6eda6SGerd Knorr  
292daa6eda6SGerd Knorr  static int
293daa6eda6SGerd Knorr  ch_readconfig(scsi_changer *ch)
294daa6eda6SGerd Knorr  {
295daa6eda6SGerd Knorr  	u_char  cmd[10], data[16];
296daa6eda6SGerd Knorr  	u_char  *buffer;
297daa6eda6SGerd Knorr  	int     result,id,lun,i;
298daa6eda6SGerd Knorr  	u_int   elem;
299daa6eda6SGerd Knorr  
3004530a169Svignesh.babu@wipro.com 	buffer = kzalloc(512, GFP_KERNEL | GFP_DMA);
301daa6eda6SGerd Knorr  	if (!buffer)
302daa6eda6SGerd Knorr  		return -ENOMEM;
303daa6eda6SGerd Knorr  
304daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
305daa6eda6SGerd Knorr  	cmd[0] = MODE_SENSE;
3069cb78c16SHannes Reinecke 	cmd[1] = (ch->device->lun & 0x7) << 5;
307daa6eda6SGerd Knorr  	cmd[2] = 0x1d;
308daa6eda6SGerd Knorr  	cmd[4] = 255;
309a9a47bf5SHannes Reinecke 	result = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE);
310daa6eda6SGerd Knorr  	if (0 != result) {
311daa6eda6SGerd Knorr  		cmd[1] |= (1<<3);
312a9a47bf5SHannes Reinecke 		result  = ch_do_scsi(ch, cmd, 10, buffer, 255, DMA_FROM_DEVICE);
313daa6eda6SGerd Knorr  	}
314daa6eda6SGerd Knorr  	if (0 == result) {
315daa6eda6SGerd Knorr  		ch->firsts[CHET_MT] =
316daa6eda6SGerd Knorr  			(buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
317daa6eda6SGerd Knorr  		ch->counts[CHET_MT] =
318daa6eda6SGerd Knorr  			(buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
319daa6eda6SGerd Knorr  		ch->firsts[CHET_ST] =
320daa6eda6SGerd Knorr  			(buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
321daa6eda6SGerd Knorr  		ch->counts[CHET_ST] =
322daa6eda6SGerd Knorr  			(buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
323daa6eda6SGerd Knorr  		ch->firsts[CHET_IE] =
324daa6eda6SGerd Knorr  			(buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
325daa6eda6SGerd Knorr  		ch->counts[CHET_IE] =
326daa6eda6SGerd Knorr  			(buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
327daa6eda6SGerd Knorr  		ch->firsts[CHET_DT] =
328daa6eda6SGerd Knorr  			(buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
329daa6eda6SGerd Knorr  		ch->counts[CHET_DT] =
330daa6eda6SGerd Knorr  			(buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
33187da3235SJoe Perches 		VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
332daa6eda6SGerd Knorr  			ch->firsts[CHET_MT],
333daa6eda6SGerd Knorr  			ch->counts[CHET_MT]);
33487da3235SJoe Perches 		VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
335daa6eda6SGerd Knorr  			ch->firsts[CHET_ST],
336daa6eda6SGerd Knorr  			ch->counts[CHET_ST]);
33787da3235SJoe Perches 		VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
338daa6eda6SGerd Knorr  			ch->firsts[CHET_IE],
339daa6eda6SGerd Knorr  			ch->counts[CHET_IE]);
34087da3235SJoe Perches 		VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
341daa6eda6SGerd Knorr  			ch->firsts[CHET_DT],
342daa6eda6SGerd Knorr  			ch->counts[CHET_DT]);
343daa6eda6SGerd Knorr  	} else {
344f42cf8d6SMasanari Iida 		VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
345daa6eda6SGerd Knorr  	}
346daa6eda6SGerd Knorr  
347daa6eda6SGerd Knorr  	/* vendor specific element types */
348daa6eda6SGerd Knorr  	for (i = 0; i < 4; i++) {
349daa6eda6SGerd Knorr  		if (0 == vendor_counts[i])
350daa6eda6SGerd Knorr  			continue;
351daa6eda6SGerd Knorr  		if (NULL == vendor_labels[i])
352daa6eda6SGerd Knorr  			continue;
353daa6eda6SGerd Knorr  		ch->firsts[CHET_V1+i] = vendor_firsts[i];
354daa6eda6SGerd Knorr  		ch->counts[CHET_V1+i] = vendor_counts[i];
35587da3235SJoe Perches 		VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
356daa6eda6SGerd Knorr  			i+5,i+1,vendor_firsts[i],vendor_counts[i],
357daa6eda6SGerd Knorr  			vendor_labels[i]);
358daa6eda6SGerd Knorr  	}
359daa6eda6SGerd Knorr  
360daa6eda6SGerd Knorr  	/* look up the devices of the data transfer elements */
36185bc081fSJulia Lawall 	ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt),
362daa6eda6SGerd Knorr  			 GFP_KERNEL);
363a2cf8a63SDavidlohr Bueso A 
364a2cf8a63SDavidlohr Bueso A 	if (!ch->dt) {
365a2cf8a63SDavidlohr Bueso A 		kfree(buffer);
366a2cf8a63SDavidlohr Bueso A 		return -ENOMEM;
367a2cf8a63SDavidlohr Bueso A 	}
368a2cf8a63SDavidlohr Bueso A 
369daa6eda6SGerd Knorr  	for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
370daa6eda6SGerd Knorr  		id  = -1;
371daa6eda6SGerd Knorr  		lun = 0;
372daa6eda6SGerd Knorr  		if (elem < CH_DT_MAX  &&  -1 != dt_id[elem]) {
373daa6eda6SGerd Knorr  			id  = dt_id[elem];
374daa6eda6SGerd Knorr  			lun = dt_lun[elem];
37587da3235SJoe Perches 			VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
376daa6eda6SGerd Knorr  				elem+ch->firsts[CHET_DT]);
377daa6eda6SGerd Knorr  		} else if (0 != ch_read_element_status
378daa6eda6SGerd Knorr  			   (ch,elem+ch->firsts[CHET_DT],data)) {
37987da3235SJoe Perches 			VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
380daa6eda6SGerd Knorr  				elem+ch->firsts[CHET_DT]);
381daa6eda6SGerd Knorr  		} else {
38287da3235SJoe Perches 			VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
383daa6eda6SGerd Knorr  			if (data[6] & 0x80) {
38487da3235SJoe Perches 				VPRINTK(KERN_CONT, "not this SCSI bus\n");
385daa6eda6SGerd Knorr  				ch->dt[elem] = NULL;
386daa6eda6SGerd Knorr  			} else if (0 == (data[6] & 0x30)) {
38787da3235SJoe Perches 				VPRINTK(KERN_CONT, "ID/LUN unknown\n");
388daa6eda6SGerd Knorr  				ch->dt[elem] = NULL;
389daa6eda6SGerd Knorr  			} else {
390daa6eda6SGerd Knorr  				id  = ch->device->id;
391daa6eda6SGerd Knorr  				lun = 0;
392daa6eda6SGerd Knorr  				if (data[6] & 0x20) id  = data[7];
393daa6eda6SGerd Knorr  				if (data[6] & 0x10) lun = data[6] & 7;
394daa6eda6SGerd Knorr  			}
395daa6eda6SGerd Knorr  		}
396daa6eda6SGerd Knorr  		if (-1 != id) {
39787da3235SJoe Perches 			VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
398daa6eda6SGerd Knorr  			ch->dt[elem] =
399daa6eda6SGerd Knorr  				scsi_device_lookup(ch->device->host,
400daa6eda6SGerd Knorr  						   ch->device->channel,
401daa6eda6SGerd Knorr  						   id,lun);
402daa6eda6SGerd Knorr  			if (!ch->dt[elem]) {
403daa6eda6SGerd Knorr  				/* should not happen */
40487da3235SJoe Perches 				VPRINTK(KERN_CONT, "Huh? device not found!\n");
405daa6eda6SGerd Knorr  			} else {
40687da3235SJoe Perches 				VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
407daa6eda6SGerd Knorr  					ch->dt[elem]->vendor,
408daa6eda6SGerd Knorr  					ch->dt[elem]->model,
409daa6eda6SGerd Knorr  					ch->dt[elem]->rev);
410daa6eda6SGerd Knorr  			}
411daa6eda6SGerd Knorr  		}
412daa6eda6SGerd Knorr  	}
413daa6eda6SGerd Knorr  	ch->voltags = 1;
414daa6eda6SGerd Knorr  	kfree(buffer);
415daa6eda6SGerd Knorr  
416daa6eda6SGerd Knorr  	return 0;
417daa6eda6SGerd Knorr  }
418daa6eda6SGerd Knorr  
419daa6eda6SGerd Knorr  /* ------------------------------------------------------------------------ */
420daa6eda6SGerd Knorr  
421daa6eda6SGerd Knorr  static int
422daa6eda6SGerd Knorr  ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
423daa6eda6SGerd Knorr  {
424daa6eda6SGerd Knorr  	u_char  cmd[10];
425daa6eda6SGerd Knorr  
42687da3235SJoe Perches 	DPRINTK("position: 0x%x\n",elem);
427daa6eda6SGerd Knorr  	if (0 == trans)
428daa6eda6SGerd Knorr  		trans = ch->firsts[CHET_MT];
429daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
430daa6eda6SGerd Knorr  	cmd[0]  = POSITION_TO_ELEMENT;
4319cb78c16SHannes Reinecke 	cmd[1]  = (ch->device->lun & 0x7) << 5;
432daa6eda6SGerd Knorr  	cmd[2]  = (trans >> 8) & 0xff;
433daa6eda6SGerd Knorr  	cmd[3]  =  trans       & 0xff;
434daa6eda6SGerd Knorr  	cmd[4]  = (elem  >> 8) & 0xff;
435daa6eda6SGerd Knorr  	cmd[5]  =  elem        & 0xff;
436daa6eda6SGerd Knorr  	cmd[8]  = rotate ? 1 : 0;
437a9a47bf5SHannes Reinecke 	return ch_do_scsi(ch, cmd, 10, NULL, 0, DMA_NONE);
438daa6eda6SGerd Knorr  }
439daa6eda6SGerd Knorr  
440daa6eda6SGerd Knorr  static int
441daa6eda6SGerd Knorr  ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
442daa6eda6SGerd Knorr  {
443daa6eda6SGerd Knorr  	u_char  cmd[12];
444daa6eda6SGerd Knorr  
44587da3235SJoe Perches 	DPRINTK("move: 0x%x => 0x%x\n",src,dest);
446daa6eda6SGerd Knorr  	if (0 == trans)
447daa6eda6SGerd Knorr  		trans = ch->firsts[CHET_MT];
448daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
449daa6eda6SGerd Knorr  	cmd[0]  = MOVE_MEDIUM;
4509cb78c16SHannes Reinecke 	cmd[1]  = (ch->device->lun & 0x7) << 5;
451daa6eda6SGerd Knorr  	cmd[2]  = (trans >> 8) & 0xff;
452daa6eda6SGerd Knorr  	cmd[3]  =  trans       & 0xff;
453daa6eda6SGerd Knorr  	cmd[4]  = (src   >> 8) & 0xff;
454daa6eda6SGerd Knorr  	cmd[5]  =  src         & 0xff;
455daa6eda6SGerd Knorr  	cmd[6]  = (dest  >> 8) & 0xff;
456daa6eda6SGerd Knorr  	cmd[7]  =  dest        & 0xff;
457daa6eda6SGerd Knorr  	cmd[10] = rotate ? 1 : 0;
458a9a47bf5SHannes Reinecke 	return ch_do_scsi(ch, cmd, 12, NULL,0, DMA_NONE);
459daa6eda6SGerd Knorr  }
460daa6eda6SGerd Knorr  
461daa6eda6SGerd Knorr  static int
462daa6eda6SGerd Knorr  ch_exchange(scsi_changer *ch, u_int trans, u_int src,
463daa6eda6SGerd Knorr  	    u_int dest1, u_int dest2, int rotate1, int rotate2)
464daa6eda6SGerd Knorr  {
465daa6eda6SGerd Knorr  	u_char  cmd[12];
466daa6eda6SGerd Knorr  
46787da3235SJoe Perches 	DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
468daa6eda6SGerd Knorr  		src,dest1,dest2);
469daa6eda6SGerd Knorr  	if (0 == trans)
470daa6eda6SGerd Knorr  		trans = ch->firsts[CHET_MT];
471daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
472daa6eda6SGerd Knorr  	cmd[0]  = EXCHANGE_MEDIUM;
4739cb78c16SHannes Reinecke 	cmd[1]  = (ch->device->lun & 0x7) << 5;
474daa6eda6SGerd Knorr  	cmd[2]  = (trans >> 8) & 0xff;
475daa6eda6SGerd Knorr  	cmd[3]  =  trans       & 0xff;
476daa6eda6SGerd Knorr  	cmd[4]  = (src   >> 8) & 0xff;
477daa6eda6SGerd Knorr  	cmd[5]  =  src         & 0xff;
478daa6eda6SGerd Knorr  	cmd[6]  = (dest1 >> 8) & 0xff;
479daa6eda6SGerd Knorr  	cmd[7]  =  dest1       & 0xff;
480daa6eda6SGerd Knorr  	cmd[8]  = (dest2 >> 8) & 0xff;
481daa6eda6SGerd Knorr  	cmd[9]  =  dest2       & 0xff;
482daa6eda6SGerd Knorr  	cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
483daa6eda6SGerd Knorr  
484a9a47bf5SHannes Reinecke 	return ch_do_scsi(ch, cmd, 12, NULL, 0, DMA_NONE);
485daa6eda6SGerd Knorr  }
486daa6eda6SGerd Knorr  
487daa6eda6SGerd Knorr  static void
488daa6eda6SGerd Knorr  ch_check_voltag(char *tag)
489daa6eda6SGerd Knorr  {
490daa6eda6SGerd Knorr  	int i;
491daa6eda6SGerd Knorr  
492daa6eda6SGerd Knorr  	for (i = 0; i < 32; i++) {
493daa6eda6SGerd Knorr  		/* restrict to ascii */
494daa6eda6SGerd Knorr  		if (tag[i] >= 0x7f || tag[i] < 0x20)
495daa6eda6SGerd Knorr  			tag[i] = ' ';
496daa6eda6SGerd Knorr  		/* don't allow search wildcards */
497daa6eda6SGerd Knorr  		if (tag[i] == '?' ||
498daa6eda6SGerd Knorr  		    tag[i] == '*')
499daa6eda6SGerd Knorr  			tag[i] = ' ';
500daa6eda6SGerd Knorr  	}
501daa6eda6SGerd Knorr  }
502daa6eda6SGerd Knorr  
503daa6eda6SGerd Knorr  static int
504daa6eda6SGerd Knorr  ch_set_voltag(scsi_changer *ch, u_int elem,
505daa6eda6SGerd Knorr  	      int alternate, int clear, u_char *tag)
506daa6eda6SGerd Knorr  {
507daa6eda6SGerd Knorr  	u_char  cmd[12];
508daa6eda6SGerd Knorr  	u_char  *buffer;
509daa6eda6SGerd Knorr  	int result;
510daa6eda6SGerd Knorr  
5114530a169Svignesh.babu@wipro.com 	buffer = kzalloc(512, GFP_KERNEL);
512daa6eda6SGerd Knorr  	if (!buffer)
513daa6eda6SGerd Knorr  		return -ENOMEM;
514daa6eda6SGerd Knorr  
51587da3235SJoe Perches 	DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
516daa6eda6SGerd Knorr  		clear     ? "clear"     : "set",
517daa6eda6SGerd Knorr  		alternate ? "alternate" : "primary",
518daa6eda6SGerd Knorr  		elem, tag);
519daa6eda6SGerd Knorr  	memset(cmd,0,sizeof(cmd));
520daa6eda6SGerd Knorr  	cmd[0]  = SEND_VOLUME_TAG;
5219cb78c16SHannes Reinecke 	cmd[1] = ((ch->device->lun & 0x7) << 5) |
522daa6eda6SGerd Knorr  		ch_elem_to_typecode(ch,elem);
523daa6eda6SGerd Knorr  	cmd[2] = (elem >> 8) & 0xff;
524daa6eda6SGerd Knorr  	cmd[3] = elem        & 0xff;
525daa6eda6SGerd Knorr  	cmd[5] = clear
526daa6eda6SGerd Knorr  		? (alternate ? 0x0d : 0x0c)
527daa6eda6SGerd Knorr  		: (alternate ? 0x0b : 0x0a);
528daa6eda6SGerd Knorr  
529daa6eda6SGerd Knorr  	cmd[9] = 255;
530daa6eda6SGerd Knorr  
531daa6eda6SGerd Knorr  	memcpy(buffer,tag,32);
532daa6eda6SGerd Knorr  	ch_check_voltag(buffer);
533daa6eda6SGerd Knorr  
534a9a47bf5SHannes Reinecke 	result = ch_do_scsi(ch, cmd, 12, buffer, 256, DMA_TO_DEVICE);
535daa6eda6SGerd Knorr  	kfree(buffer);
536daa6eda6SGerd Knorr  	return result;
537daa6eda6SGerd Knorr  }
538daa6eda6SGerd Knorr  
539fe08ac31Sviro@ZenIV.linux.org.uk static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
540daa6eda6SGerd Knorr  {
541daa6eda6SGerd Knorr  	int retval = 0;
542daa6eda6SGerd Knorr  	u_char data[16];
543daa6eda6SGerd Knorr  	unsigned int i;
544daa6eda6SGerd Knorr  
5450b950672SArjan van de Ven 	mutex_lock(&ch->lock);
546daa6eda6SGerd Knorr  	for (i = 0; i < ch->counts[type]; i++) {
547daa6eda6SGerd Knorr  		if (0 != ch_read_element_status
548daa6eda6SGerd Knorr  		    (ch, ch->firsts[type]+i,data)) {
549daa6eda6SGerd Knorr  			retval = -EIO;
550daa6eda6SGerd Knorr  			break;
551daa6eda6SGerd Knorr  		}
552daa6eda6SGerd Knorr  		put_user(data[2], dest+i);
553daa6eda6SGerd Knorr  		if (data[2] & CESTATUS_EXCEPT)
55487da3235SJoe Perches 			VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
555daa6eda6SGerd Knorr  				ch->firsts[type]+i,
556daa6eda6SGerd Knorr  				(int)data[4],(int)data[5]);
557daa6eda6SGerd Knorr  		retval = ch_read_element_status
558daa6eda6SGerd Knorr  			(ch, ch->firsts[type]+i,data);
559daa6eda6SGerd Knorr  		if (0 != retval)
560daa6eda6SGerd Knorr  			break;
561daa6eda6SGerd Knorr  	}
5620b950672SArjan van de Ven 	mutex_unlock(&ch->lock);
563daa6eda6SGerd Knorr  	return retval;
564daa6eda6SGerd Knorr  }
565daa6eda6SGerd Knorr  
566daa6eda6SGerd Knorr  /* ------------------------------------------------------------------------ */
567daa6eda6SGerd Knorr  
568085e5676SHannes Reinecke static void ch_destroy(struct kref *ref)
569085e5676SHannes Reinecke {
570085e5676SHannes Reinecke 	scsi_changer *ch = container_of(ref, scsi_changer, ref);
571085e5676SHannes Reinecke 
57266167283SHannes Reinecke 	ch->device = NULL;
573085e5676SHannes Reinecke 	kfree(ch->dt);
574085e5676SHannes Reinecke 	kfree(ch);
575085e5676SHannes Reinecke }
576085e5676SHannes Reinecke 
577daa6eda6SGerd Knorr  static int
578daa6eda6SGerd Knorr  ch_release(struct inode *inode, struct file *file)
579daa6eda6SGerd Knorr  {
580daa6eda6SGerd Knorr  	scsi_changer *ch = file->private_data;
581daa6eda6SGerd Knorr  
582daa6eda6SGerd Knorr  	scsi_device_put(ch->device);
583daa6eda6SGerd Knorr  	file->private_data = NULL;
584085e5676SHannes Reinecke 	kref_put(&ch->ref, ch_destroy);
585daa6eda6SGerd Knorr  	return 0;
586daa6eda6SGerd Knorr  }
587daa6eda6SGerd Knorr  
588daa6eda6SGerd Knorr  static int
589daa6eda6SGerd Knorr  ch_open(struct inode *inode, struct file *file)
590daa6eda6SGerd Knorr  {
591da707c54SFUJITA Tomonori 	scsi_changer *ch;
592daa6eda6SGerd Knorr  	int minor = iminor(inode);
593daa6eda6SGerd Knorr  
594da707c54SFUJITA Tomonori 	spin_lock(&ch_index_lock);
595da707c54SFUJITA Tomonori 	ch = idr_find(&ch_index_idr, minor);
596da707c54SFUJITA Tomonori 
59766167283SHannes Reinecke 	if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
598da707c54SFUJITA Tomonori 		spin_unlock(&ch_index_lock);
599daa6eda6SGerd Knorr  		return -ENXIO;
600daa6eda6SGerd Knorr  	}
601da707c54SFUJITA Tomonori 	spin_unlock(&ch_index_lock);
60266167283SHannes Reinecke 	if (scsi_device_get(ch->device)) {
60366167283SHannes Reinecke 		kref_put(&ch->ref, ch_destroy);
60466167283SHannes Reinecke 		return -ENXIO;
60566167283SHannes Reinecke 	}
6061c7ce4bcSHannes Reinecke 	/* Synchronize with ch_probe() */
6071c7ce4bcSHannes Reinecke 	mutex_lock(&ch->lock);
608daa6eda6SGerd Knorr  	file->private_data = ch;
6091c7ce4bcSHannes Reinecke 	mutex_unlock(&ch->lock);
610daa6eda6SGerd Knorr  	return 0;
611daa6eda6SGerd Knorr  }
612daa6eda6SGerd Knorr  
613daa6eda6SGerd Knorr  static int
614daa6eda6SGerd Knorr  ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
615daa6eda6SGerd Knorr  {
616daa6eda6SGerd Knorr  	if (type >= CH_TYPES  ||  unit >= ch->counts[type])
617daa6eda6SGerd Knorr  		return -1;
618daa6eda6SGerd Knorr  	return 0;
619daa6eda6SGerd Knorr  }
620daa6eda6SGerd Knorr  
621f7fea185SMathieu Segaud static long ch_ioctl(struct file *file,
622daa6eda6SGerd Knorr  		    unsigned int cmd, unsigned long arg)
623daa6eda6SGerd Knorr  {
624daa6eda6SGerd Knorr  	scsi_changer *ch = file->private_data;
625daa6eda6SGerd Knorr  	int retval;
626fe08ac31Sviro@ZenIV.linux.org.uk 	void __user *argp = (void __user *)arg;
627daa6eda6SGerd Knorr  
628906d15fbSChristoph Hellwig 	retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
629906d15fbSChristoph Hellwig 			file->f_flags & O_NDELAY);
630906d15fbSChristoph Hellwig 	if (retval)
631906d15fbSChristoph Hellwig 		return retval;
632906d15fbSChristoph Hellwig 
633daa6eda6SGerd Knorr  	switch (cmd) {
634daa6eda6SGerd Knorr  	case CHIOGPARAMS:
635daa6eda6SGerd Knorr  	{
636daa6eda6SGerd Knorr  		struct changer_params params;
637daa6eda6SGerd Knorr  
638daa6eda6SGerd Knorr  		params.cp_curpicker = 0;
639daa6eda6SGerd Knorr  		params.cp_npickers  = ch->counts[CHET_MT];
640daa6eda6SGerd Knorr  		params.cp_nslots    = ch->counts[CHET_ST];
641daa6eda6SGerd Knorr  		params.cp_nportals  = ch->counts[CHET_IE];
642daa6eda6SGerd Knorr  		params.cp_ndrives   = ch->counts[CHET_DT];
643daa6eda6SGerd Knorr  
644fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_to_user(argp, &params, sizeof(params)))
645daa6eda6SGerd Knorr  			return -EFAULT;
646daa6eda6SGerd Knorr  		return 0;
647daa6eda6SGerd Knorr  	}
648daa6eda6SGerd Knorr  	case CHIOGVPARAMS:
649daa6eda6SGerd Knorr  	{
650daa6eda6SGerd Knorr  		struct changer_vendor_params vparams;
651daa6eda6SGerd Knorr  
652daa6eda6SGerd Knorr  		memset(&vparams,0,sizeof(vparams));
653daa6eda6SGerd Knorr  		if (ch->counts[CHET_V1]) {
654daa6eda6SGerd Knorr  			vparams.cvp_n1  = ch->counts[CHET_V1];
655daa6eda6SGerd Knorr  			strncpy(vparams.cvp_label1,vendor_labels[0],16);
656daa6eda6SGerd Knorr  		}
657daa6eda6SGerd Knorr  		if (ch->counts[CHET_V2]) {
658daa6eda6SGerd Knorr  			vparams.cvp_n2  = ch->counts[CHET_V2];
659daa6eda6SGerd Knorr  			strncpy(vparams.cvp_label2,vendor_labels[1],16);
660daa6eda6SGerd Knorr  		}
661daa6eda6SGerd Knorr  		if (ch->counts[CHET_V3]) {
662daa6eda6SGerd Knorr  			vparams.cvp_n3  = ch->counts[CHET_V3];
663daa6eda6SGerd Knorr  			strncpy(vparams.cvp_label3,vendor_labels[2],16);
664daa6eda6SGerd Knorr  		}
665daa6eda6SGerd Knorr  		if (ch->counts[CHET_V4]) {
666daa6eda6SGerd Knorr  			vparams.cvp_n4  = ch->counts[CHET_V4];
667daa6eda6SGerd Knorr  			strncpy(vparams.cvp_label4,vendor_labels[3],16);
668daa6eda6SGerd Knorr  		}
669fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_to_user(argp, &vparams, sizeof(vparams)))
670daa6eda6SGerd Knorr  			return -EFAULT;
671daa6eda6SGerd Knorr  		return 0;
672daa6eda6SGerd Knorr  	}
673daa6eda6SGerd Knorr  
674daa6eda6SGerd Knorr  	case CHIOPOSITION:
675daa6eda6SGerd Knorr  	{
676daa6eda6SGerd Knorr  		struct changer_position pos;
677daa6eda6SGerd Knorr  
678fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&pos, argp, sizeof (pos)))
679daa6eda6SGerd Knorr  			return -EFAULT;
680daa6eda6SGerd Knorr  
681daa6eda6SGerd Knorr  		if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
68287da3235SJoe Perches 			DPRINTK("CHIOPOSITION: invalid parameter\n");
683daa6eda6SGerd Knorr  			return -EBADSLT;
684daa6eda6SGerd Knorr  		}
6850b950672SArjan van de Ven 		mutex_lock(&ch->lock);
686daa6eda6SGerd Knorr  		retval = ch_position(ch,0,
687daa6eda6SGerd Knorr  				     ch->firsts[pos.cp_type] + pos.cp_unit,
688daa6eda6SGerd Knorr  				     pos.cp_flags & CP_INVERT);
6890b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
690daa6eda6SGerd Knorr  		return retval;
691daa6eda6SGerd Knorr  	}
692daa6eda6SGerd Knorr  
693daa6eda6SGerd Knorr  	case CHIOMOVE:
694daa6eda6SGerd Knorr  	{
695daa6eda6SGerd Knorr  		struct changer_move mv;
696daa6eda6SGerd Knorr  
697fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&mv, argp, sizeof (mv)))
698daa6eda6SGerd Knorr  			return -EFAULT;
699daa6eda6SGerd Knorr  
700daa6eda6SGerd Knorr  		if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
701daa6eda6SGerd Knorr  		    0 != ch_checkrange(ch, mv.cm_totype,   mv.cm_tounit  )) {
70287da3235SJoe Perches 			DPRINTK("CHIOMOVE: invalid parameter\n");
703daa6eda6SGerd Knorr  			return -EBADSLT;
704daa6eda6SGerd Knorr  		}
705daa6eda6SGerd Knorr  
7060b950672SArjan van de Ven 		mutex_lock(&ch->lock);
707daa6eda6SGerd Knorr  		retval = ch_move(ch,0,
708daa6eda6SGerd Knorr  				 ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
709daa6eda6SGerd Knorr  				 ch->firsts[mv.cm_totype]   + mv.cm_tounit,
710daa6eda6SGerd Knorr  				 mv.cm_flags & CM_INVERT);
7110b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
712daa6eda6SGerd Knorr  		return retval;
713daa6eda6SGerd Knorr  	}
714daa6eda6SGerd Knorr  
715daa6eda6SGerd Knorr  	case CHIOEXCHANGE:
716daa6eda6SGerd Knorr  	{
717daa6eda6SGerd Knorr  		struct changer_exchange mv;
718daa6eda6SGerd Knorr  
719fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&mv, argp, sizeof (mv)))
720daa6eda6SGerd Knorr  			return -EFAULT;
721daa6eda6SGerd Knorr  
722daa6eda6SGerd Knorr  		if (0 != ch_checkrange(ch, mv.ce_srctype,  mv.ce_srcunit ) ||
723daa6eda6SGerd Knorr  		    0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
724daa6eda6SGerd Knorr  		    0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
72587da3235SJoe Perches 			DPRINTK("CHIOEXCHANGE: invalid parameter\n");
726daa6eda6SGerd Knorr  			return -EBADSLT;
727daa6eda6SGerd Knorr  		}
728daa6eda6SGerd Knorr  
7290b950672SArjan van de Ven 		mutex_lock(&ch->lock);
730daa6eda6SGerd Knorr  		retval = ch_exchange
731daa6eda6SGerd Knorr  			(ch,0,
732daa6eda6SGerd Knorr  			 ch->firsts[mv.ce_srctype]  + mv.ce_srcunit,
733daa6eda6SGerd Knorr  			 ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
734daa6eda6SGerd Knorr  			 ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
735daa6eda6SGerd Knorr  			 mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
7360b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
737daa6eda6SGerd Knorr  		return retval;
738daa6eda6SGerd Knorr  	}
739daa6eda6SGerd Knorr  
740daa6eda6SGerd Knorr  	case CHIOGSTATUS:
741daa6eda6SGerd Knorr  	{
742daa6eda6SGerd Knorr  		struct changer_element_status ces;
743daa6eda6SGerd Knorr  
744fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&ces, argp, sizeof (ces)))
745daa6eda6SGerd Knorr  			return -EFAULT;
746daa6eda6SGerd Knorr  		if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
747daa6eda6SGerd Knorr  			return -EINVAL;
748daa6eda6SGerd Knorr  
749daa6eda6SGerd Knorr  		return ch_gstatus(ch, ces.ces_type, ces.ces_data);
750daa6eda6SGerd Knorr  	}
751daa6eda6SGerd Knorr  
752daa6eda6SGerd Knorr  	case CHIOGELEM:
753daa6eda6SGerd Knorr  	{
754daa6eda6SGerd Knorr  		struct changer_get_element cge;
755d70d4667SHarvey Harrison 		u_char ch_cmd[12];
756daa6eda6SGerd Knorr  		u_char *buffer;
757daa6eda6SGerd Knorr  		unsigned int elem;
758daa6eda6SGerd Knorr  		int     result,i;
759daa6eda6SGerd Knorr  
760fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&cge, argp, sizeof (cge)))
761daa6eda6SGerd Knorr  			return -EFAULT;
762daa6eda6SGerd Knorr  
763daa6eda6SGerd Knorr  		if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
764daa6eda6SGerd Knorr  			return -EINVAL;
765daa6eda6SGerd Knorr  		elem = ch->firsts[cge.cge_type] + cge.cge_unit;
766daa6eda6SGerd Knorr  
767daa6eda6SGerd Knorr  		buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
768daa6eda6SGerd Knorr  		if (!buffer)
769daa6eda6SGerd Knorr  			return -ENOMEM;
7700b950672SArjan van de Ven 		mutex_lock(&ch->lock);
771daa6eda6SGerd Knorr  
772daa6eda6SGerd Knorr  	voltag_retry:
773d70d4667SHarvey Harrison 		memset(ch_cmd, 0, sizeof(ch_cmd));
774d70d4667SHarvey Harrison 		ch_cmd[0] = READ_ELEMENT_STATUS;
7759cb78c16SHannes Reinecke 		ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
776daa6eda6SGerd Knorr  			(ch->voltags ? 0x10 : 0) |
777daa6eda6SGerd Knorr  			ch_elem_to_typecode(ch,elem);
778d70d4667SHarvey Harrison 		ch_cmd[2] = (elem >> 8) & 0xff;
779d70d4667SHarvey Harrison 		ch_cmd[3] = elem        & 0xff;
780d70d4667SHarvey Harrison 		ch_cmd[5] = 1;
781d70d4667SHarvey Harrison 		ch_cmd[9] = 255;
782daa6eda6SGerd Knorr  
783a9a47bf5SHannes Reinecke 		result = ch_do_scsi(ch, ch_cmd, 12,
784a9a47bf5SHannes Reinecke 				    buffer, 256, DMA_FROM_DEVICE);
785d70d4667SHarvey Harrison 		if (!result) {
786daa6eda6SGerd Knorr  			cge.cge_status = buffer[18];
787daa6eda6SGerd Knorr  			cge.cge_flags = 0;
788daa6eda6SGerd Knorr  			if (buffer[18] & CESTATUS_EXCEPT) {
789daa6eda6SGerd Knorr  				cge.cge_errno = EIO;
790daa6eda6SGerd Knorr  			}
791daa6eda6SGerd Knorr  			if (buffer[25] & 0x80) {
792daa6eda6SGerd Knorr  				cge.cge_flags |= CGE_SRC;
793daa6eda6SGerd Knorr  				if (buffer[25] & 0x40)
794daa6eda6SGerd Knorr  					cge.cge_flags |= CGE_INVERT;
795daa6eda6SGerd Knorr  				elem = (buffer[26]<<8) | buffer[27];
796daa6eda6SGerd Knorr  				for (i = 0; i < 4; i++) {
797daa6eda6SGerd Knorr  					if (elem >= ch->firsts[i] &&
798daa6eda6SGerd Knorr  					    elem <  ch->firsts[i] + ch->counts[i]) {
799daa6eda6SGerd Knorr  						cge.cge_srctype = i;
800daa6eda6SGerd Knorr  						cge.cge_srcunit = elem-ch->firsts[i];
801daa6eda6SGerd Knorr  					}
802daa6eda6SGerd Knorr  				}
803daa6eda6SGerd Knorr  			}
804daa6eda6SGerd Knorr  			if ((buffer[22] & 0x30) == 0x30) {
805daa6eda6SGerd Knorr  				cge.cge_flags |= CGE_IDLUN;
806daa6eda6SGerd Knorr  				cge.cge_id  = buffer[23];
807daa6eda6SGerd Knorr  				cge.cge_lun = buffer[22] & 7;
808daa6eda6SGerd Knorr  			}
809daa6eda6SGerd Knorr  			if (buffer[9] & 0x80) {
810daa6eda6SGerd Knorr  				cge.cge_flags |= CGE_PVOLTAG;
811daa6eda6SGerd Knorr  				memcpy(cge.cge_pvoltag,buffer+28,36);
812daa6eda6SGerd Knorr  			}
813daa6eda6SGerd Knorr  			if (buffer[9] & 0x40) {
814daa6eda6SGerd Knorr  				cge.cge_flags |= CGE_AVOLTAG;
815daa6eda6SGerd Knorr  				memcpy(cge.cge_avoltag,buffer+64,36);
816daa6eda6SGerd Knorr  			}
817daa6eda6SGerd Knorr  		} else if (ch->voltags) {
818daa6eda6SGerd Knorr  			ch->voltags = 0;
81987da3235SJoe Perches 			VPRINTK(KERN_INFO, "device has no volume tag support\n");
820daa6eda6SGerd Knorr  			goto voltag_retry;
821daa6eda6SGerd Knorr  		}
822daa6eda6SGerd Knorr  		kfree(buffer);
8230b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
824daa6eda6SGerd Knorr  
825fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_to_user(argp, &cge, sizeof (cge)))
826daa6eda6SGerd Knorr  			return -EFAULT;
827daa6eda6SGerd Knorr  		return result;
828daa6eda6SGerd Knorr  	}
829daa6eda6SGerd Knorr  
830daa6eda6SGerd Knorr  	case CHIOINITELEM:
831daa6eda6SGerd Knorr  	{
8320b950672SArjan van de Ven 		mutex_lock(&ch->lock);
833daa6eda6SGerd Knorr  		retval = ch_init_elem(ch);
8340b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
835daa6eda6SGerd Knorr  		return retval;
836daa6eda6SGerd Knorr  	}
837daa6eda6SGerd Knorr  
838daa6eda6SGerd Knorr  	case CHIOSVOLTAG:
839daa6eda6SGerd Knorr  	{
840daa6eda6SGerd Knorr  		struct changer_set_voltag csv;
841daa6eda6SGerd Knorr  		int elem;
842daa6eda6SGerd Knorr  
843fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&csv, argp, sizeof(csv)))
844daa6eda6SGerd Knorr  			return -EFAULT;
845daa6eda6SGerd Knorr  
846daa6eda6SGerd Knorr  		if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
84787da3235SJoe Perches 			DPRINTK("CHIOSVOLTAG: invalid parameter\n");
848daa6eda6SGerd Knorr  			return -EBADSLT;
849daa6eda6SGerd Knorr  		}
850daa6eda6SGerd Knorr  		elem = ch->firsts[csv.csv_type] + csv.csv_unit;
8510b950672SArjan van de Ven 		mutex_lock(&ch->lock);
852daa6eda6SGerd Knorr  		retval = ch_set_voltag(ch, elem,
853daa6eda6SGerd Knorr  				       csv.csv_flags & CSV_AVOLTAG,
854daa6eda6SGerd Knorr  				       csv.csv_flags & CSV_CLEARTAG,
855daa6eda6SGerd Knorr  				       csv.csv_voltag);
8560b950672SArjan van de Ven 		mutex_unlock(&ch->lock);
857daa6eda6SGerd Knorr  		return retval;
858daa6eda6SGerd Knorr  	}
859daa6eda6SGerd Knorr  
860daa6eda6SGerd Knorr  	default:
861fe08ac31Sviro@ZenIV.linux.org.uk 		return scsi_ioctl(ch->device, cmd, argp);
862daa6eda6SGerd Knorr  
863daa6eda6SGerd Knorr  	}
864daa6eda6SGerd Knorr  }
865daa6eda6SGerd Knorr  
866daa6eda6SGerd Knorr  #ifdef CONFIG_COMPAT
867daa6eda6SGerd Knorr  
868daa6eda6SGerd Knorr  struct changer_element_status32 {
869daa6eda6SGerd Knorr  	int		ces_type;
870daa6eda6SGerd Knorr  	compat_uptr_t	ces_data;
871daa6eda6SGerd Knorr  };
872daa6eda6SGerd Knorr  #define CHIOGSTATUS32  _IOW('c', 8,struct changer_element_status32)
873daa6eda6SGerd Knorr  
874daa6eda6SGerd Knorr  static long ch_ioctl_compat(struct file * file,
875daa6eda6SGerd Knorr  			    unsigned int cmd, unsigned long arg)
876daa6eda6SGerd Knorr  {
877daa6eda6SGerd Knorr  	scsi_changer *ch = file->private_data;
878d320a955SArnd Bergmann 	int retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
879d320a955SArnd Bergmann 							file->f_flags & O_NDELAY);
880d320a955SArnd Bergmann 	if (retval)
881d320a955SArnd Bergmann 		return retval;
882daa6eda6SGerd Knorr  
883daa6eda6SGerd Knorr  	switch (cmd) {
884daa6eda6SGerd Knorr  	case CHIOGPARAMS:
885daa6eda6SGerd Knorr  	case CHIOGVPARAMS:
886daa6eda6SGerd Knorr  	case CHIOPOSITION:
887daa6eda6SGerd Knorr  	case CHIOMOVE:
888daa6eda6SGerd Knorr  	case CHIOEXCHANGE:
889daa6eda6SGerd Knorr  	case CHIOGELEM:
890daa6eda6SGerd Knorr  	case CHIOINITELEM:
891daa6eda6SGerd Knorr  	case CHIOSVOLTAG:
892daa6eda6SGerd Knorr  		/* compatible */
893d320a955SArnd Bergmann 		return ch_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
894daa6eda6SGerd Knorr  	case CHIOGSTATUS32:
895daa6eda6SGerd Knorr  	{
896daa6eda6SGerd Knorr  		struct changer_element_status32 ces32;
897fe08ac31Sviro@ZenIV.linux.org.uk 		unsigned char __user *data;
898daa6eda6SGerd Knorr  
899fe08ac31Sviro@ZenIV.linux.org.uk 		if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32)))
900daa6eda6SGerd Knorr  			return -EFAULT;
901daa6eda6SGerd Knorr  		if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
902daa6eda6SGerd Knorr  			return -EINVAL;
903daa6eda6SGerd Knorr  
904daa6eda6SGerd Knorr  		data = compat_ptr(ces32.ces_data);
905daa6eda6SGerd Knorr  		return ch_gstatus(ch, ces32.ces_type, data);
906daa6eda6SGerd Knorr  	}
907daa6eda6SGerd Knorr  	default:
908d320a955SArnd Bergmann 		return scsi_compat_ioctl(ch->device, cmd, compat_ptr(arg));
909daa6eda6SGerd Knorr  
910daa6eda6SGerd Knorr  	}
911daa6eda6SGerd Knorr  }
912daa6eda6SGerd Knorr  #endif
913daa6eda6SGerd Knorr  
914daa6eda6SGerd Knorr  /* ------------------------------------------------------------------------ */
915daa6eda6SGerd Knorr  
916daa6eda6SGerd Knorr  static int ch_probe(struct device *dev)
917daa6eda6SGerd Knorr  {
918daa6eda6SGerd Knorr  	struct scsi_device *sd = to_scsi_device(dev);
919ee959b00STony Jones 	struct device *class_dev;
920b98c52b5STejun Heo 	int ret;
921daa6eda6SGerd Knorr  	scsi_changer *ch;
922daa6eda6SGerd Knorr  
923daa6eda6SGerd Knorr  	if (sd->type != TYPE_MEDIUM_CHANGER)
924daa6eda6SGerd Knorr  		return -ENODEV;
925daa6eda6SGerd Knorr  
9264530a169Svignesh.babu@wipro.com 	ch = kzalloc(sizeof(*ch), GFP_KERNEL);
927daa6eda6SGerd Knorr  	if (NULL == ch)
928daa6eda6SGerd Knorr  		return -ENOMEM;
929daa6eda6SGerd Knorr  
930b98c52b5STejun Heo 	idr_preload(GFP_KERNEL);
931da707c54SFUJITA Tomonori 	spin_lock(&ch_index_lock);
932b98c52b5STejun Heo 	ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
933da707c54SFUJITA Tomonori 	spin_unlock(&ch_index_lock);
934b98c52b5STejun Heo 	idr_preload_end();
935da707c54SFUJITA Tomonori 
936b98c52b5STejun Heo 	if (ret < 0) {
937b98c52b5STejun Heo 		if (ret == -ENOSPC)
938da707c54SFUJITA Tomonori 			ret = -ENODEV;
939b98c52b5STejun Heo 		goto free_ch;
940da707c54SFUJITA Tomonori 	}
941da707c54SFUJITA Tomonori 
942b98c52b5STejun Heo 	ch->minor = ret;
943daa6eda6SGerd Knorr  	sprintf(ch->name,"ch%d",ch->minor);
94466167283SHannes Reinecke 	ret = scsi_device_get(sd);
94566167283SHannes Reinecke 	if (ret) {
94666167283SHannes Reinecke 		sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
94766167283SHannes Reinecke 			    ch->minor);
94866167283SHannes Reinecke 		goto remove_idr;
94966167283SHannes Reinecke 	}
950a3d2c2e8SFUJITA Tomonori 
9511c7ce4bcSHannes Reinecke 	mutex_init(&ch->lock);
9521c7ce4bcSHannes Reinecke 	kref_init(&ch->ref);
9531c7ce4bcSHannes Reinecke 	ch->device = sd;
954d73a1a67SGreg Kroah-Hartman 	class_dev = device_create(ch_sysfs_class, dev,
955d73a1a67SGreg Kroah-Hartman 				  MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
956d73a1a67SGreg Kroah-Hartman 				  "s%s", ch->name);
957a3d2c2e8SFUJITA Tomonori 	if (IS_ERR(class_dev)) {
95828c31729SHannes Reinecke 		sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
959a3d2c2e8SFUJITA Tomonori 			    ch->minor);
960da707c54SFUJITA Tomonori 		ret = PTR_ERR(class_dev);
96166167283SHannes Reinecke 		goto put_device;
962a3d2c2e8SFUJITA Tomonori 	}
963a3d2c2e8SFUJITA Tomonori 
9641c7ce4bcSHannes Reinecke 	mutex_lock(&ch->lock);
965085e5676SHannes Reinecke 	ret = ch_readconfig(ch);
9661c7ce4bcSHannes Reinecke 	if (ret) {
9671c7ce4bcSHannes Reinecke 		mutex_unlock(&ch->lock);
968085e5676SHannes Reinecke 		goto destroy_dev;
9691c7ce4bcSHannes Reinecke 	}
970daa6eda6SGerd Knorr  	if (init)
971daa6eda6SGerd Knorr  		ch_init_elem(ch);
972daa6eda6SGerd Knorr  
9731c7ce4bcSHannes Reinecke 	mutex_unlock(&ch->lock);
9743d164fb0SFUJITA Tomonori 	dev_set_drvdata(dev, ch);
975017560fcSJeff Garzik 	sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
976daa6eda6SGerd Knorr  
977daa6eda6SGerd Knorr  	return 0;
978085e5676SHannes Reinecke destroy_dev:
979085e5676SHannes Reinecke 	device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
98066167283SHannes Reinecke put_device:
98166167283SHannes Reinecke 	scsi_device_put(sd);
982da707c54SFUJITA Tomonori remove_idr:
983b98c52b5STejun Heo 	idr_remove(&ch_index_idr, ch->minor);
984da707c54SFUJITA Tomonori free_ch:
985da707c54SFUJITA Tomonori 	kfree(ch);
986da707c54SFUJITA Tomonori 	return ret;
987daa6eda6SGerd Knorr  }
988daa6eda6SGerd Knorr  
989daa6eda6SGerd Knorr  static int ch_remove(struct device *dev)
990daa6eda6SGerd Knorr  {
991da707c54SFUJITA Tomonori 	scsi_changer *ch = dev_get_drvdata(dev);
992daa6eda6SGerd Knorr  
993da707c54SFUJITA Tomonori 	spin_lock(&ch_index_lock);
994da707c54SFUJITA Tomonori 	idr_remove(&ch_index_idr, ch->minor);
99566167283SHannes Reinecke 	dev_set_drvdata(dev, NULL);
996da707c54SFUJITA Tomonori 	spin_unlock(&ch_index_lock);
997daa6eda6SGerd Knorr  
998ee959b00STony Jones 	device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
99966167283SHannes Reinecke 	scsi_device_put(ch->device);
1000085e5676SHannes Reinecke 	kref_put(&ch->ref, ch_destroy);
1001daa6eda6SGerd Knorr  	return 0;
1002daa6eda6SGerd Knorr  }
1003daa6eda6SGerd Knorr  
10045aa22af3SFUJITA Tomonori static struct scsi_driver ch_template = {
10055aa22af3SFUJITA Tomonori 	.gendrv     	= {
10065aa22af3SFUJITA Tomonori 		.name	= "ch",
10073af6b352SChristoph Hellwig 		.owner	= THIS_MODULE,
10085aa22af3SFUJITA Tomonori 		.probe  = ch_probe,
10095aa22af3SFUJITA Tomonori 		.remove = ch_remove,
10105aa22af3SFUJITA Tomonori 	},
10115aa22af3SFUJITA Tomonori };
10125aa22af3SFUJITA Tomonori 
10135aa22af3SFUJITA Tomonori static const struct file_operations changer_fops = {
10145aa22af3SFUJITA Tomonori 	.owner		= THIS_MODULE,
10155aa22af3SFUJITA Tomonori 	.open		= ch_open,
10165aa22af3SFUJITA Tomonori 	.release	= ch_release,
10175aa22af3SFUJITA Tomonori 	.unlocked_ioctl	= ch_ioctl,
10185aa22af3SFUJITA Tomonori #ifdef CONFIG_COMPAT
10195aa22af3SFUJITA Tomonori 	.compat_ioctl	= ch_ioctl_compat,
10205aa22af3SFUJITA Tomonori #endif
10216038f373SArnd Bergmann 	.llseek		= noop_llseek,
10225aa22af3SFUJITA Tomonori };
10235aa22af3SFUJITA Tomonori 
1024daa6eda6SGerd Knorr  static int __init init_ch_module(void)
1025daa6eda6SGerd Knorr  {
1026daa6eda6SGerd Knorr  	int rc;
1027daa6eda6SGerd Knorr  
1028daa6eda6SGerd Knorr  	printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
102921feb5ccSGerd Knorr          ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer");
1030daa6eda6SGerd Knorr          if (IS_ERR(ch_sysfs_class)) {
1031daa6eda6SGerd Knorr  		rc = PTR_ERR(ch_sysfs_class);
1032daa6eda6SGerd Knorr  		return rc;
1033daa6eda6SGerd Knorr          }
1034daa6eda6SGerd Knorr  	rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
1035daa6eda6SGerd Knorr  	if (rc < 0) {
1036daa6eda6SGerd Knorr  		printk("Unable to get major %d for SCSI-Changer\n",
1037daa6eda6SGerd Knorr  		       SCSI_CHANGER_MAJOR);
1038daa6eda6SGerd Knorr  		goto fail1;
1039daa6eda6SGerd Knorr  	}
1040daa6eda6SGerd Knorr  	rc = scsi_register_driver(&ch_template.gendrv);
1041daa6eda6SGerd Knorr  	if (rc < 0)
1042daa6eda6SGerd Knorr  		goto fail2;
1043daa6eda6SGerd Knorr  	return 0;
1044daa6eda6SGerd Knorr  
1045daa6eda6SGerd Knorr   fail2:
1046daa6eda6SGerd Knorr  	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
1047daa6eda6SGerd Knorr   fail1:
104821feb5ccSGerd Knorr  	class_destroy(ch_sysfs_class);
1049daa6eda6SGerd Knorr  	return rc;
1050daa6eda6SGerd Knorr  }
1051daa6eda6SGerd Knorr  
1052daa6eda6SGerd Knorr  static void __exit exit_ch_module(void)
1053daa6eda6SGerd Knorr  {
1054daa6eda6SGerd Knorr  	scsi_unregister_driver(&ch_template.gendrv);
1055daa6eda6SGerd Knorr  	unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
105621feb5ccSGerd Knorr  	class_destroy(ch_sysfs_class);
1057da707c54SFUJITA Tomonori 	idr_destroy(&ch_index_idr);
1058daa6eda6SGerd Knorr  }
1059daa6eda6SGerd Knorr  
1060daa6eda6SGerd Knorr  module_init(init_ch_module);
1061daa6eda6SGerd Knorr  module_exit(exit_ch_module);
1062