blob: a4aa477b9b2c3d2e1d6af6a3a06a7b2a635e26ca [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 Richterbe5bbd62009-01-04 16:23:29 +010021#include <linux/compat.h>
22#include <linux/delay.h>
23#include <linux/device.h>
24#include <linux/errno.h>
Stefan Richter77c9a5d2009-06-05 16:26:18 +020025#include <linux/firewire.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010026#include <linux/firewire-cdev.h>
27#include <linux/idr.h>
Stefan Richter4a9bde92010-02-20 22:24:43 +010028#include <linux/irqflags.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010029#include <linux/jiffies.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050030#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010031#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010032#include <linux/mm.h>
33#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020034#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050035#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040036#include <linux/sched.h>
Jay Fenlasoncf417e52008-10-03 11:19:09 -040037#include <linux/spinlock.h>
Stefan Richter281e2032010-01-24 16:45:03 +010038#include <linux/string.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010039#include <linux/time.h>
Stefan Richtere034d242009-06-06 18:36:24 +020040#include <linux/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010041#include <linux/vmalloc.h>
42#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010043#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010044
Stefan Richtera64408b2007-09-29 10:41:58 +020045#include <asm/system.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010046
Stefan Richter77c9a5d2009-06-05 16:26:18 +020047#include "core.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050048
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050049struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050050 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050051 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010052
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050053 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010054 bool in_shutdown;
55 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050056 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040058 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050059
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050060 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040061 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050062 struct fw_iso_buffer buffer;
63 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050064
65 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010066 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050067};
68
Stefan Richterfb443032009-01-04 16:23:29 +010069static inline void client_get(struct client *client)
70{
71 kref_get(&client->kref);
72}
73
74static void client_release(struct kref *kref)
75{
76 struct client *client = container_of(kref, struct client, kref);
77
78 fw_device_put(client->device);
79 kfree(client);
80}
81
82static void client_put(struct client *client)
83{
84 kref_put(&client->kref, client_release);
85}
86
Stefan Richter97c18b72009-01-04 16:23:29 +010087struct client_resource;
88typedef void (*client_resource_release_fn_t)(struct client *,
89 struct client_resource *);
90struct client_resource {
91 client_resource_release_fn_t release;
92 int handle;
93};
94
95struct address_handler_resource {
96 struct client_resource resource;
97 struct fw_address_handler handler;
98 __u64 closure;
99 struct client *client;
100};
101
102struct outbound_transaction_resource {
103 struct client_resource resource;
104 struct fw_transaction transaction;
105};
106
107struct inbound_transaction_resource {
108 struct client_resource resource;
109 struct fw_request *request;
110 void *data;
111 size_t length;
112};
113
114struct descriptor_resource {
115 struct client_resource resource;
116 struct fw_descriptor descriptor;
117 u32 data[0];
118};
119
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100120struct iso_resource {
121 struct client_resource resource;
122 struct client *client;
123 /* Schedule work and access todo only with client->lock held. */
124 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100125 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
126 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100127 int generation;
128 u64 channels;
129 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200130 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100131 struct iso_resource_event *e_alloc, *e_dealloc;
132};
133
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100134static void release_iso_resource(struct client *, struct client_resource *);
135
Stefan Richter9fb551b2009-10-08 00:41:10 +0200136static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
137{
138 client_get(r->client);
139 if (!schedule_delayed_work(&r->work, delay))
140 client_put(r->client);
141}
142
143static void schedule_if_iso_resource(struct client_resource *resource)
144{
145 if (resource->release == release_iso_resource)
146 schedule_iso_resource(container_of(resource,
147 struct iso_resource, resource), 0);
148}
149
Stefan Richter97c18b72009-01-04 16:23:29 +0100150/*
151 * dequeue_event() just kfree()'s the event, so the event has to be
152 * the first field in a struct XYZ_event.
153 */
154struct event {
155 struct { void *data; size_t size; } v[2];
156 struct list_head link;
157};
158
159struct bus_reset_event {
160 struct event event;
161 struct fw_cdev_event_bus_reset reset;
162};
163
164struct outbound_transaction_event {
165 struct event event;
166 struct client *client;
167 struct outbound_transaction_resource r;
168 struct fw_cdev_event_response response;
169};
170
171struct inbound_transaction_event {
172 struct event event;
173 struct fw_cdev_event_request request;
174};
175
176struct iso_interrupt_event {
177 struct event event;
178 struct fw_cdev_event_iso_interrupt interrupt;
179};
180
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100181struct iso_resource_event {
182 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200183 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100184};
185
Stefan Richter53dca512008-12-14 21:47:04 +0100186static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500187{
188 return (void __user *)(unsigned long)value;
189}
190
Stefan Richter53dca512008-12-14 21:47:04 +0100191static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500192{
193 return (__u64)(unsigned long)ptr;
194}
195
196static int fw_device_op_open(struct inode *inode, struct file *file)
197{
198 struct fw_device *device;
199 struct client *client;
200
Stefan Richter96b19062008-02-02 15:01:09 +0100201 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500202 if (device == NULL)
203 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500204
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400205 if (fw_device_is_shutdown(device)) {
206 fw_device_put(device);
207 return -ENODEV;
208 }
209
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400210 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100211 if (client == NULL) {
212 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500213 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100214 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500215
Stefan Richter96b19062008-02-02 15:01:09 +0100216 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500217 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100218 idr_init(&client->resource_idr);
219 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500220 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100221 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500222
223 file->private_data = client;
224
Stefan Richterd67cfb92008-10-05 10:37:11 +0200225 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500226 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200227 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500228
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500229 return 0;
230}
231
232static void queue_event(struct client *client, struct event *event,
233 void *data0, size_t size0, void *data1, size_t size1)
234{
235 unsigned long flags;
236
237 event->v[0].data = data0;
238 event->v[0].size = size0;
239 event->v[1].data = data1;
240 event->v[1].size = size1;
241
242 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100243 if (client->in_shutdown)
244 kfree(event);
245 else
246 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500247 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400248
249 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500250}
251
Stefan Richter53dca512008-12-14 21:47:04 +0100252static int dequeue_event(struct client *client,
253 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500254{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500255 struct event *event;
256 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100257 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500258
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100259 ret = wait_event_interruptible(client->wait,
260 !list_empty(&client->event_list) ||
261 fw_device_is_shutdown(client->device));
262 if (ret < 0)
263 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500264
265 if (list_empty(&client->event_list) &&
266 fw_device_is_shutdown(client->device))
267 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500268
Stefan Richter3ba94982009-01-04 16:23:29 +0100269 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100270 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500271 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100272 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500273
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500274 total = 0;
275 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
276 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500277 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100278 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500279 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500280 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500281 total += size;
282 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100283 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500284
285 out:
286 kfree(event);
287
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100288 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500289}
290
Stefan Richter53dca512008-12-14 21:47:04 +0100291static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
292 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500293{
294 struct client *client = file->private_data;
295
296 return dequeue_event(client, buffer, count);
297}
298
Stefan Richter53dca512008-12-14 21:47:04 +0100299static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
300 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500301{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400302 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e52008-10-03 11:19:09 -0400303
Stefan Richter3ba94982009-01-04 16:23:29 +0100304 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500305
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400306 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500307 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100308 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400309 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500310 event->local_node_id = card->local_node->node_id;
311 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
312 event->irm_node_id = card->irm_node->node_id;
313 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e52008-10-03 11:19:09 -0400314
Stefan Richter3ba94982009-01-04 16:23:29 +0100315 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500316}
317
Stefan Richter53dca512008-12-14 21:47:04 +0100318static void for_each_client(struct fw_device *device,
319 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500320{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500321 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500322
Stefan Richterd67cfb92008-10-05 10:37:11 +0200323 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500324 list_for_each_entry(c, &device->client_list, link)
325 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200326 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500327}
328
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100329static int schedule_reallocations(int id, void *p, void *data)
330{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200331 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100332
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100333 return 0;
334}
335
Stefan Richter53dca512008-12-14 21:47:04 +0100336static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500337{
Stefan Richter97c18b72009-01-04 16:23:29 +0100338 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500339
Stefan Richter97c18b72009-01-04 16:23:29 +0100340 e = kzalloc(sizeof(*e), GFP_KERNEL);
341 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500342 fw_notify("Out of memory when allocating bus reset event\n");
343 return;
344 }
345
Stefan Richter97c18b72009-01-04 16:23:29 +0100346 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500347
Stefan Richter97c18b72009-01-04 16:23:29 +0100348 queue_event(client, &e->event,
349 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100350
351 spin_lock_irq(&client->lock);
352 idr_for_each(&client->resource_idr, schedule_reallocations, client);
353 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500354}
355
356void fw_device_cdev_update(struct fw_device *device)
357{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500358 for_each_client(device, queue_bus_reset_event);
359}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500360
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500361static void wake_up_client(struct client *client)
362{
363 wake_up_interruptible(&client->wait);
364}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500365
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500366void fw_device_cdev_remove(struct fw_device *device)
367{
368 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500369}
370
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400371static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500372{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400373 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500374 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100375 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500376
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400377 client->version = get_info->version;
378 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e52008-10-03 11:19:09 -0400379 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500380
Stefan Richterc9755e12008-03-24 20:54:28 +0100381 down_read(&fw_device_rwsem);
382
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400383 if (get_info->rom != 0) {
384 void __user *uptr = u64_to_uptr(get_info->rom);
385 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100386 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500387
Stefan Richterc9755e12008-03-24 20:54:28 +0100388 ret = copy_to_user(uptr, client->device->config_rom,
389 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500390 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400391 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500392
Stefan Richterc9755e12008-03-24 20:54:28 +0100393 up_read(&fw_device_rwsem);
394
395 if (ret != 0)
396 return -EFAULT;
397
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400398 client->bus_reset_closure = get_info->bus_reset_closure;
399 if (get_info->bus_reset != 0) {
400 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500401
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400402 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400403 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500404 return -EFAULT;
405 }
406
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500407 return 0;
408}
409
Stefan Richter53dca512008-12-14 21:47:04 +0100410static int add_client_resource(struct client *client,
411 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400412{
413 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100414 int ret;
415
416 retry:
417 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
418 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400419
420 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100421 if (client->in_shutdown)
422 ret = -ECANCELED;
423 else
424 ret = idr_get_new(&client->resource_idr, resource,
425 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100426 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100427 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200428 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100429 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400430 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100431
432 if (ret == -EAGAIN)
433 goto retry;
434
435 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400436}
437
Stefan Richter53dca512008-12-14 21:47:04 +0100438static int release_client_resource(struct client *client, u32 handle,
439 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200440 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400441{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200442 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400443
Stefan Richter3ba94982009-01-04 16:23:29 +0100444 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100445 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200446 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100447 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200448 resource = idr_find(&client->resource_idr, handle);
449 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100450 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100451 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400452
Stefan Richtere21fcf72009-10-08 00:41:38 +0200453 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400454 return -EINVAL;
455
Stefan Richtere21fcf72009-10-08 00:41:38 +0200456 if (return_resource)
457 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400458 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200459 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400460
Stefan Richterfb443032009-01-04 16:23:29 +0100461 client_put(client);
462
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400463 return 0;
464}
465
Stefan Richter53dca512008-12-14 21:47:04 +0100466static void release_transaction(struct client *client,
467 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400468{
Stefan Richter97c18b72009-01-04 16:23:29 +0100469 struct outbound_transaction_resource *r = container_of(resource,
470 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400471
Stefan Richter97c18b72009-01-04 16:23:29 +0100472 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400473}
474
Stefan Richter53dca512008-12-14 21:47:04 +0100475static void complete_transaction(struct fw_card *card, int rcode,
476 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500477{
Stefan Richter97c18b72009-01-04 16:23:29 +0100478 struct outbound_transaction_event *e = data;
479 struct fw_cdev_event_response *rsp = &e->response;
480 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500481 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500482
Stefan Richter97c18b72009-01-04 16:23:29 +0100483 if (length < rsp->length)
484 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500485 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100486 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500487
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500488 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100489 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100490 * 1. If called while in shutdown, the idr tree must be left untouched.
491 * The idr handle will be removed and the client reference will be
492 * dropped later.
493 * 2. If the call chain was release_client_resource ->
494 * release_transaction -> complete_transaction (instead of a normal
495 * conclusion of the transaction), i.e. if this resource was already
496 * unregistered from the idr, the client reference will be dropped
497 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100498 */
Stefan Richterfb443032009-01-04 16:23:29 +0100499 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100500 idr_find(&client->resource_idr, e->r.resource.handle)) {
501 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100502 /* Drop the idr's reference */
503 client_put(client);
504 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500505 spin_unlock_irqrestore(&client->lock, flags);
506
Stefan Richter97c18b72009-01-04 16:23:29 +0100507 rsp->type = FW_CDEV_EVENT_RESPONSE;
508 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700509
510 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100511 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700512 * data, and the read is short, preserve an extra copy of the data
513 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
514 * for short reads and some apps depended on it, this is both safe
515 * and prudent for compatibility.
516 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100517 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
518 queue_event(client, &e->event, rsp, sizeof(*rsp),
519 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700520 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100521 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700522 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100523
524 /* Drop the transaction callback's reference */
525 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500526}
527
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100528static int init_request(struct client *client,
529 struct fw_cdev_send_request *request,
530 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500531{
Stefan Richter97c18b72009-01-04 16:23:29 +0100532 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100533 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500534
Stefan Richter18e9b102009-03-10 21:02:21 +0100535 if (request->tcode != TCODE_STREAM_DATA &&
536 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100537 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500538
Stefan Richter97c18b72009-01-04 16:23:29 +0100539 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
540 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500541 return -ENOMEM;
542
Stefan Richter97c18b72009-01-04 16:23:29 +0100543 e->client = client;
544 e->response.length = request->length;
545 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500546
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400547 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100548 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400549 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100550 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100551 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100552 }
553
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100554 e->r.resource.release = release_transaction;
555 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
556 if (ret < 0)
557 goto failed;
558
559 /* Get a reference for the transaction callback */
560 client_get(client);
561
562 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100563 request->tcode, destination_id, request->generation,
564 speed, request->offset, e->response.data,
565 request->length, complete_transaction, e);
566 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100567
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100568 failed:
569 kfree(e);
570
571 return ret;
572}
573
574static int ioctl_send_request(struct client *client, void *buffer)
575{
576 struct fw_cdev_send_request *request = buffer;
577
Stefan Richter1f3125a2008-12-05 22:44:42 +0100578 switch (request->tcode) {
579 case TCODE_WRITE_QUADLET_REQUEST:
580 case TCODE_WRITE_BLOCK_REQUEST:
581 case TCODE_READ_QUADLET_REQUEST:
582 case TCODE_READ_BLOCK_REQUEST:
583 case TCODE_LOCK_MASK_SWAP:
584 case TCODE_LOCK_COMPARE_SWAP:
585 case TCODE_LOCK_FETCH_ADD:
586 case TCODE_LOCK_LITTLE_ADD:
587 case TCODE_LOCK_BOUNDED_ADD:
588 case TCODE_LOCK_WRAP_ADD:
589 case TCODE_LOCK_VENDOR_DEPENDENT:
590 break;
591 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100592 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500593 }
594
Stefan Richter207fbef2009-03-10 21:01:08 +0100595 return init_request(client, request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100596 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500597}
598
Stefan Richter281e2032010-01-24 16:45:03 +0100599static inline bool is_fcp_request(struct fw_request *request)
600{
601 return request == NULL;
602}
603
Stefan Richter53dca512008-12-14 21:47:04 +0100604static void release_request(struct client *client,
605 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400606{
Stefan Richter97c18b72009-01-04 16:23:29 +0100607 struct inbound_transaction_resource *r = container_of(resource,
608 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400609
Stefan Richter281e2032010-01-24 16:45:03 +0100610 if (is_fcp_request(r->request))
611 kfree(r->data);
612 else
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100613 fw_send_response(client->device->card, r->request,
614 RCODE_CONFLICT_ERROR);
Stefan Richter97c18b72009-01-04 16:23:29 +0100615 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400616}
617
Stefan Richter97c18b72009-01-04 16:23:29 +0100618static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100619 int tcode, int destination, int source,
620 int generation, int speed,
621 unsigned long long offset,
622 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500623{
Stefan Richter97c18b72009-01-04 16:23:29 +0100624 struct address_handler_resource *handler = callback_data;
625 struct inbound_transaction_resource *r;
626 struct inbound_transaction_event *e;
Stefan Richter281e2032010-01-24 16:45:03 +0100627 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100628 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500629
Stefan Richter97c18b72009-01-04 16:23:29 +0100630 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400631 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100632 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100633 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500634
Stefan Richter97c18b72009-01-04 16:23:29 +0100635 r->request = request;
636 r->data = payload;
637 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500638
Stefan Richter281e2032010-01-24 16:45:03 +0100639 if (is_fcp_request(request)) {
640 /*
641 * FIXME: Let core-transaction.c manage a
642 * single reference-counted copy?
643 */
644 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
645 if (fcp_frame == NULL)
646 goto failed;
647
648 r->data = fcp_frame;
649 }
650
Stefan Richter97c18b72009-01-04 16:23:29 +0100651 r->resource.release = release_request;
652 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100653 if (ret < 0)
654 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655
656 e->request.type = FW_CDEV_EVENT_REQUEST;
657 e->request.tcode = tcode;
658 e->request.offset = offset;
659 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100660 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500661 e->request.closure = handler->closure;
662
Stefan Richter97c18b72009-01-04 16:23:29 +0100663 queue_event(handler->client, &e->event,
Stefan Richter281e2032010-01-24 16:45:03 +0100664 &e->request, sizeof(e->request), r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100665 return;
666
667 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100668 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100669 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100670 kfree(fcp_frame);
671
672 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100673 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500674}
675
Stefan Richter53dca512008-12-14 21:47:04 +0100676static void release_address_handler(struct client *client,
677 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400678{
Stefan Richter97c18b72009-01-04 16:23:29 +0100679 struct address_handler_resource *r =
680 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400681
Stefan Richter97c18b72009-01-04 16:23:29 +0100682 fw_core_remove_address_handler(&r->handler);
683 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400684}
685
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400686static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500687{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400688 struct fw_cdev_allocate *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100689 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500690 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100691 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500692
Stefan Richter97c18b72009-01-04 16:23:29 +0100693 r = kmalloc(sizeof(*r), GFP_KERNEL);
694 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500695 return -ENOMEM;
696
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400697 region.start = request->offset;
698 region.end = request->offset + request->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100699 r->handler.length = request->length;
700 r->handler.address_callback = handle_request;
701 r->handler.callback_data = r;
702 r->closure = request->closure;
703 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500704
Stefan Richter97c18b72009-01-04 16:23:29 +0100705 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100706 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100707 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100708 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500709 }
710
Stefan Richter97c18b72009-01-04 16:23:29 +0100711 r->resource.release = release_address_handler;
712 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100713 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100714 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100715 return ret;
716 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100717 request->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500718
719 return 0;
720}
721
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400722static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400723{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400724 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400725
Jay Fenlason45ee3192008-12-21 16:47:17 +0100726 return release_client_resource(client, request->handle,
727 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400728}
729
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400730static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400732 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400733 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100734 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200735 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500736
Jay Fenlason45ee3192008-12-21 16:47:17 +0100737 if (release_client_resource(client, request->handle,
738 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500739 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100740
Stefan Richter97c18b72009-01-04 16:23:29 +0100741 r = container_of(resource, struct inbound_transaction_resource,
742 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100743 if (is_fcp_request(r->request))
744 goto out;
745
746 if (request->length < r->length)
747 r->length = request->length;
748 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) {
749 ret = -EFAULT;
750 kfree(r->request);
751 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200752 }
Stefan Richter281e2032010-01-24 16:45:03 +0100753 fw_send_response(client->device->card, r->request, request->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200754 out:
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500755 kfree(r);
756
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200757 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500758}
759
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400760static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500761{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400762 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500763 int short_reset;
764
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400765 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500766
767 return fw_core_initiate_bus_reset(client->device->card, short_reset);
768}
769
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400770static void release_descriptor(struct client *client,
771 struct client_resource *resource)
772{
Stefan Richter97c18b72009-01-04 16:23:29 +0100773 struct descriptor_resource *r =
774 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400775
Stefan Richter97c18b72009-01-04 16:23:29 +0100776 fw_core_remove_descriptor(&r->descriptor);
777 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400778}
779
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400780static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200781{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400782 struct fw_cdev_add_descriptor *request = buffer;
Stefan Richter97c18b72009-01-04 16:23:29 +0100783 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100784 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200785
Stefan Richterde487da2009-03-10 21:00:23 +0100786 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200787 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100788 return -ENOSYS;
789
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400790 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200791 return -EINVAL;
792
Stefan Richter97c18b72009-01-04 16:23:29 +0100793 r = kmalloc(sizeof(*r) + request->length * 4, GFP_KERNEL);
794 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200795 return -ENOMEM;
796
Stefan Richter97c18b72009-01-04 16:23:29 +0100797 if (copy_from_user(r->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400798 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100799 ret = -EFAULT;
800 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200801 }
802
Stefan Richter97c18b72009-01-04 16:23:29 +0100803 r->descriptor.length = request->length;
804 r->descriptor.immediate = request->immediate;
805 r->descriptor.key = request->key;
806 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200807
Stefan Richter97c18b72009-01-04 16:23:29 +0100808 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100809 if (ret < 0)
810 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200811
Stefan Richter97c18b72009-01-04 16:23:29 +0100812 r->resource.release = release_descriptor;
813 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100814 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100815 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100816 goto failed;
817 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100818 request->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200819
820 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100821 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100822 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100823
824 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200825}
826
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400827static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200828{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400829 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200830
Jay Fenlason45ee3192008-12-21 16:47:17 +0100831 return release_client_resource(client, request->handle,
832 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200833}
834
Stefan Richter53dca512008-12-14 21:47:04 +0100835static void iso_callback(struct fw_iso_context *context, u32 cycle,
836 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500837{
838 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100839 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500840
Stefan Richter97c18b72009-01-04 16:23:29 +0100841 e = kzalloc(sizeof(*e) + header_length, GFP_ATOMIC);
842 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500843 return;
844
Stefan Richter97c18b72009-01-04 16:23:29 +0100845 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
846 e->interrupt.closure = client->iso_closure;
847 e->interrupt.cycle = cycle;
848 e->interrupt.header_length = header_length;
849 memcpy(e->interrupt.header, header, header_length);
850 queue_event(client, &e->event, &e->interrupt,
851 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500852}
853
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400854static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400856 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400857 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500858
Stefan Richterfae60312008-02-20 21:10:06 +0100859 /* We only support one context at this time. */
860 if (client->iso_context != NULL)
861 return -EBUSY;
862
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400863 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500864 return -EINVAL;
865
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400866 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400867 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400868 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400869 return -EINVAL;
870
871 break;
872
873 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400874 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400875 return -EINVAL;
876
877 break;
878
879 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500880 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400881 }
882
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400883 context = fw_iso_context_create(client->device->card,
884 request->type,
885 request->channel,
886 request->speed,
887 request->header_size,
888 iso_callback, client);
889 if (IS_ERR(context))
890 return PTR_ERR(context);
891
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400892 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400893 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500894
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400895 /* We only support one context at this time. */
896 request->handle = 0;
897
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500898 return 0;
899}
900
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400901/* Macros for decoding the iso packet control header. */
902#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
903#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
904#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200905#define GET_TAG(v) (((v) >> 18) & 0x03)
906#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400907#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
908
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400909static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500910{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400911 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500912 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500913 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200914 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400915 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500916 int count;
917 struct {
918 struct fw_iso_packet packet;
919 u8 header[256];
920 } u;
921
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400922 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500923 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500924
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400925 /*
926 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500927 * the iso buffer, and the pointer points inside the buffer,
928 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500929 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500930 * payload_length == 0 through. In other words, if no packets
931 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400932 * and the request->data pointer is ignored.
933 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500934
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400935 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200936 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400937 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200938 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500939 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200940 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500941 }
942
Al Viro1ccc9142007-10-14 19:34:40 +0100943 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
944
945 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500946 return -EFAULT;
947
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400948 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949 count = 0;
950 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400951 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500952 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400953 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
954 u.packet.interrupt = GET_INTERRUPT(control);
955 u.packet.skip = GET_SKIP(control);
956 u.packet.tag = GET_TAG(control);
957 u.packet.sy = GET_SY(control);
958 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500959
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500960 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500961 header_length = u.packet.header_length;
962 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400963 /*
964 * We require that header_length is a multiple of
965 * the fixed header size, ctx->header_size.
966 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500967 if (ctx->header_size == 0) {
968 if (u.packet.header_length > 0)
969 return -EINVAL;
970 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500971 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500972 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500973 header_length = 0;
974 }
975
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500976 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500977 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500978 if (next > end)
979 return -EINVAL;
980 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500981 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500982 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500983 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500984 u.packet.header_length + u.packet.payload_length > 0)
985 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200986 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500987 return -EINVAL;
988
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500989 if (fw_iso_context_queue(ctx, &u.packet,
990 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500991 break;
992
993 p = next;
994 payload += u.packet.payload_length;
995 count++;
996 }
997
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400998 request->size -= uptr_to_u64(p) - request->packets;
999 request->packets = uptr_to_u64(p);
1000 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001001
1002 return count;
1003}
1004
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001005static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001006{
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001007 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001008
Stefan Richterfae60312008-02-20 21:10:06 +01001009 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001010 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001011
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001012 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001013 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001014 return -EINVAL;
1015
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001016 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001017 return -EINVAL;
1018 }
1019
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001020 return fw_iso_context_start(client->iso_context, request->cycle,
1021 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001022}
1023
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001024static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001025{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001026 struct fw_cdev_stop_iso *request = buffer;
1027
Stefan Richterfae60312008-02-20 21:10:06 +01001028 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001029 return -EINVAL;
1030
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001031 return fw_iso_context_stop(client->iso_context);
1032}
1033
Stefan Richterabfe5a02010-02-20 12:13:49 +01001034static int ioctl_get_cycle_timer2(struct client *client, void *buffer)
Stefan Richtera64408b2007-09-29 10:41:58 +02001035{
Stefan Richterabfe5a02010-02-20 12:13:49 +01001036 struct fw_cdev_get_cycle_timer2 *request = buffer;
Stefan Richtera64408b2007-09-29 10:41:58 +02001037 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001038 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001039 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001040 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001041
Stefan Richter4a9bde92010-02-20 22:24:43 +01001042 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001043
Stefan Richter168cf9a2010-02-14 18:49:18 +01001044 cycle_time = card->driver->get_cycle_time(card);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001045
1046 switch (request->clk_id) {
1047 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1048 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1049 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1050 default:
1051 ret = -EINVAL;
1052 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001053
Stefan Richter4a9bde92010-02-20 22:24:43 +01001054 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001055
Stefan Richterabfe5a02010-02-20 12:13:49 +01001056 request->tv_sec = ts.tv_sec;
1057 request->tv_nsec = ts.tv_nsec;
1058 request->cycle_timer = cycle_time;
1059
1060 return ret;
1061}
1062
1063static int ioctl_get_cycle_timer(struct client *client, void *buffer)
1064{
1065 struct fw_cdev_get_cycle_timer *request = buffer;
1066 struct fw_cdev_get_cycle_timer2 ct2;
1067
1068 ct2.clk_id = CLOCK_REALTIME;
1069 ioctl_get_cycle_timer2(client, &ct2);
1070
1071 request->local_time = ct2.tv_sec * USEC_PER_SEC +
1072 ct2.tv_nsec / NSEC_PER_USEC;
1073 request->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001074
Stefan Richtera64408b2007-09-29 10:41:58 +02001075 return 0;
1076}
1077
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001078static void iso_resource_work(struct work_struct *work)
1079{
1080 struct iso_resource_event *e;
1081 struct iso_resource *r =
1082 container_of(work, struct iso_resource, work.work);
1083 struct client *client = r->client;
1084 int generation, channel, bandwidth, todo;
1085 bool skip, free, success;
1086
1087 spin_lock_irq(&client->lock);
1088 generation = client->device->generation;
1089 todo = r->todo;
1090 /* Allow 1000ms grace period for other reallocations. */
1091 if (todo == ISO_RES_ALLOC &&
1092 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001093 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001094 skip = true;
1095 } else {
1096 /* We could be called twice within the same generation. */
1097 skip = todo == ISO_RES_REALLOC &&
1098 r->generation == generation;
1099 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001100 free = todo == ISO_RES_DEALLOC ||
1101 todo == ISO_RES_ALLOC_ONCE ||
1102 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001103 r->generation = generation;
1104 spin_unlock_irq(&client->lock);
1105
1106 if (skip)
1107 goto out;
1108
1109 bandwidth = r->bandwidth;
1110
1111 fw_iso_resource_manage(client->device->card, generation,
1112 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001113 todo == ISO_RES_ALLOC ||
1114 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001115 todo == ISO_RES_ALLOC_ONCE,
1116 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001117 /*
1118 * Is this generation outdated already? As long as this resource sticks
1119 * in the idr, it will be scheduled again for a newer generation or at
1120 * shutdown.
1121 */
1122 if (channel == -EAGAIN &&
1123 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1124 goto out;
1125
1126 success = channel >= 0 || bandwidth > 0;
1127
1128 spin_lock_irq(&client->lock);
1129 /*
1130 * Transit from allocation to reallocation, except if the client
1131 * requested deallocation in the meantime.
1132 */
1133 if (r->todo == ISO_RES_ALLOC)
1134 r->todo = ISO_RES_REALLOC;
1135 /*
1136 * Allocation or reallocation failure? Pull this resource out of the
1137 * idr and prepare for deletion, unless the client is shutting down.
1138 */
1139 if (r->todo == ISO_RES_REALLOC && !success &&
1140 !client->in_shutdown &&
1141 idr_find(&client->resource_idr, r->resource.handle)) {
1142 idr_remove(&client->resource_idr, r->resource.handle);
1143 client_put(client);
1144 free = true;
1145 }
1146 spin_unlock_irq(&client->lock);
1147
1148 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d2009-01-08 23:07:40 +01001149 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001150
1151 if (todo == ISO_RES_REALLOC && success)
1152 goto out;
1153
Stefan Richter1ec3c022009-01-04 16:23:29 +01001154 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001155 e = r->e_alloc;
1156 r->e_alloc = NULL;
1157 } else {
1158 e = r->e_dealloc;
1159 r->e_dealloc = NULL;
1160 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001161 e->iso_resource.handle = r->resource.handle;
1162 e->iso_resource.channel = channel;
1163 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001164
1165 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001166 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001167
1168 if (free) {
1169 cancel_delayed_work(&r->work);
1170 kfree(r->e_alloc);
1171 kfree(r->e_dealloc);
1172 kfree(r);
1173 }
1174 out:
1175 client_put(client);
1176}
1177
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001178static void release_iso_resource(struct client *client,
1179 struct client_resource *resource)
1180{
1181 struct iso_resource *r =
1182 container_of(resource, struct iso_resource, resource);
1183
1184 spin_lock_irq(&client->lock);
1185 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001186 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001187 spin_unlock_irq(&client->lock);
1188}
1189
Stefan Richter1ec3c022009-01-04 16:23:29 +01001190static int init_iso_resource(struct client *client,
1191 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001192{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001193 struct iso_resource_event *e1, *e2;
1194 struct iso_resource *r;
1195 int ret;
1196
1197 if ((request->channels == 0 && request->bandwidth == 0) ||
1198 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1199 request->bandwidth < 0)
1200 return -EINVAL;
1201
1202 r = kmalloc(sizeof(*r), GFP_KERNEL);
1203 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1204 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1205 if (r == NULL || e1 == NULL || e2 == NULL) {
1206 ret = -ENOMEM;
1207 goto fail;
1208 }
1209
1210 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1211 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001212 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001213 r->generation = -1;
1214 r->channels = request->channels;
1215 r->bandwidth = request->bandwidth;
1216 r->e_alloc = e1;
1217 r->e_dealloc = e2;
1218
Stefan Richtere21fcf72009-10-08 00:41:38 +02001219 e1->iso_resource.closure = request->closure;
1220 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1221 e2->iso_resource.closure = request->closure;
1222 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001223
Stefan Richter1ec3c022009-01-04 16:23:29 +01001224 if (todo == ISO_RES_ALLOC) {
1225 r->resource.release = release_iso_resource;
1226 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001227 if (ret < 0)
1228 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001229 } else {
1230 r->resource.release = NULL;
1231 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001232 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001233 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001234 request->handle = r->resource.handle;
1235
1236 return 0;
1237 fail:
1238 kfree(r);
1239 kfree(e1);
1240 kfree(e2);
1241
1242 return ret;
1243}
1244
Stefan Richter1ec3c022009-01-04 16:23:29 +01001245static int ioctl_allocate_iso_resource(struct client *client, void *buffer)
1246{
1247 struct fw_cdev_allocate_iso_resource *request = buffer;
1248
1249 return init_iso_resource(client, request, ISO_RES_ALLOC);
1250}
1251
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001252static int ioctl_deallocate_iso_resource(struct client *client, void *buffer)
1253{
1254 struct fw_cdev_deallocate *request = buffer;
1255
1256 return release_client_resource(client, request->handle,
1257 release_iso_resource, NULL);
1258}
1259
Stefan Richter1ec3c022009-01-04 16:23:29 +01001260static int ioctl_allocate_iso_resource_once(struct client *client, void *buffer)
1261{
1262 struct fw_cdev_allocate_iso_resource *request = buffer;
1263
1264 return init_iso_resource(client, request, ISO_RES_ALLOC_ONCE);
1265}
1266
1267static int ioctl_deallocate_iso_resource_once(struct client *client, void *buffer)
1268{
1269 struct fw_cdev_allocate_iso_resource *request = buffer;
1270
1271 return init_iso_resource(client, request, ISO_RES_DEALLOC_ONCE);
1272}
1273
Stefan Richterc8a25902009-03-10 20:59:16 +01001274/*
1275 * Returns a speed code: Maximum speed to or from this device,
1276 * limited by the device's link speed, the local node's link speed,
1277 * and all PHY port speeds between the two links.
1278 */
Stefan Richter33580a32009-01-04 16:23:29 +01001279static int ioctl_get_speed(struct client *client, void *buffer)
1280{
Stefan Richterc8a25902009-03-10 20:59:16 +01001281 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001282}
1283
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001284static int ioctl_send_broadcast_request(struct client *client, void *buffer)
1285{
1286 struct fw_cdev_send_request *request = buffer;
1287
1288 switch (request->tcode) {
1289 case TCODE_WRITE_QUADLET_REQUEST:
1290 case TCODE_WRITE_BLOCK_REQUEST:
1291 break;
1292 default:
1293 return -EINVAL;
1294 }
1295
Stefan Richter1566f3d2009-01-04 16:23:29 +01001296 /* Security policy: Only allow accesses to Units Space. */
1297 if (request->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1298 return -EACCES;
1299
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001300 return init_request(client, request, LOCAL_BUS | 0x3f, SCODE_100);
1301}
1302
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001303static int ioctl_send_stream_packet(struct client *client, void *buffer)
1304{
Stefan Richter18e9b102009-03-10 21:02:21 +01001305 struct fw_cdev_send_stream_packet *p = buffer;
1306 struct fw_cdev_send_request request;
1307 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001308
Stefan Richter18e9b102009-03-10 21:02:21 +01001309 if (p->speed > client->device->card->link_speed ||
1310 p->length > 1024 << p->speed)
1311 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001312
Stefan Richter18e9b102009-03-10 21:02:21 +01001313 if (p->tag > 3 || p->channel > 63 || p->sy > 15)
1314 return -EINVAL;
1315
1316 dest = fw_stream_packet_destination_id(p->tag, p->channel, p->sy);
1317 request.tcode = TCODE_STREAM_DATA;
1318 request.length = p->length;
1319 request.closure = p->closure;
1320 request.data = p->data;
1321 request.generation = p->generation;
1322
1323 return init_request(client, &request, dest, p->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001324}
1325
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001326static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
1327 ioctl_get_info,
1328 ioctl_send_request,
1329 ioctl_allocate,
1330 ioctl_deallocate,
1331 ioctl_send_response,
1332 ioctl_initiate_bus_reset,
1333 ioctl_add_descriptor,
1334 ioctl_remove_descriptor,
1335 ioctl_create_iso_context,
1336 ioctl_queue_iso,
1337 ioctl_start_iso,
1338 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001339 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001340 ioctl_allocate_iso_resource,
1341 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001342 ioctl_allocate_iso_resource_once,
1343 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001344 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001345 ioctl_send_broadcast_request,
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001346 ioctl_send_stream_packet,
Stefan Richterabfe5a02010-02-20 12:13:49 +01001347 ioctl_get_cycle_timer2,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001348};
1349
Stefan Richter53dca512008-12-14 21:47:04 +01001350static int dispatch_ioctl(struct client *client,
1351 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001352{
Stefan Richterb2c0a2a2009-10-15 21:16:53 +02001353 char buffer[sizeof(union {
1354 struct fw_cdev_get_info _00;
1355 struct fw_cdev_send_request _01;
1356 struct fw_cdev_allocate _02;
1357 struct fw_cdev_deallocate _03;
1358 struct fw_cdev_send_response _04;
1359 struct fw_cdev_initiate_bus_reset _05;
1360 struct fw_cdev_add_descriptor _06;
1361 struct fw_cdev_remove_descriptor _07;
1362 struct fw_cdev_create_iso_context _08;
1363 struct fw_cdev_queue_iso _09;
1364 struct fw_cdev_start_iso _0a;
1365 struct fw_cdev_stop_iso _0b;
1366 struct fw_cdev_get_cycle_timer _0c;
1367 struct fw_cdev_allocate_iso_resource _0d;
1368 struct fw_cdev_send_stream_packet _13;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001369 struct fw_cdev_get_cycle_timer2 _14;
Stefan Richterb2c0a2a2009-10-15 21:16:53 +02001370 })];
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001371 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001372
1373 if (_IOC_TYPE(cmd) != '#' ||
1374 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001375 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001376
1377 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001378 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001379 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
1380 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001381 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001382
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001383 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
1384 if (ret < 0)
1385 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001386
1387 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -04001388 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001389 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
1390 return -EFAULT;
1391 }
1392
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001393 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001394}
1395
Stefan Richter53dca512008-12-14 21:47:04 +01001396static long fw_device_op_ioctl(struct file *file,
1397 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001398{
1399 struct client *client = file->private_data;
1400
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001401 if (fw_device_is_shutdown(client->device))
1402 return -ENODEV;
1403
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001404 return dispatch_ioctl(client, cmd, (void __user *) arg);
1405}
1406
1407#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001408static long fw_device_op_compat_ioctl(struct file *file,
1409 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001410{
1411 struct client *client = file->private_data;
1412
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001413 if (fw_device_is_shutdown(client->device))
1414 return -ENODEV;
1415
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001416 return dispatch_ioctl(client, cmd, compat_ptr(arg));
1417}
1418#endif
1419
1420static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1421{
1422 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001423 enum dma_data_direction direction;
1424 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001425 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001426
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001427 if (fw_device_is_shutdown(client->device))
1428 return -ENODEV;
1429
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001430 /* FIXME: We could support multiple buffers, but we don't. */
1431 if (client->buffer.pages != NULL)
1432 return -EBUSY;
1433
1434 if (!(vma->vm_flags & VM_SHARED))
1435 return -EINVAL;
1436
1437 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001438 return -EINVAL;
1439
1440 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001441 size = vma->vm_end - vma->vm_start;
1442 page_count = size >> PAGE_SHIFT;
1443 if (size & ~PAGE_MASK)
1444 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001445
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001446 if (vma->vm_flags & VM_WRITE)
1447 direction = DMA_TO_DEVICE;
1448 else
1449 direction = DMA_FROM_DEVICE;
1450
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001451 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1452 page_count, direction);
1453 if (ret < 0)
1454 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001455
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001456 ret = fw_iso_buffer_map(&client->buffer, vma);
1457 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001458 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1459
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001460 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001461}
1462
Jay Fenlason45ee3192008-12-21 16:47:17 +01001463static int shutdown_resource(int id, void *p, void *data)
1464{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001465 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001466 struct client *client = data;
1467
Stefan Richtere21fcf72009-10-08 00:41:38 +02001468 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001469 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001470
1471 return 0;
1472}
1473
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001474static int fw_device_op_release(struct inode *inode, struct file *file)
1475{
1476 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001477 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001478
Stefan Richter97811e32008-12-14 19:19:23 +01001479 mutex_lock(&client->device->client_list_mutex);
1480 list_del(&client->link);
1481 mutex_unlock(&client->device->client_list_mutex);
1482
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001483 if (client->iso_context)
1484 fw_iso_context_destroy(client->iso_context);
1485
Stefan Richter36a755c2009-01-05 20:28:10 +01001486 if (client->buffer.pages)
1487 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1488
Jay Fenlason45ee3192008-12-21 16:47:17 +01001489 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001490 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001491 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001492 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001493
Jay Fenlason45ee3192008-12-21 16:47:17 +01001494 idr_for_each(&client->resource_idr, shutdown_resource, client);
1495 idr_remove_all(&client->resource_idr);
1496 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001497
Stefan Richtere21fcf72009-10-08 00:41:38 +02001498 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1499 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001500
Stefan Richterfb443032009-01-04 16:23:29 +01001501 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001502
1503 return 0;
1504}
1505
1506static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1507{
1508 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001509 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001510
1511 poll_wait(file, &client->wait, pt);
1512
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001513 if (fw_device_is_shutdown(client->device))
1514 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001515 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001516 mask |= POLLIN | POLLRDNORM;
1517
1518 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001519}
1520
Stefan Richter21ebcd12007-01-14 15:29:07 +01001521const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001522 .owner = THIS_MODULE,
1523 .open = fw_device_op_open,
1524 .read = fw_device_op_read,
1525 .unlocked_ioctl = fw_device_op_ioctl,
1526 .poll = fw_device_op_poll,
1527 .release = fw_device_op_release,
1528 .mmap = fw_device_op_mmap,
1529
1530#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001531 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001532#endif
1533};