blob: 9b8df2039155563730644bcfe2f170abffd99fb7 [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;
321 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
322 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
Stefan Richter97c18b72009-01-04 16:23:29 +0100566 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
567 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500568 return -ENOMEM;
569
Stefan Richter97c18b72009-01-04 16:23:29 +0100570 e->client = client;
571 e->response.length = request->length;
572 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500573
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400574 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100575 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400576 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100577 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100578 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100579 }
580
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100581 e->r.resource.release = release_transaction;
582 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
583 if (ret < 0)
584 goto failed;
585
586 /* Get a reference for the transaction callback */
587 client_get(client);
588
589 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100590 request->tcode, destination_id, request->generation,
591 speed, request->offset, e->response.data,
592 request->length, complete_transaction, e);
593 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100594
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100595 failed:
596 kfree(e);
597
598 return ret;
599}
600
Stefan Richter6e95dea2010-02-21 17:56:21 +0100601static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100602{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100603 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100604 case TCODE_WRITE_QUADLET_REQUEST:
605 case TCODE_WRITE_BLOCK_REQUEST:
606 case TCODE_READ_QUADLET_REQUEST:
607 case TCODE_READ_BLOCK_REQUEST:
608 case TCODE_LOCK_MASK_SWAP:
609 case TCODE_LOCK_COMPARE_SWAP:
610 case TCODE_LOCK_FETCH_ADD:
611 case TCODE_LOCK_LITTLE_ADD:
612 case TCODE_LOCK_BOUNDED_ADD:
613 case TCODE_LOCK_WRAP_ADD:
614 case TCODE_LOCK_VENDOR_DEPENDENT:
615 break;
616 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100617 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618 }
619
Stefan Richter6e95dea2010-02-21 17:56:21 +0100620 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100621 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622}
623
Stefan Richter281e2032010-01-24 16:45:03 +0100624static inline bool is_fcp_request(struct fw_request *request)
625{
626 return request == NULL;
627}
628
Stefan Richter53dca512008-12-14 21:47:04 +0100629static void release_request(struct client *client,
630 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400631{
Stefan Richter97c18b72009-01-04 16:23:29 +0100632 struct inbound_transaction_resource *r = container_of(resource,
633 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400634
Stefan Richter281e2032010-01-24 16:45:03 +0100635 if (is_fcp_request(r->request))
636 kfree(r->data);
637 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400638 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200639
640 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100641 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400642}
643
Stefan Richter97c18b72009-01-04 16:23:29 +0100644static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100645 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200646 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100647 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500648{
Stefan Richter97c18b72009-01-04 16:23:29 +0100649 struct address_handler_resource *handler = callback_data;
650 struct inbound_transaction_resource *r;
651 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200652 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100653 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100654 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500655
Stefan Richter0244f572010-06-20 22:52:27 +0200656 /* card may be different from handler->client->device->card */
657 fw_card_get(card);
658
Stefan Richter97c18b72009-01-04 16:23:29 +0100659 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400660 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100661 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100662 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400664 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100665 r->request = request;
666 r->data = payload;
667 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500668
Stefan Richter281e2032010-01-24 16:45:03 +0100669 if (is_fcp_request(request)) {
670 /*
671 * FIXME: Let core-transaction.c manage a
672 * single reference-counted copy?
673 */
674 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
675 if (fcp_frame == NULL)
676 goto failed;
677
678 r->data = fcp_frame;
679 }
680
Stefan Richter97c18b72009-01-04 16:23:29 +0100681 r->resource.release = release_request;
682 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100683 if (ret < 0)
684 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500685
Stefan Richtere2055972010-06-20 22:53:55 +0200686 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
687 struct fw_cdev_event_request *req = &e->req.request;
688
689 if (tcode & 0x10)
690 tcode = TCODE_LOCK_REQUEST;
691
692 req->type = FW_CDEV_EVENT_REQUEST;
693 req->tcode = tcode;
694 req->offset = offset;
695 req->length = length;
696 req->handle = r->resource.handle;
697 req->closure = handler->closure;
698 event_size0 = sizeof(*req);
699 } else {
700 struct fw_cdev_event_request2 *req = &e->req.request2;
701
702 req->type = FW_CDEV_EVENT_REQUEST2;
703 req->tcode = tcode;
704 req->offset = offset;
705 req->source_node_id = source;
706 req->destination_node_id = destination;
707 req->card = card->index;
708 req->generation = generation;
709 req->length = length;
710 req->handle = r->resource.handle;
711 req->closure = handler->closure;
712 event_size0 = sizeof(*req);
713 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714
Stefan Richter97c18b72009-01-04 16:23:29 +0100715 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200716 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100717 return;
718
719 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100720 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100721 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100722 kfree(fcp_frame);
723
724 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100725 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200726
727 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500728}
729
Stefan Richter53dca512008-12-14 21:47:04 +0100730static void release_address_handler(struct client *client,
731 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400732{
Stefan Richter97c18b72009-01-04 16:23:29 +0100733 struct address_handler_resource *r =
734 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400735
Stefan Richter97c18b72009-01-04 16:23:29 +0100736 fw_core_remove_address_handler(&r->handler);
737 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400738}
739
Stefan Richter6e95dea2010-02-21 17:56:21 +0100740static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500741{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100742 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100743 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500744 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100745 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500746
Stefan Richter97c18b72009-01-04 16:23:29 +0100747 r = kmalloc(sizeof(*r), GFP_KERNEL);
748 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500749 return -ENOMEM;
750
Stefan Richter6e95dea2010-02-21 17:56:21 +0100751 region.start = a->offset;
752 region.end = a->offset + a->length;
753 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100754 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100755 r->handler.callback_data = r;
756 r->closure = a->closure;
757 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500758
Stefan Richter97c18b72009-01-04 16:23:29 +0100759 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100760 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100761 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100762 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500763 }
764
Stefan Richter97c18b72009-01-04 16:23:29 +0100765 r->resource.release = release_address_handler;
766 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100767 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100768 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100769 return ret;
770 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100771 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500772
773 return 0;
774}
775
Stefan Richter6e95dea2010-02-21 17:56:21 +0100776static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400777{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100778 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100779 release_address_handler, NULL);
Kristian Høgsberg9472316b2007-03-14 17:34:55 -0400780}
781
Stefan Richter6e95dea2010-02-21 17:56:21 +0100782static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500783{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100784 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400785 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100786 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200787 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500788
Stefan Richter6e95dea2010-02-21 17:56:21 +0100789 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100790 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500791 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100792
Stefan Richter97c18b72009-01-04 16:23:29 +0100793 r = container_of(resource, struct inbound_transaction_resource,
794 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100795 if (is_fcp_request(r->request))
796 goto out;
797
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200798 if (a->length != fw_get_response_length(r->request)) {
799 ret = -EINVAL;
800 kfree(r->request);
801 goto out;
802 }
803 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100804 ret = -EFAULT;
805 kfree(r->request);
806 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200807 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400808 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200809 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200810 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500811 kfree(r);
812
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200813 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500814}
815
Stefan Richter6e95dea2010-02-21 17:56:21 +0100816static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500817{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100818 return fw_core_initiate_bus_reset(client->device->card,
819 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500820}
821
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400822static void release_descriptor(struct client *client,
823 struct client_resource *resource)
824{
Stefan Richter97c18b72009-01-04 16:23:29 +0100825 struct descriptor_resource *r =
826 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400827
Stefan Richter97c18b72009-01-04 16:23:29 +0100828 fw_core_remove_descriptor(&r->descriptor);
829 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400830}
831
Stefan Richter6e95dea2010-02-21 17:56:21 +0100832static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200833{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100834 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100835 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100836 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200837
Stefan Richterde487da2009-03-10 21:00:23 +0100838 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200839 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100840 return -ENOSYS;
841
Stefan Richter6e95dea2010-02-21 17:56:21 +0100842 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200843 return -EINVAL;
844
Stefan Richter6e95dea2010-02-21 17:56:21 +0100845 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100846 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200847 return -ENOMEM;
848
Stefan Richter6e95dea2010-02-21 17:56:21 +0100849 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100850 ret = -EFAULT;
851 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200852 }
853
Stefan Richter6e95dea2010-02-21 17:56:21 +0100854 r->descriptor.length = a->length;
855 r->descriptor.immediate = a->immediate;
856 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100857 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200858
Stefan Richter97c18b72009-01-04 16:23:29 +0100859 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100860 if (ret < 0)
861 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200862
Stefan Richter97c18b72009-01-04 16:23:29 +0100863 r->resource.release = release_descriptor;
864 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100865 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100866 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100867 goto failed;
868 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100869 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200870
871 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100872 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100873 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100874
875 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200876}
877
Stefan Richter6e95dea2010-02-21 17:56:21 +0100878static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200879{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100880 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100881 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200882}
883
Stefan Richter53dca512008-12-14 21:47:04 +0100884static void iso_callback(struct fw_iso_context *context, u32 cycle,
885 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500886{
887 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100888 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500889
Stefan Richter56d04cb12010-06-08 00:20:10 +0200890 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100891 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500892 return;
893
Stefan Richter97c18b72009-01-04 16:23:29 +0100894 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
895 e->interrupt.closure = client->iso_closure;
896 e->interrupt.cycle = cycle;
897 e->interrupt.header_length = header_length;
898 memcpy(e->interrupt.header, header, header_length);
899 queue_event(client, &e->event, &e->interrupt,
900 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500901}
902
Stefan Richter6e95dea2010-02-21 17:56:21 +0100903static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500904{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100905 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400906 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500907
Stefan Richter6e95dea2010-02-21 17:56:21 +0100908 if (a->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500909 return -EINVAL;
910
Stefan Richter6e95dea2010-02-21 17:56:21 +0100911 switch (a->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400912 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100913 if (a->header_size < 4 || (a->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400914 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400915 break;
916
917 case FW_ISO_CONTEXT_TRANSMIT:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100918 if (a->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400919 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400920 break;
921
922 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500923 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400924 }
925
Stefan Richter6e95dea2010-02-21 17:56:21 +0100926 context = fw_iso_context_create(client->device->card, a->type,
927 a->channel, a->speed, a->header_size,
928 iso_callback, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400929 if (IS_ERR(context))
930 return PTR_ERR(context);
931
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200932 /* We only support one context at this time. */
933 spin_lock_irq(&client->lock);
934 if (client->iso_context != NULL) {
935 spin_unlock_irq(&client->lock);
936 fw_iso_context_destroy(context);
937 return -EBUSY;
938 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100939 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400940 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200941 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500942
Stefan Richter6e95dea2010-02-21 17:56:21 +0100943 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400944
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500945 return 0;
946}
947
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400948/* Macros for decoding the iso packet control header. */
949#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
950#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
951#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200952#define GET_TAG(v) (((v) >> 18) & 0x03)
953#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400954#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
955
Stefan Richter6e95dea2010-02-21 17:56:21 +0100956static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500957{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100958 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500959 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500960 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200961 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400962 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500963 int count;
964 struct {
965 struct fw_iso_packet packet;
966 u8 header[256];
967 } u;
968
Stefan Richter6e95dea2010-02-21 17:56:21 +0100969 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500970 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500971
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400972 /*
973 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500974 * the iso buffer, and the pointer points inside the buffer,
975 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500976 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500977 * payload_length == 0 through. In other words, if no packets
978 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +0100979 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400980 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500981
Stefan Richter6e95dea2010-02-21 17:56:21 +0100982 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200983 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100984 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200985 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500986 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200987 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500988 }
989
Stefan Richter6e95dea2010-02-21 17:56:21 +0100990 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Al Viro1ccc9142007-10-14 19:34:40 +0100991
Stefan Richter6e95dea2010-02-21 17:56:21 +0100992 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500993 return -EFAULT;
994
Stefan Richter6e95dea2010-02-21 17:56:21 +0100995 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500996 count = 0;
997 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400998 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500999 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001000 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1001 u.packet.interrupt = GET_INTERRUPT(control);
1002 u.packet.skip = GET_SKIP(control);
1003 u.packet.tag = GET_TAG(control);
1004 u.packet.sy = GET_SY(control);
1005 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001006
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001007 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +02001008 if (u.packet.header_length % 4 != 0)
1009 return -EINVAL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001010 header_length = u.packet.header_length;
1011 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001012 /*
1013 * We require that header_length is a multiple of
1014 * the fixed header size, ctx->header_size.
1015 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001016 if (ctx->header_size == 0) {
1017 if (u.packet.header_length > 0)
1018 return -EINVAL;
Clemens Ladisch4ba1d9c2010-03-31 16:26:39 +02001019 } else if (u.packet.header_length == 0 ||
1020 u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001021 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001022 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001023 header_length = 0;
1024 }
1025
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001026 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001027 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001028 if (next > end)
1029 return -EINVAL;
1030 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001031 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001032 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001033 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001034 u.packet.header_length + u.packet.payload_length > 0)
1035 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001036 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001037 return -EINVAL;
1038
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001039 if (fw_iso_context_queue(ctx, &u.packet,
1040 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001041 break;
1042
1043 p = next;
1044 payload += u.packet.payload_length;
1045 count++;
1046 }
1047
Stefan Richter6e95dea2010-02-21 17:56:21 +01001048 a->size -= uptr_to_u64(p) - a->packets;
1049 a->packets = uptr_to_u64(p);
1050 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001051
1052 return count;
1053}
1054
Stefan Richter6e95dea2010-02-21 17:56:21 +01001055static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001056{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001057 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001058
Stefan Richter6e95dea2010-02-21 17:56:21 +01001059 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001060 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001061
Stefan Richter6e95dea2010-02-21 17:56:21 +01001062 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1063 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1064 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001065
Stefan Richter6e95dea2010-02-21 17:56:21 +01001066 return fw_iso_context_start(client->iso_context,
1067 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001068}
1069
Stefan Richter6e95dea2010-02-21 17:56:21 +01001070static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001071{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001072 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001073
Stefan Richter6e95dea2010-02-21 17:56:21 +01001074 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001075 return -EINVAL;
1076
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001077 return fw_iso_context_stop(client->iso_context);
1078}
1079
Stefan Richter6e95dea2010-02-21 17:56:21 +01001080static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001081{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001082 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001083 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001084 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001085 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001086 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001087
Stefan Richter4a9bde92010-02-20 22:24:43 +01001088 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001089
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001090 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001091
Stefan Richter6e95dea2010-02-21 17:56:21 +01001092 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001093 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1094 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1095 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1096 default:
1097 ret = -EINVAL;
1098 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001099
Stefan Richter4a9bde92010-02-20 22:24:43 +01001100 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001101
Stefan Richter6e95dea2010-02-21 17:56:21 +01001102 a->tv_sec = ts.tv_sec;
1103 a->tv_nsec = ts.tv_nsec;
1104 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001105
1106 return ret;
1107}
1108
Stefan Richter6e95dea2010-02-21 17:56:21 +01001109static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001110{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001111 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001112 struct fw_cdev_get_cycle_timer2 ct2;
1113
1114 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001115 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001116
Stefan Richter6e95dea2010-02-21 17:56:21 +01001117 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1118 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001119
Stefan Richtera64408b2007-09-29 10:41:58 +02001120 return 0;
1121}
1122
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001123static void iso_resource_work(struct work_struct *work)
1124{
1125 struct iso_resource_event *e;
1126 struct iso_resource *r =
1127 container_of(work, struct iso_resource, work.work);
1128 struct client *client = r->client;
1129 int generation, channel, bandwidth, todo;
1130 bool skip, free, success;
1131
1132 spin_lock_irq(&client->lock);
1133 generation = client->device->generation;
1134 todo = r->todo;
1135 /* Allow 1000ms grace period for other reallocations. */
1136 if (todo == ISO_RES_ALLOC &&
1137 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001138 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001139 skip = true;
1140 } else {
1141 /* We could be called twice within the same generation. */
1142 skip = todo == ISO_RES_REALLOC &&
1143 r->generation == generation;
1144 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001145 free = todo == ISO_RES_DEALLOC ||
1146 todo == ISO_RES_ALLOC_ONCE ||
1147 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001148 r->generation = generation;
1149 spin_unlock_irq(&client->lock);
1150
1151 if (skip)
1152 goto out;
1153
1154 bandwidth = r->bandwidth;
1155
1156 fw_iso_resource_manage(client->device->card, generation,
1157 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001158 todo == ISO_RES_ALLOC ||
1159 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001160 todo == ISO_RES_ALLOC_ONCE,
1161 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001162 /*
1163 * Is this generation outdated already? As long as this resource sticks
1164 * in the idr, it will be scheduled again for a newer generation or at
1165 * shutdown.
1166 */
1167 if (channel == -EAGAIN &&
1168 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1169 goto out;
1170
1171 success = channel >= 0 || bandwidth > 0;
1172
1173 spin_lock_irq(&client->lock);
1174 /*
1175 * Transit from allocation to reallocation, except if the client
1176 * requested deallocation in the meantime.
1177 */
1178 if (r->todo == ISO_RES_ALLOC)
1179 r->todo = ISO_RES_REALLOC;
1180 /*
1181 * Allocation or reallocation failure? Pull this resource out of the
1182 * idr and prepare for deletion, unless the client is shutting down.
1183 */
1184 if (r->todo == ISO_RES_REALLOC && !success &&
1185 !client->in_shutdown &&
1186 idr_find(&client->resource_idr, r->resource.handle)) {
1187 idr_remove(&client->resource_idr, r->resource.handle);
1188 client_put(client);
1189 free = true;
1190 }
1191 spin_unlock_irq(&client->lock);
1192
1193 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001194 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001195
1196 if (todo == ISO_RES_REALLOC && success)
1197 goto out;
1198
Stefan Richter1ec3c022009-01-04 16:23:29 +01001199 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001200 e = r->e_alloc;
1201 r->e_alloc = NULL;
1202 } else {
1203 e = r->e_dealloc;
1204 r->e_dealloc = NULL;
1205 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001206 e->iso_resource.handle = r->resource.handle;
1207 e->iso_resource.channel = channel;
1208 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001209
1210 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001211 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001212
1213 if (free) {
1214 cancel_delayed_work(&r->work);
1215 kfree(r->e_alloc);
1216 kfree(r->e_dealloc);
1217 kfree(r);
1218 }
1219 out:
1220 client_put(client);
1221}
1222
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001223static void release_iso_resource(struct client *client,
1224 struct client_resource *resource)
1225{
1226 struct iso_resource *r =
1227 container_of(resource, struct iso_resource, resource);
1228
1229 spin_lock_irq(&client->lock);
1230 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001231 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001232 spin_unlock_irq(&client->lock);
1233}
1234
Stefan Richter1ec3c022009-01-04 16:23:29 +01001235static int init_iso_resource(struct client *client,
1236 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001237{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001238 struct iso_resource_event *e1, *e2;
1239 struct iso_resource *r;
1240 int ret;
1241
1242 if ((request->channels == 0 && request->bandwidth == 0) ||
1243 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1244 request->bandwidth < 0)
1245 return -EINVAL;
1246
1247 r = kmalloc(sizeof(*r), GFP_KERNEL);
1248 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1249 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1250 if (r == NULL || e1 == NULL || e2 == NULL) {
1251 ret = -ENOMEM;
1252 goto fail;
1253 }
1254
1255 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1256 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001257 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001258 r->generation = -1;
1259 r->channels = request->channels;
1260 r->bandwidth = request->bandwidth;
1261 r->e_alloc = e1;
1262 r->e_dealloc = e2;
1263
Stefan Richtere21fcf72009-10-08 00:41:38 +02001264 e1->iso_resource.closure = request->closure;
1265 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1266 e2->iso_resource.closure = request->closure;
1267 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001268
Stefan Richter1ec3c022009-01-04 16:23:29 +01001269 if (todo == ISO_RES_ALLOC) {
1270 r->resource.release = release_iso_resource;
1271 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001272 if (ret < 0)
1273 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001274 } else {
1275 r->resource.release = NULL;
1276 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001277 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001278 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001279 request->handle = r->resource.handle;
1280
1281 return 0;
1282 fail:
1283 kfree(r);
1284 kfree(e1);
1285 kfree(e2);
1286
1287 return ret;
1288}
1289
Stefan Richter6e95dea2010-02-21 17:56:21 +01001290static int ioctl_allocate_iso_resource(struct client *client,
1291 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001292{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001293 return init_iso_resource(client,
1294 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001295}
1296
Stefan Richter6e95dea2010-02-21 17:56:21 +01001297static int ioctl_deallocate_iso_resource(struct client *client,
1298 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001299{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001300 return release_client_resource(client,
1301 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001302}
1303
Stefan Richter6e95dea2010-02-21 17:56:21 +01001304static int ioctl_allocate_iso_resource_once(struct client *client,
1305 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001306{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001307 return init_iso_resource(client,
1308 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001309}
1310
Stefan Richter6e95dea2010-02-21 17:56:21 +01001311static int ioctl_deallocate_iso_resource_once(struct client *client,
1312 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001313{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001314 return init_iso_resource(client,
1315 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001316}
1317
Stefan Richterc8a25902009-03-10 20:59:16 +01001318/*
1319 * Returns a speed code: Maximum speed to or from this device,
1320 * limited by the device's link speed, the local node's link speed,
1321 * and all PHY port speeds between the two links.
1322 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001323static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001324{
Stefan Richterc8a25902009-03-10 20:59:16 +01001325 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001326}
1327
Stefan Richter6e95dea2010-02-21 17:56:21 +01001328static int ioctl_send_broadcast_request(struct client *client,
1329 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001330{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001331 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001332
Stefan Richter6e95dea2010-02-21 17:56:21 +01001333 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001334 case TCODE_WRITE_QUADLET_REQUEST:
1335 case TCODE_WRITE_BLOCK_REQUEST:
1336 break;
1337 default:
1338 return -EINVAL;
1339 }
1340
Stefan Richter1566f3d2009-01-04 16:23:29 +01001341 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001342 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001343 return -EACCES;
1344
Stefan Richter6e95dea2010-02-21 17:56:21 +01001345 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001346}
1347
Stefan Richter6e95dea2010-02-21 17:56:21 +01001348static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001349{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001350 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001351 struct fw_cdev_send_request request;
1352 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001353
Stefan Richter6e95dea2010-02-21 17:56:21 +01001354 if (a->speed > client->device->card->link_speed ||
1355 a->length > 1024 << a->speed)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001356 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001357
Stefan Richter6e95dea2010-02-21 17:56:21 +01001358 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001359 return -EINVAL;
1360
Stefan Richter6e95dea2010-02-21 17:56:21 +01001361 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001362 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001363 request.length = a->length;
1364 request.closure = a->closure;
1365 request.data = a->data;
1366 request.generation = a->generation;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001367
Stefan Richter6e95dea2010-02-21 17:56:21 +01001368 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001369}
1370
Stefan Richter6e95dea2010-02-21 17:56:21 +01001371static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001372 ioctl_get_info,
1373 ioctl_send_request,
1374 ioctl_allocate,
1375 ioctl_deallocate,
1376 ioctl_send_response,
1377 ioctl_initiate_bus_reset,
1378 ioctl_add_descriptor,
1379 ioctl_remove_descriptor,
1380 ioctl_create_iso_context,
1381 ioctl_queue_iso,
1382 ioctl_start_iso,
1383 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001384 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001385 ioctl_allocate_iso_resource,
1386 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001387 ioctl_allocate_iso_resource_once,
1388 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001389 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001390 ioctl_send_broadcast_request,
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001391 ioctl_send_stream_packet,
Stefan Richterabfe5a02010-02-20 12:13:49 +01001392 ioctl_get_cycle_timer2,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001393};
1394
Stefan Richter53dca512008-12-14 21:47:04 +01001395static int dispatch_ioctl(struct client *client,
1396 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001397{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001398 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001399 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001400
Stefan Richter64582292010-02-21 17:56:42 +01001401 if (fw_device_is_shutdown(client->device))
1402 return -ENODEV;
1403
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001404 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001405 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1406 _IOC_SIZE(cmd) > sizeof(buffer))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001407 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001408
Stefan Richter9cac00b2010-04-07 08:30:50 +02001409 if (_IOC_DIR(cmd) == _IOC_READ)
1410 memset(&buffer, 0, _IOC_SIZE(cmd));
1411
1412 if (_IOC_DIR(cmd) & _IOC_WRITE)
1413 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001414 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001415
Stefan Richter6e95dea2010-02-21 17:56:21 +01001416 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001417 if (ret < 0)
1418 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001419
Stefan Richter9cac00b2010-04-07 08:30:50 +02001420 if (_IOC_DIR(cmd) & _IOC_READ)
1421 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001422 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001423
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001424 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001425}
1426
Stefan Richter53dca512008-12-14 21:47:04 +01001427static long fw_device_op_ioctl(struct file *file,
1428 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001429{
Stefan Richter64582292010-02-21 17:56:42 +01001430 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001431}
1432
1433#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001434static long fw_device_op_compat_ioctl(struct file *file,
1435 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001436{
Stefan Richter64582292010-02-21 17:56:42 +01001437 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001438}
1439#endif
1440
1441static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1442{
1443 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001444 enum dma_data_direction direction;
1445 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001446 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001447
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001448 if (fw_device_is_shutdown(client->device))
1449 return -ENODEV;
1450
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001451 /* FIXME: We could support multiple buffers, but we don't. */
1452 if (client->buffer.pages != NULL)
1453 return -EBUSY;
1454
1455 if (!(vma->vm_flags & VM_SHARED))
1456 return -EINVAL;
1457
1458 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001459 return -EINVAL;
1460
1461 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001462 size = vma->vm_end - vma->vm_start;
1463 page_count = size >> PAGE_SHIFT;
1464 if (size & ~PAGE_MASK)
1465 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001466
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001467 if (vma->vm_flags & VM_WRITE)
1468 direction = DMA_TO_DEVICE;
1469 else
1470 direction = DMA_FROM_DEVICE;
1471
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001472 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1473 page_count, direction);
1474 if (ret < 0)
1475 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001476
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001477 ret = fw_iso_buffer_map(&client->buffer, vma);
1478 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001479 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1480
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001481 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001482}
1483
Jay Fenlason45ee3192008-12-21 16:47:17 +01001484static int shutdown_resource(int id, void *p, void *data)
1485{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001486 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001487 struct client *client = data;
1488
Stefan Richtere21fcf72009-10-08 00:41:38 +02001489 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001490 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001491
1492 return 0;
1493}
1494
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001495static int fw_device_op_release(struct inode *inode, struct file *file)
1496{
1497 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001498 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001499
Stefan Richter97811e32008-12-14 19:19:23 +01001500 mutex_lock(&client->device->client_list_mutex);
1501 list_del(&client->link);
1502 mutex_unlock(&client->device->client_list_mutex);
1503
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001504 if (client->iso_context)
1505 fw_iso_context_destroy(client->iso_context);
1506
Stefan Richter36a755c2009-01-05 20:28:10 +01001507 if (client->buffer.pages)
1508 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1509
Jay Fenlason45ee3192008-12-21 16:47:17 +01001510 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001511 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001512 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001513 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001514
Jay Fenlason45ee3192008-12-21 16:47:17 +01001515 idr_for_each(&client->resource_idr, shutdown_resource, client);
1516 idr_remove_all(&client->resource_idr);
1517 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001518
Stefan Richtere21fcf72009-10-08 00:41:38 +02001519 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1520 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001521
Stefan Richterfb443032009-01-04 16:23:29 +01001522 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001523
1524 return 0;
1525}
1526
1527static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1528{
1529 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001530 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001531
1532 poll_wait(file, &client->wait, pt);
1533
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001534 if (fw_device_is_shutdown(client->device))
1535 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001536 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001537 mask |= POLLIN | POLLRDNORM;
1538
1539 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001540}
1541
Stefan Richter21ebcd12007-01-14 15:29:07 +01001542const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001543 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001544 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001545 .open = fw_device_op_open,
1546 .read = fw_device_op_read,
1547 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001548 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001549 .release = fw_device_op_release,
1550 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001551#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001552 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001553#endif
1554};