blob: e6ad3bb6c1a6b5efad00df79995f733be9a9c1f7 [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 Richterae2a97662010-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;
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +010067 wait_queue_head_t tx_flush_wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040068 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050069
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050070 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040071 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050072 struct fw_iso_buffer buffer;
73 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050074
Stefan Richterbf54e142010-07-16 22:25:51 +020075 struct list_head phy_receiver_link;
76 u64 phy_receiver_closure;
77
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050078 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010079 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050080};
81
Stefan Richterfb443032009-01-04 16:23:29 +010082static inline void client_get(struct client *client)
83{
84 kref_get(&client->kref);
85}
86
87static void client_release(struct kref *kref)
88{
89 struct client *client = container_of(kref, struct client, kref);
90
91 fw_device_put(client->device);
92 kfree(client);
93}
94
95static void client_put(struct client *client)
96{
97 kref_put(&client->kref, client_release);
98}
99
Stefan Richter97c18b72009-01-04 16:23:29 +0100100struct client_resource;
101typedef void (*client_resource_release_fn_t)(struct client *,
102 struct client_resource *);
103struct client_resource {
104 client_resource_release_fn_t release;
105 int handle;
106};
107
108struct address_handler_resource {
109 struct client_resource resource;
110 struct fw_address_handler handler;
111 __u64 closure;
112 struct client *client;
113};
114
115struct outbound_transaction_resource {
116 struct client_resource resource;
117 struct fw_transaction transaction;
118};
119
120struct inbound_transaction_resource {
121 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400122 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100123 struct fw_request *request;
124 void *data;
125 size_t length;
126};
127
128struct descriptor_resource {
129 struct client_resource resource;
130 struct fw_descriptor descriptor;
131 u32 data[0];
132};
133
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100134struct iso_resource {
135 struct client_resource resource;
136 struct client *client;
137 /* Schedule work and access todo only with client->lock held. */
138 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100139 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
140 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100141 int generation;
142 u64 channels;
143 s32 bandwidth;
144 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);
Stefan Richter105e53f2011-05-01 20:50:31 +0200152 if (!queue_delayed_work(fw_workqueue, &r->work, delay))
Stefan Richter9fb551b2009-10-08 00:41:10 +0200153 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);
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100254 init_waitqueue_head(&client->tx_flush_wait);
Stefan Richterbf54e142010-07-16 22:25:51 +0200255 INIT_LIST_HEAD(&client->phy_receiver_link);
Stefan Richter93b37902011-07-09 16:43:22 +0200256 INIT_LIST_HEAD(&client->link);
Stefan Richterfb443032009-01-04 16:23:29 +0100257 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500258
259 file->private_data = client;
260
Stefan Richter3ac26b22010-04-10 16:38:05 +0100261 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500262}
263
264static void queue_event(struct client *client, struct event *event,
265 void *data0, size_t size0, void *data1, size_t size1)
266{
267 unsigned long flags;
268
269 event->v[0].data = data0;
270 event->v[0].size = size0;
271 event->v[1].data = data1;
272 event->v[1].size = size1;
273
274 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100275 if (client->in_shutdown)
276 kfree(event);
277 else
278 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500279 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400280
281 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500282}
283
Stefan Richter53dca512008-12-14 21:47:04 +0100284static int dequeue_event(struct client *client,
285 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500286{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500287 struct event *event;
288 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100289 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500290
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100291 ret = wait_event_interruptible(client->wait,
292 !list_empty(&client->event_list) ||
293 fw_device_is_shutdown(client->device));
294 if (ret < 0)
295 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500296
297 if (list_empty(&client->event_list) &&
298 fw_device_is_shutdown(client->device))
299 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500300
Stefan Richter3ba94982009-01-04 16:23:29 +0100301 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100302 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500303 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100304 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500305
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500306 total = 0;
307 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
308 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500309 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100310 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500311 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500312 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500313 total += size;
314 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100315 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500316
317 out:
318 kfree(event);
319
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100320 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500321}
322
Stefan Richter53dca512008-12-14 21:47:04 +0100323static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
324 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500325{
326 struct client *client = file->private_data;
327
328 return dequeue_event(client, buffer, count);
329}
330
Stefan Richter53dca512008-12-14 21:47:04 +0100331static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
332 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500333{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400334 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400335
Stefan Richter3ba94982009-01-04 16:23:29 +0100336 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500337
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400338 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500339 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100340 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400341 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500342 event->local_node_id = card->local_node->node_id;
Stefan Richter250b2b62010-06-21 23:24:35 +0200343 event->bm_node_id = card->bm_node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500344 event->irm_node_id = card->irm_node->node_id;
345 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400346
Stefan Richter3ba94982009-01-04 16:23:29 +0100347 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500348}
349
Stefan Richter53dca512008-12-14 21:47:04 +0100350static void for_each_client(struct fw_device *device,
351 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500352{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500353 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500354
Stefan Richterd67cfb92008-10-05 10:37:11 +0200355 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500356 list_for_each_entry(c, &device->client_list, link)
357 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200358 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500359}
360
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100361static int schedule_reallocations(int id, void *p, void *data)
362{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200363 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100364
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100365 return 0;
366}
367
Stefan Richter53dca512008-12-14 21:47:04 +0100368static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500369{
Stefan Richter97c18b72009-01-04 16:23:29 +0100370 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500371
Stefan Richter97c18b72009-01-04 16:23:29 +0100372 e = kzalloc(sizeof(*e), GFP_KERNEL);
373 if (e == NULL) {
Stefan Richterbf54e142010-07-16 22:25:51 +0200374 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500375 return;
376 }
377
Stefan Richter97c18b72009-01-04 16:23:29 +0100378 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500379
Stefan Richter97c18b72009-01-04 16:23:29 +0100380 queue_event(client, &e->event,
381 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100382
383 spin_lock_irq(&client->lock);
384 idr_for_each(&client->resource_idr, schedule_reallocations, client);
385 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500386}
387
388void fw_device_cdev_update(struct fw_device *device)
389{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500390 for_each_client(device, queue_bus_reset_event);
391}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500392
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500393static void wake_up_client(struct client *client)
394{
395 wake_up_interruptible(&client->wait);
396}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500397
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500398void fw_device_cdev_remove(struct fw_device *device)
399{
400 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500401}
402
Stefan Richter6e95dea2010-02-21 17:56:21 +0100403union ioctl_arg {
404 struct fw_cdev_get_info get_info;
405 struct fw_cdev_send_request send_request;
406 struct fw_cdev_allocate allocate;
407 struct fw_cdev_deallocate deallocate;
408 struct fw_cdev_send_response send_response;
409 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
410 struct fw_cdev_add_descriptor add_descriptor;
411 struct fw_cdev_remove_descriptor remove_descriptor;
412 struct fw_cdev_create_iso_context create_iso_context;
413 struct fw_cdev_queue_iso queue_iso;
414 struct fw_cdev_start_iso start_iso;
415 struct fw_cdev_stop_iso stop_iso;
416 struct fw_cdev_get_cycle_timer get_cycle_timer;
417 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
418 struct fw_cdev_send_stream_packet send_stream_packet;
419 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
Stefan Richter850bb6f2010-07-16 22:25:14 +0200420 struct fw_cdev_send_phy_packet send_phy_packet;
Stefan Richterbf54e142010-07-16 22:25:51 +0200421 struct fw_cdev_receive_phy_packets receive_phy_packets;
Stefan Richter872e3302010-07-29 18:19:22 +0200422 struct fw_cdev_set_iso_channels set_iso_channels;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100423};
424
425static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500426{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100427 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500428 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100429 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500430
Stefan Richter6e95dea2010-02-21 17:56:21 +0100431 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200432 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100433 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500434
Stefan Richterc9755e12008-03-24 20:54:28 +0100435 down_read(&fw_device_rwsem);
436
Stefan Richter6e95dea2010-02-21 17:56:21 +0100437 if (a->rom != 0) {
438 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100439 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500440
Stefan Richter6e95dea2010-02-21 17:56:21 +0100441 ret = copy_to_user(u64_to_uptr(a->rom),
442 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500443 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100444 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500445
Stefan Richterc9755e12008-03-24 20:54:28 +0100446 up_read(&fw_device_rwsem);
447
448 if (ret != 0)
449 return -EFAULT;
450
Stefan Richter93b37902011-07-09 16:43:22 +0200451 mutex_lock(&client->device->client_list_mutex);
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 Richter93b37902011-07-09 16:43:22 +0200456 ret = copy_to_user(u64_to_uptr(a->bus_reset),
457 &bus_reset, sizeof(bus_reset));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500458 }
Stefan Richter93b37902011-07-09 16:43:22 +0200459 if (ret == 0 && list_empty(&client->link))
460 list_add_tail(&client->link, &client->device->client_list);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500461
Stefan Richter93b37902011-07-09 16:43:22 +0200462 mutex_unlock(&client->device->client_list_mutex);
463
464 return ret ? -EFAULT : 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500465}
466
Stefan Richter53dca512008-12-14 21:47:04 +0100467static int add_client_resource(struct client *client,
468 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400469{
470 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100471 int ret;
472
473 retry:
474 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
475 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400476
477 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100478 if (client->in_shutdown)
479 ret = -ECANCELED;
480 else
481 ret = idr_get_new(&client->resource_idr, resource,
482 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100483 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100484 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200485 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100486 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400487 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100488
489 if (ret == -EAGAIN)
490 goto retry;
491
492 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400493}
494
Stefan Richter53dca512008-12-14 21:47:04 +0100495static int release_client_resource(struct client *client, u32 handle,
496 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200497 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400498{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200499 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400500
Stefan Richter3ba94982009-01-04 16:23:29 +0100501 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100502 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200503 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100504 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200505 resource = idr_find(&client->resource_idr, handle);
506 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100507 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100508 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400509
Stefan Richtere21fcf72009-10-08 00:41:38 +0200510 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400511 return -EINVAL;
512
Stefan Richtere21fcf72009-10-08 00:41:38 +0200513 if (return_resource)
514 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400515 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200516 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400517
Stefan Richterfb443032009-01-04 16:23:29 +0100518 client_put(client);
519
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400520 return 0;
521}
522
Stefan Richter53dca512008-12-14 21:47:04 +0100523static void release_transaction(struct client *client,
524 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400525{
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400526}
527
Stefan Richter53dca512008-12-14 21:47:04 +0100528static void complete_transaction(struct fw_card *card, int rcode,
529 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500530{
Stefan Richter97c18b72009-01-04 16:23:29 +0100531 struct outbound_transaction_event *e = data;
532 struct fw_cdev_event_response *rsp = &e->response;
533 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500534 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500535
Stefan Richter97c18b72009-01-04 16:23:29 +0100536 if (length < rsp->length)
537 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100539 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500540
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500541 spin_lock_irqsave(&client->lock, flags);
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100542 idr_remove(&client->resource_idr, e->r.resource.handle);
543 if (client->in_shutdown)
544 wake_up(&client->tx_flush_wait);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500545 spin_unlock_irqrestore(&client->lock, flags);
546
Stefan Richter97c18b72009-01-04 16:23:29 +0100547 rsp->type = FW_CDEV_EVENT_RESPONSE;
548 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700549
550 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100551 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700552 * data, and the read is short, preserve an extra copy of the data
553 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
554 * for short reads and some apps depended on it, this is both safe
555 * and prudent for compatibility.
556 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100557 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
558 queue_event(client, &e->event, rsp, sizeof(*rsp),
559 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700560 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100561 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700562 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100563
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100564 /* Drop the idr's reference */
565 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500566}
567
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100568static int init_request(struct client *client,
569 struct fw_cdev_send_request *request,
570 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500571{
Stefan Richter97c18b72009-01-04 16:23:29 +0100572 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100573 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500574
Stefan Richter18e9b102009-03-10 21:02:21 +0100575 if (request->tcode != TCODE_STREAM_DATA &&
576 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100577 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500578
Clemens Ladischa8e93f32010-07-07 14:37:30 +0200579 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
580 request->length < 4)
581 return -EINVAL;
582
Stefan Richter97c18b72009-01-04 16:23:29 +0100583 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
584 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500585 return -ENOMEM;
586
Stefan Richter97c18b72009-01-04 16:23:29 +0100587 e->client = client;
588 e->response.length = request->length;
589 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500590
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400591 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100592 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400593 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100594 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100595 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100596 }
597
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100598 e->r.resource.release = release_transaction;
599 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
600 if (ret < 0)
601 goto failed;
602
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100603 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100604 request->tcode, destination_id, request->generation,
605 speed, request->offset, e->response.data,
606 request->length, complete_transaction, e);
607 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100608
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100609 failed:
610 kfree(e);
611
612 return ret;
613}
614
Stefan Richter6e95dea2010-02-21 17:56:21 +0100615static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100616{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100617 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100618 case TCODE_WRITE_QUADLET_REQUEST:
619 case TCODE_WRITE_BLOCK_REQUEST:
620 case TCODE_READ_QUADLET_REQUEST:
621 case TCODE_READ_BLOCK_REQUEST:
622 case TCODE_LOCK_MASK_SWAP:
623 case TCODE_LOCK_COMPARE_SWAP:
624 case TCODE_LOCK_FETCH_ADD:
625 case TCODE_LOCK_LITTLE_ADD:
626 case TCODE_LOCK_BOUNDED_ADD:
627 case TCODE_LOCK_WRAP_ADD:
628 case TCODE_LOCK_VENDOR_DEPENDENT:
629 break;
630 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100631 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500632 }
633
Stefan Richter6e95dea2010-02-21 17:56:21 +0100634 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100635 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500636}
637
Stefan Richter281e2032010-01-24 16:45:03 +0100638static inline bool is_fcp_request(struct fw_request *request)
639{
640 return request == NULL;
641}
642
Stefan Richter53dca512008-12-14 21:47:04 +0100643static void release_request(struct client *client,
644 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400645{
Stefan Richter97c18b72009-01-04 16:23:29 +0100646 struct inbound_transaction_resource *r = container_of(resource,
647 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400648
Stefan Richter281e2032010-01-24 16:45:03 +0100649 if (is_fcp_request(r->request))
650 kfree(r->data);
651 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400652 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200653
654 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100655 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400656}
657
Stefan Richter97c18b72009-01-04 16:23:29 +0100658static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100659 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200660 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100661 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500662{
Stefan Richter97c18b72009-01-04 16:23:29 +0100663 struct address_handler_resource *handler = callback_data;
664 struct inbound_transaction_resource *r;
665 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200666 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100667 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100668 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500669
Stefan Richter0244f572010-06-20 22:52:27 +0200670 /* card may be different from handler->client->device->card */
671 fw_card_get(card);
672
Stefan Richter97c18b72009-01-04 16:23:29 +0100673 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400674 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200675 if (r == NULL || e == NULL) {
676 fw_notify("Out of memory when allocating event\n");
Jay Fenlason45ee3192008-12-21 16:47:17 +0100677 goto failed;
Stefan Richterbf54e142010-07-16 22:25:51 +0200678 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400679 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100680 r->request = request;
681 r->data = payload;
682 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500683
Stefan Richter281e2032010-01-24 16:45:03 +0100684 if (is_fcp_request(request)) {
685 /*
686 * FIXME: Let core-transaction.c manage a
687 * single reference-counted copy?
688 */
689 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
690 if (fcp_frame == NULL)
691 goto failed;
692
693 r->data = fcp_frame;
694 }
695
Stefan Richter97c18b72009-01-04 16:23:29 +0100696 r->resource.release = release_request;
697 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100698 if (ret < 0)
699 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500700
Stefan Richtere2055972010-06-20 22:53:55 +0200701 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
702 struct fw_cdev_event_request *req = &e->req.request;
703
704 if (tcode & 0x10)
705 tcode = TCODE_LOCK_REQUEST;
706
707 req->type = FW_CDEV_EVENT_REQUEST;
708 req->tcode = tcode;
709 req->offset = offset;
710 req->length = length;
711 req->handle = r->resource.handle;
712 req->closure = handler->closure;
713 event_size0 = sizeof(*req);
714 } else {
715 struct fw_cdev_event_request2 *req = &e->req.request2;
716
717 req->type = FW_CDEV_EVENT_REQUEST2;
718 req->tcode = tcode;
719 req->offset = offset;
720 req->source_node_id = source;
721 req->destination_node_id = destination;
722 req->card = card->index;
723 req->generation = generation;
724 req->length = length;
725 req->handle = r->resource.handle;
726 req->closure = handler->closure;
727 event_size0 = sizeof(*req);
728 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500729
Stefan Richter97c18b72009-01-04 16:23:29 +0100730 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200731 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100732 return;
733
734 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100735 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100736 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100737 kfree(fcp_frame);
738
739 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100740 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200741
742 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500743}
744
Stefan Richter53dca512008-12-14 21:47:04 +0100745static void release_address_handler(struct client *client,
746 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400747{
Stefan Richter97c18b72009-01-04 16:23:29 +0100748 struct address_handler_resource *r =
749 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400750
Stefan Richter97c18b72009-01-04 16:23:29 +0100751 fw_core_remove_address_handler(&r->handler);
752 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400753}
754
Stefan Richter6e95dea2010-02-21 17:56:21 +0100755static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500756{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100757 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100758 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500759 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100760 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500761
Stefan Richter97c18b72009-01-04 16:23:29 +0100762 r = kmalloc(sizeof(*r), GFP_KERNEL);
763 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500764 return -ENOMEM;
765
Stefan Richter6e95dea2010-02-21 17:56:21 +0100766 region.start = a->offset;
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200767 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
768 region.end = a->offset + a->length;
769 else
770 region.end = a->region_end;
771
Stefan Richter6e95dea2010-02-21 17:56:21 +0100772 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100773 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100774 r->handler.callback_data = r;
775 r->closure = a->closure;
776 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500777
Stefan Richter97c18b72009-01-04 16:23:29 +0100778 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100779 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100780 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100781 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500782 }
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200783 a->offset = r->handler.offset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784
Stefan Richter97c18b72009-01-04 16:23:29 +0100785 r->resource.release = release_address_handler;
786 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100787 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100788 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100789 return ret;
790 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100791 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500792
793 return 0;
794}
795
Stefan Richter6e95dea2010-02-21 17:56:21 +0100796static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400797{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100798 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100799 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400800}
801
Stefan Richter6e95dea2010-02-21 17:56:21 +0100802static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100804 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400805 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100806 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200807 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500808
Stefan Richter6e95dea2010-02-21 17:56:21 +0100809 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100810 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500811 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100812
Stefan Richter97c18b72009-01-04 16:23:29 +0100813 r = container_of(resource, struct inbound_transaction_resource,
814 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100815 if (is_fcp_request(r->request))
816 goto out;
817
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200818 if (a->length != fw_get_response_length(r->request)) {
819 ret = -EINVAL;
820 kfree(r->request);
821 goto out;
822 }
823 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100824 ret = -EFAULT;
825 kfree(r->request);
826 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200827 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400828 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200829 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200830 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500831 kfree(r);
832
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200833 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500834}
835
Stefan Richter6e95dea2010-02-21 17:56:21 +0100836static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500837{
Stefan Richter02d37be2010-07-08 16:09:06 +0200838 fw_schedule_bus_reset(client->device->card, true,
Stefan Richter6e95dea2010-02-21 17:56:21 +0100839 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Stefan Richter02d37be2010-07-08 16:09:06 +0200840 return 0;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500841}
842
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400843static void release_descriptor(struct client *client,
844 struct client_resource *resource)
845{
Stefan Richter97c18b72009-01-04 16:23:29 +0100846 struct descriptor_resource *r =
847 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400848
Stefan Richter97c18b72009-01-04 16:23:29 +0100849 fw_core_remove_descriptor(&r->descriptor);
850 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400851}
852
Stefan Richter6e95dea2010-02-21 17:56:21 +0100853static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200854{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100855 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100856 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100857 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200858
Stefan Richterde487da2009-03-10 21:00:23 +0100859 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200860 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100861 return -ENOSYS;
862
Stefan Richter6e95dea2010-02-21 17:56:21 +0100863 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200864 return -EINVAL;
865
Stefan Richter6e95dea2010-02-21 17:56:21 +0100866 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100867 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200868 return -ENOMEM;
869
Stefan Richter6e95dea2010-02-21 17:56:21 +0100870 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100871 ret = -EFAULT;
872 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200873 }
874
Stefan Richter6e95dea2010-02-21 17:56:21 +0100875 r->descriptor.length = a->length;
876 r->descriptor.immediate = a->immediate;
877 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100878 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200879
Stefan Richter97c18b72009-01-04 16:23:29 +0100880 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100881 if (ret < 0)
882 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200883
Stefan Richter97c18b72009-01-04 16:23:29 +0100884 r->resource.release = release_descriptor;
885 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100886 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100887 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100888 goto failed;
889 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100890 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200891
892 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100893 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100894 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100895
896 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200897}
898
Stefan Richter6e95dea2010-02-21 17:56:21 +0100899static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200900{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100901 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100902 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200903}
904
Stefan Richter53dca512008-12-14 21:47:04 +0100905static void iso_callback(struct fw_iso_context *context, u32 cycle,
906 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500907{
908 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100909 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500910
Stefan Richter56d04cb2010-06-08 00:20:10 +0200911 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200912 if (e == NULL) {
913 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500914 return;
Stefan Richterbf54e142010-07-16 22:25:51 +0200915 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100916 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
917 e->interrupt.closure = client->iso_closure;
918 e->interrupt.cycle = cycle;
919 e->interrupt.header_length = header_length;
920 memcpy(e->interrupt.header, header, header_length);
921 queue_event(client, &e->event, &e->interrupt,
922 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923}
924
Stefan Richter872e3302010-07-29 18:19:22 +0200925static void iso_mc_callback(struct fw_iso_context *context,
926 dma_addr_t completed, void *data)
927{
928 struct client *client = data;
929 struct iso_interrupt_mc_event *e;
930
931 e = kmalloc(sizeof(*e), GFP_ATOMIC);
932 if (e == NULL) {
933 fw_notify("Out of memory when allocating event\n");
934 return;
935 }
936 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
937 e->interrupt.closure = client->iso_closure;
938 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
939 completed);
940 queue_event(client, &e->event, &e->interrupt,
941 sizeof(e->interrupt), NULL, 0);
942}
943
Stefan Richter6e95dea2010-02-21 17:56:21 +0100944static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500945{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100946 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400947 struct fw_iso_context *context;
Stefan Richter872e3302010-07-29 18:19:22 +0200948 fw_iso_callback_t cb;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949
Stefan Richtereb5b35a2010-07-07 14:13:14 +0200950 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
Stefan Richter872e3302010-07-29 18:19:22 +0200951 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
952 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
953 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500954
Stefan Richter6e95dea2010-02-21 17:56:21 +0100955 switch (a->type) {
Stefan Richter872e3302010-07-29 18:19:22 +0200956 case FW_ISO_CONTEXT_TRANSMIT:
957 if (a->speed > SCODE_3200 || a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400958 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200959
960 cb = iso_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400961 break;
962
Stefan Richter872e3302010-07-29 18:19:22 +0200963 case FW_ISO_CONTEXT_RECEIVE:
964 if (a->header_size < 4 || (a->header_size & 3) ||
965 a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400966 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200967
968 cb = iso_callback;
969 break;
970
971 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
972 cb = (fw_iso_callback_t)iso_mc_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400973 break;
974
975 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500976 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400977 }
978
Stefan Richter6e95dea2010-02-21 17:56:21 +0100979 context = fw_iso_context_create(client->device->card, a->type,
Stefan Richter872e3302010-07-29 18:19:22 +0200980 a->channel, a->speed, a->header_size, cb, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400981 if (IS_ERR(context))
982 return PTR_ERR(context);
983
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200984 /* We only support one context at this time. */
985 spin_lock_irq(&client->lock);
986 if (client->iso_context != NULL) {
987 spin_unlock_irq(&client->lock);
988 fw_iso_context_destroy(context);
989 return -EBUSY;
990 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100991 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400992 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200993 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500994
Stefan Richter6e95dea2010-02-21 17:56:21 +0100995 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400996
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500997 return 0;
998}
999
Stefan Richter872e3302010-07-29 18:19:22 +02001000static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1001{
1002 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1003 struct fw_iso_context *ctx = client->iso_context;
1004
1005 if (ctx == NULL || a->handle != 0)
1006 return -EINVAL;
1007
1008 return fw_iso_context_set_channels(ctx, &a->channels);
1009}
1010
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001011/* Macros for decoding the iso packet control header. */
1012#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1013#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1014#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +02001015#define GET_TAG(v) (((v) >> 18) & 0x03)
1016#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001017#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1018
Stefan Richter6e95dea2010-02-21 17:56:21 +01001019static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001020{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001021 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001022 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001023 struct fw_iso_context *ctx = client->iso_context;
Stefan Richter872e3302010-07-29 18:19:22 +02001024 unsigned long payload, buffer_end, transmit_header_bytes = 0;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001025 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001026 int count;
1027 struct {
1028 struct fw_iso_packet packet;
1029 u8 header[256];
1030 } u;
1031
Stefan Richter6e95dea2010-02-21 17:56:21 +01001032 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001033 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001034
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001035 /*
1036 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001037 * the iso buffer, and the pointer points inside the buffer,
1038 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001039 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001040 * payload_length == 0 through. In other words, if no packets
1041 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +01001042 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001043 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001044 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001045 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001046 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001047 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001048 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001049 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001050 }
1051
Stefan Richter872e3302010-07-29 18:19:22 +02001052 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1053 return -EINVAL;
Al Viro1ccc9142007-10-14 19:34:40 +01001054
Stefan Richter872e3302010-07-29 18:19:22 +02001055 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Stefan Richter6e95dea2010-02-21 17:56:21 +01001056 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001057 return -EFAULT;
1058
Stefan Richter6e95dea2010-02-21 17:56:21 +01001059 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001060 count = 0;
1061 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001062 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001063 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001064 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1065 u.packet.interrupt = GET_INTERRUPT(control);
1066 u.packet.skip = GET_SKIP(control);
1067 u.packet.tag = GET_TAG(control);
1068 u.packet.sy = GET_SY(control);
1069 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001070
Stefan Richter872e3302010-07-29 18:19:22 +02001071 switch (ctx->type) {
1072 case FW_ISO_CONTEXT_TRANSMIT:
1073 if (u.packet.header_length & 3)
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +02001074 return -EINVAL;
Stefan Richterae2a97662010-07-29 09:31:56 +02001075 transmit_header_bytes = u.packet.header_length;
Stefan Richter872e3302010-07-29 18:19:22 +02001076 break;
1077
1078 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter69e61d02010-07-28 23:49:45 +02001079 if (u.packet.header_length == 0 ||
1080 u.packet.header_length % ctx->header_size != 0)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001081 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +02001082 break;
1083
1084 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1085 if (u.packet.payload_length == 0 ||
1086 u.packet.payload_length & 3)
1087 return -EINVAL;
1088 break;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001089 }
1090
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001091 next = (struct fw_cdev_iso_packet __user *)
Stefan Richterae2a97662010-07-29 09:31:56 +02001092 &p->header[transmit_header_bytes / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001093 if (next > end)
1094 return -EINVAL;
1095 if (__copy_from_user
Stefan Richterae2a97662010-07-29 09:31:56 +02001096 (u.packet.header, p->header, transmit_header_bytes))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001097 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001098 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001099 u.packet.header_length + u.packet.payload_length > 0)
1100 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001101 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001102 return -EINVAL;
1103
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001104 if (fw_iso_context_queue(ctx, &u.packet,
1105 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001106 break;
1107
1108 p = next;
1109 payload += u.packet.payload_length;
1110 count++;
1111 }
Clemens Ladisch13882a82011-05-02 09:33:56 +02001112 fw_iso_context_queue_flush(ctx);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001113
Stefan Richter6e95dea2010-02-21 17:56:21 +01001114 a->size -= uptr_to_u64(p) - a->packets;
1115 a->packets = uptr_to_u64(p);
1116 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001117
1118 return count;
1119}
1120
Stefan Richter6e95dea2010-02-21 17:56:21 +01001121static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001122{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001123 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001124
Stefan Richtereb5b35a2010-07-07 14:13:14 +02001125 BUILD_BUG_ON(
1126 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1127 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1128 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1129 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1130 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1131
Stefan Richter6e95dea2010-02-21 17:56:21 +01001132 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001133 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001134
Stefan Richter6e95dea2010-02-21 17:56:21 +01001135 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1136 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1137 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001138
Stefan Richter6e95dea2010-02-21 17:56:21 +01001139 return fw_iso_context_start(client->iso_context,
1140 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001141}
1142
Stefan Richter6e95dea2010-02-21 17:56:21 +01001143static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001144{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001145 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001146
Stefan Richter6e95dea2010-02-21 17:56:21 +01001147 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001148 return -EINVAL;
1149
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001150 return fw_iso_context_stop(client->iso_context);
1151}
1152
Stefan Richter6e95dea2010-02-21 17:56:21 +01001153static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001154{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001155 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001156 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001157 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001158 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001159 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001160
Stefan Richter4a9bde92010-02-20 22:24:43 +01001161 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001162
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001163 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001164
Stefan Richter6e95dea2010-02-21 17:56:21 +01001165 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001166 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1167 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1168 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1169 default:
1170 ret = -EINVAL;
1171 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001172
Stefan Richter4a9bde92010-02-20 22:24:43 +01001173 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001174
Stefan Richter6e95dea2010-02-21 17:56:21 +01001175 a->tv_sec = ts.tv_sec;
1176 a->tv_nsec = ts.tv_nsec;
1177 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001178
1179 return ret;
1180}
1181
Stefan Richter6e95dea2010-02-21 17:56:21 +01001182static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001183{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001184 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001185 struct fw_cdev_get_cycle_timer2 ct2;
1186
1187 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001188 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001189
Stefan Richter6e95dea2010-02-21 17:56:21 +01001190 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1191 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001192
Stefan Richtera64408b2007-09-29 10:41:58 +02001193 return 0;
1194}
1195
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001196static void iso_resource_work(struct work_struct *work)
1197{
1198 struct iso_resource_event *e;
1199 struct iso_resource *r =
1200 container_of(work, struct iso_resource, work.work);
1201 struct client *client = r->client;
1202 int generation, channel, bandwidth, todo;
1203 bool skip, free, success;
1204
1205 spin_lock_irq(&client->lock);
1206 generation = client->device->generation;
1207 todo = r->todo;
1208 /* Allow 1000ms grace period for other reallocations. */
1209 if (todo == ISO_RES_ALLOC &&
Clemens Ladische71084a2011-01-22 15:05:03 +01001210 time_before64(get_jiffies_64(),
1211 client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001212 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001213 skip = true;
1214 } else {
1215 /* We could be called twice within the same generation. */
1216 skip = todo == ISO_RES_REALLOC &&
1217 r->generation == generation;
1218 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001219 free = todo == ISO_RES_DEALLOC ||
1220 todo == ISO_RES_ALLOC_ONCE ||
1221 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001222 r->generation = generation;
1223 spin_unlock_irq(&client->lock);
1224
1225 if (skip)
1226 goto out;
1227
1228 bandwidth = r->bandwidth;
1229
1230 fw_iso_resource_manage(client->device->card, generation,
1231 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001232 todo == ISO_RES_ALLOC ||
1233 todo == ISO_RES_REALLOC ||
Stefan Richterf30e6d32011-04-22 15:13:54 +02001234 todo == ISO_RES_ALLOC_ONCE);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001235 /*
1236 * Is this generation outdated already? As long as this resource sticks
1237 * in the idr, it will be scheduled again for a newer generation or at
1238 * shutdown.
1239 */
1240 if (channel == -EAGAIN &&
1241 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1242 goto out;
1243
1244 success = channel >= 0 || bandwidth > 0;
1245
1246 spin_lock_irq(&client->lock);
1247 /*
1248 * Transit from allocation to reallocation, except if the client
1249 * requested deallocation in the meantime.
1250 */
1251 if (r->todo == ISO_RES_ALLOC)
1252 r->todo = ISO_RES_REALLOC;
1253 /*
1254 * Allocation or reallocation failure? Pull this resource out of the
1255 * idr and prepare for deletion, unless the client is shutting down.
1256 */
1257 if (r->todo == ISO_RES_REALLOC && !success &&
1258 !client->in_shutdown &&
1259 idr_find(&client->resource_idr, r->resource.handle)) {
1260 idr_remove(&client->resource_idr, r->resource.handle);
1261 client_put(client);
1262 free = true;
1263 }
1264 spin_unlock_irq(&client->lock);
1265
1266 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001267 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001268
1269 if (todo == ISO_RES_REALLOC && success)
1270 goto out;
1271
Stefan Richter1ec3c022009-01-04 16:23:29 +01001272 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001273 e = r->e_alloc;
1274 r->e_alloc = NULL;
1275 } else {
1276 e = r->e_dealloc;
1277 r->e_dealloc = NULL;
1278 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001279 e->iso_resource.handle = r->resource.handle;
1280 e->iso_resource.channel = channel;
1281 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001282
1283 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001284 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001285
1286 if (free) {
1287 cancel_delayed_work(&r->work);
1288 kfree(r->e_alloc);
1289 kfree(r->e_dealloc);
1290 kfree(r);
1291 }
1292 out:
1293 client_put(client);
1294}
1295
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001296static void release_iso_resource(struct client *client,
1297 struct client_resource *resource)
1298{
1299 struct iso_resource *r =
1300 container_of(resource, struct iso_resource, resource);
1301
1302 spin_lock_irq(&client->lock);
1303 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001304 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001305 spin_unlock_irq(&client->lock);
1306}
1307
Stefan Richter1ec3c022009-01-04 16:23:29 +01001308static int init_iso_resource(struct client *client,
1309 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001310{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001311 struct iso_resource_event *e1, *e2;
1312 struct iso_resource *r;
1313 int ret;
1314
1315 if ((request->channels == 0 && request->bandwidth == 0) ||
1316 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1317 request->bandwidth < 0)
1318 return -EINVAL;
1319
1320 r = kmalloc(sizeof(*r), GFP_KERNEL);
1321 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1322 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1323 if (r == NULL || e1 == NULL || e2 == NULL) {
1324 ret = -ENOMEM;
1325 goto fail;
1326 }
1327
1328 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1329 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001330 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001331 r->generation = -1;
1332 r->channels = request->channels;
1333 r->bandwidth = request->bandwidth;
1334 r->e_alloc = e1;
1335 r->e_dealloc = e2;
1336
Stefan Richtere21fcf72009-10-08 00:41:38 +02001337 e1->iso_resource.closure = request->closure;
1338 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1339 e2->iso_resource.closure = request->closure;
1340 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001341
Stefan Richter1ec3c022009-01-04 16:23:29 +01001342 if (todo == ISO_RES_ALLOC) {
1343 r->resource.release = release_iso_resource;
1344 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001345 if (ret < 0)
1346 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001347 } else {
1348 r->resource.release = NULL;
1349 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001350 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001351 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001352 request->handle = r->resource.handle;
1353
1354 return 0;
1355 fail:
1356 kfree(r);
1357 kfree(e1);
1358 kfree(e2);
1359
1360 return ret;
1361}
1362
Stefan Richter6e95dea2010-02-21 17:56:21 +01001363static int ioctl_allocate_iso_resource(struct client *client,
1364 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001365{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001366 return init_iso_resource(client,
1367 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001368}
1369
Stefan Richter6e95dea2010-02-21 17:56:21 +01001370static int ioctl_deallocate_iso_resource(struct client *client,
1371 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001372{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001373 return release_client_resource(client,
1374 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001375}
1376
Stefan Richter6e95dea2010-02-21 17:56:21 +01001377static int ioctl_allocate_iso_resource_once(struct client *client,
1378 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001379{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001380 return init_iso_resource(client,
1381 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001382}
1383
Stefan Richter6e95dea2010-02-21 17:56:21 +01001384static int ioctl_deallocate_iso_resource_once(struct client *client,
1385 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001386{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001387 return init_iso_resource(client,
1388 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001389}
1390
Stefan Richterc8a25902009-03-10 20:59:16 +01001391/*
1392 * Returns a speed code: Maximum speed to or from this device,
1393 * limited by the device's link speed, the local node's link speed,
1394 * and all PHY port speeds between the two links.
1395 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001396static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001397{
Stefan Richterc8a25902009-03-10 20:59:16 +01001398 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001399}
1400
Stefan Richter6e95dea2010-02-21 17:56:21 +01001401static int ioctl_send_broadcast_request(struct client *client,
1402 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001403{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001404 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001405
Stefan Richter6e95dea2010-02-21 17:56:21 +01001406 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001407 case TCODE_WRITE_QUADLET_REQUEST:
1408 case TCODE_WRITE_BLOCK_REQUEST:
1409 break;
1410 default:
1411 return -EINVAL;
1412 }
1413
Stefan Richter1566f3d2009-01-04 16:23:29 +01001414 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001415 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001416 return -EACCES;
1417
Stefan Richter6e95dea2010-02-21 17:56:21 +01001418 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001419}
1420
Stefan Richter6e95dea2010-02-21 17:56:21 +01001421static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001422{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001423 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b102009-03-10 21:02:21 +01001424 struct fw_cdev_send_request request;
1425 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001426
Stefan Richter6e95dea2010-02-21 17:56:21 +01001427 if (a->speed > client->device->card->link_speed ||
1428 a->length > 1024 << a->speed)
Stefan Richter18e9b102009-03-10 21:02:21 +01001429 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001430
Stefan Richter6e95dea2010-02-21 17:56:21 +01001431 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b102009-03-10 21:02:21 +01001432 return -EINVAL;
1433
Stefan Richter6e95dea2010-02-21 17:56:21 +01001434 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b102009-03-10 21:02:21 +01001435 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001436 request.length = a->length;
1437 request.closure = a->closure;
1438 request.data = a->data;
1439 request.generation = a->generation;
Stefan Richter18e9b102009-03-10 21:02:21 +01001440
Stefan Richter6e95dea2010-02-21 17:56:21 +01001441 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001442}
1443
Stefan Richter850bb6f2010-07-16 22:25:14 +02001444static void outbound_phy_packet_callback(struct fw_packet *packet,
1445 struct fw_card *card, int status)
1446{
1447 struct outbound_phy_packet_event *e =
1448 container_of(packet, struct outbound_phy_packet_event, p);
1449
1450 switch (status) {
1451 /* expected: */
1452 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1453 /* should never happen with PHY packets: */
1454 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1455 case ACK_BUSY_X:
1456 case ACK_BUSY_A:
1457 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1458 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1459 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1460 /* stale generation; cancelled; on certain controllers: no ack */
1461 default: e->phy_packet.rcode = status; break;
1462 }
Stefan Richtercc550212010-07-18 13:00:50 +02001463 e->phy_packet.data[0] = packet->timestamp;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001464
Stefan Richtercc550212010-07-18 13:00:50 +02001465 queue_event(e->client, &e->event, &e->phy_packet,
1466 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001467 client_put(e->client);
1468}
1469
1470static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1471{
1472 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1473 struct fw_card *card = client->device->card;
1474 struct outbound_phy_packet_event *e;
1475
1476 /* Access policy: Allow this ioctl only on local nodes' device files. */
1477 if (!client->device->is_local)
1478 return -ENOSYS;
1479
Stefan Richtercc550212010-07-18 13:00:50 +02001480 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001481 if (e == NULL)
1482 return -ENOMEM;
1483
1484 client_get(client);
1485 e->client = client;
1486 e->p.speed = SCODE_100;
1487 e->p.generation = a->generation;
Clemens Ladisch5b06db12010-11-30 08:24:47 +01001488 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1489 e->p.header[1] = a->data[0];
1490 e->p.header[2] = a->data[1];
1491 e->p.header_length = 12;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001492 e->p.callback = outbound_phy_packet_callback;
1493 e->phy_packet.closure = a->closure;
1494 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
Stefan Richtercc550212010-07-18 13:00:50 +02001495 if (is_ping_packet(a->data))
1496 e->phy_packet.length = 4;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001497
1498 card->driver->send_request(card, &e->p);
1499
1500 return 0;
1501}
1502
Stefan Richterbf54e142010-07-16 22:25:51 +02001503static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1504{
1505 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1506 struct fw_card *card = client->device->card;
1507
1508 /* Access policy: Allow this ioctl only on local nodes' device files. */
1509 if (!client->device->is_local)
1510 return -ENOSYS;
1511
1512 spin_lock_irq(&card->lock);
1513
1514 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1515 client->phy_receiver_closure = a->closure;
1516
1517 spin_unlock_irq(&card->lock);
1518
1519 return 0;
1520}
1521
1522void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1523{
1524 struct client *client;
1525 struct inbound_phy_packet_event *e;
1526 unsigned long flags;
1527
1528 spin_lock_irqsave(&card->lock, flags);
1529
1530 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1531 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1532 if (e == NULL) {
1533 fw_notify("Out of memory when allocating event\n");
1534 break;
1535 }
1536 e->phy_packet.closure = client->phy_receiver_closure;
1537 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1538 e->phy_packet.rcode = RCODE_COMPLETE;
1539 e->phy_packet.length = 8;
1540 e->phy_packet.data[0] = p->header[1];
1541 e->phy_packet.data[1] = p->header[2];
1542 queue_event(client, &e->event,
1543 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1544 }
1545
1546 spin_unlock_irqrestore(&card->lock, flags);
1547}
1548
Stefan Richter6e95dea2010-02-21 17:56:21 +01001549static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Stefan Richterb9dc61cf2010-07-16 22:24:29 +02001550 [0x00] = ioctl_get_info,
1551 [0x01] = ioctl_send_request,
1552 [0x02] = ioctl_allocate,
1553 [0x03] = ioctl_deallocate,
1554 [0x04] = ioctl_send_response,
1555 [0x05] = ioctl_initiate_bus_reset,
1556 [0x06] = ioctl_add_descriptor,
1557 [0x07] = ioctl_remove_descriptor,
1558 [0x08] = ioctl_create_iso_context,
1559 [0x09] = ioctl_queue_iso,
1560 [0x0a] = ioctl_start_iso,
1561 [0x0b] = ioctl_stop_iso,
1562 [0x0c] = ioctl_get_cycle_timer,
1563 [0x0d] = ioctl_allocate_iso_resource,
1564 [0x0e] = ioctl_deallocate_iso_resource,
1565 [0x0f] = ioctl_allocate_iso_resource_once,
1566 [0x10] = ioctl_deallocate_iso_resource_once,
1567 [0x11] = ioctl_get_speed,
1568 [0x12] = ioctl_send_broadcast_request,
1569 [0x13] = ioctl_send_stream_packet,
1570 [0x14] = ioctl_get_cycle_timer2,
Stefan Richter850bb6f2010-07-16 22:25:14 +02001571 [0x15] = ioctl_send_phy_packet,
Stefan Richterbf54e142010-07-16 22:25:51 +02001572 [0x16] = ioctl_receive_phy_packets,
Stefan Richter872e3302010-07-29 18:19:22 +02001573 [0x17] = ioctl_set_iso_channels,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001574};
1575
Stefan Richter53dca512008-12-14 21:47:04 +01001576static int dispatch_ioctl(struct client *client,
1577 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001578{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001579 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001580 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001581
Stefan Richter64582292010-02-21 17:56:42 +01001582 if (fw_device_is_shutdown(client->device))
1583 return -ENODEV;
1584
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001585 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001586 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1587 _IOC_SIZE(cmd) > sizeof(buffer))
Stefan Richterd873d792011-07-09 16:42:26 +02001588 return -ENOTTY;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001589
Stefan Richter9cac00b2010-04-07 08:30:50 +02001590 if (_IOC_DIR(cmd) == _IOC_READ)
1591 memset(&buffer, 0, _IOC_SIZE(cmd));
1592
1593 if (_IOC_DIR(cmd) & _IOC_WRITE)
1594 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001595 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001596
Stefan Richter6e95dea2010-02-21 17:56:21 +01001597 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001598 if (ret < 0)
1599 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001600
Stefan Richter9cac00b2010-04-07 08:30:50 +02001601 if (_IOC_DIR(cmd) & _IOC_READ)
1602 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001603 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001604
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001605 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001606}
1607
Stefan Richter53dca512008-12-14 21:47:04 +01001608static long fw_device_op_ioctl(struct file *file,
1609 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001610{
Stefan Richter64582292010-02-21 17:56:42 +01001611 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001612}
1613
1614#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001615static long fw_device_op_compat_ioctl(struct file *file,
1616 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001617{
Stefan Richter64582292010-02-21 17:56:42 +01001618 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001619}
1620#endif
1621
1622static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1623{
1624 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001625 enum dma_data_direction direction;
1626 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001627 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001628
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001629 if (fw_device_is_shutdown(client->device))
1630 return -ENODEV;
1631
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001632 /* FIXME: We could support multiple buffers, but we don't. */
1633 if (client->buffer.pages != NULL)
1634 return -EBUSY;
1635
1636 if (!(vma->vm_flags & VM_SHARED))
1637 return -EINVAL;
1638
1639 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001640 return -EINVAL;
1641
1642 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001643 size = vma->vm_end - vma->vm_start;
1644 page_count = size >> PAGE_SHIFT;
1645 if (size & ~PAGE_MASK)
1646 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001647
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001648 if (vma->vm_flags & VM_WRITE)
1649 direction = DMA_TO_DEVICE;
1650 else
1651 direction = DMA_FROM_DEVICE;
1652
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001653 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1654 page_count, direction);
1655 if (ret < 0)
1656 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001657
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001658 ret = fw_iso_buffer_map(&client->buffer, vma);
1659 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001660 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1661
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001662 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001663}
1664
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +01001665static int is_outbound_transaction_resource(int id, void *p, void *data)
1666{
1667 struct client_resource *resource = p;
1668
1669 return resource->release == release_transaction;
1670}
1671
1672static int has_outbound_transactions(struct client *client)
1673{
1674 int ret;
1675
1676 spin_lock_irq(&client->lock);
1677 ret = idr_for_each(&client->resource_idr,
1678 is_outbound_transaction_resource, NULL);
1679 spin_unlock_irq(&client->lock);
1680
1681 return ret;
1682}
1683
Jay Fenlason45ee3192008-12-21 16:47:17 +01001684static int shutdown_resource(int id, void *p, void *data)
1685{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001686 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001687 struct client *client = data;
1688
Stefan Richtere21fcf72009-10-08 00:41:38 +02001689 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001690 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001691
1692 return 0;
1693}
1694
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001695static int fw_device_op_release(struct inode *inode, struct file *file)
1696{
1697 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001698 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001699
Stefan Richterbf54e142010-07-16 22:25:51 +02001700 spin_lock_irq(&client->device->card->lock);
1701 list_del(&client->phy_receiver_link);
1702 spin_unlock_irq(&client->device->card->lock);
1703
Stefan Richter97811e32008-12-14 19:19:23 +01001704 mutex_lock(&client->device->client_list_mutex);
1705 list_del(&client->link);
1706 mutex_unlock(&client->device->client_list_mutex);
1707
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001708 if (client->iso_context)
1709 fw_iso_context_destroy(client->iso_context);
1710
Stefan Richter36a755c2009-01-05 20:28:10 +01001711 if (client->buffer.pages)
1712 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1713
Jay Fenlason45ee3192008-12-21 16:47:17 +01001714 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001715 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001716 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001717 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001718
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +01001719 wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1720
Jay Fenlason45ee3192008-12-21 16:47:17 +01001721 idr_for_each(&client->resource_idr, shutdown_resource, client);
1722 idr_remove_all(&client->resource_idr);
1723 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001724
Stefan Richtere21fcf72009-10-08 00:41:38 +02001725 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1726 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001727
Stefan Richterfb443032009-01-04 16:23:29 +01001728 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001729
1730 return 0;
1731}
1732
1733static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1734{
1735 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001736 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001737
1738 poll_wait(file, &client->wait, pt);
1739
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001740 if (fw_device_is_shutdown(client->device))
1741 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001742 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001743 mask |= POLLIN | POLLRDNORM;
1744
1745 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001746}
1747
Stefan Richter21ebcd12007-01-14 15:29:07 +01001748const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001749 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001750 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001751 .open = fw_device_op_open,
1752 .read = fw_device_op_read,
1753 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001754 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001755 .release = fw_device_op_release,
1756 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001757#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001758 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001759#endif
1760};