blob: 975ed7062aa10b1a14c3eb92ed25c8072717f4b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1394 for Linux
3 *
4 * Raw interface to the bus
5 *
6 * Copyright (C) 1999, 2000 Andreas E. Bombe
7 * 2001, 2002 Manfred Weihs <weihs@ict.tuwien.ac.at>
8 * 2002 Christian Toegel <christian.toegel@gmx.at>
9 *
10 * This code is licensed under the GPL. See the file COPYING in the root
11 * directory of the kernel sources for details.
12 *
13 *
14 * Contributions:
15 *
16 * Manfred Weihs <weihs@ict.tuwien.ac.at>
17 * configuration ROM manipulation
18 * address range mapping
19 * adaptation for new (transparent) loopback mechanism
20 * sending of arbitrary async packets
21 * Christian Toegel <christian.toegel@gmx.at>
22 * address range mapping
23 * lock64 request
24 * transmit physical packet
25 * busreset notification control (switch on/off)
26 * busreset with selection of type (short/long)
27 * request_reply
28 */
29
30#include <linux/kernel.h>
31#include <linux/list.h>
32#include <linux/string.h>
33#include <linux/slab.h>
34#include <linux/fs.h>
35#include <linux/poll.h>
36#include <linux/module.h>
Stefan Richter10963ea2008-08-16 00:11:48 +020037#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/vmalloc.h>
41#include <linux/cdev.h>
42#include <asm/uaccess.h>
43#include <asm/atomic.h>
Andi Kleen4bc32c42006-03-25 16:30:07 +010044#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "csr1212.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "highlevel.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040048#include "hosts.h"
49#include "ieee1394.h"
50#include "ieee1394_core.h"
51#include "ieee1394_hotplug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "ieee1394_transactions.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040053#include "ieee1394_types.h"
54#include "iso.h"
55#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "raw1394.h"
57#include "raw1394-private.h"
58
59#define int2ptr(x) ((void __user *)(unsigned long)x)
60#define ptr2int(x) ((u64)(unsigned long)(void __user *)x)
61
62#ifdef CONFIG_IEEE1394_VERBOSEDEBUG
63#define RAW1394_DEBUG
64#endif
65
66#ifdef RAW1394_DEBUG
67#define DBGMSG(fmt, args...) \
68printk(KERN_INFO "raw1394:" fmt "\n" , ## args)
69#else
Stefan Richter611aa192006-08-02 18:44:00 +020070#define DBGMSG(fmt, args...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#endif
72
73static LIST_HEAD(host_info_list);
74static int host_count;
75static DEFINE_SPINLOCK(host_info_lock);
76static atomic_t internal_generation = ATOMIC_INIT(0);
77
78static atomic_t iso_buffer_size;
79static const int iso_buffer_max = 4 * 1024 * 1024; /* 4 MB */
80
81static struct hpsb_highlevel raw1394_highlevel;
82
83static int arm_read(struct hpsb_host *host, int nodeid, quadlet_t * buffer,
84 u64 addr, size_t length, u16 flags);
85static int arm_write(struct hpsb_host *host, int nodeid, int destid,
86 quadlet_t * data, u64 addr, size_t length, u16 flags);
87static int arm_lock(struct hpsb_host *host, int nodeid, quadlet_t * store,
88 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
89 u16 flags);
90static int arm_lock64(struct hpsb_host *host, int nodeid, octlet_t * store,
91 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
92 u16 flags);
93static struct hpsb_address_ops arm_ops = {
94 .read = arm_read,
95 .write = arm_write,
96 .lock = arm_lock,
97 .lock64 = arm_lock64,
98};
99
100static void queue_complete_cb(struct pending_request *req);
101
Al Virodd0fc662005-10-07 07:46:04 +0100102static struct pending_request *__alloc_pending_request(gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 struct pending_request *req;
105
Stefan Richter85511582005-11-07 06:31:45 -0500106 req = kzalloc(sizeof(*req), flags);
107 if (req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 INIT_LIST_HEAD(&req->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 return req;
111}
112
113static inline struct pending_request *alloc_pending_request(void)
114{
Christoph Lametere94b1762006-12-06 20:33:17 -0800115 return __alloc_pending_request(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static void free_pending_request(struct pending_request *req)
119{
120 if (req->ibs) {
121 if (atomic_dec_and_test(&req->ibs->refcount)) {
122 atomic_sub(req->ibs->data_size, &iso_buffer_size);
123 kfree(req->ibs);
124 }
125 } else if (req->free_data) {
126 kfree(req->data);
127 }
128 hpsb_free_packet(req->packet);
129 kfree(req);
130}
131
132/* fi->reqlists_lock must be taken */
133static void __queue_complete_req(struct pending_request *req)
134{
135 struct file_info *fi = req->file_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Stefan Richter45289bf2006-07-03 12:02:33 -0400137 list_move_tail(&req->list, &fi->req_complete);
138 wake_up(&fi->wait_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141static void queue_complete_req(struct pending_request *req)
142{
143 unsigned long flags;
144 struct file_info *fi = req->file_info;
145
146 spin_lock_irqsave(&fi->reqlists_lock, flags);
147 __queue_complete_req(req);
148 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
149}
150
151static void queue_complete_cb(struct pending_request *req)
152{
153 struct hpsb_packet *packet = req->packet;
154 int rcode = (packet->header[1] >> 12) & 0xf;
155
156 switch (packet->ack_code) {
157 case ACKX_NONE:
158 case ACKX_SEND_ERROR:
159 req->req.error = RAW1394_ERROR_SEND_ERROR;
160 break;
161 case ACKX_ABORTED:
162 req->req.error = RAW1394_ERROR_ABORTED;
163 break;
164 case ACKX_TIMEOUT:
165 req->req.error = RAW1394_ERROR_TIMEOUT;
166 break;
167 default:
168 req->req.error = (packet->ack_code << 16) | rcode;
169 break;
170 }
171
172 if (!((packet->ack_code == ACK_PENDING) && (rcode == RCODE_COMPLETE))) {
173 req->req.length = 0;
174 }
175
176 if ((req->req.type == RAW1394_REQ_ASYNC_READ) ||
177 (req->req.type == RAW1394_REQ_ASYNC_WRITE) ||
178 (req->req.type == RAW1394_REQ_ASYNC_STREAM) ||
179 (req->req.type == RAW1394_REQ_LOCK) ||
180 (req->req.type == RAW1394_REQ_LOCK64))
181 hpsb_free_tlabel(packet);
182
183 queue_complete_req(req);
184}
185
186static void add_host(struct hpsb_host *host)
187{
188 struct host_info *hi;
189 unsigned long flags;
190
Stefan Richter85511582005-11-07 06:31:45 -0500191 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Stefan Richter85511582005-11-07 06:31:45 -0500193 if (hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 INIT_LIST_HEAD(&hi->list);
195 hi->host = host;
196 INIT_LIST_HEAD(&hi->file_info_list);
197
198 spin_lock_irqsave(&host_info_lock, flags);
199 list_add_tail(&hi->list, &host_info_list);
200 host_count++;
201 spin_unlock_irqrestore(&host_info_lock, flags);
202 }
203
204 atomic_inc(&internal_generation);
205}
206
207static struct host_info *find_host_info(struct hpsb_host *host)
208{
209 struct host_info *hi;
210
211 list_for_each_entry(hi, &host_info_list, list)
212 if (hi->host == host)
213 return hi;
214
215 return NULL;
216}
217
218static void remove_host(struct hpsb_host *host)
219{
220 struct host_info *hi;
221 unsigned long flags;
222
223 spin_lock_irqsave(&host_info_lock, flags);
224 hi = find_host_info(host);
225
226 if (hi != NULL) {
227 list_del(&hi->list);
228 host_count--;
229 /*
230 FIXME: address ranges should be removed
231 and fileinfo states should be initialized
232 (including setting generation to
233 internal-generation ...)
234 */
235 }
236 spin_unlock_irqrestore(&host_info_lock, flags);
237
238 if (hi == NULL) {
239 printk(KERN_ERR "raw1394: attempt to remove unknown host "
240 "0x%p\n", host);
241 return;
242 }
243
244 kfree(hi);
245
246 atomic_inc(&internal_generation);
247}
248
249static void host_reset(struct hpsb_host *host)
250{
251 unsigned long flags;
252 struct host_info *hi;
253 struct file_info *fi;
254 struct pending_request *req;
255
256 spin_lock_irqsave(&host_info_lock, flags);
257 hi = find_host_info(host);
258
259 if (hi != NULL) {
260 list_for_each_entry(fi, &hi->file_info_list, list) {
261 if (fi->notification == RAW1394_NOTIFY_ON) {
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800262 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (req != NULL) {
265 req->file_info = fi;
266 req->req.type = RAW1394_REQ_BUS_RESET;
267 req->req.generation =
268 get_hpsb_generation(host);
269 req->req.misc = (host->node_id << 16)
270 | host->node_count;
271 if (fi->protocol_version > 3) {
272 req->req.misc |=
273 (NODEID_TO_NODE
274 (host->irm_id)
275 << 8);
276 }
277
278 queue_complete_req(req);
279 }
280 }
281 }
282 }
283 spin_unlock_irqrestore(&host_info_lock, flags);
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static void fcp_request(struct hpsb_host *host, int nodeid, int direction,
287 int cts, u8 * data, size_t length)
288{
289 unsigned long flags;
290 struct host_info *hi;
291 struct file_info *fi;
292 struct pending_request *req, *req_next;
293 struct iso_block_store *ibs = NULL;
294 LIST_HEAD(reqs);
295
296 if ((atomic_read(&iso_buffer_size) + length) > iso_buffer_max) {
297 HPSB_INFO("dropped fcp request");
298 return;
299 }
300
301 spin_lock_irqsave(&host_info_lock, flags);
302 hi = find_host_info(host);
303
304 if (hi != NULL) {
305 list_for_each_entry(fi, &hi->file_info_list, list) {
306 if (!fi->fcp_buffer)
307 continue;
308
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800309 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!req)
311 break;
312
313 if (!ibs) {
Stefan Richter85511582005-11-07 06:31:45 -0500314 ibs = kmalloc(sizeof(*ibs) + length,
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800315 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!ibs) {
317 kfree(req);
318 break;
319 }
320
321 atomic_add(length, &iso_buffer_size);
322 atomic_set(&ibs->refcount, 0);
323 ibs->data_size = length;
324 memcpy(ibs->data, data, length);
325 }
326
327 atomic_inc(&ibs->refcount);
328
329 req->file_info = fi;
330 req->ibs = ibs;
331 req->data = ibs->data;
332 req->req.type = RAW1394_REQ_FCP_REQUEST;
333 req->req.generation = get_hpsb_generation(host);
334 req->req.misc = nodeid | (direction << 16);
335 req->req.recvb = ptr2int(fi->fcp_buffer);
336 req->req.length = length;
337
338 list_add_tail(&req->list, &reqs);
339 }
340 }
341 spin_unlock_irqrestore(&host_info_lock, flags);
342
343 list_for_each_entry_safe(req, req_next, &reqs, list)
344 queue_complete_req(req);
345}
346
Andi Kleen4bc32c42006-03-25 16:30:07 +0100347#ifdef CONFIG_COMPAT
348struct compat_raw1394_req {
Ben Collins75970282006-06-12 18:12:10 -0400349 __u32 type;
350 __s32 error;
351 __u32 misc;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100352
Ben Collins75970282006-06-12 18:12:10 -0400353 __u32 generation;
354 __u32 length;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100355
Ben Collins75970282006-06-12 18:12:10 -0400356 __u64 address;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100357
Ben Collins75970282006-06-12 18:12:10 -0400358 __u64 tag;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100359
Ben Collins75970282006-06-12 18:12:10 -0400360 __u64 sendb;
361 __u64 recvb;
Stefan Richter59337082007-07-04 23:13:53 +0200362}
363#if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
364__attribute__((packed))
365#endif
366;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100367
368static const char __user *raw1394_compat_write(const char __user *buf)
369{
Ben Collins75970282006-06-12 18:12:10 -0400370 struct compat_raw1394_req __user *cr = (typeof(cr)) buf;
Andi Kleen4bc32c42006-03-25 16:30:07 +0100371 struct raw1394_request __user *r;
372 r = compat_alloc_user_space(sizeof(struct raw1394_request));
373
374#define C(x) __copy_in_user(&r->x, &cr->x, sizeof(r->x))
375
376 if (copy_in_user(r, cr, sizeof(struct compat_raw1394_req)) ||
Ben Collins75970282006-06-12 18:12:10 -0400377 C(address) ||
378 C(tag) ||
379 C(sendb) ||
380 C(recvb))
Andi Kleen4bc32c42006-03-25 16:30:07 +0100381 return ERR_PTR(-EFAULT);
382 return (const char __user *)r;
383}
384#undef C
385
386#define P(x) __put_user(r->x, &cr->x)
387
Ben Collins75970282006-06-12 18:12:10 -0400388static int
Andi Kleen4bc32c42006-03-25 16:30:07 +0100389raw1394_compat_read(const char __user *buf, struct raw1394_request *r)
390{
Petr Vandrovecee9be422007-05-07 04:14:47 +0200391 struct compat_raw1394_req __user *cr = (typeof(cr)) buf;
Ben Collins75970282006-06-12 18:12:10 -0400392 if (!access_ok(VERIFY_WRITE, cr, sizeof(struct compat_raw1394_req)) ||
Andi Kleen4bc32c42006-03-25 16:30:07 +0100393 P(type) ||
394 P(error) ||
395 P(misc) ||
396 P(generation) ||
397 P(length) ||
398 P(address) ||
399 P(tag) ||
400 P(sendb) ||
401 P(recvb))
402 return -EFAULT;
403 return sizeof(struct compat_raw1394_req);
404}
405#undef P
406
407#endif
408
Stefan Richter45289bf2006-07-03 12:02:33 -0400409/* get next completed request (caller must hold fi->reqlists_lock) */
410static inline struct pending_request *__next_complete_req(struct file_info *fi)
411{
412 struct list_head *lh;
413 struct pending_request *req = NULL;
414
415 if (!list_empty(&fi->req_complete)) {
416 lh = fi->req_complete.next;
417 list_del(lh);
418 req = list_entry(lh, struct pending_request, list);
419 }
420 return req;
421}
422
423/* atomically get next completed request */
424static struct pending_request *next_complete_req(struct file_info *fi)
425{
426 unsigned long flags;
427 struct pending_request *req;
428
429 spin_lock_irqsave(&fi->reqlists_lock, flags);
430 req = __next_complete_req(fi);
431 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
432 return req;
433}
Andi Kleen4bc32c42006-03-25 16:30:07 +0100434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static ssize_t raw1394_read(struct file *file, char __user * buffer,
436 size_t count, loff_t * offset_is_ignored)
437{
438 struct file_info *fi = (struct file_info *)file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct pending_request *req;
440 ssize_t ret;
441
Andi Kleen4bc32c42006-03-25 16:30:07 +0100442#ifdef CONFIG_COMPAT
443 if (count == sizeof(struct compat_raw1394_req)) {
444 /* ok */
445 } else
446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (count != sizeof(struct raw1394_request)) {
448 return -EINVAL;
449 }
450
451 if (!access_ok(VERIFY_WRITE, buffer, count)) {
452 return -EFAULT;
453 }
454
455 if (file->f_flags & O_NONBLOCK) {
Stefan Richter45289bf2006-07-03 12:02:33 -0400456 if (!(req = next_complete_req(fi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
Stefan Richter45289bf2006-07-03 12:02:33 -0400459 /*
460 * NB: We call the macro wait_event_interruptible() with a
461 * condition argument with side effect. This is only possible
462 * because the side effect does not occur until the condition
463 * became true, and wait_event_interruptible() won't evaluate
464 * the condition again after that.
465 */
466 if (wait_event_interruptible(fi->wait_complete,
467 (req = next_complete_req(fi))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (req->req.length) {
472 if (copy_to_user(int2ptr(req->req.recvb), req->data,
473 req->req.length)) {
474 req->req.error = RAW1394_ERROR_MEMFAULT;
475 }
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Andi Kleen4bc32c42006-03-25 16:30:07 +0100478#ifdef CONFIG_COMPAT
Ben Collins75970282006-06-12 18:12:10 -0400479 if (count == sizeof(struct compat_raw1394_req) &&
480 sizeof(struct compat_raw1394_req) !=
481 sizeof(struct raw1394_request)) {
Andi Kleen4bc32c42006-03-25 16:30:07 +0100482 ret = raw1394_compat_read(buffer, &req->req);
Ben Collins75970282006-06-12 18:12:10 -0400483 } else
Andi Kleen4bc32c42006-03-25 16:30:07 +0100484#endif
485 {
486 if (copy_to_user(buffer, &req->req, sizeof(req->req))) {
487 ret = -EFAULT;
488 goto out;
Ben Collins75970282006-06-12 18:12:10 -0400489 }
Andi Kleen4bc32c42006-03-25 16:30:07 +0100490 ret = (ssize_t) sizeof(struct raw1394_request);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 out:
493 free_pending_request(req);
494 return ret;
495}
496
497static int state_opened(struct file_info *fi, struct pending_request *req)
498{
499 if (req->req.type == RAW1394_REQ_INITIALIZE) {
500 switch (req->req.misc) {
501 case RAW1394_KERNELAPI_VERSION:
502 case 3:
503 fi->state = initialized;
504 fi->protocol_version = req->req.misc;
505 req->req.error = RAW1394_ERROR_NONE;
506 req->req.generation = atomic_read(&internal_generation);
507 break;
508
509 default:
510 req->req.error = RAW1394_ERROR_COMPAT;
511 req->req.misc = RAW1394_KERNELAPI_VERSION;
512 }
513 } else {
514 req->req.error = RAW1394_ERROR_STATE_ORDER;
515 }
516
517 req->req.length = 0;
518 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static int state_initialized(struct file_info *fi, struct pending_request *req)
523{
Andy Wingo4a9949d2005-10-19 21:23:46 -0700524 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct host_info *hi;
526 struct raw1394_khost_list *khl;
527
528 if (req->req.generation != atomic_read(&internal_generation)) {
529 req->req.error = RAW1394_ERROR_GENERATION;
530 req->req.generation = atomic_read(&internal_generation);
531 req->req.length = 0;
532 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 switch (req->req.type) {
537 case RAW1394_REQ_LIST_CARDS:
Andy Wingo4a9949d2005-10-19 21:23:46 -0700538 spin_lock_irqsave(&host_info_lock, flags);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800539 khl = kmalloc(sizeof(*khl) * host_count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Stefan Richter85511582005-11-07 06:31:45 -0500541 if (khl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 req->req.misc = host_count;
543 req->data = (quadlet_t *) khl;
544
545 list_for_each_entry(hi, &host_info_list, list) {
546 khl->nodes = hi->host->node_count;
547 strcpy(khl->name, hi->host->driver->name);
548 khl++;
549 }
550 }
Andy Wingo4a9949d2005-10-19 21:23:46 -0700551 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Stefan Richter85511582005-11-07 06:31:45 -0500553 if (khl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 req->req.error = RAW1394_ERROR_NONE;
555 req->req.length = min(req->req.length,
556 (u32) (sizeof
557 (struct raw1394_khost_list)
558 * req->req.misc));
559 req->free_data = 1;
560 } else {
561 return -ENOMEM;
562 }
563 break;
564
565 case RAW1394_REQ_SET_CARD:
Andy Wingo4a9949d2005-10-19 21:23:46 -0700566 spin_lock_irqsave(&host_info_lock, flags);
Stefan Richter0fe4c6f2007-02-03 16:48:51 +0100567 if (req->req.misc >= host_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 req->req.error = RAW1394_ERROR_INVALID_ARG;
Stefan Richter0fe4c6f2007-02-03 16:48:51 +0100569 goto out_set_card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Stefan Richter0fe4c6f2007-02-03 16:48:51 +0100571 list_for_each_entry(hi, &host_info_list, list)
572 if (!req->req.misc--)
573 break;
574 get_device(&hi->host->device); /* FIXME handle failure case */
575 list_add_tail(&fi->list, &hi->file_info_list);
576
577 /* prevent unloading of the host's low-level driver */
578 if (!try_module_get(hi->host->driver->owner)) {
579 req->req.error = RAW1394_ERROR_ABORTED;
580 goto out_set_card;
581 }
582 WARN_ON(fi->host);
583 fi->host = hi->host;
584 fi->state = connected;
585
586 req->req.error = RAW1394_ERROR_NONE;
587 req->req.generation = get_hpsb_generation(fi->host);
588 req->req.misc = (fi->host->node_id << 16)
589 | fi->host->node_count;
590 if (fi->protocol_version > 3)
591 req->req.misc |= NODEID_TO_NODE(fi->host->irm_id) << 8;
592out_set_card:
Andy Wingo4a9949d2005-10-19 21:23:46 -0700593 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 req->req.length = 0;
596 break;
597
598 default:
599 req->req.error = RAW1394_ERROR_STATE_ORDER;
600 req->req.length = 0;
601 break;
602 }
603
604 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static void handle_fcp_listen(struct file_info *fi, struct pending_request *req)
609{
610 if (req->req.misc) {
611 if (fi->fcp_buffer) {
612 req->req.error = RAW1394_ERROR_ALREADY;
613 } else {
614 fi->fcp_buffer = int2ptr(req->req.recvb);
615 }
616 } else {
617 if (!fi->fcp_buffer) {
618 req->req.error = RAW1394_ERROR_ALREADY;
619 } else {
620 fi->fcp_buffer = NULL;
621 }
622 }
623
624 req->req.length = 0;
625 queue_complete_req(req);
626}
627
628static int handle_async_request(struct file_info *fi,
629 struct pending_request *req, int node)
630{
Andy Wingo4a9949d2005-10-19 21:23:46 -0700631 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct hpsb_packet *packet = NULL;
633 u64 addr = req->req.address & 0xffffffffffffULL;
634
635 switch (req->req.type) {
636 case RAW1394_REQ_ASYNC_READ:
637 DBGMSG("read_request called");
638 packet =
639 hpsb_make_readpacket(fi->host, node, addr, req->req.length);
640
641 if (!packet)
642 return -ENOMEM;
643
644 if (req->req.length == 4)
645 req->data = &packet->header[3];
646 else
647 req->data = packet->data;
648
649 break;
650
651 case RAW1394_REQ_ASYNC_WRITE:
652 DBGMSG("write_request called");
653
654 packet = hpsb_make_writepacket(fi->host, node, addr, NULL,
655 req->req.length);
656 if (!packet)
657 return -ENOMEM;
658
659 if (req->req.length == 4) {
660 if (copy_from_user
661 (&packet->header[3], int2ptr(req->req.sendb),
662 req->req.length))
663 req->req.error = RAW1394_ERROR_MEMFAULT;
664 } else {
665 if (copy_from_user
666 (packet->data, int2ptr(req->req.sendb),
667 req->req.length))
668 req->req.error = RAW1394_ERROR_MEMFAULT;
669 }
670
671 req->req.length = 0;
672 break;
673
674 case RAW1394_REQ_ASYNC_STREAM:
675 DBGMSG("stream_request called");
676
677 packet =
678 hpsb_make_streampacket(fi->host, NULL, req->req.length,
679 node & 0x3f /*channel */ ,
680 (req->req.misc >> 16) & 0x3,
681 req->req.misc & 0xf);
682 if (!packet)
683 return -ENOMEM;
684
685 if (copy_from_user(packet->data, int2ptr(req->req.sendb),
686 req->req.length))
687 req->req.error = RAW1394_ERROR_MEMFAULT;
688
689 req->req.length = 0;
690 break;
691
692 case RAW1394_REQ_LOCK:
693 DBGMSG("lock_request called");
694 if ((req->req.misc == EXTCODE_FETCH_ADD)
695 || (req->req.misc == EXTCODE_LITTLE_ADD)) {
696 if (req->req.length != 4) {
697 req->req.error = RAW1394_ERROR_INVALID_ARG;
698 break;
699 }
700 } else {
701 if (req->req.length != 8) {
702 req->req.error = RAW1394_ERROR_INVALID_ARG;
703 break;
704 }
705 }
706
707 packet = hpsb_make_lockpacket(fi->host, node, addr,
708 req->req.misc, NULL, 0);
709 if (!packet)
710 return -ENOMEM;
711
712 if (copy_from_user(packet->data, int2ptr(req->req.sendb),
713 req->req.length)) {
714 req->req.error = RAW1394_ERROR_MEMFAULT;
715 break;
716 }
717
718 req->data = packet->data;
719 req->req.length = 4;
720 break;
721
722 case RAW1394_REQ_LOCK64:
723 DBGMSG("lock64_request called");
724 if ((req->req.misc == EXTCODE_FETCH_ADD)
725 || (req->req.misc == EXTCODE_LITTLE_ADD)) {
726 if (req->req.length != 8) {
727 req->req.error = RAW1394_ERROR_INVALID_ARG;
728 break;
729 }
730 } else {
731 if (req->req.length != 16) {
732 req->req.error = RAW1394_ERROR_INVALID_ARG;
733 break;
734 }
735 }
736 packet = hpsb_make_lock64packet(fi->host, node, addr,
737 req->req.misc, NULL, 0);
738 if (!packet)
739 return -ENOMEM;
740
741 if (copy_from_user(packet->data, int2ptr(req->req.sendb),
742 req->req.length)) {
743 req->req.error = RAW1394_ERROR_MEMFAULT;
744 break;
745 }
746
747 req->data = packet->data;
748 req->req.length = 8;
749 break;
750
751 default:
752 req->req.error = RAW1394_ERROR_STATE_ORDER;
753 }
754
755 req->packet = packet;
756
757 if (req->req.error) {
758 req->req.length = 0;
759 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 hpsb_set_packet_complete_task(packet,
764 (void (*)(void *))queue_complete_cb, req);
765
Andy Wingo4a9949d2005-10-19 21:23:46 -0700766 spin_lock_irqsave(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 list_add_tail(&req->list, &fi->req_pending);
Andy Wingo4a9949d2005-10-19 21:23:46 -0700768 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 packet->generation = req->req.generation;
771
772 if (hpsb_send_packet(packet) < 0) {
773 req->req.error = RAW1394_ERROR_SEND_ERROR;
774 req->req.length = 0;
775 hpsb_free_tlabel(packet);
776 queue_complete_req(req);
777 }
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200778 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int handle_async_send(struct file_info *fi, struct pending_request *req)
782{
Andy Wingo4a9949d2005-10-19 21:23:46 -0700783 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 struct hpsb_packet *packet;
785 int header_length = req->req.misc & 0xffff;
786 int expect_response = req->req.misc >> 16;
Petr Vandrovec976da962007-05-13 22:14:44 -0700787 size_t data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Stefan Richter7542e0e2007-03-25 22:22:40 +0200789 if (header_length > req->req.length || header_length < 12 ||
790 header_length > FIELD_SIZEOF(struct hpsb_packet, header)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 req->req.error = RAW1394_ERROR_INVALID_ARG;
792 req->req.length = 0;
793 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Petr Vandrovec976da962007-05-13 22:14:44 -0700797 data_size = req->req.length - header_length;
798 packet = hpsb_alloc_packet(data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 req->packet = packet;
800 if (!packet)
801 return -ENOMEM;
802
803 if (copy_from_user(packet->header, int2ptr(req->req.sendb),
804 header_length)) {
805 req->req.error = RAW1394_ERROR_MEMFAULT;
806 req->req.length = 0;
807 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 if (copy_from_user
812 (packet->data, int2ptr(req->req.sendb) + header_length,
Petr Vandrovec976da962007-05-13 22:14:44 -0700813 data_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 req->req.error = RAW1394_ERROR_MEMFAULT;
815 req->req.length = 0;
816 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
820 packet->type = hpsb_async;
821 packet->node_id = packet->header[0] >> 16;
822 packet->tcode = (packet->header[0] >> 4) & 0xf;
823 packet->tlabel = (packet->header[0] >> 10) & 0x3f;
824 packet->host = fi->host;
825 packet->expect_response = expect_response;
826 packet->header_size = header_length;
Petr Vandrovec976da962007-05-13 22:14:44 -0700827 packet->data_size = data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 req->req.length = 0;
830 hpsb_set_packet_complete_task(packet,
831 (void (*)(void *))queue_complete_cb, req);
832
Andy Wingo4a9949d2005-10-19 21:23:46 -0700833 spin_lock_irqsave(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 list_add_tail(&req->list, &fi->req_pending);
Andy Wingo4a9949d2005-10-19 21:23:46 -0700835 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* Update the generation of the packet just before sending. */
838 packet->generation = req->req.generation;
839
840 if (hpsb_send_packet(packet) < 0) {
841 req->req.error = RAW1394_ERROR_SEND_ERROR;
842 queue_complete_req(req);
843 }
844
Petr Vandrovec883b97e2007-05-07 04:14:47 +0200845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static int arm_read(struct hpsb_host *host, int nodeid, quadlet_t * buffer,
849 u64 addr, size_t length, u16 flags)
850{
Andy Wingo4a9949d2005-10-19 21:23:46 -0700851 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct pending_request *req;
853 struct host_info *hi;
854 struct file_info *fi = NULL;
855 struct list_head *entry;
856 struct arm_addr *arm_addr = NULL;
857 struct arm_request *arm_req = NULL;
858 struct arm_response *arm_resp = NULL;
859 int found = 0, size = 0, rcode = -1;
860 struct arm_request_response *arm_req_resp = NULL;
861
Joe Perchesa5c52df2007-11-19 17:48:10 -0800862 DBGMSG("arm_read called by node: %X "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 "addr: %4.4x %8.8x length: %Zu", nodeid,
864 (u16) ((addr >> 32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
865 length);
Andy Wingo4a9949d2005-10-19 21:23:46 -0700866 spin_lock_irqsave(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 hi = find_host_info(host); /* search address-entry */
868 if (hi != NULL) {
869 list_for_each_entry(fi, &hi->file_info_list, list) {
870 entry = fi->addr_list.next;
871 while (entry != &(fi->addr_list)) {
872 arm_addr =
873 list_entry(entry, struct arm_addr,
874 addr_list);
875 if (((arm_addr->start) <= (addr))
876 && ((arm_addr->end) >= (addr + length))) {
877 found = 1;
878 break;
879 }
880 entry = entry->next;
881 }
882 if (found) {
883 break;
884 }
885 }
886 }
887 rcode = -1;
888 if (!found) {
889 printk(KERN_ERR "raw1394: arm_read FAILED addr_entry not found"
890 " -> rcode_address_error\n");
Andy Wingo4a9949d2005-10-19 21:23:46 -0700891 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return (RCODE_ADDRESS_ERROR);
893 } else {
894 DBGMSG("arm_read addr_entry FOUND");
895 }
896 if (arm_addr->rec_length < length) {
897 DBGMSG("arm_read blocklength too big -> rcode_data_error");
898 rcode = RCODE_DATA_ERROR; /* hardware error, data is unavailable */
899 }
900 if (rcode == -1) {
901 if (arm_addr->access_rights & ARM_READ) {
902 if (!(arm_addr->client_transactions & ARM_READ)) {
903 memcpy(buffer,
904 (arm_addr->addr_space_buffer) + (addr -
905 (arm_addr->
906 start)),
907 length);
908 DBGMSG("arm_read -> (rcode_complete)");
909 rcode = RCODE_COMPLETE;
910 }
911 } else {
912 rcode = RCODE_TYPE_ERROR; /* function not allowed */
913 DBGMSG("arm_read -> rcode_type_error (access denied)");
914 }
915 }
916 if (arm_addr->notification_options & ARM_READ) {
917 DBGMSG("arm_read -> entering notification-section");
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800918 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (!req) {
920 DBGMSG("arm_read -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -0700921 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
923 The request may be retried */
924 }
925 if (rcode == RCODE_COMPLETE) {
926 size =
927 sizeof(struct arm_request) +
928 sizeof(struct arm_response) +
929 length * sizeof(byte_t) +
930 sizeof(struct arm_request_response);
931 } else {
932 size =
933 sizeof(struct arm_request) +
934 sizeof(struct arm_response) +
935 sizeof(struct arm_request_response);
936 }
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800937 req->data = kmalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (!(req->data)) {
939 free_pending_request(req);
940 DBGMSG("arm_read -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -0700941 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
943 The request may be retried */
944 }
945 req->free_data = 1;
946 req->file_info = fi;
947 req->req.type = RAW1394_REQ_ARM;
948 req->req.generation = get_hpsb_generation(host);
949 req->req.misc =
950 (((length << 16) & (0xFFFF0000)) | (ARM_READ & 0xFF));
951 req->req.tag = arm_addr->arm_tag;
952 req->req.recvb = arm_addr->recvb;
953 req->req.length = size;
954 arm_req_resp = (struct arm_request_response *)(req->data);
955 arm_req = (struct arm_request *)((byte_t *) (req->data) +
956 (sizeof
957 (struct
958 arm_request_response)));
959 arm_resp =
960 (struct arm_response *)((byte_t *) (arm_req) +
961 (sizeof(struct arm_request)));
962 arm_req->buffer = NULL;
963 arm_resp->buffer = NULL;
964 if (rcode == RCODE_COMPLETE) {
965 byte_t *buf =
966 (byte_t *) arm_resp + sizeof(struct arm_response);
967 memcpy(buf,
968 (arm_addr->addr_space_buffer) + (addr -
969 (arm_addr->
970 start)),
971 length);
972 arm_resp->buffer =
973 int2ptr((arm_addr->recvb) +
974 sizeof(struct arm_request_response) +
975 sizeof(struct arm_request) +
976 sizeof(struct arm_response));
977 }
978 arm_resp->buffer_length =
979 (rcode == RCODE_COMPLETE) ? length : 0;
980 arm_resp->response_code = rcode;
981 arm_req->buffer_length = 0;
982 arm_req->generation = req->req.generation;
983 arm_req->extended_transaction_code = 0;
984 arm_req->destination_offset = addr;
985 arm_req->source_nodeid = nodeid;
986 arm_req->destination_nodeid = host->node_id;
987 arm_req->tlabel = (flags >> 10) & 0x3f;
988 arm_req->tcode = (flags >> 4) & 0x0f;
989 arm_req_resp->request = int2ptr((arm_addr->recvb) +
990 sizeof(struct
991 arm_request_response));
992 arm_req_resp->response =
993 int2ptr((arm_addr->recvb) +
994 sizeof(struct arm_request_response) +
995 sizeof(struct arm_request));
996 queue_complete_req(req);
997 }
Andy Wingo4a9949d2005-10-19 21:23:46 -0700998 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return (rcode);
1000}
1001
1002static int arm_write(struct hpsb_host *host, int nodeid, int destid,
1003 quadlet_t * data, u64 addr, size_t length, u16 flags)
1004{
Andy Wingo4a9949d2005-10-19 21:23:46 -07001005 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct pending_request *req;
1007 struct host_info *hi;
1008 struct file_info *fi = NULL;
1009 struct list_head *entry;
1010 struct arm_addr *arm_addr = NULL;
1011 struct arm_request *arm_req = NULL;
1012 struct arm_response *arm_resp = NULL;
1013 int found = 0, size = 0, rcode = -1, length_conflict = 0;
1014 struct arm_request_response *arm_req_resp = NULL;
1015
Joe Perchesa5c52df2007-11-19 17:48:10 -08001016 DBGMSG("arm_write called by node: %X "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 "addr: %4.4x %8.8x length: %Zu", nodeid,
1018 (u16) ((addr >> 32) & 0xFFFF), (u32) (addr & 0xFFFFFFFF),
1019 length);
Andy Wingo4a9949d2005-10-19 21:23:46 -07001020 spin_lock_irqsave(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 hi = find_host_info(host); /* search address-entry */
1022 if (hi != NULL) {
1023 list_for_each_entry(fi, &hi->file_info_list, list) {
1024 entry = fi->addr_list.next;
1025 while (entry != &(fi->addr_list)) {
1026 arm_addr =
1027 list_entry(entry, struct arm_addr,
1028 addr_list);
1029 if (((arm_addr->start) <= (addr))
1030 && ((arm_addr->end) >= (addr + length))) {
1031 found = 1;
1032 break;
1033 }
1034 entry = entry->next;
1035 }
1036 if (found) {
1037 break;
1038 }
1039 }
1040 }
1041 rcode = -1;
1042 if (!found) {
1043 printk(KERN_ERR "raw1394: arm_write FAILED addr_entry not found"
1044 " -> rcode_address_error\n");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001045 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return (RCODE_ADDRESS_ERROR);
1047 } else {
1048 DBGMSG("arm_write addr_entry FOUND");
1049 }
1050 if (arm_addr->rec_length < length) {
1051 DBGMSG("arm_write blocklength too big -> rcode_data_error");
1052 length_conflict = 1;
1053 rcode = RCODE_DATA_ERROR; /* hardware error, data is unavailable */
1054 }
1055 if (rcode == -1) {
1056 if (arm_addr->access_rights & ARM_WRITE) {
1057 if (!(arm_addr->client_transactions & ARM_WRITE)) {
1058 memcpy((arm_addr->addr_space_buffer) +
1059 (addr - (arm_addr->start)), data,
1060 length);
1061 DBGMSG("arm_write -> (rcode_complete)");
1062 rcode = RCODE_COMPLETE;
1063 }
1064 } else {
1065 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1066 DBGMSG("arm_write -> rcode_type_error (access denied)");
1067 }
1068 }
1069 if (arm_addr->notification_options & ARM_WRITE) {
1070 DBGMSG("arm_write -> entering notification-section");
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001071 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!req) {
1073 DBGMSG("arm_write -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001074 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1076 The request my be retried */
1077 }
1078 size =
1079 sizeof(struct arm_request) + sizeof(struct arm_response) +
1080 (length) * sizeof(byte_t) +
1081 sizeof(struct arm_request_response);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001082 req->data = kmalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!(req->data)) {
1084 free_pending_request(req);
1085 DBGMSG("arm_write -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001086 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1088 The request may be retried */
1089 }
1090 req->free_data = 1;
1091 req->file_info = fi;
1092 req->req.type = RAW1394_REQ_ARM;
1093 req->req.generation = get_hpsb_generation(host);
1094 req->req.misc =
1095 (((length << 16) & (0xFFFF0000)) | (ARM_WRITE & 0xFF));
1096 req->req.tag = arm_addr->arm_tag;
1097 req->req.recvb = arm_addr->recvb;
1098 req->req.length = size;
1099 arm_req_resp = (struct arm_request_response *)(req->data);
1100 arm_req = (struct arm_request *)((byte_t *) (req->data) +
1101 (sizeof
1102 (struct
1103 arm_request_response)));
1104 arm_resp =
1105 (struct arm_response *)((byte_t *) (arm_req) +
1106 (sizeof(struct arm_request)));
1107 arm_resp->buffer = NULL;
1108 memcpy((byte_t *) arm_resp + sizeof(struct arm_response),
1109 data, length);
1110 arm_req->buffer = int2ptr((arm_addr->recvb) +
1111 sizeof(struct arm_request_response) +
1112 sizeof(struct arm_request) +
1113 sizeof(struct arm_response));
1114 arm_req->buffer_length = length;
1115 arm_req->generation = req->req.generation;
1116 arm_req->extended_transaction_code = 0;
1117 arm_req->destination_offset = addr;
1118 arm_req->source_nodeid = nodeid;
1119 arm_req->destination_nodeid = destid;
1120 arm_req->tlabel = (flags >> 10) & 0x3f;
1121 arm_req->tcode = (flags >> 4) & 0x0f;
1122 arm_resp->buffer_length = 0;
1123 arm_resp->response_code = rcode;
1124 arm_req_resp->request = int2ptr((arm_addr->recvb) +
1125 sizeof(struct
1126 arm_request_response));
1127 arm_req_resp->response =
1128 int2ptr((arm_addr->recvb) +
1129 sizeof(struct arm_request_response) +
1130 sizeof(struct arm_request));
1131 queue_complete_req(req);
1132 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07001133 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return (rcode);
1135}
1136
1137static int arm_lock(struct hpsb_host *host, int nodeid, quadlet_t * store,
1138 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
1139 u16 flags)
1140{
Andy Wingo4a9949d2005-10-19 21:23:46 -07001141 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 struct pending_request *req;
1143 struct host_info *hi;
1144 struct file_info *fi = NULL;
1145 struct list_head *entry;
1146 struct arm_addr *arm_addr = NULL;
1147 struct arm_request *arm_req = NULL;
1148 struct arm_response *arm_resp = NULL;
1149 int found = 0, size = 0, rcode = -1;
1150 quadlet_t old, new;
1151 struct arm_request_response *arm_req_resp = NULL;
1152
1153 if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
1154 ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
1155 DBGMSG("arm_lock called by node: %X "
1156 "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X",
1157 nodeid, (u16) ((addr >> 32) & 0xFFFF),
1158 (u32) (addr & 0xFFFFFFFF), ext_tcode & 0xFF,
1159 be32_to_cpu(data));
1160 } else {
1161 DBGMSG("arm_lock called by node: %X "
1162 "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X arg: %8.8X",
1163 nodeid, (u16) ((addr >> 32) & 0xFFFF),
1164 (u32) (addr & 0xFFFFFFFF), ext_tcode & 0xFF,
1165 be32_to_cpu(data), be32_to_cpu(arg));
1166 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07001167 spin_lock_irqsave(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 hi = find_host_info(host); /* search address-entry */
1169 if (hi != NULL) {
1170 list_for_each_entry(fi, &hi->file_info_list, list) {
1171 entry = fi->addr_list.next;
1172 while (entry != &(fi->addr_list)) {
1173 arm_addr =
1174 list_entry(entry, struct arm_addr,
1175 addr_list);
1176 if (((arm_addr->start) <= (addr))
1177 && ((arm_addr->end) >=
1178 (addr + sizeof(*store)))) {
1179 found = 1;
1180 break;
1181 }
1182 entry = entry->next;
1183 }
1184 if (found) {
1185 break;
1186 }
1187 }
1188 }
1189 rcode = -1;
1190 if (!found) {
1191 printk(KERN_ERR "raw1394: arm_lock FAILED addr_entry not found"
1192 " -> rcode_address_error\n");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001193 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return (RCODE_ADDRESS_ERROR);
1195 } else {
1196 DBGMSG("arm_lock addr_entry FOUND");
1197 }
1198 if (rcode == -1) {
1199 if (arm_addr->access_rights & ARM_LOCK) {
1200 if (!(arm_addr->client_transactions & ARM_LOCK)) {
1201 memcpy(&old,
1202 (arm_addr->addr_space_buffer) + (addr -
1203 (arm_addr->
1204 start)),
1205 sizeof(old));
1206 switch (ext_tcode) {
1207 case (EXTCODE_MASK_SWAP):
1208 new = data | (old & ~arg);
1209 break;
1210 case (EXTCODE_COMPARE_SWAP):
1211 if (old == arg) {
1212 new = data;
1213 } else {
1214 new = old;
1215 }
1216 break;
1217 case (EXTCODE_FETCH_ADD):
1218 new =
1219 cpu_to_be32(be32_to_cpu(data) +
1220 be32_to_cpu(old));
1221 break;
1222 case (EXTCODE_LITTLE_ADD):
1223 new =
1224 cpu_to_le32(le32_to_cpu(data) +
1225 le32_to_cpu(old));
1226 break;
1227 case (EXTCODE_BOUNDED_ADD):
1228 if (old != arg) {
1229 new =
1230 cpu_to_be32(be32_to_cpu
1231 (data) +
1232 be32_to_cpu
1233 (old));
1234 } else {
1235 new = old;
1236 }
1237 break;
1238 case (EXTCODE_WRAP_ADD):
1239 if (old != arg) {
1240 new =
1241 cpu_to_be32(be32_to_cpu
1242 (data) +
1243 be32_to_cpu
1244 (old));
1245 } else {
1246 new = data;
1247 }
1248 break;
1249 default:
1250 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1251 printk(KERN_ERR
1252 "raw1394: arm_lock FAILED "
1253 "ext_tcode not allowed -> rcode_type_error\n");
1254 break;
1255 } /*switch */
1256 if (rcode == -1) {
1257 DBGMSG("arm_lock -> (rcode_complete)");
1258 rcode = RCODE_COMPLETE;
1259 memcpy(store, &old, sizeof(*store));
1260 memcpy((arm_addr->addr_space_buffer) +
1261 (addr - (arm_addr->start)),
1262 &new, sizeof(*store));
1263 }
1264 }
1265 } else {
1266 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1267 DBGMSG("arm_lock -> rcode_type_error (access denied)");
1268 }
1269 }
1270 if (arm_addr->notification_options & ARM_LOCK) {
1271 byte_t *buf1, *buf2;
1272 DBGMSG("arm_lock -> entering notification-section");
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001273 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!req) {
1275 DBGMSG("arm_lock -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001276 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1278 The request may be retried */
1279 }
1280 size = sizeof(struct arm_request) + sizeof(struct arm_response) + 3 * sizeof(*store) + sizeof(struct arm_request_response); /* maximum */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001281 req->data = kmalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!(req->data)) {
1283 free_pending_request(req);
1284 DBGMSG("arm_lock -> rcode_conflict_error");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001285 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1287 The request may be retried */
1288 }
1289 req->free_data = 1;
1290 arm_req_resp = (struct arm_request_response *)(req->data);
1291 arm_req = (struct arm_request *)((byte_t *) (req->data) +
1292 (sizeof
1293 (struct
1294 arm_request_response)));
1295 arm_resp =
1296 (struct arm_response *)((byte_t *) (arm_req) +
1297 (sizeof(struct arm_request)));
1298 buf1 = (byte_t *) arm_resp + sizeof(struct arm_response);
1299 buf2 = buf1 + 2 * sizeof(*store);
1300 if ((ext_tcode == EXTCODE_FETCH_ADD) ||
1301 (ext_tcode == EXTCODE_LITTLE_ADD)) {
1302 arm_req->buffer_length = sizeof(*store);
1303 memcpy(buf1, &data, sizeof(*store));
1304
1305 } else {
1306 arm_req->buffer_length = 2 * sizeof(*store);
1307 memcpy(buf1, &arg, sizeof(*store));
1308 memcpy(buf1 + sizeof(*store), &data, sizeof(*store));
1309 }
1310 if (rcode == RCODE_COMPLETE) {
1311 arm_resp->buffer_length = sizeof(*store);
1312 memcpy(buf2, &old, sizeof(*store));
1313 } else {
1314 arm_resp->buffer_length = 0;
1315 }
1316 req->file_info = fi;
1317 req->req.type = RAW1394_REQ_ARM;
1318 req->req.generation = get_hpsb_generation(host);
1319 req->req.misc = ((((sizeof(*store)) << 16) & (0xFFFF0000)) |
1320 (ARM_LOCK & 0xFF));
1321 req->req.tag = arm_addr->arm_tag;
1322 req->req.recvb = arm_addr->recvb;
1323 req->req.length = size;
1324 arm_req->generation = req->req.generation;
1325 arm_req->extended_transaction_code = ext_tcode;
1326 arm_req->destination_offset = addr;
1327 arm_req->source_nodeid = nodeid;
1328 arm_req->destination_nodeid = host->node_id;
1329 arm_req->tlabel = (flags >> 10) & 0x3f;
1330 arm_req->tcode = (flags >> 4) & 0x0f;
1331 arm_resp->response_code = rcode;
1332 arm_req_resp->request = int2ptr((arm_addr->recvb) +
1333 sizeof(struct
1334 arm_request_response));
1335 arm_req_resp->response =
1336 int2ptr((arm_addr->recvb) +
1337 sizeof(struct arm_request_response) +
1338 sizeof(struct arm_request));
1339 arm_req->buffer =
1340 int2ptr((arm_addr->recvb) +
1341 sizeof(struct arm_request_response) +
1342 sizeof(struct arm_request) +
1343 sizeof(struct arm_response));
1344 arm_resp->buffer =
1345 int2ptr((arm_addr->recvb) +
1346 sizeof(struct arm_request_response) +
1347 sizeof(struct arm_request) +
1348 sizeof(struct arm_response) + 2 * sizeof(*store));
1349 queue_complete_req(req);
1350 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07001351 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return (rcode);
1353}
1354
1355static int arm_lock64(struct hpsb_host *host, int nodeid, octlet_t * store,
1356 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
1357 u16 flags)
1358{
Andy Wingo4a9949d2005-10-19 21:23:46 -07001359 unsigned long irqflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 struct pending_request *req;
1361 struct host_info *hi;
1362 struct file_info *fi = NULL;
1363 struct list_head *entry;
1364 struct arm_addr *arm_addr = NULL;
1365 struct arm_request *arm_req = NULL;
1366 struct arm_response *arm_resp = NULL;
1367 int found = 0, size = 0, rcode = -1;
1368 octlet_t old, new;
1369 struct arm_request_response *arm_req_resp = NULL;
1370
1371 if (((ext_tcode & 0xFF) == EXTCODE_FETCH_ADD) ||
1372 ((ext_tcode & 0xFF) == EXTCODE_LITTLE_ADD)) {
1373 DBGMSG("arm_lock64 called by node: %X "
1374 "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X ",
1375 nodeid, (u16) ((addr >> 32) & 0xFFFF),
1376 (u32) (addr & 0xFFFFFFFF),
1377 ext_tcode & 0xFF,
1378 (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF),
1379 (u32) (be64_to_cpu(data) & 0xFFFFFFFF));
1380 } else {
1381 DBGMSG("arm_lock64 called by node: %X "
1382 "addr: %4.4x %8.8x extcode: %2.2X data: %8.8X %8.8X arg: "
1383 "%8.8X %8.8X ",
1384 nodeid, (u16) ((addr >> 32) & 0xFFFF),
1385 (u32) (addr & 0xFFFFFFFF),
1386 ext_tcode & 0xFF,
1387 (u32) ((be64_to_cpu(data) >> 32) & 0xFFFFFFFF),
1388 (u32) (be64_to_cpu(data) & 0xFFFFFFFF),
1389 (u32) ((be64_to_cpu(arg) >> 32) & 0xFFFFFFFF),
1390 (u32) (be64_to_cpu(arg) & 0xFFFFFFFF));
1391 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07001392 spin_lock_irqsave(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 hi = find_host_info(host); /* search addressentry in file_info's for host */
1394 if (hi != NULL) {
1395 list_for_each_entry(fi, &hi->file_info_list, list) {
1396 entry = fi->addr_list.next;
1397 while (entry != &(fi->addr_list)) {
1398 arm_addr =
1399 list_entry(entry, struct arm_addr,
1400 addr_list);
1401 if (((arm_addr->start) <= (addr))
1402 && ((arm_addr->end) >=
1403 (addr + sizeof(*store)))) {
1404 found = 1;
1405 break;
1406 }
1407 entry = entry->next;
1408 }
1409 if (found) {
1410 break;
1411 }
1412 }
1413 }
1414 rcode = -1;
1415 if (!found) {
1416 printk(KERN_ERR
1417 "raw1394: arm_lock64 FAILED addr_entry not found"
1418 " -> rcode_address_error\n");
Andy Wingo4a9949d2005-10-19 21:23:46 -07001419 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return (RCODE_ADDRESS_ERROR);
1421 } else {
1422 DBGMSG("arm_lock64 addr_entry FOUND");
1423 }
1424 if (rcode == -1) {
1425 if (arm_addr->access_rights & ARM_LOCK) {
1426 if (!(arm_addr->client_transactions & ARM_LOCK)) {
1427 memcpy(&old,
1428 (arm_addr->addr_space_buffer) + (addr -
1429 (arm_addr->
1430 start)),
1431 sizeof(old));
1432 switch (ext_tcode) {
1433 case (EXTCODE_MASK_SWAP):
1434 new = data | (old & ~arg);
1435 break;
1436 case (EXTCODE_COMPARE_SWAP):
1437 if (old == arg) {
1438 new = data;
1439 } else {
1440 new = old;
1441 }
1442 break;
1443 case (EXTCODE_FETCH_ADD):
1444 new =
1445 cpu_to_be64(be64_to_cpu(data) +
1446 be64_to_cpu(old));
1447 break;
1448 case (EXTCODE_LITTLE_ADD):
1449 new =
1450 cpu_to_le64(le64_to_cpu(data) +
1451 le64_to_cpu(old));
1452 break;
1453 case (EXTCODE_BOUNDED_ADD):
1454 if (old != arg) {
1455 new =
1456 cpu_to_be64(be64_to_cpu
1457 (data) +
1458 be64_to_cpu
1459 (old));
1460 } else {
1461 new = old;
1462 }
1463 break;
1464 case (EXTCODE_WRAP_ADD):
1465 if (old != arg) {
1466 new =
1467 cpu_to_be64(be64_to_cpu
1468 (data) +
1469 be64_to_cpu
1470 (old));
1471 } else {
1472 new = data;
1473 }
1474 break;
1475 default:
1476 printk(KERN_ERR
1477 "raw1394: arm_lock64 FAILED "
1478 "ext_tcode not allowed -> rcode_type_error\n");
1479 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1480 break;
1481 } /*switch */
1482 if (rcode == -1) {
1483 DBGMSG
1484 ("arm_lock64 -> (rcode_complete)");
1485 rcode = RCODE_COMPLETE;
1486 memcpy(store, &old, sizeof(*store));
1487 memcpy((arm_addr->addr_space_buffer) +
1488 (addr - (arm_addr->start)),
1489 &new, sizeof(*store));
1490 }
1491 }
1492 } else {
1493 rcode = RCODE_TYPE_ERROR; /* function not allowed */
1494 DBGMSG
1495 ("arm_lock64 -> rcode_type_error (access denied)");
1496 }
1497 }
1498 if (arm_addr->notification_options & ARM_LOCK) {
1499 byte_t *buf1, *buf2;
1500 DBGMSG("arm_lock64 -> entering notification-section");
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001501 req = __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!req) {
Andy Wingo4a9949d2005-10-19 21:23:46 -07001503 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 DBGMSG("arm_lock64 -> rcode_conflict_error");
1505 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1506 The request may be retried */
1507 }
1508 size = sizeof(struct arm_request) + sizeof(struct arm_response) + 3 * sizeof(*store) + sizeof(struct arm_request_response); /* maximum */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001509 req->data = kmalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (!(req->data)) {
1511 free_pending_request(req);
Andy Wingo4a9949d2005-10-19 21:23:46 -07001512 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 DBGMSG("arm_lock64 -> rcode_conflict_error");
1514 return (RCODE_CONFLICT_ERROR); /* A resource conflict was detected.
1515 The request may be retried */
1516 }
1517 req->free_data = 1;
1518 arm_req_resp = (struct arm_request_response *)(req->data);
1519 arm_req = (struct arm_request *)((byte_t *) (req->data) +
1520 (sizeof
1521 (struct
1522 arm_request_response)));
1523 arm_resp =
1524 (struct arm_response *)((byte_t *) (arm_req) +
1525 (sizeof(struct arm_request)));
1526 buf1 = (byte_t *) arm_resp + sizeof(struct arm_response);
1527 buf2 = buf1 + 2 * sizeof(*store);
1528 if ((ext_tcode == EXTCODE_FETCH_ADD) ||
1529 (ext_tcode == EXTCODE_LITTLE_ADD)) {
1530 arm_req->buffer_length = sizeof(*store);
1531 memcpy(buf1, &data, sizeof(*store));
1532
1533 } else {
1534 arm_req->buffer_length = 2 * sizeof(*store);
1535 memcpy(buf1, &arg, sizeof(*store));
1536 memcpy(buf1 + sizeof(*store), &data, sizeof(*store));
1537 }
1538 if (rcode == RCODE_COMPLETE) {
1539 arm_resp->buffer_length = sizeof(*store);
1540 memcpy(buf2, &old, sizeof(*store));
1541 } else {
1542 arm_resp->buffer_length = 0;
1543 }
1544 req->file_info = fi;
1545 req->req.type = RAW1394_REQ_ARM;
1546 req->req.generation = get_hpsb_generation(host);
1547 req->req.misc = ((((sizeof(*store)) << 16) & (0xFFFF0000)) |
1548 (ARM_LOCK & 0xFF));
1549 req->req.tag = arm_addr->arm_tag;
1550 req->req.recvb = arm_addr->recvb;
1551 req->req.length = size;
1552 arm_req->generation = req->req.generation;
1553 arm_req->extended_transaction_code = ext_tcode;
1554 arm_req->destination_offset = addr;
1555 arm_req->source_nodeid = nodeid;
1556 arm_req->destination_nodeid = host->node_id;
1557 arm_req->tlabel = (flags >> 10) & 0x3f;
1558 arm_req->tcode = (flags >> 4) & 0x0f;
1559 arm_resp->response_code = rcode;
1560 arm_req_resp->request = int2ptr((arm_addr->recvb) +
1561 sizeof(struct
1562 arm_request_response));
1563 arm_req_resp->response =
1564 int2ptr((arm_addr->recvb) +
1565 sizeof(struct arm_request_response) +
1566 sizeof(struct arm_request));
1567 arm_req->buffer =
1568 int2ptr((arm_addr->recvb) +
1569 sizeof(struct arm_request_response) +
1570 sizeof(struct arm_request) +
1571 sizeof(struct arm_response));
1572 arm_resp->buffer =
1573 int2ptr((arm_addr->recvb) +
1574 sizeof(struct arm_request_response) +
1575 sizeof(struct arm_request) +
1576 sizeof(struct arm_response) + 2 * sizeof(*store));
1577 queue_complete_req(req);
1578 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07001579 spin_unlock_irqrestore(&host_info_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return (rcode);
1581}
1582
1583static int arm_register(struct file_info *fi, struct pending_request *req)
1584{
1585 int retval;
1586 struct arm_addr *addr;
1587 struct host_info *hi;
1588 struct file_info *fi_hlp = NULL;
1589 struct list_head *entry;
1590 struct arm_addr *arm_addr = NULL;
1591 int same_host, another_host;
1592 unsigned long flags;
1593
1594 DBGMSG("arm_register called "
1595 "addr(Offset): %8.8x %8.8x length: %u "
1596 "rights: %2.2X notify: %2.2X "
1597 "max_blk_len: %4.4X",
1598 (u32) ((req->req.address >> 32) & 0xFFFF),
1599 (u32) (req->req.address & 0xFFFFFFFF),
1600 req->req.length, ((req->req.misc >> 8) & 0xFF),
1601 (req->req.misc & 0xFF), ((req->req.misc >> 16) & 0xFFFF));
1602 /* check addressrange */
1603 if ((((req->req.address) & ~(0xFFFFFFFFFFFFULL)) != 0) ||
1604 (((req->req.address + req->req.length) & ~(0xFFFFFFFFFFFFULL)) !=
1605 0)) {
1606 req->req.length = 0;
1607 return (-EINVAL);
1608 }
1609 /* addr-list-entry for fileinfo */
Christoph Lametere94b1762006-12-06 20:33:17 -08001610 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (!addr) {
1612 req->req.length = 0;
1613 return (-ENOMEM);
1614 }
1615 /* allocation of addr_space_buffer */
Stefan Richter85511582005-11-07 06:31:45 -05001616 addr->addr_space_buffer = vmalloc(req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (!(addr->addr_space_buffer)) {
1618 kfree(addr);
1619 req->req.length = 0;
1620 return (-ENOMEM);
1621 }
1622 /* initialization of addr_space_buffer */
1623 if ((req->req.sendb) == (unsigned long)NULL) {
1624 /* init: set 0 */
1625 memset(addr->addr_space_buffer, 0, req->req.length);
1626 } else {
1627 /* init: user -> kernel */
1628 if (copy_from_user
1629 (addr->addr_space_buffer, int2ptr(req->req.sendb),
1630 req->req.length)) {
1631 vfree(addr->addr_space_buffer);
1632 kfree(addr);
1633 return (-EFAULT);
1634 }
1635 }
1636 INIT_LIST_HEAD(&addr->addr_list);
1637 addr->arm_tag = req->req.tag;
1638 addr->start = req->req.address;
1639 addr->end = req->req.address + req->req.length;
1640 addr->access_rights = (u8) (req->req.misc & 0x0F);
1641 addr->notification_options = (u8) ((req->req.misc >> 4) & 0x0F);
1642 addr->client_transactions = (u8) ((req->req.misc >> 8) & 0x0F);
1643 addr->access_rights |= addr->client_transactions;
1644 addr->notification_options |= addr->client_transactions;
1645 addr->recvb = req->req.recvb;
1646 addr->rec_length = (u16) ((req->req.misc >> 16) & 0xFFFF);
Stefan Richter3253b662006-09-14 22:05:16 +02001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 spin_lock_irqsave(&host_info_lock, flags);
1649 hi = find_host_info(fi->host);
1650 same_host = 0;
1651 another_host = 0;
1652 /* same host with address-entry containing same addressrange ? */
1653 list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
1654 entry = fi_hlp->addr_list.next;
1655 while (entry != &(fi_hlp->addr_list)) {
1656 arm_addr =
1657 list_entry(entry, struct arm_addr, addr_list);
1658 if ((arm_addr->start == addr->start)
1659 && (arm_addr->end == addr->end)) {
1660 DBGMSG("same host ownes same "
1661 "addressrange -> EALREADY");
1662 same_host = 1;
1663 break;
1664 }
1665 entry = entry->next;
1666 }
1667 if (same_host) {
1668 break;
1669 }
1670 }
1671 if (same_host) {
1672 /* addressrange occupied by same host */
Stefan Richter3253b662006-09-14 22:05:16 +02001673 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 vfree(addr->addr_space_buffer);
1675 kfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return (-EALREADY);
1677 }
1678 /* another host with valid address-entry containing same addressrange */
1679 list_for_each_entry(hi, &host_info_list, list) {
1680 if (hi->host != fi->host) {
1681 list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
1682 entry = fi_hlp->addr_list.next;
1683 while (entry != &(fi_hlp->addr_list)) {
1684 arm_addr =
1685 list_entry(entry, struct arm_addr,
1686 addr_list);
1687 if ((arm_addr->start == addr->start)
1688 && (arm_addr->end == addr->end)) {
1689 DBGMSG
1690 ("another host ownes same "
1691 "addressrange");
1692 another_host = 1;
1693 break;
1694 }
1695 entry = entry->next;
1696 }
1697 if (another_host) {
1698 break;
1699 }
1700 }
1701 }
1702 }
Stefan Richter3253b662006-09-14 22:05:16 +02001703 spin_unlock_irqrestore(&host_info_lock, flags);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (another_host) {
1706 DBGMSG("another hosts entry is valid -> SUCCESS");
1707 if (copy_to_user(int2ptr(req->req.recvb),
1708 &addr->start, sizeof(u64))) {
1709 printk(KERN_ERR "raw1394: arm_register failed "
1710 " address-range-entry is invalid -> EFAULT !!!\n");
1711 vfree(addr->addr_space_buffer);
1712 kfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return (-EFAULT);
1714 }
1715 free_pending_request(req); /* immediate success or fail */
1716 /* INSERT ENTRY */
Stefan Richter3253b662006-09-14 22:05:16 +02001717 spin_lock_irqsave(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 list_add_tail(&addr->addr_list, &fi->addr_list);
1719 spin_unlock_irqrestore(&host_info_lock, flags);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 retval =
1723 hpsb_register_addrspace(&raw1394_highlevel, fi->host, &arm_ops,
1724 req->req.address,
1725 req->req.address + req->req.length);
1726 if (retval) {
1727 /* INSERT ENTRY */
Stefan Richter3253b662006-09-14 22:05:16 +02001728 spin_lock_irqsave(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 list_add_tail(&addr->addr_list, &fi->addr_list);
Stefan Richter3253b662006-09-14 22:05:16 +02001730 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 } else {
1732 DBGMSG("arm_register failed errno: %d \n", retval);
1733 vfree(addr->addr_space_buffer);
1734 kfree(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return (-EALREADY);
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 free_pending_request(req); /* immediate success or fail */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740
1741static int arm_unregister(struct file_info *fi, struct pending_request *req)
1742{
1743 int found = 0;
1744 int retval = 0;
1745 struct list_head *entry;
1746 struct arm_addr *addr = NULL;
1747 struct host_info *hi;
1748 struct file_info *fi_hlp = NULL;
1749 struct arm_addr *arm_addr = NULL;
1750 int another_host;
1751 unsigned long flags;
1752
1753 DBGMSG("arm_Unregister called addr(Offset): "
1754 "%8.8x %8.8x",
1755 (u32) ((req->req.address >> 32) & 0xFFFF),
1756 (u32) (req->req.address & 0xFFFFFFFF));
1757 spin_lock_irqsave(&host_info_lock, flags);
1758 /* get addr */
1759 entry = fi->addr_list.next;
1760 while (entry != &(fi->addr_list)) {
1761 addr = list_entry(entry, struct arm_addr, addr_list);
1762 if (addr->start == req->req.address) {
1763 found = 1;
1764 break;
1765 }
1766 entry = entry->next;
1767 }
1768 if (!found) {
1769 DBGMSG("arm_Unregister addr not found");
1770 spin_unlock_irqrestore(&host_info_lock, flags);
1771 return (-EINVAL);
1772 }
1773 DBGMSG("arm_Unregister addr found");
1774 another_host = 0;
1775 /* another host with valid address-entry containing
1776 same addressrange */
1777 list_for_each_entry(hi, &host_info_list, list) {
1778 if (hi->host != fi->host) {
1779 list_for_each_entry(fi_hlp, &hi->file_info_list, list) {
1780 entry = fi_hlp->addr_list.next;
1781 while (entry != &(fi_hlp->addr_list)) {
1782 arm_addr = list_entry(entry,
1783 struct arm_addr,
1784 addr_list);
1785 if (arm_addr->start == addr->start) {
1786 DBGMSG("another host ownes "
1787 "same addressrange");
1788 another_host = 1;
1789 break;
1790 }
1791 entry = entry->next;
1792 }
1793 if (another_host) {
1794 break;
1795 }
1796 }
1797 }
1798 }
1799 if (another_host) {
1800 DBGMSG("delete entry from list -> success");
1801 list_del(&addr->addr_list);
Stefan Richter3253b662006-09-14 22:05:16 +02001802 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 vfree(addr->addr_space_buffer);
1804 kfree(addr);
1805 free_pending_request(req); /* immediate success or fail */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808 retval =
1809 hpsb_unregister_addrspace(&raw1394_highlevel, fi->host,
1810 addr->start);
1811 if (!retval) {
1812 printk(KERN_ERR "raw1394: arm_Unregister failed -> EINVAL\n");
1813 spin_unlock_irqrestore(&host_info_lock, flags);
1814 return (-EINVAL);
1815 }
1816 DBGMSG("delete entry from list -> success");
1817 list_del(&addr->addr_list);
1818 spin_unlock_irqrestore(&host_info_lock, flags);
1819 vfree(addr->addr_space_buffer);
1820 kfree(addr);
1821 free_pending_request(req); /* immediate success or fail */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825/* Copy data from ARM buffer(s) to user buffer. */
1826static int arm_get_buf(struct file_info *fi, struct pending_request *req)
1827{
1828 struct arm_addr *arm_addr = NULL;
1829 unsigned long flags;
1830 unsigned long offset;
1831
1832 struct list_head *entry;
1833
1834 DBGMSG("arm_get_buf "
1835 "addr(Offset): %04X %08X length: %u",
1836 (u32) ((req->req.address >> 32) & 0xFFFF),
1837 (u32) (req->req.address & 0xFFFFFFFF), (u32) req->req.length);
1838
1839 spin_lock_irqsave(&host_info_lock, flags);
1840 entry = fi->addr_list.next;
1841 while (entry != &(fi->addr_list)) {
1842 arm_addr = list_entry(entry, struct arm_addr, addr_list);
1843 if ((arm_addr->start <= req->req.address) &&
1844 (arm_addr->end > req->req.address)) {
1845 if (req->req.address + req->req.length <= arm_addr->end) {
1846 offset = req->req.address - arm_addr->start;
Stefan Richter3253b662006-09-14 22:05:16 +02001847 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 DBGMSG
1850 ("arm_get_buf copy_to_user( %08X, %p, %u )",
1851 (u32) req->req.recvb,
1852 arm_addr->addr_space_buffer + offset,
1853 (u32) req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (copy_to_user
1855 (int2ptr(req->req.recvb),
1856 arm_addr->addr_space_buffer + offset,
Stefan Richter3253b662006-09-14 22:05:16 +02001857 req->req.length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return (-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* We have to free the request, because we
1861 * queue no response, and therefore nobody
1862 * will free it. */
1863 free_pending_request(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 } else {
1866 DBGMSG("arm_get_buf request exceeded mapping");
1867 spin_unlock_irqrestore(&host_info_lock, flags);
1868 return (-EINVAL);
1869 }
1870 }
1871 entry = entry->next;
1872 }
1873 spin_unlock_irqrestore(&host_info_lock, flags);
1874 return (-EINVAL);
1875}
1876
1877/* Copy data from user buffer to ARM buffer(s). */
1878static int arm_set_buf(struct file_info *fi, struct pending_request *req)
1879{
1880 struct arm_addr *arm_addr = NULL;
1881 unsigned long flags;
1882 unsigned long offset;
1883
1884 struct list_head *entry;
1885
1886 DBGMSG("arm_set_buf "
1887 "addr(Offset): %04X %08X length: %u",
1888 (u32) ((req->req.address >> 32) & 0xFFFF),
1889 (u32) (req->req.address & 0xFFFFFFFF), (u32) req->req.length);
1890
1891 spin_lock_irqsave(&host_info_lock, flags);
1892 entry = fi->addr_list.next;
1893 while (entry != &(fi->addr_list)) {
1894 arm_addr = list_entry(entry, struct arm_addr, addr_list);
1895 if ((arm_addr->start <= req->req.address) &&
1896 (arm_addr->end > req->req.address)) {
1897 if (req->req.address + req->req.length <= arm_addr->end) {
1898 offset = req->req.address - arm_addr->start;
Stefan Richter3253b662006-09-14 22:05:16 +02001899 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 DBGMSG
1902 ("arm_set_buf copy_from_user( %p, %08X, %u )",
1903 arm_addr->addr_space_buffer + offset,
1904 (u32) req->req.sendb,
1905 (u32) req->req.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (copy_from_user
1907 (arm_addr->addr_space_buffer + offset,
1908 int2ptr(req->req.sendb),
Stefan Richter3253b662006-09-14 22:05:16 +02001909 req->req.length))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return (-EFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Stefan Richter3253b662006-09-14 22:05:16 +02001912 /* We have to free the request, because we
1913 * queue no response, and therefore nobody
1914 * will free it. */
1915 free_pending_request(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 } else {
1918 DBGMSG("arm_set_buf request exceeded mapping");
1919 spin_unlock_irqrestore(&host_info_lock, flags);
1920 return (-EINVAL);
1921 }
1922 }
1923 entry = entry->next;
1924 }
1925 spin_unlock_irqrestore(&host_info_lock, flags);
1926 return (-EINVAL);
1927}
1928
1929static int reset_notification(struct file_info *fi, struct pending_request *req)
1930{
1931 DBGMSG("reset_notification called - switch %s ",
1932 (req->req.misc == RAW1394_NOTIFY_OFF) ? "OFF" : "ON");
1933 if ((req->req.misc == RAW1394_NOTIFY_OFF) ||
1934 (req->req.misc == RAW1394_NOTIFY_ON)) {
1935 fi->notification = (u8) req->req.misc;
1936 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 /* error EINVAL (22) invalid argument */
1940 return (-EINVAL);
1941}
1942
1943static int write_phypacket(struct file_info *fi, struct pending_request *req)
1944{
1945 struct hpsb_packet *packet = NULL;
1946 int retval = 0;
1947 quadlet_t data;
Andy Wingo4a9949d2005-10-19 21:23:46 -07001948 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 data = be32_to_cpu((u32) req->req.sendb);
1951 DBGMSG("write_phypacket called - quadlet 0x%8.8x ", data);
1952 packet = hpsb_make_phypacket(fi->host, data);
1953 if (!packet)
1954 return -ENOMEM;
1955 req->req.length = 0;
1956 req->packet = packet;
1957 hpsb_set_packet_complete_task(packet,
1958 (void (*)(void *))queue_complete_cb, req);
Andy Wingo4a9949d2005-10-19 21:23:46 -07001959 spin_lock_irqsave(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 list_add_tail(&req->list, &fi->req_pending);
Andy Wingo4a9949d2005-10-19 21:23:46 -07001961 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 packet->generation = req->req.generation;
1963 retval = hpsb_send_packet(packet);
1964 DBGMSG("write_phypacket send_packet called => retval: %d ", retval);
1965 if (retval < 0) {
1966 req->req.error = RAW1394_ERROR_SEND_ERROR;
1967 req->req.length = 0;
1968 queue_complete_req(req);
1969 }
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static int get_config_rom(struct file_info *fi, struct pending_request *req)
1974{
Petr Vandrovec883b97e2007-05-07 04:14:47 +02001975 int ret = 0;
Christoph Lametere94b1762006-12-06 20:33:17 -08001976 quadlet_t *data = kmalloc(req->req.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int status;
1978
1979 if (!data)
1980 return -ENOMEM;
1981
1982 status =
1983 csr1212_read(fi->host->csr.rom, CSR1212_CONFIG_ROM_SPACE_OFFSET,
1984 data, req->req.length);
1985 if (copy_to_user(int2ptr(req->req.recvb), data, req->req.length))
1986 ret = -EFAULT;
1987 if (copy_to_user
1988 (int2ptr(req->req.tag), &fi->host->csr.rom->cache_head->len,
1989 sizeof(fi->host->csr.rom->cache_head->len)))
1990 ret = -EFAULT;
1991 if (copy_to_user(int2ptr(req->req.address), &fi->host->csr.generation,
1992 sizeof(fi->host->csr.generation)))
1993 ret = -EFAULT;
1994 if (copy_to_user(int2ptr(req->req.sendb), &status, sizeof(status)))
1995 ret = -EFAULT;
1996 kfree(data);
1997 if (ret >= 0) {
1998 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
1999 }
2000 return ret;
2001}
2002
2003static int update_config_rom(struct file_info *fi, struct pending_request *req)
2004{
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002005 int ret = 0;
Christoph Lametere94b1762006-12-06 20:33:17 -08002006 quadlet_t *data = kmalloc(req->req.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!data)
2008 return -ENOMEM;
2009 if (copy_from_user(data, int2ptr(req->req.sendb), req->req.length)) {
2010 ret = -EFAULT;
2011 } else {
2012 int status = hpsb_update_config_rom(fi->host,
2013 data, req->req.length,
2014 (unsigned char)req->req.
2015 misc);
2016 if (copy_to_user
2017 (int2ptr(req->req.recvb), &status, sizeof(status)))
2018 ret = -ENOMEM;
2019 }
2020 kfree(data);
2021 if (ret >= 0) {
2022 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
2023 fi->cfgrom_upd = 1;
2024 }
2025 return ret;
2026}
2027
2028static int modify_config_rom(struct file_info *fi, struct pending_request *req)
2029{
2030 struct csr1212_keyval *kv;
2031 struct csr1212_csr_rom_cache *cache;
2032 struct csr1212_dentry *dentry;
2033 u32 dr;
2034 int ret = 0;
2035
2036 if (req->req.misc == ~0) {
2037 if (req->req.length == 0)
2038 return -EINVAL;
2039
2040 /* Find an unused slot */
2041 for (dr = 0;
2042 dr < RAW1394_MAX_USER_CSR_DIRS && fi->csr1212_dirs[dr];
2043 dr++) ;
2044
2045 if (dr == RAW1394_MAX_USER_CSR_DIRS)
2046 return -ENOMEM;
2047
2048 fi->csr1212_dirs[dr] =
2049 csr1212_new_directory(CSR1212_KV_ID_VENDOR);
2050 if (!fi->csr1212_dirs[dr])
2051 return -ENOMEM;
2052 } else {
2053 dr = req->req.misc;
2054 if (!fi->csr1212_dirs[dr])
2055 return -EINVAL;
2056
2057 /* Delete old stuff */
2058 for (dentry =
2059 fi->csr1212_dirs[dr]->value.directory.dentries_head;
2060 dentry; dentry = dentry->next) {
2061 csr1212_detach_keyval_from_directory(fi->host->csr.rom->
2062 root_kv,
2063 dentry->kv);
2064 }
2065
2066 if (req->req.length == 0) {
2067 csr1212_release_keyval(fi->csr1212_dirs[dr]);
2068 fi->csr1212_dirs[dr] = NULL;
2069
2070 hpsb_update_config_rom_image(fi->host);
2071 free_pending_request(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074 }
2075
2076 cache = csr1212_rom_cache_malloc(0, req->req.length);
2077 if (!cache) {
2078 csr1212_release_keyval(fi->csr1212_dirs[dr]);
2079 fi->csr1212_dirs[dr] = NULL;
2080 return -ENOMEM;
2081 }
2082
Stefan Richter85511582005-11-07 06:31:45 -05002083 cache->filled_head = kmalloc(sizeof(*cache->filled_head), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (!cache->filled_head) {
2085 csr1212_release_keyval(fi->csr1212_dirs[dr]);
2086 fi->csr1212_dirs[dr] = NULL;
2087 CSR1212_FREE(cache);
2088 return -ENOMEM;
2089 }
2090 cache->filled_tail = cache->filled_head;
2091
2092 if (copy_from_user(cache->data, int2ptr(req->req.sendb),
2093 req->req.length)) {
2094 csr1212_release_keyval(fi->csr1212_dirs[dr]);
2095 fi->csr1212_dirs[dr] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ret = -EFAULT;
2097 } else {
2098 cache->len = req->req.length;
2099 cache->filled_head->offset_start = 0;
2100 cache->filled_head->offset_end = cache->size - 1;
2101
2102 cache->layout_head = cache->layout_tail = fi->csr1212_dirs[dr];
2103
2104 ret = CSR1212_SUCCESS;
2105 /* parse all the items */
2106 for (kv = cache->layout_head; ret == CSR1212_SUCCESS && kv;
2107 kv = kv->next) {
2108 ret = csr1212_parse_keyval(kv, cache);
2109 }
2110
2111 /* attach top level items to the root directory */
2112 for (dentry =
2113 fi->csr1212_dirs[dr]->value.directory.dentries_head;
2114 ret == CSR1212_SUCCESS && dentry; dentry = dentry->next) {
2115 ret =
2116 csr1212_attach_keyval_to_directory(fi->host->csr.
2117 rom->root_kv,
2118 dentry->kv);
2119 }
2120
2121 if (ret == CSR1212_SUCCESS) {
2122 ret = hpsb_update_config_rom_image(fi->host);
2123
2124 if (ret >= 0 && copy_to_user(int2ptr(req->req.recvb),
2125 &dr, sizeof(dr))) {
2126 ret = -ENOMEM;
2127 }
2128 }
2129 }
2130 kfree(cache->filled_head);
Stefan Richterb12479d2005-11-21 17:32:18 -05002131 CSR1212_FREE(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 if (ret >= 0) {
2134 /* we have to free the request, because we queue no response,
2135 * and therefore nobody will free it */
2136 free_pending_request(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 } else {
2139 for (dentry =
2140 fi->csr1212_dirs[dr]->value.directory.dentries_head;
2141 dentry; dentry = dentry->next) {
2142 csr1212_detach_keyval_from_directory(fi->host->csr.rom->
2143 root_kv,
2144 dentry->kv);
2145 }
2146 csr1212_release_keyval(fi->csr1212_dirs[dr]);
2147 fi->csr1212_dirs[dr] = NULL;
2148 return ret;
2149 }
2150}
2151
2152static int state_connected(struct file_info *fi, struct pending_request *req)
2153{
2154 int node = req->req.address >> 48;
2155
2156 req->req.error = RAW1394_ERROR_NONE;
2157
2158 switch (req->req.type) {
2159
2160 case RAW1394_REQ_ECHO:
2161 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 case RAW1394_REQ_ARM_REGISTER:
2165 return arm_register(fi, req);
2166
2167 case RAW1394_REQ_ARM_UNREGISTER:
2168 return arm_unregister(fi, req);
2169
2170 case RAW1394_REQ_ARM_SET_BUF:
2171 return arm_set_buf(fi, req);
2172
2173 case RAW1394_REQ_ARM_GET_BUF:
2174 return arm_get_buf(fi, req);
2175
2176 case RAW1394_REQ_RESET_NOTIFY:
2177 return reset_notification(fi, req);
2178
Stefan Richter53c96b42007-06-24 15:31:54 +02002179 case RAW1394_REQ_ISO_SEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 case RAW1394_REQ_ISO_LISTEN:
Stefan Richter53c96b42007-06-24 15:31:54 +02002181 printk(KERN_DEBUG "raw1394: old iso ABI has been removed\n");
2182 req->req.error = RAW1394_ERROR_COMPAT;
2183 req->req.misc = RAW1394_KERNELAPI_VERSION;
2184 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 case RAW1394_REQ_FCP_LISTEN:
2188 handle_fcp_listen(fi, req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 case RAW1394_REQ_RESET_BUS:
2192 if (req->req.misc == RAW1394_LONG_RESET) {
2193 DBGMSG("busreset called (type: LONG)");
2194 hpsb_reset_bus(fi->host, LONG_RESET);
2195 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198 if (req->req.misc == RAW1394_SHORT_RESET) {
2199 DBGMSG("busreset called (type: SHORT)");
2200 hpsb_reset_bus(fi->host, SHORT_RESET);
2201 free_pending_request(req); /* we have to free the request, because we queue no response, and therefore nobody will free it */
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 /* error EINVAL (22) invalid argument */
2205 return (-EINVAL);
2206 case RAW1394_REQ_GET_ROM:
2207 return get_config_rom(fi, req);
2208
2209 case RAW1394_REQ_UPDATE_ROM:
2210 return update_config_rom(fi, req);
2211
2212 case RAW1394_REQ_MODIFY_ROM:
2213 return modify_config_rom(fi, req);
2214 }
2215
2216 if (req->req.generation != get_hpsb_generation(fi->host)) {
2217 req->req.error = RAW1394_ERROR_GENERATION;
2218 req->req.generation = get_hpsb_generation(fi->host);
2219 req->req.length = 0;
2220 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
2224 switch (req->req.type) {
2225 case RAW1394_REQ_PHYPACKET:
2226 return write_phypacket(fi, req);
2227 case RAW1394_REQ_ASYNC_SEND:
2228 return handle_async_send(fi, req);
2229 }
2230
2231 if (req->req.length == 0) {
2232 req->req.error = RAW1394_ERROR_INVALID_ARG;
2233 queue_complete_req(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
2237 return handle_async_request(fi, req, node);
2238}
2239
2240static ssize_t raw1394_write(struct file *file, const char __user * buffer,
2241 size_t count, loff_t * offset_is_ignored)
2242{
2243 struct file_info *fi = (struct file_info *)file->private_data;
2244 struct pending_request *req;
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002245 ssize_t retval = -EBADFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Andi Kleen4bc32c42006-03-25 16:30:07 +01002247#ifdef CONFIG_COMPAT
Ben Collins75970282006-06-12 18:12:10 -04002248 if (count == sizeof(struct compat_raw1394_req) &&
2249 sizeof(struct compat_raw1394_req) !=
2250 sizeof(struct raw1394_request)) {
Andi Kleen4bc32c42006-03-25 16:30:07 +01002251 buffer = raw1394_compat_write(buffer);
2252 if (IS_ERR(buffer))
2253 return PTR_ERR(buffer);
2254 } else
2255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (count != sizeof(struct raw1394_request)) {
2257 return -EINVAL;
2258 }
2259
2260 req = alloc_pending_request();
2261 if (req == NULL) {
2262 return -ENOMEM;
2263 }
2264 req->file_info = fi;
2265
2266 if (copy_from_user(&req->req, buffer, sizeof(struct raw1394_request))) {
2267 free_pending_request(req);
2268 return -EFAULT;
2269 }
2270
2271 switch (fi->state) {
2272 case opened:
2273 retval = state_opened(fi, req);
2274 break;
2275
2276 case initialized:
2277 retval = state_initialized(fi, req);
2278 break;
2279
2280 case connected:
2281 retval = state_connected(fi, req);
2282 break;
2283 }
2284
2285 if (retval < 0) {
2286 free_pending_request(req);
Petr Vandrovec883b97e2007-05-07 04:14:47 +02002287 } else {
2288 BUG_ON(retval);
2289 retval = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291
2292 return retval;
2293}
2294
2295/* rawiso operations */
2296
2297/* check if any RAW1394_REQ_RAWISO_ACTIVITY event is already in the
2298 * completion queue (reqlists_lock must be taken) */
2299static inline int __rawiso_event_in_queue(struct file_info *fi)
2300{
2301 struct pending_request *req;
2302
2303 list_for_each_entry(req, &fi->req_complete, list)
2304 if (req->req.type == RAW1394_REQ_RAWISO_ACTIVITY)
2305 return 1;
2306
2307 return 0;
2308}
2309
2310/* put a RAWISO_ACTIVITY event in the queue, if one isn't there already */
2311static void queue_rawiso_event(struct file_info *fi)
2312{
2313 unsigned long flags;
2314
2315 spin_lock_irqsave(&fi->reqlists_lock, flags);
2316
2317 /* only one ISO activity event may be in the queue */
2318 if (!__rawiso_event_in_queue(fi)) {
2319 struct pending_request *req =
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08002320 __alloc_pending_request(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 if (req) {
2323 req->file_info = fi;
2324 req->req.type = RAW1394_REQ_RAWISO_ACTIVITY;
2325 req->req.generation = get_hpsb_generation(fi->host);
2326 __queue_complete_req(req);
2327 } else {
2328 /* on allocation failure, signal an overflow */
2329 if (fi->iso_handle) {
2330 atomic_inc(&fi->iso_handle->overflows);
2331 }
2332 }
2333 }
2334 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
2335}
2336
2337static void rawiso_activity_cb(struct hpsb_iso *iso)
2338{
2339 unsigned long flags;
2340 struct host_info *hi;
2341 struct file_info *fi;
2342
2343 spin_lock_irqsave(&host_info_lock, flags);
2344 hi = find_host_info(iso->host);
2345
2346 if (hi != NULL) {
2347 list_for_each_entry(fi, &hi->file_info_list, list) {
2348 if (fi->iso_handle == iso)
2349 queue_rawiso_event(fi);
2350 }
2351 }
2352
2353 spin_unlock_irqrestore(&host_info_lock, flags);
2354}
2355
2356/* helper function - gather all the kernel iso status bits for returning to user-space */
2357static void raw1394_iso_fill_status(struct hpsb_iso *iso,
2358 struct raw1394_iso_status *stat)
2359{
Pieter Palmerscc9429b2008-03-19 22:10:59 +01002360 int overflows = atomic_read(&iso->overflows);
2361 int skips = atomic_read(&iso->skips);
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 stat->config.data_buf_size = iso->buf_size;
2364 stat->config.buf_packets = iso->buf_packets;
2365 stat->config.channel = iso->channel;
2366 stat->config.speed = iso->speed;
2367 stat->config.irq_interval = iso->irq_interval;
2368 stat->n_packets = hpsb_iso_n_ready(iso);
Pieter Palmerscc9429b2008-03-19 22:10:59 +01002369 stat->overflows = ((skips & 0xFFFF) << 16) | ((overflows & 0xFFFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 stat->xmit_cycle = iso->xmit_cycle;
2371}
2372
2373static int raw1394_iso_xmit_init(struct file_info *fi, void __user * uaddr)
2374{
2375 struct raw1394_iso_status stat;
2376
2377 if (!fi->host)
2378 return -EINVAL;
2379
2380 if (copy_from_user(&stat, uaddr, sizeof(stat)))
2381 return -EFAULT;
2382
2383 fi->iso_handle = hpsb_iso_xmit_init(fi->host,
2384 stat.config.data_buf_size,
2385 stat.config.buf_packets,
2386 stat.config.channel,
2387 stat.config.speed,
2388 stat.config.irq_interval,
2389 rawiso_activity_cb);
2390 if (!fi->iso_handle)
2391 return -ENOMEM;
2392
2393 fi->iso_state = RAW1394_ISO_XMIT;
2394
2395 raw1394_iso_fill_status(fi->iso_handle, &stat);
2396 if (copy_to_user(uaddr, &stat, sizeof(stat)))
2397 return -EFAULT;
2398
2399 /* queue an event to get things started */
2400 rawiso_activity_cb(fi->iso_handle);
2401
2402 return 0;
2403}
2404
2405static int raw1394_iso_recv_init(struct file_info *fi, void __user * uaddr)
2406{
2407 struct raw1394_iso_status stat;
2408
2409 if (!fi->host)
2410 return -EINVAL;
2411
2412 if (copy_from_user(&stat, uaddr, sizeof(stat)))
2413 return -EFAULT;
2414
2415 fi->iso_handle = hpsb_iso_recv_init(fi->host,
2416 stat.config.data_buf_size,
2417 stat.config.buf_packets,
2418 stat.config.channel,
2419 stat.config.dma_mode,
2420 stat.config.irq_interval,
2421 rawiso_activity_cb);
2422 if (!fi->iso_handle)
2423 return -ENOMEM;
2424
2425 fi->iso_state = RAW1394_ISO_RECV;
2426
2427 raw1394_iso_fill_status(fi->iso_handle, &stat);
2428 if (copy_to_user(uaddr, &stat, sizeof(stat)))
2429 return -EFAULT;
2430 return 0;
2431}
2432
2433static int raw1394_iso_get_status(struct file_info *fi, void __user * uaddr)
2434{
2435 struct raw1394_iso_status stat;
2436 struct hpsb_iso *iso = fi->iso_handle;
2437
2438 raw1394_iso_fill_status(fi->iso_handle, &stat);
2439 if (copy_to_user(uaddr, &stat, sizeof(stat)))
2440 return -EFAULT;
2441
2442 /* reset overflow counter */
2443 atomic_set(&iso->overflows, 0);
Pieter Palmerscc9429b2008-03-19 22:10:59 +01002444 /* reset skip counter */
2445 atomic_set(&iso->skips, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 return 0;
2448}
2449
2450/* copy N packet_infos out of the ringbuffer into user-supplied array */
2451static int raw1394_iso_recv_packets(struct file_info *fi, void __user * uaddr)
2452{
2453 struct raw1394_iso_packets upackets;
2454 unsigned int packet = fi->iso_handle->first_packet;
2455 int i;
2456
2457 if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
2458 return -EFAULT;
2459
2460 if (upackets.n_packets > hpsb_iso_n_ready(fi->iso_handle))
2461 return -EINVAL;
2462
2463 /* ensure user-supplied buffer is accessible and big enough */
2464 if (!access_ok(VERIFY_WRITE, upackets.infos,
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05002465 upackets.n_packets *
2466 sizeof(struct raw1394_iso_packet_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return -EFAULT;
2468
2469 /* copy the packet_infos out */
2470 for (i = 0; i < upackets.n_packets; i++) {
2471 if (__copy_to_user(&upackets.infos[i],
2472 &fi->iso_handle->infos[packet],
2473 sizeof(struct raw1394_iso_packet_info)))
2474 return -EFAULT;
2475
2476 packet = (packet + 1) % fi->iso_handle->buf_packets;
2477 }
2478
2479 return 0;
2480}
2481
2482/* copy N packet_infos from user to ringbuffer, and queue them for transmission */
2483static int raw1394_iso_send_packets(struct file_info *fi, void __user * uaddr)
2484{
2485 struct raw1394_iso_packets upackets;
2486 int i, rv;
2487
2488 if (copy_from_user(&upackets, uaddr, sizeof(upackets)))
2489 return -EFAULT;
2490
Ben Collins1934b8b2005-07-09 20:01:23 -04002491 if (upackets.n_packets >= fi->iso_handle->buf_packets)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return -EINVAL;
2493
Ben Collins1934b8b2005-07-09 20:01:23 -04002494 if (upackets.n_packets >= hpsb_iso_n_ready(fi->iso_handle))
2495 return -EAGAIN;
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /* ensure user-supplied buffer is accessible and big enough */
2498 if (!access_ok(VERIFY_READ, upackets.infos,
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05002499 upackets.n_packets *
2500 sizeof(struct raw1394_iso_packet_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return -EFAULT;
2502
2503 /* copy the infos structs in and queue the packets */
2504 for (i = 0; i < upackets.n_packets; i++) {
2505 struct raw1394_iso_packet_info info;
2506
2507 if (__copy_from_user(&info, &upackets.infos[i],
2508 sizeof(struct raw1394_iso_packet_info)))
2509 return -EFAULT;
2510
2511 rv = hpsb_iso_xmit_queue_packet(fi->iso_handle, info.offset,
2512 info.len, info.tag, info.sy);
2513 if (rv)
2514 return rv;
2515 }
2516
2517 return 0;
2518}
2519
2520static void raw1394_iso_shutdown(struct file_info *fi)
2521{
2522 if (fi->iso_handle)
2523 hpsb_iso_shutdown(fi->iso_handle);
2524
2525 fi->iso_handle = NULL;
2526 fi->iso_state = RAW1394_ISO_INACTIVE;
2527}
2528
Pieter Palmers3dc5ea92007-02-03 17:44:39 +01002529static int raw1394_read_cycle_timer(struct file_info *fi, void __user * uaddr)
2530{
2531 struct raw1394_cycle_timer ct;
2532 int err;
2533
2534 err = hpsb_read_cycle_timer(fi->host, &ct.cycle_timer, &ct.local_time);
2535 if (!err)
2536 if (copy_to_user(uaddr, &ct, sizeof(ct)))
2537 err = -EFAULT;
2538 return err;
2539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541/* mmap the rawiso xmit/recv buffer */
2542static int raw1394_mmap(struct file *file, struct vm_area_struct *vma)
2543{
2544 struct file_info *fi = file->private_data;
Stefan Richter10963ea2008-08-16 00:11:48 +02002545 int ret;
2546
2547 mutex_lock(&fi->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 if (fi->iso_state == RAW1394_ISO_INACTIVE)
Stefan Richter10963ea2008-08-16 00:11:48 +02002550 ret = -EINVAL;
2551 else
2552 ret = dma_region_mmap(&fi->iso_handle->data_buf, file, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Stefan Richter10963ea2008-08-16 00:11:48 +02002554 mutex_unlock(&fi->state_mutex);
2555
2556 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559/* ioctl is only used for rawiso operations */
Alan Cox3aea50a2008-05-23 11:57:41 +01002560static long do_raw1394_ioctl(struct file *file, unsigned int cmd,
2561 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
2563 struct file_info *fi = file->private_data;
2564 void __user *argp = (void __user *)arg;
2565
2566 switch (fi->iso_state) {
2567 case RAW1394_ISO_INACTIVE:
2568 switch (cmd) {
2569 case RAW1394_IOC_ISO_XMIT_INIT:
2570 return raw1394_iso_xmit_init(fi, argp);
2571 case RAW1394_IOC_ISO_RECV_INIT:
2572 return raw1394_iso_recv_init(fi, argp);
2573 default:
2574 break;
2575 }
2576 break;
2577 case RAW1394_ISO_RECV:
2578 switch (cmd) {
2579 case RAW1394_IOC_ISO_RECV_START:{
2580 /* copy args from user-space */
2581 int args[3];
2582 if (copy_from_user
2583 (&args[0], argp, sizeof(args)))
2584 return -EFAULT;
2585 return hpsb_iso_recv_start(fi->iso_handle,
2586 args[0], args[1],
2587 args[2]);
2588 }
2589 case RAW1394_IOC_ISO_XMIT_RECV_STOP:
2590 hpsb_iso_stop(fi->iso_handle);
2591 return 0;
2592 case RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL:
2593 return hpsb_iso_recv_listen_channel(fi->iso_handle,
2594 arg);
2595 case RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL:
2596 return hpsb_iso_recv_unlisten_channel(fi->iso_handle,
2597 arg);
2598 case RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK:{
2599 /* copy the u64 from user-space */
2600 u64 mask;
2601 if (copy_from_user(&mask, argp, sizeof(mask)))
2602 return -EFAULT;
2603 return hpsb_iso_recv_set_channel_mask(fi->
2604 iso_handle,
2605 mask);
2606 }
2607 case RAW1394_IOC_ISO_GET_STATUS:
2608 return raw1394_iso_get_status(fi, argp);
2609 case RAW1394_IOC_ISO_RECV_PACKETS:
2610 return raw1394_iso_recv_packets(fi, argp);
2611 case RAW1394_IOC_ISO_RECV_RELEASE_PACKETS:
2612 return hpsb_iso_recv_release_packets(fi->iso_handle,
2613 arg);
2614 case RAW1394_IOC_ISO_RECV_FLUSH:
2615 return hpsb_iso_recv_flush(fi->iso_handle);
2616 case RAW1394_IOC_ISO_SHUTDOWN:
2617 raw1394_iso_shutdown(fi);
2618 return 0;
2619 case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
2620 queue_rawiso_event(fi);
2621 return 0;
2622 }
2623 break;
2624 case RAW1394_ISO_XMIT:
2625 switch (cmd) {
2626 case RAW1394_IOC_ISO_XMIT_START:{
2627 /* copy two ints from user-space */
2628 int args[2];
2629 if (copy_from_user
2630 (&args[0], argp, sizeof(args)))
2631 return -EFAULT;
2632 return hpsb_iso_xmit_start(fi->iso_handle,
2633 args[0], args[1]);
2634 }
2635 case RAW1394_IOC_ISO_XMIT_SYNC:
2636 return hpsb_iso_xmit_sync(fi->iso_handle);
2637 case RAW1394_IOC_ISO_XMIT_RECV_STOP:
2638 hpsb_iso_stop(fi->iso_handle);
2639 return 0;
2640 case RAW1394_IOC_ISO_GET_STATUS:
2641 return raw1394_iso_get_status(fi, argp);
2642 case RAW1394_IOC_ISO_XMIT_PACKETS:
2643 return raw1394_iso_send_packets(fi, argp);
2644 case RAW1394_IOC_ISO_SHUTDOWN:
2645 raw1394_iso_shutdown(fi);
2646 return 0;
2647 case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
2648 queue_rawiso_event(fi);
2649 return 0;
2650 }
2651 break;
2652 default:
2653 break;
2654 }
2655
Pieter Palmers3dc5ea92007-02-03 17:44:39 +01002656 /* state-independent commands */
2657 switch(cmd) {
2658 case RAW1394_IOC_GET_CYCLE_TIMER:
2659 return raw1394_read_cycle_timer(fi, argp);
2660 default:
2661 break;
2662 }
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return -EINVAL;
2665}
2666
Alan Cox3aea50a2008-05-23 11:57:41 +01002667static long raw1394_ioctl(struct file *file, unsigned int cmd,
2668 unsigned long arg)
2669{
Stefan Richter10963ea2008-08-16 00:11:48 +02002670 struct file_info *fi = file->private_data;
Alan Cox3aea50a2008-05-23 11:57:41 +01002671 long ret;
Stefan Richter10963ea2008-08-16 00:11:48 +02002672
2673 mutex_lock(&fi->state_mutex);
Alan Cox3aea50a2008-05-23 11:57:41 +01002674 ret = do_raw1394_ioctl(file, cmd, arg);
Stefan Richter10963ea2008-08-16 00:11:48 +02002675 mutex_unlock(&fi->state_mutex);
Alan Cox3aea50a2008-05-23 11:57:41 +01002676 return ret;
2677}
2678
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002679#ifdef CONFIG_COMPAT
2680struct raw1394_iso_packets32 {
2681 __u32 n_packets;
2682 compat_uptr_t infos;
2683} __attribute__((packed));
2684
2685struct raw1394_cycle_timer32 {
2686 __u32 cycle_timer;
2687 __u64 local_time;
Stefan Richter19f00e62007-05-21 18:52:06 +02002688}
2689#if defined(CONFIG_X86_64) || defined(CONFIG_IA64)
2690__attribute__((packed))
2691#endif
2692;
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002693
2694#define RAW1394_IOC_ISO_RECV_PACKETS32 \
2695 _IOW ('#', 0x25, struct raw1394_iso_packets32)
2696#define RAW1394_IOC_ISO_XMIT_PACKETS32 \
2697 _IOW ('#', 0x27, struct raw1394_iso_packets32)
2698#define RAW1394_IOC_GET_CYCLE_TIMER32 \
2699 _IOR ('#', 0x30, struct raw1394_cycle_timer32)
2700
2701static long raw1394_iso_xmit_recv_packets32(struct file *file, unsigned int cmd,
2702 struct raw1394_iso_packets32 __user *arg)
2703{
2704 compat_uptr_t infos32;
Al Viro5b26e642007-07-26 17:36:19 +01002705 void __user *infos;
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002706 long err = -EFAULT;
2707 struct raw1394_iso_packets __user *dst = compat_alloc_user_space(sizeof(struct raw1394_iso_packets));
2708
2709 if (!copy_in_user(&dst->n_packets, &arg->n_packets, sizeof arg->n_packets) &&
2710 !copy_from_user(&infos32, &arg->infos, sizeof infos32)) {
2711 infos = compat_ptr(infos32);
2712 if (!copy_to_user(&dst->infos, &infos, sizeof infos))
Alan Cox3aea50a2008-05-23 11:57:41 +01002713 err = do_raw1394_ioctl(file, cmd, (unsigned long)dst);
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002714 }
2715 return err;
2716}
2717
2718static long raw1394_read_cycle_timer32(struct file_info *fi, void __user * uaddr)
2719{
2720 struct raw1394_cycle_timer32 ct;
2721 int err;
2722
2723 err = hpsb_read_cycle_timer(fi->host, &ct.cycle_timer, &ct.local_time);
2724 if (!err)
2725 if (copy_to_user(uaddr, &ct, sizeof(ct)))
2726 err = -EFAULT;
2727 return err;
2728}
2729
2730static long raw1394_compat_ioctl(struct file *file,
2731 unsigned int cmd, unsigned long arg)
2732{
2733 struct file_info *fi = file->private_data;
2734 void __user *argp = (void __user *)arg;
2735 long err;
2736
Stefan Richter10963ea2008-08-16 00:11:48 +02002737 mutex_lock(&fi->state_mutex);
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002738 switch (cmd) {
2739 /* These requests have same format as long as 'int' has same size. */
2740 case RAW1394_IOC_ISO_RECV_INIT:
2741 case RAW1394_IOC_ISO_RECV_START:
2742 case RAW1394_IOC_ISO_RECV_LISTEN_CHANNEL:
2743 case RAW1394_IOC_ISO_RECV_UNLISTEN_CHANNEL:
2744 case RAW1394_IOC_ISO_RECV_SET_CHANNEL_MASK:
2745 case RAW1394_IOC_ISO_RECV_RELEASE_PACKETS:
2746 case RAW1394_IOC_ISO_RECV_FLUSH:
2747 case RAW1394_IOC_ISO_XMIT_RECV_STOP:
2748 case RAW1394_IOC_ISO_XMIT_INIT:
2749 case RAW1394_IOC_ISO_XMIT_START:
2750 case RAW1394_IOC_ISO_XMIT_SYNC:
2751 case RAW1394_IOC_ISO_GET_STATUS:
2752 case RAW1394_IOC_ISO_SHUTDOWN:
2753 case RAW1394_IOC_ISO_QUEUE_ACTIVITY:
Alan Cox3aea50a2008-05-23 11:57:41 +01002754 err = do_raw1394_ioctl(file, cmd, arg);
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002755 break;
2756 /* These request have different format. */
2757 case RAW1394_IOC_ISO_RECV_PACKETS32:
2758 err = raw1394_iso_xmit_recv_packets32(file, RAW1394_IOC_ISO_RECV_PACKETS, argp);
2759 break;
2760 case RAW1394_IOC_ISO_XMIT_PACKETS32:
2761 err = raw1394_iso_xmit_recv_packets32(file, RAW1394_IOC_ISO_XMIT_PACKETS, argp);
2762 break;
2763 case RAW1394_IOC_GET_CYCLE_TIMER32:
2764 err = raw1394_read_cycle_timer32(fi, argp);
2765 break;
2766 default:
2767 err = -EINVAL;
2768 break;
2769 }
Stefan Richter10963ea2008-08-16 00:11:48 +02002770 mutex_unlock(&fi->state_mutex);
Petr Vandrovec650c12c2007-05-07 04:14:47 +02002771
2772 return err;
2773}
2774#endif
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static unsigned int raw1394_poll(struct file *file, poll_table * pt)
2777{
2778 struct file_info *fi = file->private_data;
2779 unsigned int mask = POLLOUT | POLLWRNORM;
Andy Wingo4a9949d2005-10-19 21:23:46 -07002780 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Stefan Richter45289bf2006-07-03 12:02:33 -04002782 poll_wait(file, &fi->wait_complete, pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Andy Wingo4a9949d2005-10-19 21:23:46 -07002784 spin_lock_irqsave(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (!list_empty(&fi->req_complete)) {
2786 mask |= POLLIN | POLLRDNORM;
2787 }
Andy Wingo4a9949d2005-10-19 21:23:46 -07002788 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 return mask;
2791}
2792
2793static int raw1394_open(struct inode *inode, struct file *file)
2794{
2795 struct file_info *fi;
2796
Christoph Lametere94b1762006-12-06 20:33:17 -08002797 fi = kzalloc(sizeof(*fi), GFP_KERNEL);
Stefan Richter85511582005-11-07 06:31:45 -05002798 if (!fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return -ENOMEM;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 fi->notification = (u8) RAW1394_NOTIFY_ON; /* busreset notification */
2802
2803 INIT_LIST_HEAD(&fi->list);
Stefan Richter10963ea2008-08-16 00:11:48 +02002804 mutex_init(&fi->state_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 fi->state = opened;
2806 INIT_LIST_HEAD(&fi->req_pending);
2807 INIT_LIST_HEAD(&fi->req_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 spin_lock_init(&fi->reqlists_lock);
Stefan Richter45289bf2006-07-03 12:02:33 -04002809 init_waitqueue_head(&fi->wait_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 INIT_LIST_HEAD(&fi->addr_list);
2811
2812 file->private_data = fi;
2813
2814 return 0;
2815}
2816
2817static int raw1394_release(struct inode *inode, struct file *file)
2818{
2819 struct file_info *fi = file->private_data;
2820 struct list_head *lh;
2821 struct pending_request *req;
Stefan Richter45289bf2006-07-03 12:02:33 -04002822 int i, fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int retval = 0;
2824 struct list_head *entry;
2825 struct arm_addr *addr = NULL;
2826 struct host_info *hi;
2827 struct file_info *fi_hlp = NULL;
2828 struct arm_addr *arm_addr = NULL;
2829 int another_host;
2830 int csr_mod = 0;
Andy Wingo4a9949d2005-10-19 21:23:46 -07002831 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 if (fi->iso_state != RAW1394_ISO_INACTIVE)
2834 raw1394_iso_shutdown(fi);
2835
Andy Wingo4a9949d2005-10-19 21:23:46 -07002836 spin_lock_irqsave(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 fail = 0;
2839 /* set address-entries invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 while (!list_empty(&fi->addr_list)) {
2842 another_host = 0;
2843 lh = fi->addr_list.next;
2844 addr = list_entry(lh, struct arm_addr, addr_list);
2845 /* another host with valid address-entry containing
2846 same addressrange? */
2847 list_for_each_entry(hi, &host_info_list, list) {
2848 if (hi->host != fi->host) {
2849 list_for_each_entry(fi_hlp, &hi->file_info_list,
2850 list) {
2851 entry = fi_hlp->addr_list.next;
2852 while (entry != &(fi_hlp->addr_list)) {
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05002853 arm_addr = list_entry(entry, struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 arm_addr,
2855 addr_list);
2856 if (arm_addr->start ==
2857 addr->start) {
2858 DBGMSG
2859 ("raw1394_release: "
2860 "another host ownes "
2861 "same addressrange");
2862 another_host = 1;
2863 break;
2864 }
2865 entry = entry->next;
2866 }
2867 if (another_host) {
2868 break;
2869 }
2870 }
2871 }
2872 }
2873 if (!another_host) {
2874 DBGMSG("raw1394_release: call hpsb_arm_unregister");
2875 retval =
2876 hpsb_unregister_addrspace(&raw1394_highlevel,
2877 fi->host, addr->start);
2878 if (!retval) {
2879 ++fail;
2880 printk(KERN_ERR
2881 "raw1394_release arm_Unregister failed\n");
2882 }
2883 }
2884 DBGMSG("raw1394_release: delete addr_entry from list");
2885 list_del(&addr->addr_list);
2886 vfree(addr->addr_space_buffer);
2887 kfree(addr);
2888 } /* while */
Andy Wingo4a9949d2005-10-19 21:23:46 -07002889 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (fail > 0) {
2891 printk(KERN_ERR "raw1394: during addr_list-release "
2892 "error(s) occurred \n");
2893 }
2894
Stefan Richter45289bf2006-07-03 12:02:33 -04002895 for (;;) {
2896 /* This locked section guarantees that neither
2897 * complete nor pending requests exist once i!=0 */
Andy Wingo4a9949d2005-10-19 21:23:46 -07002898 spin_lock_irqsave(&fi->reqlists_lock, flags);
Stefan Richter45289bf2006-07-03 12:02:33 -04002899 while ((req = __next_complete_req(fi)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 free_pending_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Stefan Richter45289bf2006-07-03 12:02:33 -04002902 i = list_empty(&fi->req_pending);
Andy Wingo4a9949d2005-10-19 21:23:46 -07002903 spin_unlock_irqrestore(&fi->reqlists_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Stefan Richter45289bf2006-07-03 12:02:33 -04002905 if (i)
2906 break;
2907 /*
2908 * Sleep until more requests can be freed.
2909 *
2910 * NB: We call the macro wait_event() with a condition argument
2911 * with side effect. This is only possible because the side
2912 * effect does not occur until the condition became true, and
2913 * wait_event() won't evaluate the condition again after that.
2914 */
2915 wait_event(fi->wait_complete, (req = next_complete_req(fi)));
2916 free_pending_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
2919 /* Remove any sub-trees left by user space programs */
2920 for (i = 0; i < RAW1394_MAX_USER_CSR_DIRS; i++) {
2921 struct csr1212_dentry *dentry;
2922 if (!fi->csr1212_dirs[i])
2923 continue;
2924 for (dentry =
2925 fi->csr1212_dirs[i]->value.directory.dentries_head; dentry;
2926 dentry = dentry->next) {
2927 csr1212_detach_keyval_from_directory(fi->host->csr.rom->
2928 root_kv,
2929 dentry->kv);
2930 }
2931 csr1212_release_keyval(fi->csr1212_dirs[i]);
2932 fi->csr1212_dirs[i] = NULL;
2933 csr_mod = 1;
2934 }
2935
2936 if ((csr_mod || fi->cfgrom_upd)
2937 && hpsb_update_config_rom_image(fi->host) < 0)
2938 HPSB_ERR
2939 ("Failed to generate Configuration ROM image for host %d",
2940 fi->host->id);
2941
2942 if (fi->state == connected) {
Andy Wingo4a9949d2005-10-19 21:23:46 -07002943 spin_lock_irqsave(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 list_del(&fi->list);
Andy Wingo4a9949d2005-10-19 21:23:46 -07002945 spin_unlock_irqrestore(&host_info_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 put_device(&fi->host->device);
2948 }
2949
Stefan Richter0fe4c6f2007-02-03 16:48:51 +01002950 spin_lock_irqsave(&host_info_lock, flags);
2951 if (fi->host)
2952 module_put(fi->host->driver->owner);
2953 spin_unlock_irqrestore(&host_info_lock, flags);
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 kfree(fi);
2956
2957 return 0;
2958}
2959
2960/*** HOTPLUG STUFF **********************************************************/
2961/*
2962 * Export information about protocols/devices supported by this driver.
2963 */
Tony Breedse3864972008-04-24 09:02:04 +02002964#ifdef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static struct ieee1394_device_id raw1394_id_table[] = {
2966 {
2967 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2968 .specifier_id = AVC_UNIT_SPEC_ID_ENTRY & 0xffffff,
2969 .version = AVC_SW_VERSION_ENTRY & 0xffffff},
2970 {
2971 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2972 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
2973 .version = CAMERA_SW_VERSION_ENTRY & 0xffffff},
2974 {
2975 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2976 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
2977 .version = (CAMERA_SW_VERSION_ENTRY + 1) & 0xffffff},
2978 {
2979 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
2980 .specifier_id = CAMERA_UNIT_SPEC_ID_ENTRY & 0xffffff,
2981 .version = (CAMERA_SW_VERSION_ENTRY + 2) & 0xffffff},
2982 {}
2983};
2984
2985MODULE_DEVICE_TABLE(ieee1394, raw1394_id_table);
Tony Breedse3864972008-04-24 09:02:04 +02002986#endif /* MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988static struct hpsb_protocol_driver raw1394_driver = {
Ben Collinsed30c262006-11-23 13:59:48 -05002989 .name = "raw1394",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990};
2991
2992/******************************************************************************/
2993
2994static struct hpsb_highlevel raw1394_highlevel = {
2995 .name = RAW1394_DEVICE_NAME,
2996 .add_host = add_host,
2997 .remove_host = remove_host,
2998 .host_reset = host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .fcp_request = fcp_request,
3000};
3001
3002static struct cdev raw1394_cdev;
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08003003static const struct file_operations raw1394_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .owner = THIS_MODULE,
3005 .read = raw1394_read,
3006 .write = raw1394_write,
3007 .mmap = raw1394_mmap,
Alan Cox3aea50a2008-05-23 11:57:41 +01003008 .unlocked_ioctl = raw1394_ioctl,
Petr Vandrovec650c12c2007-05-07 04:14:47 +02003009#ifdef CONFIG_COMPAT
3010 .compat_ioctl = raw1394_compat_ioctl,
3011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 .poll = raw1394_poll,
3013 .open = raw1394_open,
3014 .release = raw1394_release,
3015};
3016
3017static int __init init_raw1394(void)
3018{
3019 int ret = 0;
3020
3021 hpsb_register_highlevel(&raw1394_highlevel);
3022
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05003023 if (IS_ERR
Greg Kroah-Hartmanf71674a2008-05-21 12:52:33 -07003024 (device_create_drvdata(
Kay Sieversdd7f2922007-05-25 11:50:53 +02003025 hpsb_protocol_class, NULL,
3026 MKDEV(IEEE1394_MAJOR, IEEE1394_MINOR_BLOCK_RAW1394 * 16),
Greg Kroah-Hartmanf71674a2008-05-21 12:52:33 -07003027 NULL, RAW1394_DEVICE_NAME))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 ret = -EFAULT;
3029 goto out_unreg;
3030 }
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 cdev_init(&raw1394_cdev, &raw1394_fops);
3033 raw1394_cdev.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 ret = cdev_add(&raw1394_cdev, IEEE1394_RAW1394_DEV, 1);
3035 if (ret) {
3036 HPSB_ERR("raw1394 failed to register minor device block");
3037 goto out_dev;
3038 }
3039
3040 HPSB_INFO("raw1394: /dev/%s device initialized", RAW1394_DEVICE_NAME);
3041
3042 ret = hpsb_register_protocol(&raw1394_driver);
3043 if (ret) {
3044 HPSB_ERR("raw1394: failed to register protocol");
3045 cdev_del(&raw1394_cdev);
3046 goto out_dev;
3047 }
3048
3049 goto out;
3050
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05003051 out_dev:
Kay Sieversdd7f2922007-05-25 11:50:53 +02003052 device_destroy(hpsb_protocol_class,
3053 MKDEV(IEEE1394_MAJOR,
3054 IEEE1394_MINOR_BLOCK_RAW1394 * 16));
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05003055 out_unreg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 hpsb_unregister_highlevel(&raw1394_highlevel);
Jens-Michael Hoffmannc64d4722005-11-22 12:37:10 -05003057 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return ret;
3059}
3060
3061static void __exit cleanup_raw1394(void)
3062{
Kay Sieversdd7f2922007-05-25 11:50:53 +02003063 device_destroy(hpsb_protocol_class,
3064 MKDEV(IEEE1394_MAJOR,
3065 IEEE1394_MINOR_BLOCK_RAW1394 * 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 cdev_del(&raw1394_cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 hpsb_unregister_highlevel(&raw1394_highlevel);
3068 hpsb_unregister_protocol(&raw1394_driver);
3069}
3070
3071module_init(init_raw1394);
3072module_exit(cleanup_raw1394);
3073MODULE_LICENSE("GPL");