Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / i2o / i2o_config.c
blob9a9cfec6e14273185a5307b8574841d0cb6f9254
1 /*
2 * I2O Configuration Interface Driver
4 * (C) Copyright 1999 Red Hat Software
5 *
6 * Written by Alan Cox, Building Number Three Ltd
8 * Modified 04/20/1999 by Deepak Saxena
9 * - Added basic ioctl() support
10 * Modified 06/07/1999 by Deepak Saxena
11 * - Added software download ioctl (still testing)
12 * Modified 09/10/1999 by Auvo Häkkinen
13 * - Changes to i2o_cfg_reply(), ioctl_parms()
14 * - Added ioct_validate()
15 * Modified 09/30/1999 by Taneli Vähäkangas
16 * - Fixed ioctl_swdl()
17 * Modified 10/04/1999 by Taneli Vähäkangas
18 * - Changed ioctl_swdl(), implemented ioctl_swul() and ioctl_swdel()
19 * Modified 11/18/199 by Deepak Saxena
20 * - Added event managmenet support
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version
25 * 2 of the License, or (at your option) any later version.
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/pci.h>
31 #include <linux/i2o.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/malloc.h>
35 #include <linux/miscdevice.h>
36 #include <linux/mm.h>
37 #include <linux/spinlock.h>
38 #include <linux/smp_lock.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
43 static int i2o_cfg_context = -1;
44 static void *page_buf;
45 static void *i2o_buffer;
46 static spinlock_t i2o_config_lock = SPIN_LOCK_UNLOCKED;
47 struct wait_queue *i2o_wait_queue;
49 #define MODINC(x,y) (x = x++ % y)
51 struct i2o_cfg_info
53 struct file* fp;
54 struct fasync_struct *fasync;
55 struct i2o_evt_info event_q[I2O_EVT_Q_LEN];
56 u16 q_in; // Queue head index
57 u16 q_out; // Queue tail index
58 u16 q_len; // Queue length
59 u16 q_lost; // Number of lost events
60 u32 q_id; // Event queue ID...used as tx_context
61 struct i2o_cfg_info *next;
63 static struct i2o_cfg_info *open_files = NULL;
64 static int i2o_cfg_info_id = 0;
66 static int ioctl_getiops(unsigned long);
67 static int ioctl_gethrt(unsigned long);
68 static int ioctl_getlct(unsigned long);
69 static int ioctl_parms(unsigned long, unsigned int);
70 static int ioctl_html(unsigned long);
71 static int ioctl_swdl(unsigned long);
72 static int ioctl_swul(unsigned long);
73 static int ioctl_swdel(unsigned long);
74 static int ioctl_validate(unsigned long);
75 static int ioctl_evt_reg(unsigned long, struct file *);
76 static int ioctl_evt_get(unsigned long, struct file *);
77 static int cfg_fasync(int, struct file*, int);
80 * This is the callback for any message we have posted. The message itself
81 * will be returned to the message pool when we return from the IRQ
83 * This runs in irq context so be short and sweet.
85 static void i2o_cfg_reply(struct i2o_handler *h, struct i2o_controller *c, struct i2o_message *m)
87 u32 *msg = (u32 *)m;
89 if (msg[0] & MSG_FAIL) {
90 u32 *preserved_msg = (u32*)(c->mem_offset + msg[7]);
92 printk(KERN_ERR "i2o_config: IOP failed to process the msg.\n");
94 /* Release the preserved msg frame by resubmitting it as a NOP */
96 preserved_msg[0] = THREE_WORD_MSG_SIZE | SGL_OFFSET_0;
97 preserved_msg[1] = I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0;
98 preserved_msg[2] = 0;
99 i2o_post_message(c, msg[7]);
102 if (msg[4] >> 24) // ReqStatus != SUCCESS
103 i2o_report_status(KERN_INFO,"i2o_config", msg);
105 if(m->function == I2O_CMD_UTIL_EVT_REGISTER)
107 struct i2o_cfg_info *inf;
109 for(inf = open_files; inf; inf = inf->next)
110 if(inf->q_id == msg[3])
111 break;
114 // If this is the case, it means that we're getting
115 // events for a file descriptor that's been close()'d
116 // w/o the user unregistering for events first.
117 // The code currently assumes that the user will
118 // take care of unregistering for events before closing
119 // a file.
121 // TODO:
122 // Should we track event registartion and deregister
123 // for events when a file is close()'d so this doesn't
124 // happen? That would get rid of the search through
125 // the linked list since file->private_data could point
126 // directly to the i2o_config_info data structure...but
127 // it would mean having all sorts of tables to track
128 // what each file is registered for...I think the
129 // current method is simpler. - DS
131 if(!inf)
132 return;
134 inf->event_q[inf->q_in].id.iop = c->unit;
135 inf->event_q[inf->q_in].id.tid = m->target_tid;
136 inf->event_q[inf->q_in].id.evt_mask = msg[4];
139 // Data size = msg size - reply header
141 inf->event_q[inf->q_in].data_size = (m->size - 5) * 4;
142 if(inf->event_q[inf->q_in].data_size)
143 memcpy(inf->event_q[inf->q_in].evt_data,
144 (unsigned char *)(msg + 5),
145 inf->event_q[inf->q_in].data_size);
147 spin_lock(&i2o_config_lock);
148 MODINC(inf->q_in, I2O_EVT_Q_LEN);
149 if(inf->q_len == I2O_EVT_Q_LEN)
151 MODINC(inf->q_out, I2O_EVT_Q_LEN);
152 inf->q_lost++;
154 else
156 // Keep I2OEVTGET on another CPU from touching this
157 inf->q_len++;
159 spin_unlock(&i2o_config_lock);
162 // printk(KERN_INFO "File %p w/id %d has %d events\n",
163 // inf->fp, inf->q_id, inf->q_len);
165 kill_fasync(&inf->fasync, SIGIO, POLL_IN);
168 return;
172 * Each of these describes an i2o message handler. They are
173 * multiplexed by the i2o_core code
176 struct i2o_handler cfg_handler=
178 i2o_cfg_reply,
179 NULL,
180 NULL,
181 NULL,
182 "Configuration",
184 0xffffffff // All classes
187 static long long cfg_llseek(struct file *file, long long offset, int origin)
189 return -ESPIPE;
193 static ssize_t cfg_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
195 printk(KERN_INFO "i2o_config write not yet supported\n");
197 return 0;
201 static ssize_t cfg_read(struct file *file, char *buf, size_t count, loff_t *ptr)
203 return 0;
207 * IOCTL Handler
209 static int cfg_ioctl(struct inode *inode, struct file *fp, unsigned int cmd,
210 unsigned long arg)
212 int ret;
214 switch(cmd)
216 case I2OGETIOPS:
217 ret = ioctl_getiops(arg);
218 break;
220 case I2OHRTGET:
221 ret = ioctl_gethrt(arg);
222 break;
224 case I2OLCTGET:
225 ret = ioctl_getlct(arg);
226 break;
228 case I2OPARMSET:
229 ret = ioctl_parms(arg, I2OPARMSET);
230 break;
232 case I2OPARMGET:
233 ret = ioctl_parms(arg, I2OPARMGET);
234 break;
236 case I2OSWDL:
237 ret = ioctl_swdl(arg);
238 break;
240 case I2OSWUL:
241 ret = ioctl_swul(arg);
242 break;
244 case I2OSWDEL:
245 ret = ioctl_swdel(arg);
246 break;
248 case I2OVALIDATE:
249 ret = ioctl_validate(arg);
250 break;
252 case I2OHTML:
253 ret = ioctl_html(arg);
254 break;
256 case I2OEVTREG:
257 ret = ioctl_evt_reg(arg, fp);
258 break;
260 case I2OEVTGET:
261 ret = ioctl_evt_get(arg, fp);
262 break;
264 default:
265 ret = -EINVAL;
268 return ret;
271 int ioctl_getiops(unsigned long arg)
273 u8 *user_iop_table = (u8*)arg;
274 struct i2o_controller *c = NULL;
275 int i;
276 u8 foo[MAX_I2O_CONTROLLERS];
278 if(!access_ok(VERIFY_WRITE, user_iop_table, MAX_I2O_CONTROLLERS))
279 return -EFAULT;
281 for(i = 0; i < MAX_I2O_CONTROLLERS; i++)
283 c = i2o_find_controller(i);
284 if(c)
286 foo[i] = 1;
287 i2o_unlock_controller(c);
289 else
291 foo[i] = 0;
295 __copy_to_user(user_iop_table, foo, MAX_I2O_CONTROLLERS);
296 return 0;
299 int ioctl_gethrt(unsigned long arg)
301 struct i2o_controller *c;
302 struct i2o_cmd_hrtlct *cmd = (struct i2o_cmd_hrtlct*)arg;
303 struct i2o_cmd_hrtlct kcmd;
304 i2o_hrt *hrt;
305 int len;
306 u32 reslen;
307 int ret = 0;
309 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
310 return -EFAULT;
312 if(get_user(reslen, kcmd.reslen) < 0)
313 return -EFAULT;
315 if(kcmd.resbuf == NULL)
316 return -EFAULT;
318 c = i2o_find_controller(kcmd.iop);
319 if(!c)
320 return -ENXIO;
322 hrt = (i2o_hrt *)c->hrt;
324 i2o_unlock_controller(c);
326 len = 8 + ((hrt->entry_len * hrt->num_entries) << 2);
328 /* We did a get user...so assuming mem is ok...is this bad? */
329 put_user(len, kcmd.reslen);
330 if(len > reslen)
331 ret = -ENOBUFS;
332 if(copy_to_user(kcmd.resbuf, (void*)hrt, len))
333 ret = -EFAULT;
335 return ret;
338 int ioctl_getlct(unsigned long arg)
340 struct i2o_controller *c;
341 struct i2o_cmd_hrtlct *cmd = (struct i2o_cmd_hrtlct*)arg;
342 struct i2o_cmd_hrtlct kcmd;
343 i2o_lct *lct;
344 int len;
345 int ret = 0;
346 u32 reslen;
348 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_hrtlct)))
349 return -EFAULT;
351 if(get_user(reslen, kcmd.reslen) < 0)
352 return -EFAULT;
354 if(kcmd.resbuf == NULL)
355 return -EFAULT;
357 c = i2o_find_controller(kcmd.iop);
358 if(!c)
359 return -ENXIO;
361 lct = (i2o_lct *)c->lct;
362 i2o_unlock_controller(c);
364 len = (unsigned int)lct->table_size << 2;
365 put_user(len, kcmd.reslen);
366 if(len > reslen)
367 ret = -ENOBUFS;
368 else if(copy_to_user(kcmd.resbuf, (void*)lct, len))
369 ret = -EFAULT;
371 return ret;
374 static int ioctl_parms(unsigned long arg, unsigned int type)
376 int ret = 0;
377 struct i2o_controller *c;
378 struct i2o_cmd_psetget *cmd = (struct i2o_cmd_psetget*)arg;
379 struct i2o_cmd_psetget kcmd;
380 u32 reslen;
381 u8 *ops;
382 u8 *res;
383 int len;
385 u32 i2o_cmd = (type == I2OPARMGET ?
386 I2O_CMD_UTIL_PARAMS_GET :
387 I2O_CMD_UTIL_PARAMS_SET);
389 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_cmd_psetget)))
390 return -EFAULT;
392 if(get_user(reslen, kcmd.reslen))
393 return -EFAULT;
395 c = i2o_find_controller(kcmd.iop);
396 if(!c)
397 return -ENXIO;
399 ops = (u8*)kmalloc(kcmd.oplen, GFP_KERNEL);
400 if(!ops)
402 i2o_unlock_controller(c);
403 return -ENOMEM;
406 if(copy_from_user(ops, kcmd.opbuf, kcmd.oplen))
408 i2o_unlock_controller(c);
409 kfree(ops);
410 return -EFAULT;
414 * It's possible to have a _very_ large table
415 * and that the user asks for all of it at once...
417 res = (u8*)kmalloc(65536, GFP_KERNEL);
418 if(!res)
420 i2o_unlock_controller(c);
421 kfree(ops);
422 return -ENOMEM;
425 len = i2o_issue_params(i2o_cmd, c, kcmd.tid,
426 ops, kcmd.oplen, res, 65536);
427 i2o_unlock_controller(c);
428 kfree(ops);
430 if (len < 0) {
431 kfree(res);
432 return -EAGAIN;
435 put_user(len, kcmd.reslen);
436 if(len > reslen)
437 ret = -ENOBUFS;
438 else if(copy_to_user(cmd->resbuf, res, len))
439 ret = -EFAULT;
441 kfree(res);
443 return ret;
446 int ioctl_html(unsigned long arg)
448 struct i2o_html *cmd = (struct i2o_html*)arg;
449 struct i2o_html kcmd;
450 struct i2o_controller *c;
451 u8 *res = NULL;
452 void *query = NULL;
453 int ret = 0;
454 int token;
455 u32 len;
456 u32 reslen;
457 u32 msg[MSG_FRAME_SIZE/4];
459 if(copy_from_user(&kcmd, cmd, sizeof(struct i2o_html)))
461 printk(KERN_INFO "i2o_config: can't copy html cmd\n");
462 return -EFAULT;
465 if(get_user(reslen, kcmd.reslen) < 0)
467 printk(KERN_INFO "i2o_config: can't copy html reslen\n");
468 return -EFAULT;
471 if(!kcmd.resbuf)
473 printk(KERN_INFO "i2o_config: NULL html buffer\n");
474 return -EFAULT;
477 c = i2o_find_controller(kcmd.iop);
478 if(!c)
479 return -ENXIO;
481 if(kcmd.qlen) /* Check for post data */
483 query = kmalloc(kcmd.qlen, GFP_KERNEL);
484 if(!query)
486 i2o_unlock_controller(c);
487 return -ENOMEM;
489 if(copy_from_user(query, kcmd.qbuf, kcmd.qlen))
491 i2o_unlock_controller(c);
492 printk(KERN_INFO "i2o_config: could not get query\n");
493 kfree(query);
494 return -EFAULT;
498 res = kmalloc(65536, GFP_KERNEL);
499 if(!res)
501 i2o_unlock_controller(c);
502 return -ENOMEM;
505 msg[1] = (I2O_CMD_UTIL_CONFIG_DIALOG << 24)|HOST_TID<<12|kcmd.tid;
506 msg[2] = i2o_cfg_context;
507 msg[3] = 0;
508 msg[4] = kcmd.page;
509 msg[5] = 0xD0000000|65536;
510 msg[6] = virt_to_bus(res);
511 if(!kcmd.qlen) /* Check for post data */
512 msg[0] = SEVEN_WORD_MSG_SIZE|SGL_OFFSET_5;
513 else
515 msg[0] = NINE_WORD_MSG_SIZE|SGL_OFFSET_5;
516 msg[5] = 0x50000000|65536;
517 msg[7] = 0xD4000000|(kcmd.qlen);
518 msg[8] = virt_to_bus(query);
521 token = i2o_post_wait(c, msg, 9*4, 10);
522 if(token)
524 printk(KERN_DEBUG "token = %#10x\n", token);
525 i2o_unlock_controller(c);
526 kfree(res);
527 if(kcmd.qlen) kfree(query);
529 return -ETIMEDOUT;
531 i2o_unlock_controller(c);
533 len = strnlen(res, 65536);
534 put_user(len, kcmd.reslen);
535 if(len > reslen)
536 ret = -ENOMEM;
537 if(copy_to_user(kcmd.resbuf, res, len))
538 ret = -EFAULT;
540 kfree(res);
541 if(kcmd.qlen)
542 kfree(query);
544 return ret;
547 int ioctl_swdl(unsigned long arg)
549 struct i2o_sw_xfer kxfer;
550 struct i2o_sw_xfer *pxfer = (struct i2o_sw_xfer *)arg;
551 unsigned char maxfrag = 0, curfrag = 1;
552 unsigned char *buffer;
553 u32 msg[9];
554 unsigned int status = 0, swlen = 0, fragsize = 8192;
555 struct i2o_controller *c;
557 if(copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
558 return -EFAULT;
560 if(get_user(swlen, kxfer.swlen) < 0)
561 return -EFAULT;
563 if(get_user(maxfrag, kxfer.maxfrag) < 0)
564 return -EFAULT;
566 if(get_user(curfrag, kxfer.curfrag) < 0)
567 return -EFAULT;
569 if(curfrag==maxfrag) fragsize = swlen-(maxfrag-1)*8192;
571 if(!kxfer.buf || !access_ok(VERIFY_READ, kxfer.buf, fragsize))
572 return -EFAULT;
574 c = i2o_find_controller(kxfer.iop);
575 if(!c)
576 return -ENXIO;
578 buffer=kmalloc(fragsize, GFP_KERNEL);
579 if (buffer==NULL)
581 i2o_unlock_controller(c);
582 return -ENOMEM;
584 __copy_from_user(buffer, kxfer.buf, fragsize);
586 msg[0]= NINE_WORD_MSG_SIZE | SGL_OFFSET_7;
587 msg[1]= I2O_CMD_SW_DOWNLOAD<<24 | HOST_TID<<12 | ADAPTER_TID;
588 msg[2]= (u32)cfg_handler.context;
589 msg[3]= 0;
590 msg[4]= (((u32)kxfer.flags)<<24) | (((u32)kxfer.sw_type)<<16) |
591 (((u32)maxfrag)<<8) | (((u32)curfrag));
592 msg[5]= swlen;
593 msg[6]= kxfer.sw_id;
594 msg[7]= (0xD0000000 | fragsize);
595 msg[8]= virt_to_bus(buffer);
597 // printk("i2o_config: swdl frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
598 status = i2o_post_wait(c, msg, sizeof(msg), 60);
600 i2o_unlock_controller(c);
601 kfree(buffer);
603 if (status != I2O_POST_WAIT_OK)
605 // it fails if you try and send frags out of order
606 // and for some yet unknown reasons too
607 printk(KERN_INFO "i2o_config: swdl failed, DetailedStatus = %d\n", status);
608 return -ETIMEDOUT;
611 return 0;
614 int ioctl_swul(unsigned long arg)
616 struct i2o_sw_xfer kxfer;
617 struct i2o_sw_xfer *pxfer = (struct i2o_sw_xfer *)arg;
618 unsigned char maxfrag = 0, curfrag = 1;
619 unsigned char *buffer;
620 u32 msg[9];
621 unsigned int status = 0, swlen = 0, fragsize = 8192;
622 struct i2o_controller *c;
624 if(copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
625 return -EFAULT;
627 if(get_user(swlen, kxfer.swlen) < 0)
628 return -EFAULT;
630 if(get_user(maxfrag, kxfer.maxfrag) < 0)
631 return -EFAULT;
633 if(get_user(curfrag, kxfer.curfrag) < 0)
634 return -EFAULT;
636 if(curfrag==maxfrag) fragsize = swlen-(maxfrag-1)*8192;
638 if(!kxfer.buf || !access_ok(VERIFY_WRITE, kxfer.buf, fragsize))
639 return -EFAULT;
641 c = i2o_find_controller(kxfer.iop);
642 if(!c)
643 return -ENXIO;
645 buffer=kmalloc(fragsize, GFP_KERNEL);
646 if (buffer==NULL)
648 i2o_unlock_controller(c);
649 return -ENOMEM;
652 msg[0]= NINE_WORD_MSG_SIZE | SGL_OFFSET_7;
653 msg[1]= I2O_CMD_SW_UPLOAD<<24 | HOST_TID<<12 | ADAPTER_TID;
654 msg[2]= (u32)cfg_handler.context;
655 msg[3]= 0;
656 msg[4]= (u32)kxfer.flags<<24|(u32)kxfer.sw_type<<16|(u32)maxfrag<<8|(u32)curfrag;
657 msg[5]= swlen;
658 msg[6]= kxfer.sw_id;
659 msg[7]= (0xD0000000 | fragsize);
660 msg[8]= virt_to_bus(buffer);
662 // printk("i2o_config: swul frag %d/%d (size %d)\n", curfrag, maxfrag, fragsize);
663 status = i2o_post_wait(c, msg, sizeof(msg), 60);
664 i2o_unlock_controller(c);
666 if (status != I2O_POST_WAIT_OK)
668 kfree(buffer);
669 printk(KERN_INFO "i2o_config: swul failed, DetailedStatus = %d\n", status);
670 return -ETIMEDOUT;
673 __copy_to_user(kxfer.buf, buffer, fragsize);
674 kfree(buffer);
676 return 0;
679 int ioctl_swdel(unsigned long arg)
681 struct i2o_controller *c;
682 struct i2o_sw_xfer kxfer, *pxfer = (struct i2o_sw_xfer *)arg;
683 u32 msg[7];
684 unsigned int swlen;
685 int token;
687 if (copy_from_user(&kxfer, pxfer, sizeof(struct i2o_sw_xfer)))
688 return -EFAULT;
690 if (get_user(swlen, kxfer.swlen) < 0)
691 return -EFAULT;
693 c = i2o_find_controller(kxfer.iop);
694 if (!c)
695 return -ENXIO;
697 msg[0] = SEVEN_WORD_MSG_SIZE | SGL_OFFSET_0;
698 msg[1] = I2O_CMD_SW_REMOVE<<24 | HOST_TID<<12 | ADAPTER_TID;
699 msg[2] = (u32)i2o_cfg_context;
700 msg[3] = 0;
701 msg[4] = (u32)kxfer.flags<<24 | (u32)kxfer.sw_type<<16;
702 msg[5] = swlen;
703 msg[6] = kxfer.sw_id;
705 token = i2o_post_wait(c, msg, sizeof(msg), 10);
706 i2o_unlock_controller(c);
708 if (token != I2O_POST_WAIT_OK)
710 printk(KERN_INFO "i2o_config: swdel failed, DetailedStatus = %d\n", token);
711 return -ETIMEDOUT;
714 return 0;
717 int ioctl_validate(unsigned long arg)
719 int token;
720 int iop = (int)arg;
721 u32 msg[4];
722 struct i2o_controller *c;
724 c=i2o_find_controller(iop);
725 if (!c)
726 return -ENXIO;
728 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
729 msg[1] = I2O_CMD_CONFIG_VALIDATE<<24 | HOST_TID<<12 | iop;
730 msg[2] = (u32)i2o_cfg_context;
731 msg[3] = 0;
733 token = i2o_post_wait(c, msg, sizeof(msg), 10);
734 i2o_unlock_controller(c);
736 if (token != I2O_POST_WAIT_OK)
738 printk(KERN_INFO "Can't validate configuration, ErrorStatus = %d\n",
739 token);
740 return -ETIMEDOUT;
743 return 0;
746 static int ioctl_evt_reg(unsigned long arg, struct file *fp)
748 u32 msg[5];
749 struct i2o_evt_id *pdesc = (struct i2o_evt_id *)arg;
750 struct i2o_evt_id kdesc;
751 struct i2o_controller *iop;
752 struct i2o_device *d;
754 if (copy_from_user(&kdesc, pdesc, sizeof(struct i2o_evt_id)))
755 return -EFAULT;
757 /* IOP exists? */
758 iop = i2o_find_controller(kdesc.iop);
759 if(!iop)
760 return -ENXIO;
761 i2o_unlock_controller(iop);
763 /* Device exists? */
764 for(d = iop->devices; d; d = d->next)
765 if(d->lct_data.tid == kdesc.tid)
766 break;
768 if(!d)
769 return -ENODEV;
771 msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
772 msg[1] = I2O_CMD_UTIL_EVT_REGISTER<<24 | HOST_TID<<12 | kdesc.tid;
773 msg[2] = (u32)i2o_cfg_context;
774 msg[3] = (u32)fp->private_data;
775 msg[4] = kdesc.evt_mask;
777 i2o_post_this(iop, msg, 20);
779 return 0;
782 static int ioctl_evt_get(unsigned long arg, struct file *fp)
784 u32 id = (u32)fp->private_data;
785 struct i2o_cfg_info *p = NULL;
786 struct i2o_evt_get *uget = (struct i2o_evt_get*)arg;
787 struct i2o_evt_get kget;
788 unsigned int flags;
790 // access_ok doesn't check for NULL?!?!
791 if(!arg)
792 return -EFAULT;
794 if(!access_ok(VERIFY_WRITE, uget, sizeof(struct i2o_evt_get)))
795 return -EFAULT;
797 for(p = open_files; p; p = p->next)
798 if(p->q_id == id)
799 break;
801 if(!p->q_len)
803 return -ENOENT;
804 return 0;
807 memcpy(&kget.info, &p->event_q[p->q_out], sizeof(struct i2o_evt_info));
808 MODINC(p->q_out, I2O_EVT_Q_LEN);
809 spin_lock_irqsave(&i2o_config_lock, flags);
810 p->q_len--;
811 kget.pending = p->q_len;
812 kget.lost = p->q_lost;
813 spin_unlock_irqrestore(&i2o_config_lock, flags);
815 __copy_to_user(uget, &kget, sizeof(struct i2o_evt_get));
817 return 0;
820 static int cfg_open(struct inode *inode, struct file *file)
822 struct i2o_cfg_info *tmp =
823 (struct i2o_cfg_info *)kmalloc(sizeof(struct i2o_cfg_info), GFP_KERNEL);
824 unsigned int flags;
826 if(!tmp)
827 return -ENOMEM;
829 file->private_data = (void*)(i2o_cfg_info_id++);
830 tmp->fp = file;
831 tmp->fasync = NULL;
832 tmp->q_id = (u32)file->private_data;
833 tmp->q_len = 0;
834 tmp->q_in = 0;
835 tmp->q_out = 0;
836 tmp->q_lost = 0;
837 tmp->next = open_files;
839 spin_lock_irqsave(&i2o_config_lock, flags);
840 open_files = tmp;
841 spin_unlock_irqrestore(&i2o_config_lock, flags);
843 return 0;
846 static int cfg_release(struct inode *inode, struct file *file)
848 u32 id = (u32)file->private_data;
849 struct i2o_cfg_info *p1, *p2;
850 unsigned int flags;
852 lock_kernel();
853 p1 = p2 = NULL;
855 spin_lock_irqsave(&i2o_config_lock, flags);
856 for(p1 = open_files; p1; )
858 if(p1->q_id == id)
861 if(p1->fasync)
862 cfg_fasync(-1, file, 0);
863 if(p2)
864 p2->next = p1->next;
865 else
866 open_files = p1->next;
868 kfree(p1);
869 break;
871 p2 = p1;
872 p1 = p1->next;
874 spin_unlock_irqrestore(&i2o_config_lock, flags);
875 unlock_kernel();
877 return 0;
880 static int cfg_fasync(int fd, struct file *fp, int on)
882 u32 id = (u32)fp->private_data;
883 struct i2o_cfg_info *p;
885 for(p = open_files; p; p = p->next)
886 if(p->q_id == id)
887 break;
889 if(!p)
890 return -EBADF;
892 return fasync_helper(fd, fp, on, &p->fasync);
895 static struct file_operations config_fops =
897 owner: THIS_MODULE,
898 llseek: cfg_llseek,
899 read: cfg_read,
900 write: cfg_write,
901 ioctl: cfg_ioctl,
902 open: cfg_open,
903 release: cfg_release,
904 fasync: cfg_fasync,
907 static struct miscdevice i2o_miscdev = {
908 I2O_MINOR,
909 "i2octl",
910 &config_fops
913 #ifdef MODULE
914 int init_module(void)
915 #else
916 int __init i2o_config_init(void)
917 #endif
919 printk(KERN_INFO "I2O configuration manager v 0.04.\n");
920 printk(KERN_INFO " (C) Copyright 1999 Red Hat Software\n");
922 if((page_buf = kmalloc(4096, GFP_KERNEL))==NULL)
924 printk(KERN_ERR "i2o_config: no memory for page buffer.\n");
925 return -ENOBUFS;
927 if(misc_register(&i2o_miscdev)==-1)
929 printk(KERN_ERR "i2o_config: can't register device.\n");
930 kfree(page_buf);
931 return -EBUSY;
934 * Install our handler
936 if(i2o_install_handler(&cfg_handler)<0)
938 kfree(page_buf);
939 printk(KERN_ERR "i2o_config: handler register failed.\n");
940 misc_deregister(&i2o_miscdev);
941 return -EBUSY;
944 * The low 16bits of the transaction context must match this
945 * for everything we post. Otherwise someone else gets our mail
947 i2o_cfg_context = cfg_handler.context;
948 return 0;
951 #ifdef MODULE
953 void cleanup_module(void)
955 misc_deregister(&i2o_miscdev);
957 if(page_buf)
958 kfree(page_buf);
959 if(i2o_cfg_context != -1)
960 i2o_remove_handler(&cfg_handler);
961 if(i2o_buffer)
962 kfree(i2o_buffer);
965 EXPORT_NO_SYMBOLS;
966 MODULE_AUTHOR("Red Hat Software");
967 MODULE_DESCRIPTION("I2O Configuration");
969 #endif