commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / ieee1394 / raw1394.c
1 /*
2  * IEEE 1394 for Linux
3  *
4  * Raw interface to the bus
5  *
6  * Copyright (C) 1999, 2000 Andreas E. Bombe
7  *               2001, 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
8  *                     2002 Christian Toegel <christian.toegel@gmx.at>
9  *
10  * This code is licensed under the GPL.  See the file COPYING in the root
11  * directory of the kernel sources for details.
12  *
13  *
14  * Contributions:
15  *
16  * Manfred Weihs <weihs@ict.tuwien.ac.at>
17  *        configuration ROM manipulation
18  *        address range mapping
19  *        adaptation for new (transparent) loopback mechanism
20  *        sending of arbitrary async packets
21  * Christian Toegel <christian.toegel@gmx.at>
22  *        address range mapping
23  *        lock64 request
24  *        transmit physical packet
25  *        busreset notification control (switch on/off)
26  *        busreset with selection of type (short/long)
27  *        request_reply
28  */
29
30 #include <linux/kernel.h>
31 #include <linux/list.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
34 #include <linux/fs.h>
35 #include <linux/poll.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/version.h>
39 #include <linux/smp_lock.h>
40 #include <linux/interrupt.h>
41 #include <asm/uaccess.h>
42 #include <asm/atomic.h>
43 #include <linux/devfs_fs_kernel.h>
44
45 #include "ieee1394.h"
46 #include "ieee1394_types.h"
47 #include "ieee1394_core.h"
48 #include "nodemgr.h"
49 #include "hosts.h"
50 #include "highlevel.h"
51 #include "iso.h"
52 #include "ieee1394_transactions.h"
53 #include "raw1394.h"
54 #include "raw1394-private.h"
55
56 #if BITS_PER_LONG == 64
57 #define int2ptr(x) ((void *)x)
58 #define ptr2int(x) ((u64)x)
59 #else
60 #define int2ptr(x) ((void *)(u32)x)
61 #define ptr2int(x) ((u64)(u32)x)
62 #endif
63
64 #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
65 #define RAW1394_DEBUG
66 #endif
67
68 #ifdef RAW1394_DEBUG
69 #define DBGMSG(fmt, args...) \
70 printk(KERN_INFO "raw1394:" fmt "\n" , ## args)
71 #else
72 #define DBGMSG(fmt, args...)
73 #endif
74
75 static LIST_HEAD(host_info_list);
76 static int host_count;
77 static spinlock_t host_info_lock = SPIN_LOCK_UNLOCKED;
78 static atomic_t internal_generation = ATOMIC_INIT(0);
79
80 static atomic_t iso_buffer_size;
81 static const int iso_buffer_max = 4 * 1024 * 1024; /* 4 MB */
82
83 static struct hpsb_highlevel raw1394_highlevel;
84
85 static int arm_read (struct hpsb_host *host, int nodeid, quadlet_t *buffer,
86                      u64 addr, size_t length, u16 flags);
87 static int arm_write (struct hpsb_host *host, int nodeid, int destid,
88                       quadlet_t *data, u64 addr, size_t length, u16 flags);
89 static int arm_lock (struct hpsb_host *host, int nodeid, quadlet_t *store,
90              u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags);
91 static int arm_lock64 (struct hpsb_host *host, int nodeid, octlet_t *store,
92                u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags);
93 static struct hpsb_address_ops arm_ops = {
94         .read   = arm_read,
95         .write  = arm_write,
96         .lock   = arm_lock,
97         .lock64 = arm_lock64,
98 };
99
100 static void queue_complete_cb(struct pending_request *req);
101
102 static struct pending_request *__alloc_pending_request(int flags)
103 {
104         struct pending_request *req;
105
106         req = (struct pending_request *)kmalloc(sizeof(struct pending_request),
107                                                 flags);
108         if (req != NULL) {
109                 memset(req, 0, sizeof(struct pending_request));
110                 INIT_LIST_HEAD(&req->list);
111         }
112
113         return req;
114 }
115
116 static inline struct pending_request *alloc_pending_request(void)
117 {
118         return __alloc_pending_request(SLAB_KERNEL);
119 }
120
121 static void free_pending_request(struct pending_request *req)
122 {
123         if (req->ibs) {
124                 if (atomic_dec_and_test(&req->ibs->refcount)) {
125                         atomic_sub(req->ibs->data_size, &iso_buffer_size);
126                         kfree(req->ibs);
127                 }
128         } else if (req->free_data) {
129                 kfree(req->data);
130         }
131         free_hpsb_packet(req->packet);
132         kfree(req);
133 }
134
135 /* fi->reqlists_lock must be taken */
136 static void __queue_complete_req(struct pending_request *req)
137 {
138         struct file_info *fi = req->file_info;
139         list_del(&req->list);
140         list_add_tail(&req->list, &fi->req_complete);
141
142         up(&fi->complete_sem);
143         wake_up_interruptible(&fi->poll_wait_complete);
144 }
145
146 static void queue_complete_req(struct pending_request *req)
147 {
148         unsigned long flags;
149         struct file_info *fi = req->file_info;
150
151         spin_lock_irqsave(&fi->reqlists_lock, flags);
152         __queue_complete_req(req);
153         spin_unlock_irqrestore(&fi->reqlists_lock, flags);
154 }
155
156 static void queue_complete_cb(struct pending_request *req)
157 {
158         struct hpsb_packet *packet = req->packet;
159         int rcode = (packet->header[1] >> 12) & 0xf;
160
161         switch (packet->ack_code) {
162         case ACKX_NONE:
163         case ACKX_SEND_ERROR:
164                 req->req.error = RAW1394_ERROR_SEND_ERROR;
165                 break;
166         case ACKX_ABORTED:
167                 req->req.error = RAW1394_ERROR_ABORTED;
168                 break;
169         case ACKX_TIMEOUT:
170                 req->req.error = RAW1394_ERROR_TIMEOUT;
171                 break;
172         default:
173                 req->req.error = (packet->ack_code << 16) | rcode;
174                 break;
175         }
176
177         if (!((packet->ack_code == ACK_PENDING) && (rcode == RCODE_COMPLETE))) {
178                 req->req.length = 0;
179         }
180
181         if ((req->req.type == RAW1394_REQ_ASYNC_READ) ||
182             (req->req.type == RAW1394_REQ_ASYNC_WRITE) ||
183             (req->req.type == RAW1394_REQ_LOCK) ||
184             (req->req.type == RAW1394_REQ_LOCK64))
185                 hpsb_free_tlabel(packet);
186
187         queue_complete_req(req);
188 }
189
190
191 static void add_host(struct hpsb_host *host)
192 {
193         struct host_info *hi;
194         unsigned long flags;
195
196         hi = (struct host_info *)kmalloc(sizeof(struct host_info), GFP_KERNEL);
197
198         if (hi != NULL) {
199                 INIT_LIST_HEAD(&hi->list);
200                 hi->host = host;
201                 INIT_LIST_HEAD(&hi->file_info_list);
202
203                 spin_lock_irqsave(&host_info_lock, flags);
204                 list_add_tail(&hi->list, &host_info_list);
205                 host_count++;
206                 spin_unlock_irqrestore(&host_info_lock, flags);
207         }
208
209         atomic_inc(&internal_generation);
210 }
211
212
213 static struct host_info *find_host_info(struct hpsb_host *host)
214 {
215         struct list_head *lh;
216         struct host_info *hi;
217
218         list_for_each(lh, &host_info_list) {
219                 hi = list_entry(lh, struct host_info, list);
220                 if (hi->host == host) {
221                         return hi;
222                 }
223         }
224
225         return NULL;
226 }
227
228 static void remove_host(struct hpsb_host *host)
229 {
230         struct host_info *hi;
231         unsigned long flags;
232
233         spin_lock_irqsave(&host_info_lock, flags);
234         hi = find_host_info(host);
235
236         if (hi != NULL) {
237                 list_del(&hi->list);
238                 host_count--;
239                 /* 
240                    FIXME: address ranges should be removed 
241                    and fileinfo states should be initialized
242                    (including setting generation to 
243                    internal-generation ...)
244                 */
245         }
246         spin_unlock_irqrestore(&host_info_lock, flags);
247
248         if (hi == NULL) {
249                 printk(KERN_ERR "raw1394: attempt to remove unknown host "
250                        "0x%p\n", host);
251                 return;
252         }
253
254         kfree(hi);
255
256         atomic_inc(&internal_generation);
257 }
258
259 static void host_reset(struct hpsb_host *host)
260 {
261         unsigned long flags;
262         struct list_head *lh;
263         struct host_info *hi;
264         struct file_info *fi;
265         struct pending_request *req;
266
267         spin_lock_irqsave(&host_info_lock, flags);
268         hi = find_host_info(host);
269
270         if (hi != NULL) {
271                 list_for_each(lh, &hi->file_info_list) {
272                         fi = list_entry(lh, struct file_info, list);
273                         if (fi->notification == RAW1394_NOTIFY_ON) {
274                                 req = __alloc_pending_request(SLAB_ATOMIC);
275
276                                 if (req != NULL) {
277                                         req->file_info = fi;
278                                         req->req.type = RAW1394_REQ_BUS_RESET;
279                                         req->req.generation = get_hpsb_generation(host);
280                                         req->req.misc = (host->node_id << 16)
281                                                 | host->node_count;
282                                         if (fi->protocol_version > 3) {
283                                                 req->req.misc |= (NODEID_TO_NODE(host->irm_id)
284                                                                   << 8);
285                                         }
286
287                                         queue_complete_req(req);
288                                 }
289                         }
290                 }
291         }
292         spin_unlock_irqrestore(&host_info_lock, flags);
293 }
294
295 static void iso_receive(struct hpsb_host *host, int channel, quadlet_t *data,
296                         size_t length)
297 {
298         unsigned long flags;
299         struct list_head *lh;
300         struct host_info *hi;
301         struct file_info *fi;
302         struct pending_request *req;
303         struct iso_block_store *ibs = NULL;
304         LIST_HEAD(reqs);
305
306         if ((atomic_read(&iso_buffer_size) + length) > iso_buffer_max) {
307                 HPSB_INFO("dropped iso packet");
308                 return;
309         }
310
311         spin_lock_irqsave(&host_info_lock, flags);
312         hi = find_host_info(host);
313
314         if (hi != NULL) {
315                 list_for_each(lh, &hi->file_info_list) {
316                         fi = list_entry(lh, struct file_info, list);
317
318                         if (!(fi->listen_channels & (1ULL << channel))) {
319                                 continue;
320                         }
321
322                         req = __alloc_pending_request(SLAB_ATOMIC);
323                         if (!req) break;
324
325                         if (!ibs) {
326                                 ibs = kmalloc(sizeof(struct iso_block_store)
327                                               + length, SLAB_ATOMIC);
328                                 if (!ibs) {
329                                         kfree(req);
330                                         break;
331                                 }
332
333                                 atomic_add(length, &iso_buffer_size);
334                                 atomic_set(&ibs->refcount, 0);
335                                 ibs->data_size = length;
336                                 memcpy(ibs->data, data, length);
337                         }
338
339                         atomic_inc(&ibs->refcount);
340
341                         req->file_info = fi;
342                         req->ibs = ibs;
343                         req->data = ibs->data;
344                         req->req.type = RAW1394_REQ_ISO_RECEIVE;
345                         req->req.generation = get_hpsb_generation(host);
346                         req->req.misc = 0;
347                         req->req.recvb = ptr2int(fi->iso_buffer);
348                         req->req.length = min(length, fi->iso_buffer_length);
349                         
350                         list_add_tail(&req->list, &reqs);
351                 }
352         }
353         spin_unlock_irqrestore(&host_info_lock, flags);
354
355         lh = reqs.next;
356         while (lh != &reqs) {
357                 req = list_entry(lh, struct pending_request, list);
358                 lh = lh->next;
359
360                 queue_complete_req(req);
361         }
362 }
363
364 static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
365                         int cts, u8 *data, size_t length)
366 {
367         unsigned long flags;
368         struct list_head *lh;
369         struct host_info *hi;
370         struct file_info *fi;
371         struct pending_request *req;
372         struct iso_block_store *ibs = NULL;
373         LIST_HEAD(reqs);
374
375         if ((atomic_read(&iso_buffer_size) + length) > iso_buffer_max) {
376                 HPSB_INFO("dropped fcp request");
377                 return;
378         }
379
380         spin_lock_irqsave(&host_info_lock, flags);
381         hi = find_host_info(host);
382
383         if (hi != NULL) {
384                 list_for_each(lh, &hi->file_info_list) {
385                         fi = list_entry(lh, struct file_info, list);
386
387                         if (!fi->fcp_buffer) {
388                                 continue;
389                         }
390
391                         req = __alloc_pending_request(SLAB_ATOMIC);
392                         if (!req) break;
393
394                         if (!ibs) {
395                                 ibs = kmalloc(sizeof(struct iso_block_store)
396                                               + length, SLAB_ATOMIC);
397                                 if (!ibs) {
398                                         kfree(req);
399                                         break;
400                                 }
401
402                                 atomic_add(length, &iso_buffer_size);
403                                 atomic_set(&ibs->refcount, 0);
404                                 ibs->data_size = length;
405                                 memcpy(ibs->data, data, length);
406                         }
407
408                         atomic_inc(&ibs->refcount);
409
410                         req->file_info = fi;
411                         req->ibs = ibs;
412                         req->data = ibs->data;
413                         req->req.type = RAW1394_REQ_FCP_REQUEST;
414                         req->req.generation = get_hpsb_generation(host);
415                         req->req.misc = nodeid | (direction << 16);
416                         req->req.recvb = ptr2int(fi->fcp_buffer);
417                         req->req.length = length;
418                         
419                         list_add_tail(&req->list, &reqs);
420                 }
421         }
422         spin_unlock_irqrestore(&host_info_lock, flags);
423
424         lh = reqs.next;
425         while (lh != &reqs) {
426                 req = list_entry(lh, struct pending_request, list);
427                 lh = lh->next;
428
429                 queue_complete_req(req);
430         }
431 }
432
433
434 static ssize_t raw1394_read(struct file *file, char *buffer, size_t count,
435                     loff_t *offset_is_ignored)
436 {
437         struct file_info *fi = (struct file_info *)file->private_data;
438         struct list_head *lh;
439         struct pending_request *req;
440
441         if (count != sizeof(struct raw1394_request)) {
442                 return -EINVAL;
443         }
444
445         if (!access_ok(VERIFY_WRITE, buffer, count)) {
446                 return -EFAULT;
447         }
448
449         if (file->f_flags & O_NONBLOCK) {
450                 if (down_trylock(&fi->complete_sem)) {
451                         return -EAGAIN;
452                 }
453         } else {
454                 if (down_interruptible(&fi->complete_sem)) {
455                         return -ERESTARTSYS;
456                 }
457         }
458
459         spin_lock_irq(&fi->reqlists_lock);
460         lh = fi->req_complete.next;
461         list_del(lh);
462         spin_unlock_irq(&fi->reqlists_lock);
463
464         req = list_entry(lh, struct pending_request, list);
465
466         if (req->req.length) {
467                 if (copy_to_user(int2ptr(req->req.recvb), req->data,
468                                  req->req.length)) {
469                         req->req.error = RAW1394_ERROR_MEMFAULT;
470                 }
471         }
472         __copy_to_user(buffer, &req->req, sizeof(req->req));
473
474         free_pending_request(req);
475         return sizeof(struct raw1394_request);
476 }
477
478
479 static int state_opened(struct file_info *fi, struct pending_request *req)
480 {
481         if (req->req.type == RAW1394_REQ_INITIALIZE) {
482                 switch (req->req.misc) {
483                 case RAW1394_KERNELAPI_VERSION:
484                 case 3:
485                         fi->state = initialized;
486                         fi->protocol_version = req->req.misc;
487                         req->req.error = RAW1394_ERROR_NONE;
488                         req->req.generation = atomic_read(&internal_generation);
489                         break;
490
491                 default:
492                         req->req.error = RAW1394_ERROR_COMPAT;
493                         req->req.misc = RAW1394_KERNELAPI_VERSION;
494                 }
495         } else {
496                 req->req.error = RAW1394_ERROR_STATE_ORDER;
497         }
498
499         req->req.length = 0;
500         queue_complete_req(req);
501         return sizeof(struct raw1394_request);
502 }
503
504 static int state_initialized(struct file_info *fi, struct pending_request *req)
505 {
506         struct list_head *lh;
507         struct host_info *hi;
508         struct raw1394_khost_list *khl;
509
510         if (req->req.generation != atomic_read(&internal_generation)) {
511                 req->req.error = RAW1394_ERROR_GENERATION;
512                 req->req.generation = atomic_read(&internal_generation);
513                 req->req.length = 0;
514                 queue_complete_req(req);
515                 return sizeof(struct raw1394_request);
516         }
517
518         switch (req->req.type) {
519         case RAW1394_REQ_LIST_CARDS:
520                 spin_lock_irq(&host_info_lock);
521                 khl = kmalloc(sizeof(struct raw1394_khost_list) * host_count,
522                               SLAB_ATOMIC);
523
524                 if (khl != NULL) {
525                         req->req.misc = host_count;
526                         req->data = (quadlet_t *)khl;
527                         
528                         list_for_each(lh, &host_info_list) {
529                                 hi = list_entry(lh, struct host_info, list);
530
531                                 khl->nodes = hi->host->node_count;
532                                 strcpy(khl->name, hi->host->driver->name);
533
534                                 khl++;
535                         }
536                 }
537                 spin_unlock_irq(&host_info_lock);
538
539                 if (khl != NULL) {
540                         req->req.error = RAW1394_ERROR_NONE;
541                         req->req.length = min(req->req.length,
542                                               (u32)(sizeof(struct raw1394_khost_list)
543                                               * req->req.misc));
544                         req->free_data = 1;
545                 } else {
546                         return -ENOMEM;
547                 }
548                 break;
549
550         case RAW1394_REQ_SET_CARD:
551                 lh = NULL;
552
553                 spin_lock_irq(&host_info_lock);
554                 if (req->req.misc < host_count) {
555                         lh = host_info_list.next;
556                         while (req->req.misc--) {
557                                 lh = lh->next;
558                         }
559                         hi = list_entry(lh, struct host_info, list);
560                         hpsb_ref_host(hi->host); // XXX Need to handle failure case
561                         list_add_tail(&fi->list, &hi->file_info_list);
562                         fi->host = hi->host;
563                         fi->state = connected;
564                 }
565                 spin_unlock_irq(&host_info_lock);
566
567                 if (lh != NULL) {
568                         req->req.error = RAW1394_ERROR_NONE;
569                         req->req.generation = get_hpsb_generation(fi->host);
570                         req->req.misc = (fi->host->node_id << 16) 
571                                 | fi->host->node_count;
572                         if (fi->protocol_version > 3) {
573                                 req->req.misc |= NODEID_TO_NODE(fi->host->irm_id) << 8;
574                         }
575                 } else {
576                         req->req.error = RAW1394_ERROR_INVALID_ARG;
577                 }
578
579                 req->req.length = 0;
580                 break;
581
582         default:
583                 req->req.error = RAW1394_ERROR_STATE_ORDER;
584                 req->req.length = 0;
585                 break;
586         }
587
588         queue_complete_req(req);
589         return sizeof(struct raw1394_request);
590 }
591
592 static void handle_iso_listen(struct file_info *fi, struct pending_request *req)
593 {
594         int channel = req->req.misc;
595
596         spin_lock_irq(&host_info_lock);
597         if ((channel > 63) || (channel < -64)) {
598                 req->req.error = RAW1394_ERROR_INVALID_ARG;
599         } else if (channel >= 0) {
600                 /* allocate channel req.misc */
601                 if (fi->listen_channels & (1ULL << channel)) {
602                         req->req.error = RAW1394_ERROR_ALREADY;
603                 } else {
604                         if (hpsb_listen_channel(&raw1394_highlevel, fi->host, channel)) {
605                                 req->req.error = RAW1394_ERROR_ALREADY;
606                         } else {
607                                 fi->listen_channels |= 1ULL << channel;
608                                 fi->iso_buffer = int2ptr(req->req.recvb);
609                                 fi->iso_buffer_length = req->req.length;
610                         }
611                 }
612         } else {
613                 /* deallocate channel (one's complement neg) req.misc */
614                 channel = ~channel;
615
616                 if (fi->listen_channels & (1ULL << channel)) {
617                         hpsb_unlisten_channel(&raw1394_highlevel, fi->host, channel);
618                         fi->listen_channels &= ~(1ULL << channel);
619                 } else {
620                         req->req.error = RAW1394_ERROR_INVALID_ARG;
621                 }
622         }
623
624         req->req.length = 0;
625         queue_complete_req(req);
626         spin_unlock_irq(&host_info_lock);
627 }
628
629 static void handle_fcp_listen(struct file_info *fi, struct pending_request *req)
630 {
631         if (req->req.misc) {
632                 if (fi->fcp_buffer) {
633                         req->req.error = RAW1394_ERROR_ALREADY;
634                 } else {
635                         fi->fcp_buffer = (u8 *)int2ptr(req->req.recvb);
636                 }
637         } else {
638                 if (!fi->fcp_buffer) {
639                         req->req.error = RAW1394_ERROR_ALREADY;
640                 } else {
641                         fi->fcp_buffer = NULL;
642                 }
643         }
644
645         req->req.length = 0;
646         queue_complete_req(req);
647 }
648
649
650 static int handle_async_request(struct file_info *fi,
651                                 struct pending_request *req, int node)
652 {
653         struct hpsb_packet *packet = NULL;
654         u64 addr = req->req.address & 0xffffffffffffULL;
655
656         switch (req->req.type) {
657         case RAW1394_REQ_ASYNC_READ:
658                 DBGMSG("read_request called");
659                 packet = hpsb_make_readpacket(fi->host, node, addr, req->req.length);
660
661                 if (!packet)
662                         return -ENOMEM;
663
664                 if (req->req.length == 4)
665                         req->data = &packet->header[3];
666                 else
667                         req->data = packet->data;
668   
669                 break;
670
671         case RAW1394_REQ_ASYNC_WRITE:
672                 DBGMSG("write_request called");
673
674                 packet = hpsb_make_writepacket(fi->host, node, addr, NULL,
675                                                req->req.length);
676                 if (!packet)
677                         return -ENOMEM;
678
679                 if (req->req.length == 4) {
680                         if (copy_from_user(&packet->header[3], int2ptr(req->req.sendb),
681                                         req->req.length))
682                                 req->req.error = RAW1394_ERROR_MEMFAULT;
683                 } else {
684                         if (copy_from_user(packet->data, int2ptr(req->req.sendb),
685                                         req->req.length))
686                                 req->req.error = RAW1394_ERROR_MEMFAULT;
687                 }
688                         
689                 req->req.length = 0;
690             break;
691
692         case RAW1394_REQ_LOCK:
693                 DBGMSG("lock_request called");
694                 if ((req->req.misc == EXTCODE_FETCH_ADD)
695                     || (req->req.misc == EXTCODE_LITTLE_ADD)) {
696                         if (req->req.length != 4) {
697                                 req->req.error = RAW1394_ERROR_INVALID_ARG;
698                                 break;
699                         }
700                 } else {
701                         if (req->req.length != 8) {
702                                 req->req.error = RAW1394_ERROR_INVALID_ARG;
703                                 break;
704                         }
705                 }
706
707                 packet = hpsb_make_lockpacket(fi->host, node, addr,
708                                               req->req.misc, NULL, 0);
709                 if (!packet) return -ENOMEM;
710
711                 if (copy_from_user(packet->data, int2ptr(req->req.sendb),
712                                    req->req.length)) {
713                         req->req.error = RAW1394_ERROR_MEMFAULT;
714                         break;
715                 }
716
717                 req->data = packet->data;
718                 req->req.length = 4;
719                 break;
720
721         case RAW1394_REQ_LOCK64:
722                 DBGMSG("lock64_request called");
723                 if ((req->req.misc == EXTCODE_FETCH_ADD)
724                     || (req->req.misc == EXTCODE_LITTLE_ADD)) {
725                         if (req->req.length != 8) {
726                                 req->req.error = RAW1394_ERROR_INVALID_ARG;
727                                 break;
728                         }
729                 } else {
730                         if (req->req.length != 16) {
731                                 req->req.error = RAW1394_ERROR_INVALID_ARG;
732                                 break;
733                         }
734                 }
735                 packet = hpsb_make_lock64packet(fi->host, node, addr,
736                                                 req->req.misc, NULL, 0);
737                 if (!packet) return -ENOMEM;
738
739                 if (copy_from_user(packet->data, int2ptr(req->req.sendb),
740                                    req->req.length)) {
741                         req->req.error = RAW1394_ERROR_MEMFAULT;
742                         break;
743                 }
744
745                 req->data = packet->data;
746                 req->req.length = 8;
747                 break;
748
749         default:
750                 req->req.error = RAW1394_ERROR_STATE_ORDER;
751         }
752
753         req->packet = packet;
754
755         if (req->req.error) {
756                 req->req.length = 0;
757                 queue_complete_req(req);
758                 return sizeof(struct raw1394_request);
759         }
760
761         hpsb_set_packet_complete_task(packet, (void(*)(void*))queue_complete_cb, req);
762
763         spin_lock_irq(&fi->reqlists_lock);
764         list_add_tail(&req->list, &fi->req_pending);
765         spin_unlock_irq(&fi->reqlists_lock);
766
767         packet->generation = req->req.generation;
768
769         if (!hpsb_send_packet(packet)) {
770                 req->req.error = RAW1394_ERROR_SEND_ERROR;
771                 req->req.length = 0;
772                 hpsb_free_tlabel(packet);
773                 queue_complete_req(req);
774         }
775         return sizeof(struct raw1394_request);
776 }
777
778 static int handle_iso_send(struct file_info *fi, struct pending_request *req,
779                            int channel)
780 {
781         struct hpsb_packet *packet;
782
783         packet = hpsb_make_isopacket(fi->host, req->req.length, channel & 0x3f,
784                                      (req->req.misc >> 16) & 0x3, req->req.misc & 0xf);
785         if (!packet)
786                 return -ENOMEM;
787
788         packet->speed_code = req->req.address & 0x3;
789
790         req->packet = packet;
791
792         if (copy_from_user(packet->data, int2ptr(req->req.sendb),
793                            req->req.length)) {
794                 req->req.error = RAW1394_ERROR_MEMFAULT;
795                 req->req.length = 0;
796                 queue_complete_req(req);
797                 return sizeof(struct raw1394_request);
798         }
799
800         req->req.length = 0;
801         hpsb_set_packet_complete_task(packet, (void (*)(void*))queue_complete_req, req);
802
803         spin_lock_irq(&fi->reqlists_lock);
804         list_add_tail(&req->list, &fi->req_pending);
805         spin_unlock_irq(&fi->reqlists_lock);
806
807         /* Update the generation of the packet just before sending. */
808         packet->generation = req->req.generation;
809
810         if (!hpsb_send_packet(packet)) {
811                 req->req.error = RAW1394_ERROR_SEND_ERROR;
812                 queue_complete_req(req);
813         }
814
815         return sizeof(struct raw1394_request);
816 }
817
818 static int handle_async_send(struct file_info *fi, struct pending_request *req)
819 {
820         struct hpsb_packet *packet;
821         int header_length = req->req.misc & 0xffff;
822         int expect_response = req->req.misc >> 16;
823
824         if ((header_length > req->req.length) ||
825             (header_length  < 12))
826         {
827                 req->req.error = RAW1394_ERROR_INVALID_ARG;
828                 req->req.length = 0;
829                 queue_complete_req(req);
830                 return sizeof(struct raw1394_request);
831         } 
832
833         packet = alloc_hpsb_packet(req->req.length-header_length);
834         req->packet = packet;
835         if (!packet) return -ENOMEM;
836
837         if (copy_from_user(packet->header, int2ptr(req->req.sendb),
838                            header_length)) {
839                 req->req.error = RAW1394_ERROR_MEMFAULT;
840                 req->req.length = 0;
841                 queue_complete_req(req);
842                 return sizeof(struct raw1394_request);
843         }
844
845         if (copy_from_user(packet->data, ((u8*) int2ptr(req->req.sendb)) + header_length,
846                            packet->data_size)) {
847                 req->req.error = RAW1394_ERROR_MEMFAULT;
848                 req->req.length = 0;
849                 queue_complete_req(req);
850                 return sizeof(struct raw1394_request);
851         }
852
853         packet->type = hpsb_async;
854         packet->node_id = packet->header[0] >> 16;
855         packet->tcode = (packet->header[0] >> 4) & 0xf;
856         packet->tlabel = (packet->header[0] >> 10) &0x3f;
857         packet->host = fi->host;
858         packet->expect_response = expect_response;
859         packet->header_size=header_length;
860         packet->data_size=req->req.length-header_length;
861
862         req->req.length = 0;
863         hpsb_set_packet_complete_task(packet, (void(*)(void*))queue_complete_cb, req);
864
865         spin_lock_irq(&fi->reqlists_lock);
866         list_add_tail(&req->list, &fi->req_pending);
867         spin_unlock_irq(&fi->reqlists_lock);
868
869         /* Update the generation of the packet just before sending. */
870         packet->generation = req->req.generation;
871
872         if (!hpsb_send_packet(packet)) {
873                 req->req.error = RAW1394_ERROR_SEND_ERROR;
874                 queue_complete_req(req);
875         }
876
877         return sizeof(struct raw1394_request);
878 }
879
880 static int arm_read (struct hpsb_host *host, int nodeid, quadlet_t *buffer,
881                      u64 addr, size_t length, u16 flags)
882 {
883         struct pending_request *req;
884         struct list_head *lh;
885         struct host_info *hi;
886         struct file_info *fi = NULL;
887         struct list_head *entry;
888         struct arm_addr  *arm_addr = NULL;
889         struct arm_request  *arm_req = NULL;
890         struct arm_response *arm_resp = NULL;
891         int found=0, size=0, rcode=-1;
892         struct arm_request_response *arm_req_resp = NULL;
893
894         DBGMSG("arm_read  called by node: %X"
895               "addr: %4.4x %8.8x length: %u", nodeid,
896               (u16) ((addr >>32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
897               length);
898         spin_lock(&host_info_lock);
899         hi = find_host_info(host); /* search address-entry */
900         if (hi != NULL) {
901                 list_for_each(lh, &hi->file_info_list) {
902                         fi = list_entry(lh, struct file_info, list);
903                         entry = fi->addr_list.next;
904                         while (entry != &(fi->addr_list)) {
905                                 arm_addr = list_entry(entry, struct arm_addr, addr_list);
906                                 if (((arm_addr->start) <= (addr)) && 
907                                         ((arm_addr->end) >= (addr+length))) {
908                                         found = 1;
909                                         break;
910                                 }
911                                 entry = entry->next;
912                         }
913                         if (found) {
914                                 break;
915                         }
916                 }
917         }
918         rcode = -1;
919         if (!found) {
920                 printk(KERN_ERR "raw1394: arm_read FAILED addr_entry not found"
921                 " -> rcode_address_error\n");
922                 spin_unlock(&host_info_lock);
923                 return (RCODE_ADDRESS_ERROR);
924         } else {
925                 DBGMSG("arm_read addr_entry FOUND");
926         }
927         if (arm_addr->rec_length < length) {
928                 DBGMSG("arm_read blocklength too big -> rcode_data_error");
929                 rcode = RCODE_DATA_ERROR; /* hardware error, data is unavailable */
930         }
931         if (rcode == -1) {
932                 if (arm_addr->access_rights & ARM_READ) {
933                         if (!(arm_addr->client_transactions & ARM_READ)) {
934                                 memcpy(buffer,(arm_addr->addr_space_buffer)+(addr-(arm_addr->start)), 
935                                        length);
936                                 DBGMSG("arm_read -> (rcode_complete)");
937                                 rcode = RCODE_COMPLETE;
938                         }
939                 } else {
940                         rcode = RCODE_TYPE_ERROR; /* function not allowed */
941                         DBGMSG("arm_read -> rcode_type_error (access denied)");
942                 }
943         }
944         if (arm_addr->notification_options & ARM_READ) {
945                 DBGMSG("arm_read -> entering notification-section");
946                 req = __alloc_pending_request(SLAB_ATOMIC);
947                 if (!req) {
948                         DBGMSG("arm_read -> rcode_conflict_error");
949                         spin_unlock(&host_info_lock);
950                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
951                                                         The request may be retried */
952                 }
953                 if (rcode == RCODE_COMPLETE) {
954                         size =  sizeof(struct arm_request)+sizeof(struct arm_response) +
955                                 length * sizeof(byte_t) +
956                                 sizeof (struct arm_request_response);
957                 } else {
958                         size =  sizeof(struct arm_request)+sizeof(struct arm_response) +
959                                 sizeof (struct arm_request_response);
960                 }
961                 req->data = kmalloc(size, SLAB_ATOMIC);
962                 if (!(req->data)) {
963                         free_pending_request(req);
964                         DBGMSG("arm_read -> rcode_conflict_error");
965                         spin_unlock(&host_info_lock);
966                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
967                                                         The request may be retried */
968                 }
969                 req->free_data=1;
970                 req->file_info = fi;
971                 req->req.type = RAW1394_REQ_ARM;
972                 req->req.generation = get_hpsb_generation(host);
973                 req->req.misc = ( ((length << 16) & (0xFFFF0000)) | (ARM_READ & 0xFF));
974                 req->req.tag  = arm_addr->arm_tag;
975                 req->req.recvb = arm_addr->recvb;
976                 req->req.length = size;
977                 arm_req_resp = (struct arm_request_response *) (req->data);
978                 arm_req  = (struct arm_request *) ((byte_t *)(req->data) + 
979                         (sizeof (struct arm_request_response)));
980                 arm_resp = (struct arm_response *) ((byte_t *)(arm_req) + 
981                         (sizeof(struct arm_request)));
982                 arm_req->buffer  = NULL;
983                 arm_resp->buffer = NULL;
984                 if (rcode == RCODE_COMPLETE) {
985                         arm_resp->buffer = ((byte_t *)(arm_resp) + 
986                                 (sizeof(struct arm_response)));
987                         memcpy (arm_resp->buffer,
988                                 (arm_addr->addr_space_buffer)+(addr-(arm_addr->start)), 
989                                 length);
990                         arm_resp->buffer = int2ptr((arm_addr->recvb) + 
991                                 sizeof (struct arm_request_response) +
992                                 sizeof (struct arm_request) +
993                                 sizeof (struct arm_response));
994                 }
995                 arm_resp->buffer_length = (rcode == RCODE_COMPLETE) ? length : 0;
996                 arm_resp->response_code = rcode;
997                 arm_req->buffer_length = 0;
998                 arm_req->generation = req->req.generation;
999                 arm_req->extended_transaction_code = 0;
1000                 arm_req->destination_offset = addr;
1001                 arm_req->source_nodeid = nodeid;
1002                 arm_req->destination_nodeid = host->node_id;
1003                 arm_req->tlabel = (flags >> 10) & 0x3f;
1004                 arm_req->tcode = (flags >> 4) & 0x0f;
1005                 arm_req_resp->request  = int2ptr((arm_addr->recvb) + 
1006                         sizeof (struct arm_request_response));
1007                 arm_req_resp->response = int2ptr((arm_addr->recvb) + 
1008                         sizeof (struct arm_request_response) +
1009                         sizeof (struct arm_request));
1010                 queue_complete_req(req);
1011         }
1012         spin_unlock(&host_info_lock);
1013         return(rcode);
1014 }
1015
1016 static int arm_write (struct hpsb_host *host, int nodeid, int destid,
1017                       quadlet_t *data, u64 addr, size_t length, u16 flags)
1018 {
1019         struct pending_request *req;
1020         struct list_head *lh;
1021         struct host_info *hi;
1022         struct file_info *fi = NULL;
1023         struct list_head *entry;
1024         struct arm_addr  *arm_addr = NULL;
1025         struct arm_request  *arm_req = NULL;
1026         struct arm_response *arm_resp = NULL;        
1027         int found=0, size=0, rcode=-1, length_conflict=0;
1028         struct arm_request_response *arm_req_resp = NULL;
1029
1030         DBGMSG("arm_write called by node: %X"
1031               "addr: %4.4x %8.8x length: %u", nodeid,
1032               (u16) ((addr >>32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
1033               length);
1034         spin_lock(&host_info_lock);
1035         hi = find_host_info(host); /* search address-entry */
1036         if (hi != NULL) {
1037                 list_for_each(lh, &hi->file_info_list) {
1038                         fi = list_entry(lh, struct file_info, list);
1039                         entry = fi->addr_list.next;
1040                         while (entry != &(fi->addr_list)) {
1041                                 arm_addr = list_entry(entry, struct arm_addr, addr_list);
1042                                 if (((arm_addr->start) <= (addr)) && 
1043                                         ((arm_addr->end) >= (addr+length))) {
1044                                         found = 1;
1045                                         break;
1046                                 }
1047                                 entry = entry->next;
1048                         }
1049                         if (found) {
1050                                 break;
1051                         }
1052                 }
1053         }
1054         rcode = -1;
1055         if (!found) {
1056                 printk(KERN_ERR "raw1394: arm_write FAILED addr_entry not found"
1057                 " -> rcode_address_error\n");
1058                 spin_unlock(&host_info_lock);
1059                 return (RCODE_ADDRESS_ERROR);
1060         } else {
1061                 DBGMSG("arm_write addr_entry FOUND");
1062         }
1063         if (arm_addr->rec_length < length) {
1064                 DBGMSG("arm_write blocklength too big -> rcode_data_error");
1065                 length_conflict = 1;
1066                 rcode = RCODE_DATA_ERROR; /* hardware error, data is unavailable */
1067         }
1068         if (rcode == -1) {
1069                 if (arm_addr->access_rights & ARM_WRITE) {
1070                         if (!(arm_addr->client_transactions & ARM_WRITE)) {
1071                                 memcpy((arm_addr->addr_space_buffer)+(addr-(arm_addr->start)),
1072                                         data, length);
1073                                 DBGMSG("arm_write -> (rcode_complete)");
1074                                 rcode = RCODE_COMPLETE;
1075                         }
1076                 } else {
1077                         rcode = RCODE_TYPE_ERROR; /* function not allowed */
1078                         DBGMSG("arm_write -> rcode_type_error (access denied)");
1079                 }
1080         }
1081         if (arm_addr->notification_options & ARM_WRITE) {
1082                 DBGMSG("arm_write -> entering notification-section");
1083                 req = __alloc_pending_request(SLAB_ATOMIC);
1084                 if (!req) {
1085                         DBGMSG("arm_write -> rcode_conflict_error");
1086                         spin_unlock(&host_info_lock);
1087                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1088                                                         The request my be retried */
1089                 }
1090                 size =  sizeof(struct arm_request)+sizeof(struct arm_response) +
1091                         (length) * sizeof(byte_t) +
1092                         sizeof (struct arm_request_response);
1093                 req->data = kmalloc(size, SLAB_ATOMIC);
1094                 if (!(req->data)) {
1095                         free_pending_request(req);
1096                         DBGMSG("arm_write -> rcode_conflict_error");
1097                         spin_unlock(&host_info_lock);
1098                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1099                                                         The request may be retried */
1100                 }
1101                 req->free_data=1;
1102                 req->file_info = fi;
1103                 req->req.type = RAW1394_REQ_ARM;
1104                 req->req.generation = get_hpsb_generation(host);
1105                 req->req.misc = ( ((length << 16) & (0xFFFF0000)) | (ARM_WRITE & 0xFF));
1106                 req->req.tag  = arm_addr->arm_tag;
1107                 req->req.recvb = arm_addr->recvb;
1108                 req->req.length = size;
1109                 arm_req_resp = (struct arm_request_response *) (req->data);
1110                 arm_req  = (struct arm_request *) ((byte_t *)(req->data) + 
1111                         (sizeof (struct arm_request_response)));
1112                 arm_resp = (struct arm_response *) ((byte_t *)(arm_req) + 
1113                         (sizeof(struct arm_request)));
1114                 arm_req->buffer = ((byte_t *)(arm_resp) + 
1115                         (sizeof(struct arm_response)));
1116                 arm_resp->buffer = NULL;
1117                 memcpy (arm_req->buffer, data, length);
1118                 arm_req->buffer = int2ptr((arm_addr->recvb) + 
1119                         sizeof (struct arm_request_response) +
1120                         sizeof (struct arm_request) +
1121                         sizeof (struct arm_response));
1122                 arm_req->buffer_length = length;
1123                 arm_req->generation = req->req.generation;
1124                 arm_req->extended_transaction_code = 0;
1125                 arm_req->destination_offset = addr;
1126                 arm_req->source_nodeid = nodeid;
1127                 arm_req->destination_nodeid = destid;
1128                 arm_req->tlabel = (flags >> 10) & 0x3f;
1129                 arm_req->tcode = (flags >> 4) & 0x0f;
1130                 arm_resp->buffer_length = 0;
1131                 arm_resp->response_code = rcode;
1132                 arm_req_resp->request  = int2ptr((arm_addr->recvb) + 
1133                         sizeof (struct arm_request_response));
1134                 arm_req_resp->response = int2ptr((arm_addr->recvb) + 
1135                         sizeof (struct arm_request_response) +
1136                         sizeof (struct arm_request));
1137                 queue_complete_req(req);
1138         }
1139         spin_unlock(&host_info_lock);
1140         return(rcode);
1141 }
1142
1143 static int arm_lock (struct hpsb_host *host, int nodeid, quadlet_t *store,
1144              u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags)
1145 {
1146         struct pending_request *req;
1147         struct list_head *lh;
1148         struct host_info *hi;
1149         struct file_info *fi = NULL;
1150         struct list_head *entry;
1151         struct arm_addr  *arm_addr = NULL;
1152         struct arm_request  *arm_req = NULL;
1153         struct arm_response *arm_resp = NULL;        
1154         int found=0, size=0, rcode=-1;
1155         quadlet_t old, new;
1156         struct arm_request_response *arm_req_resp = NULL;
1157
1158         if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
1159                 ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
1160                 DBGMSG("arm_lock  called by node: %X "
1161                       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X", 
1162                       nodeid, (u16) ((addr >>32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
1163                       ext_tcode & 0xFF , be32_to_cpu(data));
1164         } else {
1165                 DBGMSG("arm_lock  called by node: %X "
1166                       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X arg: %8.8X", 
1167                       nodeid, (u16) ((addr >>32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
1168                       ext_tcode & 0xFF , be32_to_cpu(data), be32_to_cpu(arg));
1169         }
1170         spin_lock(&host_info_lock);
1171         hi = find_host_info(host); /* search address-entry */
1172         if (hi != NULL) {
1173                 list_for_each(lh, &hi->file_info_list) {
1174                         fi = list_entry(lh, struct file_info, list);
1175                         entry = fi->addr_list.next;
1176                         while (entry != &(fi->addr_list)) {
1177                                 arm_addr = list_entry(entry, struct arm_addr, addr_list);
1178                                 if (((arm_addr->start) <= (addr)) && 
1179                                         ((arm_addr->end) >= (addr+sizeof(*store)))) {
1180                                         found = 1;
1181                                         break;
1182                                 }
1183                                 entry = entry->next;
1184                         }
1185                         if (found) {
1186                                 break;
1187                         }
1188                 }
1189         }
1190         rcode = -1;
1191         if (!found) {
1192                 printk(KERN_ERR "raw1394: arm_lock FAILED addr_entry not found"
1193                 " -> rcode_address_error\n");
1194                 spin_unlock(&host_info_lock);
1195                 return (RCODE_ADDRESS_ERROR);
1196         } else {
1197                 DBGMSG("arm_lock addr_entry FOUND");
1198         }
1199         if (rcode == -1) {
1200                 if (arm_addr->access_rights & ARM_LOCK) {
1201                         if (!(arm_addr->client_transactions & ARM_LOCK)) {
1202                                 memcpy(&old,(arm_addr->addr_space_buffer)+(addr-(arm_addr->start)),
1203                                         sizeof(old));
1204                                 switch (ext_tcode) {
1205                                         case (EXTCODE_MASK_SWAP):
1206                                                 new = data | (old & ~arg);
1207                                                 break;
1208                                         case (EXTCODE_COMPARE_SWAP):
1209                                                 if (old == arg) {
1210                                                         new = data;
1211                                                 } else {
1212                                                         new = old;
1213                                                 }
1214                                                 break;
1215                                         case (EXTCODE_FETCH_ADD):
1216                                                 new = cpu_to_be32(be32_to_cpu(data) + be32_to_cpu(old));
1217                                                 break;
1218                                         case (EXTCODE_LITTLE_ADD):
1219                                                 new = cpu_to_le32(le32_to_cpu(data) + le32_to_cpu(old));
1220                                                 break;
1221                                         case (EXTCODE_BOUNDED_ADD):
1222                                                 if (old != arg) {
1223                                                         new = cpu_to_be32(be32_to_cpu(data) + 
1224                                                                 be32_to_cpu(old));
1225                                                 } else {
1226                                                         new = old;
1227                                                 }
1228                                                 break;
1229                                         case (EXTCODE_WRAP_ADD):
1230                                                 if (old != arg) {
1231                                                         new = cpu_to_be32(be32_to_cpu(data) + 
1232                                                                 be32_to_cpu(old));
1233                                                 } else {
1234                                                         new = data;
1235                                                 }
1236                                                 break;
1237                                         default:
1238                                                 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1239                                                 printk(KERN_ERR "raw1394: arm_lock FAILED "
1240                                                 "ext_tcode not allowed -> rcode_type_error\n");
1241                                                 break;
1242                                 } /*switch*/
1243                                 if (rcode == -1) {
1244                                         DBGMSG("arm_lock -> (rcode_complete)");
1245                                         rcode = RCODE_COMPLETE;
1246                                         memcpy (store, &old, sizeof(*store));
1247                                         memcpy ((arm_addr->addr_space_buffer)+
1248                                                 (addr-(arm_addr->start)), 
1249                                                 &new, sizeof(*store));
1250                                 }
1251                         }
1252                 } else {
1253                         rcode = RCODE_TYPE_ERROR; /* function not allowed */
1254                         DBGMSG("arm_lock -> rcode_type_error (access denied)");
1255                 }
1256         }
1257         if (arm_addr->notification_options & ARM_LOCK) {
1258                 DBGMSG("arm_lock -> entering notification-section");
1259                 req = __alloc_pending_request(SLAB_ATOMIC);
1260                 if (!req) {
1261                         DBGMSG("arm_lock -> rcode_conflict_error");
1262                         spin_unlock(&host_info_lock);
1263                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1264                                                         The request may be retried */
1265                 }
1266                 size =  sizeof(struct arm_request)+sizeof(struct arm_response) +
1267                         3 * sizeof(*store) + 
1268                         sizeof (struct arm_request_response);  /* maximum */
1269                 req->data = kmalloc(size, SLAB_ATOMIC);
1270                 if (!(req->data)) {
1271                         free_pending_request(req);
1272                         DBGMSG("arm_lock -> rcode_conflict_error");
1273                         spin_unlock(&host_info_lock);
1274                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1275                                                         The request may be retried */
1276                 }
1277                 req->free_data=1;
1278                 arm_req_resp = (struct arm_request_response *) (req->data);
1279                 arm_req  = (struct arm_request *) ((byte_t *)(req->data) + 
1280                         (sizeof (struct arm_request_response)));
1281                 arm_resp = (struct arm_response *) ((byte_t *)(arm_req) + 
1282                         (sizeof(struct arm_request)));
1283                 arm_req->buffer = ((byte_t *)(arm_resp) + 
1284                         (sizeof(struct arm_response)));
1285                 arm_resp->buffer = ((byte_t *)(arm_req->buffer) + 
1286                         (2* sizeof(*store)));
1287                 if ((ext_tcode == EXTCODE_FETCH_ADD) || 
1288                         (ext_tcode == EXTCODE_LITTLE_ADD)) {
1289                         arm_req->buffer_length = sizeof(*store);
1290                         memcpy (arm_req->buffer, &data, sizeof(*store));
1291
1292                 } else {
1293                         arm_req->buffer_length = 2 * sizeof(*store);
1294                         memcpy (arm_req->buffer, &arg,  sizeof(*store));
1295                         memcpy (((arm_req->buffer) + sizeof(*store)), 
1296                                 &data, sizeof(*store));
1297                 }
1298                 if (rcode == RCODE_COMPLETE) {
1299                         arm_resp->buffer_length = sizeof(*store);
1300                         memcpy (arm_resp->buffer, &old, sizeof(*store));
1301                 } else {
1302                         arm_resp->buffer = NULL;
1303                         arm_resp->buffer_length = 0;
1304                 }
1305                 req->file_info = fi;
1306                 req->req.type = RAW1394_REQ_ARM;
1307                 req->req.generation = get_hpsb_generation(host);
1308                 req->req.misc = ( (((sizeof(*store)) << 16) & (0xFFFF0000)) | 
1309                         (ARM_LOCK & 0xFF));
1310                 req->req.tag  = arm_addr->arm_tag;
1311                 req->req.recvb = arm_addr->recvb;
1312                 req->req.length = size;
1313                 arm_req->generation = req->req.generation;
1314                 arm_req->extended_transaction_code = ext_tcode;
1315                 arm_req->destination_offset = addr;
1316                 arm_req->source_nodeid = nodeid;
1317                 arm_req->destination_nodeid = host->node_id;
1318                 arm_req->tlabel = (flags >> 10) & 0x3f;
1319                 arm_req->tcode = (flags >> 4) & 0x0f;
1320                 arm_resp->response_code = rcode;
1321                 arm_req_resp->request  = int2ptr((arm_addr->recvb) + 
1322                         sizeof (struct arm_request_response));
1323                 arm_req_resp->response = int2ptr((arm_addr->recvb) + 
1324                         sizeof (struct arm_request_response) +
1325                         sizeof (struct arm_request));
1326                 arm_req->buffer = int2ptr((arm_addr->recvb) + 
1327                         sizeof (struct arm_request_response) +
1328                         sizeof (struct arm_request) +
1329                         sizeof (struct arm_response));
1330                 arm_resp->buffer = int2ptr((arm_addr->recvb) + 
1331                         sizeof (struct arm_request_response) +
1332                         sizeof (struct arm_request) +
1333                         sizeof (struct arm_response) +
1334                         2* sizeof (*store));
1335                 queue_complete_req(req);
1336         }
1337         spin_unlock(&host_info_lock);
1338         return(rcode);
1339 }
1340
1341 static int arm_lock64 (struct hpsb_host *host, int nodeid, octlet_t *store,
1342                u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags)
1343 {
1344         struct pending_request *req;
1345         struct list_head *lh;
1346         struct host_info *hi;
1347         struct file_info *fi = NULL;
1348         struct list_head *entry;
1349         struct arm_addr  *arm_addr = NULL;
1350         struct arm_request  *arm_req = NULL;
1351         struct arm_response *arm_resp = NULL;
1352         int found=0, size=0, rcode=-1;
1353         octlet_t old, new;
1354         struct arm_request_response *arm_req_resp = NULL;
1355
1356         if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
1357                 ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
1358                 DBGMSG("arm_lock64 called by node: %X "
1359                       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X ",
1360                       nodeid, (u16) ((addr >>32) & 0xFFFF),
1361                       (u32) (addr & 0xFFFFFFFF), 
1362                       ext_tcode & 0xFF , 
1363                       (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF), 
1364                       (u32) (be64_to_cpu(data) & 0xFFFFFFFF));
1365         } else {
1366                 DBGMSG("arm_lock64 called by node: %X "
1367                       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X arg: "
1368                       "%8.8X %8.8X ",
1369                       nodeid, (u16) ((addr >>32) & 0xFFFF),
1370                       (u32) (addr & 0xFFFFFFFF), 
1371                       ext_tcode & 0xFF , 
1372                       (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF), 
1373                       (u32) (be64_to_cpu(data) & 0xFFFFFFFF),
1374                       (u32) ((be64_to_cpu(arg)  >> 32) & 0xFFFFFFFF), 
1375                       (u32) (be64_to_cpu(arg)  & 0xFFFFFFFF));
1376         }
1377         spin_lock(&host_info_lock);
1378         hi = find_host_info(host); /* search addressentry in file_info's for host */
1379         if (hi != NULL) {
1380                 list_for_each(lh, &hi->file_info_list) {
1381                         fi = list_entry(lh, struct file_info, list);
1382                         entry = fi->addr_list.next;
1383                         while (entry != &(fi->addr_list)) {
1384                                 arm_addr = list_entry(entry, struct arm_addr, addr_list);
1385                                 if (((arm_addr->start) <= (addr)) && 
1386                                         ((arm_addr->end) >= (addr+sizeof(*store)))) {
1387                                         found = 1;
1388                                         break;
1389                                 }
1390                                 entry = entry->next;
1391                         }
1392                         if (found) {
1393                                 break;
1394                         }
1395                 }
1396         }
1397         rcode = -1;
1398         if (!found) {
1399                 printk(KERN_ERR "raw1394: arm_lock64 FAILED addr_entry not found"
1400                 " -> rcode_address_error\n");
1401                 spin_unlock(&host_info_lock);
1402                 return (RCODE_ADDRESS_ERROR);
1403         } else {
1404                 DBGMSG("arm_lock64 addr_entry FOUND");
1405         }
1406         if (rcode == -1) {
1407                 if (arm_addr->access_rights & ARM_LOCK) {
1408                         if (!(arm_addr->client_transactions & ARM_LOCK)) {
1409                                 memcpy(&old,(arm_addr->addr_space_buffer)+(addr-(arm_addr->start)),
1410                                         sizeof(old));
1411                                 switch (ext_tcode) {
1412                                         case (EXTCODE_MASK_SWAP):
1413                                                 new = data | (old & ~arg);
1414                                                 break;
1415                                         case (EXTCODE_COMPARE_SWAP):
1416                                                 if (old == arg) {
1417                                                         new = data;
1418                                                 } else {
1419                                                         new = old;
1420                                                 }
1421                                                 break;
1422                                         case (EXTCODE_FETCH_ADD):
1423                                                 new = cpu_to_be64(be64_to_cpu(data) + be64_to_cpu(old));
1424                                                 break;
1425                                         case (EXTCODE_LITTLE_ADD):
1426                                                 new = cpu_to_le64(le64_to_cpu(data) + le64_to_cpu(old));
1427                                                 break;
1428                                         case (EXTCODE_BOUNDED_ADD):
1429                                                 if (old != arg) {
1430                                                         new = cpu_to_be64(be64_to_cpu(data) + 
1431                                                                 be64_to_cpu(old));
1432                                                 } else {
1433                                                         new = old;
1434                                                 }
1435                                                 break;
1436                                         case (EXTCODE_WRAP_ADD):
1437                                                 if (old != arg) {
1438                                                         new = cpu_to_be64(be64_to_cpu(data) + 
1439                                                                 be64_to_cpu(old));
1440                                                 } else {
1441                                                         new = data;
1442                                                 }
1443                                                 break;
1444                                         default:
1445                                                 printk(KERN_ERR "raw1394: arm_lock64 FAILED "
1446                                                 "ext_tcode not allowed -> rcode_type_error\n");
1447                                                 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1448                                                 break;
1449                                 } /*switch*/
1450                                 if (rcode == -1) {
1451                                         DBGMSG("arm_lock64 -> (rcode_complete)");
1452                                         rcode = RCODE_COMPLETE;
1453                                         memcpy (store, &old, sizeof(*store));
1454                                         memcpy ((arm_addr->addr_space_buffer)+
1455                                                 (addr-(arm_addr->start)), 
1456                                                 &new, sizeof(*store));
1457                                 } 
1458                         }
1459                 } else {
1460                         rcode = RCODE_TYPE_ERROR; /* function not allowed */
1461                         DBGMSG("arm_lock64 -> rcode_type_error (access denied)");
1462                 }
1463         }
1464         if (arm_addr->notification_options & ARM_LOCK) {
1465                 DBGMSG("arm_lock64 -> entering notification-section");
1466                 req = __alloc_pending_request(SLAB_ATOMIC);
1467                 if (!req) {
1468                         spin_unlock(&host_info_lock);
1469                         DBGMSG("arm_lock64 -> rcode_conflict_error");
1470                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1471                                                         The request may be retried */
1472                 }
1473                 size =  sizeof(struct arm_request)+sizeof(struct arm_response) +
1474                         3 * sizeof(*store) +
1475                         sizeof (struct arm_request_response); /* maximum */
1476                 req->data = kmalloc(size, SLAB_ATOMIC);
1477                 if (!(req->data)) {
1478                         free_pending_request(req);
1479                         spin_unlock(&host_info_lock);
1480                         DBGMSG("arm_lock64 -> rcode_conflict_error");
1481                         return(RCODE_CONFLICT_ERROR); /* A resource conflict was detected. 
1482                                                         The request may be retried */
1483                 }
1484                 req->free_data=1;
1485                 arm_req_resp = (struct arm_request_response *) (req->data);
1486                 arm_req  = (struct arm_request *) ((byte_t *)(req->data) + 
1487                         (sizeof (struct arm_request_response)));
1488                 arm_resp = (struct arm_response *) ((byte_t *)(arm_req) + 
1489                         (sizeof(struct arm_request)));
1490                 arm_req->buffer = ((byte_t *)(arm_resp) + 
1491                         (sizeof(struct arm_response)));
1492                 arm_resp->buffer = ((byte_t *)(arm_req->buffer) + 
1493                         (2* sizeof(*store)));
1494                 if ((ext_tcode == EXTCODE_FETCH_ADD) || 
1495                         (ext_tcode == EXTCODE_LITTLE_ADD)) {
1496                         arm_req->buffer_length = sizeof(*store);
1497                         memcpy (arm_req->buffer, &data, sizeof(*store));
1498
1499                 } else {
1500                         arm_req->buffer_length = 2 * sizeof(*store);
1501                         memcpy (arm_req->buffer, &arg,  sizeof(*store));
1502                         memcpy (((arm_req->buffer) + sizeof(*store)), 
1503                                 &data, sizeof(*store));
1504                 }
1505                 if (rcode == RCODE_COMPLETE) {
1506                         arm_resp->buffer_length = sizeof(*store);
1507                         memcpy (arm_resp->buffer, &old, sizeof(*store));
1508                 } else {
1509                         arm_resp->buffer = NULL;
1510                         arm_resp->buffer_length = 0;
1511                 }
1512                 req->file_info = fi;
1513                 req->req.type = RAW1394_REQ_ARM;
1514                 req->req.generation = get_hpsb_generation(host);
1515                 req->req.misc = ( (((sizeof(*store)) << 16) & (0xFFFF0000)) | 
1516                         (ARM_LOCK & 0xFF));
1517                 req->req.tag  = arm_addr->arm_tag;
1518                 req->req.recvb = arm_addr->recvb;
1519                 req->req.length = size;
1520                 arm_req->generation = req->req.generation;
1521                 arm_req->extended_transaction_code = ext_tcode;
1522                 arm_req->destination_offset = addr;
1523                 arm_req->source_nodeid = nodeid;
1524                 arm_req->destination_nodeid = host->node_id;
1525                 arm_req->tlabel = (flags >> 10) & 0x3f;
1526                 arm_req->tcode = (flags >> 4) & 0x0f;
1527                 arm_resp->response_code = rcode;
1528                 arm_req_resp->request  = int2ptr((arm_addr->recvb) + 
1529                         sizeof (struct arm_request_response));
1530                 arm_req_resp->response = int2ptr((arm_addr->recvb) + 
1531                         sizeof (struct arm_request_response) +
1532                         sizeof (struct arm_request));
1533                 arm_req->buffer = int2ptr((arm_addr->recvb) + 
1534                         sizeof (struct arm_request_response) +
1535                         sizeof (struct arm_request) +
1536                         sizeof (struct arm_response));
1537                 arm_resp->buffer = int2ptr((arm_addr->recvb) + 
1538                         sizeof (struct arm_request_response) +
1539                         sizeof (struct arm_request) +
1540                         sizeof (struct arm_response) +
1541                         2* sizeof (*store));
1542                 queue_complete_req(req);
1543         }
1544         spin_unlock(&host_info_lock);
1545         return(rcode);
1546 }
1547
1548 static int arm_register(struct file_info *fi, struct pending_request *req)
1549 {
1550         int retval;
1551         struct arm_addr *addr;
1552         struct list_head *lh, *lh_1, *lh_2;
1553         struct host_info *hi;
1554         struct file_info *fi_hlp = NULL;
1555         struct list_head *entry;
1556         struct arm_addr  *arm_addr = NULL;
1557         int same_host, another_host;
1558         unsigned long flags;
1559
1560         DBGMSG("arm_register called "
1561               "addr(Offset): %8.8x %8.8x length: %u "
1562               "rights: %2.2X notify: %2.2X "
1563               "max_blk_len: %4.4X",
1564               (u32) ((req->req.address >>32) & 0xFFFF),
1565               (u32) (req->req.address & 0xFFFFFFFF),
1566               req->req.length, ((req->req.misc >> 8) & 0xFF),
1567               (req->req.misc & 0xFF),((req->req.misc >> 16) & 0xFFFF));
1568         /* check addressrange */
1569         if ((((req->req.address) & ~((u64)0xFFFFFFFFFFFFLL)) != 0) ||
1570                 (((req->req.address + req->req.length) & ~((u64)0xFFFFFFFFFFFFLL)) != 0)) {
1571                 req->req.length = 0;
1572                 return (-EINVAL);
1573         }
1574         /* addr-list-entry for fileinfo */
1575         addr = (struct arm_addr *)kmalloc(sizeof(struct arm_addr), SLAB_KERNEL); 
1576         if (!addr) {
1577                 req->req.length = 0;
1578                 return (-ENOMEM);
1579         } 
1580         /* allocation of addr_space_buffer */
1581         addr->addr_space_buffer = (u8 *)kmalloc(req->req.length,SLAB_KERNEL);
1582         if (!(addr->addr_space_buffer)) {
1583                 kfree(addr);
1584                 req->req.length = 0;
1585                 return (-ENOMEM);
1586         }
1587         /* initialization of addr_space_buffer */
1588         if ((req->req.sendb)== (unsigned long)NULL) {
1589                 /* init: set 0 */
1590                 memset(addr->addr_space_buffer, 0,req->req.length);
1591         } else {
1592                 /* init: user -> kernel */
1593                 if (copy_from_user(addr->addr_space_buffer,int2ptr(req->req.sendb),
1594                         req->req.length)) {
1595                         kfree(addr->addr_space_buffer);
1596                         kfree(addr);
1597                         return (-EFAULT);
1598                 }
1599         }
1600         INIT_LIST_HEAD(&addr->addr_list);
1601         addr->arm_tag   = req->req.tag;
1602         addr->start     = req->req.address;
1603         addr->end       = req->req.address + req->req.length;
1604         addr->access_rights = (u8) (req->req.misc & 0x0F);
1605         addr->notification_options = (u8) ((req->req.misc >> 4) & 0x0F);
1606         addr->client_transactions = (u8) ((req->req.misc >> 8) & 0x0F);
1607         addr->access_rights |= addr->client_transactions;
1608         addr->notification_options |= addr->client_transactions;
1609         addr->recvb     = req->req.recvb;
1610         addr->rec_length = (u16) ((req->req.misc >> 16) & 0xFFFF);
1611         spin_lock_irqsave(&host_info_lock, flags);
1612         hi = find_host_info(fi->host);
1613         same_host = 0;
1614         another_host = 0;
1615         /* same host with address-entry containing same addressrange ? */
1616         list_for_each(lh, &hi->file_info_list) {
1617                 fi_hlp = list_entry(lh, struct file_info, list);
1618                 entry = fi_hlp->addr_list.next;
1619                 while (entry != &(fi_hlp->addr_list)) {
1620                         arm_addr = list_entry(entry, struct arm_addr, addr_list);
1621                         if ( (arm_addr->start == addr->start) && 
1622                                 (arm_addr->end == addr->end)) {
1623                                 DBGMSG("same host ownes same "
1624                                         "addressrange -> EALREADY");
1625                                 same_host = 1;
1626                                 break;
1627                         }
1628                         entry = entry->next;
1629                 }
1630                 if (same_host) {
1631                         break;
1632                 }
1633         }
1634         if (same_host) {
1635                 /* addressrange occupied by same host */
1636                 kfree(addr->addr_space_buffer);
1637                 kfree(addr);
1638                 spin_unlock_irqrestore(&host_info_lock, flags);
1639                 return (-EALREADY);
1640         }
1641         /* another host with valid address-entry containing same addressrange */
1642         list_for_each(lh_1, &host_info_list) {
1643                 hi = list_entry(lh_1, struct host_info, list);
1644                 if (hi->host != fi->host) {
1645                         list_for_each(lh_2, &hi->file_info_list) {
1646                                 fi_hlp = list_entry(lh_2, struct file_info, list);
1647                                 entry = fi_hlp->addr_list.next;
1648                                 while (entry != &(fi_hlp->addr_list)) {
1649                                         arm_addr = list_entry(entry, struct arm_addr, addr_list);
1650                                         if ( (arm_addr->start == addr->start) && 
1651                                                 (arm_addr->end == addr->end)) {
1652                                                 DBGMSG("another host ownes same "
1653                                                 "addressrange");
1654                                                 another_host = 1;
1655                                                 break;
1656                                         }
1657                                         entry = entry->next;
1658                                 }
1659                                 if (another_host) {
1660                                         break;
1661                                 }
1662                         }
1663                 }
1664         }
1665         if (another_host) {
1666                 DBGMSG("another hosts entry is valid -> SUCCESS");
1667                 if (copy_to_user(int2ptr(req->req.recvb),
1668                         int2ptr(&addr->start),sizeof(u64))) {
1669                         printk(KERN_ERR "raw1394: arm_register failed "
1670                               " address-range-entry is invalid -> EFAULT !!!\n");
1671                         kfree(addr->addr_space_buffer);
1672                         kfree(addr);
1673                         spin_unlock_irqrestore(&host_info_lock, flags);
1674                         return (-EFAULT);
1675                 }
1676                 free_pending_request(req); /* immediate success or fail */
1677                 /* INSERT ENTRY */
1678                 list_add_tail(&addr->addr_list, &fi->addr_list);
1679                 spin_unlock_irqrestore(&host_info_lock, flags);
1680                 return sizeof(struct raw1394_request);
1681         }
1682         retval = hpsb_register_addrspace(&raw1394_highlevel, &arm_ops, req->req.address,
1683                 req->req.address + req->req.length);
1684         if (retval) {
1685                /* INSERT ENTRY */
1686                list_add_tail(&addr->addr_list, &fi->addr_list);
1687         } else {
1688                 DBGMSG("arm_register failed errno: %d \n",retval);
1689                 kfree(addr->addr_space_buffer);
1690                 kfree(addr);
1691                 spin_unlock_irqrestore(&host_info_lock, flags);
1692                 return (-EALREADY); 
1693         }
1694         spin_unlock_irqrestore(&host_info_lock, flags);
1695         free_pending_request(req); /* immediate success or fail */
1696         return sizeof(struct raw1394_request);
1697 }
1698
1699 static int arm_unregister(struct file_info *fi, struct pending_request *req)
1700 {
1701         int found  = 0;
1702         int retval = 0;
1703         struct list_head *entry;
1704         struct arm_addr  *addr = NULL;
1705         struct list_head *lh_1, *lh_2;
1706         struct host_info *hi;
1707         struct file_info *fi_hlp = NULL;
1708         struct arm_addr  *arm_addr = NULL;
1709         int another_host;
1710         unsigned long flags;
1711
1712         DBGMSG("arm_Unregister called addr(Offset): "
1713               "%8.8x %8.8x",
1714               (u32) ((req->req.address >>32) & 0xFFFF),
1715               (u32) (req->req.address & 0xFFFFFFFF));
1716         spin_lock_irqsave(&host_info_lock, flags);
1717         /* get addr */
1718         entry = fi->addr_list.next;
1719         while (entry != &(fi->addr_list)) {
1720                 addr = list_entry(entry, struct arm_addr, addr_list);
1721                 if (addr->start == req->req.address) {
1722                         found = 1;
1723                         break;
1724                 }
1725                 entry = entry->next;
1726         }
1727         if (!found) {
1728                 DBGMSG("arm_Unregister addr not found");
1729                 spin_unlock_irqrestore(&host_info_lock, flags);
1730                 return (-EINVAL);
1731         }
1732         DBGMSG("arm_Unregister addr found");
1733         another_host = 0;
1734         /* another host with valid address-entry containing 
1735            same addressrange */
1736         list_for_each(lh_1, &host_info_list) {
1737                 hi = list_entry(lh_1, struct host_info, list);
1738                 if (hi->host != fi->host) {
1739                         list_for_each(lh_2, &hi->file_info_list) {
1740                                 fi_hlp = list_entry(lh_2, struct file_info, list);
1741                                 entry = fi_hlp->addr_list.next;
1742                                 while (entry != &(fi_hlp->addr_list)) {
1743                                         arm_addr = list_entry(entry, 
1744                                                 struct arm_addr, addr_list);
1745                                         if (arm_addr->start == 
1746                                                 addr->start) {
1747                                                 DBGMSG("another host ownes "
1748                                                 "same addressrange");
1749                                                 another_host = 1;
1750                                                 break;
1751                                         }
1752                                         entry = entry->next;
1753                                 }
1754                                 if (another_host) {
1755                                         break;
1756                                 }
1757                         }
1758                 }
1759         }
1760         if (another_host) {
1761                 DBGMSG("delete entry from list -> success");
1762                 list_del(&addr->addr_list);
1763                 kfree(addr->addr_space_buffer);
1764                 kfree(addr);
1765                 free_pending_request(req); /* immediate success or fail */
1766                 spin_unlock_irqrestore(&host_info_lock, flags);
1767                 return sizeof(struct raw1394_request);
1768         } 
1769         retval = hpsb_unregister_addrspace(&raw1394_highlevel, addr->start);
1770         if (!retval) {
1771                 printk(KERN_ERR "raw1394: arm_Unregister failed -> EINVAL\n");
1772                 spin_unlock_irqrestore(&host_info_lock, flags);
1773                 return (-EINVAL);
1774         }
1775         DBGMSG("delete entry from list -> success");
1776         list_del(&addr->addr_list);
1777         spin_unlock_irqrestore(&host_info_lock, flags);
1778         kfree(addr->addr_space_buffer);
1779         kfree(addr);
1780         free_pending_request(req); /* immediate success or fail */
1781         return sizeof(struct raw1394_request);
1782 }
1783
1784 static int reset_notification(struct file_info *fi, struct pending_request *req)
1785 {
1786         DBGMSG("reset_notification called - switch %s ",
1787                 (req->req.misc == RAW1394_NOTIFY_OFF)?"OFF":"ON");
1788         if ((req->req.misc == RAW1394_NOTIFY_OFF) ||
1789                 (req->req.misc == RAW1394_NOTIFY_ON)) {
1790                 fi->notification=(u8)req->req.misc;
1791                 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1792                 return sizeof(struct raw1394_request);
1793         } 
1794         /* error EINVAL (22) invalid argument */
1795         return (-EINVAL);
1796 }
1797
1798 static int write_phypacket(struct file_info *fi, struct pending_request *req)
1799 {
1800         struct hpsb_packet *packet = NULL;
1801         int retval=0;
1802         quadlet_t data;
1803
1804         data = be32_to_cpu((u32)req->req.sendb);
1805         DBGMSG("write_phypacket called - quadlet 0x%8.8x ",data);
1806         packet = hpsb_make_phypacket (fi->host, data);
1807         if (!packet) return -ENOMEM;
1808         req->req.length=0;
1809         req->packet=packet;
1810         hpsb_set_packet_complete_task(packet, (void(*)(void*))queue_complete_cb, req);
1811         spin_lock_irq(&fi->reqlists_lock);
1812         list_add_tail(&req->list, &fi->req_pending);
1813         spin_unlock_irq(&fi->reqlists_lock);
1814         packet->generation = req->req.generation;
1815         retval = hpsb_send_packet(packet);
1816         DBGMSG("write_phypacket send_packet called => retval: %d ",
1817                 retval);
1818         if (! retval) {
1819                 req->req.error = RAW1394_ERROR_SEND_ERROR;
1820                 req->req.length = 0;
1821                 queue_complete_req(req);
1822         }
1823         return sizeof(struct raw1394_request);
1824 }
1825
1826 static int get_config_rom(struct file_info *fi, struct pending_request *req)
1827 {
1828         size_t return_size;
1829         unsigned char rom_version;
1830         int ret=sizeof(struct raw1394_request);
1831         quadlet_t *data = kmalloc(req->req.length, SLAB_KERNEL);
1832         int status;
1833         if (!data) return -ENOMEM;
1834         status = hpsb_get_config_rom(fi->host, data, 
1835                 req->req.length, &return_size, &rom_version);
1836         if (copy_to_user(int2ptr(req->req.recvb), data, 
1837                 req->req.length))
1838                 ret = -EFAULT;
1839         if (copy_to_user(int2ptr(req->req.tag), &return_size, 
1840                 sizeof(return_size)))
1841                 ret = -EFAULT;
1842         if (copy_to_user(int2ptr(req->req.address), &rom_version, 
1843                 sizeof(rom_version)))
1844                 ret = -EFAULT;
1845         if (copy_to_user(int2ptr(req->req.sendb), &status, 
1846                 sizeof(status)))
1847                 ret = -EFAULT;
1848         kfree(data);
1849         if (ret >= 0) {
1850                 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1851         }
1852         return ret;
1853 }
1854
1855 static int update_config_rom(struct file_info *fi, struct pending_request *req)
1856 {
1857         int ret=sizeof(struct raw1394_request);
1858         quadlet_t *data = kmalloc(req->req.length, SLAB_KERNEL);
1859         if (!data) return -ENOMEM;
1860         if (copy_from_user(data,int2ptr(req->req.sendb), 
1861                 req->req.length)) {
1862                 ret= -EFAULT;
1863         } else {
1864                 int status = hpsb_update_config_rom(fi->host, 
1865                         data, req->req.length, 
1866                         (unsigned char) req->req.misc);
1867                 if (copy_to_user(int2ptr(req->req.recvb), 
1868                         &status, sizeof(status)))
1869                         ret = -ENOMEM;
1870         }
1871         kfree(data);
1872         if (ret >= 0) {
1873                 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1874         }
1875         return ret;
1876 }
1877
1878 static int state_connected(struct file_info *fi, struct pending_request *req)
1879 {
1880         int node = req->req.address >> 48;
1881
1882         req->req.error = RAW1394_ERROR_NONE;
1883
1884         switch (req->req.type) {
1885
1886         case RAW1394_REQ_ECHO:
1887                 queue_complete_req(req);
1888                 return sizeof(struct raw1394_request);
1889
1890         case RAW1394_REQ_ISO_SEND:
1891                 return handle_iso_send(fi, req, node);
1892
1893         case RAW1394_REQ_ARM_REGISTER:
1894                 return arm_register(fi, req);
1895
1896         case RAW1394_REQ_ARM_UNREGISTER:
1897                 return arm_unregister(fi, req);
1898
1899         case RAW1394_REQ_RESET_NOTIFY:
1900                 return reset_notification(fi, req);
1901
1902         case RAW1394_REQ_ISO_LISTEN:
1903                 handle_iso_listen(fi, req);
1904                 return sizeof(struct raw1394_request);
1905
1906         case RAW1394_REQ_FCP_LISTEN:
1907                 handle_fcp_listen(fi, req);
1908                 return sizeof(struct raw1394_request);
1909
1910         case RAW1394_REQ_RESET_BUS:
1911                 if (req->req.misc == RAW1394_LONG_RESET) {
1912                         DBGMSG("busreset called (type: LONG)");
1913                         hpsb_reset_bus(fi->host, LONG_RESET);
1914                         free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1915                         return sizeof(struct raw1394_request);
1916                 }
1917                 if (req->req.misc == RAW1394_SHORT_RESET) {
1918                         DBGMSG("busreset called (type: SHORT)");
1919                         hpsb_reset_bus(fi->host, SHORT_RESET);
1920                         free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1921                         return sizeof(struct raw1394_request);
1922                 }
1923                 /* error EINVAL (22) invalid argument */
1924                 return (-EINVAL);
1925         case RAW1394_REQ_GET_ROM:
1926                 return get_config_rom(fi, req);
1927
1928         case RAW1394_REQ_UPDATE_ROM:
1929                 return update_config_rom(fi, req);
1930         }
1931
1932         if (req->req.generation != get_hpsb_generation(fi->host)) {
1933                 req->req.error = RAW1394_ERROR_GENERATION;
1934                 req->req.generation = get_hpsb_generation(fi->host);
1935                 req->req.length = 0;
1936                 queue_complete_req(req);
1937                 return sizeof(struct raw1394_request);
1938         }
1939
1940         switch (req->req.type) {
1941         case RAW1394_REQ_PHYPACKET:
1942                 return write_phypacket(fi, req);
1943         case RAW1394_REQ_ASYNC_SEND:
1944                 return handle_async_send(fi, req);
1945         }
1946
1947         if (req->req.length == 0) {
1948                 req->req.error = RAW1394_ERROR_INVALID_ARG;
1949                 queue_complete_req(req);
1950                 return sizeof(struct raw1394_request);
1951         }
1952
1953         return handle_async_request(fi, req, node);
1954 }
1955
1956
1957 static ssize_t raw1394_write(struct file *file, const char *buffer, size_t count,
1958                      loff_t *offset_is_ignored)
1959 {
1960         struct file_info *fi = (struct file_info *)file->private_data;
1961         struct pending_request *req;
1962         ssize_t retval = 0;
1963
1964         if (count != sizeof(struct raw1394_request)) {
1965                 return -EINVAL;
1966         }
1967
1968         req = alloc_pending_request();
1969         if (req == NULL) {
1970                 return -ENOMEM;
1971         }
1972         req->file_info = fi;
1973
1974         if (copy_from_user(&req->req, buffer, sizeof(struct raw1394_request))) {
1975                 free_pending_request(req);
1976                 return -EFAULT;
1977         }
1978
1979         switch (fi->state) {
1980         case opened:
1981                 retval = state_opened(fi, req);
1982                 break;
1983
1984         case initialized:
1985                 retval = state_initialized(fi, req);
1986                 break;
1987
1988         case connected:
1989                 retval = state_connected(fi, req);
1990                 break;
1991         }
1992
1993         if (retval < 0) {
1994                 free_pending_request(req);
1995         }
1996
1997         return retval;
1998 }
1999
2000 /* rawiso operations */
2001
2002 /* check if any RAW1394_REQ_RAWISO_ACTIVITY event is already in the
2003  * completion queue (reqlists_lock must be taken) */
2004 static inline int __rawiso_event_in_queue(struct file_info *fi)
2005 {
2006         struct list_head *lh;
2007         struct pending_request *req;
2008
2009         list_for_each(lh, &fi->req_complete) {
2010                 req = list_entry(lh, struct pending_request, list);
2011                 if (req->req.type == RAW1394_REQ_RAWISO_ACTIVITY) {
2012                         return 1;
2013                 }
2014         }
2015
2016         return 0;
2017 }
2018
2019 /* put a RAWISO_ACTIVITY event in the queue, if one isn't there already */
2020 static void queue_rawiso_event(struct file_info *fi)
2021 {
2022         unsigned long flags;
2023
2024         spin_lock_irqsave(&fi->reqlists_lock, flags);
2025
2026         /* only one ISO activity event may be in the queue */
2027         if (!__rawiso_event_in_queue(fi)) {
2028                 struct pending_request *req = __alloc_pending_request(SLAB_ATOMIC);
2029
2030                 if (req) {
2031                         req->file_info = fi;
2032                         req->req.type = RAW1394_REQ_RAWISO_ACTIVITY;
2033                         req->req.generation = get_hpsb_generation(fi->host);
2034                         __queue_complete_req(req);
2035                 } else {
2036                         /* on allocation failure, signal an overflow */
2037                         if (fi->iso_handle) {
2038                                 atomic_inc(&fi->iso_handle->overflows);
2039                         }
2040                 }
2041         }
2042         spin_unlock_irqrestore(&fi->reqlists_lock, flags);
2043 }
2044
2045 static void rawiso_activity_cb(struct hpsb_iso *iso)
2046 {
2047         unsigned long flags;
2048         struct list_head *lh;
2049         struct host_info *hi;
2050
2051         spin_lock_irqsave(&host_info_lock, flags);
2052         hi = find_host_info(iso->host);
2053
2054         if (hi != NULL) {
2055                 list_for_each(lh, &hi->file_info_list) {
2056                         struct file_info *fi = list_entry(lh, struct file_info, list);
2057                         if (fi->iso_handle == iso)
2058                                 queue_rawiso_event(fi);
2059                 }
2060         }
2061
2062         spin_unlock_irqrestore(&host_info_lock, flags);
2063 }
2064
2065 /* helper function - gather all the kernel iso status bits for returning to user-space */
2066 static void raw1394_iso_fill_status(struct hpsb_iso *iso, struct raw1394_iso_status *stat)
2067 {
2068         stat->config.data_buf_size = iso->buf_size;
2069         stat->config.buf_packets = iso->buf_packets;
2070         stat->config.channel = iso->channel;
2071         stat->config.speed = iso->speed;
2072         stat->config.irq_interval = iso->irq_interval;
2073         stat->n_packets = hpsb_iso_n_ready(iso);
2074         stat->overflows = atomic_read(&iso->overflows);
2075         stat->xmit_cycle = iso->xmit_cycle;
2076 }
2077
2078 static int raw1394_iso_xmit_init(struct file_info *fi, void *uaddr)
2079 {
2080         struct raw1394_iso_status stat;
2081
2082         if (!fi->host)
2083                 return -EINVAL;
2084
2085         if (copy_from_user(&stat, uaddr, sizeof(stat)))
2086                 return -EFAULT;
2087
2088         fi->iso_handle = hpsb_iso_xmit_init(fi->host,
2089                                             stat.config.data_buf_size,
2090                                             stat.config.buf_packets,
2091                                             stat.config.channel,
2092                                             stat.config.speed,
2093                                             stat.config.irq_interval,
2094                                             rawiso_activity_cb);
2095         if (!fi->iso_handle)
2096                 return -ENOMEM;
2097
2098         fi->iso_state = RAW1394_ISO_XMIT;
2099
2100         raw1394_iso_fill_status(fi->iso_handle, &stat);
2101         if (copy_to_user(uaddr, &stat, sizeof(stat)))
2102                 return -EFAULT;
2103
2104         /* queue an event to get things started */
2105         rawiso_activity_cb(fi->iso_handle);
2106
2107         return 0;
2108 }
2109
2110 static int raw1394_iso_recv_init(struct file_info *fi, void *uaddr)
2111 {
2112         struct raw1394_iso_status stat;
2113
2114         if (!fi->host)
2115                 return -EINVAL;
2116
2117         if (copy_from_user(&stat, uaddr, sizeof(stat)))
2118                 return -EFAULT;
2119
2120         fi->iso_handle = hpsb_iso_recv_init(fi->host,
2121                                             stat.config.data_buf_size,
2122                                             stat.config.buf_packets,
2123                                             stat.config.channel,
2124                                             stat.config.irq_interval,
2125                                             rawiso_activity_cb);
2126         if (!fi->iso_handle)
2127                 return -ENOMEM;
2128
2129         fi->iso_state = RAW1394_ISO_RECV;
2130
2131         raw1394_iso_fill_status(fi->iso_handle, &stat);
2132         if (copy_to_user(uaddr, &stat, sizeof(stat)))
2133                 return -EFAULT;
2134         return 0;
2135 }
2136
2137 static int raw1394_iso_get_status(struct file_info *fi, void *uaddr)
2138 {
2139         struct raw1394_iso_status stat;
2140         struct hpsb_iso *iso = fi->iso_handle;
2141
2142         raw1394_iso_fill_status(fi->iso_handle, &stat);
2143         if (copy_to_user(uaddr, &stat, sizeof(stat)))
2144                 return -EFAULT;
2145
2146         /* reset overflow counter */
2147         atomic_set(&iso->overflows, 0);
2148
2149         return 0;
2150 }
2151
2152 /* copy N packet_infos out of the ringbuffer into user-supplied array */
2153 static int raw1394_iso_recv_packets(struct file_info *fi, void *uaddr)
2154 {
2155         struct raw1394_iso_packets upackets;
2156         unsigned int packet = fi->iso_handle->first_packet;
2157         int i;
2158
2159         if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
2160                 return -EFAULT;
2161
2162         if (upackets.n_packets > hpsb_iso_n_ready(fi->iso_handle))
2163                 return -EINVAL;
2164
2165         /* ensure user-supplied buffer is accessible and big enough */
2166         if (verify_area(VERIFY_WRITE, upackets.infos,
2167                        upackets.n_packets * sizeof(struct raw1394_iso_packet_info)))
2168                 return -EFAULT;
2169
2170         /* copy the packet_infos out */
2171         for (i = 0; i < upackets.n_packets; i++) {
2172                 if (__copy_to_user(&upackets.infos[i],
2173                                   &fi->iso_handle->infos[packet],
2174                                   sizeof(struct raw1394_iso_packet_info)))
2175                         return -EFAULT;
2176                 
2177                 packet = (packet + 1) % fi->iso_handle->buf_packets;
2178         }
2179
2180         return 0;
2181 }
2182
2183 /* copy N packet_infos from user to ringbuffer, and queue them for transmission */
2184 static int raw1394_iso_send_packets(struct file_info *fi, void *uaddr)
2185 {
2186         struct raw1394_iso_packets upackets;
2187         int i, rv;
2188
2189         if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
2190                 return -EFAULT;
2191
2192         if (upackets.n_packets > hpsb_iso_n_ready(fi->iso_handle))
2193                 return -EINVAL;
2194
2195         /* ensure user-supplied buffer is accessible and big enough */
2196         if (verify_area(VERIFY_READ, upackets.infos,
2197                        upackets.n_packets * sizeof(struct raw1394_iso_packet_info)))
2198                 return -EFAULT;
2199
2200         /* copy the infos structs in and queue the packets */
2201         for (i = 0; i < upackets.n_packets; i++) {
2202                 struct raw1394_iso_packet_info info;
2203
2204                 if (__copy_from_user(&info, &upackets.infos[i],
2205                                     sizeof(struct raw1394_iso_packet_info)))
2206                         return -EFAULT;
2207
2208                 rv = hpsb_iso_xmit_queue_packet(fi->iso_handle, info.offset,
2209                                                 info.len, info.tag, info.sy);
2210                 if (rv)
2211                         return rv;
2212         }
2213
2214         return 0;
2215 }
2216
2217 static void raw1394_iso_shutdown(struct file_info *fi)
2218 {
2219         if (fi->iso_handle)
2220                 hpsb_iso_shutdown(fi->iso_handle);
2221
2222         fi->iso_handle = NULL;
2223         fi->iso_state = RAW1394_ISO_INACTIVE;
2224 }
2225
2226 /* mmap the rawiso xmit/recv buffer */
2227 static int raw1394_mmap(struct file *file, struct vm_area_struct *vma)
2228 {
2229         struct file_info *fi = file->private_data;
2230
2231         if (fi->iso_state == RAW1394_ISO_INACTIVE)
2232                 return -EINVAL;
2233
2234         return dma_region_mmap(&fi->iso_handle->data_buf, file, vma);
2235 }
2236
2237 /* ioctl is only used for rawiso operations */
2238 static int raw1394_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
2239 {
2240         struct file_info *fi = file->private_data;
2241
2242         switch(fi->iso_state) {
2243         case RAW1394_ISO_INACTIVE:
2244                 switch(cmd) {
2245                 case RAW1394_IOC_ISO_XMIT_INIT:
2246                         return raw1394_iso_xmit_init(fi, (void*) arg);
2247                 case RAW1394_IOC_ISO_RECV_INIT:
2248                         return raw1394_iso_recv_init(fi, (void*) arg);
2249                 default:
2250                         break;
2251                 }
2252                 break;
2253         case RAW1394_ISO_RECV:
2254                 switch(cmd) {
2255                 case RAW1394_IOC_ISO_RECV_START: {
2256                         /* copy args from user-space */
2257                         int args[3];
2258                         if (copy_from_user(&args[0], (void*) arg, sizeof(args)))
2259                                 return -EFAULT;
2260                         return hpsb_iso_recv_start(fi->iso_handle, args[0], args[1], args[2]);
2261                 }
2262                 case RAW1394_IOC_ISO_XMIT_RECV_STOP:
2263                         hpsb_iso_stop(fi->iso_handle);
2264                         return 0;
2265                 case RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL:
2266                         return hpsb_iso_recv_listen_channel(fi->iso_handle, arg);
2267                 case RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL:
2268                         return hpsb_iso_recv_unlisten_channel(fi->iso_handle, arg);
2269                 case RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK: {
2270                         /* copy the u64 from user-space */
2271                         u64 mask;
2272                         if (copy_from_user(&mask, (void*) arg, sizeof(mask)))
2273                                 return -EFAULT;
2274                         return hpsb_iso_recv_set_channel_mask(fi->iso_handle, mask);
2275                 }
2276                 case RAW1394_IOC_ISO_GET_STATUS:
2277                         return raw1394_iso_get_status(fi, (void*) arg);
2278                 case RAW1394_IOC_ISO_RECV_PACKETS:
2279                         return raw1394_iso_recv_packets(fi, (void*) arg);
2280                 case RAW1394_IOC_ISO_RECV_RELEASE_PACKETS:
2281                         return hpsb_iso_recv_release_packets(fi->iso_handle, arg);
2282                 case RAW1394_IOC_ISO_RECV_FLUSH:
2283                         return hpsb_iso_recv_flush(fi->iso_handle);
2284                 case RAW1394_IOC_ISO_SHUTDOWN:
2285                         raw1394_iso_shutdown(fi);
2286                         return 0;
2287                 case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
2288                         queue_rawiso_event(fi);
2289                         return 0;
2290                 }
2291                 break;
2292         case RAW1394_ISO_XMIT:
2293                 switch(cmd) {
2294                 case RAW1394_IOC_ISO_XMIT_START: {
2295                         /* copy two ints from user-space */
2296                         int args[2];
2297                         if (copy_from_user(&args[0], (void*) arg, sizeof(args)))
2298                                 return -EFAULT;
2299                         return hpsb_iso_xmit_start(fi->iso_handle, args[0], args[1]);
2300                 }
2301                 case RAW1394_IOC_ISO_XMIT_SYNC:
2302                         return hpsb_iso_xmit_sync(fi->iso_handle);
2303                 case RAW1394_IOC_ISO_XMIT_RECV_STOP:
2304                         hpsb_iso_stop(fi->iso_handle);
2305                         return 0;
2306                 case RAW1394_IOC_ISO_GET_STATUS:
2307                         return raw1394_iso_get_status(fi, (void*) arg);
2308                 case RAW1394_IOC_ISO_XMIT_PACKETS:
2309                         return raw1394_iso_send_packets(fi, (void*) arg);
2310                 case RAW1394_IOC_ISO_SHUTDOWN:
2311                         raw1394_iso_shutdown(fi);
2312                         return 0;
2313                 case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
2314                         queue_rawiso_event(fi);
2315                         return 0;
2316                 }
2317                 break;
2318         default:
2319                 break;
2320         }
2321
2322         return -EINVAL;
2323 }
2324
2325 static unsigned int raw1394_poll(struct file *file, poll_table *pt)
2326 {
2327         struct file_info *fi = file->private_data;
2328         unsigned int mask = POLLOUT | POLLWRNORM;
2329
2330         poll_wait(file, &fi->poll_wait_complete, pt);
2331
2332         spin_lock_irq(&fi->reqlists_lock);
2333         if (!list_empty(&fi->req_complete)) {
2334                 mask |= POLLIN | POLLRDNORM;
2335         }
2336         spin_unlock_irq(&fi->reqlists_lock);
2337
2338         return mask;
2339 }
2340
2341 static int raw1394_open(struct inode *inode, struct file *file)
2342 {
2343         struct file_info *fi;
2344
2345         if (ieee1394_file_to_instance(file) > 0) {
2346                 return -ENXIO;
2347         }
2348
2349         fi = kmalloc(sizeof(struct file_info), SLAB_KERNEL);
2350         if (fi == NULL)
2351                 return -ENOMEM;
2352         
2353         memset(fi, 0, sizeof(struct file_info));
2354         fi->notification = (u8) RAW1394_NOTIFY_ON; /* busreset notification */
2355
2356         INIT_LIST_HEAD(&fi->list);
2357         fi->state = opened;
2358         INIT_LIST_HEAD(&fi->req_pending);
2359         INIT_LIST_HEAD(&fi->req_complete);
2360         sema_init(&fi->complete_sem, 0);
2361         spin_lock_init(&fi->reqlists_lock);
2362         init_waitqueue_head(&fi->poll_wait_complete);
2363         INIT_LIST_HEAD(&fi->addr_list);
2364
2365         file->private_data = fi;
2366
2367         return 0;
2368 }
2369
2370 static int raw1394_release(struct inode *inode, struct file *file)
2371 {
2372         struct file_info *fi = file->private_data;
2373         struct list_head *lh;
2374         struct pending_request *req;
2375         int done = 0, i, fail = 0;
2376         int retval = 0;
2377         struct list_head *entry;
2378         struct arm_addr  *addr = NULL;
2379         struct list_head *lh_1, *lh_2;
2380         struct host_info *hi;
2381         struct file_info *fi_hlp = NULL;
2382         struct arm_addr  *arm_addr = NULL;
2383         int another_host;
2384
2385         if (fi->iso_state != RAW1394_ISO_INACTIVE)
2386                 raw1394_iso_shutdown(fi);
2387
2388         for (i = 0; i < 64; i++) {
2389                 if (fi->listen_channels & (1ULL << i)) {
2390                         hpsb_unlisten_channel(&raw1394_highlevel, fi->host, i);
2391                 }
2392         }
2393
2394         spin_lock_irq(&host_info_lock);
2395         fi->listen_channels = 0;
2396         spin_unlock_irq(&host_info_lock);
2397
2398         fail = 0;
2399         /* set address-entries invalid */
2400         spin_lock_irq(&host_info_lock);
2401
2402         while (!list_empty(&fi->addr_list)) {
2403                 another_host = 0;
2404                 lh = fi->addr_list.next;
2405                 addr = list_entry(lh, struct arm_addr, addr_list);
2406                 /* another host with valid address-entry containing 
2407                    same addressrange? */
2408                 list_for_each(lh_1, &host_info_list) {
2409                         hi = list_entry(lh_1, struct host_info, list);
2410                         if (hi->host != fi->host) {
2411                                 list_for_each(lh_2, &hi->file_info_list) {
2412                                         fi_hlp = list_entry(lh_2, struct file_info, list);
2413                                         entry = fi_hlp->addr_list.next;
2414                                         while (entry != &(fi_hlp->addr_list)) {
2415                                                 arm_addr = list_entry(entry, 
2416                                                         struct arm_addr, addr_list);
2417                                                 if (arm_addr->start == 
2418                                                         addr->start) {
2419                                                         DBGMSG("raw1394_release: "
2420                                                         "another host ownes "
2421                                                         "same addressrange");
2422                                                         another_host = 1;
2423                                                         break;
2424                                                 }
2425                                                 entry = entry->next;
2426                                         }
2427                                         if (another_host) {
2428                                                 break;
2429                                         }
2430                                 }
2431                         }
2432                 }
2433                 if (!another_host) {
2434                         DBGMSG("raw1394_release: call hpsb_arm_unregister");
2435                         retval = hpsb_unregister_addrspace(&raw1394_highlevel, addr->start);
2436                         if (!retval) {
2437                                 ++fail;
2438                                 printk(KERN_ERR "raw1394_release arm_Unregister failed\n");
2439                         }
2440                 }
2441                 DBGMSG("raw1394_release: delete addr_entry from list");
2442                 list_del(&addr->addr_list);
2443                 kfree(addr->addr_space_buffer);
2444                 kfree(addr);
2445         } /* while */
2446         spin_unlock_irq(&host_info_lock);
2447         if (fail > 0) {
2448                 printk(KERN_ERR "raw1394: during addr_list-release "
2449                         "error(s) occurred \n");
2450         }
2451
2452         while (!done) {
2453                 spin_lock_irq(&fi->reqlists_lock);
2454
2455                 while (!list_empty(&fi->req_complete)) {
2456                         lh = fi->req_complete.next;
2457                         list_del(lh);
2458
2459                         req = list_entry(lh, struct pending_request, list);
2460
2461                         free_pending_request(req);
2462                 }
2463
2464                 if (list_empty(&fi->req_pending)) done = 1;
2465
2466                 spin_unlock_irq(&fi->reqlists_lock);
2467
2468                 if (!done) down_interruptible(&fi->complete_sem);
2469         }
2470
2471         if (fi->state == connected) {
2472                 spin_lock_irq(&host_info_lock);
2473                 list_del(&fi->list);
2474                 spin_unlock_irq(&host_info_lock);
2475
2476                 hpsb_unref_host(fi->host);
2477         }
2478
2479         kfree(fi);
2480
2481         return 0;
2482 }
2483
2484
2485 /*** HOTPLUG STUFF **********************************************************/
2486 /*
2487  * Export information about protocols/devices supported by this driver.
2488  */
2489 static struct ieee1394_device_id raw1394_id_table[] = {
2490         {
2491                 .match_flags    = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2492                 .specifier_id   = AVC_UNIT_SPEC_ID_ENTRY & 0xffffff,
2493                 .version        = AVC_SW_VERSION_ENTRY & 0xffffff
2494         },
2495         {
2496                 .match_flags    = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2497                 .specifier_id   = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
2498                 .version        = CAMERA_SW_VERSION_ENTRY & 0xffffff
2499         },
2500         { }
2501 };
2502
2503 MODULE_DEVICE_TABLE(ieee1394, raw1394_id_table);
2504
2505 static struct hpsb_protocol_driver raw1394_driver = {
2506         .name           = "raw1394 Driver",
2507         .id_table       = raw1394_id_table,
2508         .driver         = {
2509                 .name   = "raw1394",
2510                 .bus    = &ieee1394_bus_type,
2511         },
2512 };
2513
2514
2515 /******************************************************************************/
2516
2517
2518 static struct hpsb_highlevel raw1394_highlevel = {
2519         .name =         RAW1394_DEVICE_NAME,
2520         .add_host =    add_host,
2521         .remove_host = remove_host,
2522         .host_reset =  host_reset,
2523         .iso_receive = iso_receive,
2524         .fcp_request = fcp_request,
2525 };
2526
2527 static struct file_operations file_ops = {
2528         .owner =        THIS_MODULE,
2529         .read =         raw1394_read, 
2530         .write =        raw1394_write,
2531         .mmap =         raw1394_mmap,
2532         .ioctl =        raw1394_ioctl,
2533         .poll =         raw1394_poll, 
2534         .open =         raw1394_open, 
2535         .release =      raw1394_release, 
2536 };
2537
2538 static int __init init_raw1394(void)
2539 {
2540         hpsb_register_highlevel(&raw1394_highlevel);
2541
2542         devfs_mk_cdev(MKDEV(IEEE1394_MAJOR, IEEE1394_MINOR_BLOCK_RAW1394 * 16),
2543                         S_IFCHR | S_IRUSR | S_IWUSR, RAW1394_DEVICE_NAME);
2544
2545         if (ieee1394_register_chardev(IEEE1394_MINOR_BLOCK_RAW1394,
2546                                       THIS_MODULE, &file_ops)) {
2547                 HPSB_ERR("raw1394 failed to register minor device block");
2548                 devfs_remove(RAW1394_DEVICE_NAME);
2549                 hpsb_unregister_highlevel(&raw1394_highlevel);
2550                 return -EBUSY;
2551         }
2552
2553         printk(KERN_INFO "raw1394: /dev/%s device initialized\n", RAW1394_DEVICE_NAME);
2554
2555         hpsb_register_protocol(&raw1394_driver);
2556
2557         return 0;
2558 }
2559
2560 static void __exit cleanup_raw1394(void)
2561 {
2562         hpsb_unregister_protocol(&raw1394_driver);
2563         ieee1394_unregister_chardev(IEEE1394_MINOR_BLOCK_RAW1394);
2564         devfs_remove(RAW1394_DEVICE_NAME);
2565         hpsb_unregister_highlevel(&raw1394_highlevel);
2566 }
2567
2568 module_init(init_raw1394);
2569 module_exit(cleanup_raw1394);
2570 MODULE_LICENSE("GPL");