blob: f7559bfeaba3c86a0b72c184012a428fc46d2319 [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 Fenlasoncf417e542008-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
Stefan Richter604f4512010-06-20 22:52:55 +020049/*
50 * ABI version history is documented in linux/firewire-cdev.h.
51 */
Stefan Richtere2055972010-06-20 22:53:55 +020052#define FW_CDEV_KERNEL_VERSION 4
53#define FW_CDEV_VERSION_EVENT_REQUEST2 4
Stefan Richter604f4512010-06-20 22:52:55 +020054
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050055struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050056 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010058
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050059 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010060 bool in_shutdown;
61 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050062 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050063 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040064 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050065
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050066 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040067 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050068 struct fw_iso_buffer buffer;
69 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050070
71 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010072 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050073};
74
Stefan Richterfb443032009-01-04 16:23:29 +010075static inline void client_get(struct client *client)
76{
77 kref_get(&client->kref);
78}
79
80static void client_release(struct kref *kref)
81{
82 struct client *client = container_of(kref, struct client, kref);
83
84 fw_device_put(client->device);
85 kfree(client);
86}
87
88static void client_put(struct client *client)
89{
90 kref_put(&client->kref, client_release);
91}
92
Stefan Richter97c18b72009-01-04 16:23:29 +010093struct client_resource;
94typedef void (*client_resource_release_fn_t)(struct client *,
95 struct client_resource *);
96struct client_resource {
97 client_resource_release_fn_t release;
98 int handle;
99};
100
101struct address_handler_resource {
102 struct client_resource resource;
103 struct fw_address_handler handler;
104 __u64 closure;
105 struct client *client;
106};
107
108struct outbound_transaction_resource {
109 struct client_resource resource;
110 struct fw_transaction transaction;
111};
112
113struct inbound_transaction_resource {
114 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400115 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100116 struct fw_request *request;
117 void *data;
118 size_t length;
119};
120
121struct descriptor_resource {
122 struct client_resource resource;
123 struct fw_descriptor descriptor;
124 u32 data[0];
125};
126
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100127struct iso_resource {
128 struct client_resource resource;
129 struct client *client;
130 /* Schedule work and access todo only with client->lock held. */
131 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100132 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
133 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100134 int generation;
135 u64 channels;
136 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200137 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100138 struct iso_resource_event *e_alloc, *e_dealloc;
139};
140
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100141static void release_iso_resource(struct client *, struct client_resource *);
142
Stefan Richter9fb551b2009-10-08 00:41:10 +0200143static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
144{
145 client_get(r->client);
146 if (!schedule_delayed_work(&r->work, delay))
147 client_put(r->client);
148}
149
150static void schedule_if_iso_resource(struct client_resource *resource)
151{
152 if (resource->release == release_iso_resource)
153 schedule_iso_resource(container_of(resource,
154 struct iso_resource, resource), 0);
155}
156
Stefan Richter97c18b72009-01-04 16:23:29 +0100157/*
158 * dequeue_event() just kfree()'s the event, so the event has to be
159 * the first field in a struct XYZ_event.
160 */
161struct event {
162 struct { void *data; size_t size; } v[2];
163 struct list_head link;
164};
165
166struct bus_reset_event {
167 struct event event;
168 struct fw_cdev_event_bus_reset reset;
169};
170
171struct outbound_transaction_event {
172 struct event event;
173 struct client *client;
174 struct outbound_transaction_resource r;
175 struct fw_cdev_event_response response;
176};
177
178struct inbound_transaction_event {
179 struct event event;
Stefan Richtere2055972010-06-20 22:53:55 +0200180 union {
181 struct fw_cdev_event_request request;
182 struct fw_cdev_event_request2 request2;
183 } req;
Stefan Richter97c18b72009-01-04 16:23:29 +0100184};
185
186struct iso_interrupt_event {
187 struct event event;
188 struct fw_cdev_event_iso_interrupt interrupt;
189};
190
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100191struct iso_resource_event {
192 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200193 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100194};
195
Stefan Richter53dca512008-12-14 21:47:04 +0100196static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500197{
198 return (void __user *)(unsigned long)value;
199}
200
Stefan Richter53dca512008-12-14 21:47:04 +0100201static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500202{
203 return (__u64)(unsigned long)ptr;
204}
205
206static int fw_device_op_open(struct inode *inode, struct file *file)
207{
208 struct fw_device *device;
209 struct client *client;
210
Stefan Richter96b19062008-02-02 15:01:09 +0100211 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500212 if (device == NULL)
213 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500214
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400215 if (fw_device_is_shutdown(device)) {
216 fw_device_put(device);
217 return -ENODEV;
218 }
219
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400220 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100221 if (client == NULL) {
222 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500223 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100224 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500225
Stefan Richter96b19062008-02-02 15:01:09 +0100226 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500227 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100228 idr_init(&client->resource_idr);
229 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500230 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100231 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500232
233 file->private_data = client;
234
Stefan Richterd67cfb92008-10-05 10:37:11 +0200235 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500236 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200237 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500238
Stefan Richter3ac26b22010-04-10 16:38:05 +0100239 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500240}
241
242static void queue_event(struct client *client, struct event *event,
243 void *data0, size_t size0, void *data1, size_t size1)
244{
245 unsigned long flags;
246
247 event->v[0].data = data0;
248 event->v[0].size = size0;
249 event->v[1].data = data1;
250 event->v[1].size = size1;
251
252 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100253 if (client->in_shutdown)
254 kfree(event);
255 else
256 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500257 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400258
259 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500260}
261
Stefan Richter53dca512008-12-14 21:47:04 +0100262static int dequeue_event(struct client *client,
263 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500264{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500265 struct event *event;
266 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100267 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500268
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100269 ret = wait_event_interruptible(client->wait,
270 !list_empty(&client->event_list) ||
271 fw_device_is_shutdown(client->device));
272 if (ret < 0)
273 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500274
275 if (list_empty(&client->event_list) &&
276 fw_device_is_shutdown(client->device))
277 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500278
Stefan Richter3ba94982009-01-04 16:23:29 +0100279 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100280 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500281 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100282 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500283
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500284 total = 0;
285 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
286 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500287 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100288 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500289 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500290 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500291 total += size;
292 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100293 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500294
295 out:
296 kfree(event);
297
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100298 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500299}
300
Stefan Richter53dca512008-12-14 21:47:04 +0100301static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
302 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500303{
304 struct client *client = file->private_data;
305
306 return dequeue_event(client, buffer, count);
307}
308
Stefan Richter53dca512008-12-14 21:47:04 +0100309static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
310 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500311{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400312 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400313
Stefan Richter3ba94982009-01-04 16:23:29 +0100314 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500315
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400316 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500317 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100318 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400319 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500320 event->local_node_id = card->local_node->node_id;
Stefan Richter250b2b62010-06-21 23:24:35 +0200321 event->bm_node_id = card->bm_node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500322 event->irm_node_id = card->irm_node->node_id;
323 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400324
Stefan Richter3ba94982009-01-04 16:23:29 +0100325 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500326}
327
Stefan Richter53dca512008-12-14 21:47:04 +0100328static void for_each_client(struct fw_device *device,
329 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500330{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500331 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500332
Stefan Richterd67cfb92008-10-05 10:37:11 +0200333 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500334 list_for_each_entry(c, &device->client_list, link)
335 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200336 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500337}
338
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100339static int schedule_reallocations(int id, void *p, void *data)
340{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200341 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100342
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100343 return 0;
344}
345
Stefan Richter53dca512008-12-14 21:47:04 +0100346static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500347{
Stefan Richter97c18b72009-01-04 16:23:29 +0100348 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500349
Stefan Richter97c18b72009-01-04 16:23:29 +0100350 e = kzalloc(sizeof(*e), GFP_KERNEL);
351 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500352 fw_notify("Out of memory when allocating bus reset event\n");
353 return;
354 }
355
Stefan Richter97c18b72009-01-04 16:23:29 +0100356 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500357
Stefan Richter97c18b72009-01-04 16:23:29 +0100358 queue_event(client, &e->event,
359 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100360
361 spin_lock_irq(&client->lock);
362 idr_for_each(&client->resource_idr, schedule_reallocations, client);
363 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500364}
365
366void fw_device_cdev_update(struct fw_device *device)
367{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500368 for_each_client(device, queue_bus_reset_event);
369}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500370
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500371static void wake_up_client(struct client *client)
372{
373 wake_up_interruptible(&client->wait);
374}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500375
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500376void fw_device_cdev_remove(struct fw_device *device)
377{
378 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500379}
380
Stefan Richter6e95dea2010-02-21 17:56:21 +0100381union ioctl_arg {
382 struct fw_cdev_get_info get_info;
383 struct fw_cdev_send_request send_request;
384 struct fw_cdev_allocate allocate;
385 struct fw_cdev_deallocate deallocate;
386 struct fw_cdev_send_response send_response;
387 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
388 struct fw_cdev_add_descriptor add_descriptor;
389 struct fw_cdev_remove_descriptor remove_descriptor;
390 struct fw_cdev_create_iso_context create_iso_context;
391 struct fw_cdev_queue_iso queue_iso;
392 struct fw_cdev_start_iso start_iso;
393 struct fw_cdev_stop_iso stop_iso;
394 struct fw_cdev_get_cycle_timer get_cycle_timer;
395 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
396 struct fw_cdev_send_stream_packet send_stream_packet;
397 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
398};
399
400static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500401{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100402 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500403 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100404 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500405
Stefan Richter6e95dea2010-02-21 17:56:21 +0100406 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200407 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100408 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500409
Stefan Richterc9755e12008-03-24 20:54:28 +0100410 down_read(&fw_device_rwsem);
411
Stefan Richter6e95dea2010-02-21 17:56:21 +0100412 if (a->rom != 0) {
413 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100414 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500415
Stefan Richter6e95dea2010-02-21 17:56:21 +0100416 ret = copy_to_user(u64_to_uptr(a->rom),
417 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500418 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100419 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500420
Stefan Richterc9755e12008-03-24 20:54:28 +0100421 up_read(&fw_device_rwsem);
422
423 if (ret != 0)
424 return -EFAULT;
425
Stefan Richter6e95dea2010-02-21 17:56:21 +0100426 client->bus_reset_closure = a->bus_reset_closure;
427 if (a->bus_reset != 0) {
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400428 fill_bus_reset_event(&bus_reset, client);
Stefan Richter6e95dea2010-02-21 17:56:21 +0100429 if (copy_to_user(u64_to_uptr(a->bus_reset),
430 &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500431 return -EFAULT;
432 }
433
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500434 return 0;
435}
436
Stefan Richter53dca512008-12-14 21:47:04 +0100437static int add_client_resource(struct client *client,
438 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400439{
440 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100441 int ret;
442
443 retry:
444 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
445 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400446
447 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100448 if (client->in_shutdown)
449 ret = -ECANCELED;
450 else
451 ret = idr_get_new(&client->resource_idr, resource,
452 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100453 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100454 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200455 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100456 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400457 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100458
459 if (ret == -EAGAIN)
460 goto retry;
461
462 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400463}
464
Stefan Richter53dca512008-12-14 21:47:04 +0100465static int release_client_resource(struct client *client, u32 handle,
466 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200467 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400468{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200469 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400470
Stefan Richter3ba94982009-01-04 16:23:29 +0100471 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100472 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200473 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100474 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200475 resource = idr_find(&client->resource_idr, handle);
476 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100477 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100478 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400479
Stefan Richtere21fcf72009-10-08 00:41:38 +0200480 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400481 return -EINVAL;
482
Stefan Richtere21fcf72009-10-08 00:41:38 +0200483 if (return_resource)
484 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400485 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200486 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400487
Stefan Richterfb443032009-01-04 16:23:29 +0100488 client_put(client);
489
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400490 return 0;
491}
492
Stefan Richter53dca512008-12-14 21:47:04 +0100493static void release_transaction(struct client *client,
494 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400495{
Stefan Richter97c18b72009-01-04 16:23:29 +0100496 struct outbound_transaction_resource *r = container_of(resource,
497 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400498
Stefan Richter97c18b72009-01-04 16:23:29 +0100499 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400500}
501
Stefan Richter53dca512008-12-14 21:47:04 +0100502static void complete_transaction(struct fw_card *card, int rcode,
503 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500504{
Stefan Richter97c18b72009-01-04 16:23:29 +0100505 struct outbound_transaction_event *e = data;
506 struct fw_cdev_event_response *rsp = &e->response;
507 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500508 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500509
Stefan Richter97c18b72009-01-04 16:23:29 +0100510 if (length < rsp->length)
511 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500512 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100513 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500514
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500515 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100516 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100517 * 1. If called while in shutdown, the idr tree must be left untouched.
518 * The idr handle will be removed and the client reference will be
519 * dropped later.
520 * 2. If the call chain was release_client_resource ->
521 * release_transaction -> complete_transaction (instead of a normal
522 * conclusion of the transaction), i.e. if this resource was already
523 * unregistered from the idr, the client reference will be dropped
524 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100525 */
Stefan Richterfb443032009-01-04 16:23:29 +0100526 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100527 idr_find(&client->resource_idr, e->r.resource.handle)) {
528 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100529 /* Drop the idr's reference */
530 client_put(client);
531 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500532 spin_unlock_irqrestore(&client->lock, flags);
533
Stefan Richter97c18b72009-01-04 16:23:29 +0100534 rsp->type = FW_CDEV_EVENT_RESPONSE;
535 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700536
537 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100538 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700539 * data, and the read is short, preserve an extra copy of the data
540 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
541 * for short reads and some apps depended on it, this is both safe
542 * and prudent for compatibility.
543 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100544 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
545 queue_event(client, &e->event, rsp, sizeof(*rsp),
546 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700547 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100548 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700549 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100550
551 /* Drop the transaction callback's reference */
552 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500553}
554
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100555static int init_request(struct client *client,
556 struct fw_cdev_send_request *request,
557 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500558{
Stefan Richter97c18b72009-01-04 16:23:29 +0100559 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100560 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561
Stefan Richter18e9b10f2009-03-10 21:02:21 +0100562 if (request->tcode != TCODE_STREAM_DATA &&
563 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100564 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500565
Clemens Ladischa8e93f32010-07-07 14:37:30 +0200566 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
567 request->length < 4)
568 return -EINVAL;
569
Stefan Richter97c18b72009-01-04 16:23:29 +0100570 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
571 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500572 return -ENOMEM;
573
Stefan Richter97c18b72009-01-04 16:23:29 +0100574 e->client = client;
575 e->response.length = request->length;
576 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500577
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400578 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100579 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400580 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100581 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100582 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100583 }
584
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100585 e->r.resource.release = release_transaction;
586 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
587 if (ret < 0)
588 goto failed;
589
590 /* Get a reference for the transaction callback */
591 client_get(client);
592
593 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100594 request->tcode, destination_id, request->generation,
595 speed, request->offset, e->response.data,
596 request->length, complete_transaction, e);
597 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100598
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100599 failed:
600 kfree(e);
601
602 return ret;
603}
604
Stefan Richter6e95dea2010-02-21 17:56:21 +0100605static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100606{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100607 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100608 case TCODE_WRITE_QUADLET_REQUEST:
609 case TCODE_WRITE_BLOCK_REQUEST:
610 case TCODE_READ_QUADLET_REQUEST:
611 case TCODE_READ_BLOCK_REQUEST:
612 case TCODE_LOCK_MASK_SWAP:
613 case TCODE_LOCK_COMPARE_SWAP:
614 case TCODE_LOCK_FETCH_ADD:
615 case TCODE_LOCK_LITTLE_ADD:
616 case TCODE_LOCK_BOUNDED_ADD:
617 case TCODE_LOCK_WRAP_ADD:
618 case TCODE_LOCK_VENDOR_DEPENDENT:
619 break;
620 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100621 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622 }
623
Stefan Richter6e95dea2010-02-21 17:56:21 +0100624 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100625 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500626}
627
Stefan Richter281e2032010-01-24 16:45:03 +0100628static inline bool is_fcp_request(struct fw_request *request)
629{
630 return request == NULL;
631}
632
Stefan Richter53dca512008-12-14 21:47:04 +0100633static void release_request(struct client *client,
634 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400635{
Stefan Richter97c18b72009-01-04 16:23:29 +0100636 struct inbound_transaction_resource *r = container_of(resource,
637 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400638
Stefan Richter281e2032010-01-24 16:45:03 +0100639 if (is_fcp_request(r->request))
640 kfree(r->data);
641 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400642 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200643
644 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100645 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400646}
647
Stefan Richter97c18b72009-01-04 16:23:29 +0100648static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100649 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200650 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100651 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500652{
Stefan Richter97c18b72009-01-04 16:23:29 +0100653 struct address_handler_resource *handler = callback_data;
654 struct inbound_transaction_resource *r;
655 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200656 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100657 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100658 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500659
Stefan Richter0244f572010-06-20 22:52:27 +0200660 /* card may be different from handler->client->device->card */
661 fw_card_get(card);
662
Stefan Richter97c18b72009-01-04 16:23:29 +0100663 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400664 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100665 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100666 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500667
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400668 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100669 r->request = request;
670 r->data = payload;
671 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500672
Stefan Richter281e2032010-01-24 16:45:03 +0100673 if (is_fcp_request(request)) {
674 /*
675 * FIXME: Let core-transaction.c manage a
676 * single reference-counted copy?
677 */
678 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
679 if (fcp_frame == NULL)
680 goto failed;
681
682 r->data = fcp_frame;
683 }
684
Stefan Richter97c18b72009-01-04 16:23:29 +0100685 r->resource.release = release_request;
686 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100687 if (ret < 0)
688 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500689
Stefan Richtere2055972010-06-20 22:53:55 +0200690 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
691 struct fw_cdev_event_request *req = &e->req.request;
692
693 if (tcode & 0x10)
694 tcode = TCODE_LOCK_REQUEST;
695
696 req->type = FW_CDEV_EVENT_REQUEST;
697 req->tcode = tcode;
698 req->offset = offset;
699 req->length = length;
700 req->handle = r->resource.handle;
701 req->closure = handler->closure;
702 event_size0 = sizeof(*req);
703 } else {
704 struct fw_cdev_event_request2 *req = &e->req.request2;
705
706 req->type = FW_CDEV_EVENT_REQUEST2;
707 req->tcode = tcode;
708 req->offset = offset;
709 req->source_node_id = source;
710 req->destination_node_id = destination;
711 req->card = card->index;
712 req->generation = generation;
713 req->length = length;
714 req->handle = r->resource.handle;
715 req->closure = handler->closure;
716 event_size0 = sizeof(*req);
717 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500718
Stefan Richter97c18b72009-01-04 16:23:29 +0100719 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200720 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100721 return;
722
723 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100724 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100725 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100726 kfree(fcp_frame);
727
728 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100729 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200730
731 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500732}
733
Stefan Richter53dca512008-12-14 21:47:04 +0100734static void release_address_handler(struct client *client,
735 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400736{
Stefan Richter97c18b72009-01-04 16:23:29 +0100737 struct address_handler_resource *r =
738 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400739
Stefan Richter97c18b72009-01-04 16:23:29 +0100740 fw_core_remove_address_handler(&r->handler);
741 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400742}
743
Stefan Richter6e95dea2010-02-21 17:56:21 +0100744static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500745{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100746 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100747 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500748 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100749 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500750
Stefan Richter97c18b72009-01-04 16:23:29 +0100751 r = kmalloc(sizeof(*r), GFP_KERNEL);
752 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500753 return -ENOMEM;
754
Stefan Richter6e95dea2010-02-21 17:56:21 +0100755 region.start = a->offset;
756 region.end = a->offset + a->length;
757 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100758 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100759 r->handler.callback_data = r;
760 r->closure = a->closure;
761 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500762
Stefan Richter97c18b72009-01-04 16:23:29 +0100763 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100764 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100765 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100766 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500767 }
768
Stefan Richter97c18b72009-01-04 16:23:29 +0100769 r->resource.release = release_address_handler;
770 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100771 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100772 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100773 return ret;
774 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100775 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500776
777 return 0;
778}
779
Stefan Richter6e95dea2010-02-21 17:56:21 +0100780static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400781{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100782 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100783 release_address_handler, NULL);
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400784}
785
Stefan Richter6e95dea2010-02-21 17:56:21 +0100786static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500787{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100788 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400789 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100790 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200791 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500792
Stefan Richter6e95dea2010-02-21 17:56:21 +0100793 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100794 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500795 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100796
Stefan Richter97c18b72009-01-04 16:23:29 +0100797 r = container_of(resource, struct inbound_transaction_resource,
798 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100799 if (is_fcp_request(r->request))
800 goto out;
801
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200802 if (a->length != fw_get_response_length(r->request)) {
803 ret = -EINVAL;
804 kfree(r->request);
805 goto out;
806 }
807 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100808 ret = -EFAULT;
809 kfree(r->request);
810 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200811 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400812 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200813 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200814 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500815 kfree(r);
816
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200817 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500818}
819
Stefan Richter6e95dea2010-02-21 17:56:21 +0100820static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500821{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100822 return fw_core_initiate_bus_reset(client->device->card,
823 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500824}
825
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400826static void release_descriptor(struct client *client,
827 struct client_resource *resource)
828{
Stefan Richter97c18b72009-01-04 16:23:29 +0100829 struct descriptor_resource *r =
830 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400831
Stefan Richter97c18b72009-01-04 16:23:29 +0100832 fw_core_remove_descriptor(&r->descriptor);
833 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400834}
835
Stefan Richter6e95dea2010-02-21 17:56:21 +0100836static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200837{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100838 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100839 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100840 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200841
Stefan Richterde487da2009-03-10 21:00:23 +0100842 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200843 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100844 return -ENOSYS;
845
Stefan Richter6e95dea2010-02-21 17:56:21 +0100846 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200847 return -EINVAL;
848
Stefan Richter6e95dea2010-02-21 17:56:21 +0100849 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100850 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200851 return -ENOMEM;
852
Stefan Richter6e95dea2010-02-21 17:56:21 +0100853 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100854 ret = -EFAULT;
855 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200856 }
857
Stefan Richter6e95dea2010-02-21 17:56:21 +0100858 r->descriptor.length = a->length;
859 r->descriptor.immediate = a->immediate;
860 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100861 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200862
Stefan Richter97c18b72009-01-04 16:23:29 +0100863 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100864 if (ret < 0)
865 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200866
Stefan Richter97c18b72009-01-04 16:23:29 +0100867 r->resource.release = release_descriptor;
868 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100869 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100870 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100871 goto failed;
872 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100873 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200874
875 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100876 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100877 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100878
879 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200880}
881
Stefan Richter6e95dea2010-02-21 17:56:21 +0100882static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200883{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100884 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100885 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200886}
887
Stefan Richter53dca512008-12-14 21:47:04 +0100888static void iso_callback(struct fw_iso_context *context, u32 cycle,
889 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500890{
891 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100892 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500893
Stefan Richter56d04cb12010-06-08 00:20:10 +0200894 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100895 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500896 return;
897
Stefan Richter97c18b72009-01-04 16:23:29 +0100898 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
899 e->interrupt.closure = client->iso_closure;
900 e->interrupt.cycle = cycle;
901 e->interrupt.header_length = header_length;
902 memcpy(e->interrupt.header, header, header_length);
903 queue_event(client, &e->event, &e->interrupt,
904 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500905}
906
Stefan Richter6e95dea2010-02-21 17:56:21 +0100907static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500908{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100909 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400910 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500911
Stefan Richter6e95dea2010-02-21 17:56:21 +0100912 if (a->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500913 return -EINVAL;
914
Stefan Richter6e95dea2010-02-21 17:56:21 +0100915 switch (a->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400916 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100917 if (a->header_size < 4 || (a->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400918 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400919 break;
920
921 case FW_ISO_CONTEXT_TRANSMIT:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100922 if (a->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400923 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400924 break;
925
926 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500927 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400928 }
929
Stefan Richter6e95dea2010-02-21 17:56:21 +0100930 context = fw_iso_context_create(client->device->card, a->type,
931 a->channel, a->speed, a->header_size,
932 iso_callback, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400933 if (IS_ERR(context))
934 return PTR_ERR(context);
935
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200936 /* We only support one context at this time. */
937 spin_lock_irq(&client->lock);
938 if (client->iso_context != NULL) {
939 spin_unlock_irq(&client->lock);
940 fw_iso_context_destroy(context);
941 return -EBUSY;
942 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100943 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400944 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200945 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500946
Stefan Richter6e95dea2010-02-21 17:56:21 +0100947 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400948
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500949 return 0;
950}
951
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400952/* Macros for decoding the iso packet control header. */
953#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
954#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
955#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200956#define GET_TAG(v) (((v) >> 18) & 0x03)
957#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400958#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
959
Stefan Richter6e95dea2010-02-21 17:56:21 +0100960static int ioctl_queue_iso(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_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500963 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500964 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200965 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400966 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500967 int count;
968 struct {
969 struct fw_iso_packet packet;
970 u8 header[256];
971 } u;
972
Stefan Richter6e95dea2010-02-21 17:56:21 +0100973 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500974 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500975
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400976 /*
977 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500978 * the iso buffer, and the pointer points inside the buffer,
979 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500980 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500981 * payload_length == 0 through. In other words, if no packets
982 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +0100983 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400984 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500985
Stefan Richter6e95dea2010-02-21 17:56:21 +0100986 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200987 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100988 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200989 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500990 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200991 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500992 }
993
Stefan Richter6e95dea2010-02-21 17:56:21 +0100994 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Al Viro1ccc9142007-10-14 19:34:40 +0100995
Stefan Richter6e95dea2010-02-21 17:56:21 +0100996 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500997 return -EFAULT;
998
Stefan Richter6e95dea2010-02-21 17:56:21 +0100999 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001000 count = 0;
1001 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001002 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001003 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001004 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1005 u.packet.interrupt = GET_INTERRUPT(control);
1006 u.packet.skip = GET_SKIP(control);
1007 u.packet.tag = GET_TAG(control);
1008 u.packet.sy = GET_SY(control);
1009 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001010
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001011 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +02001012 if (u.packet.header_length % 4 != 0)
1013 return -EINVAL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001014 header_length = u.packet.header_length;
1015 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001016 /*
1017 * We require that header_length is a multiple of
1018 * the fixed header size, ctx->header_size.
1019 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001020 if (ctx->header_size == 0) {
1021 if (u.packet.header_length > 0)
1022 return -EINVAL;
Clemens Ladisch4ba1d9c2010-03-31 16:26:39 +02001023 } else if (u.packet.header_length == 0 ||
1024 u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001025 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001026 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001027 header_length = 0;
1028 }
1029
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001030 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001031 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001032 if (next > end)
1033 return -EINVAL;
1034 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001035 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001036 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001037 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001038 u.packet.header_length + u.packet.payload_length > 0)
1039 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001040 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001041 return -EINVAL;
1042
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001043 if (fw_iso_context_queue(ctx, &u.packet,
1044 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001045 break;
1046
1047 p = next;
1048 payload += u.packet.payload_length;
1049 count++;
1050 }
1051
Stefan Richter6e95dea2010-02-21 17:56:21 +01001052 a->size -= uptr_to_u64(p) - a->packets;
1053 a->packets = uptr_to_u64(p);
1054 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001055
1056 return count;
1057}
1058
Stefan Richter6e95dea2010-02-21 17:56:21 +01001059static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001060{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001061 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001062
Stefan Richter6e95dea2010-02-21 17:56:21 +01001063 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001064 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001065
Stefan Richter6e95dea2010-02-21 17:56:21 +01001066 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1067 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1068 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001069
Stefan Richter6e95dea2010-02-21 17:56:21 +01001070 return fw_iso_context_start(client->iso_context,
1071 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001072}
1073
Stefan Richter6e95dea2010-02-21 17:56:21 +01001074static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001075{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001076 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001077
Stefan Richter6e95dea2010-02-21 17:56:21 +01001078 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001079 return -EINVAL;
1080
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001081 return fw_iso_context_stop(client->iso_context);
1082}
1083
Stefan Richter6e95dea2010-02-21 17:56:21 +01001084static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001085{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001086 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001087 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001088 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001089 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001090 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001091
Stefan Richter4a9bde92010-02-20 22:24:43 +01001092 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001093
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001094 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001095
Stefan Richter6e95dea2010-02-21 17:56:21 +01001096 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001097 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1098 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1099 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1100 default:
1101 ret = -EINVAL;
1102 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001103
Stefan Richter4a9bde92010-02-20 22:24:43 +01001104 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001105
Stefan Richter6e95dea2010-02-21 17:56:21 +01001106 a->tv_sec = ts.tv_sec;
1107 a->tv_nsec = ts.tv_nsec;
1108 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001109
1110 return ret;
1111}
1112
Stefan Richter6e95dea2010-02-21 17:56:21 +01001113static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001114{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001115 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001116 struct fw_cdev_get_cycle_timer2 ct2;
1117
1118 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001119 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001120
Stefan Richter6e95dea2010-02-21 17:56:21 +01001121 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1122 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001123
Stefan Richtera64408b2007-09-29 10:41:58 +02001124 return 0;
1125}
1126
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001127static void iso_resource_work(struct work_struct *work)
1128{
1129 struct iso_resource_event *e;
1130 struct iso_resource *r =
1131 container_of(work, struct iso_resource, work.work);
1132 struct client *client = r->client;
1133 int generation, channel, bandwidth, todo;
1134 bool skip, free, success;
1135
1136 spin_lock_irq(&client->lock);
1137 generation = client->device->generation;
1138 todo = r->todo;
1139 /* Allow 1000ms grace period for other reallocations. */
1140 if (todo == ISO_RES_ALLOC &&
1141 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001142 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001143 skip = true;
1144 } else {
1145 /* We could be called twice within the same generation. */
1146 skip = todo == ISO_RES_REALLOC &&
1147 r->generation == generation;
1148 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001149 free = todo == ISO_RES_DEALLOC ||
1150 todo == ISO_RES_ALLOC_ONCE ||
1151 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001152 r->generation = generation;
1153 spin_unlock_irq(&client->lock);
1154
1155 if (skip)
1156 goto out;
1157
1158 bandwidth = r->bandwidth;
1159
1160 fw_iso_resource_manage(client->device->card, generation,
1161 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001162 todo == ISO_RES_ALLOC ||
1163 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001164 todo == ISO_RES_ALLOC_ONCE,
1165 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001166 /*
1167 * Is this generation outdated already? As long as this resource sticks
1168 * in the idr, it will be scheduled again for a newer generation or at
1169 * shutdown.
1170 */
1171 if (channel == -EAGAIN &&
1172 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1173 goto out;
1174
1175 success = channel >= 0 || bandwidth > 0;
1176
1177 spin_lock_irq(&client->lock);
1178 /*
1179 * Transit from allocation to reallocation, except if the client
1180 * requested deallocation in the meantime.
1181 */
1182 if (r->todo == ISO_RES_ALLOC)
1183 r->todo = ISO_RES_REALLOC;
1184 /*
1185 * Allocation or reallocation failure? Pull this resource out of the
1186 * idr and prepare for deletion, unless the client is shutting down.
1187 */
1188 if (r->todo == ISO_RES_REALLOC && !success &&
1189 !client->in_shutdown &&
1190 idr_find(&client->resource_idr, r->resource.handle)) {
1191 idr_remove(&client->resource_idr, r->resource.handle);
1192 client_put(client);
1193 free = true;
1194 }
1195 spin_unlock_irq(&client->lock);
1196
1197 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001198 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001199
1200 if (todo == ISO_RES_REALLOC && success)
1201 goto out;
1202
Stefan Richter1ec3c022009-01-04 16:23:29 +01001203 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001204 e = r->e_alloc;
1205 r->e_alloc = NULL;
1206 } else {
1207 e = r->e_dealloc;
1208 r->e_dealloc = NULL;
1209 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001210 e->iso_resource.handle = r->resource.handle;
1211 e->iso_resource.channel = channel;
1212 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001213
1214 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001215 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001216
1217 if (free) {
1218 cancel_delayed_work(&r->work);
1219 kfree(r->e_alloc);
1220 kfree(r->e_dealloc);
1221 kfree(r);
1222 }
1223 out:
1224 client_put(client);
1225}
1226
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001227static void release_iso_resource(struct client *client,
1228 struct client_resource *resource)
1229{
1230 struct iso_resource *r =
1231 container_of(resource, struct iso_resource, resource);
1232
1233 spin_lock_irq(&client->lock);
1234 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001235 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001236 spin_unlock_irq(&client->lock);
1237}
1238
Stefan Richter1ec3c022009-01-04 16:23:29 +01001239static int init_iso_resource(struct client *client,
1240 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001241{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001242 struct iso_resource_event *e1, *e2;
1243 struct iso_resource *r;
1244 int ret;
1245
1246 if ((request->channels == 0 && request->bandwidth == 0) ||
1247 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1248 request->bandwidth < 0)
1249 return -EINVAL;
1250
1251 r = kmalloc(sizeof(*r), GFP_KERNEL);
1252 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1253 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1254 if (r == NULL || e1 == NULL || e2 == NULL) {
1255 ret = -ENOMEM;
1256 goto fail;
1257 }
1258
1259 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1260 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001261 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001262 r->generation = -1;
1263 r->channels = request->channels;
1264 r->bandwidth = request->bandwidth;
1265 r->e_alloc = e1;
1266 r->e_dealloc = e2;
1267
Stefan Richtere21fcf72009-10-08 00:41:38 +02001268 e1->iso_resource.closure = request->closure;
1269 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1270 e2->iso_resource.closure = request->closure;
1271 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001272
Stefan Richter1ec3c022009-01-04 16:23:29 +01001273 if (todo == ISO_RES_ALLOC) {
1274 r->resource.release = release_iso_resource;
1275 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001276 if (ret < 0)
1277 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001278 } else {
1279 r->resource.release = NULL;
1280 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001281 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001282 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001283 request->handle = r->resource.handle;
1284
1285 return 0;
1286 fail:
1287 kfree(r);
1288 kfree(e1);
1289 kfree(e2);
1290
1291 return ret;
1292}
1293
Stefan Richter6e95dea2010-02-21 17:56:21 +01001294static int ioctl_allocate_iso_resource(struct client *client,
1295 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001296{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001297 return init_iso_resource(client,
1298 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001299}
1300
Stefan Richter6e95dea2010-02-21 17:56:21 +01001301static int ioctl_deallocate_iso_resource(struct client *client,
1302 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001303{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001304 return release_client_resource(client,
1305 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001306}
1307
Stefan Richter6e95dea2010-02-21 17:56:21 +01001308static int ioctl_allocate_iso_resource_once(struct client *client,
1309 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001310{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001311 return init_iso_resource(client,
1312 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001313}
1314
Stefan Richter6e95dea2010-02-21 17:56:21 +01001315static int ioctl_deallocate_iso_resource_once(struct client *client,
1316 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001317{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001318 return init_iso_resource(client,
1319 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001320}
1321
Stefan Richterc8a25902009-03-10 20:59:16 +01001322/*
1323 * Returns a speed code: Maximum speed to or from this device,
1324 * limited by the device's link speed, the local node's link speed,
1325 * and all PHY port speeds between the two links.
1326 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001327static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001328{
Stefan Richterc8a25902009-03-10 20:59:16 +01001329 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001330}
1331
Stefan Richter6e95dea2010-02-21 17:56:21 +01001332static int ioctl_send_broadcast_request(struct client *client,
1333 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001334{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001335 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001336
Stefan Richter6e95dea2010-02-21 17:56:21 +01001337 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001338 case TCODE_WRITE_QUADLET_REQUEST:
1339 case TCODE_WRITE_BLOCK_REQUEST:
1340 break;
1341 default:
1342 return -EINVAL;
1343 }
1344
Stefan Richter1566f3d2009-01-04 16:23:29 +01001345 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001346 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001347 return -EACCES;
1348
Stefan Richter6e95dea2010-02-21 17:56:21 +01001349 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001350}
1351
Stefan Richter6e95dea2010-02-21 17:56:21 +01001352static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001353{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001354 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001355 struct fw_cdev_send_request request;
1356 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001357
Stefan Richter6e95dea2010-02-21 17:56:21 +01001358 if (a->speed > client->device->card->link_speed ||
1359 a->length > 1024 << a->speed)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001360 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001361
Stefan Richter6e95dea2010-02-21 17:56:21 +01001362 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001363 return -EINVAL;
1364
Stefan Richter6e95dea2010-02-21 17:56:21 +01001365 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001366 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001367 request.length = a->length;
1368 request.closure = a->closure;
1369 request.data = a->data;
1370 request.generation = a->generation;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001371
Stefan Richter6e95dea2010-02-21 17:56:21 +01001372 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001373}
1374
Stefan Richter6e95dea2010-02-21 17:56:21 +01001375static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001376 ioctl_get_info,
1377 ioctl_send_request,
1378 ioctl_allocate,
1379 ioctl_deallocate,
1380 ioctl_send_response,
1381 ioctl_initiate_bus_reset,
1382 ioctl_add_descriptor,
1383 ioctl_remove_descriptor,
1384 ioctl_create_iso_context,
1385 ioctl_queue_iso,
1386 ioctl_start_iso,
1387 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001388 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001389 ioctl_allocate_iso_resource,
1390 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001391 ioctl_allocate_iso_resource_once,
1392 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001393 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001394 ioctl_send_broadcast_request,
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001395 ioctl_send_stream_packet,
Stefan Richterabfe5a02010-02-20 12:13:49 +01001396 ioctl_get_cycle_timer2,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001397};
1398
Stefan Richter53dca512008-12-14 21:47:04 +01001399static int dispatch_ioctl(struct client *client,
1400 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001401{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001402 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001403 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001404
Stefan Richter64582292010-02-21 17:56:42 +01001405 if (fw_device_is_shutdown(client->device))
1406 return -ENODEV;
1407
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001408 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001409 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1410 _IOC_SIZE(cmd) > sizeof(buffer))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001411 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001412
Stefan Richter9cac00b2010-04-07 08:30:50 +02001413 if (_IOC_DIR(cmd) == _IOC_READ)
1414 memset(&buffer, 0, _IOC_SIZE(cmd));
1415
1416 if (_IOC_DIR(cmd) & _IOC_WRITE)
1417 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001418 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001419
Stefan Richter6e95dea2010-02-21 17:56:21 +01001420 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001421 if (ret < 0)
1422 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001423
Stefan Richter9cac00b2010-04-07 08:30:50 +02001424 if (_IOC_DIR(cmd) & _IOC_READ)
1425 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001426 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001427
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001428 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001429}
1430
Stefan Richter53dca512008-12-14 21:47:04 +01001431static long fw_device_op_ioctl(struct file *file,
1432 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001433{
Stefan Richter64582292010-02-21 17:56:42 +01001434 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001435}
1436
1437#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001438static long fw_device_op_compat_ioctl(struct file *file,
1439 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001440{
Stefan Richter64582292010-02-21 17:56:42 +01001441 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001442}
1443#endif
1444
1445static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1446{
1447 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001448 enum dma_data_direction direction;
1449 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001450 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001451
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001452 if (fw_device_is_shutdown(client->device))
1453 return -ENODEV;
1454
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001455 /* FIXME: We could support multiple buffers, but we don't. */
1456 if (client->buffer.pages != NULL)
1457 return -EBUSY;
1458
1459 if (!(vma->vm_flags & VM_SHARED))
1460 return -EINVAL;
1461
1462 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001463 return -EINVAL;
1464
1465 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001466 size = vma->vm_end - vma->vm_start;
1467 page_count = size >> PAGE_SHIFT;
1468 if (size & ~PAGE_MASK)
1469 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001470
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001471 if (vma->vm_flags & VM_WRITE)
1472 direction = DMA_TO_DEVICE;
1473 else
1474 direction = DMA_FROM_DEVICE;
1475
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001476 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1477 page_count, direction);
1478 if (ret < 0)
1479 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001480
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001481 ret = fw_iso_buffer_map(&client->buffer, vma);
1482 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001483 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1484
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001485 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001486}
1487
Jay Fenlason45ee3192008-12-21 16:47:17 +01001488static int shutdown_resource(int id, void *p, void *data)
1489{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001490 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001491 struct client *client = data;
1492
Stefan Richtere21fcf72009-10-08 00:41:38 +02001493 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001494 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001495
1496 return 0;
1497}
1498
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001499static int fw_device_op_release(struct inode *inode, struct file *file)
1500{
1501 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001502 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001503
Stefan Richter97811e32008-12-14 19:19:23 +01001504 mutex_lock(&client->device->client_list_mutex);
1505 list_del(&client->link);
1506 mutex_unlock(&client->device->client_list_mutex);
1507
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001508 if (client->iso_context)
1509 fw_iso_context_destroy(client->iso_context);
1510
Stefan Richter36a755c2009-01-05 20:28:10 +01001511 if (client->buffer.pages)
1512 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1513
Jay Fenlason45ee3192008-12-21 16:47:17 +01001514 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001515 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001516 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001517 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001518
Jay Fenlason45ee3192008-12-21 16:47:17 +01001519 idr_for_each(&client->resource_idr, shutdown_resource, client);
1520 idr_remove_all(&client->resource_idr);
1521 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001522
Stefan Richtere21fcf72009-10-08 00:41:38 +02001523 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1524 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001525
Stefan Richterfb443032009-01-04 16:23:29 +01001526 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001527
1528 return 0;
1529}
1530
1531static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1532{
1533 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001534 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001535
1536 poll_wait(file, &client->wait, pt);
1537
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001538 if (fw_device_is_shutdown(client->device))
1539 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001540 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001541 mask |= POLLIN | POLLRDNORM;
1542
1543 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001544}
1545
Stefan Richter21ebcd12007-01-14 15:29:07 +01001546const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001547 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001548 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001549 .open = fw_device_op_open,
1550 .read = fw_device_op_read,
1551 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001552 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001553 .release = fw_device_op_release,
1554 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001555#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001556 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001557#endif
1558};