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