blob: ba23646bb108a6e55439a7c7e016094835ab1604 [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 */
Jay Fenlasoncf417e542008-10-03 11:19:09 -040038#include <linux/spinlock.h>
Stefan Richter281e2032010-01-24 16:45:03 +010039#include <linux/string.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010040#include <linux/time.h>
Stefan Richtere034d242009-06-06 18:36:24 +020041#include <linux/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010042#include <linux/vmalloc.h>
43#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010044#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010045
Stefan Richtera64408b2007-09-29 10:41:58 +020046#include <asm/system.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010047
Stefan Richter77c9a5d2009-06-05 16:26:18 +020048#include "core.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050049
Stefan Richter604f4512010-06-20 22:52:55 +020050/*
51 * ABI version history is documented in linux/firewire-cdev.h.
52 */
Stefan Richter8e2b2b42010-07-23 13:05:39 +020053#define FW_CDEV_KERNEL_VERSION 4
54#define FW_CDEV_VERSION_EVENT_REQUEST2 4
55#define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
Stefan Richter604f4512010-06-20 22:52:55 +020056
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050058 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050059 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010060
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050061 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010062 bool in_shutdown;
63 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050064 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050065 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040066 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050067
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050068 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040069 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050070 struct fw_iso_buffer buffer;
71 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050072
Stefan Richterbf54e142010-07-16 22:25:51 +020073 struct list_head phy_receiver_link;
74 u64 phy_receiver_closure;
75
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050076 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010077 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050078};
79
Stefan Richterfb443032009-01-04 16:23:29 +010080static inline void client_get(struct client *client)
81{
82 kref_get(&client->kref);
83}
84
85static void client_release(struct kref *kref)
86{
87 struct client *client = container_of(kref, struct client, kref);
88
89 fw_device_put(client->device);
90 kfree(client);
91}
92
93static void client_put(struct client *client)
94{
95 kref_put(&client->kref, client_release);
96}
97
Stefan Richter97c18b72009-01-04 16:23:29 +010098struct client_resource;
99typedef void (*client_resource_release_fn_t)(struct client *,
100 struct client_resource *);
101struct client_resource {
102 client_resource_release_fn_t release;
103 int handle;
104};
105
106struct address_handler_resource {
107 struct client_resource resource;
108 struct fw_address_handler handler;
109 __u64 closure;
110 struct client *client;
111};
112
113struct outbound_transaction_resource {
114 struct client_resource resource;
115 struct fw_transaction transaction;
116};
117
118struct inbound_transaction_resource {
119 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400120 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100121 struct fw_request *request;
122 void *data;
123 size_t length;
124};
125
126struct descriptor_resource {
127 struct client_resource resource;
128 struct fw_descriptor descriptor;
129 u32 data[0];
130};
131
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100132struct iso_resource {
133 struct client_resource resource;
134 struct client *client;
135 /* Schedule work and access todo only with client->lock held. */
136 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100137 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
138 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100139 int generation;
140 u64 channels;
141 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200142 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100143 struct iso_resource_event *e_alloc, *e_dealloc;
144};
145
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100146static void release_iso_resource(struct client *, struct client_resource *);
147
Stefan Richter9fb551b2009-10-08 00:41:10 +0200148static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
149{
150 client_get(r->client);
151 if (!schedule_delayed_work(&r->work, delay))
152 client_put(r->client);
153}
154
155static void schedule_if_iso_resource(struct client_resource *resource)
156{
157 if (resource->release == release_iso_resource)
158 schedule_iso_resource(container_of(resource,
159 struct iso_resource, resource), 0);
160}
161
Stefan Richter97c18b72009-01-04 16:23:29 +0100162/*
163 * dequeue_event() just kfree()'s the event, so the event has to be
164 * the first field in a struct XYZ_event.
165 */
166struct event {
167 struct { void *data; size_t size; } v[2];
168 struct list_head link;
169};
170
171struct bus_reset_event {
172 struct event event;
173 struct fw_cdev_event_bus_reset reset;
174};
175
176struct outbound_transaction_event {
177 struct event event;
178 struct client *client;
179 struct outbound_transaction_resource r;
180 struct fw_cdev_event_response response;
181};
182
183struct inbound_transaction_event {
184 struct event event;
Stefan Richtere2055972010-06-20 22:53:55 +0200185 union {
186 struct fw_cdev_event_request request;
187 struct fw_cdev_event_request2 request2;
188 } req;
Stefan Richter97c18b72009-01-04 16:23:29 +0100189};
190
191struct iso_interrupt_event {
192 struct event event;
193 struct fw_cdev_event_iso_interrupt interrupt;
194};
195
Stefan Richter872e3302010-07-29 18:19:22 +0200196struct iso_interrupt_mc_event {
197 struct event event;
198 struct fw_cdev_event_iso_interrupt_mc interrupt;
199};
200
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100201struct iso_resource_event {
202 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200203 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100204};
205
Stefan Richter850bb6f2010-07-16 22:25:14 +0200206struct outbound_phy_packet_event {
207 struct event event;
208 struct client *client;
209 struct fw_packet p;
210 struct fw_cdev_event_phy_packet phy_packet;
211};
212
Stefan Richterbf54e142010-07-16 22:25:51 +0200213struct inbound_phy_packet_event {
214 struct event event;
215 struct fw_cdev_event_phy_packet phy_packet;
216};
217
Stefan Richter53dca512008-12-14 21:47:04 +0100218static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500219{
220 return (void __user *)(unsigned long)value;
221}
222
Stefan Richter53dca512008-12-14 21:47:04 +0100223static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500224{
225 return (__u64)(unsigned long)ptr;
226}
227
228static int fw_device_op_open(struct inode *inode, struct file *file)
229{
230 struct fw_device *device;
231 struct client *client;
232
Stefan Richter96b19062008-02-02 15:01:09 +0100233 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500234 if (device == NULL)
235 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500236
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400237 if (fw_device_is_shutdown(device)) {
238 fw_device_put(device);
239 return -ENODEV;
240 }
241
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400242 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100243 if (client == NULL) {
244 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500245 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100246 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500247
Stefan Richter96b19062008-02-02 15:01:09 +0100248 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500249 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100250 idr_init(&client->resource_idr);
251 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500252 init_waitqueue_head(&client->wait);
Stefan Richterbf54e142010-07-16 22:25:51 +0200253 INIT_LIST_HEAD(&client->phy_receiver_link);
Stefan Richterfb443032009-01-04 16:23:29 +0100254 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500255
256 file->private_data = client;
257
Stefan Richterd67cfb92008-10-05 10:37:11 +0200258 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500259 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200260 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500261
Stefan Richter3ac26b22010-04-10 16:38:05 +0100262 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500263}
264
265static void queue_event(struct client *client, struct event *event,
266 void *data0, size_t size0, void *data1, size_t size1)
267{
268 unsigned long flags;
269
270 event->v[0].data = data0;
271 event->v[0].size = size0;
272 event->v[1].data = data1;
273 event->v[1].size = size1;
274
275 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100276 if (client->in_shutdown)
277 kfree(event);
278 else
279 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500280 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400281
282 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500283}
284
Stefan Richter53dca512008-12-14 21:47:04 +0100285static int dequeue_event(struct client *client,
286 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500287{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500288 struct event *event;
289 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100290 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500291
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100292 ret = wait_event_interruptible(client->wait,
293 !list_empty(&client->event_list) ||
294 fw_device_is_shutdown(client->device));
295 if (ret < 0)
296 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500297
298 if (list_empty(&client->event_list) &&
299 fw_device_is_shutdown(client->device))
300 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500301
Stefan Richter3ba94982009-01-04 16:23:29 +0100302 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100303 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500304 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100305 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500306
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500307 total = 0;
308 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
309 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500310 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100311 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500312 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500313 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500314 total += size;
315 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100316 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500317
318 out:
319 kfree(event);
320
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100321 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500322}
323
Stefan Richter53dca512008-12-14 21:47:04 +0100324static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
325 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500326{
327 struct client *client = file->private_data;
328
329 return dequeue_event(client, buffer, count);
330}
331
Stefan Richter53dca512008-12-14 21:47:04 +0100332static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
333 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500334{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400335 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400336
Stefan Richter3ba94982009-01-04 16:23:29 +0100337 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500338
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400339 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500340 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100341 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400342 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500343 event->local_node_id = card->local_node->node_id;
Stefan Richter250b2b62010-06-21 23:24:35 +0200344 event->bm_node_id = card->bm_node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500345 event->irm_node_id = card->irm_node->node_id;
346 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400347
Stefan Richter3ba94982009-01-04 16:23:29 +0100348 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500349}
350
Stefan Richter53dca512008-12-14 21:47:04 +0100351static void for_each_client(struct fw_device *device,
352 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500353{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500354 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500355
Stefan Richterd67cfb92008-10-05 10:37:11 +0200356 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500357 list_for_each_entry(c, &device->client_list, link)
358 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200359 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500360}
361
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100362static int schedule_reallocations(int id, void *p, void *data)
363{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200364 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100365
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100366 return 0;
367}
368
Stefan Richter53dca512008-12-14 21:47:04 +0100369static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500370{
Stefan Richter97c18b72009-01-04 16:23:29 +0100371 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500372
Stefan Richter97c18b72009-01-04 16:23:29 +0100373 e = kzalloc(sizeof(*e), GFP_KERNEL);
374 if (e == NULL) {
Stefan Richterbf54e142010-07-16 22:25:51 +0200375 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500376 return;
377 }
378
Stefan Richter97c18b72009-01-04 16:23:29 +0100379 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500380
Stefan Richter97c18b72009-01-04 16:23:29 +0100381 queue_event(client, &e->event,
382 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100383
384 spin_lock_irq(&client->lock);
385 idr_for_each(&client->resource_idr, schedule_reallocations, client);
386 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500387}
388
389void fw_device_cdev_update(struct fw_device *device)
390{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500391 for_each_client(device, queue_bus_reset_event);
392}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500393
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500394static void wake_up_client(struct client *client)
395{
396 wake_up_interruptible(&client->wait);
397}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500398
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500399void fw_device_cdev_remove(struct fw_device *device)
400{
401 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500402}
403
Stefan Richter6e95dea2010-02-21 17:56:21 +0100404union ioctl_arg {
405 struct fw_cdev_get_info get_info;
406 struct fw_cdev_send_request send_request;
407 struct fw_cdev_allocate allocate;
408 struct fw_cdev_deallocate deallocate;
409 struct fw_cdev_send_response send_response;
410 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
411 struct fw_cdev_add_descriptor add_descriptor;
412 struct fw_cdev_remove_descriptor remove_descriptor;
413 struct fw_cdev_create_iso_context create_iso_context;
414 struct fw_cdev_queue_iso queue_iso;
415 struct fw_cdev_start_iso start_iso;
416 struct fw_cdev_stop_iso stop_iso;
417 struct fw_cdev_get_cycle_timer get_cycle_timer;
418 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
419 struct fw_cdev_send_stream_packet send_stream_packet;
420 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
Stefan Richter850bb6f2010-07-16 22:25:14 +0200421 struct fw_cdev_send_phy_packet send_phy_packet;
Stefan Richterbf54e142010-07-16 22:25:51 +0200422 struct fw_cdev_receive_phy_packets receive_phy_packets;
Stefan Richter872e3302010-07-29 18:19:22 +0200423 struct fw_cdev_set_iso_channels set_iso_channels;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100424};
425
426static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500427{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100428 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500429 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100430 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500431
Stefan Richter6e95dea2010-02-21 17:56:21 +0100432 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200433 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100434 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500435
Stefan Richterc9755e12008-03-24 20:54:28 +0100436 down_read(&fw_device_rwsem);
437
Stefan Richter6e95dea2010-02-21 17:56:21 +0100438 if (a->rom != 0) {
439 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100440 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500441
Stefan Richter6e95dea2010-02-21 17:56:21 +0100442 ret = copy_to_user(u64_to_uptr(a->rom),
443 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500444 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100445 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500446
Stefan Richterc9755e12008-03-24 20:54:28 +0100447 up_read(&fw_device_rwsem);
448
449 if (ret != 0)
450 return -EFAULT;
451
Stefan Richter6e95dea2010-02-21 17:56:21 +0100452 client->bus_reset_closure = a->bus_reset_closure;
453 if (a->bus_reset != 0) {
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400454 fill_bus_reset_event(&bus_reset, client);
Stefan Richter6e95dea2010-02-21 17:56:21 +0100455 if (copy_to_user(u64_to_uptr(a->bus_reset),
456 &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500457 return -EFAULT;
458 }
459
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500460 return 0;
461}
462
Stefan Richter53dca512008-12-14 21:47:04 +0100463static int add_client_resource(struct client *client,
464 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400465{
466 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100467 int ret;
468
469 retry:
470 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
471 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400472
473 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100474 if (client->in_shutdown)
475 ret = -ECANCELED;
476 else
477 ret = idr_get_new(&client->resource_idr, resource,
478 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100479 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100480 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200481 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100482 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400483 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100484
485 if (ret == -EAGAIN)
486 goto retry;
487
488 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400489}
490
Stefan Richter53dca512008-12-14 21:47:04 +0100491static int release_client_resource(struct client *client, u32 handle,
492 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200493 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400494{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200495 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400496
Stefan Richter3ba94982009-01-04 16:23:29 +0100497 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100498 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200499 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100500 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200501 resource = idr_find(&client->resource_idr, handle);
502 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100503 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100504 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400505
Stefan Richtere21fcf72009-10-08 00:41:38 +0200506 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400507 return -EINVAL;
508
Stefan Richtere21fcf72009-10-08 00:41:38 +0200509 if (return_resource)
510 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400511 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200512 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400513
Stefan Richterfb443032009-01-04 16:23:29 +0100514 client_put(client);
515
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400516 return 0;
517}
518
Stefan Richter53dca512008-12-14 21:47:04 +0100519static void release_transaction(struct client *client,
520 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400521{
Stefan Richter97c18b72009-01-04 16:23:29 +0100522 struct outbound_transaction_resource *r = container_of(resource,
523 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400524
Stefan Richter97c18b72009-01-04 16:23:29 +0100525 fw_cancel_transaction(client->device->card, &r->transaction);
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);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100542 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100543 * 1. If called while in shutdown, the idr tree must be left untouched.
544 * The idr handle will be removed and the client reference will be
545 * dropped later.
546 * 2. If the call chain was release_client_resource ->
547 * release_transaction -> complete_transaction (instead of a normal
548 * conclusion of the transaction), i.e. if this resource was already
549 * unregistered from the idr, the client reference will be dropped
550 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100551 */
Stefan Richterfb443032009-01-04 16:23:29 +0100552 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100553 idr_find(&client->resource_idr, e->r.resource.handle)) {
554 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100555 /* Drop the idr's reference */
556 client_put(client);
557 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500558 spin_unlock_irqrestore(&client->lock, flags);
559
Stefan Richter97c18b72009-01-04 16:23:29 +0100560 rsp->type = FW_CDEV_EVENT_RESPONSE;
561 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700562
563 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100564 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700565 * data, and the read is short, preserve an extra copy of the data
566 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
567 * for short reads and some apps depended on it, this is both safe
568 * and prudent for compatibility.
569 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100570 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
571 queue_event(client, &e->event, rsp, sizeof(*rsp),
572 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700573 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100574 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700575 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100576
577 /* Drop the transaction callback's reference */
578 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500579}
580
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100581static int init_request(struct client *client,
582 struct fw_cdev_send_request *request,
583 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500584{
Stefan Richter97c18b72009-01-04 16:23:29 +0100585 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100586 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500587
Stefan Richter18e9b10f2009-03-10 21:02:21 +0100588 if (request->tcode != TCODE_STREAM_DATA &&
589 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100590 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500591
Clemens Ladischa8e93f32010-07-07 14:37:30 +0200592 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
593 request->length < 4)
594 return -EINVAL;
595
Stefan Richter97c18b72009-01-04 16:23:29 +0100596 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
597 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500598 return -ENOMEM;
599
Stefan Richter97c18b72009-01-04 16:23:29 +0100600 e->client = client;
601 e->response.length = request->length;
602 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500603
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400604 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100605 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400606 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100607 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100608 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100609 }
610
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100611 e->r.resource.release = release_transaction;
612 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
613 if (ret < 0)
614 goto failed;
615
616 /* Get a reference for the transaction callback */
617 client_get(client);
618
619 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100620 request->tcode, destination_id, request->generation,
621 speed, request->offset, e->response.data,
622 request->length, complete_transaction, e);
623 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100624
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100625 failed:
626 kfree(e);
627
628 return ret;
629}
630
Stefan Richter6e95dea2010-02-21 17:56:21 +0100631static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100632{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100633 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100634 case TCODE_WRITE_QUADLET_REQUEST:
635 case TCODE_WRITE_BLOCK_REQUEST:
636 case TCODE_READ_QUADLET_REQUEST:
637 case TCODE_READ_BLOCK_REQUEST:
638 case TCODE_LOCK_MASK_SWAP:
639 case TCODE_LOCK_COMPARE_SWAP:
640 case TCODE_LOCK_FETCH_ADD:
641 case TCODE_LOCK_LITTLE_ADD:
642 case TCODE_LOCK_BOUNDED_ADD:
643 case TCODE_LOCK_WRAP_ADD:
644 case TCODE_LOCK_VENDOR_DEPENDENT:
645 break;
646 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100647 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500648 }
649
Stefan Richter6e95dea2010-02-21 17:56:21 +0100650 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100651 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500652}
653
Stefan Richter281e2032010-01-24 16:45:03 +0100654static inline bool is_fcp_request(struct fw_request *request)
655{
656 return request == NULL;
657}
658
Stefan Richter53dca512008-12-14 21:47:04 +0100659static void release_request(struct client *client,
660 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400661{
Stefan Richter97c18b72009-01-04 16:23:29 +0100662 struct inbound_transaction_resource *r = container_of(resource,
663 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400664
Stefan Richter281e2032010-01-24 16:45:03 +0100665 if (is_fcp_request(r->request))
666 kfree(r->data);
667 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400668 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200669
670 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100671 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400672}
673
Stefan Richter97c18b72009-01-04 16:23:29 +0100674static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100675 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200676 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100677 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500678{
Stefan Richter97c18b72009-01-04 16:23:29 +0100679 struct address_handler_resource *handler = callback_data;
680 struct inbound_transaction_resource *r;
681 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200682 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100683 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100684 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500685
Stefan Richter0244f572010-06-20 22:52:27 +0200686 /* card may be different from handler->client->device->card */
687 fw_card_get(card);
688
Stefan Richter97c18b72009-01-04 16:23:29 +0100689 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400690 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200691 if (r == NULL || e == NULL) {
692 fw_notify("Out of memory when allocating event\n");
Jay Fenlason45ee3192008-12-21 16:47:17 +0100693 goto failed;
Stefan Richterbf54e142010-07-16 22:25:51 +0200694 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400695 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100696 r->request = request;
697 r->data = payload;
698 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500699
Stefan Richter281e2032010-01-24 16:45:03 +0100700 if (is_fcp_request(request)) {
701 /*
702 * FIXME: Let core-transaction.c manage a
703 * single reference-counted copy?
704 */
705 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
706 if (fcp_frame == NULL)
707 goto failed;
708
709 r->data = fcp_frame;
710 }
711
Stefan Richter97c18b72009-01-04 16:23:29 +0100712 r->resource.release = release_request;
713 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100714 if (ret < 0)
715 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500716
Stefan Richtere2055972010-06-20 22:53:55 +0200717 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
718 struct fw_cdev_event_request *req = &e->req.request;
719
720 if (tcode & 0x10)
721 tcode = TCODE_LOCK_REQUEST;
722
723 req->type = FW_CDEV_EVENT_REQUEST;
724 req->tcode = tcode;
725 req->offset = offset;
726 req->length = length;
727 req->handle = r->resource.handle;
728 req->closure = handler->closure;
729 event_size0 = sizeof(*req);
730 } else {
731 struct fw_cdev_event_request2 *req = &e->req.request2;
732
733 req->type = FW_CDEV_EVENT_REQUEST2;
734 req->tcode = tcode;
735 req->offset = offset;
736 req->source_node_id = source;
737 req->destination_node_id = destination;
738 req->card = card->index;
739 req->generation = generation;
740 req->length = length;
741 req->handle = r->resource.handle;
742 req->closure = handler->closure;
743 event_size0 = sizeof(*req);
744 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500745
Stefan Richter97c18b72009-01-04 16:23:29 +0100746 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200747 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100748 return;
749
750 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100751 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100752 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100753 kfree(fcp_frame);
754
755 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100756 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200757
758 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500759}
760
Stefan Richter53dca512008-12-14 21:47:04 +0100761static void release_address_handler(struct client *client,
762 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400763{
Stefan Richter97c18b72009-01-04 16:23:29 +0100764 struct address_handler_resource *r =
765 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400766
Stefan Richter97c18b72009-01-04 16:23:29 +0100767 fw_core_remove_address_handler(&r->handler);
768 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400769}
770
Stefan Richter6e95dea2010-02-21 17:56:21 +0100771static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100773 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100774 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500775 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100776 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500777
Stefan Richter97c18b72009-01-04 16:23:29 +0100778 r = kmalloc(sizeof(*r), GFP_KERNEL);
779 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500780 return -ENOMEM;
781
Stefan Richter6e95dea2010-02-21 17:56:21 +0100782 region.start = a->offset;
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200783 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
784 region.end = a->offset + a->length;
785 else
786 region.end = a->region_end;
787
Stefan Richter6e95dea2010-02-21 17:56:21 +0100788 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100789 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100790 r->handler.callback_data = r;
791 r->closure = a->closure;
792 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500793
Stefan Richter97c18b72009-01-04 16:23:29 +0100794 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100795 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100796 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100797 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798 }
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200799 a->offset = r->handler.offset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500800
Stefan Richter97c18b72009-01-04 16:23:29 +0100801 r->resource.release = release_address_handler;
802 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100803 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100804 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100805 return ret;
806 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100807 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500808
809 return 0;
810}
811
Stefan Richter6e95dea2010-02-21 17:56:21 +0100812static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400813{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100814 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100815 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400816}
817
Stefan Richter6e95dea2010-02-21 17:56:21 +0100818static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100820 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400821 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100822 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200823 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500824
Stefan Richter6e95dea2010-02-21 17:56:21 +0100825 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100826 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500827 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100828
Stefan Richter97c18b72009-01-04 16:23:29 +0100829 r = container_of(resource, struct inbound_transaction_resource,
830 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100831 if (is_fcp_request(r->request))
832 goto out;
833
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200834 if (a->length != fw_get_response_length(r->request)) {
835 ret = -EINVAL;
836 kfree(r->request);
837 goto out;
838 }
839 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100840 ret = -EFAULT;
841 kfree(r->request);
842 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200843 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400844 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200845 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200846 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500847 kfree(r);
848
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200849 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500850}
851
Stefan Richter6e95dea2010-02-21 17:56:21 +0100852static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500853{
Stefan Richter02d37be2010-07-08 16:09:06 +0200854 fw_schedule_bus_reset(client->device->card, true,
Stefan Richter6e95dea2010-02-21 17:56:21 +0100855 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Stefan Richter02d37be2010-07-08 16:09:06 +0200856 return 0;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500857}
858
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400859static void release_descriptor(struct client *client,
860 struct client_resource *resource)
861{
Stefan Richter97c18b72009-01-04 16:23:29 +0100862 struct descriptor_resource *r =
863 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400864
Stefan Richter97c18b72009-01-04 16:23:29 +0100865 fw_core_remove_descriptor(&r->descriptor);
866 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400867}
868
Stefan Richter6e95dea2010-02-21 17:56:21 +0100869static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200870{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100871 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100872 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100873 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200874
Stefan Richterde487da2009-03-10 21:00:23 +0100875 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200876 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100877 return -ENOSYS;
878
Stefan Richter6e95dea2010-02-21 17:56:21 +0100879 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200880 return -EINVAL;
881
Stefan Richter6e95dea2010-02-21 17:56:21 +0100882 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100883 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200884 return -ENOMEM;
885
Stefan Richter6e95dea2010-02-21 17:56:21 +0100886 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100887 ret = -EFAULT;
888 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200889 }
890
Stefan Richter6e95dea2010-02-21 17:56:21 +0100891 r->descriptor.length = a->length;
892 r->descriptor.immediate = a->immediate;
893 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100894 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200895
Stefan Richter97c18b72009-01-04 16:23:29 +0100896 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100897 if (ret < 0)
898 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200899
Stefan Richter97c18b72009-01-04 16:23:29 +0100900 r->resource.release = release_descriptor;
901 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100902 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100903 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100904 goto failed;
905 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100906 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200907
908 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100909 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100910 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100911
912 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200913}
914
Stefan Richter6e95dea2010-02-21 17:56:21 +0100915static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200916{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100917 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100918 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200919}
920
Stefan Richter53dca512008-12-14 21:47:04 +0100921static void iso_callback(struct fw_iso_context *context, u32 cycle,
922 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923{
924 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100925 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500926
Stefan Richter56d04cb12010-06-08 00:20:10 +0200927 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200928 if (e == NULL) {
929 fw_notify("Out of memory when allocating event\n");
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500930 return;
Stefan Richterbf54e142010-07-16 22:25:51 +0200931 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100932 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
933 e->interrupt.closure = client->iso_closure;
934 e->interrupt.cycle = cycle;
935 e->interrupt.header_length = header_length;
936 memcpy(e->interrupt.header, header, header_length);
937 queue_event(client, &e->event, &e->interrupt,
938 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500939}
940
Stefan Richter872e3302010-07-29 18:19:22 +0200941static void iso_mc_callback(struct fw_iso_context *context,
942 dma_addr_t completed, void *data)
943{
944 struct client *client = data;
945 struct iso_interrupt_mc_event *e;
946
947 e = kmalloc(sizeof(*e), GFP_ATOMIC);
948 if (e == NULL) {
949 fw_notify("Out of memory when allocating event\n");
950 return;
951 }
952 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
953 e->interrupt.closure = client->iso_closure;
954 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
955 completed);
956 queue_event(client, &e->event, &e->interrupt,
957 sizeof(e->interrupt), NULL, 0);
958}
959
Stefan Richter6e95dea2010-02-21 17:56:21 +0100960static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500961{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100962 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400963 struct fw_iso_context *context;
Stefan Richter872e3302010-07-29 18:19:22 +0200964 fw_iso_callback_t cb;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500965
Stefan Richtereb5b35a2010-07-07 14:13:14 +0200966 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
Stefan Richter872e3302010-07-29 18:19:22 +0200967 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
968 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
969 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500970
Stefan Richter6e95dea2010-02-21 17:56:21 +0100971 switch (a->type) {
Stefan Richter872e3302010-07-29 18:19:22 +0200972 case FW_ISO_CONTEXT_TRANSMIT:
973 if (a->speed > SCODE_3200 || a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400974 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200975
976 cb = iso_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400977 break;
978
Stefan Richter872e3302010-07-29 18:19:22 +0200979 case FW_ISO_CONTEXT_RECEIVE:
980 if (a->header_size < 4 || (a->header_size & 3) ||
981 a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400982 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200983
984 cb = iso_callback;
985 break;
986
987 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
988 cb = (fw_iso_callback_t)iso_mc_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400989 break;
990
991 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500992 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400993 }
994
Stefan Richter6e95dea2010-02-21 17:56:21 +0100995 context = fw_iso_context_create(client->device->card, a->type,
Stefan Richter872e3302010-07-29 18:19:22 +0200996 a->channel, a->speed, a->header_size, cb, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400997 if (IS_ERR(context))
998 return PTR_ERR(context);
999
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001000 /* We only support one context at this time. */
1001 spin_lock_irq(&client->lock);
1002 if (client->iso_context != NULL) {
1003 spin_unlock_irq(&client->lock);
1004 fw_iso_context_destroy(context);
1005 return -EBUSY;
1006 }
Stefan Richter6e95dea2010-02-21 17:56:21 +01001007 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -04001008 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001009 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001010
Stefan Richter6e95dea2010-02-21 17:56:21 +01001011 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001012
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001013 return 0;
1014}
1015
Stefan Richter872e3302010-07-29 18:19:22 +02001016static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1017{
1018 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1019 struct fw_iso_context *ctx = client->iso_context;
1020
1021 if (ctx == NULL || a->handle != 0)
1022 return -EINVAL;
1023
1024 return fw_iso_context_set_channels(ctx, &a->channels);
1025}
1026
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001027/* Macros for decoding the iso packet control header. */
1028#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1029#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1030#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +02001031#define GET_TAG(v) (((v) >> 18) & 0x03)
1032#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001033#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1034
Stefan Richter6e95dea2010-02-21 17:56:21 +01001035static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001036{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001037 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001038 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001039 struct fw_iso_context *ctx = client->iso_context;
Stefan Richter872e3302010-07-29 18:19:22 +02001040 unsigned long payload, buffer_end, transmit_header_bytes = 0;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001041 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001042 int count;
1043 struct {
1044 struct fw_iso_packet packet;
1045 u8 header[256];
1046 } u;
1047
Stefan Richter6e95dea2010-02-21 17:56:21 +01001048 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001049 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001050
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001051 /*
1052 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001053 * the iso buffer, and the pointer points inside the buffer,
1054 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001055 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001056 * payload_length == 0 through. In other words, if no packets
1057 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +01001058 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001059 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001060 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001061 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001062 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001063 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001064 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001065 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001066 }
1067
Stefan Richter872e3302010-07-29 18:19:22 +02001068 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1069 return -EINVAL;
Al Viro1ccc9142007-10-14 19:34:40 +01001070
Stefan Richter872e3302010-07-29 18:19:22 +02001071 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Stefan Richter6e95dea2010-02-21 17:56:21 +01001072 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001073 return -EFAULT;
1074
Stefan Richter6e95dea2010-02-21 17:56:21 +01001075 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001076 count = 0;
1077 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001078 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001079 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001080 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1081 u.packet.interrupt = GET_INTERRUPT(control);
1082 u.packet.skip = GET_SKIP(control);
1083 u.packet.tag = GET_TAG(control);
1084 u.packet.sy = GET_SY(control);
1085 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001086
Stefan Richter872e3302010-07-29 18:19:22 +02001087 switch (ctx->type) {
1088 case FW_ISO_CONTEXT_TRANSMIT:
1089 if (u.packet.header_length & 3)
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +02001090 return -EINVAL;
Stefan Richterae2a97662010-07-29 09:31:56 +02001091 transmit_header_bytes = u.packet.header_length;
Stefan Richter872e3302010-07-29 18:19:22 +02001092 break;
1093
1094 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter69e61d02010-07-28 23:49:45 +02001095 if (u.packet.header_length == 0 ||
1096 u.packet.header_length % ctx->header_size != 0)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001097 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +02001098 break;
1099
1100 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1101 if (u.packet.payload_length == 0 ||
1102 u.packet.payload_length & 3)
1103 return -EINVAL;
1104 break;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001105 }
1106
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001107 next = (struct fw_cdev_iso_packet __user *)
Stefan Richterae2a97662010-07-29 09:31:56 +02001108 &p->header[transmit_header_bytes / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001109 if (next > end)
1110 return -EINVAL;
1111 if (__copy_from_user
Stefan Richterae2a97662010-07-29 09:31:56 +02001112 (u.packet.header, p->header, transmit_header_bytes))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001113 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001114 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001115 u.packet.header_length + u.packet.payload_length > 0)
1116 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001117 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001118 return -EINVAL;
1119
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001120 if (fw_iso_context_queue(ctx, &u.packet,
1121 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001122 break;
1123
1124 p = next;
1125 payload += u.packet.payload_length;
1126 count++;
1127 }
1128
Stefan Richter6e95dea2010-02-21 17:56:21 +01001129 a->size -= uptr_to_u64(p) - a->packets;
1130 a->packets = uptr_to_u64(p);
1131 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001132
1133 return count;
1134}
1135
Stefan Richter6e95dea2010-02-21 17:56:21 +01001136static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001137{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001138 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001139
Stefan Richtereb5b35a2010-07-07 14:13:14 +02001140 BUILD_BUG_ON(
1141 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1142 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1143 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1144 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1145 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1146
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;
Stefan Richterfae60312008-02-20 21:10:06 +01001149
Stefan Richter6e95dea2010-02-21 17:56:21 +01001150 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1151 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1152 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001153
Stefan Richter6e95dea2010-02-21 17:56:21 +01001154 return fw_iso_context_start(client->iso_context,
1155 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001156}
1157
Stefan Richter6e95dea2010-02-21 17:56:21 +01001158static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001159{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001160 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001161
Stefan Richter6e95dea2010-02-21 17:56:21 +01001162 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001163 return -EINVAL;
1164
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001165 return fw_iso_context_stop(client->iso_context);
1166}
1167
Stefan Richter6e95dea2010-02-21 17:56:21 +01001168static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001169{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001170 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001171 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001172 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001173 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001174 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001175
Stefan Richter4a9bde92010-02-20 22:24:43 +01001176 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001177
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001178 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001179
Stefan Richter6e95dea2010-02-21 17:56:21 +01001180 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001181 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1182 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1183 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1184 default:
1185 ret = -EINVAL;
1186 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001187
Stefan Richter4a9bde92010-02-20 22:24:43 +01001188 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001189
Stefan Richter6e95dea2010-02-21 17:56:21 +01001190 a->tv_sec = ts.tv_sec;
1191 a->tv_nsec = ts.tv_nsec;
1192 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001193
1194 return ret;
1195}
1196
Stefan Richter6e95dea2010-02-21 17:56:21 +01001197static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001198{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001199 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001200 struct fw_cdev_get_cycle_timer2 ct2;
1201
1202 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001203 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001204
Stefan Richter6e95dea2010-02-21 17:56:21 +01001205 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1206 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001207
Stefan Richtera64408b2007-09-29 10:41:58 +02001208 return 0;
1209}
1210
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001211static void iso_resource_work(struct work_struct *work)
1212{
1213 struct iso_resource_event *e;
1214 struct iso_resource *r =
1215 container_of(work, struct iso_resource, work.work);
1216 struct client *client = r->client;
1217 int generation, channel, bandwidth, todo;
1218 bool skip, free, success;
1219
1220 spin_lock_irq(&client->lock);
1221 generation = client->device->generation;
1222 todo = r->todo;
1223 /* Allow 1000ms grace period for other reallocations. */
1224 if (todo == ISO_RES_ALLOC &&
1225 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001226 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001227 skip = true;
1228 } else {
1229 /* We could be called twice within the same generation. */
1230 skip = todo == ISO_RES_REALLOC &&
1231 r->generation == generation;
1232 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001233 free = todo == ISO_RES_DEALLOC ||
1234 todo == ISO_RES_ALLOC_ONCE ||
1235 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001236 r->generation = generation;
1237 spin_unlock_irq(&client->lock);
1238
1239 if (skip)
1240 goto out;
1241
1242 bandwidth = r->bandwidth;
1243
1244 fw_iso_resource_manage(client->device->card, generation,
1245 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001246 todo == ISO_RES_ALLOC ||
1247 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001248 todo == ISO_RES_ALLOC_ONCE,
1249 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001250 /*
1251 * Is this generation outdated already? As long as this resource sticks
1252 * in the idr, it will be scheduled again for a newer generation or at
1253 * shutdown.
1254 */
1255 if (channel == -EAGAIN &&
1256 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1257 goto out;
1258
1259 success = channel >= 0 || bandwidth > 0;
1260
1261 spin_lock_irq(&client->lock);
1262 /*
1263 * Transit from allocation to reallocation, except if the client
1264 * requested deallocation in the meantime.
1265 */
1266 if (r->todo == ISO_RES_ALLOC)
1267 r->todo = ISO_RES_REALLOC;
1268 /*
1269 * Allocation or reallocation failure? Pull this resource out of the
1270 * idr and prepare for deletion, unless the client is shutting down.
1271 */
1272 if (r->todo == ISO_RES_REALLOC && !success &&
1273 !client->in_shutdown &&
1274 idr_find(&client->resource_idr, r->resource.handle)) {
1275 idr_remove(&client->resource_idr, r->resource.handle);
1276 client_put(client);
1277 free = true;
1278 }
1279 spin_unlock_irq(&client->lock);
1280
1281 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001282 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001283
1284 if (todo == ISO_RES_REALLOC && success)
1285 goto out;
1286
Stefan Richter1ec3c022009-01-04 16:23:29 +01001287 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001288 e = r->e_alloc;
1289 r->e_alloc = NULL;
1290 } else {
1291 e = r->e_dealloc;
1292 r->e_dealloc = NULL;
1293 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001294 e->iso_resource.handle = r->resource.handle;
1295 e->iso_resource.channel = channel;
1296 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001297
1298 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001299 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001300
1301 if (free) {
1302 cancel_delayed_work(&r->work);
1303 kfree(r->e_alloc);
1304 kfree(r->e_dealloc);
1305 kfree(r);
1306 }
1307 out:
1308 client_put(client);
1309}
1310
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001311static void release_iso_resource(struct client *client,
1312 struct client_resource *resource)
1313{
1314 struct iso_resource *r =
1315 container_of(resource, struct iso_resource, resource);
1316
1317 spin_lock_irq(&client->lock);
1318 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001319 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001320 spin_unlock_irq(&client->lock);
1321}
1322
Stefan Richter1ec3c022009-01-04 16:23:29 +01001323static int init_iso_resource(struct client *client,
1324 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001325{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001326 struct iso_resource_event *e1, *e2;
1327 struct iso_resource *r;
1328 int ret;
1329
1330 if ((request->channels == 0 && request->bandwidth == 0) ||
1331 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1332 request->bandwidth < 0)
1333 return -EINVAL;
1334
1335 r = kmalloc(sizeof(*r), GFP_KERNEL);
1336 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1337 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1338 if (r == NULL || e1 == NULL || e2 == NULL) {
1339 ret = -ENOMEM;
1340 goto fail;
1341 }
1342
1343 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1344 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001345 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001346 r->generation = -1;
1347 r->channels = request->channels;
1348 r->bandwidth = request->bandwidth;
1349 r->e_alloc = e1;
1350 r->e_dealloc = e2;
1351
Stefan Richtere21fcf72009-10-08 00:41:38 +02001352 e1->iso_resource.closure = request->closure;
1353 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1354 e2->iso_resource.closure = request->closure;
1355 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001356
Stefan Richter1ec3c022009-01-04 16:23:29 +01001357 if (todo == ISO_RES_ALLOC) {
1358 r->resource.release = release_iso_resource;
1359 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001360 if (ret < 0)
1361 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001362 } else {
1363 r->resource.release = NULL;
1364 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001365 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001366 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001367 request->handle = r->resource.handle;
1368
1369 return 0;
1370 fail:
1371 kfree(r);
1372 kfree(e1);
1373 kfree(e2);
1374
1375 return ret;
1376}
1377
Stefan Richter6e95dea2010-02-21 17:56:21 +01001378static int ioctl_allocate_iso_resource(struct client *client,
1379 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001380{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001381 return init_iso_resource(client,
1382 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001383}
1384
Stefan Richter6e95dea2010-02-21 17:56:21 +01001385static int ioctl_deallocate_iso_resource(struct client *client,
1386 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001387{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001388 return release_client_resource(client,
1389 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001390}
1391
Stefan Richter6e95dea2010-02-21 17:56:21 +01001392static int ioctl_allocate_iso_resource_once(struct client *client,
1393 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001394{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001395 return init_iso_resource(client,
1396 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001397}
1398
Stefan Richter6e95dea2010-02-21 17:56:21 +01001399static int ioctl_deallocate_iso_resource_once(struct client *client,
1400 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001401{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001402 return init_iso_resource(client,
1403 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001404}
1405
Stefan Richterc8a25902009-03-10 20:59:16 +01001406/*
1407 * Returns a speed code: Maximum speed to or from this device,
1408 * limited by the device's link speed, the local node's link speed,
1409 * and all PHY port speeds between the two links.
1410 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001411static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001412{
Stefan Richterc8a25902009-03-10 20:59:16 +01001413 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001414}
1415
Stefan Richter6e95dea2010-02-21 17:56:21 +01001416static int ioctl_send_broadcast_request(struct client *client,
1417 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001418{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001419 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001420
Stefan Richter6e95dea2010-02-21 17:56:21 +01001421 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001422 case TCODE_WRITE_QUADLET_REQUEST:
1423 case TCODE_WRITE_BLOCK_REQUEST:
1424 break;
1425 default:
1426 return -EINVAL;
1427 }
1428
Stefan Richter1566f3d2009-01-04 16:23:29 +01001429 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001430 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001431 return -EACCES;
1432
Stefan Richter6e95dea2010-02-21 17:56:21 +01001433 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001434}
1435
Stefan Richter6e95dea2010-02-21 17:56:21 +01001436static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001437{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001438 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001439 struct fw_cdev_send_request request;
1440 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001441
Stefan Richter6e95dea2010-02-21 17:56:21 +01001442 if (a->speed > client->device->card->link_speed ||
1443 a->length > 1024 << a->speed)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001444 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001445
Stefan Richter6e95dea2010-02-21 17:56:21 +01001446 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001447 return -EINVAL;
1448
Stefan Richter6e95dea2010-02-21 17:56:21 +01001449 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001450 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001451 request.length = a->length;
1452 request.closure = a->closure;
1453 request.data = a->data;
1454 request.generation = a->generation;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001455
Stefan Richter6e95dea2010-02-21 17:56:21 +01001456 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001457}
1458
Stefan Richter850bb6f2010-07-16 22:25:14 +02001459static void outbound_phy_packet_callback(struct fw_packet *packet,
1460 struct fw_card *card, int status)
1461{
1462 struct outbound_phy_packet_event *e =
1463 container_of(packet, struct outbound_phy_packet_event, p);
1464
1465 switch (status) {
1466 /* expected: */
1467 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1468 /* should never happen with PHY packets: */
1469 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1470 case ACK_BUSY_X:
1471 case ACK_BUSY_A:
1472 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1473 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1474 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1475 /* stale generation; cancelled; on certain controllers: no ack */
1476 default: e->phy_packet.rcode = status; break;
1477 }
Stefan Richtercc550212010-07-18 13:00:50 +02001478 e->phy_packet.data[0] = packet->timestamp;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001479
Stefan Richtercc550212010-07-18 13:00:50 +02001480 queue_event(e->client, &e->event, &e->phy_packet,
1481 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001482 client_put(e->client);
1483}
1484
1485static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1486{
1487 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1488 struct fw_card *card = client->device->card;
1489 struct outbound_phy_packet_event *e;
1490
1491 /* Access policy: Allow this ioctl only on local nodes' device files. */
1492 if (!client->device->is_local)
1493 return -ENOSYS;
1494
Stefan Richtercc550212010-07-18 13:00:50 +02001495 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001496 if (e == NULL)
1497 return -ENOMEM;
1498
1499 client_get(client);
1500 e->client = client;
1501 e->p.speed = SCODE_100;
1502 e->p.generation = a->generation;
1503 e->p.header[0] = a->data[0];
1504 e->p.header[1] = a->data[1];
1505 e->p.header_length = 8;
1506 e->p.callback = outbound_phy_packet_callback;
1507 e->phy_packet.closure = a->closure;
1508 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
Stefan Richtercc550212010-07-18 13:00:50 +02001509 if (is_ping_packet(a->data))
1510 e->phy_packet.length = 4;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001511
1512 card->driver->send_request(card, &e->p);
1513
1514 return 0;
1515}
1516
Stefan Richterbf54e142010-07-16 22:25:51 +02001517static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1518{
1519 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1520 struct fw_card *card = client->device->card;
1521
1522 /* Access policy: Allow this ioctl only on local nodes' device files. */
1523 if (!client->device->is_local)
1524 return -ENOSYS;
1525
1526 spin_lock_irq(&card->lock);
1527
1528 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1529 client->phy_receiver_closure = a->closure;
1530
1531 spin_unlock_irq(&card->lock);
1532
1533 return 0;
1534}
1535
1536void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1537{
1538 struct client *client;
1539 struct inbound_phy_packet_event *e;
1540 unsigned long flags;
1541
1542 spin_lock_irqsave(&card->lock, flags);
1543
1544 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1545 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1546 if (e == NULL) {
1547 fw_notify("Out of memory when allocating event\n");
1548 break;
1549 }
1550 e->phy_packet.closure = client->phy_receiver_closure;
1551 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1552 e->phy_packet.rcode = RCODE_COMPLETE;
1553 e->phy_packet.length = 8;
1554 e->phy_packet.data[0] = p->header[1];
1555 e->phy_packet.data[1] = p->header[2];
1556 queue_event(client, &e->event,
1557 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1558 }
1559
1560 spin_unlock_irqrestore(&card->lock, flags);
1561}
1562
Stefan Richter6e95dea2010-02-21 17:56:21 +01001563static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Stefan Richterb9dc61c2010-07-16 22:24:29 +02001564 [0x00] = ioctl_get_info,
1565 [0x01] = ioctl_send_request,
1566 [0x02] = ioctl_allocate,
1567 [0x03] = ioctl_deallocate,
1568 [0x04] = ioctl_send_response,
1569 [0x05] = ioctl_initiate_bus_reset,
1570 [0x06] = ioctl_add_descriptor,
1571 [0x07] = ioctl_remove_descriptor,
1572 [0x08] = ioctl_create_iso_context,
1573 [0x09] = ioctl_queue_iso,
1574 [0x0a] = ioctl_start_iso,
1575 [0x0b] = ioctl_stop_iso,
1576 [0x0c] = ioctl_get_cycle_timer,
1577 [0x0d] = ioctl_allocate_iso_resource,
1578 [0x0e] = ioctl_deallocate_iso_resource,
1579 [0x0f] = ioctl_allocate_iso_resource_once,
1580 [0x10] = ioctl_deallocate_iso_resource_once,
1581 [0x11] = ioctl_get_speed,
1582 [0x12] = ioctl_send_broadcast_request,
1583 [0x13] = ioctl_send_stream_packet,
1584 [0x14] = ioctl_get_cycle_timer2,
Stefan Richter850bb6f2010-07-16 22:25:14 +02001585 [0x15] = ioctl_send_phy_packet,
Stefan Richterbf54e142010-07-16 22:25:51 +02001586 [0x16] = ioctl_receive_phy_packets,
Stefan Richter872e3302010-07-29 18:19:22 +02001587 [0x17] = ioctl_set_iso_channels,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001588};
1589
Stefan Richter53dca512008-12-14 21:47:04 +01001590static int dispatch_ioctl(struct client *client,
1591 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001592{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001593 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001594 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001595
Stefan Richter64582292010-02-21 17:56:42 +01001596 if (fw_device_is_shutdown(client->device))
1597 return -ENODEV;
1598
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001599 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001600 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1601 _IOC_SIZE(cmd) > sizeof(buffer))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001602 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001603
Stefan Richter9cac00b2010-04-07 08:30:50 +02001604 if (_IOC_DIR(cmd) == _IOC_READ)
1605 memset(&buffer, 0, _IOC_SIZE(cmd));
1606
1607 if (_IOC_DIR(cmd) & _IOC_WRITE)
1608 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001609 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001610
Stefan Richter6e95dea2010-02-21 17:56:21 +01001611 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001612 if (ret < 0)
1613 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001614
Stefan Richter9cac00b2010-04-07 08:30:50 +02001615 if (_IOC_DIR(cmd) & _IOC_READ)
1616 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001617 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001618
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001619 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001620}
1621
Stefan Richter53dca512008-12-14 21:47:04 +01001622static long fw_device_op_ioctl(struct file *file,
1623 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001624{
Stefan Richter64582292010-02-21 17:56:42 +01001625 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001626}
1627
1628#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001629static long fw_device_op_compat_ioctl(struct file *file,
1630 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001631{
Stefan Richter64582292010-02-21 17:56:42 +01001632 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001633}
1634#endif
1635
1636static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1637{
1638 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001639 enum dma_data_direction direction;
1640 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001641 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001642
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001643 if (fw_device_is_shutdown(client->device))
1644 return -ENODEV;
1645
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001646 /* FIXME: We could support multiple buffers, but we don't. */
1647 if (client->buffer.pages != NULL)
1648 return -EBUSY;
1649
1650 if (!(vma->vm_flags & VM_SHARED))
1651 return -EINVAL;
1652
1653 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001654 return -EINVAL;
1655
1656 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001657 size = vma->vm_end - vma->vm_start;
1658 page_count = size >> PAGE_SHIFT;
1659 if (size & ~PAGE_MASK)
1660 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001661
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001662 if (vma->vm_flags & VM_WRITE)
1663 direction = DMA_TO_DEVICE;
1664 else
1665 direction = DMA_FROM_DEVICE;
1666
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001667 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1668 page_count, direction);
1669 if (ret < 0)
1670 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001671
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001672 ret = fw_iso_buffer_map(&client->buffer, vma);
1673 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001674 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1675
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001676 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001677}
1678
Jay Fenlason45ee3192008-12-21 16:47:17 +01001679static int shutdown_resource(int id, void *p, void *data)
1680{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001681 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001682 struct client *client = data;
1683
Stefan Richtere21fcf72009-10-08 00:41:38 +02001684 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001685 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001686
1687 return 0;
1688}
1689
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001690static int fw_device_op_release(struct inode *inode, struct file *file)
1691{
1692 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001693 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001694
Stefan Richterbf54e142010-07-16 22:25:51 +02001695 spin_lock_irq(&client->device->card->lock);
1696 list_del(&client->phy_receiver_link);
1697 spin_unlock_irq(&client->device->card->lock);
1698
Stefan Richter97811e32008-12-14 19:19:23 +01001699 mutex_lock(&client->device->client_list_mutex);
1700 list_del(&client->link);
1701 mutex_unlock(&client->device->client_list_mutex);
1702
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001703 if (client->iso_context)
1704 fw_iso_context_destroy(client->iso_context);
1705
Stefan Richter36a755c2009-01-05 20:28:10 +01001706 if (client->buffer.pages)
1707 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1708
Jay Fenlason45ee3192008-12-21 16:47:17 +01001709 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001710 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001711 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001712 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001713
Jay Fenlason45ee3192008-12-21 16:47:17 +01001714 idr_for_each(&client->resource_idr, shutdown_resource, client);
1715 idr_remove_all(&client->resource_idr);
1716 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001717
Stefan Richtere21fcf72009-10-08 00:41:38 +02001718 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1719 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001720
Stefan Richterfb443032009-01-04 16:23:29 +01001721 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001722
1723 return 0;
1724}
1725
1726static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1727{
1728 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001729 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001730
1731 poll_wait(file, &client->wait, pt);
1732
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001733 if (fw_device_is_shutdown(client->device))
1734 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001735 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001736 mask |= POLLIN | POLLRDNORM;
1737
1738 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001739}
1740
Stefan Richter21ebcd12007-01-14 15:29:07 +01001741const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001742 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001743 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001744 .open = fw_device_op_open,
1745 .read = fw_device_op_read,
1746 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001747 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001748 .release = fw_device_op_release,
1749 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001750#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001751 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001752#endif
1753};