blob: 4434f7ca11d535ba68fbcd35048fc7708f91a82b [file] [log] [blame]
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001/*
2 * Char device for device raw access
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05003 *
Kristian Høgsbergc781c062007-05-07 20:33:32 -04004 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
Stefan Richtereb5b35a2010-07-07 14:13:14 +020021#include <linux/bug.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010022#include <linux/compat.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/errno.h>
Stefan Richter77c9a5d2009-06-05 16:26:18 +020026#include <linux/firewire.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010027#include <linux/firewire-cdev.h>
28#include <linux/idr.h>
Stefan Richter4a9bde92010-02-20 22:24:43 +010029#include <linux/irqflags.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010030#include <linux/jiffies.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050031#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010032#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010033#include <linux/mm.h>
34#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020035#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050036#include <linux/poll.h>
Stefan Richterae2a9762010-07-29 09:31:56 +020037#include <linux/sched.h> /* required for linux/wait.h */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040039#include <linux/spinlock.h>
Stefan Richter281e2032010-01-24 16:45:03 +010040#include <linux/string.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010041#include <linux/time.h>
Stefan Richtere034d242009-06-06 18:36:24 +020042#include <linux/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010043#include <linux/vmalloc.h>
44#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010045#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010046
Stefan Richtera64408b2007-09-29 10:41:58 +020047#include <asm/system.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010048
Stefan Richter77c9a5d2009-06-05 16:26:18 +020049#include "core.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050050
Stefan Richter604f4512010-06-20 22:52:55 +020051/*
52 * ABI version history is documented in linux/firewire-cdev.h.
53 */
Stefan Richter8e2b2b42010-07-23 13:05:39 +020054#define FW_CDEV_KERNEL_VERSION 4
55#define FW_CDEV_VERSION_EVENT_REQUEST2 4
56#define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
Stefan Richter604f4512010-06-20 22:52:55 +020057
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050058struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050059 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050060 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010061
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050062 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010063 bool in_shutdown;
64 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050065 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050066 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040067 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050068
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050069 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040070 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050071 struct fw_iso_buffer buffer;
72 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050073
Stefan Richterbf54e142010-07-16 22:25:51 +020074 struct list_head phy_receiver_link;
75 u64 phy_receiver_closure;
76
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050077 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010078 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050079};
80
Stefan Richterfb443032009-01-04 16:23:29 +010081static inline void client_get(struct client *client)
82{
83 kref_get(&client->kref);
84}
85
86static void client_release(struct kref *kref)
87{
88 struct client *client = container_of(kref, struct client, kref);
89
90 fw_device_put(client->device);
91 kfree(client);
92}
93
94static void client_put(struct client *client)
95{
96 kref_put(&client->kref, client_release);
97}
98
Stefan Richter97c18b72009-01-04 16:23:29 +010099struct client_resource;
100typedef void (*client_resource_release_fn_t)(struct client *,
101 struct client_resource *);
102struct client_resource {
103 client_resource_release_fn_t release;
104 int handle;
105};
106
107struct address_handler_resource {
108 struct client_resource resource;
109 struct fw_address_handler handler;
110 __u64 closure;
111 struct client *client;
112};
113
114struct outbound_transaction_resource {
115 struct client_resource resource;
116 struct fw_transaction transaction;
117};
118
119struct inbound_transaction_resource {
120 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400121 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100122 struct fw_request *request;
123 void *data;
124 size_t length;
125};
126
127struct descriptor_resource {
128 struct client_resource resource;
129 struct fw_descriptor descriptor;
130 u32 data[0];
131};
132
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100133struct iso_resource {
134 struct client_resource resource;
135 struct client *client;
136 /* Schedule work and access todo only with client->lock held. */
137 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100138 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
139 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100140 int generation;
141 u64 channels;
142 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200143 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100144 struct iso_resource_event *e_alloc, *e_dealloc;
145};
146
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100147static void release_iso_resource(struct client *, struct client_resource *);
148
Stefan Richter9fb551b2009-10-08 00:41:10 +0200149static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
150{
151 client_get(r->client);
152 if (!schedule_delayed_work(&r->work, delay))
153 client_put(r->client);
154}
155
156static void schedule_if_iso_resource(struct client_resource *resource)
157{
158 if (resource->release == release_iso_resource)
159 schedule_iso_resource(container_of(resource,
160 struct iso_resource, resource), 0);
161}
162
Stefan Richter97c18b72009-01-04 16:23:29 +0100163/*
164 * dequeue_event() just kfree()'s the event, so the event has to be
165 * the first field in a struct XYZ_event.
166 */
167struct event {
168 struct { void *data; size_t size; } v[2];
169 struct list_head link;
170};
171
172struct bus_reset_event {
173 struct event event;
174 struct fw_cdev_event_bus_reset reset;
175};
176
177struct outbound_transaction_event {
178 struct event event;
179 struct client *client;
180 struct outbound_transaction_resource r;
181 struct fw_cdev_event_response response;
182};
183
184struct inbound_transaction_event {
185 struct event event;
Stefan Richtere2055972010-06-20 22:53:55 +0200186 union {
187 struct fw_cdev_event_request request;
188 struct fw_cdev_event_request2 request2;
189 } req;
Stefan Richter97c18b72009-01-04 16:23:29 +0100190};
191
192struct iso_interrupt_event {
193 struct event event;
194 struct fw_cdev_event_iso_interrupt interrupt;
195};
196
Stefan Richter872e3302010-07-29 18:19:22 +0200197struct iso_interrupt_mc_event {
198 struct event event;
199 struct fw_cdev_event_iso_interrupt_mc interrupt;
200};
201
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100202struct iso_resource_event {
203 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200204 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100205};
206
Stefan Richter850bb6f2010-07-16 22:25:14 +0200207struct outbound_phy_packet_event {
208 struct event event;
209 struct client *client;
210 struct fw_packet p;
211 struct fw_cdev_event_phy_packet phy_packet;
212};
213
Stefan Richterbf54e142010-07-16 22:25:51 +0200214struct inbound_phy_packet_event {
215 struct event event;
216 struct fw_cdev_event_phy_packet phy_packet;
217};
218
Stefan Richter53dca512008-12-14 21:47:04 +0100219static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500220{
221 return (void __user *)(unsigned long)value;
222}
223
Stefan Richter53dca512008-12-14 21:47:04 +0100224static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500225{
226 return (__u64)(unsigned long)ptr;
227}
228
229static int fw_device_op_open(struct inode *inode, struct file *file)
230{
231 struct fw_device *device;
232 struct client *client;
233
Stefan Richter96b19062008-02-02 15:01:09 +0100234 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500235 if (device == NULL)
236 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500237
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400238 if (fw_device_is_shutdown(device)) {
239 fw_device_put(device);
240 return -ENODEV;
241 }
242
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400243 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100244 if (client == NULL) {
245 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500246 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100247 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500248
Stefan Richter96b19062008-02-02 15:01:09 +0100249 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500250 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100251 idr_init(&client->resource_idr);
252 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500253 init_waitqueue_head(&client->wait);
Stefan Richterbf54e142010-07-16 22:25:51 +0200254 INIT_LIST_HEAD(&client->phy_receiver_link);
Stefan Richterfb443032009-01-04 16:23:29 +0100255 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500256
257 file->private_data = client;
258
Stefan Richterd67cfb92008-10-05 10:37:11 +0200259 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500260 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200261 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500262
Stefan Richter3ac26b22010-04-10 16:38:05 +0100263 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500264}
265
266static void queue_event(struct client *client, struct event *event,
267 void *data0, size_t size0, void *data1, size_t size1)
268{
269 unsigned long flags;
270
271 event->v[0].data = data0;
272 event->v[0].size = size0;
273 event->v[1].data = data1;
274 event->v[1].size = size1;
275
276 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100277 if (client->in_shutdown)
278 kfree(event);
279 else
280 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500281 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400282
283 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500284}
285
Stefan Richter53dca512008-12-14 21:47:04 +0100286static int dequeue_event(struct client *client,
287 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500288{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500289 struct event *event;
290 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100291 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500292
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100293 ret = wait_event_interruptible(client->wait,
294 !list_empty(&client->event_list) ||
295 fw_device_is_shutdown(client->device));
296 if (ret < 0)
297 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500298
299 if (list_empty(&client->event_list) &&
300 fw_device_is_shutdown(client->device))
301 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500302
Stefan Richter3ba94982009-01-04 16:23:29 +0100303 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100304 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500305 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100306 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500307
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500308 total = 0;
309 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
310 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500311 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100312 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500313 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500314 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500315 total += size;
316 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100317 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500318
319 out:
320 kfree(event);
321
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100322 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500323}
324
Stefan Richter53dca512008-12-14 21:47:04 +0100325static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
326 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500327{
328 struct client *client = file->private_data;
329
330 return dequeue_event(client, buffer, count);
331}
332
Stefan Richter53dca512008-12-14 21:47:04 +0100333static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
334 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500335{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400336 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400337
Stefan Richter3ba94982009-01-04 16:23:29 +0100338 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500339
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400340 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500341 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100342 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400343 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500344 event->local_node_id = card->local_node->node_id;
Stefan Richter250b2b62010-06-21 23:24:35 +0200345 event->bm_node_id = card->bm_node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500346 event->irm_node_id = card->irm_node->node_id;
347 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400348
Stefan Richter3ba94982009-01-04 16:23:29 +0100349 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500350}
351
Stefan Richter53dca512008-12-14 21:47:04 +0100352static void for_each_client(struct fw_device *device,
353 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500354{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500355 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500356
Stefan Richterd67cfb92008-10-05 10:37:11 +0200357 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500358 list_for_each_entry(c, &device->client_list, link)
359 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200360 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500361}
362
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100363static int schedule_reallocations(int id, void *p, void *data)
364{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200365 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100366
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100367 return 0;
368}
369
Stefan Richter53dca512008-12-14 21:47:04 +0100370static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500371{
Stefan Richter97c18b72009-01-04 16:23:29 +0100372 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500373
Stefan Richter97c18b72009-01-04 16:23:29 +0100374 e = kzalloc(sizeof(*e), GFP_KERNEL);
375 if (e == NULL) {
Stefan Richterbf54e142010-07-16 22:25:51 +0200376 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500377 return;
378 }
379
Stefan Richter97c18b72009-01-04 16:23:29 +0100380 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500381
Stefan Richter97c18b72009-01-04 16:23:29 +0100382 queue_event(client, &e->event,
383 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100384
385 spin_lock_irq(&client->lock);
386 idr_for_each(&client->resource_idr, schedule_reallocations, client);
387 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500388}
389
390void fw_device_cdev_update(struct fw_device *device)
391{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500392 for_each_client(device, queue_bus_reset_event);
393}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500394
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500395static void wake_up_client(struct client *client)
396{
397 wake_up_interruptible(&client->wait);
398}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500399
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500400void fw_device_cdev_remove(struct fw_device *device)
401{
402 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500403}
404
Stefan Richter6e95dea2010-02-21 17:56:21 +0100405union ioctl_arg {
406 struct fw_cdev_get_info get_info;
407 struct fw_cdev_send_request send_request;
408 struct fw_cdev_allocate allocate;
409 struct fw_cdev_deallocate deallocate;
410 struct fw_cdev_send_response send_response;
411 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
412 struct fw_cdev_add_descriptor add_descriptor;
413 struct fw_cdev_remove_descriptor remove_descriptor;
414 struct fw_cdev_create_iso_context create_iso_context;
415 struct fw_cdev_queue_iso queue_iso;
416 struct fw_cdev_start_iso start_iso;
417 struct fw_cdev_stop_iso stop_iso;
418 struct fw_cdev_get_cycle_timer get_cycle_timer;
419 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
420 struct fw_cdev_send_stream_packet send_stream_packet;
421 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
Stefan Richter850bb6f2010-07-16 22:25:14 +0200422 struct fw_cdev_send_phy_packet send_phy_packet;
Stefan Richterbf54e142010-07-16 22:25:51 +0200423 struct fw_cdev_receive_phy_packets receive_phy_packets;
Stefan Richter872e3302010-07-29 18:19:22 +0200424 struct fw_cdev_set_iso_channels set_iso_channels;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100425};
426
427static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500428{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100429 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500430 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100431 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500432
Stefan Richter6e95dea2010-02-21 17:56:21 +0100433 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200434 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100435 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500436
Stefan Richterc9755e12008-03-24 20:54:28 +0100437 down_read(&fw_device_rwsem);
438
Stefan Richter6e95dea2010-02-21 17:56:21 +0100439 if (a->rom != 0) {
440 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100441 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500442
Stefan Richter6e95dea2010-02-21 17:56:21 +0100443 ret = copy_to_user(u64_to_uptr(a->rom),
444 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500445 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100446 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500447
Stefan Richterc9755e12008-03-24 20:54:28 +0100448 up_read(&fw_device_rwsem);
449
450 if (ret != 0)
451 return -EFAULT;
452
Stefan Richter6e95dea2010-02-21 17:56:21 +0100453 client->bus_reset_closure = a->bus_reset_closure;
454 if (a->bus_reset != 0) {
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400455 fill_bus_reset_event(&bus_reset, client);
Stefan Richter6e95dea2010-02-21 17:56:21 +0100456 if (copy_to_user(u64_to_uptr(a->bus_reset),
457 &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500458 return -EFAULT;
459 }
460
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500461 return 0;
462}
463
Stefan Richter53dca512008-12-14 21:47:04 +0100464static int add_client_resource(struct client *client,
465 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400466{
467 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100468 int ret;
469
470 retry:
471 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
472 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400473
474 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100475 if (client->in_shutdown)
476 ret = -ECANCELED;
477 else
478 ret = idr_get_new(&client->resource_idr, resource,
479 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100480 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100481 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200482 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100483 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400484 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100485
486 if (ret == -EAGAIN)
487 goto retry;
488
489 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400490}
491
Stefan Richter53dca512008-12-14 21:47:04 +0100492static int release_client_resource(struct client *client, u32 handle,
493 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200494 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400495{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200496 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400497
Stefan Richter3ba94982009-01-04 16:23:29 +0100498 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100499 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200500 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100501 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200502 resource = idr_find(&client->resource_idr, handle);
503 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100504 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100505 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400506
Stefan Richtere21fcf72009-10-08 00:41:38 +0200507 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400508 return -EINVAL;
509
Stefan Richtere21fcf72009-10-08 00:41:38 +0200510 if (return_resource)
511 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400512 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200513 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400514
Stefan Richterfb443032009-01-04 16:23:29 +0100515 client_put(client);
516
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400517 return 0;
518}
519
Stefan Richter53dca512008-12-14 21:47:04 +0100520static void release_transaction(struct client *client,
521 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400522{
Stefan Richter97c18b72009-01-04 16:23:29 +0100523 struct outbound_transaction_resource *r = container_of(resource,
524 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400525
Stefan Richter97c18b72009-01-04 16:23:29 +0100526 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400527}
528
Stefan Richter53dca512008-12-14 21:47:04 +0100529static void complete_transaction(struct fw_card *card, int rcode,
530 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500531{
Stefan Richter97c18b72009-01-04 16:23:29 +0100532 struct outbound_transaction_event *e = data;
533 struct fw_cdev_event_response *rsp = &e->response;
534 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500535 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500536
Stefan Richter97c18b72009-01-04 16:23:29 +0100537 if (length < rsp->length)
538 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500539 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100540 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500541
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500542 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100543 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100544 * 1. If called while in shutdown, the idr tree must be left untouched.
545 * The idr handle will be removed and the client reference will be
546 * dropped later.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100547 */
Clemens Ladisch3e204df2011-01-10 17:28:27 +0100548 if (!client->in_shutdown) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100549 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100550 /* Drop the idr's reference */
551 client_put(client);
552 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500553 spin_unlock_irqrestore(&client->lock, flags);
554
Stefan Richter97c18b72009-01-04 16:23:29 +0100555 rsp->type = FW_CDEV_EVENT_RESPONSE;
556 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700557
558 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100559 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700560 * data, and the read is short, preserve an extra copy of the data
561 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
562 * for short reads and some apps depended on it, this is both safe
563 * and prudent for compatibility.
564 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100565 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
566 queue_event(client, &e->event, rsp, sizeof(*rsp),
567 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700568 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100569 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700570 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100571
572 /* Drop the transaction callback's reference */
573 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500574}
575
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100576static int init_request(struct client *client,
577 struct fw_cdev_send_request *request,
578 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500579{
Stefan Richter97c18b72009-01-04 16:23:29 +0100580 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100581 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500582
Stefan Richter18e9b102009-03-10 21:02:21 +0100583 if (request->tcode != TCODE_STREAM_DATA &&
584 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100585 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500586
Clemens Ladischa8e93f32010-07-07 14:37:30 +0200587 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
588 request->length < 4)
589 return -EINVAL;
590
Stefan Richter97c18b72009-01-04 16:23:29 +0100591 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
592 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593 return -ENOMEM;
594
Stefan Richter97c18b72009-01-04 16:23:29 +0100595 e->client = client;
596 e->response.length = request->length;
597 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500598
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400599 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100600 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400601 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100602 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100603 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100604 }
605
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100606 e->r.resource.release = release_transaction;
607 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
608 if (ret < 0)
609 goto failed;
610
611 /* Get a reference for the transaction callback */
612 client_get(client);
613
614 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100615 request->tcode, destination_id, request->generation,
616 speed, request->offset, e->response.data,
617 request->length, complete_transaction, e);
618 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100619
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100620 failed:
621 kfree(e);
622
623 return ret;
624}
625
Stefan Richter6e95dea2010-02-21 17:56:21 +0100626static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100627{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100628 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100629 case TCODE_WRITE_QUADLET_REQUEST:
630 case TCODE_WRITE_BLOCK_REQUEST:
631 case TCODE_READ_QUADLET_REQUEST:
632 case TCODE_READ_BLOCK_REQUEST:
633 case TCODE_LOCK_MASK_SWAP:
634 case TCODE_LOCK_COMPARE_SWAP:
635 case TCODE_LOCK_FETCH_ADD:
636 case TCODE_LOCK_LITTLE_ADD:
637 case TCODE_LOCK_BOUNDED_ADD:
638 case TCODE_LOCK_WRAP_ADD:
639 case TCODE_LOCK_VENDOR_DEPENDENT:
640 break;
641 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100642 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500643 }
644
Stefan Richter6e95dea2010-02-21 17:56:21 +0100645 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100646 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500647}
648
Stefan Richter281e2032010-01-24 16:45:03 +0100649static inline bool is_fcp_request(struct fw_request *request)
650{
651 return request == NULL;
652}
653
Stefan Richter53dca512008-12-14 21:47:04 +0100654static void release_request(struct client *client,
655 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400656{
Stefan Richter97c18b72009-01-04 16:23:29 +0100657 struct inbound_transaction_resource *r = container_of(resource,
658 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400659
Stefan Richter281e2032010-01-24 16:45:03 +0100660 if (is_fcp_request(r->request))
661 kfree(r->data);
662 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400663 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200664
665 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100666 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400667}
668
Stefan Richter97c18b72009-01-04 16:23:29 +0100669static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100670 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200671 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100672 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500673{
Stefan Richter97c18b72009-01-04 16:23:29 +0100674 struct address_handler_resource *handler = callback_data;
675 struct inbound_transaction_resource *r;
676 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200677 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100678 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100679 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500680
Stefan Richter0244f572010-06-20 22:52:27 +0200681 /* card may be different from handler->client->device->card */
682 fw_card_get(card);
683
Stefan Richter97c18b72009-01-04 16:23:29 +0100684 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400685 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200686 if (r == NULL || e == NULL) {
687 fw_notify("Out of memory when allocating event\n");
Jay Fenlason45ee3192008-12-21 16:47:17 +0100688 goto failed;
Stefan Richterbf54e142010-07-16 22:25:51 +0200689 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400690 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100691 r->request = request;
692 r->data = payload;
693 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500694
Stefan Richter281e2032010-01-24 16:45:03 +0100695 if (is_fcp_request(request)) {
696 /*
697 * FIXME: Let core-transaction.c manage a
698 * single reference-counted copy?
699 */
700 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
701 if (fcp_frame == NULL)
702 goto failed;
703
704 r->data = fcp_frame;
705 }
706
Stefan Richter97c18b72009-01-04 16:23:29 +0100707 r->resource.release = release_request;
708 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100709 if (ret < 0)
710 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500711
Stefan Richtere2055972010-06-20 22:53:55 +0200712 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
713 struct fw_cdev_event_request *req = &e->req.request;
714
715 if (tcode & 0x10)
716 tcode = TCODE_LOCK_REQUEST;
717
718 req->type = FW_CDEV_EVENT_REQUEST;
719 req->tcode = tcode;
720 req->offset = offset;
721 req->length = length;
722 req->handle = r->resource.handle;
723 req->closure = handler->closure;
724 event_size0 = sizeof(*req);
725 } else {
726 struct fw_cdev_event_request2 *req = &e->req.request2;
727
728 req->type = FW_CDEV_EVENT_REQUEST2;
729 req->tcode = tcode;
730 req->offset = offset;
731 req->source_node_id = source;
732 req->destination_node_id = destination;
733 req->card = card->index;
734 req->generation = generation;
735 req->length = length;
736 req->handle = r->resource.handle;
737 req->closure = handler->closure;
738 event_size0 = sizeof(*req);
739 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500740
Stefan Richter97c18b72009-01-04 16:23:29 +0100741 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200742 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100743 return;
744
745 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100746 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100747 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100748 kfree(fcp_frame);
749
750 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100751 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200752
753 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500754}
755
Stefan Richter53dca512008-12-14 21:47:04 +0100756static void release_address_handler(struct client *client,
757 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400758{
Stefan Richter97c18b72009-01-04 16:23:29 +0100759 struct address_handler_resource *r =
760 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400761
Stefan Richter97c18b72009-01-04 16:23:29 +0100762 fw_core_remove_address_handler(&r->handler);
763 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400764}
765
Stefan Richter6e95dea2010-02-21 17:56:21 +0100766static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500767{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100768 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100769 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500770 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100771 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772
Stefan Richter97c18b72009-01-04 16:23:29 +0100773 r = kmalloc(sizeof(*r), GFP_KERNEL);
774 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500775 return -ENOMEM;
776
Stefan Richter6e95dea2010-02-21 17:56:21 +0100777 region.start = a->offset;
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200778 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
779 region.end = a->offset + a->length;
780 else
781 region.end = a->region_end;
782
Stefan Richter6e95dea2010-02-21 17:56:21 +0100783 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100784 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100785 r->handler.callback_data = r;
786 r->closure = a->closure;
787 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500788
Stefan Richter97c18b72009-01-04 16:23:29 +0100789 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100790 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100791 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100792 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500793 }
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200794 a->offset = r->handler.offset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500795
Stefan Richter97c18b72009-01-04 16:23:29 +0100796 r->resource.release = release_address_handler;
797 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100798 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100799 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100800 return ret;
801 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100802 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803
804 return 0;
805}
806
Stefan Richter6e95dea2010-02-21 17:56:21 +0100807static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400808{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100809 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100810 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400811}
812
Stefan Richter6e95dea2010-02-21 17:56:21 +0100813static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500814{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100815 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400816 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100817 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200818 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819
Stefan Richter6e95dea2010-02-21 17:56:21 +0100820 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100821 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500822 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100823
Stefan Richter97c18b72009-01-04 16:23:29 +0100824 r = container_of(resource, struct inbound_transaction_resource,
825 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100826 if (is_fcp_request(r->request))
827 goto out;
828
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200829 if (a->length != fw_get_response_length(r->request)) {
830 ret = -EINVAL;
831 kfree(r->request);
832 goto out;
833 }
834 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100835 ret = -EFAULT;
836 kfree(r->request);
837 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200838 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400839 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200840 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200841 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500842 kfree(r);
843
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200844 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500845}
846
Stefan Richter6e95dea2010-02-21 17:56:21 +0100847static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500848{
Stefan Richter02d37be2010-07-08 16:09:06 +0200849 fw_schedule_bus_reset(client->device->card, true,
Stefan Richter6e95dea2010-02-21 17:56:21 +0100850 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Stefan Richter02d37be2010-07-08 16:09:06 +0200851 return 0;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500852}
853
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400854static void release_descriptor(struct client *client,
855 struct client_resource *resource)
856{
Stefan Richter97c18b72009-01-04 16:23:29 +0100857 struct descriptor_resource *r =
858 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400859
Stefan Richter97c18b72009-01-04 16:23:29 +0100860 fw_core_remove_descriptor(&r->descriptor);
861 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400862}
863
Stefan Richter6e95dea2010-02-21 17:56:21 +0100864static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200865{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100866 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100867 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100868 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200869
Stefan Richterde487da2009-03-10 21:00:23 +0100870 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200871 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100872 return -ENOSYS;
873
Stefan Richter6e95dea2010-02-21 17:56:21 +0100874 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200875 return -EINVAL;
876
Stefan Richter6e95dea2010-02-21 17:56:21 +0100877 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100878 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200879 return -ENOMEM;
880
Stefan Richter6e95dea2010-02-21 17:56:21 +0100881 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100882 ret = -EFAULT;
883 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200884 }
885
Stefan Richter6e95dea2010-02-21 17:56:21 +0100886 r->descriptor.length = a->length;
887 r->descriptor.immediate = a->immediate;
888 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100889 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200890
Stefan Richter97c18b72009-01-04 16:23:29 +0100891 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100892 if (ret < 0)
893 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200894
Stefan Richter97c18b72009-01-04 16:23:29 +0100895 r->resource.release = release_descriptor;
896 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100897 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100898 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100899 goto failed;
900 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100901 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200902
903 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100904 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100905 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100906
907 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200908}
909
Stefan Richter6e95dea2010-02-21 17:56:21 +0100910static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200911{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100912 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100913 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200914}
915
Stefan Richter53dca512008-12-14 21:47:04 +0100916static void iso_callback(struct fw_iso_context *context, u32 cycle,
917 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500918{
919 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100920 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500921
Stefan Richter56d04cb2010-06-08 00:20:10 +0200922 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200923 if (e == NULL) {
924 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500925 return;
Stefan Richterbf54e142010-07-16 22:25:51 +0200926 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100927 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
928 e->interrupt.closure = client->iso_closure;
929 e->interrupt.cycle = cycle;
930 e->interrupt.header_length = header_length;
931 memcpy(e->interrupt.header, header, header_length);
932 queue_event(client, &e->event, &e->interrupt,
933 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500934}
935
Stefan Richter872e3302010-07-29 18:19:22 +0200936static void iso_mc_callback(struct fw_iso_context *context,
937 dma_addr_t completed, void *data)
938{
939 struct client *client = data;
940 struct iso_interrupt_mc_event *e;
941
942 e = kmalloc(sizeof(*e), GFP_ATOMIC);
943 if (e == NULL) {
944 fw_notify("Out of memory when allocating event\n");
945 return;
946 }
947 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
948 e->interrupt.closure = client->iso_closure;
949 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
950 completed);
951 queue_event(client, &e->event, &e->interrupt,
952 sizeof(e->interrupt), NULL, 0);
953}
954
Stefan Richter6e95dea2010-02-21 17:56:21 +0100955static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500956{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100957 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400958 struct fw_iso_context *context;
Stefan Richter872e3302010-07-29 18:19:22 +0200959 fw_iso_callback_t cb;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500960
Stefan Richtereb5b35a2010-07-07 14:13:14 +0200961 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
Stefan Richter872e3302010-07-29 18:19:22 +0200962 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
963 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
964 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500965
Stefan Richter6e95dea2010-02-21 17:56:21 +0100966 switch (a->type) {
Stefan Richter872e3302010-07-29 18:19:22 +0200967 case FW_ISO_CONTEXT_TRANSMIT:
968 if (a->speed > SCODE_3200 || a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400969 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200970
971 cb = iso_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400972 break;
973
Stefan Richter872e3302010-07-29 18:19:22 +0200974 case FW_ISO_CONTEXT_RECEIVE:
975 if (a->header_size < 4 || (a->header_size & 3) ||
976 a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400977 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200978
979 cb = iso_callback;
980 break;
981
982 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
983 cb = (fw_iso_callback_t)iso_mc_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400984 break;
985
986 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500987 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400988 }
989
Stefan Richter6e95dea2010-02-21 17:56:21 +0100990 context = fw_iso_context_create(client->device->card, a->type,
Stefan Richter872e3302010-07-29 18:19:22 +0200991 a->channel, a->speed, a->header_size, cb, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400992 if (IS_ERR(context))
993 return PTR_ERR(context);
994
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200995 /* We only support one context at this time. */
996 spin_lock_irq(&client->lock);
997 if (client->iso_context != NULL) {
998 spin_unlock_irq(&client->lock);
999 fw_iso_context_destroy(context);
1000 return -EBUSY;
1001 }
Stefan Richter6e95dea2010-02-21 17:56:21 +01001002 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -04001003 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001004 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001005
Stefan Richter6e95dea2010-02-21 17:56:21 +01001006 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001007
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001008 return 0;
1009}
1010
Stefan Richter872e3302010-07-29 18:19:22 +02001011static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1012{
1013 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1014 struct fw_iso_context *ctx = client->iso_context;
1015
1016 if (ctx == NULL || a->handle != 0)
1017 return -EINVAL;
1018
1019 return fw_iso_context_set_channels(ctx, &a->channels);
1020}
1021
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001022/* Macros for decoding the iso packet control header. */
1023#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1024#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1025#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +02001026#define GET_TAG(v) (((v) >> 18) & 0x03)
1027#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001028#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1029
Stefan Richter6e95dea2010-02-21 17:56:21 +01001030static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001031{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001032 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001033 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001034 struct fw_iso_context *ctx = client->iso_context;
Stefan Richter872e3302010-07-29 18:19:22 +02001035 unsigned long payload, buffer_end, transmit_header_bytes = 0;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001036 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001037 int count;
1038 struct {
1039 struct fw_iso_packet packet;
1040 u8 header[256];
1041 } u;
1042
Stefan Richter6e95dea2010-02-21 17:56:21 +01001043 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001044 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001045
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001046 /*
1047 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001048 * the iso buffer, and the pointer points inside the buffer,
1049 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001050 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001051 * payload_length == 0 through. In other words, if no packets
1052 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +01001053 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001054 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001055 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001056 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001057 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001058 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001059 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001060 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001061 }
1062
Stefan Richter872e3302010-07-29 18:19:22 +02001063 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1064 return -EINVAL;
Al Viro1ccc9142007-10-14 19:34:40 +01001065
Stefan Richter872e3302010-07-29 18:19:22 +02001066 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Stefan Richter6e95dea2010-02-21 17:56:21 +01001067 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001068 return -EFAULT;
1069
Stefan Richter6e95dea2010-02-21 17:56:21 +01001070 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001071 count = 0;
1072 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001073 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001074 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001075 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1076 u.packet.interrupt = GET_INTERRUPT(control);
1077 u.packet.skip = GET_SKIP(control);
1078 u.packet.tag = GET_TAG(control);
1079 u.packet.sy = GET_SY(control);
1080 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001081
Stefan Richter872e3302010-07-29 18:19:22 +02001082 switch (ctx->type) {
1083 case FW_ISO_CONTEXT_TRANSMIT:
1084 if (u.packet.header_length & 3)
Clemens Ladisch385ab5b2010-03-31 16:26:46 +02001085 return -EINVAL;
Stefan Richterae2a9762010-07-29 09:31:56 +02001086 transmit_header_bytes = u.packet.header_length;
Stefan Richter872e3302010-07-29 18:19:22 +02001087 break;
1088
1089 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter69e61d02010-07-28 23:49:45 +02001090 if (u.packet.header_length == 0 ||
1091 u.packet.header_length % ctx->header_size != 0)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001092 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +02001093 break;
1094
1095 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1096 if (u.packet.payload_length == 0 ||
1097 u.packet.payload_length & 3)
1098 return -EINVAL;
1099 break;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001100 }
1101
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001102 next = (struct fw_cdev_iso_packet __user *)
Stefan Richterae2a9762010-07-29 09:31:56 +02001103 &p->header[transmit_header_bytes / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001104 if (next > end)
1105 return -EINVAL;
1106 if (__copy_from_user
Stefan Richterae2a9762010-07-29 09:31:56 +02001107 (u.packet.header, p->header, transmit_header_bytes))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001108 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001109 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001110 u.packet.header_length + u.packet.payload_length > 0)
1111 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001112 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001113 return -EINVAL;
1114
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001115 if (fw_iso_context_queue(ctx, &u.packet,
1116 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001117 break;
1118
1119 p = next;
1120 payload += u.packet.payload_length;
1121 count++;
1122 }
1123
Stefan Richter6e95dea2010-02-21 17:56:21 +01001124 a->size -= uptr_to_u64(p) - a->packets;
1125 a->packets = uptr_to_u64(p);
1126 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001127
1128 return count;
1129}
1130
Stefan Richter6e95dea2010-02-21 17:56:21 +01001131static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001132{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001133 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001134
Stefan Richtereb5b35a2010-07-07 14:13:14 +02001135 BUILD_BUG_ON(
1136 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1137 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1138 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1139 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1140 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1141
Stefan Richter6e95dea2010-02-21 17:56:21 +01001142 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001143 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001144
Stefan Richter6e95dea2010-02-21 17:56:21 +01001145 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1146 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1147 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001148
Stefan Richter6e95dea2010-02-21 17:56:21 +01001149 return fw_iso_context_start(client->iso_context,
1150 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001151}
1152
Stefan Richter6e95dea2010-02-21 17:56:21 +01001153static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001154{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001155 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001156
Stefan Richter6e95dea2010-02-21 17:56:21 +01001157 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001158 return -EINVAL;
1159
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001160 return fw_iso_context_stop(client->iso_context);
1161}
1162
Stefan Richter6e95dea2010-02-21 17:56:21 +01001163static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001164{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001165 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001166 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001167 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001168 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001169 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001170
Stefan Richter4a9bde92010-02-20 22:24:43 +01001171 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001172
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001173 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001174
Stefan Richter6e95dea2010-02-21 17:56:21 +01001175 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001176 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1177 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1178 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1179 default:
1180 ret = -EINVAL;
1181 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001182
Stefan Richter4a9bde92010-02-20 22:24:43 +01001183 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001184
Stefan Richter6e95dea2010-02-21 17:56:21 +01001185 a->tv_sec = ts.tv_sec;
1186 a->tv_nsec = ts.tv_nsec;
1187 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001188
1189 return ret;
1190}
1191
Stefan Richter6e95dea2010-02-21 17:56:21 +01001192static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001193{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001194 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001195 struct fw_cdev_get_cycle_timer2 ct2;
1196
1197 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001198 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001199
Stefan Richter6e95dea2010-02-21 17:56:21 +01001200 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1201 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001202
Stefan Richtera64408b2007-09-29 10:41:58 +02001203 return 0;
1204}
1205
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001206static void iso_resource_work(struct work_struct *work)
1207{
1208 struct iso_resource_event *e;
1209 struct iso_resource *r =
1210 container_of(work, struct iso_resource, work.work);
1211 struct client *client = r->client;
1212 int generation, channel, bandwidth, todo;
1213 bool skip, free, success;
1214
1215 spin_lock_irq(&client->lock);
1216 generation = client->device->generation;
1217 todo = r->todo;
1218 /* Allow 1000ms grace period for other reallocations. */
1219 if (todo == ISO_RES_ALLOC &&
1220 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001221 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001222 skip = true;
1223 } else {
1224 /* We could be called twice within the same generation. */
1225 skip = todo == ISO_RES_REALLOC &&
1226 r->generation == generation;
1227 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001228 free = todo == ISO_RES_DEALLOC ||
1229 todo == ISO_RES_ALLOC_ONCE ||
1230 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001231 r->generation = generation;
1232 spin_unlock_irq(&client->lock);
1233
1234 if (skip)
1235 goto out;
1236
1237 bandwidth = r->bandwidth;
1238
1239 fw_iso_resource_manage(client->device->card, generation,
1240 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001241 todo == ISO_RES_ALLOC ||
1242 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001243 todo == ISO_RES_ALLOC_ONCE,
1244 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001245 /*
1246 * Is this generation outdated already? As long as this resource sticks
1247 * in the idr, it will be scheduled again for a newer generation or at
1248 * shutdown.
1249 */
1250 if (channel == -EAGAIN &&
1251 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1252 goto out;
1253
1254 success = channel >= 0 || bandwidth > 0;
1255
1256 spin_lock_irq(&client->lock);
1257 /*
1258 * Transit from allocation to reallocation, except if the client
1259 * requested deallocation in the meantime.
1260 */
1261 if (r->todo == ISO_RES_ALLOC)
1262 r->todo = ISO_RES_REALLOC;
1263 /*
1264 * Allocation or reallocation failure? Pull this resource out of the
1265 * idr and prepare for deletion, unless the client is shutting down.
1266 */
1267 if (r->todo == ISO_RES_REALLOC && !success &&
1268 !client->in_shutdown &&
1269 idr_find(&client->resource_idr, r->resource.handle)) {
1270 idr_remove(&client->resource_idr, r->resource.handle);
1271 client_put(client);
1272 free = true;
1273 }
1274 spin_unlock_irq(&client->lock);
1275
1276 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d2009-01-08 23:07:40 +01001277 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001278
1279 if (todo == ISO_RES_REALLOC && success)
1280 goto out;
1281
Stefan Richter1ec3c022009-01-04 16:23:29 +01001282 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001283 e = r->e_alloc;
1284 r->e_alloc = NULL;
1285 } else {
1286 e = r->e_dealloc;
1287 r->e_dealloc = NULL;
1288 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001289 e->iso_resource.handle = r->resource.handle;
1290 e->iso_resource.channel = channel;
1291 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001292
1293 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001294 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001295
1296 if (free) {
1297 cancel_delayed_work(&r->work);
1298 kfree(r->e_alloc);
1299 kfree(r->e_dealloc);
1300 kfree(r);
1301 }
1302 out:
1303 client_put(client);
1304}
1305
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001306static void release_iso_resource(struct client *client,
1307 struct client_resource *resource)
1308{
1309 struct iso_resource *r =
1310 container_of(resource, struct iso_resource, resource);
1311
1312 spin_lock_irq(&client->lock);
1313 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001314 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001315 spin_unlock_irq(&client->lock);
1316}
1317
Stefan Richter1ec3c022009-01-04 16:23:29 +01001318static int init_iso_resource(struct client *client,
1319 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001320{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001321 struct iso_resource_event *e1, *e2;
1322 struct iso_resource *r;
1323 int ret;
1324
1325 if ((request->channels == 0 && request->bandwidth == 0) ||
1326 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1327 request->bandwidth < 0)
1328 return -EINVAL;
1329
1330 r = kmalloc(sizeof(*r), GFP_KERNEL);
1331 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1332 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1333 if (r == NULL || e1 == NULL || e2 == NULL) {
1334 ret = -ENOMEM;
1335 goto fail;
1336 }
1337
1338 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1339 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001340 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001341 r->generation = -1;
1342 r->channels = request->channels;
1343 r->bandwidth = request->bandwidth;
1344 r->e_alloc = e1;
1345 r->e_dealloc = e2;
1346
Stefan Richtere21fcf72009-10-08 00:41:38 +02001347 e1->iso_resource.closure = request->closure;
1348 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1349 e2->iso_resource.closure = request->closure;
1350 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001351
Stefan Richter1ec3c022009-01-04 16:23:29 +01001352 if (todo == ISO_RES_ALLOC) {
1353 r->resource.release = release_iso_resource;
1354 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001355 if (ret < 0)
1356 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001357 } else {
1358 r->resource.release = NULL;
1359 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001360 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001361 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001362 request->handle = r->resource.handle;
1363
1364 return 0;
1365 fail:
1366 kfree(r);
1367 kfree(e1);
1368 kfree(e2);
1369
1370 return ret;
1371}
1372
Stefan Richter6e95dea2010-02-21 17:56:21 +01001373static int ioctl_allocate_iso_resource(struct client *client,
1374 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001375{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001376 return init_iso_resource(client,
1377 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001378}
1379
Stefan Richter6e95dea2010-02-21 17:56:21 +01001380static int ioctl_deallocate_iso_resource(struct client *client,
1381 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001382{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001383 return release_client_resource(client,
1384 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001385}
1386
Stefan Richter6e95dea2010-02-21 17:56:21 +01001387static int ioctl_allocate_iso_resource_once(struct client *client,
1388 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001389{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001390 return init_iso_resource(client,
1391 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001392}
1393
Stefan Richter6e95dea2010-02-21 17:56:21 +01001394static int ioctl_deallocate_iso_resource_once(struct client *client,
1395 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001396{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001397 return init_iso_resource(client,
1398 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001399}
1400
Stefan Richterc8a25902009-03-10 20:59:16 +01001401/*
1402 * Returns a speed code: Maximum speed to or from this device,
1403 * limited by the device's link speed, the local node's link speed,
1404 * and all PHY port speeds between the two links.
1405 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001406static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001407{
Stefan Richterc8a25902009-03-10 20:59:16 +01001408 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001409}
1410
Stefan Richter6e95dea2010-02-21 17:56:21 +01001411static int ioctl_send_broadcast_request(struct client *client,
1412 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001413{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001414 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001415
Stefan Richter6e95dea2010-02-21 17:56:21 +01001416 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001417 case TCODE_WRITE_QUADLET_REQUEST:
1418 case TCODE_WRITE_BLOCK_REQUEST:
1419 break;
1420 default:
1421 return -EINVAL;
1422 }
1423
Stefan Richter1566f3d2009-01-04 16:23:29 +01001424 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001425 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001426 return -EACCES;
1427
Stefan Richter6e95dea2010-02-21 17:56:21 +01001428 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001429}
1430
Stefan Richter6e95dea2010-02-21 17:56:21 +01001431static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001432{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001433 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b102009-03-10 21:02:21 +01001434 struct fw_cdev_send_request request;
1435 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001436
Stefan Richter6e95dea2010-02-21 17:56:21 +01001437 if (a->speed > client->device->card->link_speed ||
1438 a->length > 1024 << a->speed)
Stefan Richter18e9b102009-03-10 21:02:21 +01001439 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001440
Stefan Richter6e95dea2010-02-21 17:56:21 +01001441 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b102009-03-10 21:02:21 +01001442 return -EINVAL;
1443
Stefan Richter6e95dea2010-02-21 17:56:21 +01001444 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b102009-03-10 21:02:21 +01001445 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001446 request.length = a->length;
1447 request.closure = a->closure;
1448 request.data = a->data;
1449 request.generation = a->generation;
Stefan Richter18e9b102009-03-10 21:02:21 +01001450
Stefan Richter6e95dea2010-02-21 17:56:21 +01001451 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001452}
1453
Stefan Richter850bb6f2010-07-16 22:25:14 +02001454static void outbound_phy_packet_callback(struct fw_packet *packet,
1455 struct fw_card *card, int status)
1456{
1457 struct outbound_phy_packet_event *e =
1458 container_of(packet, struct outbound_phy_packet_event, p);
1459
1460 switch (status) {
1461 /* expected: */
1462 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1463 /* should never happen with PHY packets: */
1464 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1465 case ACK_BUSY_X:
1466 case ACK_BUSY_A:
1467 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1468 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1469 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1470 /* stale generation; cancelled; on certain controllers: no ack */
1471 default: e->phy_packet.rcode = status; break;
1472 }
Stefan Richtercc550212010-07-18 13:00:50 +02001473 e->phy_packet.data[0] = packet->timestamp;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001474
Stefan Richtercc550212010-07-18 13:00:50 +02001475 queue_event(e->client, &e->event, &e->phy_packet,
1476 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001477 client_put(e->client);
1478}
1479
1480static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1481{
1482 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1483 struct fw_card *card = client->device->card;
1484 struct outbound_phy_packet_event *e;
1485
1486 /* Access policy: Allow this ioctl only on local nodes' device files. */
1487 if (!client->device->is_local)
1488 return -ENOSYS;
1489
Stefan Richtercc550212010-07-18 13:00:50 +02001490 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001491 if (e == NULL)
1492 return -ENOMEM;
1493
1494 client_get(client);
1495 e->client = client;
1496 e->p.speed = SCODE_100;
1497 e->p.generation = a->generation;
Clemens Ladisch5b06db12010-11-30 08:24:47 +01001498 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1499 e->p.header[1] = a->data[0];
1500 e->p.header[2] = a->data[1];
1501 e->p.header_length = 12;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001502 e->p.callback = outbound_phy_packet_callback;
1503 e->phy_packet.closure = a->closure;
1504 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
Stefan Richtercc550212010-07-18 13:00:50 +02001505 if (is_ping_packet(a->data))
1506 e->phy_packet.length = 4;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001507
1508 card->driver->send_request(card, &e->p);
1509
1510 return 0;
1511}
1512
Stefan Richterbf54e142010-07-16 22:25:51 +02001513static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1514{
1515 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1516 struct fw_card *card = client->device->card;
1517
1518 /* Access policy: Allow this ioctl only on local nodes' device files. */
1519 if (!client->device->is_local)
1520 return -ENOSYS;
1521
1522 spin_lock_irq(&card->lock);
1523
1524 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1525 client->phy_receiver_closure = a->closure;
1526
1527 spin_unlock_irq(&card->lock);
1528
1529 return 0;
1530}
1531
1532void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1533{
1534 struct client *client;
1535 struct inbound_phy_packet_event *e;
1536 unsigned long flags;
1537
1538 spin_lock_irqsave(&card->lock, flags);
1539
1540 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1541 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1542 if (e == NULL) {
1543 fw_notify("Out of memory when allocating event\n");
1544 break;
1545 }
1546 e->phy_packet.closure = client->phy_receiver_closure;
1547 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1548 e->phy_packet.rcode = RCODE_COMPLETE;
1549 e->phy_packet.length = 8;
1550 e->phy_packet.data[0] = p->header[1];
1551 e->phy_packet.data[1] = p->header[2];
1552 queue_event(client, &e->event,
1553 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1554 }
1555
1556 spin_unlock_irqrestore(&card->lock, flags);
1557}
1558
Stefan Richter6e95dea2010-02-21 17:56:21 +01001559static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Stefan Richterb9dc61c2010-07-16 22:24:29 +02001560 [0x00] = ioctl_get_info,
1561 [0x01] = ioctl_send_request,
1562 [0x02] = ioctl_allocate,
1563 [0x03] = ioctl_deallocate,
1564 [0x04] = ioctl_send_response,
1565 [0x05] = ioctl_initiate_bus_reset,
1566 [0x06] = ioctl_add_descriptor,
1567 [0x07] = ioctl_remove_descriptor,
1568 [0x08] = ioctl_create_iso_context,
1569 [0x09] = ioctl_queue_iso,
1570 [0x0a] = ioctl_start_iso,
1571 [0x0b] = ioctl_stop_iso,
1572 [0x0c] = ioctl_get_cycle_timer,
1573 [0x0d] = ioctl_allocate_iso_resource,
1574 [0x0e] = ioctl_deallocate_iso_resource,
1575 [0x0f] = ioctl_allocate_iso_resource_once,
1576 [0x10] = ioctl_deallocate_iso_resource_once,
1577 [0x11] = ioctl_get_speed,
1578 [0x12] = ioctl_send_broadcast_request,
1579 [0x13] = ioctl_send_stream_packet,
1580 [0x14] = ioctl_get_cycle_timer2,
Stefan Richter850bb6f2010-07-16 22:25:14 +02001581 [0x15] = ioctl_send_phy_packet,
Stefan Richterbf54e142010-07-16 22:25:51 +02001582 [0x16] = ioctl_receive_phy_packets,
Stefan Richter872e3302010-07-29 18:19:22 +02001583 [0x17] = ioctl_set_iso_channels,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001584};
1585
Stefan Richter53dca512008-12-14 21:47:04 +01001586static int dispatch_ioctl(struct client *client,
1587 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001588{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001589 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001590 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001591
Stefan Richter64582292010-02-21 17:56:42 +01001592 if (fw_device_is_shutdown(client->device))
1593 return -ENODEV;
1594
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001595 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001596 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1597 _IOC_SIZE(cmd) > sizeof(buffer))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001598 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001599
Stefan Richter9cac00b2010-04-07 08:30:50 +02001600 if (_IOC_DIR(cmd) == _IOC_READ)
1601 memset(&buffer, 0, _IOC_SIZE(cmd));
1602
1603 if (_IOC_DIR(cmd) & _IOC_WRITE)
1604 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001605 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001606
Stefan Richter6e95dea2010-02-21 17:56:21 +01001607 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001608 if (ret < 0)
1609 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001610
Stefan Richter9cac00b2010-04-07 08:30:50 +02001611 if (_IOC_DIR(cmd) & _IOC_READ)
1612 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001613 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001614
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001615 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001616}
1617
Stefan Richter53dca512008-12-14 21:47:04 +01001618static long fw_device_op_ioctl(struct file *file,
1619 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001620{
Stefan Richter64582292010-02-21 17:56:42 +01001621 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001622}
1623
1624#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001625static long fw_device_op_compat_ioctl(struct file *file,
1626 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001627{
Stefan Richter64582292010-02-21 17:56:42 +01001628 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001629}
1630#endif
1631
1632static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1633{
1634 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001635 enum dma_data_direction direction;
1636 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001637 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001638
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001639 if (fw_device_is_shutdown(client->device))
1640 return -ENODEV;
1641
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001642 /* FIXME: We could support multiple buffers, but we don't. */
1643 if (client->buffer.pages != NULL)
1644 return -EBUSY;
1645
1646 if (!(vma->vm_flags & VM_SHARED))
1647 return -EINVAL;
1648
1649 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001650 return -EINVAL;
1651
1652 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001653 size = vma->vm_end - vma->vm_start;
1654 page_count = size >> PAGE_SHIFT;
1655 if (size & ~PAGE_MASK)
1656 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001657
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001658 if (vma->vm_flags & VM_WRITE)
1659 direction = DMA_TO_DEVICE;
1660 else
1661 direction = DMA_FROM_DEVICE;
1662
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001663 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1664 page_count, direction);
1665 if (ret < 0)
1666 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001667
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001668 ret = fw_iso_buffer_map(&client->buffer, vma);
1669 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001670 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1671
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001672 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001673}
1674
Jay Fenlason45ee3192008-12-21 16:47:17 +01001675static int shutdown_resource(int id, void *p, void *data)
1676{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001677 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001678 struct client *client = data;
1679
Stefan Richtere21fcf72009-10-08 00:41:38 +02001680 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001681 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001682
1683 return 0;
1684}
1685
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001686static int fw_device_op_release(struct inode *inode, struct file *file)
1687{
1688 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001689 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001690
Stefan Richterbf54e142010-07-16 22:25:51 +02001691 spin_lock_irq(&client->device->card->lock);
1692 list_del(&client->phy_receiver_link);
1693 spin_unlock_irq(&client->device->card->lock);
1694
Stefan Richter97811e32008-12-14 19:19:23 +01001695 mutex_lock(&client->device->client_list_mutex);
1696 list_del(&client->link);
1697 mutex_unlock(&client->device->client_list_mutex);
1698
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001699 if (client->iso_context)
1700 fw_iso_context_destroy(client->iso_context);
1701
Stefan Richter36a755c2009-01-05 20:28:10 +01001702 if (client->buffer.pages)
1703 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1704
Jay Fenlason45ee3192008-12-21 16:47:17 +01001705 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001706 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001707 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001708 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001709
Jay Fenlason45ee3192008-12-21 16:47:17 +01001710 idr_for_each(&client->resource_idr, shutdown_resource, client);
1711 idr_remove_all(&client->resource_idr);
1712 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001713
Stefan Richtere21fcf72009-10-08 00:41:38 +02001714 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1715 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001716
Stefan Richterfb443032009-01-04 16:23:29 +01001717 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001718
1719 return 0;
1720}
1721
1722static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1723{
1724 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001725 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001726
1727 poll_wait(file, &client->wait, pt);
1728
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001729 if (fw_device_is_shutdown(client->device))
1730 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001731 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001732 mask |= POLLIN | POLLRDNORM;
1733
1734 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001735}
1736
Stefan Richter21ebcd12007-01-14 15:29:07 +01001737const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001738 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001739 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001740 .open = fw_device_op_open,
1741 .read = fw_device_op_read,
1742 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001743 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001744 .release = fw_device_op_release,
1745 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001746#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001747 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001748#endif
1749};