Blame view

drivers/ieee1394/raw1394.c 83 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
   * IEEE 1394 for Linux
   *
   * Raw interface to the bus
   *
   * Copyright (C) 1999, 2000 Andreas E. Bombe
   *               2001, 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
   *                     2002 Christian Toegel <christian.toegel@gmx.at>
   *
   * This code is licensed under the GPL.  See the file COPYING in the root
   * directory of the kernel sources for details.
   *
   *
   * Contributions:
   *
   * Manfred Weihs <weihs@ict.tuwien.ac.at>
   *        configuration ROM manipulation
   *        address range mapping
   *        adaptation for new (transparent) loopback mechanism
   *        sending of arbitrary async packets
   * Christian Toegel <christian.toegel@gmx.at>
   *        address range mapping
   *        lock64 request
   *        transmit physical packet
   *        busreset notification control (switch on/off)
   *        busreset with selection of type (short/long)
   *        request_reply
   */
  
  #include <linux/kernel.h>
  #include <linux/list.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
32
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
  #include <linux/string.h>
  #include <linux/slab.h>
  #include <linux/fs.h>
  #include <linux/poll.h>
  #include <linux/module.h>
10963ea1b   Stefan Richter   ieee1394: raw1394...
38
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
43
44
  #include <linux/interrupt.h>
  #include <linux/vmalloc.h>
  #include <linux/cdev.h>
  #include <asm/uaccess.h>
  #include <asm/atomic.h>
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
45
  #include <linux/compat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
  
  #include "csr1212.h"
de4394f13   Stefan Richter   [PATCH] ieee1394:...
48
49
  #include "highlevel.h"
  #include "hosts.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  #include "ieee1394.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  #include "ieee1394_core.h"
de4394f13   Stefan Richter   [PATCH] ieee1394:...
52
  #include "ieee1394_hotplug.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  #include "ieee1394_transactions.h"
de4394f13   Stefan Richter   [PATCH] ieee1394:...
54
55
56
  #include "ieee1394_types.h"
  #include "iso.h"
  #include "nodemgr.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include "raw1394.h"
  #include "raw1394-private.h"
  
  #define int2ptr(x) ((void __user *)(unsigned long)x)
  #define ptr2int(x) ((u64)(unsigned long)(void __user *)x)
  
  #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  #define RAW1394_DEBUG
  #endif
  
  #ifdef RAW1394_DEBUG
  #define DBGMSG(fmt, args...) \
  printk(KERN_INFO "raw1394:" fmt "
  " , ## args)
  #else
611aa19fd   Stefan Richter   ieee1394: safer d...
72
  #define DBGMSG(fmt, args...) do {} while (0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #endif
  
  static LIST_HEAD(host_info_list);
  static int host_count;
  static DEFINE_SPINLOCK(host_info_lock);
  static atomic_t internal_generation = ATOMIC_INIT(0);
  
  static atomic_t iso_buffer_size;
  static const int iso_buffer_max = 4 * 1024 * 1024;	/* 4 MB */
  
  static struct hpsb_highlevel raw1394_highlevel;
  
  static int arm_read(struct hpsb_host *host, int nodeid, quadlet_t * buffer,
  		    u64 addr, size_t length, u16 flags);
  static int arm_write(struct hpsb_host *host, int nodeid, int destid,
  		     quadlet_t * data, u64 addr, size_t length, u16 flags);
  static int arm_lock(struct hpsb_host *host, int nodeid, quadlet_t * store,
  		    u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
  		    u16 flags);
  static int arm_lock64(struct hpsb_host *host, int nodeid, octlet_t * store,
  		      u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
  		      u16 flags);
1c4fb577a   Tobias Klauser   ieee1394: Storage...
95
  static const struct hpsb_address_ops arm_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
  	.read = arm_read,
  	.write = arm_write,
  	.lock = arm_lock,
  	.lock64 = arm_lock64,
  };
  
  static void queue_complete_cb(struct pending_request *req);
