1daa6eda6SGerd Knorr /* 2daa6eda6SGerd Knorr * SCSI Media Changer device driver for Linux 2.6 3daa6eda6SGerd Knorr * 4daa6eda6SGerd Knorr * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org> 5daa6eda6SGerd Knorr * 6daa6eda6SGerd Knorr */ 7daa6eda6SGerd Knorr 8daa6eda6SGerd Knorr #define VERSION "0.25" 9daa6eda6SGerd Knorr 10daa6eda6SGerd Knorr #include <linux/module.h> 11daa6eda6SGerd Knorr #include <linux/init.h> 12daa6eda6SGerd Knorr #include <linux/fs.h> 13daa6eda6SGerd Knorr #include <linux/kernel.h> 14daa6eda6SGerd Knorr #include <linux/mm.h> 15daa6eda6SGerd Knorr #include <linux/major.h> 16daa6eda6SGerd Knorr #include <linux/string.h> 17daa6eda6SGerd Knorr #include <linux/errno.h> 18daa6eda6SGerd Knorr #include <linux/interrupt.h> 19daa6eda6SGerd Knorr #include <linux/blkdev.h> 20daa6eda6SGerd Knorr #include <linux/completion.h> 21daa6eda6SGerd Knorr #include <linux/compat.h> 22daa6eda6SGerd Knorr #include <linux/chio.h> /* here are all the ioctls */ 230b950672SArjan van de Ven #include <linux/mutex.h> 24da707c54SFUJITA Tomonori #include <linux/idr.h> 25b0061a0eSJonathan Corbet #include <linux/smp_lock.h> 26daa6eda6SGerd Knorr 27daa6eda6SGerd Knorr #include <scsi/scsi.h> 28daa6eda6SGerd Knorr #include <scsi/scsi_cmnd.h> 29daa6eda6SGerd Knorr #include <scsi/scsi_driver.h> 30daa6eda6SGerd Knorr #include <scsi/scsi_ioctl.h> 31daa6eda6SGerd Knorr #include <scsi/scsi_host.h> 32daa6eda6SGerd Knorr #include <scsi/scsi_device.h> 3384743bbcSJames Bottomley #include <scsi/scsi_eh.h> 34daa6eda6SGerd Knorr #include <scsi/scsi_dbg.h> 35daa6eda6SGerd Knorr 36daa6eda6SGerd Knorr #define CH_DT_MAX 16 37daa6eda6SGerd Knorr #define CH_TYPES 8 38da707c54SFUJITA Tomonori #define CH_MAX_DEVS 128 39daa6eda6SGerd Knorr 40daa6eda6SGerd Knorr MODULE_DESCRIPTION("device driver for scsi media changer devices"); 41daa6eda6SGerd Knorr MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>"); 42daa6eda6SGerd Knorr MODULE_LICENSE("GPL"); 43f018fa55SRene Herman MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR); 4495f6c83fSScott James Remnant MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER); 45daa6eda6SGerd Knorr 46daa6eda6SGerd Knorr static int init = 1; 47daa6eda6SGerd Knorr module_param(init, int, 0444); 48daa6eda6SGerd Knorr MODULE_PARM_DESC(init, \ 49daa6eda6SGerd Knorr "initialize element status on driver load (default: on)"); 50daa6eda6SGerd Knorr 51daa6eda6SGerd Knorr static int timeout_move = 300; 52daa6eda6SGerd Knorr module_param(timeout_move, int, 0644); 53daa6eda6SGerd Knorr MODULE_PARM_DESC(timeout_move,"timeout for move commands " 54daa6eda6SGerd Knorr "(default: 300 seconds)"); 55daa6eda6SGerd Knorr 56daa6eda6SGerd Knorr static int timeout_init = 3600; 57daa6eda6SGerd Knorr module_param(timeout_init, int, 0644); 58daa6eda6SGerd Knorr MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS " 59daa6eda6SGerd Knorr "(default: 3600 seconds)"); 60daa6eda6SGerd Knorr 61daa6eda6SGerd Knorr static int verbose = 1; 62daa6eda6SGerd Knorr module_param(verbose, int, 0644); 63daa6eda6SGerd Knorr MODULE_PARM_DESC(verbose,"be verbose (default: on)"); 64daa6eda6SGerd Knorr 65daa6eda6SGerd Knorr static int debug = 0; 66daa6eda6SGerd Knorr module_param(debug, int, 0644); 67daa6eda6SGerd Knorr MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more " 68daa6eda6SGerd Knorr "detailed sense codes on scsi errors (default: off)"); 69daa6eda6SGerd Knorr 70daa6eda6SGerd Knorr static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 }; 71daa6eda6SGerd Knorr static int dt_lun[CH_DT_MAX]; 72daa6eda6SGerd Knorr module_param_array(dt_id, int, NULL, 0444); 73daa6eda6SGerd Knorr module_param_array(dt_lun, int, NULL, 0444); 74daa6eda6SGerd Knorr 75daa6eda6SGerd Knorr /* tell the driver about vendor-specific slots */ 76daa6eda6SGerd Knorr static int vendor_firsts[CH_TYPES-4]; 77daa6eda6SGerd Knorr static int vendor_counts[CH_TYPES-4]; 78daa6eda6SGerd Knorr module_param_array(vendor_firsts, int, NULL, 0444); 79daa6eda6SGerd Knorr module_param_array(vendor_counts, int, NULL, 0444); 80daa6eda6SGerd Knorr 810ad78200SArjan van de Ven static const char * vendor_labels[CH_TYPES-4] = { 82daa6eda6SGerd Knorr "v0", "v1", "v2", "v3" 83daa6eda6SGerd Knorr }; 84daa6eda6SGerd Knorr // module_param_string_array(vendor_labels, NULL, 0444); 85daa6eda6SGerd Knorr 86daa6eda6SGerd Knorr #define dprintk(fmt, arg...) if (debug) \ 87daa6eda6SGerd Knorr printk(KERN_DEBUG "%s: " fmt, ch->name , ## arg) 88daa6eda6SGerd Knorr #define vprintk(fmt, arg...) if (verbose) \ 89daa6eda6SGerd Knorr printk(KERN_INFO "%s: " fmt, ch->name , ## arg) 90daa6eda6SGerd Knorr 91daa6eda6SGerd Knorr /* ------------------------------------------------------------------- */ 92daa6eda6SGerd Knorr 93daa6eda6SGerd Knorr #define MAX_RETRIES 1 94daa6eda6SGerd Knorr 9521feb5ccSGerd Knorr static struct class * ch_sysfs_class; 96daa6eda6SGerd Knorr 97daa6eda6SGerd Knorr typedef struct { 98daa6eda6SGerd Knorr struct list_head list; 99daa6eda6SGerd Knorr int minor; 100daa6eda6SGerd Knorr char name[8]; 101daa6eda6SGerd Knorr struct scsi_device *device; 102daa6eda6SGerd Knorr struct scsi_device **dt; /* ptrs to data transfer elements */ 103daa6eda6SGerd Knorr u_int firsts[CH_TYPES]; 104daa6eda6SGerd Knorr u_int counts[CH_TYPES]; 105daa6eda6SGerd Knorr u_int unit_attention; 106daa6eda6SGerd Knorr u_int voltags; 1070b950672SArjan van de Ven struct mutex lock; 108daa6eda6SGerd Knorr } scsi_changer; 109daa6eda6SGerd Knorr 110da707c54SFUJITA Tomonori static DEFINE_IDR(ch_index_idr); 111da707c54SFUJITA Tomonori static DEFINE_SPINLOCK(ch_index_lock); 112daa6eda6SGerd Knorr 1130ad78200SArjan van de Ven static const struct { 114daa6eda6SGerd Knorr unsigned char sense; 115daa6eda6SGerd Knorr unsigned char asc; 116daa6eda6SGerd Knorr unsigned char ascq; 117daa6eda6SGerd Knorr int errno; 118d70d4667SHarvey Harrison } ch_err[] = { 119daa6eda6SGerd Knorr /* Just filled in what looks right. Hav'nt checked any standard paper for 120daa6eda6SGerd Knorr these errno assignments, so they may be wrong... */ 121daa6eda6SGerd Knorr { 122daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 123daa6eda6SGerd Knorr .asc = 0x21, 124daa6eda6SGerd Knorr .ascq = 0x01, 125daa6eda6SGerd Knorr .errno = EBADSLT, /* Invalid element address */ 126daa6eda6SGerd Knorr },{ 127daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 128daa6eda6SGerd Knorr .asc = 0x28, 129daa6eda6SGerd Knorr .ascq = 0x01, 130daa6eda6SGerd Knorr .errno = EBADE, /* Import or export element accessed */ 131daa6eda6SGerd Knorr },{ 132daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 133daa6eda6SGerd Knorr .asc = 0x3B, 134daa6eda6SGerd Knorr .ascq = 0x0D, 135daa6eda6SGerd Knorr .errno = EXFULL, /* Medium destination element full */ 136daa6eda6SGerd Knorr },{ 137daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 138daa6eda6SGerd Knorr .asc = 0x3B, 139daa6eda6SGerd Knorr .ascq = 0x0E, 140daa6eda6SGerd Knorr .errno = EBADE, /* Medium source element empty */ 141daa6eda6SGerd Knorr },{ 142daa6eda6SGerd Knorr .sense = ILLEGAL_REQUEST, 143daa6eda6SGerd Knorr .asc = 0x20, 144daa6eda6SGerd Knorr .ascq = 0x00, 145daa6eda6SGerd Knorr .errno = EBADRQC, /* Invalid command operation code */ 146daa6eda6SGerd Knorr },{ 147daa6eda6SGerd Knorr /* end of list */ 148daa6eda6SGerd Knorr } 149daa6eda6SGerd Knorr }; 150daa6eda6SGerd Knorr 151daa6eda6SGerd Knorr /* ------------------------------------------------------------------- */ 152daa6eda6SGerd Knorr 15384743bbcSJames Bottomley static int ch_find_errno(struct scsi_sense_hdr *sshdr) 154daa6eda6SGerd Knorr { 155daa6eda6SGerd Knorr int i,errno = 0; 156daa6eda6SGerd Knorr 157daa6eda6SGerd Knorr /* Check to see if additional sense information is available */ 15884743bbcSJames Bottomley if (scsi_sense_valid(sshdr) && 15984743bbcSJames Bottomley sshdr->asc != 0) { 160d70d4667SHarvey Harrison for (i = 0; ch_err[i].errno != 0; i++) { 161d70d4667SHarvey Harrison if (ch_err[i].sense == sshdr->sense_key && 162d70d4667SHarvey Harrison ch_err[i].asc == sshdr->asc && 163d70d4667SHarvey Harrison ch_err[i].ascq == sshdr->ascq) { 164d70d4667SHarvey Harrison errno = -ch_err[i].errno; 165daa6eda6SGerd Knorr break; 166daa6eda6SGerd Knorr } 167daa6eda6SGerd Knorr } 168daa6eda6SGerd Knorr } 169daa6eda6SGerd Knorr if (errno == 0) 170daa6eda6SGerd Knorr errno = -EIO; 171daa6eda6SGerd Knorr return errno; 172daa6eda6SGerd Knorr } 173daa6eda6SGerd Knorr 174daa6eda6SGerd Knorr static int 175daa6eda6SGerd Knorr ch_do_scsi(scsi_changer *ch, unsigned char *cmd, 176daa6eda6SGerd Knorr void *buffer, unsigned buflength, 177daa6eda6SGerd Knorr enum dma_data_direction direction) 178daa6eda6SGerd Knorr { 17984743bbcSJames Bottomley int errno, retries = 0, timeout, result; 18084743bbcSJames Bottomley struct scsi_sense_hdr sshdr; 181daa6eda6SGerd Knorr 182daa6eda6SGerd Knorr timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS) 183daa6eda6SGerd Knorr ? timeout_init : timeout_move; 184daa6eda6SGerd Knorr 185daa6eda6SGerd Knorr retry: 186daa6eda6SGerd Knorr errno = 0; 187daa6eda6SGerd Knorr if (debug) { 188daa6eda6SGerd Knorr dprintk("command: "); 189daa6eda6SGerd Knorr __scsi_print_command(cmd); 190daa6eda6SGerd Knorr } 191daa6eda6SGerd Knorr 19284743bbcSJames Bottomley result = scsi_execute_req(ch->device, cmd, direction, buffer, 19384743bbcSJames Bottomley buflength, &sshdr, timeout * HZ, 194f4f4e47eSFUJITA Tomonori MAX_RETRIES, NULL); 195daa6eda6SGerd Knorr 19684743bbcSJames Bottomley dprintk("result: 0x%x\n",result); 19784743bbcSJames Bottomley if (driver_byte(result) & DRIVER_SENSE) { 198daa6eda6SGerd Knorr if (debug) 19984743bbcSJames Bottomley scsi_print_sense_hdr(ch->name, &sshdr); 20084743bbcSJames Bottomley errno = ch_find_errno(&sshdr); 201daa6eda6SGerd Knorr 20284743bbcSJames Bottomley switch(sshdr.sense_key) { 203daa6eda6SGerd Knorr case UNIT_ATTENTION: 204daa6eda6SGerd Knorr ch->unit_attention = 1; 205daa6eda6SGerd Knorr if (retries++ < 3) 206daa6eda6SGerd Knorr goto retry; 207daa6eda6SGerd Knorr break; 208daa6eda6SGerd Knorr } 209daa6eda6SGerd Knorr } 210daa6eda6SGerd Knorr return errno; 211daa6eda6SGerd Knorr } 212daa6eda6SGerd Knorr 213daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 214daa6eda6SGerd Knorr 215daa6eda6SGerd Knorr static int 216daa6eda6SGerd Knorr ch_elem_to_typecode(scsi_changer *ch, u_int elem) 217daa6eda6SGerd Knorr { 218daa6eda6SGerd Knorr int i; 219daa6eda6SGerd Knorr 220daa6eda6SGerd Knorr for (i = 0; i < CH_TYPES; i++) { 221daa6eda6SGerd Knorr if (elem >= ch->firsts[i] && 222daa6eda6SGerd Knorr elem < ch->firsts[i] + 223daa6eda6SGerd Knorr ch->counts[i]) 224daa6eda6SGerd Knorr return i+1; 225daa6eda6SGerd Knorr } 226daa6eda6SGerd Knorr return 0; 227daa6eda6SGerd Knorr } 228daa6eda6SGerd Knorr 229daa6eda6SGerd Knorr static int 230daa6eda6SGerd Knorr ch_read_element_status(scsi_changer *ch, u_int elem, char *data) 231daa6eda6SGerd Knorr { 232daa6eda6SGerd Knorr u_char cmd[12]; 233daa6eda6SGerd Knorr u_char *buffer; 234daa6eda6SGerd Knorr int result; 235daa6eda6SGerd Knorr 236daa6eda6SGerd Knorr buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 237daa6eda6SGerd Knorr if(!buffer) 238daa6eda6SGerd Knorr return -ENOMEM; 239daa6eda6SGerd Knorr 240daa6eda6SGerd Knorr retry: 241daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 242daa6eda6SGerd Knorr cmd[0] = READ_ELEMENT_STATUS; 243daa6eda6SGerd Knorr cmd[1] = (ch->device->lun << 5) | 244daa6eda6SGerd Knorr (ch->voltags ? 0x10 : 0) | 245daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 246daa6eda6SGerd Knorr cmd[2] = (elem >> 8) & 0xff; 247daa6eda6SGerd Knorr cmd[3] = elem & 0xff; 248daa6eda6SGerd Knorr cmd[5] = 1; 249daa6eda6SGerd Knorr cmd[9] = 255; 250daa6eda6SGerd Knorr if (0 == (result = ch_do_scsi(ch, cmd, buffer, 256, DMA_FROM_DEVICE))) { 251daa6eda6SGerd Knorr if (((buffer[16] << 8) | buffer[17]) != elem) { 252daa6eda6SGerd Knorr dprintk("asked for element 0x%02x, got 0x%02x\n", 253daa6eda6SGerd Knorr elem,(buffer[16] << 8) | buffer[17]); 254daa6eda6SGerd Knorr kfree(buffer); 255daa6eda6SGerd Knorr return -EIO; 256daa6eda6SGerd Knorr } 257daa6eda6SGerd Knorr memcpy(data,buffer+16,16); 258daa6eda6SGerd Knorr } else { 259daa6eda6SGerd Knorr if (ch->voltags) { 260daa6eda6SGerd Knorr ch->voltags = 0; 261daa6eda6SGerd Knorr vprintk("device has no volume tag support\n"); 262daa6eda6SGerd Knorr goto retry; 263daa6eda6SGerd Knorr } 264daa6eda6SGerd Knorr dprintk("READ ELEMENT STATUS for element 0x%x failed\n",elem); 265daa6eda6SGerd Knorr } 266daa6eda6SGerd Knorr kfree(buffer); 267daa6eda6SGerd Knorr return result; 268daa6eda6SGerd Knorr } 269daa6eda6SGerd Knorr 270daa6eda6SGerd Knorr static int 271daa6eda6SGerd Knorr ch_init_elem(scsi_changer *ch) 272daa6eda6SGerd Knorr { 273daa6eda6SGerd Knorr int err; 274daa6eda6SGerd Knorr u_char cmd[6]; 275daa6eda6SGerd Knorr 276daa6eda6SGerd Knorr vprintk("INITIALIZE ELEMENT STATUS, may take some time ...\n"); 277daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 278daa6eda6SGerd Knorr cmd[0] = INITIALIZE_ELEMENT_STATUS; 279daa6eda6SGerd Knorr cmd[1] = ch->device->lun << 5; 280daa6eda6SGerd Knorr err = ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE); 281daa6eda6SGerd Knorr vprintk("... finished\n"); 282daa6eda6SGerd Knorr return err; 283daa6eda6SGerd Knorr } 284daa6eda6SGerd Knorr 285daa6eda6SGerd Knorr static int 286daa6eda6SGerd Knorr ch_readconfig(scsi_changer *ch) 287daa6eda6SGerd Knorr { 288daa6eda6SGerd Knorr u_char cmd[10], data[16]; 289daa6eda6SGerd Knorr u_char *buffer; 290daa6eda6SGerd Knorr int result,id,lun,i; 291daa6eda6SGerd Knorr u_int elem; 292daa6eda6SGerd Knorr 2934530a169Svignesh.babu@wipro.com buffer = kzalloc(512, GFP_KERNEL | GFP_DMA); 294daa6eda6SGerd Knorr if (!buffer) 295daa6eda6SGerd Knorr return -ENOMEM; 296daa6eda6SGerd Knorr 297daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 298daa6eda6SGerd Knorr cmd[0] = MODE_SENSE; 299daa6eda6SGerd Knorr cmd[1] = ch->device->lun << 5; 300daa6eda6SGerd Knorr cmd[2] = 0x1d; 301daa6eda6SGerd Knorr cmd[4] = 255; 302daa6eda6SGerd Knorr result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE); 303daa6eda6SGerd Knorr if (0 != result) { 304daa6eda6SGerd Knorr cmd[1] |= (1<<3); 305daa6eda6SGerd Knorr result = ch_do_scsi(ch, cmd, buffer, 255, DMA_FROM_DEVICE); 306daa6eda6SGerd Knorr } 307daa6eda6SGerd Knorr if (0 == result) { 308daa6eda6SGerd Knorr ch->firsts[CHET_MT] = 309daa6eda6SGerd Knorr (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7]; 310daa6eda6SGerd Knorr ch->counts[CHET_MT] = 311daa6eda6SGerd Knorr (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9]; 312daa6eda6SGerd Knorr ch->firsts[CHET_ST] = 313daa6eda6SGerd Knorr (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11]; 314daa6eda6SGerd Knorr ch->counts[CHET_ST] = 315daa6eda6SGerd Knorr (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13]; 316daa6eda6SGerd Knorr ch->firsts[CHET_IE] = 317daa6eda6SGerd Knorr (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15]; 318daa6eda6SGerd Knorr ch->counts[CHET_IE] = 319daa6eda6SGerd Knorr (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17]; 320daa6eda6SGerd Knorr ch->firsts[CHET_DT] = 321daa6eda6SGerd Knorr (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19]; 322daa6eda6SGerd Knorr ch->counts[CHET_DT] = 323daa6eda6SGerd Knorr (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21]; 324daa6eda6SGerd Knorr vprintk("type #1 (mt): 0x%x+%d [medium transport]\n", 325daa6eda6SGerd Knorr ch->firsts[CHET_MT], 326daa6eda6SGerd Knorr ch->counts[CHET_MT]); 327daa6eda6SGerd Knorr vprintk("type #2 (st): 0x%x+%d [storage]\n", 328daa6eda6SGerd Knorr ch->firsts[CHET_ST], 329daa6eda6SGerd Knorr ch->counts[CHET_ST]); 330daa6eda6SGerd Knorr vprintk("type #3 (ie): 0x%x+%d [import/export]\n", 331daa6eda6SGerd Knorr ch->firsts[CHET_IE], 332daa6eda6SGerd Knorr ch->counts[CHET_IE]); 333daa6eda6SGerd Knorr vprintk("type #4 (dt): 0x%x+%d [data transfer]\n", 334daa6eda6SGerd Knorr ch->firsts[CHET_DT], 335daa6eda6SGerd Knorr ch->counts[CHET_DT]); 336daa6eda6SGerd Knorr } else { 337daa6eda6SGerd Knorr vprintk("reading element address assigment page failed!\n"); 338daa6eda6SGerd Knorr } 339daa6eda6SGerd Knorr 340daa6eda6SGerd Knorr /* vendor specific element types */ 341daa6eda6SGerd Knorr for (i = 0; i < 4; i++) { 342daa6eda6SGerd Knorr if (0 == vendor_counts[i]) 343daa6eda6SGerd Knorr continue; 344daa6eda6SGerd Knorr if (NULL == vendor_labels[i]) 345daa6eda6SGerd Knorr continue; 346daa6eda6SGerd Knorr ch->firsts[CHET_V1+i] = vendor_firsts[i]; 347daa6eda6SGerd Knorr ch->counts[CHET_V1+i] = vendor_counts[i]; 348daa6eda6SGerd Knorr vprintk("type #%d (v%d): 0x%x+%d [%s, vendor specific]\n", 349daa6eda6SGerd Knorr i+5,i+1,vendor_firsts[i],vendor_counts[i], 350daa6eda6SGerd Knorr vendor_labels[i]); 351daa6eda6SGerd Knorr } 352daa6eda6SGerd Knorr 353daa6eda6SGerd Knorr /* look up the devices of the data transfer elements */ 354daa6eda6SGerd Knorr ch->dt = kmalloc(ch->counts[CHET_DT]*sizeof(struct scsi_device), 355daa6eda6SGerd Knorr GFP_KERNEL); 356a2cf8a63SDavidlohr Bueso A 357a2cf8a63SDavidlohr Bueso A if (!ch->dt) { 358a2cf8a63SDavidlohr Bueso A kfree(buffer); 359a2cf8a63SDavidlohr Bueso A return -ENOMEM; 360a2cf8a63SDavidlohr Bueso A } 361a2cf8a63SDavidlohr Bueso A 362daa6eda6SGerd Knorr for (elem = 0; elem < ch->counts[CHET_DT]; elem++) { 363daa6eda6SGerd Knorr id = -1; 364daa6eda6SGerd Knorr lun = 0; 365daa6eda6SGerd Knorr if (elem < CH_DT_MAX && -1 != dt_id[elem]) { 366daa6eda6SGerd Knorr id = dt_id[elem]; 367daa6eda6SGerd Knorr lun = dt_lun[elem]; 368daa6eda6SGerd Knorr vprintk("dt 0x%x: [insmod option] ", 369daa6eda6SGerd Knorr elem+ch->firsts[CHET_DT]); 370daa6eda6SGerd Knorr } else if (0 != ch_read_element_status 371daa6eda6SGerd Knorr (ch,elem+ch->firsts[CHET_DT],data)) { 372daa6eda6SGerd Knorr vprintk("dt 0x%x: READ ELEMENT STATUS failed\n", 373daa6eda6SGerd Knorr elem+ch->firsts[CHET_DT]); 374daa6eda6SGerd Knorr } else { 375daa6eda6SGerd Knorr vprintk("dt 0x%x: ",elem+ch->firsts[CHET_DT]); 376daa6eda6SGerd Knorr if (data[6] & 0x80) { 377daa6eda6SGerd Knorr if (verbose) 378daa6eda6SGerd Knorr printk("not this SCSI bus\n"); 379daa6eda6SGerd Knorr ch->dt[elem] = NULL; 380daa6eda6SGerd Knorr } else if (0 == (data[6] & 0x30)) { 381daa6eda6SGerd Knorr if (verbose) 382daa6eda6SGerd Knorr printk("ID/LUN unknown\n"); 383daa6eda6SGerd Knorr ch->dt[elem] = NULL; 384daa6eda6SGerd Knorr } else { 385daa6eda6SGerd Knorr id = ch->device->id; 386daa6eda6SGerd Knorr lun = 0; 387daa6eda6SGerd Knorr if (data[6] & 0x20) id = data[7]; 388daa6eda6SGerd Knorr if (data[6] & 0x10) lun = data[6] & 7; 389daa6eda6SGerd Knorr } 390daa6eda6SGerd Knorr } 391daa6eda6SGerd Knorr if (-1 != id) { 392daa6eda6SGerd Knorr if (verbose) 393daa6eda6SGerd Knorr printk("ID %i, LUN %i, ",id,lun); 394daa6eda6SGerd Knorr ch->dt[elem] = 395daa6eda6SGerd Knorr scsi_device_lookup(ch->device->host, 396daa6eda6SGerd Knorr ch->device->channel, 397daa6eda6SGerd Knorr id,lun); 398daa6eda6SGerd Knorr if (!ch->dt[elem]) { 399daa6eda6SGerd Knorr /* should not happen */ 400daa6eda6SGerd Knorr if (verbose) 401daa6eda6SGerd Knorr printk("Huh? device not found!\n"); 402daa6eda6SGerd Knorr } else { 403daa6eda6SGerd Knorr if (verbose) 404daa6eda6SGerd Knorr printk("name: %8.8s %16.16s %4.4s\n", 405daa6eda6SGerd Knorr ch->dt[elem]->vendor, 406daa6eda6SGerd Knorr ch->dt[elem]->model, 407daa6eda6SGerd Knorr ch->dt[elem]->rev); 408daa6eda6SGerd Knorr } 409daa6eda6SGerd Knorr } 410daa6eda6SGerd Knorr } 411daa6eda6SGerd Knorr ch->voltags = 1; 412daa6eda6SGerd Knorr kfree(buffer); 413daa6eda6SGerd Knorr 414daa6eda6SGerd Knorr return 0; 415daa6eda6SGerd Knorr } 416daa6eda6SGerd Knorr 417daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 418daa6eda6SGerd Knorr 419daa6eda6SGerd Knorr static int 420daa6eda6SGerd Knorr ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate) 421daa6eda6SGerd Knorr { 422daa6eda6SGerd Knorr u_char cmd[10]; 423daa6eda6SGerd Knorr 424daa6eda6SGerd Knorr dprintk("position: 0x%x\n",elem); 425daa6eda6SGerd Knorr if (0 == trans) 426daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 427daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 428daa6eda6SGerd Knorr cmd[0] = POSITION_TO_ELEMENT; 429daa6eda6SGerd Knorr cmd[1] = ch->device->lun << 5; 430daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 431daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 432daa6eda6SGerd Knorr cmd[4] = (elem >> 8) & 0xff; 433daa6eda6SGerd Knorr cmd[5] = elem & 0xff; 434daa6eda6SGerd Knorr cmd[8] = rotate ? 1 : 0; 435daa6eda6SGerd Knorr return ch_do_scsi(ch, cmd, NULL, 0, DMA_NONE); 436daa6eda6SGerd Knorr } 437daa6eda6SGerd Knorr 438daa6eda6SGerd Knorr static int 439daa6eda6SGerd Knorr ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate) 440daa6eda6SGerd Knorr { 441daa6eda6SGerd Knorr u_char cmd[12]; 442daa6eda6SGerd Knorr 443daa6eda6SGerd Knorr dprintk("move: 0x%x => 0x%x\n",src,dest); 444daa6eda6SGerd Knorr if (0 == trans) 445daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 446daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 447daa6eda6SGerd Knorr cmd[0] = MOVE_MEDIUM; 448daa6eda6SGerd Knorr cmd[1] = ch->device->lun << 5; 449daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 450daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 451daa6eda6SGerd Knorr cmd[4] = (src >> 8) & 0xff; 452daa6eda6SGerd Knorr cmd[5] = src & 0xff; 453daa6eda6SGerd Knorr cmd[6] = (dest >> 8) & 0xff; 454daa6eda6SGerd Knorr cmd[7] = dest & 0xff; 455daa6eda6SGerd Knorr cmd[10] = rotate ? 1 : 0; 456daa6eda6SGerd Knorr return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE); 457daa6eda6SGerd Knorr } 458daa6eda6SGerd Knorr 459daa6eda6SGerd Knorr static int 460daa6eda6SGerd Knorr ch_exchange(scsi_changer *ch, u_int trans, u_int src, 461daa6eda6SGerd Knorr u_int dest1, u_int dest2, int rotate1, int rotate2) 462daa6eda6SGerd Knorr { 463daa6eda6SGerd Knorr u_char cmd[12]; 464daa6eda6SGerd Knorr 465daa6eda6SGerd Knorr dprintk("exchange: 0x%x => 0x%x => 0x%x\n", 466daa6eda6SGerd Knorr src,dest1,dest2); 467daa6eda6SGerd Knorr if (0 == trans) 468daa6eda6SGerd Knorr trans = ch->firsts[CHET_MT]; 469daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 470daa6eda6SGerd Knorr cmd[0] = EXCHANGE_MEDIUM; 471daa6eda6SGerd Knorr cmd[1] = ch->device->lun << 5; 472daa6eda6SGerd Knorr cmd[2] = (trans >> 8) & 0xff; 473daa6eda6SGerd Knorr cmd[3] = trans & 0xff; 474daa6eda6SGerd Knorr cmd[4] = (src >> 8) & 0xff; 475daa6eda6SGerd Knorr cmd[5] = src & 0xff; 476daa6eda6SGerd Knorr cmd[6] = (dest1 >> 8) & 0xff; 477daa6eda6SGerd Knorr cmd[7] = dest1 & 0xff; 478daa6eda6SGerd Knorr cmd[8] = (dest2 >> 8) & 0xff; 479daa6eda6SGerd Knorr cmd[9] = dest2 & 0xff; 480daa6eda6SGerd Knorr cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0); 481daa6eda6SGerd Knorr 482daa6eda6SGerd Knorr return ch_do_scsi(ch, cmd, NULL,0, DMA_NONE); 483daa6eda6SGerd Knorr } 484daa6eda6SGerd Knorr 485daa6eda6SGerd Knorr static void 486daa6eda6SGerd Knorr ch_check_voltag(char *tag) 487daa6eda6SGerd Knorr { 488daa6eda6SGerd Knorr int i; 489daa6eda6SGerd Knorr 490daa6eda6SGerd Knorr for (i = 0; i < 32; i++) { 491daa6eda6SGerd Knorr /* restrict to ascii */ 492daa6eda6SGerd Knorr if (tag[i] >= 0x7f || tag[i] < 0x20) 493daa6eda6SGerd Knorr tag[i] = ' '; 494daa6eda6SGerd Knorr /* don't allow search wildcards */ 495daa6eda6SGerd Knorr if (tag[i] == '?' || 496daa6eda6SGerd Knorr tag[i] == '*') 497daa6eda6SGerd Knorr tag[i] = ' '; 498daa6eda6SGerd Knorr } 499daa6eda6SGerd Knorr } 500daa6eda6SGerd Knorr 501daa6eda6SGerd Knorr static int 502daa6eda6SGerd Knorr ch_set_voltag(scsi_changer *ch, u_int elem, 503daa6eda6SGerd Knorr int alternate, int clear, u_char *tag) 504daa6eda6SGerd Knorr { 505daa6eda6SGerd Knorr u_char cmd[12]; 506daa6eda6SGerd Knorr u_char *buffer; 507daa6eda6SGerd Knorr int result; 508daa6eda6SGerd Knorr 5094530a169Svignesh.babu@wipro.com buffer = kzalloc(512, GFP_KERNEL); 510daa6eda6SGerd Knorr if (!buffer) 511daa6eda6SGerd Knorr return -ENOMEM; 512daa6eda6SGerd Knorr 513daa6eda6SGerd Knorr dprintk("%s %s voltag: 0x%x => \"%s\"\n", 514daa6eda6SGerd Knorr clear ? "clear" : "set", 515daa6eda6SGerd Knorr alternate ? "alternate" : "primary", 516daa6eda6SGerd Knorr elem, tag); 517daa6eda6SGerd Knorr memset(cmd,0,sizeof(cmd)); 518daa6eda6SGerd Knorr cmd[0] = SEND_VOLUME_TAG; 519daa6eda6SGerd Knorr cmd[1] = (ch->device->lun << 5) | 520daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 521daa6eda6SGerd Knorr cmd[2] = (elem >> 8) & 0xff; 522daa6eda6SGerd Knorr cmd[3] = elem & 0xff; 523daa6eda6SGerd Knorr cmd[5] = clear 524daa6eda6SGerd Knorr ? (alternate ? 0x0d : 0x0c) 525daa6eda6SGerd Knorr : (alternate ? 0x0b : 0x0a); 526daa6eda6SGerd Knorr 527daa6eda6SGerd Knorr cmd[9] = 255; 528daa6eda6SGerd Knorr 529daa6eda6SGerd Knorr memcpy(buffer,tag,32); 530daa6eda6SGerd Knorr ch_check_voltag(buffer); 531daa6eda6SGerd Knorr 532daa6eda6SGerd Knorr result = ch_do_scsi(ch, cmd, buffer, 256, DMA_TO_DEVICE); 533daa6eda6SGerd Knorr kfree(buffer); 534daa6eda6SGerd Knorr return result; 535daa6eda6SGerd Knorr } 536daa6eda6SGerd Knorr 537fe08ac31Sviro@ZenIV.linux.org.uk static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) 538daa6eda6SGerd Knorr { 539daa6eda6SGerd Knorr int retval = 0; 540daa6eda6SGerd Knorr u_char data[16]; 541daa6eda6SGerd Knorr unsigned int i; 542daa6eda6SGerd Knorr 5430b950672SArjan van de Ven mutex_lock(&ch->lock); 544daa6eda6SGerd Knorr for (i = 0; i < ch->counts[type]; i++) { 545daa6eda6SGerd Knorr if (0 != ch_read_element_status 546daa6eda6SGerd Knorr (ch, ch->firsts[type]+i,data)) { 547daa6eda6SGerd Knorr retval = -EIO; 548daa6eda6SGerd Knorr break; 549daa6eda6SGerd Knorr } 550daa6eda6SGerd Knorr put_user(data[2], dest+i); 551daa6eda6SGerd Knorr if (data[2] & CESTATUS_EXCEPT) 552daa6eda6SGerd Knorr vprintk("element 0x%x: asc=0x%x, ascq=0x%x\n", 553daa6eda6SGerd Knorr ch->firsts[type]+i, 554daa6eda6SGerd Knorr (int)data[4],(int)data[5]); 555daa6eda6SGerd Knorr retval = ch_read_element_status 556daa6eda6SGerd Knorr (ch, ch->firsts[type]+i,data); 557daa6eda6SGerd Knorr if (0 != retval) 558daa6eda6SGerd Knorr break; 559daa6eda6SGerd Knorr } 5600b950672SArjan van de Ven mutex_unlock(&ch->lock); 561daa6eda6SGerd Knorr return retval; 562daa6eda6SGerd Knorr } 563daa6eda6SGerd Knorr 564daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 565daa6eda6SGerd Knorr 566daa6eda6SGerd Knorr static int 567daa6eda6SGerd Knorr ch_release(struct inode *inode, struct file *file) 568daa6eda6SGerd Knorr { 569daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 570daa6eda6SGerd Knorr 571daa6eda6SGerd Knorr scsi_device_put(ch->device); 572daa6eda6SGerd Knorr file->private_data = NULL; 573daa6eda6SGerd Knorr return 0; 574daa6eda6SGerd Knorr } 575daa6eda6SGerd Knorr 576daa6eda6SGerd Knorr static int 577daa6eda6SGerd Knorr ch_open(struct inode *inode, struct file *file) 578daa6eda6SGerd Knorr { 579da707c54SFUJITA Tomonori scsi_changer *ch; 580daa6eda6SGerd Knorr int minor = iminor(inode); 581daa6eda6SGerd Knorr 582b0061a0eSJonathan Corbet lock_kernel(); 583da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 584da707c54SFUJITA Tomonori ch = idr_find(&ch_index_idr, minor); 585da707c54SFUJITA Tomonori 586daa6eda6SGerd Knorr if (NULL == ch || scsi_device_get(ch->device)) { 587da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 588b0061a0eSJonathan Corbet unlock_kernel(); 589daa6eda6SGerd Knorr return -ENXIO; 590daa6eda6SGerd Knorr } 591da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 592daa6eda6SGerd Knorr 593daa6eda6SGerd Knorr file->private_data = ch; 594b0061a0eSJonathan Corbet unlock_kernel(); 595daa6eda6SGerd Knorr return 0; 596daa6eda6SGerd Knorr } 597daa6eda6SGerd Knorr 598daa6eda6SGerd Knorr static int 599daa6eda6SGerd Knorr ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit) 600daa6eda6SGerd Knorr { 601daa6eda6SGerd Knorr if (type >= CH_TYPES || unit >= ch->counts[type]) 602daa6eda6SGerd Knorr return -1; 603daa6eda6SGerd Knorr return 0; 604daa6eda6SGerd Knorr } 605daa6eda6SGerd Knorr 606f7fea185SMathieu Segaud static long ch_ioctl(struct file *file, 607daa6eda6SGerd Knorr unsigned int cmd, unsigned long arg) 608daa6eda6SGerd Knorr { 609daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 610daa6eda6SGerd Knorr int retval; 611fe08ac31Sviro@ZenIV.linux.org.uk void __user *argp = (void __user *)arg; 612daa6eda6SGerd Knorr 613daa6eda6SGerd Knorr switch (cmd) { 614daa6eda6SGerd Knorr case CHIOGPARAMS: 615daa6eda6SGerd Knorr { 616daa6eda6SGerd Knorr struct changer_params params; 617daa6eda6SGerd Knorr 618daa6eda6SGerd Knorr params.cp_curpicker = 0; 619daa6eda6SGerd Knorr params.cp_npickers = ch->counts[CHET_MT]; 620daa6eda6SGerd Knorr params.cp_nslots = ch->counts[CHET_ST]; 621daa6eda6SGerd Knorr params.cp_nportals = ch->counts[CHET_IE]; 622daa6eda6SGerd Knorr params.cp_ndrives = ch->counts[CHET_DT]; 623daa6eda6SGerd Knorr 624fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, ¶ms, sizeof(params))) 625daa6eda6SGerd Knorr return -EFAULT; 626daa6eda6SGerd Knorr return 0; 627daa6eda6SGerd Knorr } 628daa6eda6SGerd Knorr case CHIOGVPARAMS: 629daa6eda6SGerd Knorr { 630daa6eda6SGerd Knorr struct changer_vendor_params vparams; 631daa6eda6SGerd Knorr 632daa6eda6SGerd Knorr memset(&vparams,0,sizeof(vparams)); 633daa6eda6SGerd Knorr if (ch->counts[CHET_V1]) { 634daa6eda6SGerd Knorr vparams.cvp_n1 = ch->counts[CHET_V1]; 635daa6eda6SGerd Knorr strncpy(vparams.cvp_label1,vendor_labels[0],16); 636daa6eda6SGerd Knorr } 637daa6eda6SGerd Knorr if (ch->counts[CHET_V2]) { 638daa6eda6SGerd Knorr vparams.cvp_n2 = ch->counts[CHET_V2]; 639daa6eda6SGerd Knorr strncpy(vparams.cvp_label2,vendor_labels[1],16); 640daa6eda6SGerd Knorr } 641daa6eda6SGerd Knorr if (ch->counts[CHET_V3]) { 642daa6eda6SGerd Knorr vparams.cvp_n3 = ch->counts[CHET_V3]; 643daa6eda6SGerd Knorr strncpy(vparams.cvp_label3,vendor_labels[2],16); 644daa6eda6SGerd Knorr } 645daa6eda6SGerd Knorr if (ch->counts[CHET_V4]) { 646daa6eda6SGerd Knorr vparams.cvp_n4 = ch->counts[CHET_V4]; 647daa6eda6SGerd Knorr strncpy(vparams.cvp_label4,vendor_labels[3],16); 648daa6eda6SGerd Knorr } 649fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, &vparams, sizeof(vparams))) 650daa6eda6SGerd Knorr return -EFAULT; 651daa6eda6SGerd Knorr return 0; 652daa6eda6SGerd Knorr } 653daa6eda6SGerd Knorr 654daa6eda6SGerd Knorr case CHIOPOSITION: 655daa6eda6SGerd Knorr { 656daa6eda6SGerd Knorr struct changer_position pos; 657daa6eda6SGerd Knorr 658fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&pos, argp, sizeof (pos))) 659daa6eda6SGerd Knorr return -EFAULT; 660daa6eda6SGerd Knorr 661daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) { 662daa6eda6SGerd Knorr dprintk("CHIOPOSITION: invalid parameter\n"); 663daa6eda6SGerd Knorr return -EBADSLT; 664daa6eda6SGerd Knorr } 6650b950672SArjan van de Ven mutex_lock(&ch->lock); 666daa6eda6SGerd Knorr retval = ch_position(ch,0, 667daa6eda6SGerd Knorr ch->firsts[pos.cp_type] + pos.cp_unit, 668daa6eda6SGerd Knorr pos.cp_flags & CP_INVERT); 6690b950672SArjan van de Ven mutex_unlock(&ch->lock); 670daa6eda6SGerd Knorr return retval; 671daa6eda6SGerd Knorr } 672daa6eda6SGerd Knorr 673daa6eda6SGerd Knorr case CHIOMOVE: 674daa6eda6SGerd Knorr { 675daa6eda6SGerd Knorr struct changer_move mv; 676daa6eda6SGerd Knorr 677fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&mv, argp, sizeof (mv))) 678daa6eda6SGerd Knorr return -EFAULT; 679daa6eda6SGerd Knorr 680daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) || 681daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) { 682daa6eda6SGerd Knorr dprintk("CHIOMOVE: invalid parameter\n"); 683daa6eda6SGerd Knorr return -EBADSLT; 684daa6eda6SGerd Knorr } 685daa6eda6SGerd Knorr 6860b950672SArjan van de Ven mutex_lock(&ch->lock); 687daa6eda6SGerd Knorr retval = ch_move(ch,0, 688daa6eda6SGerd Knorr ch->firsts[mv.cm_fromtype] + mv.cm_fromunit, 689daa6eda6SGerd Knorr ch->firsts[mv.cm_totype] + mv.cm_tounit, 690daa6eda6SGerd Knorr mv.cm_flags & CM_INVERT); 6910b950672SArjan van de Ven mutex_unlock(&ch->lock); 692daa6eda6SGerd Knorr return retval; 693daa6eda6SGerd Knorr } 694daa6eda6SGerd Knorr 695daa6eda6SGerd Knorr case CHIOEXCHANGE: 696daa6eda6SGerd Knorr { 697daa6eda6SGerd Knorr struct changer_exchange mv; 698daa6eda6SGerd Knorr 699fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&mv, argp, sizeof (mv))) 700daa6eda6SGerd Knorr return -EFAULT; 701daa6eda6SGerd Knorr 702daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) || 703daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) || 704daa6eda6SGerd Knorr 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) { 705daa6eda6SGerd Knorr dprintk("CHIOEXCHANGE: invalid parameter\n"); 706daa6eda6SGerd Knorr return -EBADSLT; 707daa6eda6SGerd Knorr } 708daa6eda6SGerd Knorr 7090b950672SArjan van de Ven mutex_lock(&ch->lock); 710daa6eda6SGerd Knorr retval = ch_exchange 711daa6eda6SGerd Knorr (ch,0, 712daa6eda6SGerd Knorr ch->firsts[mv.ce_srctype] + mv.ce_srcunit, 713daa6eda6SGerd Knorr ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit, 714daa6eda6SGerd Knorr ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit, 715daa6eda6SGerd Knorr mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2); 7160b950672SArjan van de Ven mutex_unlock(&ch->lock); 717daa6eda6SGerd Knorr return retval; 718daa6eda6SGerd Knorr } 719daa6eda6SGerd Knorr 720daa6eda6SGerd Knorr case CHIOGSTATUS: 721daa6eda6SGerd Knorr { 722daa6eda6SGerd Knorr struct changer_element_status ces; 723daa6eda6SGerd Knorr 724fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&ces, argp, sizeof (ces))) 725daa6eda6SGerd Knorr return -EFAULT; 726daa6eda6SGerd Knorr if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES) 727daa6eda6SGerd Knorr return -EINVAL; 728daa6eda6SGerd Knorr 729daa6eda6SGerd Knorr return ch_gstatus(ch, ces.ces_type, ces.ces_data); 730daa6eda6SGerd Knorr } 731daa6eda6SGerd Knorr 732daa6eda6SGerd Knorr case CHIOGELEM: 733daa6eda6SGerd Knorr { 734daa6eda6SGerd Knorr struct changer_get_element cge; 735d70d4667SHarvey Harrison u_char ch_cmd[12]; 736daa6eda6SGerd Knorr u_char *buffer; 737daa6eda6SGerd Knorr unsigned int elem; 738daa6eda6SGerd Knorr int result,i; 739daa6eda6SGerd Knorr 740fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&cge, argp, sizeof (cge))) 741daa6eda6SGerd Knorr return -EFAULT; 742daa6eda6SGerd Knorr 743daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit)) 744daa6eda6SGerd Knorr return -EINVAL; 745daa6eda6SGerd Knorr elem = ch->firsts[cge.cge_type] + cge.cge_unit; 746daa6eda6SGerd Knorr 747daa6eda6SGerd Knorr buffer = kmalloc(512, GFP_KERNEL | GFP_DMA); 748daa6eda6SGerd Knorr if (!buffer) 749daa6eda6SGerd Knorr return -ENOMEM; 7500b950672SArjan van de Ven mutex_lock(&ch->lock); 751daa6eda6SGerd Knorr 752daa6eda6SGerd Knorr voltag_retry: 753d70d4667SHarvey Harrison memset(ch_cmd, 0, sizeof(ch_cmd)); 754d70d4667SHarvey Harrison ch_cmd[0] = READ_ELEMENT_STATUS; 755d70d4667SHarvey Harrison ch_cmd[1] = (ch->device->lun << 5) | 756daa6eda6SGerd Knorr (ch->voltags ? 0x10 : 0) | 757daa6eda6SGerd Knorr ch_elem_to_typecode(ch,elem); 758d70d4667SHarvey Harrison ch_cmd[2] = (elem >> 8) & 0xff; 759d70d4667SHarvey Harrison ch_cmd[3] = elem & 0xff; 760d70d4667SHarvey Harrison ch_cmd[5] = 1; 761d70d4667SHarvey Harrison ch_cmd[9] = 255; 762daa6eda6SGerd Knorr 763d70d4667SHarvey Harrison result = ch_do_scsi(ch, ch_cmd, buffer, 256, DMA_FROM_DEVICE); 764d70d4667SHarvey Harrison if (!result) { 765daa6eda6SGerd Knorr cge.cge_status = buffer[18]; 766daa6eda6SGerd Knorr cge.cge_flags = 0; 767daa6eda6SGerd Knorr if (buffer[18] & CESTATUS_EXCEPT) { 768daa6eda6SGerd Knorr cge.cge_errno = EIO; 769daa6eda6SGerd Knorr } 770daa6eda6SGerd Knorr if (buffer[25] & 0x80) { 771daa6eda6SGerd Knorr cge.cge_flags |= CGE_SRC; 772daa6eda6SGerd Knorr if (buffer[25] & 0x40) 773daa6eda6SGerd Knorr cge.cge_flags |= CGE_INVERT; 774daa6eda6SGerd Knorr elem = (buffer[26]<<8) | buffer[27]; 775daa6eda6SGerd Knorr for (i = 0; i < 4; i++) { 776daa6eda6SGerd Knorr if (elem >= ch->firsts[i] && 777daa6eda6SGerd Knorr elem < ch->firsts[i] + ch->counts[i]) { 778daa6eda6SGerd Knorr cge.cge_srctype = i; 779daa6eda6SGerd Knorr cge.cge_srcunit = elem-ch->firsts[i]; 780daa6eda6SGerd Knorr } 781daa6eda6SGerd Knorr } 782daa6eda6SGerd Knorr } 783daa6eda6SGerd Knorr if ((buffer[22] & 0x30) == 0x30) { 784daa6eda6SGerd Knorr cge.cge_flags |= CGE_IDLUN; 785daa6eda6SGerd Knorr cge.cge_id = buffer[23]; 786daa6eda6SGerd Knorr cge.cge_lun = buffer[22] & 7; 787daa6eda6SGerd Knorr } 788daa6eda6SGerd Knorr if (buffer[9] & 0x80) { 789daa6eda6SGerd Knorr cge.cge_flags |= CGE_PVOLTAG; 790daa6eda6SGerd Knorr memcpy(cge.cge_pvoltag,buffer+28,36); 791daa6eda6SGerd Knorr } 792daa6eda6SGerd Knorr if (buffer[9] & 0x40) { 793daa6eda6SGerd Knorr cge.cge_flags |= CGE_AVOLTAG; 794daa6eda6SGerd Knorr memcpy(cge.cge_avoltag,buffer+64,36); 795daa6eda6SGerd Knorr } 796daa6eda6SGerd Knorr } else if (ch->voltags) { 797daa6eda6SGerd Knorr ch->voltags = 0; 798daa6eda6SGerd Knorr vprintk("device has no volume tag support\n"); 799daa6eda6SGerd Knorr goto voltag_retry; 800daa6eda6SGerd Knorr } 801daa6eda6SGerd Knorr kfree(buffer); 8020b950672SArjan van de Ven mutex_unlock(&ch->lock); 803daa6eda6SGerd Knorr 804fe08ac31Sviro@ZenIV.linux.org.uk if (copy_to_user(argp, &cge, sizeof (cge))) 805daa6eda6SGerd Knorr return -EFAULT; 806daa6eda6SGerd Knorr return result; 807daa6eda6SGerd Knorr } 808daa6eda6SGerd Knorr 809daa6eda6SGerd Knorr case CHIOINITELEM: 810daa6eda6SGerd Knorr { 8110b950672SArjan van de Ven mutex_lock(&ch->lock); 812daa6eda6SGerd Knorr retval = ch_init_elem(ch); 8130b950672SArjan van de Ven mutex_unlock(&ch->lock); 814daa6eda6SGerd Knorr return retval; 815daa6eda6SGerd Knorr } 816daa6eda6SGerd Knorr 817daa6eda6SGerd Knorr case CHIOSVOLTAG: 818daa6eda6SGerd Knorr { 819daa6eda6SGerd Knorr struct changer_set_voltag csv; 820daa6eda6SGerd Knorr int elem; 821daa6eda6SGerd Knorr 822fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&csv, argp, sizeof(csv))) 823daa6eda6SGerd Knorr return -EFAULT; 824daa6eda6SGerd Knorr 825daa6eda6SGerd Knorr if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) { 826daa6eda6SGerd Knorr dprintk("CHIOSVOLTAG: invalid parameter\n"); 827daa6eda6SGerd Knorr return -EBADSLT; 828daa6eda6SGerd Knorr } 829daa6eda6SGerd Knorr elem = ch->firsts[csv.csv_type] + csv.csv_unit; 8300b950672SArjan van de Ven mutex_lock(&ch->lock); 831daa6eda6SGerd Knorr retval = ch_set_voltag(ch, elem, 832daa6eda6SGerd Knorr csv.csv_flags & CSV_AVOLTAG, 833daa6eda6SGerd Knorr csv.csv_flags & CSV_CLEARTAG, 834daa6eda6SGerd Knorr csv.csv_voltag); 8350b950672SArjan van de Ven mutex_unlock(&ch->lock); 836daa6eda6SGerd Knorr return retval; 837daa6eda6SGerd Knorr } 838daa6eda6SGerd Knorr 839daa6eda6SGerd Knorr default: 840fe08ac31Sviro@ZenIV.linux.org.uk return scsi_ioctl(ch->device, cmd, argp); 841daa6eda6SGerd Knorr 842daa6eda6SGerd Knorr } 843daa6eda6SGerd Knorr } 844daa6eda6SGerd Knorr 845daa6eda6SGerd Knorr #ifdef CONFIG_COMPAT 846daa6eda6SGerd Knorr 847daa6eda6SGerd Knorr struct changer_element_status32 { 848daa6eda6SGerd Knorr int ces_type; 849daa6eda6SGerd Knorr compat_uptr_t ces_data; 850daa6eda6SGerd Knorr }; 851daa6eda6SGerd Knorr #define CHIOGSTATUS32 _IOW('c', 8,struct changer_element_status32) 852daa6eda6SGerd Knorr 853daa6eda6SGerd Knorr static long ch_ioctl_compat(struct file * file, 854daa6eda6SGerd Knorr unsigned int cmd, unsigned long arg) 855daa6eda6SGerd Knorr { 856daa6eda6SGerd Knorr scsi_changer *ch = file->private_data; 857daa6eda6SGerd Knorr 858daa6eda6SGerd Knorr switch (cmd) { 859daa6eda6SGerd Knorr case CHIOGPARAMS: 860daa6eda6SGerd Knorr case CHIOGVPARAMS: 861daa6eda6SGerd Knorr case CHIOPOSITION: 862daa6eda6SGerd Knorr case CHIOMOVE: 863daa6eda6SGerd Knorr case CHIOEXCHANGE: 864daa6eda6SGerd Knorr case CHIOGELEM: 865daa6eda6SGerd Knorr case CHIOINITELEM: 866daa6eda6SGerd Knorr case CHIOSVOLTAG: 867daa6eda6SGerd Knorr /* compatible */ 868f7fea185SMathieu Segaud return ch_ioctl(file, cmd, arg); 869daa6eda6SGerd Knorr case CHIOGSTATUS32: 870daa6eda6SGerd Knorr { 871daa6eda6SGerd Knorr struct changer_element_status32 ces32; 872fe08ac31Sviro@ZenIV.linux.org.uk unsigned char __user *data; 873daa6eda6SGerd Knorr 874fe08ac31Sviro@ZenIV.linux.org.uk if (copy_from_user(&ces32, (void __user *)arg, sizeof (ces32))) 875daa6eda6SGerd Knorr return -EFAULT; 876daa6eda6SGerd Knorr if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES) 877daa6eda6SGerd Knorr return -EINVAL; 878daa6eda6SGerd Knorr 879daa6eda6SGerd Knorr data = compat_ptr(ces32.ces_data); 880daa6eda6SGerd Knorr return ch_gstatus(ch, ces32.ces_type, data); 881daa6eda6SGerd Knorr } 882daa6eda6SGerd Knorr default: 883daa6eda6SGerd Knorr // return scsi_ioctl_compat(ch->device, cmd, (void*)arg); 884daa6eda6SGerd Knorr return -ENOIOCTLCMD; 885daa6eda6SGerd Knorr 886daa6eda6SGerd Knorr } 887daa6eda6SGerd Knorr } 888daa6eda6SGerd Knorr #endif 889daa6eda6SGerd Knorr 890daa6eda6SGerd Knorr /* ------------------------------------------------------------------------ */ 891daa6eda6SGerd Knorr 892daa6eda6SGerd Knorr static int ch_probe(struct device *dev) 893daa6eda6SGerd Knorr { 894daa6eda6SGerd Knorr struct scsi_device *sd = to_scsi_device(dev); 895ee959b00STony Jones struct device *class_dev; 896da707c54SFUJITA Tomonori int minor, ret = -ENOMEM; 897daa6eda6SGerd Knorr scsi_changer *ch; 898daa6eda6SGerd Knorr 899daa6eda6SGerd Knorr if (sd->type != TYPE_MEDIUM_CHANGER) 900daa6eda6SGerd Knorr return -ENODEV; 901daa6eda6SGerd Knorr 9024530a169Svignesh.babu@wipro.com ch = kzalloc(sizeof(*ch), GFP_KERNEL); 903daa6eda6SGerd Knorr if (NULL == ch) 904daa6eda6SGerd Knorr return -ENOMEM; 905daa6eda6SGerd Knorr 906da707c54SFUJITA Tomonori if (!idr_pre_get(&ch_index_idr, GFP_KERNEL)) 907da707c54SFUJITA Tomonori goto free_ch; 908da707c54SFUJITA Tomonori 909da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 910da707c54SFUJITA Tomonori ret = idr_get_new(&ch_index_idr, ch, &minor); 911da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 912da707c54SFUJITA Tomonori 913da707c54SFUJITA Tomonori if (ret) 914da707c54SFUJITA Tomonori goto free_ch; 915da707c54SFUJITA Tomonori 916da707c54SFUJITA Tomonori if (minor > CH_MAX_DEVS) { 917da707c54SFUJITA Tomonori ret = -ENODEV; 918da707c54SFUJITA Tomonori goto remove_idr; 919da707c54SFUJITA Tomonori } 920da707c54SFUJITA Tomonori 921da707c54SFUJITA Tomonori ch->minor = minor; 922daa6eda6SGerd Knorr sprintf(ch->name,"ch%d",ch->minor); 923a3d2c2e8SFUJITA Tomonori 924d73a1a67SGreg Kroah-Hartman class_dev = device_create(ch_sysfs_class, dev, 925d73a1a67SGreg Kroah-Hartman MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch, 926d73a1a67SGreg Kroah-Hartman "s%s", ch->name); 927a3d2c2e8SFUJITA Tomonori if (IS_ERR(class_dev)) { 928ee959b00STony Jones printk(KERN_WARNING "ch%d: device_create failed\n", 929a3d2c2e8SFUJITA Tomonori ch->minor); 930da707c54SFUJITA Tomonori ret = PTR_ERR(class_dev); 931da707c54SFUJITA Tomonori goto remove_idr; 932a3d2c2e8SFUJITA Tomonori } 933a3d2c2e8SFUJITA Tomonori 9340b950672SArjan van de Ven mutex_init(&ch->lock); 935daa6eda6SGerd Knorr ch->device = sd; 936daa6eda6SGerd Knorr ch_readconfig(ch); 937daa6eda6SGerd Knorr if (init) 938daa6eda6SGerd Knorr ch_init_elem(ch); 939daa6eda6SGerd Knorr 9403d164fb0SFUJITA Tomonori dev_set_drvdata(dev, ch); 941017560fcSJeff Garzik sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name); 942daa6eda6SGerd Knorr 943daa6eda6SGerd Knorr return 0; 944da707c54SFUJITA Tomonori remove_idr: 945da707c54SFUJITA Tomonori idr_remove(&ch_index_idr, minor); 946da707c54SFUJITA Tomonori free_ch: 947da707c54SFUJITA Tomonori kfree(ch); 948da707c54SFUJITA Tomonori return ret; 949daa6eda6SGerd Knorr } 950daa6eda6SGerd Knorr 951daa6eda6SGerd Knorr static int ch_remove(struct device *dev) 952daa6eda6SGerd Knorr { 953da707c54SFUJITA Tomonori scsi_changer *ch = dev_get_drvdata(dev); 954daa6eda6SGerd Knorr 955da707c54SFUJITA Tomonori spin_lock(&ch_index_lock); 956da707c54SFUJITA Tomonori idr_remove(&ch_index_idr, ch->minor); 957da707c54SFUJITA Tomonori spin_unlock(&ch_index_lock); 958daa6eda6SGerd Knorr 959ee959b00STony Jones device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor)); 960daa6eda6SGerd Knorr kfree(ch->dt); 961daa6eda6SGerd Knorr kfree(ch); 962daa6eda6SGerd Knorr return 0; 963daa6eda6SGerd Knorr } 964daa6eda6SGerd Knorr 9655aa22af3SFUJITA Tomonori static struct scsi_driver ch_template = { 9665aa22af3SFUJITA Tomonori .owner = THIS_MODULE, 9675aa22af3SFUJITA Tomonori .gendrv = { 9685aa22af3SFUJITA Tomonori .name = "ch", 9695aa22af3SFUJITA Tomonori .probe = ch_probe, 9705aa22af3SFUJITA Tomonori .remove = ch_remove, 9715aa22af3SFUJITA Tomonori }, 9725aa22af3SFUJITA Tomonori }; 9735aa22af3SFUJITA Tomonori 9745aa22af3SFUJITA Tomonori static const struct file_operations changer_fops = { 9755aa22af3SFUJITA Tomonori .owner = THIS_MODULE, 9765aa22af3SFUJITA Tomonori .open = ch_open, 9775aa22af3SFUJITA Tomonori .release = ch_release, 9785aa22af3SFUJITA Tomonori .unlocked_ioctl = ch_ioctl, 9795aa22af3SFUJITA Tomonori #ifdef CONFIG_COMPAT 9805aa22af3SFUJITA Tomonori .compat_ioctl = ch_ioctl_compat, 9815aa22af3SFUJITA Tomonori #endif 9825aa22af3SFUJITA Tomonori }; 9835aa22af3SFUJITA Tomonori 984daa6eda6SGerd Knorr static int __init init_ch_module(void) 985daa6eda6SGerd Knorr { 986daa6eda6SGerd Knorr int rc; 987daa6eda6SGerd Knorr 988daa6eda6SGerd Knorr printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n"); 98921feb5ccSGerd Knorr ch_sysfs_class = class_create(THIS_MODULE, "scsi_changer"); 990daa6eda6SGerd Knorr if (IS_ERR(ch_sysfs_class)) { 991daa6eda6SGerd Knorr rc = PTR_ERR(ch_sysfs_class); 992daa6eda6SGerd Knorr return rc; 993daa6eda6SGerd Knorr } 994daa6eda6SGerd Knorr rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops); 995daa6eda6SGerd Knorr if (rc < 0) { 996daa6eda6SGerd Knorr printk("Unable to get major %d for SCSI-Changer\n", 997daa6eda6SGerd Knorr SCSI_CHANGER_MAJOR); 998daa6eda6SGerd Knorr goto fail1; 999daa6eda6SGerd Knorr } 1000daa6eda6SGerd Knorr rc = scsi_register_driver(&ch_template.gendrv); 1001daa6eda6SGerd Knorr if (rc < 0) 1002daa6eda6SGerd Knorr goto fail2; 1003daa6eda6SGerd Knorr return 0; 1004daa6eda6SGerd Knorr 1005daa6eda6SGerd Knorr fail2: 1006daa6eda6SGerd Knorr unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); 1007daa6eda6SGerd Knorr fail1: 100821feb5ccSGerd Knorr class_destroy(ch_sysfs_class); 1009daa6eda6SGerd Knorr return rc; 1010daa6eda6SGerd Knorr } 1011daa6eda6SGerd Knorr 1012daa6eda6SGerd Knorr static void __exit exit_ch_module(void) 1013daa6eda6SGerd Knorr { 1014daa6eda6SGerd Knorr scsi_unregister_driver(&ch_template.gendrv); 1015daa6eda6SGerd Knorr unregister_chrdev(SCSI_CHANGER_MAJOR, "ch"); 101621feb5ccSGerd Knorr class_destroy(ch_sysfs_class); 1017da707c54SFUJITA Tomonori idr_destroy(&ch_index_idr); 1018daa6eda6SGerd Knorr } 1019daa6eda6SGerd Knorr 1020daa6eda6SGerd Knorr module_init(init_ch_module); 1021daa6eda6SGerd Knorr module_exit(exit_ch_module); 1022daa6eda6SGerd Knorr 1023daa6eda6SGerd Knorr /* 1024daa6eda6SGerd Knorr * Local variables: 1025daa6eda6SGerd Knorr * c-basic-offset: 8 1026daa6eda6SGerd Knorr * End: 1027daa6eda6SGerd Knorr */ 1028