3 * Started: Aug 9 by Lawrence Foard (entropy@world.std.com), 4 * to allow user process control of SCSI devices. 5 * Development Sponsored by Killy Corp. NY NY 7 * Borrows code from st driver. 10 #include <linux/autoconf.h> 11 #include <linux/module.h> 12 #include <linux/version.h> 16 #include <linux/kernel.h> 17 #include <linux/sched.h> 18 #include <linux/string.h> 20 #include <linux/errno.h> 21 #include <linux/mtio.h> 22 #include <linux/ioctl.h> 23 #include <linux/fcntl.h> 25 #include <asm/segment.h> 26 #include <asm/system.h> 28 #include"../block/blk.h" 31 #include"scsi_ioctl.h" 34 static voidsg_init(void); 35 static intsg_attach(Scsi_Device
*); 36 static intsg_detect(Scsi_Device
*); 37 static voidsg_detach(Scsi_Device
*); 40 struct Scsi_Device_Template sg_template
= {NULL
, NULL
,"sg", NULL
,0xff, 41 SCSI_GENERIC_MAJOR
,0,0,0,0, 43 NULL
, sg_attach
, sg_detach
}; 46 static char*big_buff
= NULL
; 47 static struct wait_queue
*big_wait
;/* wait for buffer available */ 48 static int big_inuse
=0; 54 int users
;/* how many people have it open? */ 55 struct wait_queue
*generic_wait
;/* wait for device to be available */ 56 struct wait_queue
*read_wait
;/* wait for response */ 57 struct wait_queue
*write_wait
;/* wait for free buffer */ 58 int timeout
;/* current default value for device */ 59 int buff_len
;/* length of current buffer */ 60 char*buff
;/* the buffer */ 61 struct sg_header header
;/* header of pending command */ 62 char exclude
;/* opened for exclusive access */ 63 char pending
;/* don't accept writes now */ 64 char complete
;/* command complete allow a read */ 67 static struct scsi_generic
*scsi_generics
=NULL
; 68 static voidsg_free(char*buff
,int size
); 70 static intsg_ioctl(struct inode
* inode
,struct file
* file
, 71 unsigned int cmd_in
,unsigned long arg
) 74 int dev
=MINOR(inode
->i_rdev
); 75 if((dev
<0) || (dev
>=sg_template
.dev_max
)) 80 result
=verify_area(VERIFY_READ
, (const void*)arg
,sizeof(long)); 81 if(result
)return result
; 83 scsi_generics
[dev
].timeout
=get_user((int*) arg
); 86 return scsi_generics
[dev
].timeout
; 88 returnscsi_ioctl(scsi_generics
[dev
].device
, cmd_in
, (void*) arg
); 92 static intsg_open(struct inode
* inode
,struct file
* filp
) 94 int dev
=MINOR(inode
->i_rdev
); 95 int flags
=filp
->f_flags
; 96 if(dev
>=sg_template
.dev_max
|| !scsi_generics
[dev
].device
) 98 if(O_RDWR
!=(flags
& O_ACCMODE
)) 102 * If we want exclusive access, then wait until the device is not 103 * busy, and then set the flag to prevent anyone else from using it. 107 while(scsi_generics
[dev
].users
) 109 if(flags
& O_NONBLOCK
) 111 interruptible_sleep_on(&scsi_generics
[dev
].generic_wait
); 112 if(current
->signal
& ~current
->blocked
) 115 scsi_generics
[dev
].exclude
=1; 119 * Wait until nobody has an exclusive open on 122 while(scsi_generics
[dev
].exclude
) 124 if(flags
& O_NONBLOCK
) 126 interruptible_sleep_on(&scsi_generics
[dev
].generic_wait
); 127 if(current
->signal
& ~current
->blocked
) 132 * OK, we should have grabbed the device. Mark the thing so 133 * that other processes know that we have it, and initialize the 134 * state variables to known values. 136 if(!scsi_generics
[dev
].users
137 && scsi_generics
[dev
].pending
138 && scsi_generics
[dev
].complete
) 140 if(scsi_generics
[dev
].buff
!= NULL
) 141 sg_free(scsi_generics
[dev
].buff
,scsi_generics
[dev
].buff_len
); 142 scsi_generics
[dev
].buff
=NULL
; 143 scsi_generics
[dev
].pending
=0; 145 if(!scsi_generics
[dev
].users
) 146 scsi_generics
[dev
].timeout
=SG_DEFAULT_TIMEOUT
; 147 if(scsi_generics
[dev
].device
->host
->hostt
->usage_count
) 148 (*scsi_generics
[dev
].device
->host
->hostt
->usage_count
)++; 149 if(sg_template
.usage_count
) (*sg_template
.usage_count
)++; 150 scsi_generics
[dev
].users
++; 154 static voidsg_close(struct inode
* inode
,struct file
* filp
) 156 int dev
=MINOR(inode
->i_rdev
); 157 scsi_generics
[dev
].users
--; 158 if(scsi_generics
[dev
].device
->host
->hostt
->usage_count
) 159 (*scsi_generics
[dev
].device
->host
->hostt
->usage_count
)--; 160 if(sg_template
.usage_count
) (*sg_template
.usage_count
)--; 161 scsi_generics
[dev
].exclude
=0; 162 wake_up(&scsi_generics
[dev
].generic_wait
); 165 static char*sg_malloc(int size
) 168 return(char*)scsi_malloc(size
); 170 if(size
<=SG_BIG_BUFF
) 174 interruptible_sleep_on(&big_wait
); 175 if(current
->signal
& ~current
->blocked
) 185 static voidsg_free(char*buff
,int size
) 195 scsi_free(buff
,size
); 199 * Read back the results of a previous command. We use the pending and 200 * complete semaphores to tell us whether the buffer is available for us 201 * and whether the command is actually done. 203 static intsg_read(struct inode
*inode
,struct file
*filp
,char*buf
,int count
) 205 int dev
=MINOR(inode
->i_rdev
); 207 struct scsi_generic
*device
=&scsi_generics
[dev
]; 208 if((i
=verify_area(VERIFY_WRITE
,buf
,count
))) 212 * Wait until the command is actually done. 214 while(!device
->pending
|| !device
->complete
) 216 if(filp
->f_flags
& O_NONBLOCK
) 218 interruptible_sleep_on(&device
->read_wait
); 219 if(current
->signal
& ~current
->blocked
) 224 * Now copy the result back to the user buffer. 226 device
->header
.pack_len
=device
->header
.reply_len
; 227 device
->header
.result
=0; 228 if(count
>=sizeof(struct sg_header
)) 230 memcpy_tofs(buf
,&device
->header
,sizeof(struct sg_header
)); 231 buf
+=sizeof(struct sg_header
); 232 if(count
>device
->header
.pack_len
) 233 count
=device
->header
.pack_len
; 234 if(count
>sizeof(struct sg_header
)) { 235 memcpy_tofs(buf
,device
->buff
,count
-sizeof(struct sg_header
)); 242 * Clean up, and release the device so that we can send another 245 sg_free(device
->buff
,device
->buff_len
); 248 wake_up(&device
->write_wait
); 253 * This function is called by the interrupt handler when we 254 * actually have a command that is complete. Change the 255 * flags to indicate that we have a result. 257 static voidsg_command_done(Scsi_Cmnd
* SCpnt
) 259 int dev
=SCpnt
->request
.dev
; 260 struct scsi_generic
*device
=&scsi_generics
[dev
]; 263 printk("unexpected done for sg %d\n",dev
); 264 SCpnt
->request
.dev
=-1; 269 * See if the command completed normally, or whether something went 272 memcpy(device
->header
.sense_buffer
, SCpnt
->sense_buffer
,sizeof(SCpnt
->sense_buffer
)); 273 if(SCpnt
->sense_buffer
[0]) 275 device
->header
.result
=EIO
; 278 device
->header
.result
=SCpnt
->result
; 281 * Now wake up the process that is waiting for the 285 SCpnt
->request
.dev
=-1; 286 wake_up(&scsi_generics
[dev
].read_wait
); 292 static intsg_write(struct inode
*inode
,struct file
*filp
,const char*buf
,int count
) 294 int bsize
,size
,amt
,i
; 295 unsigned char cmnd
[MAX_COMMAND_SIZE
]; 296 int dev
=MINOR(inode
->i_rdev
); 297 struct scsi_generic
* device
=&scsi_generics
[dev
]; 299 unsigned char opcode
; 302 if((i
=verify_area(VERIFY_READ
,buf
,count
))) 305 * The minimum scsi command length is 6 bytes. If we get anything 306 * less than this, it is clearly bogus. 308 if(count
<(sizeof(struct sg_header
) +6)) 312 * If we still have a result pending from a previous command, 313 * wait until the result has been read by the user before sending 316 while(device
->pending
) 318 if(filp
->f_flags
& O_NONBLOCK
) 321 printk("sg_write: sleeping on pending request\n"); 323 interruptible_sleep_on(&device
->write_wait
); 324 if(current
->signal
& ~current
->blocked
) 329 * Mark the device flags for the new state. 333 memcpy_fromfs(&device
->header
,buf
,sizeof(struct sg_header
)); 336 * fix input size, and see if we are sending data. 338 device
->header
.pack_len
=count
; 339 buf
+=sizeof(struct sg_header
); 340 if( device
->header
.pack_len
> device
->header
.reply_len
) 342 bsize
= device
->header
.pack_len
; 345 bsize
= device
->header
.reply_len
; 350 * Don't include the command header itself in the size. 352 bsize
-=sizeof(struct sg_header
); 355 * Allocate a buffer that is large enough to hold the data 356 * that has been requested. Round up to an even number of sectors, 357 * since scsi_malloc allocates in chunks of 512 bytes. 362 bsize
=(bsize
+511) & ~511; 365 * If we cannot allocate the buffer, report an error. 367 if((bsize
<0) || !(device
->buff
=sg_malloc(device
->buff_len
=bsize
))) 370 wake_up(&device
->write_wait
); 375 printk("allocating device\n"); 379 * Grab a device pointer for the device we want to talk to. If we 380 * don't want to block, just return with the appropriate message. 382 if(!(SCpnt
=allocate_device(NULL
,device
->device
, !(filp
->f_flags
& O_NONBLOCK
)))) 385 wake_up(&device
->write_wait
); 386 sg_free(device
->buff
,device
->buff_len
); 391 printk("device allocated\n"); 395 * Now we need to grab the command itself from the user's buffer. 397 SCpnt
->request
.dev
=dev
; 398 SCpnt
->sense_buffer
[0]=0; 399 opcode
=get_user(buf
); 400 size
=COMMAND_SIZE(opcode
); 401 if(opcode
>=0xc0&& device
->header
.twelve_byte
) size
=12; 402 SCpnt
->cmd_len
= size
; 405 * If we are writing data, subtract off the size 406 * of the command itself, to get the amount of actual data 407 * that we need to send to the device. 409 if( direction
== SG_SEND
) 413 * Verify that the user has actually passed enough bytes for this command. 415 if( count
< (sizeof(struct sg_header
) + size
) ) 418 wake_up( &device
->write_wait
); 419 sg_free( device
->buff
, device
->buff_len
); 425 * Now copy the SCSI command from the user's address space. 427 memcpy_fromfs(cmnd
,buf
,size
); 431 * If we are writing data, copy the data we are writing. The pack_len 432 * field also includes the length of the header and the command, 433 * so we need to subtract these off. 435 if( direction
== SG_SEND
)memcpy_fromfs(device
->buff
,buf
, amt
); 438 * Set the LUN field in the command structure. 440 cmnd
[1]= (cmnd
[1] &0x1f) | (device
->device
->lun
<<5); 447 * Now pass the actual command down to the low-level driver. We 448 * do not do any more here - when the interrupt arrives, we will 449 * then do the post-processing. 451 scsi_do_cmd(SCpnt
,(void*) cmnd
, 452 (void*) device
->buff
,amt
, 453 sg_command_done
,device
->timeout
,SG_DEFAULT_RETRIES
); 456 printk("done cmd\n"); 462 static struct file_operations sg_fops
= { 471 sg_close
,/* release */ 476 static intsg_detect(Scsi_Device
* SDp
){ 477 ++sg_template
.dev_noticed
; 481 /* Driver initialization */ 484 static int sg_registered
=0; 486 if(sg_template
.dev_noticed
==0)return; 489 if(register_chrdev(SCSI_GENERIC_MAJOR
,"sg",&sg_fops
)) 491 printk("Unable to get major %d for generic SCSI device\n", 498 /* If we have already been through here, return */ 499 if(scsi_generics
)return; 502 printk("sg: Init generic device.\n"); 506 big_buff
= (char*)scsi_init_malloc(SG_BIG_BUFF
, GFP_ATOMIC
| GFP_DMA
); 509 scsi_generics
= (struct scsi_generic
*) 510 scsi_init_malloc((sg_template
.dev_noticed
+ SG_EXTRA_DEVS
) 511 *sizeof(struct scsi_generic
), GFP_ATOMIC
); 512 memset(scsi_generics
,0, (sg_template
.dev_noticed
+ SG_EXTRA_DEVS
) 513 *sizeof(struct scsi_generic
)); 515 sg_template
.dev_max
= sg_template
.dev_noticed
+ SG_EXTRA_DEVS
; 518 static intsg_attach(Scsi_Device
* SDp
) 520 struct scsi_generic
* gpnt
; 523 if(sg_template
.nr_dev
>= sg_template
.dev_max
) 529 for(gpnt
= scsi_generics
, i
=0; i
<sg_template
.dev_max
; i
++, gpnt
++) 530 if(!gpnt
->device
)break; 532 if(i
>= sg_template
.dev_max
)panic("scsi_devices corrupt (sg)"); 534 scsi_generics
[i
].device
=SDp
; 535 scsi_generics
[i
].users
=0; 536 scsi_generics
[i
].generic_wait
=NULL
; 537 scsi_generics
[i
].read_wait
=NULL
; 538 scsi_generics
[i
].write_wait
=NULL
; 539 scsi_generics
[i
].buff
=NULL
; 540 scsi_generics
[i
].exclude
=0; 541 scsi_generics
[i
].pending
=0; 542 scsi_generics
[i
].timeout
=SG_DEFAULT_TIMEOUT
; 543 sg_template
.nr_dev
++; 549 static voidsg_detach(Scsi_Device
* SDp
) 551 struct scsi_generic
* gpnt
; 554 for(gpnt
= scsi_generics
, i
=0; i
<sg_template
.dev_max
; i
++, gpnt
++) 555 if(gpnt
->device
== SDp
) { 558 sg_template
.nr_dev
--; 565 char kernel_version
[] = UTS_RELEASE
; 567 intinit_module(void) { 568 sg_template
.usage_count
= &mod_use_count_
; 569 returnscsi_register_module(MODULE_SCSI_DEV
, &sg_template
); 572 voidcleanup_module(void) 575 printk(KERN_INFO __FILE__
": module is in use, remove rejected\n"); 578 scsi_unregister_module(MODULE_SCSI_DEV
, &sg_template
); 579 unregister_chrdev(SCSI_GENERIC_MAJOR
,"sg"); 581 if(scsi_generics
!= NULL
) { 582 scsi_init_free((char*) scsi_generics
, 583 (sg_template
.dev_noticed
+ SG_EXTRA_DEVS
) 584 *sizeof(struct scsi_generic
)); 586 sg_template
.dev_max
=0; 589 scsi_init_free(big_buff
, SG_BIG_BUFF
); 595 * Overrides for Emacs so that we almost follow Linus's tabbing style. 596 * Emacs will notice this stuff at the end of the file and automatically 597 * adjust the settings for this buffer only. This must remain at the end 599 * --------------------------------------------------------------------------- 602 * c-brace-imaginary-offset: 0 604 * c-argdecl-indent: 4 606 * c-continued-statement-offset: 4 607 * c-continued-brace-offset: 0 608 * indent-tabs-mode: nil