dd0fc66fb   Al Viro   [PATCH] gfp flags...
103
  static struct pending_request *__alloc_pending_request(gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
  {
  	struct pending_request *req;
8551158ab   Stefan Richter   kmalloc/kzalloc c...
106
107
  	req = kzalloc(sizeof(*req), flags);
  	if (req)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  		INIT_LIST_HEAD(&req->list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
  
  	return req;
  }
  
  static inline struct pending_request *alloc_pending_request(void)
  {
e94b17660   Christoph Lameter   [PATCH] slab: rem...
115
  	return __alloc_pending_request(GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  }
  
  static void free_pending_request(struct pending_request *req)
  {
  	if (req->ibs) {
  		if (atomic_dec_and_test(&req->ibs->refcount)) {
  			atomic_sub(req->ibs->data_size, &iso_buffer_size);
  			kfree(req->ibs);
  		}
  	} else if (req->free_data) {
  		kfree(req->data);
  	}
  	hpsb_free_packet(req->packet);
  	kfree(req);
  }
  
  /* fi->reqlists_lock must be taken */
  static void __queue_complete_req(struct pending_request *req)
  {
  	struct file_info *fi = req->file_info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136

45289bf6a   Stefan Richter   [PATCH] ieee1394:...
137
138
  	list_move_tail(&req->list, &fi->req_complete);
   	wake_up(&fi->wait_complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  }
  
  static void queue_complete_req(struct pending_request *req)
  {
  	unsigned long flags;
  	struct file_info *fi = req->file_info;
  
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
  	__queue_complete_req(req);
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
  }
  
  static void queue_complete_cb(struct pending_request *req)
  {
  	struct hpsb_packet *packet = req->packet;
  	int rcode = (packet->header[1] >> 12) & 0xf;
  
  	switch (packet->ack_code) {
  	case ACKX_NONE:
  	case ACKX_SEND_ERROR:
  		req->req.error = RAW1394_ERROR_SEND_ERROR;
  		break;
  	case ACKX_ABORTED:
  		req->req.error = RAW1394_ERROR_ABORTED;
  		break;
  	case ACKX_TIMEOUT:
  		req->req.error = RAW1394_ERROR_TIMEOUT;
  		break;
  	default:
  		req->req.error = (packet->ack_code << 16) | rcode;
  		break;
  	}
  
  	if (!((packet->ack_code == ACK_PENDING) && (rcode == RCODE_COMPLETE))) {
  		req->req.length = 0;
  	}
  
  	if ((req->req.type == RAW1394_REQ_ASYNC_READ) ||
  	    (req->req.type == RAW1394_REQ_ASYNC_WRITE) ||
  	    (req->req.type == RAW1394_REQ_ASYNC_STREAM) ||
  	    (req->req.type == RAW1394_REQ_LOCK) ||
  	    (req->req.type == RAW1394_REQ_LOCK64))
  		hpsb_free_tlabel(packet);
  
  	queue_complete_req(req);
  }
  
  static void add_host(struct hpsb_host *host)
  {
  	struct host_info *hi;
  	unsigned long flags;
8551158ab   Stefan Richter   kmalloc/kzalloc c...
190
  	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191

8551158ab   Stefan Richter   kmalloc/kzalloc c...
192
  	if (hi) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  		INIT_LIST_HEAD(&hi->list);
  		hi->host = host;
  		INIT_LIST_HEAD(&hi->file_info_list);
  
  		spin_lock_irqsave(&host_info_lock, flags);
  		list_add_tail(&hi->list, &host_info_list);
  		host_count++;
  		spin_unlock_irqrestore(&host_info_lock, flags);
  	}
  
  	atomic_inc(&internal_generation);
  }
  
  static struct host_info *find_host_info(struct hpsb_host *host)
  {
  	struct host_info *hi;
  
  	list_for_each_entry(hi, &host_info_list, list)
  	    if (hi->host == host)
  		return hi;
  
  	return NULL;
  }
  
  static void remove_host(struct hpsb_host *host)
  {
  	struct host_info *hi;
  	unsigned long flags;
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	hi = find_host_info(host);
  
  	if (hi != NULL) {
  		list_del(&hi->list);
  		host_count--;
  		/*
  		   FIXME: address ranges should be removed
  		   and fileinfo states should be initialized
  		   (including setting generation to
  		   internal-generation ...)
  		 */
  	}
  	spin_unlock_irqrestore(&host_info_lock, flags);
  
  	if (hi == NULL) {
  		printk(KERN_ERR "raw1394: attempt to remove unknown host "
  		       "0x%p
  ", host);
  		return;
  	}
  
  	kfree(hi);
  
  	atomic_inc(&internal_generation);
  }
  
  static void host_reset(struct hpsb_host *host)
  {
  	unsigned long flags;
  	struct host_info *hi;
  	struct file_info *fi;
  	struct pending_request *req;
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	hi = find_host_info(host);
  
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			if (fi->notification == RAW1394_NOTIFY_ON) {
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
262
  				req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
  
  				if (req != NULL) {
  					req->file_info = fi;
  					req->req.type = RAW1394_REQ_BUS_RESET;
  					req->req.generation =
  					    get_hpsb_generation(host);
  					req->req.misc = (host->node_id << 16)
  					    | host->node_count;
  					if (fi->protocol_version > 3) {
  						req->req.misc |=
  						    (NODEID_TO_NODE
  						     (host->irm_id)
  						     << 8);
  					}
  
  					queue_complete_req(req);
  				}
  			}
  		}
  	}
  	spin_unlock_irqrestore(&host_info_lock, flags);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
  			int cts, u8 * data, size_t length)
  {
  	unsigned long flags;
  	struct host_info *hi;
  	struct file_info *fi;
  	struct pending_request *req, *req_next;
  	struct iso_block_store *ibs = NULL;
  	LIST_HEAD(reqs);
  
  	if ((atomic_read(&iso_buffer_size) + length) > iso_buffer_max) {
  		HPSB_INFO("dropped fcp request");
  		return;
  	}
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	hi = find_host_info(host);
  
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			if (!fi->fcp_buffer)
  				continue;
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
307
  			req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
308
309
310
311
  			if (!req)
  				break;
  
  			if (!ibs) {
8551158ab   Stefan Richter   kmalloc/kzalloc c...
312
  				ibs = kmalloc(sizeof(*ibs) + length,
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
313
  					      GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  				if (!ibs) {
  					kfree(req);
  					break;
  				}
  
  				atomic_add(length, &iso_buffer_size);
  				atomic_set(&ibs->refcount, 0);
  				ibs->data_size = length;
  				memcpy(ibs->data, data, length);
  			}
  
  			atomic_inc(&ibs->refcount);
  
  			req->file_info = fi;
  			req->ibs = ibs;
  			req->data = ibs->data;
  			req->req.type = RAW1394_REQ_FCP_REQUEST;
  			req->req.generation = get_hpsb_generation(host);
  			req->req.misc = nodeid | (direction << 16);
  			req->req.recvb = ptr2int(fi->fcp_buffer);
  			req->req.length = length;
  
  			list_add_tail(&req->list, &reqs);
  		}
  	}
  	spin_unlock_irqrestore(&host_info_lock, flags);
  
  	list_for_each_entry_safe(req, req_next, &reqs, list)
  	    queue_complete_req(req);
  }
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
344
345
  #ifdef CONFIG_COMPAT
  struct compat_raw1394_req {
7597028a8   Ben Collins   raw1394: fix whit...
346
347
348
  	__u32 type;
  	__s32 error;
  	__u32 misc;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
349

7597028a8   Ben Collins   raw1394: fix whit...
350
351
  	__u32 generation;
  	__u32 length;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
352

7597028a8   Ben Collins   raw1394: fix whit...
353
  	__u64 address;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
354

7597028a8   Ben Collins   raw1394: fix whit...
355
  	__u64 tag;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
356

7597028a8   Ben Collins   raw1394: fix whit...
357
358
  	__u64 sendb;
  	__u64 recvb;
59337087c   Stefan Richter   ieee1394: raw1394...
359
360
361
362
363
  }
  #if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
  __attribute__((packed))
  #endif
  ;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
364
365
366
  
  static const char __user *raw1394_compat_write(const char __user *buf)
  {
7597028a8   Ben Collins   raw1394: fix whit...
367
  	struct compat_raw1394_req __user *cr = (typeof(cr)) buf;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
368
  	struct raw1394_request __user *r;
421696887   Stefan Richter   ieee1394: raw1394...
369

4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
370
371
372
373
374
  	r = compat_alloc_user_space(sizeof(struct raw1394_request));
  
  #define C(x) __copy_in_user(&r->x, &cr->x, sizeof(r->x))
  
  	if (copy_in_user(r, cr, sizeof(struct compat_raw1394_req)) ||
7597028a8   Ben Collins   raw1394: fix whit...
375
376
377
378
  	    C(address) ||
  	    C(tag) ||
  	    C(sendb) ||
  	    C(recvb))
421696887   Stefan Richter   ieee1394: raw1394...
379
  		return (__force const char __user *)ERR_PTR(-EFAULT);
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
380
381
382
383
384
  	return (const char __user *)r;
  }
  #undef C
  
  #define P(x) __put_user(r->x, &cr->x)
7597028a8   Ben Collins   raw1394: fix whit...
385
  static int
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
386
387
  raw1394_compat_read(const char __user *buf, struct raw1394_request *r)
  {
ee9be4259   Petr Vandrovec   ieee1394: raw1394...
388
  	struct compat_raw1394_req __user *cr = (typeof(cr)) buf;
421696887   Stefan Richter   ieee1394: raw1394...
389

7597028a8   Ben Collins   raw1394: fix whit...
390
  	if (!access_ok(VERIFY_WRITE, cr, sizeof(struct compat_raw1394_req)) ||
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
391
392
393
394
395
396
397
398
399
400
  	    P(type) ||
  	    P(error) ||
  	    P(misc) ||
  	    P(generation) ||
  	    P(length) ||
  	    P(address) ||
  	    P(tag) ||
  	    P(sendb) ||
  	    P(recvb))
  		return -EFAULT;
421696887   Stefan Richter   ieee1394: raw1394...
401

4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
402
403
404
405
406
  	return sizeof(struct compat_raw1394_req);
  }
  #undef P
  
  #endif
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
  /* get next completed request  (caller must hold fi->reqlists_lock) */
  static inline struct pending_request *__next_complete_req(struct file_info *fi)
  {
  	struct list_head *lh;
  	struct pending_request *req = NULL;
  
  	if (!list_empty(&fi->req_complete)) {
  		lh = fi->req_complete.next;
  		list_del(lh);
  		req = list_entry(lh, struct pending_request, list);
  	}
  	return req;
  }
  
  /* atomically get next completed request */
  static struct pending_request *next_complete_req(struct file_info *fi)
  {
  	unsigned long flags;
  	struct pending_request *req;
  
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
  	req = __next_complete_req(fi);
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
  	return req;
  }
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
432

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
  static ssize_t raw1394_read(struct file *file, char __user * buffer,
  			    size_t count, loff_t * offset_is_ignored)
  {
80792d182   Joe Perches   ieee1394: Remove ...
436
  	struct file_info *fi = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
  	struct pending_request *req;
  	ssize_t ret;
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
439
440
441
442
443
  #ifdef CONFIG_COMPAT
  	if (count == sizeof(struct compat_raw1394_req)) {
  		/* ok */
  	} else
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
446
447
448
449
450
451
452
  	if (count != sizeof(struct raw1394_request)) {
  		return -EINVAL;
  	}
  
  	if (!access_ok(VERIFY_WRITE, buffer, count)) {
  		return -EFAULT;
  	}
  
  	if (file->f_flags & O_NONBLOCK) {
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
453
  		if (!(req = next_complete_req(fi)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  			return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  	} else {
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
456
457
458
459
460
461
462
463
464
  		/*
  		 * NB: We call the macro wait_event_interruptible() with a
  		 * condition argument with side effect.  This is only possible
  		 * because the side effect does not occur until the condition
  		 * became true, and wait_event_interruptible() won't evaluate
  		 * the condition again after that.
  		 */
  		if (wait_event_interruptible(fi->wait_complete,
  					     (req = next_complete_req(fi))))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
465
  			return -ERESTARTSYS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
  	if (req->req.length) {
  		if (copy_to_user(int2ptr(req->req.recvb), req->data,
  				 req->req.length)) {
  			req->req.error = RAW1394_ERROR_MEMFAULT;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473

4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
474
  #ifdef CONFIG_COMPAT
7597028a8   Ben Collins   raw1394: fix whit...
475
476
477
  	if (count == sizeof(struct compat_raw1394_req) &&
     	    sizeof(struct compat_raw1394_req) !=
  			sizeof(struct raw1394_request)) {
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
478
  		ret = raw1394_compat_read(buffer, &req->req);
7597028a8   Ben Collins   raw1394: fix whit...
479
  	} else
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
480
481
482
483
484
  #endif
  	{
  		if (copy_to_user(buffer, &req->req, sizeof(req->req))) {
  			ret = -EFAULT;
  			goto out;
7597028a8   Ben Collins   raw1394: fix whit...
485
  		}
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
486
487
  		ret = (ssize_t) sizeof(struct raw1394_request);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        out:
  	free_pending_request(req);
  	return ret;
  }
  
  static int state_opened(struct file_info *fi, struct pending_request *req)
  {
  	if (req->req.type == RAW1394_REQ_INITIALIZE) {
  		switch (req->req.misc) {
  		case RAW1394_KERNELAPI_VERSION:
  		case 3:
  			fi->state = initialized;
  			fi->protocol_version = req->req.misc;
  			req->req.error = RAW1394_ERROR_NONE;
  			req->req.generation = atomic_read(&internal_generation);
  			break;
  
  		default:
  			req->req.error = RAW1394_ERROR_COMPAT;
  			req->req.misc = RAW1394_KERNELAPI_VERSION;
  		}
  	} else {
  		req->req.error = RAW1394_ERROR_STATE_ORDER;
  	}
  
  	req->req.length = 0;
  	queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
515
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
  }
  
  static int state_initialized(struct file_info *fi, struct pending_request *req)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
520
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
523
524
525
526
527
528
  	struct host_info *hi;
  	struct raw1394_khost_list *khl;
  
  	if (req->req.generation != atomic_read(&internal_generation)) {
  		req->req.error = RAW1394_ERROR_GENERATION;
  		req->req.generation = atomic_read(&internal_generation);
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
529
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
531
532
533
  	}
  
  	switch (req->req.type) {
  	case RAW1394_REQ_LIST_CARDS:
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
534
  		spin_lock_irqsave(&host_info_lock, flags);
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
535
  		khl = kmalloc(sizeof(*khl) * host_count, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
536

8551158ab   Stefan Richter   kmalloc/kzalloc c...
537
  		if (khl) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
  			req->req.misc = host_count;
  			req->data = (quadlet_t *) khl;
  
  			list_for_each_entry(hi, &host_info_list, list) {
  				khl->nodes = hi->host->node_count;
  				strcpy(khl->name, hi->host->driver->name);
  				khl++;
  			}
  		}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
547
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
548

8551158ab   Stefan Richter   kmalloc/kzalloc c...
549
  		if (khl) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
550
551
552
553
554
555
556
557
558
559
560
561
  			req->req.error = RAW1394_ERROR_NONE;
  			req->req.length = min(req->req.length,
  					      (u32) (sizeof
  						     (struct raw1394_khost_list)
  						     * req->req.misc));
  			req->free_data = 1;
  		} else {
  			return -ENOMEM;
  		}
  		break;
  
  	case RAW1394_REQ_SET_CARD:
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
562
  		spin_lock_irqsave(&host_info_lock, flags);
0fe4c6fca   Stefan Richter   ieee1394: raw1394...
563
  		if (req->req.misc >= host_count) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
  			req->req.error = RAW1394_ERROR_INVALID_ARG;
0fe4c6fca   Stefan Richter   ieee1394: raw1394...
565
  			goto out_set_card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  		}
0fe4c6fca   Stefan Richter   ieee1394: raw1394...
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  		list_for_each_entry(hi, &host_info_list, list)
  			if (!req->req.misc--)
  				break;
  		get_device(&hi->host->device); /* FIXME handle failure case */
  		list_add_tail(&fi->list, &hi->file_info_list);
  
  		/* prevent unloading of the host's low-level driver */
  		if (!try_module_get(hi->host->driver->owner)) {
  			req->req.error = RAW1394_ERROR_ABORTED;
  			goto out_set_card;
  		}
  		WARN_ON(fi->host);
  		fi->host = hi->host;
  		fi->state = connected;
  
  		req->req.error = RAW1394_ERROR_NONE;
  		req->req.generation = get_hpsb_generation(fi->host);
  		req->req.misc = (fi->host->node_id << 16)
  				| fi->host->node_count;
  		if (fi->protocol_version > 3)
  			req->req.misc |= NODEID_TO_NODE(fi->host->irm_id) << 8;
  out_set_card:
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
589
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
592
593
594
595
596
597
598
599
600
  
  		req->req.length = 0;
  		break;
  
  	default:
  		req->req.error = RAW1394_ERROR_STATE_ORDER;
  		req->req.length = 0;
  		break;
  	}
  
  	queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
601
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
602
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  static void handle_fcp_listen(struct file_info *fi, struct pending_request *req)
  {
  	if (req->req.misc) {
  		if (fi->fcp_buffer) {
  			req->req.error = RAW1394_ERROR_ALREADY;
  		} else {
  			fi->fcp_buffer = int2ptr(req->req.recvb);
  		}
  	} else {
  		if (!fi->fcp_buffer) {
  			req->req.error = RAW1394_ERROR_ALREADY;
  		} else {
  			fi->fcp_buffer = NULL;
  		}
  	}
  
  	req->req.length = 0;
  	queue_complete_req(req);
  }
  
  static int handle_async_request(struct file_info *fi,
  				struct pending_request *req, int node)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
626
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
  	struct hpsb_packet *packet = NULL;
  	u64 addr = req->req.address & 0xffffffffffffULL;
  
  	switch (req->req.type) {
  	case RAW1394_REQ_ASYNC_READ:
  		DBGMSG("read_request called");
  		packet =
  		    hpsb_make_readpacket(fi->host, node, addr, req->req.length);
  
  		if (!packet)
  			return -ENOMEM;
  
  		if (req->req.length == 4)
  			req->data = &packet->header[3];
  		else
  			req->data = packet->data;
  
  		break;
  
  	case RAW1394_REQ_ASYNC_WRITE:
  		DBGMSG("write_request called");
  
  		packet = hpsb_make_writepacket(fi->host, node, addr, NULL,
  					       req->req.length);
  		if (!packet)
  			return -ENOMEM;
  
  		if (req->req.length == 4) {
  			if (copy_from_user
  			    (&packet->header[3], int2ptr(req->req.sendb),
  			     req->req.length))
  				req->req.error = RAW1394_ERROR_MEMFAULT;
  		} else {
  			if (copy_from_user
  			    (packet->data, int2ptr(req->req.sendb),
  			     req->req.length))
  				req->req.error = RAW1394_ERROR_MEMFAULT;
  		}
  
  		req->req.length = 0;
  		break;
  
  	case RAW1394_REQ_ASYNC_STREAM:
  		DBGMSG("stream_request called");
  
  		packet =
  		    hpsb_make_streampacket(fi->host, NULL, req->req.length,
  					   node & 0x3f /*channel */ ,
  					   (req->req.misc >> 16) & 0x3,
  					   req->req.misc & 0xf);
  		if (!packet)
  			return -ENOMEM;
  
  		if (copy_from_user(packet->data, int2ptr(req->req.sendb),
  				   req->req.length))
  			req->req.error = RAW1394_ERROR_MEMFAULT;
  
  		req->req.length = 0;
  		break;
  
  	case RAW1394_REQ_LOCK:
  		DBGMSG("lock_request called");
  		if ((req->req.misc == EXTCODE_FETCH_ADD)
  		    || (req->req.misc == EXTCODE_LITTLE_ADD)) {
  			if (req->req.length != 4) {
  				req->req.error = RAW1394_ERROR_INVALID_ARG;
  				break;
  			}
  		} else {
  			if (req->req.length != 8) {
  				req->req.error = RAW1394_ERROR_INVALID_ARG;
  				break;
  			}
  		}
  
  		packet = hpsb_make_lockpacket(fi->host, node, addr,
  					      req->req.misc, NULL, 0);
  		if (!packet)
  			return -ENOMEM;
  
  		if (copy_from_user(packet->data, int2ptr(req->req.sendb),
  				   req->req.length)) {
  			req->req.error = RAW1394_ERROR_MEMFAULT;
  			break;
  		}
  
  		req->data = packet->data;
  		req->req.length = 4;
  		break;
  
  	case RAW1394_REQ_LOCK64:
  		DBGMSG("lock64_request called");
  		if ((req->req.misc == EXTCODE_FETCH_ADD)
  		    || (req->req.misc == EXTCODE_LITTLE_ADD)) {
  			if (req->req.length != 8) {
  				req->req.error = RAW1394_ERROR_INVALID_ARG;
  				break;
  			}
  		} else {
  			if (req->req.length != 16) {
  				req->req.error = RAW1394_ERROR_INVALID_ARG;
  				break;
  			}
  		}
  		packet = hpsb_make_lock64packet(fi->host, node, addr,
  						req->req.misc, NULL, 0);
  		if (!packet)
  			return -ENOMEM;
  
  		if (copy_from_user(packet->data, int2ptr(req->req.sendb),
  				   req->req.length)) {
  			req->req.error = RAW1394_ERROR_MEMFAULT;
  			break;
  		}
  
  		req->data = packet->data;
  		req->req.length = 8;
  		break;
  
  	default:
  		req->req.error = RAW1394_ERROR_STATE_ORDER;
  	}
  
  	req->packet = packet;
  
  	if (req->req.error) {
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
755
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
756
757
758
759
  	}
  
  	hpsb_set_packet_complete_task(packet,
  				      (void (*)(void *))queue_complete_cb, req);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
760
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
761
  	list_add_tail(&req->list, &fi->req_pending);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
762
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
764
765
766
767
768
769
770
771
  
  	packet->generation = req->req.generation;
  
  	if (hpsb_send_packet(packet) < 0) {
  		req->req.error = RAW1394_ERROR_SEND_ERROR;
  		req->req.length = 0;
  		hpsb_free_tlabel(packet);
  		queue_complete_req(req);
  	}
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
772
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
  static int handle_async_send(struct file_info *fi, struct pending_request *req)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
776
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
779
  	struct hpsb_packet *packet;
  	int header_length = req->req.misc & 0xffff;
  	int expect_response = req->req.misc >> 16;
976da96a5   Petr Vandrovec   ieee1394: raw1394...
780
  	size_t data_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781

7542e0e69   Stefan Richter   ieee1394: remove ...
782
783
  	if (header_length > req->req.length || header_length < 12 ||
  	    header_length > FIELD_SIZEOF(struct hpsb_packet, header)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
786
  		req->req.error = RAW1394_ERROR_INVALID_ARG;
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
787
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
  	}
976da96a5   Petr Vandrovec   ieee1394: raw1394...
789
790
  	data_size = req->req.length - header_length;
  	packet = hpsb_alloc_packet(data_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
793
794
795
796
797
798
799
  	req->packet = packet;
  	if (!packet)
  		return -ENOMEM;
  
  	if (copy_from_user(packet->header, int2ptr(req->req.sendb),
  			   header_length)) {
  		req->req.error = RAW1394_ERROR_MEMFAULT;
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
800
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
804
  	}
  
  	if (copy_from_user
  	    (packet->data, int2ptr(req->req.sendb) + header_length,
976da96a5   Petr Vandrovec   ieee1394: raw1394...
805
  	     data_size)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
808
  		req->req.error = RAW1394_ERROR_MEMFAULT;
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
809
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
811
812
813
814
815
816
817
818
  	}
  
  	packet->type = hpsb_async;
  	packet->node_id = packet->header[0] >> 16;
  	packet->tcode = (packet->header[0] >> 4) & 0xf;
  	packet->tlabel = (packet->header[0] >> 10) & 0x3f;
  	packet->host = fi->host;
  	packet->expect_response = expect_response;
  	packet->header_size = header_length;
976da96a5   Petr Vandrovec   ieee1394: raw1394...
819
  	packet->data_size = data_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
820
821
822
823
  
  	req->req.length = 0;
  	hpsb_set_packet_complete_task(packet,
  				      (void (*)(void *))queue_complete_cb, req);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
824
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
  	list_add_tail(&req->list, &fi->req_pending);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
826
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
827
828
829
830
831
832
833
834
  
  	/* Update the generation of the packet just before sending. */
  	packet->generation = req->req.generation;
  
  	if (hpsb_send_packet(packet) < 0) {
  		req->req.error = RAW1394_ERROR_SEND_ERROR;
  		queue_complete_req(req);
  	}
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
835
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
837
838
839
840
  }
  
  static int arm_read(struct hpsb_host *host, int nodeid, quadlet_t * buffer,
  		    u64 addr, size_t length, u16 flags)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
841
  	unsigned long irqflags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
845
846
847
848
849
850
  	struct pending_request *req;
  	struct host_info *hi;
  	struct file_info *fi = NULL;
  	struct list_head *entry;
  	struct arm_addr *arm_addr = NULL;
  	struct arm_request *arm_req = NULL;
  	struct arm_response *arm_resp = NULL;
  	int found = 0, size = 0, rcode = -1;
  	struct arm_request_response *arm_req_resp = NULL;
a5c52df8b   Joe Perches   ieee1394: Add mis...
851
  	DBGMSG("arm_read  called by node: %X "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
853
854
  	       "addr: %4.4x %8.8x length: %Zu", nodeid,
  	       (u16) ((addr >> 32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
  	       length);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
855
  	spin_lock_irqsave(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
  	hi = find_host_info(host);	/* search address-entry */
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			entry = fi->addr_list.next;
  			while (entry != &(fi->addr_list)) {
  				arm_addr =
  				    list_entry(entry, struct arm_addr,
  					       addr_list);
  				if (((arm_addr->start) <= (addr))
  				    && ((arm_addr->end) >= (addr + length))) {
  					found = 1;
  					break;
  				}
  				entry = entry->next;
  			}
  			if (found) {
  				break;
  			}
  		}
  	}
  	rcode = -1;
  	if (!found) {
  		printk(KERN_ERR "raw1394: arm_read FAILED addr_entry not found"
  		       " -> rcode_address_error
  ");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
881
  		spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
  		return (RCODE_ADDRESS_ERROR);
  	} else {
  		DBGMSG("arm_read addr_entry FOUND");
  	}
  	if (arm_addr->rec_length < length) {
  		DBGMSG("arm_read blocklength too big -> rcode_data_error");
  		rcode = RCODE_DATA_ERROR;	/* hardware error, data is unavailable */
  	}
  	if (rcode == -1) {
  		if (arm_addr->access_rights & ARM_READ) {
  			if (!(arm_addr->client_transactions & ARM_READ)) {
  				memcpy(buffer,
  				       (arm_addr->addr_space_buffer) + (addr -
  									(arm_addr->
  									 start)),
  				       length);
  				DBGMSG("arm_read -> (rcode_complete)");
  				rcode = RCODE_COMPLETE;
  			}
  		} else {
  			rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  			DBGMSG("arm_read -> rcode_type_error (access denied)");
  		}
  	}
  	if (arm_addr->notification_options & ARM_READ) {
  		DBGMSG("arm_read -> entering notification-section");
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
908
  		req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
909
910
  		if (!req) {
  			DBGMSG("arm_read -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
911
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		if (rcode == RCODE_COMPLETE) {
  			size =
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response) +
  			    length * sizeof(byte_t) +
  			    sizeof(struct arm_request_response);
  		} else {
  			size =
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response) +
  			    sizeof(struct arm_request_response);
  		}
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
927
  		req->data = kmalloc(size, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
929
930
  		if (!(req->data)) {
  			free_pending_request(req);
  			DBGMSG("arm_read -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
931
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		req->free_data = 1;
  		req->file_info = fi;
  		req->req.type = RAW1394_REQ_ARM;
  		req->req.generation = get_hpsb_generation(host);
  		req->req.misc =
  		    (((length << 16) & (0xFFFF0000)) | (ARM_READ & 0xFF));
  		req->req.tag = arm_addr->arm_tag;
  		req->req.recvb = arm_addr->recvb;
  		req->req.length = size;
  		arm_req_resp = (struct arm_request_response *)(req->data);
  		arm_req = (struct arm_request *)((byte_t *) (req->data) +
  						 (sizeof
  						  (struct
  						   arm_request_response)));
  		arm_resp =
  		    (struct arm_response *)((byte_t *) (arm_req) +
  					    (sizeof(struct arm_request)));
  		arm_req->buffer = NULL;
  		arm_resp->buffer = NULL;
  		if (rcode == RCODE_COMPLETE) {
  			byte_t *buf =
  			    (byte_t *) arm_resp + sizeof(struct arm_response);
  			memcpy(buf,
  			       (arm_addr->addr_space_buffer) + (addr -
  								(arm_addr->
  								 start)),
  			       length);
  			arm_resp->buffer =
  			    int2ptr((arm_addr->recvb) +
  				    sizeof(struct arm_request_response) +
  				    sizeof(struct arm_request) +
  				    sizeof(struct arm_response));
  		}
  		arm_resp->buffer_length =
  		    (rcode == RCODE_COMPLETE) ? length : 0;
  		arm_resp->response_code = rcode;
  		arm_req->buffer_length = 0;
  		arm_req->generation = req->req.generation;
  		arm_req->extended_transaction_code = 0;
  		arm_req->destination_offset = addr;
  		arm_req->source_nodeid = nodeid;
  		arm_req->destination_nodeid = host->node_id;
  		arm_req->tlabel = (flags >> 10) & 0x3f;
  		arm_req->tcode = (flags >> 4) & 0x0f;
  		arm_req_resp->request = int2ptr((arm_addr->recvb) +
  						sizeof(struct
  						       arm_request_response));
  		arm_req_resp->response =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request));
  		queue_complete_req(req);
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
988
  	spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989
990
991
992
993
994
  	return (rcode);
  }
  
  static int arm_write(struct hpsb_host *host, int nodeid, int destid,
  		     quadlet_t * data, u64 addr, size_t length, u16 flags)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
995
  	unsigned long irqflags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
997
998
999
1000
1001
1002
  	struct pending_request *req;
  	struct host_info *hi;
  	struct file_info *fi = NULL;
  	struct list_head *entry;
  	struct arm_addr *arm_addr = NULL;
  	struct arm_request *arm_req = NULL;
  	struct arm_response *arm_resp = NULL;
5030c8079   Stefan Richter   ieee1394: remove ...
1003
  	int found = 0, size = 0, rcode = -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004
  	struct arm_request_response *arm_req_resp = NULL;
a5c52df8b   Joe Perches   ieee1394: Add mis...
1005
  	DBGMSG("arm_write called by node: %X "
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1006
1007
1008
  	       "addr: %4.4x %8.8x length: %Zu", nodeid,
  	       (u16) ((addr >> 32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
  	       length);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1009
  	spin_lock_irqsave(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  	hi = find_host_info(host);	/* search address-entry */
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			entry = fi->addr_list.next;
  			while (entry != &(fi->addr_list)) {
  				arm_addr =
  				    list_entry(entry, struct arm_addr,
  					       addr_list);
  				if (((arm_addr->start) <= (addr))
  				    && ((arm_addr->end) >= (addr + length))) {
  					found = 1;
  					break;
  				}
  				entry = entry->next;
  			}
  			if (found) {
  				break;
  			}
  		}
  	}
  	rcode = -1;
  	if (!found) {
  		printk(KERN_ERR "raw1394: arm_write FAILED addr_entry not found"
  		       " -> rcode_address_error
  ");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1035
  		spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
1038
1039
1040
1041
  		return (RCODE_ADDRESS_ERROR);
  	} else {
  		DBGMSG("arm_write addr_entry FOUND");
  	}
  	if (arm_addr->rec_length < length) {
  		DBGMSG("arm_write blocklength too big -> rcode_data_error");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  		rcode = RCODE_DATA_ERROR;	/* hardware error, data is unavailable */
  	}
  	if (rcode == -1) {
  		if (arm_addr->access_rights & ARM_WRITE) {
  			if (!(arm_addr->client_transactions & ARM_WRITE)) {
  				memcpy((arm_addr->addr_space_buffer) +
  				       (addr - (arm_addr->start)), data,
  				       length);
  				DBGMSG("arm_write -> (rcode_complete)");
  				rcode = RCODE_COMPLETE;
  			}
  		} else {
  			rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  			DBGMSG("arm_write -> rcode_type_error (access denied)");
  		}
  	}
  	if (arm_addr->notification_options & ARM_WRITE) {
  		DBGMSG("arm_write -> entering notification-section");
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1060
  		req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1061
1062
  		if (!req) {
  			DBGMSG("arm_write -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1063
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1064
1065
1066
1067
1068
1069
1070
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request my be retried */
  		}
  		size =
  		    sizeof(struct arm_request) + sizeof(struct arm_response) +
  		    (length) * sizeof(byte_t) +
  		    sizeof(struct arm_request_response);
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1071
  		req->data = kmalloc(size, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1072
1073
1074
  		if (!(req->data)) {
  			free_pending_request(req);
  			DBGMSG("arm_write -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1075
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		req->free_data = 1;
  		req->file_info = fi;
  		req->req.type = RAW1394_REQ_ARM;
  		req->req.generation = get_hpsb_generation(host);
  		req->req.misc =
  		    (((length << 16) & (0xFFFF0000)) | (ARM_WRITE & 0xFF));
  		req->req.tag = arm_addr->arm_tag;
  		req->req.recvb = arm_addr->recvb;
  		req->req.length = size;
  		arm_req_resp = (struct arm_request_response *)(req->data);
  		arm_req = (struct arm_request *)((byte_t *) (req->data) +
  						 (sizeof
  						  (struct
  						   arm_request_response)));
  		arm_resp =
  		    (struct arm_response *)((byte_t *) (arm_req) +
  					    (sizeof(struct arm_request)));
  		arm_resp->buffer = NULL;
  		memcpy((byte_t *) arm_resp + sizeof(struct arm_response),
  		       data, length);
  		arm_req->buffer = int2ptr((arm_addr->recvb) +
  					  sizeof(struct arm_request_response) +
  					  sizeof(struct arm_request) +
  					  sizeof(struct arm_response));
  		arm_req->buffer_length = length;
  		arm_req->generation = req->req.generation;
  		arm_req->extended_transaction_code = 0;
  		arm_req->destination_offset = addr;
  		arm_req->source_nodeid = nodeid;
  		arm_req->destination_nodeid = destid;
  		arm_req->tlabel = (flags >> 10) & 0x3f;
  		arm_req->tcode = (flags >> 4) & 0x0f;
  		arm_resp->buffer_length = 0;
  		arm_resp->response_code = rcode;
  		arm_req_resp->request = int2ptr((arm_addr->recvb) +
  						sizeof(struct
  						       arm_request_response));
  		arm_req_resp->response =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request));
  		queue_complete_req(req);
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1122
  	spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1123
1124
1125
1126
1127
1128
1129
  	return (rcode);
  }
  
  static int arm_lock(struct hpsb_host *host, int nodeid, quadlet_t * store,
  		    u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
  		    u16 flags)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1130
  	unsigned long irqflags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
  	struct pending_request *req;
  	struct host_info *hi;
  	struct file_info *fi = NULL;
  	struct list_head *entry;
  	struct arm_addr *arm_addr = NULL;
  	struct arm_request *arm_req = NULL;
  	struct arm_response *arm_resp = NULL;
  	int found = 0, size = 0, rcode = -1;
  	quadlet_t old, new;
  	struct arm_request_response *arm_req_resp = NULL;
  
  	if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
  	    ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
  		DBGMSG("arm_lock  called by node: %X "
  		       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X",
  		       nodeid, (u16) ((addr >> 32) & 0xFFFF),
  		       (u32) (addr & 0xFFFFFFFF), ext_tcode & 0xFF,
  		       be32_to_cpu(data));
  	} else {
  		DBGMSG("arm_lock  called by node: %X "
  		       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X arg: %8.8X",
  		       nodeid, (u16) ((addr >> 32) & 0xFFFF),
  		       (u32) (addr & 0xFFFFFFFF), ext_tcode & 0xFF,
  		       be32_to_cpu(data), be32_to_cpu(arg));
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1156
  	spin_lock_irqsave(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
  	hi = find_host_info(host);	/* search address-entry */
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			entry = fi->addr_list.next;
  			while (entry != &(fi->addr_list)) {
  				arm_addr =
  				    list_entry(entry, struct arm_addr,
  					       addr_list);
  				if (((arm_addr->start) <= (addr))
  				    && ((arm_addr->end) >=
  					(addr + sizeof(*store)))) {
  					found = 1;
  					break;
  				}
  				entry = entry->next;
  			}
  			if (found) {
  				break;
  			}
  		}
  	}
  	rcode = -1;
  	if (!found) {
  		printk(KERN_ERR "raw1394: arm_lock FAILED addr_entry not found"
  		       " -> rcode_address_error
  ");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1183
  		spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  		return (RCODE_ADDRESS_ERROR);
  	} else {
  		DBGMSG("arm_lock addr_entry FOUND");
  	}
  	if (rcode == -1) {
  		if (arm_addr->access_rights & ARM_LOCK) {
  			if (!(arm_addr->client_transactions & ARM_LOCK)) {
  				memcpy(&old,
  				       (arm_addr->addr_space_buffer) + (addr -
  									(arm_addr->
  									 start)),
  				       sizeof(old));
  				switch (ext_tcode) {
  				case (EXTCODE_MASK_SWAP):
  					new = data | (old & ~arg);
  					break;
  				case (EXTCODE_COMPARE_SWAP):
  					if (old == arg) {
  						new = data;
  					} else {
  						new = old;
  					}
  					break;
  				case (EXTCODE_FETCH_ADD):
  					new =
  					    cpu_to_be32(be32_to_cpu(data) +
  							be32_to_cpu(old));
  					break;
  				case (EXTCODE_LITTLE_ADD):
  					new =
  					    cpu_to_le32(le32_to_cpu(data) +
  							le32_to_cpu(old));
  					break;
  				case (EXTCODE_BOUNDED_ADD):
  					if (old != arg) {
  						new =
  						    cpu_to_be32(be32_to_cpu
  								(data) +
  								be32_to_cpu
  								(old));
  					} else {
  						new = old;
  					}
  					break;
  				case (EXTCODE_WRAP_ADD):
  					if (old != arg) {
  						new =
  						    cpu_to_be32(be32_to_cpu
  								(data) +
  								be32_to_cpu
  								(old));
  					} else {
  						new = data;
  					}
  					break;
  				default:
  					rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  					printk(KERN_ERR
  					       "raw1394: arm_lock FAILED "
  					       "ext_tcode not allowed -> rcode_type_error
  ");
  					break;
  				}	/*switch */
  				if (rcode == -1) {
  					DBGMSG("arm_lock -> (rcode_complete)");
  					rcode = RCODE_COMPLETE;
  					memcpy(store, &old, sizeof(*store));
  					memcpy((arm_addr->addr_space_buffer) +
  					       (addr - (arm_addr->start)),
  					       &new, sizeof(*store));
  				}
  			}
  		} else {
  			rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  			DBGMSG("arm_lock -> rcode_type_error (access denied)");
  		}
  	}
  	if (arm_addr->notification_options & ARM_LOCK) {
  		byte_t *buf1, *buf2;
  		DBGMSG("arm_lock -> entering notification-section");
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1264
  		req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1265
1266
  		if (!req) {
  			DBGMSG("arm_lock -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1267
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1268
1269
1270
1271
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		size = sizeof(struct arm_request) + sizeof(struct arm_response) + 3 * sizeof(*store) + sizeof(struct arm_request_response);	/* maximum */
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1272
  		req->data = kmalloc(size, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
1274
1275
  		if (!(req->data)) {
  			free_pending_request(req);
  			DBGMSG("arm_lock -> rcode_conflict_error");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1276
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		req->free_data = 1;
  		arm_req_resp = (struct arm_request_response *)(req->data);
  		arm_req = (struct arm_request *)((byte_t *) (req->data) +
  						 (sizeof
  						  (struct
  						   arm_request_response)));
  		arm_resp =
  		    (struct arm_response *)((byte_t *) (arm_req) +
  					    (sizeof(struct arm_request)));
  		buf1 = (byte_t *) arm_resp + sizeof(struct arm_response);
  		buf2 = buf1 + 2 * sizeof(*store);
  		if ((ext_tcode == EXTCODE_FETCH_ADD) ||
  		    (ext_tcode == EXTCODE_LITTLE_ADD)) {
  			arm_req->buffer_length = sizeof(*store);
  			memcpy(buf1, &data, sizeof(*store));
  
  		} else {
  			arm_req->buffer_length = 2 * sizeof(*store);
  			memcpy(buf1, &arg, sizeof(*store));
  			memcpy(buf1 + sizeof(*store), &data, sizeof(*store));
  		}
  		if (rcode == RCODE_COMPLETE) {
  			arm_resp->buffer_length = sizeof(*store);
  			memcpy(buf2, &old, sizeof(*store));
  		} else {
  			arm_resp->buffer_length = 0;
  		}
  		req->file_info = fi;
  		req->req.type = RAW1394_REQ_ARM;
  		req->req.generation = get_hpsb_generation(host);
  		req->req.misc = ((((sizeof(*store)) << 16) & (0xFFFF0000)) |
  				 (ARM_LOCK & 0xFF));
  		req->req.tag = arm_addr->arm_tag;
  		req->req.recvb = arm_addr->recvb;
  		req->req.length = size;
  		arm_req->generation = req->req.generation;
  		arm_req->extended_transaction_code = ext_tcode;
  		arm_req->destination_offset = addr;
  		arm_req->source_nodeid = nodeid;
  		arm_req->destination_nodeid = host->node_id;
  		arm_req->tlabel = (flags >> 10) & 0x3f;
  		arm_req->tcode = (flags >> 4) & 0x0f;
  		arm_resp->response_code = rcode;
  		arm_req_resp->request = int2ptr((arm_addr->recvb) +
  						sizeof(struct
  						       arm_request_response));
  		arm_req_resp->response =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request));
  		arm_req->buffer =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response));
  		arm_resp->buffer =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response) + 2 * sizeof(*store));
  		queue_complete_req(req);
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1342
  	spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1343
1344
1345
1346
1347
1348
1349
  	return (rcode);
  }
  
  static int arm_lock64(struct hpsb_host *host, int nodeid, octlet_t * store,
  		      u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
  		      u16 flags)
  {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1350
  	unsigned long irqflags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  	struct pending_request *req;
  	struct host_info *hi;
  	struct file_info *fi = NULL;
  	struct list_head *entry;
  	struct arm_addr *arm_addr = NULL;
  	struct arm_request *arm_req = NULL;
  	struct arm_response *arm_resp = NULL;
  	int found = 0, size = 0, rcode = -1;
  	octlet_t old, new;
  	struct arm_request_response *arm_req_resp = NULL;
  
  	if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
  	    ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
  		DBGMSG("arm_lock64 called by node: %X "
  		       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X ",
  		       nodeid, (u16) ((addr >> 32) & 0xFFFF),
  		       (u32) (addr & 0xFFFFFFFF),
  		       ext_tcode & 0xFF,
  		       (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF),
  		       (u32) (be64_to_cpu(data) & 0xFFFFFFFF));
  	} else {
  		DBGMSG("arm_lock64 called by node: %X "
  		       "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X arg: "
  		       "%8.8X %8.8X ",
  		       nodeid, (u16) ((addr >> 32) & 0xFFFF),
  		       (u32) (addr & 0xFFFFFFFF),
  		       ext_tcode & 0xFF,
  		       (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF),
  		       (u32) (be64_to_cpu(data) & 0xFFFFFFFF),
  		       (u32) ((be64_to_cpu(arg) >> 32) & 0xFFFFFFFF),
  		       (u32) (be64_to_cpu(arg) & 0xFFFFFFFF));
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1383
  	spin_lock_irqsave(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
  	hi = find_host_info(host);	/* search addressentry in file_info's for host */
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			entry = fi->addr_list.next;
  			while (entry != &(fi->addr_list)) {
  				arm_addr =
  				    list_entry(entry, struct arm_addr,
  					       addr_list);
  				if (((arm_addr->start) <= (addr))
  				    && ((arm_addr->end) >=
  					(addr + sizeof(*store)))) {
  					found = 1;
  					break;
  				}
  				entry = entry->next;
  			}
  			if (found) {
  				break;
  			}
  		}
  	}
  	rcode = -1;
  	if (!found) {
  		printk(KERN_ERR
  		       "raw1394: arm_lock64 FAILED addr_entry not found"
  		       " -> rcode_address_error
  ");
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1411
  		spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  		return (RCODE_ADDRESS_ERROR);
  	} else {
  		DBGMSG("arm_lock64 addr_entry FOUND");
  	}
  	if (rcode == -1) {
  		if (arm_addr->access_rights & ARM_LOCK) {
  			if (!(arm_addr->client_transactions & ARM_LOCK)) {
  				memcpy(&old,
  				       (arm_addr->addr_space_buffer) + (addr -
  									(arm_addr->
  									 start)),
  				       sizeof(old));
  				switch (ext_tcode) {
  				case (EXTCODE_MASK_SWAP):
  					new = data | (old & ~arg);
  					break;
  				case (EXTCODE_COMPARE_SWAP):
  					if (old == arg) {
  						new = data;
  					} else {
  						new = old;
  					}
  					break;
  				case (EXTCODE_FETCH_ADD):
  					new =
  					    cpu_to_be64(be64_to_cpu(data) +
  							be64_to_cpu(old));
  					break;
  				case (EXTCODE_LITTLE_ADD):
  					new =
  					    cpu_to_le64(le64_to_cpu(data) +
  							le64_to_cpu(old));
  					break;
  				case (EXTCODE_BOUNDED_ADD):
  					if (old != arg) {
  						new =
  						    cpu_to_be64(be64_to_cpu
  								(data) +
  								be64_to_cpu
  								(old));
  					} else {
  						new = old;
  					}
  					break;
  				case (EXTCODE_WRAP_ADD):
  					if (old != arg) {
  						new =
  						    cpu_to_be64(be64_to_cpu
  								(data) +
  								be64_to_cpu
  								(old));
  					} else {
  						new = data;
  					}
  					break;
  				default:
  					printk(KERN_ERR
  					       "raw1394: arm_lock64 FAILED "
  					       "ext_tcode not allowed -> rcode_type_error
  ");
  					rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  					break;
  				}	/*switch */
  				if (rcode == -1) {
  					DBGMSG
  					    ("arm_lock64 -> (rcode_complete)");
  					rcode = RCODE_COMPLETE;
  					memcpy(store, &old, sizeof(*store));
  					memcpy((arm_addr->addr_space_buffer) +
  					       (addr - (arm_addr->start)),
  					       &new, sizeof(*store));
  				}
  			}
  		} else {
  			rcode = RCODE_TYPE_ERROR;	/* function not allowed */
  			DBGMSG
  			    ("arm_lock64 -> rcode_type_error (access denied)");
  		}
  	}
  	if (arm_addr->notification_options & ARM_LOCK) {
  		byte_t *buf1, *buf2;
  		DBGMSG("arm_lock64 -> entering notification-section");
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1494
  		req = __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1495
  		if (!req) {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1496
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1497
1498
1499
1500
1501
  			DBGMSG("arm_lock64 -> rcode_conflict_error");
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		size = sizeof(struct arm_request) + sizeof(struct arm_response) + 3 * sizeof(*store) + sizeof(struct arm_request_response);	/* maximum */
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
1502
  		req->data = kmalloc(size, GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1503
1504
  		if (!(req->data)) {
  			free_pending_request(req);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1505
  			spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
  			DBGMSG("arm_lock64 -> rcode_conflict_error");
  			return (RCODE_CONFLICT_ERROR);	/* A resource conflict was detected.
  							   The request may be retried */
  		}
  		req->free_data = 1;
  		arm_req_resp = (struct arm_request_response *)(req->data);
  		arm_req = (struct arm_request *)((byte_t *) (req->data) +
  						 (sizeof
  						  (struct
  						   arm_request_response)));
  		arm_resp =
  		    (struct arm_response *)((byte_t *) (arm_req) +
  					    (sizeof(struct arm_request)));
  		buf1 = (byte_t *) arm_resp + sizeof(struct arm_response);
  		buf2 = buf1 + 2 * sizeof(*store);
  		if ((ext_tcode == EXTCODE_FETCH_ADD) ||
  		    (ext_tcode == EXTCODE_LITTLE_ADD)) {
  			arm_req->buffer_length = sizeof(*store);
  			memcpy(buf1, &data, sizeof(*store));
  
  		} else {
  			arm_req->buffer_length = 2 * sizeof(*store);
  			memcpy(buf1, &arg, sizeof(*store));
  			memcpy(buf1 + sizeof(*store), &data, sizeof(*store));
  		}
  		if (rcode == RCODE_COMPLETE) {
  			arm_resp->buffer_length = sizeof(*store);
  			memcpy(buf2, &old, sizeof(*store));
  		} else {
  			arm_resp->buffer_length = 0;
  		}
  		req->file_info = fi;
  		req->req.type = RAW1394_REQ_ARM;
  		req->req.generation = get_hpsb_generation(host);
  		req->req.misc = ((((sizeof(*store)) << 16) & (0xFFFF0000)) |
  				 (ARM_LOCK & 0xFF));
  		req->req.tag = arm_addr->arm_tag;
  		req->req.recvb = arm_addr->recvb;
  		req->req.length = size;
  		arm_req->generation = req->req.generation;
  		arm_req->extended_transaction_code = ext_tcode;
  		arm_req->destination_offset = addr;
  		arm_req->source_nodeid = nodeid;
  		arm_req->destination_nodeid = host->node_id;
  		arm_req->tlabel = (flags >> 10) & 0x3f;
  		arm_req->tcode = (flags >> 4) & 0x0f;
  		arm_resp->response_code = rcode;
  		arm_req_resp->request = int2ptr((arm_addr->recvb) +
  						sizeof(struct
  						       arm_request_response));
  		arm_req_resp->response =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request));
  		arm_req->buffer =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response));
  		arm_resp->buffer =
  		    int2ptr((arm_addr->recvb) +
  			    sizeof(struct arm_request_response) +
  			    sizeof(struct arm_request) +
  			    sizeof(struct arm_response) + 2 * sizeof(*store));
  		queue_complete_req(req);
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1572
  	spin_unlock_irqrestore(&host_info_lock, irqflags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
  	return (rcode);
  }
  
  static int arm_register(struct file_info *fi, struct pending_request *req)
  {
  	int retval;
  	struct arm_addr *addr;
  	struct host_info *hi;
  	struct file_info *fi_hlp = NULL;
  	struct list_head *entry;
  	struct arm_addr *arm_addr = NULL;
  	int same_host, another_host;
  	unsigned long flags;
  
  	DBGMSG("arm_register called "
  	       "addr(Offset): %8.8x %8.8x length: %u "
  	       "rights: %2.2X notify: %2.2X "
  	       "max_blk_len: %4.4X",
  	       (u32) ((req->req.address >> 32) & 0xFFFF),
  	       (u32) (req->req.address & 0xFFFFFFFF),
  	       req->req.length, ((req->req.misc >> 8) & 0xFF),
  	       (req->req.misc & 0xFF), ((req->req.misc >> 16) & 0xFFFF));
  	/* check addressrange */
  	if ((((req->req.address) & ~(0xFFFFFFFFFFFFULL)) != 0) ||
  	    (((req->req.address + req->req.length) & ~(0xFFFFFFFFFFFFULL)) !=
  	     0)) {
  		req->req.length = 0;
  		return (-EINVAL);
  	}
  	/* addr-list-entry for fileinfo */
e94b17660   Christoph Lameter   [PATCH] slab: rem...
1603
  	addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
1605
1606
1607
1608
  	if (!addr) {
  		req->req.length = 0;
  		return (-ENOMEM);
  	}
  	/* allocation of addr_space_buffer */
8551158ab   Stefan Richter   kmalloc/kzalloc c...
1609
  	addr->addr_space_buffer = vmalloc(req->req.length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
  	if (!(addr->addr_space_buffer)) {
  		kfree(addr);
  		req->req.length = 0;
  		return (-ENOMEM);
  	}
  	/* initialization of addr_space_buffer */
  	if ((req->req.sendb) == (unsigned long)NULL) {
  		/* init: set 0 */
  		memset(addr->addr_space_buffer, 0, req->req.length);
  	} else {
  		/* init: user -> kernel */
  		if (copy_from_user
  		    (addr->addr_space_buffer, int2ptr(req->req.sendb),
  		     req->req.length)) {
  			vfree(addr->addr_space_buffer);
  			kfree(addr);
  			return (-EFAULT);
  		}
  	}
  	INIT_LIST_HEAD(&addr->addr_list);
  	addr->arm_tag = req->req.tag;
  	addr->start = req->req.address;
  	addr->end = req->req.address + req->req.length;
  	addr->access_rights = (u8) (req->req.misc & 0x0F);
  	addr->notification_options = (u8) ((req->req.misc >> 4) & 0x0F);
  	addr->client_transactions = (u8) ((req->req.misc >> 8) & 0x0F);
  	addr->access_rights |= addr->client_transactions;
  	addr->notification_options |= addr->client_transactions;
  	addr->recvb = req->req.recvb;
  	addr->rec_length = (u16) ((req->req.misc >> 16) & 0xFFFF);
3253b669e   Stefan Richter   ieee1394: raw1394...
1640

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
  	spin_lock_irqsave(&host_info_lock, flags);
  	hi = find_host_info(fi->host);
  	same_host = 0;
  	another_host = 0;
  	/* same host with address-entry containing same addressrange ? */
  	list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
  		entry = fi_hlp->addr_list.next;
  		while (entry != &(fi_hlp->addr_list)) {
  			arm_addr =
  			    list_entry(entry, struct arm_addr, addr_list);
  			if ((arm_addr->start == addr->start)
  			    && (arm_addr->end == addr->end)) {
  				DBGMSG("same host ownes same "
  				       "addressrange -> EALREADY");
  				same_host = 1;
  				break;
  			}
  			entry = entry->next;
  		}
  		if (same_host) {
  			break;
  		}
  	}
  	if (same_host) {
  		/* addressrange occupied by same host */
3253b669e   Stefan Richter   ieee1394: raw1394...
1666
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1667
1668
  		vfree(addr->addr_space_buffer);
  		kfree(addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  		return (-EALREADY);
  	}
  	/* another host with valid address-entry containing same addressrange */
  	list_for_each_entry(hi, &host_info_list, list) {
  		if (hi->host != fi->host) {
  			list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
  				entry = fi_hlp->addr_list.next;
  				while (entry != &(fi_hlp->addr_list)) {
  					arm_addr =
  					    list_entry(entry, struct arm_addr,
  						       addr_list);
  					if ((arm_addr->start == addr->start)
  					    && (arm_addr->end == addr->end)) {
  						DBGMSG
  						    ("another host ownes same "
  						     "addressrange");
  						another_host = 1;
  						break;
  					}
  					entry = entry->next;
  				}
  				if (another_host) {
  					break;
  				}
  			}
  		}
  	}
3253b669e   Stefan Richter   ieee1394: raw1394...
1696
  	spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1697
1698
1699
1700
1701
1702
1703
1704
1705
  	if (another_host) {
  		DBGMSG("another hosts entry is valid -> SUCCESS");
  		if (copy_to_user(int2ptr(req->req.recvb),
  				 &addr->start, sizeof(u64))) {
  			printk(KERN_ERR "raw1394: arm_register failed "
  			       " address-range-entry is invalid -> EFAULT !!!
  ");
  			vfree(addr->addr_space_buffer);
  			kfree(addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1706
1707
1708
1709
  			return (-EFAULT);
  		}
  		free_pending_request(req);	/* immediate success or fail */
  		/* INSERT ENTRY */
3253b669e   Stefan Richter   ieee1394: raw1394...
1710
  		spin_lock_irqsave(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1711
1712
  		list_add_tail(&addr->addr_list, &fi->addr_list);
  		spin_unlock_irqrestore(&host_info_lock, flags);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1713
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1714
1715
1716
1717
1718
1719
1720
  	}
  	retval =
  	    hpsb_register_addrspace(&raw1394_highlevel, fi->host, &arm_ops,
  				    req->req.address,
  				    req->req.address + req->req.length);
  	if (retval) {
  		/* INSERT ENTRY */
3253b669e   Stefan Richter   ieee1394: raw1394...
1721
  		spin_lock_irqsave(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1722
  		list_add_tail(&addr->addr_list, &fi->addr_list);
3253b669e   Stefan Richter   ieee1394: raw1394...
1723
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1724
1725
1726
1727
1728
  	} else {
  		DBGMSG("arm_register failed errno: %d 
  ", retval);
  		vfree(addr->addr_space_buffer);
  		kfree(addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1729
1730
  		return (-EALREADY);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1731
  	free_pending_request(req);	/* immediate success or fail */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1732
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
  }
  
  static int arm_unregister(struct file_info *fi, struct pending_request *req)
  {
  	int found = 0;
  	int retval = 0;
  	struct list_head *entry;
  	struct arm_addr *addr = NULL;
  	struct host_info *hi;
  	struct file_info *fi_hlp = NULL;
  	struct arm_addr *arm_addr = NULL;
  	int another_host;
  	unsigned long flags;
  
  	DBGMSG("arm_Unregister called addr(Offset): "
  	       "%8.8x %8.8x",
  	       (u32) ((req->req.address >> 32) & 0xFFFF),
  	       (u32) (req->req.address & 0xFFFFFFFF));
  	spin_lock_irqsave(&host_info_lock, flags);
  	/* get addr */
  	entry = fi->addr_list.next;
  	while (entry != &(fi->addr_list)) {
  		addr = list_entry(entry, struct arm_addr, addr_list);
  		if (addr->start == req->req.address) {
  			found = 1;
  			break;
  		}
  		entry = entry->next;
  	}
  	if (!found) {
  		DBGMSG("arm_Unregister addr not found");
  		spin_unlock_irqrestore(&host_info_lock, flags);
  		return (-EINVAL);
  	}
  	DBGMSG("arm_Unregister addr found");
  	another_host = 0;
  	/* another host with valid address-entry containing
  	   same addressrange */
  	list_for_each_entry(hi, &host_info_list, list) {
  		if (hi->host != fi->host) {
  			list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
  				entry = fi_hlp->addr_list.next;
  				while (entry != &(fi_hlp->addr_list)) {
  					arm_addr = list_entry(entry,
  							      struct arm_addr,
  							      addr_list);
  					if (arm_addr->start == addr->start) {
  						DBGMSG("another host ownes "
  						       "same addressrange");
  						another_host = 1;
  						break;
  					}
  					entry = entry->next;
  				}
  				if (another_host) {
  					break;
  				}
  			}
  		}
  	}
  	if (another_host) {
  		DBGMSG("delete entry from list -> success");
  		list_del(&addr->addr_list);
3253b669e   Stefan Richter   ieee1394: raw1394...
1796
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1797
1798
1799
  		vfree(addr->addr_space_buffer);
  		kfree(addr);
  		free_pending_request(req);	/* immediate success or fail */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1800
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
  	}
  	retval =
  	    hpsb_unregister_addrspace(&raw1394_highlevel, fi->host,
  				      addr->start);
  	if (!retval) {
  		printk(KERN_ERR "raw1394: arm_Unregister failed -> EINVAL
  ");
  		spin_unlock_irqrestore(&host_info_lock, flags);
  		return (-EINVAL);
  	}
  	DBGMSG("delete entry from list -> success");
  	list_del(&addr->addr_list);
  	spin_unlock_irqrestore(&host_info_lock, flags);
  	vfree(addr->addr_space_buffer);
  	kfree(addr);
  	free_pending_request(req);	/* immediate success or fail */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1817
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
  }
  
  /* Copy data from ARM buffer(s) to user buffer. */
  static int arm_get_buf(struct file_info *fi, struct pending_request *req)
  {
  	struct arm_addr *arm_addr = NULL;
  	unsigned long flags;
  	unsigned long offset;
  
  	struct list_head *entry;
  
  	DBGMSG("arm_get_buf "
  	       "addr(Offset): %04X %08X length: %u",
  	       (u32) ((req->req.address >> 32) & 0xFFFF),
  	       (u32) (req->req.address & 0xFFFFFFFF), (u32) req->req.length);
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	entry = fi->addr_list.next;
  	while (entry != &(fi->addr_list)) {
  		arm_addr = list_entry(entry, struct arm_addr, addr_list);
  		if ((arm_addr->start <= req->req.address) &&
  		    (arm_addr->end > req->req.address)) {
  			if (req->req.address + req->req.length <= arm_addr->end) {
  				offset = req->req.address - arm_addr->start;
3253b669e   Stefan Richter   ieee1394: raw1394...
1842
  				spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1843
1844
1845
1846
1847
1848
  
  				DBGMSG
  				    ("arm_get_buf copy_to_user( %08X, %p, %u )",
  				     (u32) req->req.recvb,
  				     arm_addr->addr_space_buffer + offset,
  				     (u32) req->req.length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1849
1850
1851
  				if (copy_to_user
  				    (int2ptr(req->req.recvb),
  				     arm_addr->addr_space_buffer + offset,
3253b669e   Stefan Richter   ieee1394: raw1394...
1852
  				     req->req.length))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1853
  					return (-EFAULT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1854

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1855
1856
1857
1858
  				/* We have to free the request, because we
  				 * queue no response, and therefore nobody
  				 * will free it. */
  				free_pending_request(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1859
  				return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
  			} else {
  				DBGMSG("arm_get_buf request exceeded mapping");
  				spin_unlock_irqrestore(&host_info_lock, flags);
  				return (-EINVAL);
  			}
  		}
  		entry = entry->next;
  	}
  	spin_unlock_irqrestore(&host_info_lock, flags);
  	return (-EINVAL);
  }
  
  /* Copy data from user buffer to ARM buffer(s). */
  static int arm_set_buf(struct file_info *fi, struct pending_request *req)
  {
  	struct arm_addr *arm_addr = NULL;
  	unsigned long flags;
  	unsigned long offset;
  
  	struct list_head *entry;
  
  	DBGMSG("arm_set_buf "
  	       "addr(Offset): %04X %08X length: %u",
  	       (u32) ((req->req.address >> 32) & 0xFFFF),
  	       (u32) (req->req.address & 0xFFFFFFFF), (u32) req->req.length);
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	entry = fi->addr_list.next;
  	while (entry != &(fi->addr_list)) {
  		arm_addr = list_entry(entry, struct arm_addr, addr_list);
  		if ((arm_addr->start <= req->req.address) &&
  		    (arm_addr->end > req->req.address)) {
  			if (req->req.address + req->req.length <= arm_addr->end) {
  				offset = req->req.address - arm_addr->start;
3253b669e   Stefan Richter   ieee1394: raw1394...
1894
  				spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1895
1896
1897
1898
1899
1900
  
  				DBGMSG
  				    ("arm_set_buf copy_from_user( %p, %08X, %u )",
  				     arm_addr->addr_space_buffer + offset,
  				     (u32) req->req.sendb,
  				     (u32) req->req.length);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1901
1902
1903
  				if (copy_from_user
  				    (arm_addr->addr_space_buffer + offset,
  				     int2ptr(req->req.sendb),
3253b669e   Stefan Richter   ieee1394: raw1394...
1904
  				     req->req.length))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1905
  					return (-EFAULT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1906

3253b669e   Stefan Richter   ieee1394: raw1394...
1907
1908
1909
1910
  				/* We have to free the request, because we
  				 * queue no response, and therefore nobody
  				 * will free it. */
  				free_pending_request(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1911
  				return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
  			} else {
  				DBGMSG("arm_set_buf request exceeded mapping");
  				spin_unlock_irqrestore(&host_info_lock, flags);
  				return (-EINVAL);
  			}
  		}
  		entry = entry->next;
  	}
  	spin_unlock_irqrestore(&host_info_lock, flags);
  	return (-EINVAL);
  }
  
  static int reset_notification(struct file_info *fi, struct pending_request *req)
  {
  	DBGMSG("reset_notification called - switch %s ",
  	       (req->req.misc == RAW1394_NOTIFY_OFF) ? "OFF" : "ON");
  	if ((req->req.misc == RAW1394_NOTIFY_OFF) ||
  	    (req->req.misc == RAW1394_NOTIFY_ON)) {
  		fi->notification = (u8) req->req.misc;
  		free_pending_request(req);	/* we have to free the request, because we queue no response, and therefore nobody will free it */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1932
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
  	}
  	/* error EINVAL (22) invalid argument */
  	return (-EINVAL);
  }
  
  static int write_phypacket(struct file_info *fi, struct pending_request *req)
  {
  	struct hpsb_packet *packet = NULL;
  	int retval = 0;
  	quadlet_t data;
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1943
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
  
  	data = be32_to_cpu((u32) req->req.sendb);
  	DBGMSG("write_phypacket called - quadlet 0x%8.8x ", data);
  	packet = hpsb_make_phypacket(fi->host, data);
  	if (!packet)
  		return -ENOMEM;
  	req->req.length = 0;
  	req->packet = packet;
  	hpsb_set_packet_complete_task(packet,
  				      (void (*)(void *))queue_complete_cb, req);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1954
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955
  	list_add_tail(&req->list, &fi->req_pending);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
1956
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1957
1958
1959
1960
1961
1962
1963
1964
  	packet->generation = req->req.generation;
  	retval = hpsb_send_packet(packet);
  	DBGMSG("write_phypacket send_packet called => retval: %d ", retval);
  	if (retval < 0) {
  		req->req.error = RAW1394_ERROR_SEND_ERROR;
  		req->req.length = 0;
  		queue_complete_req(req);
  	}
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1965
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1966
1967
1968
1969
  }
  
  static int get_config_rom(struct file_info *fi, struct pending_request *req)
  {
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
1970
  	int ret = 0;
e94b17660   Christoph Lameter   [PATCH] slab: rem...
1971
  	quadlet_t *data = kmalloc(req->req.length, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
  	int status;
  
  	if (!data)
  		return -ENOMEM;
  
  	status =
  	    csr1212_read(fi->host->csr.rom, CSR1212_CONFIG_ROM_SPACE_OFFSET,
  			 data, req->req.length);
  	if (copy_to_user(int2ptr(req->req.recvb), data, req->req.length))
  		ret = -EFAULT;
  	if (copy_to_user
  	    (int2ptr(req->req.tag), &fi->host->csr.rom->cache_head->len,
  	     sizeof(fi->host->csr.rom->cache_head->len)))
  		ret = -EFAULT;
  	if (copy_to_user(int2ptr(req->req.address), &fi->host->csr.generation,
  			 sizeof(fi->host->csr.generation)))
  		ret = -EFAULT;
  	if (copy_to_user(int2ptr(req->req.sendb), &status, sizeof(status)))
  		ret = -EFAULT;
  	kfree(data);
  	if (ret >= 0) {
  		free_pending_request(req);	/* we have to free the request, because we queue no response, and therefore nobody will free it */
  	}
  	return ret;
  }
  
  static int update_config_rom(struct file_info *fi, struct pending_request *req)
  {
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2000
  	int ret = 0;
e94b17660   Christoph Lameter   [PATCH] slab: rem...
2001
  	quadlet_t *data = kmalloc(req->req.length, GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
  	if (!data)
  		return -ENOMEM;
  	if (copy_from_user(data, int2ptr(req->req.sendb), req->req.length)) {
  		ret = -EFAULT;
  	} else {
  		int status = hpsb_update_config_rom(fi->host,
  						    data, req->req.length,
  						    (unsigned char)req->req.
  						    misc);
  		if (copy_to_user
  		    (int2ptr(req->req.recvb), &status, sizeof(status)))
  			ret = -ENOMEM;
  	}
  	kfree(data);
  	if (ret >= 0) {
  		free_pending_request(req);	/* we have to free the request, because we queue no response, and therefore nobody will free it */
  		fi->cfgrom_upd = 1;
  	}
  	return ret;
  }
  
  static int modify_config_rom(struct file_info *fi, struct pending_request *req)
  {
  	struct csr1212_keyval *kv;
  	struct csr1212_csr_rom_cache *cache;
  	struct csr1212_dentry *dentry;
  	u32 dr;
  	int ret = 0;
  
  	if (req->req.misc == ~0) {
  		if (req->req.length == 0)
  			return -EINVAL;
  
  		/* Find an unused slot */
  		for (dr = 0;
  		     dr < RAW1394_MAX_USER_CSR_DIRS && fi->csr1212_dirs[dr];
  		     dr++) ;
  
  		if (dr == RAW1394_MAX_USER_CSR_DIRS)
  			return -ENOMEM;
  
  		fi->csr1212_dirs[dr] =
  		    csr1212_new_directory(CSR1212_KV_ID_VENDOR);
  		if (!fi->csr1212_dirs[dr])
  			return -ENOMEM;
  	} else {
  		dr = req->req.misc;
  		if (!fi->csr1212_dirs[dr])
  			return -EINVAL;
  
  		/* Delete old stuff */
  		for (dentry =
  		     fi->csr1212_dirs[dr]->value.directory.dentries_head;
  		     dentry; dentry = dentry->next) {
  			csr1212_detach_keyval_from_directory(fi->host->csr.rom->
  							     root_kv,
  							     dentry->kv);
  		}
  
  		if (req->req.length == 0) {
  			csr1212_release_keyval(fi->csr1212_dirs[dr]);
  			fi->csr1212_dirs[dr] = NULL;
  
  			hpsb_update_config_rom_image(fi->host);
  			free_pending_request(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2067
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2068
2069
2070
2071
2072
2073
2074
2075
2076
  		}
  	}
  
  	cache = csr1212_rom_cache_malloc(0, req->req.length);
  	if (!cache) {
  		csr1212_release_keyval(fi->csr1212_dirs[dr]);
  		fi->csr1212_dirs[dr] = NULL;
  		return -ENOMEM;
  	}
8551158ab   Stefan Richter   kmalloc/kzalloc c...
2077
  	cache->filled_head = kmalloc(sizeof(*cache->filled_head), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
  	if (!cache->filled_head) {
  		csr1212_release_keyval(fi->csr1212_dirs[dr]);
  		fi->csr1212_dirs[dr] = NULL;
  		CSR1212_FREE(cache);
  		return -ENOMEM;
  	}
  	cache->filled_tail = cache->filled_head;
  
  	if (copy_from_user(cache->data, int2ptr(req->req.sendb),
  			   req->req.length)) {
  		csr1212_release_keyval(fi->csr1212_dirs[dr]);
  		fi->csr1212_dirs[dr] = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
  		ret = -EFAULT;
  	} else {
  		cache->len = req->req.length;
  		cache->filled_head->offset_start = 0;
  		cache->filled_head->offset_end = cache->size - 1;
  
  		cache->layout_head = cache->layout_tail = fi->csr1212_dirs[dr];
  
  		ret = CSR1212_SUCCESS;
  		/* parse all the items */
  		for (kv = cache->layout_head; ret == CSR1212_SUCCESS && kv;
  		     kv = kv->next) {
  			ret = csr1212_parse_keyval(kv, cache);
  		}
  
  		/* attach top level items to the root directory */
  		for (dentry =
  		     fi->csr1212_dirs[dr]->value.directory.dentries_head;
  		     ret == CSR1212_SUCCESS && dentry; dentry = dentry->next) {
  			ret =
  			    csr1212_attach_keyval_to_directory(fi->host->csr.
  							       rom->root_kv,
  							       dentry->kv);
  		}
  
  		if (ret == CSR1212_SUCCESS) {
  			ret = hpsb_update_config_rom_image(fi->host);
  
  			if (ret >= 0 && copy_to_user(int2ptr(req->req.recvb),
  						     &dr, sizeof(dr))) {
  				ret = -ENOMEM;
  			}
  		}
  	}
  	kfree(cache->filled_head);
b12479ddc   Stefan Richter   raw1394: fix memo...
2125
  	CSR1212_FREE(cache);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2126
2127
2128
2129
2130
  
  	if (ret >= 0) {
  		/* we have to free the request, because we queue no response,
  		 * and therefore nobody will free it */
  		free_pending_request(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2131
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
  	} else {
  		for (dentry =
  		     fi->csr1212_dirs[dr]->value.directory.dentries_head;
  		     dentry; dentry = dentry->next) {
  			csr1212_detach_keyval_from_directory(fi->host->csr.rom->
  							     root_kv,
  							     dentry->kv);
  		}
  		csr1212_release_keyval(fi->csr1212_dirs[dr]);
  		fi->csr1212_dirs[dr] = NULL;
  		return ret;
  	}
  }
  
  static int state_connected(struct file_info *fi, struct pending_request *req)
  {
  	int node = req->req.address >> 48;
  
  	req->req.error = RAW1394_ERROR_NONE;
  
  	switch (req->req.type) {
  
  	case RAW1394_REQ_ECHO:
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2156
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2157

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
  	case RAW1394_REQ_ARM_REGISTER:
  		return arm_register(fi, req);
  
  	case RAW1394_REQ_ARM_UNREGISTER:
  		return arm_unregister(fi, req);
  
  	case RAW1394_REQ_ARM_SET_BUF:
  		return arm_set_buf(fi, req);
  
  	case RAW1394_REQ_ARM_GET_BUF:
  		return arm_get_buf(fi, req);
  
  	case RAW1394_REQ_RESET_NOTIFY:
  		return reset_notification(fi, req);
53c96b417   Stefan Richter   ieee1394: remove ...
2172
  	case RAW1394_REQ_ISO_SEND:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2173
  	case RAW1394_REQ_ISO_LISTEN:
53c96b417   Stefan Richter   ieee1394: remove ...
2174
2175
2176
2177
2178
  		printk(KERN_DEBUG "raw1394: old iso ABI has been removed
  ");
  		req->req.error = RAW1394_ERROR_COMPAT;
  		req->req.misc = RAW1394_KERNELAPI_VERSION;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2179
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2180
2181
2182
  
  	case RAW1394_REQ_FCP_LISTEN:
  		handle_fcp_listen(fi, req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2183
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2184
2185
2186
2187
2188
2189
  
  	case RAW1394_REQ_RESET_BUS:
  		if (req->req.misc == RAW1394_LONG_RESET) {
  			DBGMSG("busreset called (type: LONG)");
  			hpsb_reset_bus(fi->host, LONG_RESET);
  			free_pending_request(req);	/* we have to free the request, because we queue no response, and therefore nobody will free it */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2190
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2191
2192
2193
2194
2195
  		}
  		if (req->req.misc == RAW1394_SHORT_RESET) {
  			DBGMSG("busreset called (type: SHORT)");
  			hpsb_reset_bus(fi->host, SHORT_RESET);
  			free_pending_request(req);	/* we have to free the request, because we queue no response, and therefore nobody will free it */
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2196
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
  		}
  		/* error EINVAL (22) invalid argument */
  		return (-EINVAL);
  	case RAW1394_REQ_GET_ROM:
  		return get_config_rom(fi, req);
  
  	case RAW1394_REQ_UPDATE_ROM:
  		return update_config_rom(fi, req);
  
  	case RAW1394_REQ_MODIFY_ROM:
  		return modify_config_rom(fi, req);
  	}
  
  	if (req->req.generation != get_hpsb_generation(fi->host)) {
  		req->req.error = RAW1394_ERROR_GENERATION;
  		req->req.generation = get_hpsb_generation(fi->host);
  		req->req.length = 0;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2215
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
  	}
  
  	switch (req->req.type) {
  	case RAW1394_REQ_PHYPACKET:
  		return write_phypacket(fi, req);
  	case RAW1394_REQ_ASYNC_SEND:
  		return handle_async_send(fi, req);
  	}
  
  	if (req->req.length == 0) {
  		req->req.error = RAW1394_ERROR_INVALID_ARG;
  		queue_complete_req(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2228
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2229
2230
2231
2232
2233
2234
2235
2236
  	}
  
  	return handle_async_request(fi, req, node);
  }
  
  static ssize_t raw1394_write(struct file *file, const char __user * buffer,
  			     size_t count, loff_t * offset_is_ignored)
  {
80792d182   Joe Perches   ieee1394: Remove ...
2237
  	struct file_info *fi = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2238
  	struct pending_request *req;
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2239
  	ssize_t retval = -EBADFD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2240

4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
2241
  #ifdef CONFIG_COMPAT
7597028a8   Ben Collins   raw1394: fix whit...
2242
2243
2244
  	if (count == sizeof(struct compat_raw1394_req) &&
     	    sizeof(struct compat_raw1394_req) !=
  			sizeof(struct raw1394_request)) {
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
2245
  		buffer = raw1394_compat_write(buffer);
421696887   Stefan Richter   ieee1394: raw1394...
2246
2247
  		if (IS_ERR((__force void *)buffer))
  			return PTR_ERR((__force void *)buffer);
4bc32c4d5   Andi Kleen   [PATCH] x86_64: I...
2248
2249
  	} else
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
  	if (count != sizeof(struct raw1394_request)) {
  		return -EINVAL;
  	}
  
  	req = alloc_pending_request();
  	if (req == NULL) {
  		return -ENOMEM;
  	}
  	req->file_info = fi;
  
  	if (copy_from_user(&req->req, buffer, sizeof(struct raw1394_request))) {
  		free_pending_request(req);
  		return -EFAULT;
  	}
64549e935   Michael Buesch   ieee1394: raw1394...
2264
2265
  	if (!mutex_trylock(&fi->state_mutex)) {
  		free_pending_request(req);
638570b54   Stefan Richter   ieee1394: raw1394...
2266
  		return -EAGAIN;
64549e935   Michael Buesch   ieee1394: raw1394...
2267
  	}
f22e52b89   Stefan Richter   ieee1394: raw1394...
2268

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
  	switch (fi->state) {
  	case opened:
  		retval = state_opened(fi, req);
  		break;
  
  	case initialized:
  		retval = state_initialized(fi, req);
  		break;
  
  	case connected:
  		retval = state_connected(fi, req);
  		break;
  	}
f22e52b89   Stefan Richter   ieee1394: raw1394...
2282
  	mutex_unlock(&fi->state_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2283
2284
  	if (retval < 0) {
  		free_pending_request(req);
883b97eaf   Petr Vandrovec   ieee1394: raw1394...
2285
2286
2287
  	} else {
  		BUG_ON(retval);
  		retval = count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
  	}
  
  	return retval;
  }
  
  /* rawiso operations */
  
  /* check if any RAW1394_REQ_RAWISO_ACTIVITY event is already in the
   * completion queue (reqlists_lock must be taken) */
  static inline int __rawiso_event_in_queue(struct file_info *fi)
  {
  	struct pending_request *req;
  
  	list_for_each_entry(req, &fi->req_complete, list)
  	    if (req->req.type == RAW1394_REQ_RAWISO_ACTIVITY)
  		return 1;
  
  	return 0;
  }
  
  /* put a RAWISO_ACTIVITY event in the queue, if one isn't there already */
  static void queue_rawiso_event(struct file_info *fi)
  {
  	unsigned long flags;
  
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
  
  	/* only one ISO activity event may be in the queue */
  	if (!__rawiso_event_in_queue(fi)) {
  		struct pending_request *req =
54e6ecb23   Christoph Lameter   [PATCH] slab: rem...
2318
  		    __alloc_pending_request(GFP_ATOMIC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
  
  		if (req) {
  			req->file_info = fi;
  			req->req.type = RAW1394_REQ_RAWISO_ACTIVITY;
  			req->req.generation = get_hpsb_generation(fi->host);
  			__queue_complete_req(req);
  		} else {
  			/* on allocation failure, signal an overflow */
  			if (fi->iso_handle) {
  				atomic_inc(&fi->iso_handle->overflows);
  			}
  		}
  	}
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
  }
  
  static void rawiso_activity_cb(struct hpsb_iso *iso)
  {
  	unsigned long flags;
  	struct host_info *hi;
  	struct file_info *fi;
  
  	spin_lock_irqsave(&host_info_lock, flags);
  	hi = find_host_info(iso->host);
  
  	if (hi != NULL) {
  		list_for_each_entry(fi, &hi->file_info_list, list) {
  			if (fi->iso_handle == iso)
  				queue_rawiso_event(fi);
  		}
  	}
  
  	spin_unlock_irqrestore(&host_info_lock, flags);
  }
  
  /* helper function - gather all the kernel iso status bits for returning to user-space */
  static void raw1394_iso_fill_status(struct hpsb_iso *iso,
  				    struct raw1394_iso_status *stat)
  {
cc9429bcb   Pieter Palmers   ieee1394: rawiso:...
2358
2359
  	int overflows = atomic_read(&iso->overflows);
  	int skips = atomic_read(&iso->skips);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2360
2361
2362
2363
2364
2365
  	stat->config.data_buf_size = iso->buf_size;
  	stat->config.buf_packets = iso->buf_packets;
  	stat->config.channel = iso->channel;
  	stat->config.speed = iso->speed;
  	stat->config.irq_interval = iso->irq_interval;
  	stat->n_packets = hpsb_iso_n_ready(iso);
cc9429bcb   Pieter Palmers   ieee1394: rawiso:...
2366
  	stat->overflows = ((skips & 0xFFFF) << 16) | ((overflows & 0xFFFF));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
  	stat->xmit_cycle = iso->xmit_cycle;
  }
  
  static int raw1394_iso_xmit_init(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_iso_status stat;
  
  	if (!fi->host)
  		return -EINVAL;
  
  	if (copy_from_user(&stat, uaddr, sizeof(stat)))
  		return -EFAULT;
  
  	fi->iso_handle = hpsb_iso_xmit_init(fi->host,
  					    stat.config.data_buf_size,
  					    stat.config.buf_packets,
  					    stat.config.channel,
  					    stat.config.speed,
  					    stat.config.irq_interval,
  					    rawiso_activity_cb);
  	if (!fi->iso_handle)
  		return -ENOMEM;
  
  	fi->iso_state = RAW1394_ISO_XMIT;
  
  	raw1394_iso_fill_status(fi->iso_handle, &stat);
  	if (copy_to_user(uaddr, &stat, sizeof(stat)))
  		return -EFAULT;
  
  	/* queue an event to get things started */
  	rawiso_activity_cb(fi->iso_handle);
  
  	return 0;
  }
  
  static int raw1394_iso_recv_init(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_iso_status stat;
  
  	if (!fi->host)
  		return -EINVAL;
  
  	if (copy_from_user(&stat, uaddr, sizeof(stat)))
  		return -EFAULT;
  
  	fi->iso_handle = hpsb_iso_recv_init(fi->host,
  					    stat.config.data_buf_size,
  					    stat.config.buf_packets,
  					    stat.config.channel,
  					    stat.config.dma_mode,
  					    stat.config.irq_interval,
  					    rawiso_activity_cb);
  	if (!fi->iso_handle)
  		return -ENOMEM;
  
  	fi->iso_state = RAW1394_ISO_RECV;
  
  	raw1394_iso_fill_status(fi->iso_handle, &stat);
  	if (copy_to_user(uaddr, &stat, sizeof(stat)))
  		return -EFAULT;
  	return 0;
  }
  
  static int raw1394_iso_get_status(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_iso_status stat;
  	struct hpsb_iso *iso = fi->iso_handle;
  
  	raw1394_iso_fill_status(fi->iso_handle, &stat);
  	if (copy_to_user(uaddr, &stat, sizeof(stat)))
  		return -EFAULT;
  
  	/* reset overflow counter */
  	atomic_set(&iso->overflows, 0);
cc9429bcb   Pieter Palmers   ieee1394: rawiso:...
2441
2442
  	/* reset skip counter */
  	atomic_set(&iso->skips, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
  
  	return 0;
  }
  
  /* copy N packet_infos out of the ringbuffer into user-supplied array */
  static int raw1394_iso_recv_packets(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_iso_packets upackets;
  	unsigned int packet = fi->iso_handle->first_packet;
  	int i;
  
  	if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
  		return -EFAULT;
  
  	if (upackets.n_packets > hpsb_iso_n_ready(fi->iso_handle))
  		return -EINVAL;
  
  	/* ensure user-supplied buffer is accessible and big enough */
  	if (!access_ok(VERIFY_WRITE, upackets.infos,
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
2462
2463
  		       upackets.n_packets *
  		       sizeof(struct raw1394_iso_packet_info)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
  		return -EFAULT;
  
  	/* copy the packet_infos out */
  	for (i = 0; i < upackets.n_packets; i++) {
  		if (__copy_to_user(&upackets.infos[i],
  				   &fi->iso_handle->infos[packet],
  				   sizeof(struct raw1394_iso_packet_info)))
  			return -EFAULT;
  
  		packet = (packet + 1) % fi->iso_handle->buf_packets;
  	}
  
  	return 0;
  }
  
  /* copy N packet_infos from user to ringbuffer, and queue them for transmission */
  static int raw1394_iso_send_packets(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_iso_packets upackets;
  	int i, rv;
  
  	if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
  		return -EFAULT;
1934b8b65   Ben Collins   [PATCH] Sync up i...
2487
  	if (upackets.n_packets >= fi->iso_handle->buf_packets)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2488
  		return -EINVAL;
1934b8b65   Ben Collins   [PATCH] Sync up i...
2489
2490
  	if (upackets.n_packets >= hpsb_iso_n_ready(fi->iso_handle))
  		return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2491
2492
  	/* ensure user-supplied buffer is accessible and big enough */
  	if (!access_ok(VERIFY_READ, upackets.infos,
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
2493
2494
  		       upackets.n_packets *
  		       sizeof(struct raw1394_iso_packet_info)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
  		return -EFAULT;
  
  	/* copy the infos structs in and queue the packets */
  	for (i = 0; i < upackets.n_packets; i++) {
  		struct raw1394_iso_packet_info info;
  
  		if (__copy_from_user(&info, &upackets.infos[i],
  				     sizeof(struct raw1394_iso_packet_info)))
  			return -EFAULT;
  
  		rv = hpsb_iso_xmit_queue_packet(fi->iso_handle, info.offset,
  						info.len, info.tag, info.sy);
  		if (rv)
  			return rv;
  	}
  
  	return 0;
  }
  
  static void raw1394_iso_shutdown(struct file_info *fi)
  {
  	if (fi->iso_handle)
  		hpsb_iso_shutdown(fi->iso_handle);
  
  	fi->iso_handle = NULL;
  	fi->iso_state = RAW1394_ISO_INACTIVE;
  }
3dc5ea9b3   Pieter Palmers   ieee1394: cycle t...
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
  static int raw1394_read_cycle_timer(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_cycle_timer ct;
  	int err;
  
  	err = hpsb_read_cycle_timer(fi->host, &ct.cycle_timer, &ct.local_time);
  	if (!err)
  		if (copy_to_user(uaddr, &ct, sizeof(ct)))
  			err = -EFAULT;
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2533
2534
2535
2536
  /* mmap the rawiso xmit/recv buffer */
  static int raw1394_mmap(struct file *file, struct vm_area_struct *vma)
  {
  	struct file_info *fi = file->private_data;
10963ea1b   Stefan Richter   ieee1394: raw1394...
2537
  	int ret;
638570b54   Stefan Richter   ieee1394: raw1394...
2538
2539
  	if (!mutex_trylock(&fi->state_mutex))
  		return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2540
2541
  
  	if (fi->iso_state == RAW1394_ISO_INACTIVE)
10963ea1b   Stefan Richter   ieee1394: raw1394...
2542
2543
2544
2545
2546
  		ret = -EINVAL;
  	else
  		ret = dma_region_mmap(&fi->iso_handle->data_buf, file, vma);
  
  	mutex_unlock(&fi->state_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2547

10963ea1b   Stefan Richter   ieee1394: raw1394...
2548
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2549
  }
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
  static long raw1394_ioctl_inactive(struct file_info *fi, unsigned int cmd,
  				   void __user *argp)
  {
  	switch (cmd) {
  	case RAW1394_IOC_ISO_XMIT_INIT:
  		return raw1394_iso_xmit_init(fi, argp);
  	case RAW1394_IOC_ISO_RECV_INIT:
  		return raw1394_iso_recv_init(fi, argp);
  	default:
  		return -EINVAL;
  	}
  }
  
  static long raw1394_ioctl_recv(struct file_info *fi, unsigned int cmd,
  			       unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2565
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2566
  	void __user *argp = (void __user *)arg;
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2567
2568
2569
2570
2571
2572
2573
2574
  	switch (cmd) {
  	case RAW1394_IOC_ISO_RECV_START:{
  			int args[3];
  
  			if (copy_from_user(&args[0], argp, sizeof(args)))
  				return -EFAULT;
  			return hpsb_iso_recv_start(fi->iso_handle,
  						   args[0], args[1], args[2]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2575
  		}
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
  	case RAW1394_IOC_ISO_XMIT_RECV_STOP:
  		hpsb_iso_stop(fi->iso_handle);
  		return 0;
  	case RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL:
  		return hpsb_iso_recv_listen_channel(fi->iso_handle, arg);
  	case RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL:
  		return hpsb_iso_recv_unlisten_channel(fi->iso_handle, arg);
  	case RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK:{
  			u64 mask;
  
  			if (copy_from_user(&mask, argp, sizeof(mask)))
  				return -EFAULT;
  			return hpsb_iso_recv_set_channel_mask(fi->iso_handle,
  							      mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2590
  		}
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
  	case RAW1394_IOC_ISO_GET_STATUS:
  		return raw1394_iso_get_status(fi, argp);
  	case RAW1394_IOC_ISO_RECV_PACKETS:
  		return raw1394_iso_recv_packets(fi, argp);
  	case RAW1394_IOC_ISO_RECV_RELEASE_PACKETS:
  		return hpsb_iso_recv_release_packets(fi->iso_handle, arg);
  	case RAW1394_IOC_ISO_RECV_FLUSH:
  		return hpsb_iso_recv_flush(fi->iso_handle);
  	case RAW1394_IOC_ISO_SHUTDOWN:
  		raw1394_iso_shutdown(fi);
  		return 0;
  	case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
  		queue_rawiso_event(fi);
  		return 0;
  	default:
  		return -EINVAL;
  	}
  }
  
  static long raw1394_ioctl_xmit(struct file_info *fi, unsigned int cmd,
  			       void __user *argp)
  {
  	switch (cmd) {
  	case RAW1394_IOC_ISO_XMIT_START:{
  			int args[2];
  
  			if (copy_from_user(&args[0], argp, sizeof(args)))
  				return -EFAULT;
  			return hpsb_iso_xmit_start(fi->iso_handle,
  						   args[0], args[1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2621
  		}
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
  	case RAW1394_IOC_ISO_XMIT_SYNC:
  		return hpsb_iso_xmit_sync(fi->iso_handle);
  	case RAW1394_IOC_ISO_XMIT_RECV_STOP:
  		hpsb_iso_stop(fi->iso_handle);
  		return 0;
  	case RAW1394_IOC_ISO_GET_STATUS:
  		return raw1394_iso_get_status(fi, argp);
  	case RAW1394_IOC_ISO_XMIT_PACKETS:
  		return raw1394_iso_send_packets(fi, argp);
  	case RAW1394_IOC_ISO_SHUTDOWN:
  		raw1394_iso_shutdown(fi);
  		return 0;
  	case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
  		queue_rawiso_event(fi);
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2637
  	default:
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2638
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2639
  	}
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2640
2641
2642
2643
2644
2645
2646
2647
2648
  }
  
  /* ioctl is only used for rawiso operations */
  static long raw1394_ioctl(struct file *file, unsigned int cmd,
  			  unsigned long arg)
  {
  	struct file_info *fi = file->private_data;
  	void __user *argp = (void __user *)arg;
  	long ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2649

3dc5ea9b3   Pieter Palmers   ieee1394: cycle t...
2650
2651
2652
2653
2654
2655
2656
  	/* state-independent commands */
  	switch(cmd) {
  	case RAW1394_IOC_GET_CYCLE_TIMER:
  		return raw1394_read_cycle_timer(fi, argp);
  	default:
  		break;
  	}
638570b54   Stefan Richter   ieee1394: raw1394...
2657
2658
  	if (!mutex_trylock(&fi->state_mutex))
  		return -EAGAIN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2659

ddfb908d3   Stefan Richter   ieee1394: raw1394...
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
  	switch (fi->iso_state) {
  	case RAW1394_ISO_INACTIVE:
  		ret = raw1394_ioctl_inactive(fi, cmd, argp);
  		break;
  	case RAW1394_ISO_RECV:
  		ret = raw1394_ioctl_recv(fi, cmd, arg);
  		break;
  	case RAW1394_ISO_XMIT:
  		ret = raw1394_ioctl_xmit(fi, cmd, argp);
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
10963ea1b   Stefan Richter   ieee1394: raw1394...
2674

10963ea1b   Stefan Richter   ieee1394: raw1394...
2675
  	mutex_unlock(&fi->state_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2676

3aea50a37   Alan Cox   ieee1394: raw1394...
2677
2678
  	return ret;
  }
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2679
2680
2681
2682
2683
2684
2685
2686
2687
  #ifdef CONFIG_COMPAT
  struct raw1394_iso_packets32 {
          __u32 n_packets;
          compat_uptr_t infos;
  } __attribute__((packed));
  
  struct raw1394_cycle_timer32 {
          __u32 cycle_timer;
          __u64 local_time;
19f00e66f   Stefan Richter   ieee1394: raw1394...
2688
2689
2690
2691
2692
  }
  #if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
  __attribute__((packed))
  #endif
  ;
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
  
  #define RAW1394_IOC_ISO_RECV_PACKETS32          \
          _IOW ('#', 0x25, struct raw1394_iso_packets32)
  #define RAW1394_IOC_ISO_XMIT_PACKETS32          \
          _IOW ('#', 0x27, struct raw1394_iso_packets32)
  #define RAW1394_IOC_GET_CYCLE_TIMER32           \
          _IOR ('#', 0x30, struct raw1394_cycle_timer32)
  
  static long raw1394_iso_xmit_recv_packets32(struct file *file, unsigned int cmd,
                                            struct raw1394_iso_packets32 __user *arg)
  {
  	compat_uptr_t infos32;
5b26e64ea   Al Viro   raw1394 __user an...
2705
  	void __user *infos;
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2706
2707
2708
2709
2710
2711
2712
  	long err = -EFAULT;
  	struct raw1394_iso_packets __user *dst = compat_alloc_user_space(sizeof(struct raw1394_iso_packets));
  
  	if (!copy_in_user(&dst->n_packets, &arg->n_packets, sizeof arg->n_packets) &&
  	    !copy_from_user(&infos32, &arg->infos, sizeof infos32)) {
  		infos = compat_ptr(infos32);
  		if (!copy_to_user(&dst->infos, &infos, sizeof infos))
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2713
  			err = raw1394_ioctl(file, cmd, (unsigned long)dst);
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
  	}
  	return err;
  }
  
  static long raw1394_read_cycle_timer32(struct file_info *fi, void __user * uaddr)
  {
  	struct raw1394_cycle_timer32 ct;
  	int err;
  
  	err = hpsb_read_cycle_timer(fi->host, &ct.cycle_timer, &ct.local_time);
  	if (!err)
  		if (copy_to_user(uaddr, &ct, sizeof(ct)))
  			err = -EFAULT;
  	return err;
  }
  
  static long raw1394_compat_ioctl(struct file *file,
  				 unsigned int cmd, unsigned long arg)
  {
  	struct file_info *fi = file->private_data;
  	void __user *argp = (void __user *)arg;
  	long err;
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
  	switch (cmd) {
  	/* These requests have same format as long as 'int' has same size. */
  	case RAW1394_IOC_ISO_RECV_INIT:
  	case RAW1394_IOC_ISO_RECV_START:
  	case RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL:
  	case RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL:
  	case RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK:
  	case RAW1394_IOC_ISO_RECV_RELEASE_PACKETS:
  	case RAW1394_IOC_ISO_RECV_FLUSH:
  	case RAW1394_IOC_ISO_XMIT_RECV_STOP:
  	case RAW1394_IOC_ISO_XMIT_INIT:
  	case RAW1394_IOC_ISO_XMIT_START:
  	case RAW1394_IOC_ISO_XMIT_SYNC:
  	case RAW1394_IOC_ISO_GET_STATUS:
  	case RAW1394_IOC_ISO_SHUTDOWN:
  	case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
ddfb908d3   Stefan Richter   ieee1394: raw1394...
2752
  		err = raw1394_ioctl(file, cmd, arg);
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
  		break;
  	/* These request have different format. */
  	case RAW1394_IOC_ISO_RECV_PACKETS32:
  		err = raw1394_iso_xmit_recv_packets32(file, RAW1394_IOC_ISO_RECV_PACKETS, argp);
  		break;
  	case RAW1394_IOC_ISO_XMIT_PACKETS32:
  		err = raw1394_iso_xmit_recv_packets32(file, RAW1394_IOC_ISO_XMIT_PACKETS, argp);
  		break;
  	case RAW1394_IOC_GET_CYCLE_TIMER32:
  		err = raw1394_read_cycle_timer32(fi, argp);
  		break;
  	default:
  		err = -EINVAL;
  		break;
  	}
650c12c52   Petr Vandrovec   ieee1394: raw1394...
2768
2769
2770
2771
  
  	return err;
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2772
2773
2774
2775
  static unsigned int raw1394_poll(struct file *file, poll_table * pt)
  {
  	struct file_info *fi = file->private_data;
  	unsigned int mask = POLLOUT | POLLWRNORM;
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2776
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2777

45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2778
  	poll_wait(file, &fi->wait_complete, pt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2779

4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2780
  	spin_lock_irqsave(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2781
2782
2783
  	if (!list_empty(&fi->req_complete)) {
  		mask |= POLLIN | POLLRDNORM;
  	}
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2784
  	spin_unlock_irqrestore(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2785
2786
2787
2788
2789
2790
2791
  
  	return mask;
  }
  
  static int raw1394_open(struct inode *inode, struct file *file)
  {
  	struct file_info *fi;
e94b17660   Christoph Lameter   [PATCH] slab: rem...
2792
  	fi = kzalloc(sizeof(*fi), GFP_KERNEL);
8551158ab   Stefan Richter   kmalloc/kzalloc c...
2793
  	if (!fi)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2794
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2795
2796
2797
  	fi->notification = (u8) RAW1394_NOTIFY_ON;	/* busreset notification */
  
  	INIT_LIST_HEAD(&fi->list);
10963ea1b   Stefan Richter   ieee1394: raw1394...
2798
  	mutex_init(&fi->state_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2799
2800
2801
  	fi->state = opened;
  	INIT_LIST_HEAD(&fi->req_pending);
  	INIT_LIST_HEAD(&fi->req_complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2802
  	spin_lock_init(&fi->reqlists_lock);
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2803
  	init_waitqueue_head(&fi->wait_complete);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2804
2805
2806
  	INIT_LIST_HEAD(&fi->addr_list);
  
  	file->private_data = fi;
7cfe21aae   Stefan Richter   ieee1394: mark ch...
2807
  	return nonseekable_open(inode, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2808
2809
2810
2811
2812
2813
2814
  }
  
  static int raw1394_release(struct inode *inode, struct file *file)
  {
  	struct file_info *fi = file->private_data;
  	struct list_head *lh;
  	struct pending_request *req;
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2815
  	int i, fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2816
2817
2818
2819
2820
2821
2822
2823
  	int retval = 0;
  	struct list_head *entry;
  	struct arm_addr *addr = NULL;
  	struct host_info *hi;
  	struct file_info *fi_hlp = NULL;
  	struct arm_addr *arm_addr = NULL;
  	int another_host;
  	int csr_mod = 0;
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2824
  	unsigned long flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2825
2826
2827
  
  	if (fi->iso_state != RAW1394_ISO_INACTIVE)
  		raw1394_iso_shutdown(fi);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2828
  	spin_lock_irqsave(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2829
2830
2831
  
  	fail = 0;
  	/* set address-entries invalid */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
  
  	while (!list_empty(&fi->addr_list)) {
  		another_host = 0;
  		lh = fi->addr_list.next;
  		addr = list_entry(lh, struct arm_addr, addr_list);
  		/* another host with valid address-entry containing
  		   same addressrange? */
  		list_for_each_entry(hi, &host_info_list, list) {
  			if (hi->host != fi->host) {
  				list_for_each_entry(fi_hlp, &hi->file_info_list,
  						    list) {
  					entry = fi_hlp->addr_list.next;
  					while (entry != &(fi_hlp->addr_list)) {
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
2845
  						arm_addr = list_entry(entry, struct
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
  								      arm_addr,
  								      addr_list);
  						if (arm_addr->start ==
  						    addr->start) {
  							DBGMSG
  							    ("raw1394_release: "
  							     "another host ownes "
  							     "same addressrange");
  							another_host = 1;
  							break;
  						}
  						entry = entry->next;
  					}
  					if (another_host) {
  						break;
  					}
  				}
  			}
  		}
  		if (!another_host) {
  			DBGMSG("raw1394_release: call hpsb_arm_unregister");
  			retval =
  			    hpsb_unregister_addrspace(&raw1394_highlevel,
  						      fi->host, addr->start);
  			if (!retval) {
  				++fail;
  				printk(KERN_ERR
  				       "raw1394_release arm_Unregister failed
  ");
  			}
  		}
  		DBGMSG("raw1394_release: delete addr_entry from list");
  		list_del(&addr->addr_list);
  		vfree(addr->addr_space_buffer);
  		kfree(addr);
  	}			/* while */
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2882
  	spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2883
2884
2885
2886
2887
  	if (fail > 0) {
  		printk(KERN_ERR "raw1394: during addr_list-release "
  		       "error(s) occurred 
  ");
  	}
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2888
2889
2890
  	for (;;) {
  		/* This locked section guarantees that neither
  		 * complete nor pending requests exist once i!=0 */
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2891
  		spin_lock_irqsave(&fi->reqlists_lock, flags);
45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2892
  		while ((req = __next_complete_req(fi)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2893
  			free_pending_request(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2894

45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2895
  		i = list_empty(&fi->req_pending);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2896
  		spin_unlock_irqrestore(&fi->reqlists_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2897

45289bf6a   Stefan Richter   [PATCH] ieee1394:...
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
  		if (i)
  			break;
  		/*
  		 * Sleep until more requests can be freed.
  		 *
  		 * NB: We call the macro wait_event() with a condition argument
  		 * with side effect.  This is only possible because the side
  		 * effect does not occur until the condition became true, and
  		 * wait_event() won't evaluate the condition again after that.
  		 */
  		wait_event(fi->wait_complete, (req = next_complete_req(fi)));
  		free_pending_request(req);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
  	}
  
  	/* Remove any sub-trees left by user space programs */
  	for (i = 0; i < RAW1394_MAX_USER_CSR_DIRS; i++) {
  		struct csr1212_dentry *dentry;
  		if (!fi->csr1212_dirs[i])
  			continue;
  		for (dentry =
  		     fi->csr1212_dirs[i]->value.directory.dentries_head; dentry;
  		     dentry = dentry->next) {
  			csr1212_detach_keyval_from_directory(fi->host->csr.rom->
  							     root_kv,
  							     dentry->kv);
  		}
  		csr1212_release_keyval(fi->csr1212_dirs[i]);
  		fi->csr1212_dirs[i] = NULL;
  		csr_mod = 1;
  	}
  
  	if ((csr_mod || fi->cfgrom_upd)
  	    && hpsb_update_config_rom_image(fi->host) < 0)
  		HPSB_ERR
  		    ("Failed to generate Configuration ROM image for host %d",
  		     fi->host->id);
  
  	if (fi->state == connected) {
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2936
  		spin_lock_irqsave(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2937
  		list_del(&fi->list);
4a9949d7a   Andy Wingo   [PATCH] raw1394: ...
2938
  		spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2939
2940
2941
  
  		put_device(&fi->host->device);
  	}
0fe4c6fca   Stefan Richter   ieee1394: raw1394...
2942
2943
2944
2945
  	spin_lock_irqsave(&host_info_lock, flags);
  	if (fi->host)
  		module_put(fi->host->driver->owner);
  	spin_unlock_irqrestore(&host_info_lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2946
2947
2948
2949
2950
2951
2952
2953
2954
  	kfree(fi);
  
  	return 0;
  }
  
  /*** HOTPLUG STUFF **********************************************************/
  /*
   * Export information about protocols/devices supported by this driver.
   */
e38649702   Tony Breeds   ieee1394: silence...
2955
  #ifdef MODULE
c64094684   Stefan Richter   ieee1394: constif...
2956
  static const struct ieee1394_device_id raw1394_id_table[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
  	{
  	 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  	 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY & 0xffffff,
  	 .version = AVC_SW_VERSION_ENTRY & 0xffffff},
  	{
  	 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  	 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
  	 .version = CAMERA_SW_VERSION_ENTRY & 0xffffff},
  	{
  	 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  	 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
  	 .version = (CAMERA_SW_VERSION_ENTRY + 1) & 0xffffff},
  	{
  	 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
  	 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
  	 .version = (CAMERA_SW_VERSION_ENTRY + 2) & 0xffffff},
  	{}
  };
  
  MODULE_DEVICE_TABLE(ieee1394, raw1394_id_table);
e38649702   Tony Breeds   ieee1394: silence...
2977
  #endif /* MODULE */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2978
2979
  
  static struct hpsb_protocol_driver raw1394_driver = {
ed30c26ee   Ben Collins   ieee1394: Consoli...
2980
  	.name = "raw1394",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2981
2982
2983
2984
2985
2986
2987
2988
2989
  };
  
  /******************************************************************************/
  
  static struct hpsb_highlevel raw1394_highlevel = {
  	.name = RAW1394_DEVICE_NAME,
  	.add_host = add_host,
  	.remove_host = remove_host,
  	.host_reset = host_reset,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2990
2991
2992
2993
  	.fcp_request = fcp_request,
  };
  
  static struct cdev raw1394_cdev;
2b8693c06   Arjan van de Ven   [PATCH] mark stru...
2994
  static const struct file_operations raw1394_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2995
2996
2997
2998
  	.owner = THIS_MODULE,
  	.read = raw1394_read,
  	.write = raw1394_write,
  	.mmap = raw1394_mmap,
3aea50a37   Alan Cox   ieee1394: raw1394...
2999
  	.unlocked_ioctl = raw1394_ioctl,
650c12c52   Petr Vandrovec   ieee1394: raw1394...
3000
3001
3002
  #ifdef CONFIG_COMPAT
  	.compat_ioctl = raw1394_compat_ioctl,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3003
3004
3005
  	.poll = raw1394_poll,
  	.open = raw1394_open,
  	.release = raw1394_release,
7cfe21aae   Stefan Richter   ieee1394: mark ch...
3006
  	.llseek = no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3007
3008
3009
3010
3011
3012
3013
  };
  
  static int __init init_raw1394(void)
  {
  	int ret = 0;
  
  	hpsb_register_highlevel(&raw1394_highlevel);
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
3014
  	if (IS_ERR
6229df31b   Greg Kroah-Hartman   device create: ie...
3015
3016
3017
3018
  	    (device_create(hpsb_protocol_class, NULL,
  			   MKDEV(IEEE1394_MAJOR,
  				 IEEE1394_MINOR_BLOCK_RAW1394 * 16),
  			   NULL, RAW1394_DEVICE_NAME))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3019
3020
3021
  		ret = -EFAULT;
  		goto out_unreg;
  	}
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
3022

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3023
3024
  	cdev_init(&raw1394_cdev, &raw1394_fops);
  	raw1394_cdev.owner = THIS_MODULE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
  	ret = cdev_add(&raw1394_cdev, IEEE1394_RAW1394_DEV, 1);
  	if (ret) {
  		HPSB_ERR("raw1394 failed to register minor device block");
  		goto out_dev;
  	}
  
  	HPSB_INFO("raw1394: /dev/%s device initialized", RAW1394_DEVICE_NAME);
  
  	ret = hpsb_register_protocol(&raw1394_driver);
  	if (ret) {
  		HPSB_ERR("raw1394: failed to register protocol");
  		cdev_del(&raw1394_cdev);
  		goto out_dev;
  	}
  
  	goto out;
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
3041
        out_dev:
dd7f2928d   Kay Sievers   ieee1394: convert...
3042
3043
3044
  	device_destroy(hpsb_protocol_class,
  		       MKDEV(IEEE1394_MAJOR,
  			     IEEE1394_MINOR_BLOCK_RAW1394 * 16));
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
3045
        out_unreg:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3046
  	hpsb_unregister_highlevel(&raw1394_highlevel);
c64d472ab   Jens-Michael Hoffmann   ieee1394/raw1394:...
3047
        out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3048
3049
3050
3051
3052
  	return ret;
  }
  
  static void __exit cleanup_raw1394(void)
  {
dd7f2928d   Kay Sievers   ieee1394: convert...
3053
3054
3055
  	device_destroy(hpsb_protocol_class,
  		       MKDEV(IEEE1394_MAJOR,
  			     IEEE1394_MINOR_BLOCK_RAW1394 * 16));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3056
  	cdev_del(&raw1394_cdev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3057
3058
3059
3060
3061
3062
3063
  	hpsb_unregister_highlevel(&raw1394_highlevel);
  	hpsb_unregister_protocol(&raw1394_driver);
  }
  
  module_init(init_raw1394);
  module_exit(cleanup_raw1394);
  MODULE_LICENSE("GPL");