This source file includes following definitions.
- sg_ioctl
- sg_open
- sg_close
- sg_malloc
- sg_free
- sg_read
- sg_command_done
- sg_write
- sg_detect
- sg_init
- sg_attach
- sg_detach
- init_module
- cleanup_module
1
2
3
4
5
6
7
8
9 #ifdef MODULE
10 #include <linux/autoconf.h>
11 #include <linux/module.h>
12 #include <linux/version.h>
13 #endif
14
15 #include <linux/fs.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/errno.h>
21 #include <linux/mtio.h>
22 #include <linux/ioctl.h>
23 #include <linux/fcntl.h>
24 #include <asm/io.h>
25 #include <asm/segment.h>
26 #include <asm/system.h>
27
28 #include "../block/blk.h"
29 #include "scsi.h"
30 #include "hosts.h"
31 #include "scsi_ioctl.h"
32 #include "sg.h"
33
34 static void sg_init(void);
35 static int sg_attach(Scsi_Device *);
36 static int sg_detect(Scsi_Device *);
37 static void sg_detach(Scsi_Device *);
38
39
40 struct Scsi_Device_Template sg_template = {NULL, NULL, "sg", NULL, 0xff,
41 SCSI_GENERIC_MAJOR, 0, 0, 0, 0,
42 sg_detect, sg_init,
43 NULL, sg_attach, sg_detach};
44
45 #ifdef SG_BIG_BUFF
46 static char *big_buff = NULL;
47 static struct wait_queue *big_wait;
48 static int big_inuse=0;
49 #endif
50
51 struct scsi_generic
52 {
53 Scsi_Device *device;
54 int users;
55 struct wait_queue *generic_wait;
56 struct wait_queue *read_wait;
57 struct wait_queue *write_wait;
58 int timeout;
59 int buff_len;
60 char *buff;
61 struct sg_header header;
62 char exclude;
63 char pending;
64 char complete;
65 };
66
67 static struct scsi_generic *scsi_generics=NULL;
68 static void sg_free(char *buff,int size);
69
70 static int sg_ioctl(struct inode * inode,struct file * file,
71 unsigned int cmd_in, unsigned long arg)
72 {
73 int result;
74 int dev = MINOR(inode->i_rdev);
75 if ((dev<0) || (dev>=sg_template.dev_max))
76 return -ENXIO;
77 switch(cmd_in)
78 {
79 case SG_SET_TIMEOUT:
80 result = verify_area(VERIFY_READ, arg, sizeof(long));
81 if (result) return result;
82
83 scsi_generics[dev].timeout=get_user((int *) arg);
84 return 0;
85 case SG_GET_TIMEOUT:
86 return scsi_generics[dev].timeout;
87 default:
88 return scsi_ioctl(scsi_generics[dev].device, cmd_in, (void *) arg);
89 }
90 }
91
92 static int sg_open(struct inode * inode, struct file * filp)
93 {
94 int dev=MINOR(inode->i_rdev);
95 int flags=filp->f_flags;
96 if (dev>=sg_template.dev_max || !scsi_generics[dev].device)
97 return -ENXIO;
98 if (O_RDWR!=(flags & O_ACCMODE))
99 return -EACCES;
100
101
102
103
104
105 if (flags & O_EXCL)
106 {
107 while(scsi_generics[dev].users)
108 {
109 if (flags & O_NONBLOCK)
110 return -EBUSY;
111 interruptible_sleep_on(&scsi_generics[dev].generic_wait);
112 if (current->signal & ~current->blocked)
113 return -ERESTARTSYS;
114 }
115 scsi_generics[dev].exclude=1;
116 }
117 else
118
119
120
121
122 while(scsi_generics[dev].exclude)
123 {
124 if (flags & O_NONBLOCK)
125 return -EBUSY;
126 interruptible_sleep_on(&scsi_generics[dev].generic_wait);
127 if (current->signal & ~current->blocked)
128 return -ERESTARTSYS;
129 }
130
131
132
133
134
135
136 if (!scsi_generics[dev].users
137 && scsi_generics[dev].pending
138 && scsi_generics[dev].complete)
139 {
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;
144 }
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++;
151 return 0;
152 }
153
154 static void sg_close(struct inode * inode, struct file * filp)
155 {
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);
163 }
164
165 static char *sg_malloc(int size)
166 {
167 if (size<=4096)
168 return (char *) scsi_malloc(size);
169 #ifdef SG_BIG_BUFF
170 if (size<SG_BIG_BUFF)
171 {
172 while(big_inuse)
173 {
174 interruptible_sleep_on(&big_wait);
175 if (current->signal & ~current->blocked)
176 return NULL;
177 }
178 big_inuse=1;
179 return big_buff;
180 }
181 #endif
182 return NULL;
183 }
184
185 static void sg_free(char *buff,int size)
186 {
187 #ifdef SG_BIG_BUFF
188 if (buff==big_buff)
189 {
190 big_inuse=0;
191 wake_up(&big_wait);
192 return;
193 }
194 #endif
195 scsi_free(buff,size);
196 }
197
198
199
200
201
202
203 static int sg_read(struct inode *inode,struct file *filp,char *buf,int count)
204 {
205 int dev=MINOR(inode->i_rdev);
206 int i;
207 struct scsi_generic *device=&scsi_generics[dev];
208 if ((i=verify_area(VERIFY_WRITE,buf,count)))
209 return i;
210
211
212
213
214 while(!device->pending || !device->complete)
215 {
216 if (filp->f_flags & O_NONBLOCK)
217 return -EWOULDBLOCK;
218 interruptible_sleep_on(&device->read_wait);
219 if (current->signal & ~current->blocked)
220 return -ERESTARTSYS;
221 }
222
223
224
225
226 device->header.pack_len=device->header.reply_len;
227 device->header.result=0;
228 if (count>=sizeof(struct sg_header))
229 {
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));
236 }
237 }
238 else
239 count=0;
240
241
242
243
244
245 sg_free(device->buff,device->buff_len);
246 device->buff = NULL;
247 device->pending=0;
248 wake_up(&device->write_wait);
249 return count;
250 }
251
252
253
254
255
256
257 static void sg_command_done(Scsi_Cmnd * SCpnt)
258 {
259 int dev=SCpnt->request.dev;
260 struct scsi_generic *device=&scsi_generics[dev];
261 if (!device->pending)
262 {
263 printk("unexpected done for sg %d\n",dev);
264 SCpnt->request.dev=-1;
265 return;
266 }
267
268
269
270
271
272 memcpy(device->header.sense_buffer, SCpnt->sense_buffer, sizeof(SCpnt->sense_buffer));
273 if (SCpnt->sense_buffer[0])
274 {
275 device->header.result=EIO;
276 }
277 else
278 device->header.result=SCpnt->result;
279
280
281
282
283
284 device->complete=1;
285 SCpnt->request.dev=-1;
286 wake_up(&scsi_generics[dev].read_wait);
287 }
288
289 #define SG_SEND 0
290 #define SG_REC 1
291
292 static int sg_write(struct inode *inode,struct file *filp,char *buf,int count)
293 {
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];
298 int direction;
299 unsigned char opcode;
300 Scsi_Cmnd * SCpnt;
301 int sgcnt;
302
303 if ((i=verify_area(VERIFY_READ,buf,count)))
304 return i;
305
306
307
308
309 if (count<(sizeof(struct sg_header) + 6))
310 return -EIO;
311
312
313
314
315
316
317 while(device->pending)
318 {
319 if (filp->f_flags & O_NONBLOCK)
320 return -EWOULDBLOCK;
321 #ifdef DEBUG
322 printk("sg_write: sleeping on pending request\n");
323 #endif
324 interruptible_sleep_on(&device->write_wait);
325 if (current->signal & ~current->blocked)
326 return -ERESTARTSYS;
327 }
328
329
330
331
332 device->pending=1;
333 device->complete=0;
334 memcpy_fromfs(&device->header,buf,sizeof(struct sg_header));
335
336
337
338
339 device->header.pack_len=count;
340 buf+=sizeof(struct sg_header);
341 if( device->header.pack_len > device->header.reply_len )
342 {
343 bsize = device->header.pack_len;
344 direction = SG_SEND;
345 } else {
346 bsize = device->header.reply_len;
347 direction = SG_REC;
348 }
349
350
351
352
353 bsize-=sizeof(struct sg_header);
354
355
356
357
358
359
360 amt=bsize;
361 if (!bsize)
362 bsize++;
363 bsize=(bsize+511) & ~511;
364
365
366
367
368 if ((bsize<0) || !(device->buff=sg_malloc(device->buff_len=bsize)))
369 {
370 device->pending=0;
371 wake_up(&device->write_wait);
372 return -ENOMEM;
373 }
374
375 #ifdef DEBUG
376 printk("allocating device\n");
377 #endif
378
379
380
381
382
383 if (!(SCpnt=allocate_device(NULL,device->device, !(filp->f_flags & O_NONBLOCK))))
384 {
385 device->pending=0;
386 wake_up(&device->write_wait);
387 sg_free(device->buff,device->buff_len);
388 device->buff = NULL;
389 return -EWOULDBLOCK;
390 }
391 #ifdef DEBUG
392 printk("device allocated\n");
393 #endif
394
395
396
397
398 SCpnt->request.dev=dev;
399 SCpnt->sense_buffer[0]=0;
400 opcode = get_user(buf);
401 size=COMMAND_SIZE(opcode);
402 if (opcode >= 0xc0 && device->header.twelve_byte) size = 12;
403 SCpnt->cmd_len = size;
404
405
406
407
408
409
410 if( direction == SG_SEND )
411 amt -= size;
412
413
414
415
416 if( count < (sizeof(struct sg_header) + size) )
417 {
418 device->pending=0;
419 wake_up( &device->write_wait );
420 sg_free( device->buff, device->buff_len );
421 device->buff = NULL;
422 return -EIO;
423 }
424
425
426
427
428 memcpy_fromfs(cmnd,buf,size);
429 buf+=size;
430
431
432
433
434
435
436 if( direction == SG_SEND ) memcpy_fromfs(device->buff,buf, amt);
437
438
439
440
441 cmnd[1]= (cmnd[1] & 0x1f) | (device->device->lun<<5);
442
443 #ifdef DEBUG
444 printk("do cmd\n");
445 #endif
446
447
448
449
450
451
452 scsi_do_cmd (SCpnt,(void *) cmnd,
453 (void *) device->buff,amt,
454 sg_command_done,device->timeout,SG_DEFAULT_RETRIES);
455
456 #ifdef DEBUG
457 printk("done cmd\n");
458 #endif
459
460 return count;
461 }
462
463 static struct file_operations sg_fops = {
464 NULL,
465 sg_read,
466 sg_write,
467 NULL,
468 NULL,
469 sg_ioctl,
470 NULL,
471 sg_open,
472 sg_close,
473 NULL
474 };
475
476
477 static int sg_detect(Scsi_Device * SDp){
478 ++sg_template.dev_noticed;
479 return 1;
480 }
481
482
483 static void sg_init()
484 {
485 static int sg_registered = 0;
486
487 if (sg_template.dev_noticed == 0) return;
488
489 if(!sg_registered) {
490 if (register_chrdev(SCSI_GENERIC_MAJOR,"sg",&sg_fops))
491 {
492 printk("Unable to get major %d for generic SCSI device\n",
493 SCSI_GENERIC_MAJOR);
494 return;
495 }
496 sg_registered++;
497 }
498
499
500 if(scsi_generics) return;
501
502 #ifdef DEBUG
503 printk("sg: Init generic device.\n");
504 #endif
505
506 #ifdef SG_BIG_BUFF
507 big_buff= (char *) scsi_init_malloc(SG_BIG_BUFF, GFP_ATOMIC | GFP_DMA);
508 #endif
509
510 scsi_generics = (struct scsi_generic *)
511 scsi_init_malloc((sg_template.dev_noticed + SG_EXTRA_DEVS)
512 * sizeof(struct scsi_generic), GFP_ATOMIC);
513 memset(scsi_generics, 0, (sg_template.dev_noticed + SG_EXTRA_DEVS)
514 * sizeof(struct scsi_generic));
515
516 sg_template.dev_max = sg_template.dev_noticed + SG_EXTRA_DEVS;
517 }
518
519 static int sg_attach(Scsi_Device * SDp)
520 {
521 struct scsi_generic * gpnt;
522 int i;
523
524 if(sg_template.nr_dev >= sg_template.dev_max)
525 {
526 SDp->attached--;
527 return 1;
528 }
529
530 for(gpnt = scsi_generics, i=0; i<sg_template.dev_max; i++, gpnt++)
531 if(!gpnt->device) break;
532
533 if(i >= sg_template.dev_max) panic ("scsi_devices corrupt (sg)");
534
535 scsi_generics[i].device=SDp;
536 scsi_generics[i].users=0;
537 scsi_generics[i].generic_wait=NULL;
538 scsi_generics[i].read_wait=NULL;
539 scsi_generics[i].write_wait=NULL;
540 scsi_generics[i].buff=NULL;
541 scsi_generics[i].exclude=0;
542 scsi_generics[i].pending=0;
543 scsi_generics[i].timeout=SG_DEFAULT_TIMEOUT;
544 sg_template.nr_dev++;
545 return 0;
546 };
547
548
549
550 static void sg_detach(Scsi_Device * SDp)
551 {
552 struct scsi_generic * gpnt;
553 int i;
554
555 for(gpnt = scsi_generics, i=0; i<sg_template.dev_max; i++, gpnt++)
556 if(gpnt->device == SDp) {
557 gpnt->device = NULL;
558 SDp->attached--;
559 sg_template.nr_dev--;
560 return;
561 }
562 return;
563 }
564
565 #ifdef MODULE
566 char kernel_version[] = UTS_RELEASE;
567
568 int init_module(void) {
569 sg_template.usage_count = &mod_use_count_;
570 return scsi_register_module(MODULE_SCSI_DEV, &sg_template);
571 }
572
573 void cleanup_module( void)
574 {
575 if (MOD_IN_USE) {
576 printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
577 return;
578 }
579 scsi_unregister_module(MODULE_SCSI_DEV, &sg_template);
580 unregister_chrdev(SCSI_GENERIC_MAJOR, "sg");
581
582 if(scsi_generics != NULL) {
583 scsi_init_free((char *) scsi_generics,
584 (sg_template.dev_noticed + SG_EXTRA_DEVS)
585 * sizeof(struct scsi_generic));
586 }
587 sg_template.dev_max = 0;
588 #ifdef SG_BIG_BUFF
589 if(big_buff != NULL)
590 scsi_init_free(big_buff, SG_BIG_BUFF);
591 #endif
592 }
593 #endif
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612