blob: 0cf86bcbeea20f1bcb32dd8e28bd6dd4ba7003fb [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 */
52#define FW_CDEV_KERNEL_VERSION 3
53
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050054struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050055 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050056 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010057
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050058 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010059 bool in_shutdown;
60 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050061 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050062 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040063 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050064
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050065 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040066 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050067 struct fw_iso_buffer buffer;
68 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050069
70 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010071 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050072};
73
Stefan Richterfb443032009-01-04 16:23:29 +010074static inline void client_get(struct client *client)
75{
76 kref_get(&client->kref);
77}
78
79static void client_release(struct kref *kref)
80{
81 struct client *client = container_of(kref, struct client, kref);
82
83 fw_device_put(client->device);
84 kfree(client);
85}
86
87static void client_put(struct client *client)
88{
89 kref_put(&client->kref, client_release);
90}
91
Stefan Richter97c18b72009-01-04 16:23:29 +010092struct client_resource;
93typedef void (*client_resource_release_fn_t)(struct client *,
94 struct client_resource *);
95struct client_resource {
96 client_resource_release_fn_t release;
97 int handle;
98};
99
100struct address_handler_resource {
101 struct client_resource resource;
102 struct fw_address_handler handler;
103 __u64 closure;
104 struct client *client;
105};
106
107struct outbound_transaction_resource {
108 struct client_resource resource;
109 struct fw_transaction transaction;
110};
111
112struct inbound_transaction_resource {
113 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400114 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100115 struct fw_request *request;
116 void *data;
117 size_t length;
118};
119
120struct descriptor_resource {
121 struct client_resource resource;
122 struct fw_descriptor descriptor;
123 u32 data[0];
124};
125
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100126struct iso_resource {
127 struct client_resource resource;
128 struct client *client;
129 /* Schedule work and access todo only with client->lock held. */
130 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100131 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
132 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100133 int generation;
134 u64 channels;
135 s32 bandwidth;
Stefan Richter6fdc0372009-06-20 13:23:59 +0200136 __be32 transaction_data[2];
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100137 struct iso_resource_event *e_alloc, *e_dealloc;
138};
139
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100140static void release_iso_resource(struct client *, struct client_resource *);
141
Stefan Richter9fb551b2009-10-08 00:41:10 +0200142static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
143{
144 client_get(r->client);
145 if (!schedule_delayed_work(&r->work, delay))
146 client_put(r->client);
147}
148
149static void schedule_if_iso_resource(struct client_resource *resource)
150{
151 if (resource->release == release_iso_resource)
152 schedule_iso_resource(container_of(resource,
153 struct iso_resource, resource), 0);
154}
155
Stefan Richter97c18b72009-01-04 16:23:29 +0100156/*
157 * dequeue_event() just kfree()'s the event, so the event has to be
158 * the first field in a struct XYZ_event.
159 */
160struct event {
161 struct { void *data; size_t size; } v[2];
162 struct list_head link;
163};
164
165struct bus_reset_event {
166 struct event event;
167 struct fw_cdev_event_bus_reset reset;
168};
169
170struct outbound_transaction_event {
171 struct event event;
172 struct client *client;
173 struct outbound_transaction_resource r;
174 struct fw_cdev_event_response response;
175};
176
177struct inbound_transaction_event {
178 struct event event;
179 struct fw_cdev_event_request request;
180};
181
182struct iso_interrupt_event {
183 struct event event;
184 struct fw_cdev_event_iso_interrupt interrupt;
185};
186
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100187struct iso_resource_event {
188 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200189 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100190};
191
Stefan Richter53dca512008-12-14 21:47:04 +0100192static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500193{
194 return (void __user *)(unsigned long)value;
195}
196
Stefan Richter53dca512008-12-14 21:47:04 +0100197static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500198{
199 return (__u64)(unsigned long)ptr;
200}
201
202static int fw_device_op_open(struct inode *inode, struct file *file)
203{
204 struct fw_device *device;
205 struct client *client;
206
Stefan Richter96b19062008-02-02 15:01:09 +0100207 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500208 if (device == NULL)
209 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500210
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400211 if (fw_device_is_shutdown(device)) {
212 fw_device_put(device);
213 return -ENODEV;
214 }
215
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400216 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100217 if (client == NULL) {
218 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500219 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100220 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500221
Stefan Richter96b19062008-02-02 15:01:09 +0100222 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500223 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100224 idr_init(&client->resource_idr);
225 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500226 init_waitqueue_head(&client->wait);
Stefan Richterfb443032009-01-04 16:23:29 +0100227 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500228
229 file->private_data = client;
230
Stefan Richterd67cfb92008-10-05 10:37:11 +0200231 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500232 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200233 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500234
Stefan Richter3ac26b22010-04-10 16:38:05 +0100235 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500236}
237
238static void queue_event(struct client *client, struct event *event,
239 void *data0, size_t size0, void *data1, size_t size1)
240{
241 unsigned long flags;
242
243 event->v[0].data = data0;
244 event->v[0].size = size0;
245 event->v[1].data = data1;
246 event->v[1].size = size1;
247
248 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100249 if (client->in_shutdown)
250 kfree(event);
251 else
252 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500253 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400254
255 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500256}
257
Stefan Richter53dca512008-12-14 21:47:04 +0100258static int dequeue_event(struct client *client,
259 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500260{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500261 struct event *event;
262 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100263 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500264
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100265 ret = wait_event_interruptible(client->wait,
266 !list_empty(&client->event_list) ||
267 fw_device_is_shutdown(client->device));
268 if (ret < 0)
269 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500270
271 if (list_empty(&client->event_list) &&
272 fw_device_is_shutdown(client->device))
273 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500274
Stefan Richter3ba94982009-01-04 16:23:29 +0100275 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100276 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500277 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100278 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500279
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500280 total = 0;
281 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
282 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500283 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100284 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500285 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500286 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500287 total += size;
288 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100289 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500290
291 out:
292 kfree(event);
293
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100294 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500295}
296
Stefan Richter53dca512008-12-14 21:47:04 +0100297static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
298 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500299{
300 struct client *client = file->private_data;
301
302 return dequeue_event(client, buffer, count);
303}
304
Stefan Richter53dca512008-12-14 21:47:04 +0100305static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
306 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500307{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400308 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400309
Stefan Richter3ba94982009-01-04 16:23:29 +0100310 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500311
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400312 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500313 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100314 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400315 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500316 event->local_node_id = card->local_node->node_id;
317 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
318 event->irm_node_id = card->irm_node->node_id;
319 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400320
Stefan Richter3ba94982009-01-04 16:23:29 +0100321 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500322}
323
Stefan Richter53dca512008-12-14 21:47:04 +0100324static void for_each_client(struct fw_device *device,
325 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500326{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500327 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500328
Stefan Richterd67cfb92008-10-05 10:37:11 +0200329 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500330 list_for_each_entry(c, &device->client_list, link)
331 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200332 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500333}
334
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100335static int schedule_reallocations(int id, void *p, void *data)
336{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200337 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100338
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100339 return 0;
340}
341
Stefan Richter53dca512008-12-14 21:47:04 +0100342static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500343{
Stefan Richter97c18b72009-01-04 16:23:29 +0100344 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500345
Stefan Richter97c18b72009-01-04 16:23:29 +0100346 e = kzalloc(sizeof(*e), GFP_KERNEL);
347 if (e == NULL) {
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500348 fw_notify("Out of memory when allocating bus reset event\n");
349 return;
350 }
351
Stefan Richter97c18b72009-01-04 16:23:29 +0100352 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500353
Stefan Richter97c18b72009-01-04 16:23:29 +0100354 queue_event(client, &e->event,
355 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100356
357 spin_lock_irq(&client->lock);
358 idr_for_each(&client->resource_idr, schedule_reallocations, client);
359 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500360}
361
362void fw_device_cdev_update(struct fw_device *device)
363{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500364 for_each_client(device, queue_bus_reset_event);
365}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500366
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500367static void wake_up_client(struct client *client)
368{
369 wake_up_interruptible(&client->wait);
370}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500371
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500372void fw_device_cdev_remove(struct fw_device *device)
373{
374 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500375}
376
Stefan Richter6e95dea2010-02-21 17:56:21 +0100377union ioctl_arg {
378 struct fw_cdev_get_info get_info;
379 struct fw_cdev_send_request send_request;
380 struct fw_cdev_allocate allocate;
381 struct fw_cdev_deallocate deallocate;
382 struct fw_cdev_send_response send_response;
383 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
384 struct fw_cdev_add_descriptor add_descriptor;
385 struct fw_cdev_remove_descriptor remove_descriptor;
386 struct fw_cdev_create_iso_context create_iso_context;
387 struct fw_cdev_queue_iso queue_iso;
388 struct fw_cdev_start_iso start_iso;
389 struct fw_cdev_stop_iso stop_iso;
390 struct fw_cdev_get_cycle_timer get_cycle_timer;
391 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
392 struct fw_cdev_send_stream_packet send_stream_packet;
393 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
394};
395
396static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500397{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100398 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500399 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100400 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500401
Stefan Richter6e95dea2010-02-21 17:56:21 +0100402 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200403 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100404 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500405
Stefan Richterc9755e12008-03-24 20:54:28 +0100406 down_read(&fw_device_rwsem);
407
Stefan Richter6e95dea2010-02-21 17:56:21 +0100408 if (a->rom != 0) {
409 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100410 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500411
Stefan Richter6e95dea2010-02-21 17:56:21 +0100412 ret = copy_to_user(u64_to_uptr(a->rom),
413 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500414 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100415 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500416
Stefan Richterc9755e12008-03-24 20:54:28 +0100417 up_read(&fw_device_rwsem);
418
419 if (ret != 0)
420 return -EFAULT;
421
Stefan Richter6e95dea2010-02-21 17:56:21 +0100422 client->bus_reset_closure = a->bus_reset_closure;
423 if (a->bus_reset != 0) {
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400424 fill_bus_reset_event(&bus_reset, client);
Stefan Richter6e95dea2010-02-21 17:56:21 +0100425 if (copy_to_user(u64_to_uptr(a->bus_reset),
426 &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500427 return -EFAULT;
428 }
429
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500430 return 0;
431}
432
Stefan Richter53dca512008-12-14 21:47:04 +0100433static int add_client_resource(struct client *client,
434 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400435{
436 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100437 int ret;
438
439 retry:
440 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
441 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400442
443 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100444 if (client->in_shutdown)
445 ret = -ECANCELED;
446 else
447 ret = idr_get_new(&client->resource_idr, resource,
448 &resource->handle);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100449 if (ret >= 0) {
Stefan Richterfb443032009-01-04 16:23:29 +0100450 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200451 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100452 }
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400453 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100454
455 if (ret == -EAGAIN)
456 goto retry;
457
458 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400459}
460
Stefan Richter53dca512008-12-14 21:47:04 +0100461static int release_client_resource(struct client *client, u32 handle,
462 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200463 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400464{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200465 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400466
Stefan Richter3ba94982009-01-04 16:23:29 +0100467 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100468 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200469 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100470 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200471 resource = idr_find(&client->resource_idr, handle);
472 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100473 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100474 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400475
Stefan Richtere21fcf72009-10-08 00:41:38 +0200476 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400477 return -EINVAL;
478
Stefan Richtere21fcf72009-10-08 00:41:38 +0200479 if (return_resource)
480 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400481 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200482 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400483
Stefan Richterfb443032009-01-04 16:23:29 +0100484 client_put(client);
485
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400486 return 0;
487}
488
Stefan Richter53dca512008-12-14 21:47:04 +0100489static void release_transaction(struct client *client,
490 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400491{
Stefan Richter97c18b72009-01-04 16:23:29 +0100492 struct outbound_transaction_resource *r = container_of(resource,
493 struct outbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400494
Stefan Richter97c18b72009-01-04 16:23:29 +0100495 fw_cancel_transaction(client->device->card, &r->transaction);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400496}
497
Stefan Richter53dca512008-12-14 21:47:04 +0100498static void complete_transaction(struct fw_card *card, int rcode,
499 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500500{
Stefan Richter97c18b72009-01-04 16:23:29 +0100501 struct outbound_transaction_event *e = data;
502 struct fw_cdev_event_response *rsp = &e->response;
503 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500504 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500505
Stefan Richter97c18b72009-01-04 16:23:29 +0100506 if (length < rsp->length)
507 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500508 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100509 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500510
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500511 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100512 /*
Stefan Richterfb443032009-01-04 16:23:29 +0100513 * 1. If called while in shutdown, the idr tree must be left untouched.
514 * The idr handle will be removed and the client reference will be
515 * dropped later.
516 * 2. If the call chain was release_client_resource ->
517 * release_transaction -> complete_transaction (instead of a normal
518 * conclusion of the transaction), i.e. if this resource was already
519 * unregistered from the idr, the client reference will be dropped
520 * by release_client_resource and we must not drop it here.
Jay Fenlason45ee3192008-12-21 16:47:17 +0100521 */
Stefan Richterfb443032009-01-04 16:23:29 +0100522 if (!client->in_shutdown &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100523 idr_find(&client->resource_idr, e->r.resource.handle)) {
524 idr_remove(&client->resource_idr, e->r.resource.handle);
Stefan Richterfb443032009-01-04 16:23:29 +0100525 /* Drop the idr's reference */
526 client_put(client);
527 }
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500528 spin_unlock_irqrestore(&client->lock, flags);
529
Stefan Richter97c18b72009-01-04 16:23:29 +0100530 rsp->type = FW_CDEV_EVENT_RESPONSE;
531 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700532
533 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100534 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700535 * data, and the read is short, preserve an extra copy of the data
536 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
537 * for short reads and some apps depended on it, this is both safe
538 * and prudent for compatibility.
539 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100540 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
541 queue_event(client, &e->event, rsp, sizeof(*rsp),
542 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700543 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100544 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700545 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100546
547 /* Drop the transaction callback's reference */
548 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500549}
550
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100551static int init_request(struct client *client,
552 struct fw_cdev_send_request *request,
553 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500554{
Stefan Richter97c18b72009-01-04 16:23:29 +0100555 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100556 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500557
Stefan Richter18e9b10f2009-03-10 21:02:21 +0100558 if (request->tcode != TCODE_STREAM_DATA &&
559 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100560 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561
Stefan Richter97c18b72009-01-04 16:23:29 +0100562 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
563 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500564 return -ENOMEM;
565
Stefan Richter97c18b72009-01-04 16:23:29 +0100566 e->client = client;
567 e->response.length = request->length;
568 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500569
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400570 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100571 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400572 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100573 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100574 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100575 }
576
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100577 e->r.resource.release = release_transaction;
578 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
579 if (ret < 0)
580 goto failed;
581
582 /* Get a reference for the transaction callback */
583 client_get(client);
584
585 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100586 request->tcode, destination_id, request->generation,
587 speed, request->offset, e->response.data,
588 request->length, complete_transaction, e);
589 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100590
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100591 failed:
592 kfree(e);
593
594 return ret;
595}
596
Stefan Richter6e95dea2010-02-21 17:56:21 +0100597static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100598{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100599 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100600 case TCODE_WRITE_QUADLET_REQUEST:
601 case TCODE_WRITE_BLOCK_REQUEST:
602 case TCODE_READ_QUADLET_REQUEST:
603 case TCODE_READ_BLOCK_REQUEST:
604 case TCODE_LOCK_MASK_SWAP:
605 case TCODE_LOCK_COMPARE_SWAP:
606 case TCODE_LOCK_FETCH_ADD:
607 case TCODE_LOCK_LITTLE_ADD:
608 case TCODE_LOCK_BOUNDED_ADD:
609 case TCODE_LOCK_WRAP_ADD:
610 case TCODE_LOCK_VENDOR_DEPENDENT:
611 break;
612 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100613 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500614 }
615
Stefan Richter6e95dea2010-02-21 17:56:21 +0100616 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100617 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500618}
619
Stefan Richter281e2032010-01-24 16:45:03 +0100620static inline bool is_fcp_request(struct fw_request *request)
621{
622 return request == NULL;
623}
624
Stefan Richter53dca512008-12-14 21:47:04 +0100625static void release_request(struct client *client,
626 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400627{
Stefan Richter97c18b72009-01-04 16:23:29 +0100628 struct inbound_transaction_resource *r = container_of(resource,
629 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400630
Stefan Richter281e2032010-01-24 16:45:03 +0100631 if (is_fcp_request(r->request))
632 kfree(r->data);
633 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400634 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200635
636 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100637 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400638}
639
Stefan Richter97c18b72009-01-04 16:23:29 +0100640static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100641 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200642 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100643 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500644{
Stefan Richter97c18b72009-01-04 16:23:29 +0100645 struct address_handler_resource *handler = callback_data;
646 struct inbound_transaction_resource *r;
647 struct inbound_transaction_event *e;
Stefan Richter281e2032010-01-24 16:45:03 +0100648 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100649 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500650
Stefan Richter0244f572010-06-20 22:52:27 +0200651 /* card may be different from handler->client->device->card */
652 fw_card_get(card);
653
Stefan Richter97c18b72009-01-04 16:23:29 +0100654 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400655 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100656 if (r == NULL || e == NULL)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100657 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500658
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400659 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100660 r->request = request;
661 r->data = payload;
662 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500663
Stefan Richter281e2032010-01-24 16:45:03 +0100664 if (is_fcp_request(request)) {
665 /*
666 * FIXME: Let core-transaction.c manage a
667 * single reference-counted copy?
668 */
669 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
670 if (fcp_frame == NULL)
671 goto failed;
672
673 r->data = fcp_frame;
674 }
675
Stefan Richter97c18b72009-01-04 16:23:29 +0100676 r->resource.release = release_request;
677 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100678 if (ret < 0)
679 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500680
681 e->request.type = FW_CDEV_EVENT_REQUEST;
682 e->request.tcode = tcode;
683 e->request.offset = offset;
684 e->request.length = length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100685 e->request.handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500686 e->request.closure = handler->closure;
687
Stefan Richter97c18b72009-01-04 16:23:29 +0100688 queue_event(handler->client, &e->event,
Stefan Richter281e2032010-01-24 16:45:03 +0100689 &e->request, sizeof(e->request), r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100690 return;
691
692 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100693 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100694 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100695 kfree(fcp_frame);
696
697 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100698 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200699
700 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500701}
702
Stefan Richter53dca512008-12-14 21:47:04 +0100703static void release_address_handler(struct client *client,
704 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400705{
Stefan Richter97c18b72009-01-04 16:23:29 +0100706 struct address_handler_resource *r =
707 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400708
Stefan Richter97c18b72009-01-04 16:23:29 +0100709 fw_core_remove_address_handler(&r->handler);
710 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400711}
712
Stefan Richter6e95dea2010-02-21 17:56:21 +0100713static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500714{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100715 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100716 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500717 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100718 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500719
Stefan Richter97c18b72009-01-04 16:23:29 +0100720 r = kmalloc(sizeof(*r), GFP_KERNEL);
721 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500722 return -ENOMEM;
723
Stefan Richter6e95dea2010-02-21 17:56:21 +0100724 region.start = a->offset;
725 region.end = a->offset + a->length;
726 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100727 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100728 r->handler.callback_data = r;
729 r->closure = a->closure;
730 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731
Stefan Richter97c18b72009-01-04 16:23:29 +0100732 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100733 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100734 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100735 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500736 }
737
Stefan Richter97c18b72009-01-04 16:23:29 +0100738 r->resource.release = release_address_handler;
739 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100740 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100741 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100742 return ret;
743 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100744 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500745
746 return 0;
747}
748
Stefan Richter6e95dea2010-02-21 17:56:21 +0100749static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400750{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100751 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100752 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400753}
754
Stefan Richter6e95dea2010-02-21 17:56:21 +0100755static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500756{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100757 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400758 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100759 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200760 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500761
Stefan Richter6e95dea2010-02-21 17:56:21 +0100762 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100763 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500764 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100765
Stefan Richter97c18b72009-01-04 16:23:29 +0100766 r = container_of(resource, struct inbound_transaction_resource,
767 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100768 if (is_fcp_request(r->request))
769 goto out;
770
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200771 if (a->length != fw_get_response_length(r->request)) {
772 ret = -EINVAL;
773 kfree(r->request);
774 goto out;
775 }
776 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100777 ret = -EFAULT;
778 kfree(r->request);
779 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200780 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400781 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200782 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200783 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500784 kfree(r);
785
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200786 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500787}
788
Stefan Richter6e95dea2010-02-21 17:56:21 +0100789static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500790{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100791 return fw_core_initiate_bus_reset(client->device->card,
792 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500793}
794
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400795static void release_descriptor(struct client *client,
796 struct client_resource *resource)
797{
Stefan Richter97c18b72009-01-04 16:23:29 +0100798 struct descriptor_resource *r =
799 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400800
Stefan Richter97c18b72009-01-04 16:23:29 +0100801 fw_core_remove_descriptor(&r->descriptor);
802 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400803}
804
Stefan Richter6e95dea2010-02-21 17:56:21 +0100805static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200806{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100807 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100808 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100809 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200810
Stefan Richterde487da2009-03-10 21:00:23 +0100811 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200812 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100813 return -ENOSYS;
814
Stefan Richter6e95dea2010-02-21 17:56:21 +0100815 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200816 return -EINVAL;
817
Stefan Richter6e95dea2010-02-21 17:56:21 +0100818 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100819 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200820 return -ENOMEM;
821
Stefan Richter6e95dea2010-02-21 17:56:21 +0100822 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100823 ret = -EFAULT;
824 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200825 }
826
Stefan Richter6e95dea2010-02-21 17:56:21 +0100827 r->descriptor.length = a->length;
828 r->descriptor.immediate = a->immediate;
829 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100830 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200831
Stefan Richter97c18b72009-01-04 16:23:29 +0100832 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100833 if (ret < 0)
834 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200835
Stefan Richter97c18b72009-01-04 16:23:29 +0100836 r->resource.release = release_descriptor;
837 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100838 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100839 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100840 goto failed;
841 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100842 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200843
844 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100845 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100846 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100847
848 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200849}
850
Stefan Richter6e95dea2010-02-21 17:56:21 +0100851static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200852{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100853 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100854 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200855}
856
Stefan Richter53dca512008-12-14 21:47:04 +0100857static void iso_callback(struct fw_iso_context *context, u32 cycle,
858 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500859{
860 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100861 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500862
Stefan Richter56d04cb12010-06-08 00:20:10 +0200863 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richter97c18b72009-01-04 16:23:29 +0100864 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500865 return;
866
Stefan Richter97c18b72009-01-04 16:23:29 +0100867 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
868 e->interrupt.closure = client->iso_closure;
869 e->interrupt.cycle = cycle;
870 e->interrupt.header_length = header_length;
871 memcpy(e->interrupt.header, header, header_length);
872 queue_event(client, &e->event, &e->interrupt,
873 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500874}
875
Stefan Richter6e95dea2010-02-21 17:56:21 +0100876static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500877{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100878 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400879 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500880
Stefan Richter6e95dea2010-02-21 17:56:21 +0100881 if (a->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500882 return -EINVAL;
883
Stefan Richter6e95dea2010-02-21 17:56:21 +0100884 switch (a->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400885 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100886 if (a->header_size < 4 || (a->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400887 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400888 break;
889
890 case FW_ISO_CONTEXT_TRANSMIT:
Stefan Richter6e95dea2010-02-21 17:56:21 +0100891 if (a->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400892 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400893 break;
894
895 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500896 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400897 }
898
Stefan Richter6e95dea2010-02-21 17:56:21 +0100899 context = fw_iso_context_create(client->device->card, a->type,
900 a->channel, a->speed, a->header_size,
901 iso_callback, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400902 if (IS_ERR(context))
903 return PTR_ERR(context);
904
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200905 /* We only support one context at this time. */
906 spin_lock_irq(&client->lock);
907 if (client->iso_context != NULL) {
908 spin_unlock_irq(&client->lock);
909 fw_iso_context_destroy(context);
910 return -EBUSY;
911 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100912 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400913 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +0200914 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500915
Stefan Richter6e95dea2010-02-21 17:56:21 +0100916 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400917
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500918 return 0;
919}
920
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400921/* Macros for decoding the iso packet control header. */
922#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
923#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
924#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200925#define GET_TAG(v) (((v) >> 18) & 0x03)
926#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400927#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
928
Stefan Richter6e95dea2010-02-21 17:56:21 +0100929static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500930{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100931 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500932 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500933 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200934 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400935 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500936 int count;
937 struct {
938 struct fw_iso_packet packet;
939 u8 header[256];
940 } u;
941
Stefan Richter6e95dea2010-02-21 17:56:21 +0100942 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500943 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500944
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400945 /*
946 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500947 * the iso buffer, and the pointer points inside the buffer,
948 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500949 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500950 * payload_length == 0 through. In other words, if no packets
951 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +0100952 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400953 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500954
Stefan Richter6e95dea2010-02-21 17:56:21 +0100955 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200956 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100957 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200958 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500959 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200960 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500961 }
962
Stefan Richter6e95dea2010-02-21 17:56:21 +0100963 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Al Viro1ccc9142007-10-14 19:34:40 +0100964
Stefan Richter6e95dea2010-02-21 17:56:21 +0100965 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500966 return -EFAULT;
967
Stefan Richter6e95dea2010-02-21 17:56:21 +0100968 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500969 count = 0;
970 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400971 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500972 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400973 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
974 u.packet.interrupt = GET_INTERRUPT(control);
975 u.packet.skip = GET_SKIP(control);
976 u.packet.tag = GET_TAG(control);
977 u.packet.sy = GET_SY(control);
978 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500979
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500980 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +0200981 if (u.packet.header_length % 4 != 0)
982 return -EINVAL;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500983 header_length = u.packet.header_length;
984 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400985 /*
986 * We require that header_length is a multiple of
987 * the fixed header size, ctx->header_size.
988 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500989 if (ctx->header_size == 0) {
990 if (u.packet.header_length > 0)
991 return -EINVAL;
Clemens Ladisch4ba1d9c2010-03-31 16:26:39 +0200992 } else if (u.packet.header_length == 0 ||
993 u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500994 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500995 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500996 header_length = 0;
997 }
998
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500999 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001000 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001001 if (next > end)
1002 return -EINVAL;
1003 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001004 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001005 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001006 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001007 u.packet.header_length + u.packet.payload_length > 0)
1008 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001009 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001010 return -EINVAL;
1011
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001012 if (fw_iso_context_queue(ctx, &u.packet,
1013 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001014 break;
1015
1016 p = next;
1017 payload += u.packet.payload_length;
1018 count++;
1019 }
1020
Stefan Richter6e95dea2010-02-21 17:56:21 +01001021 a->size -= uptr_to_u64(p) - a->packets;
1022 a->packets = uptr_to_u64(p);
1023 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001024
1025 return count;
1026}
1027
Stefan Richter6e95dea2010-02-21 17:56:21 +01001028static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001029{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001030 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001031
Stefan Richter6e95dea2010-02-21 17:56:21 +01001032 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001033 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001034
Stefan Richter6e95dea2010-02-21 17:56:21 +01001035 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1036 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1037 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001038
Stefan Richter6e95dea2010-02-21 17:56:21 +01001039 return fw_iso_context_start(client->iso_context,
1040 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001041}
1042
Stefan Richter6e95dea2010-02-21 17:56:21 +01001043static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001044{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001045 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001046
Stefan Richter6e95dea2010-02-21 17:56:21 +01001047 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001048 return -EINVAL;
1049
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001050 return fw_iso_context_stop(client->iso_context);
1051}
1052
Stefan Richter6e95dea2010-02-21 17:56:21 +01001053static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001054{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001055 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001056 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001057 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001058 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001059 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001060
Stefan Richter4a9bde92010-02-20 22:24:43 +01001061 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001062
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001063 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001064
Stefan Richter6e95dea2010-02-21 17:56:21 +01001065 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001066 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1067 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1068 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1069 default:
1070 ret = -EINVAL;
1071 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001072
Stefan Richter4a9bde92010-02-20 22:24:43 +01001073 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001074
Stefan Richter6e95dea2010-02-21 17:56:21 +01001075 a->tv_sec = ts.tv_sec;
1076 a->tv_nsec = ts.tv_nsec;
1077 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001078
1079 return ret;
1080}
1081
Stefan Richter6e95dea2010-02-21 17:56:21 +01001082static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001083{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001084 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001085 struct fw_cdev_get_cycle_timer2 ct2;
1086
1087 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001088 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001089
Stefan Richter6e95dea2010-02-21 17:56:21 +01001090 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1091 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001092
Stefan Richtera64408b2007-09-29 10:41:58 +02001093 return 0;
1094}
1095
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001096static void iso_resource_work(struct work_struct *work)
1097{
1098 struct iso_resource_event *e;
1099 struct iso_resource *r =
1100 container_of(work, struct iso_resource, work.work);
1101 struct client *client = r->client;
1102 int generation, channel, bandwidth, todo;
1103 bool skip, free, success;
1104
1105 spin_lock_irq(&client->lock);
1106 generation = client->device->generation;
1107 todo = r->todo;
1108 /* Allow 1000ms grace period for other reallocations. */
1109 if (todo == ISO_RES_ALLOC &&
1110 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001111 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001112 skip = true;
1113 } else {
1114 /* We could be called twice within the same generation. */
1115 skip = todo == ISO_RES_REALLOC &&
1116 r->generation == generation;
1117 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001118 free = todo == ISO_RES_DEALLOC ||
1119 todo == ISO_RES_ALLOC_ONCE ||
1120 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001121 r->generation = generation;
1122 spin_unlock_irq(&client->lock);
1123
1124 if (skip)
1125 goto out;
1126
1127 bandwidth = r->bandwidth;
1128
1129 fw_iso_resource_manage(client->device->card, generation,
1130 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001131 todo == ISO_RES_ALLOC ||
1132 todo == ISO_RES_REALLOC ||
Stefan Richter6fdc0372009-06-20 13:23:59 +02001133 todo == ISO_RES_ALLOC_ONCE,
1134 r->transaction_data);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001135 /*
1136 * Is this generation outdated already? As long as this resource sticks
1137 * in the idr, it will be scheduled again for a newer generation or at
1138 * shutdown.
1139 */
1140 if (channel == -EAGAIN &&
1141 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1142 goto out;
1143
1144 success = channel >= 0 || bandwidth > 0;
1145
1146 spin_lock_irq(&client->lock);
1147 /*
1148 * Transit from allocation to reallocation, except if the client
1149 * requested deallocation in the meantime.
1150 */
1151 if (r->todo == ISO_RES_ALLOC)
1152 r->todo = ISO_RES_REALLOC;
1153 /*
1154 * Allocation or reallocation failure? Pull this resource out of the
1155 * idr and prepare for deletion, unless the client is shutting down.
1156 */
1157 if (r->todo == ISO_RES_REALLOC && !success &&
1158 !client->in_shutdown &&
1159 idr_find(&client->resource_idr, r->resource.handle)) {
1160 idr_remove(&client->resource_idr, r->resource.handle);
1161 client_put(client);
1162 free = true;
1163 }
1164 spin_unlock_irq(&client->lock);
1165
1166 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001167 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001168
1169 if (todo == ISO_RES_REALLOC && success)
1170 goto out;
1171
Stefan Richter1ec3c022009-01-04 16:23:29 +01001172 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001173 e = r->e_alloc;
1174 r->e_alloc = NULL;
1175 } else {
1176 e = r->e_dealloc;
1177 r->e_dealloc = NULL;
1178 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001179 e->iso_resource.handle = r->resource.handle;
1180 e->iso_resource.channel = channel;
1181 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001182
1183 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001184 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001185
1186 if (free) {
1187 cancel_delayed_work(&r->work);
1188 kfree(r->e_alloc);
1189 kfree(r->e_dealloc);
1190 kfree(r);
1191 }
1192 out:
1193 client_put(client);
1194}
1195
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001196static void release_iso_resource(struct client *client,
1197 struct client_resource *resource)
1198{
1199 struct iso_resource *r =
1200 container_of(resource, struct iso_resource, resource);
1201
1202 spin_lock_irq(&client->lock);
1203 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001204 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001205 spin_unlock_irq(&client->lock);
1206}
1207
Stefan Richter1ec3c022009-01-04 16:23:29 +01001208static int init_iso_resource(struct client *client,
1209 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001210{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001211 struct iso_resource_event *e1, *e2;
1212 struct iso_resource *r;
1213 int ret;
1214
1215 if ((request->channels == 0 && request->bandwidth == 0) ||
1216 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1217 request->bandwidth < 0)
1218 return -EINVAL;
1219
1220 r = kmalloc(sizeof(*r), GFP_KERNEL);
1221 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1222 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1223 if (r == NULL || e1 == NULL || e2 == NULL) {
1224 ret = -ENOMEM;
1225 goto fail;
1226 }
1227
1228 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1229 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001230 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001231 r->generation = -1;
1232 r->channels = request->channels;
1233 r->bandwidth = request->bandwidth;
1234 r->e_alloc = e1;
1235 r->e_dealloc = e2;
1236
Stefan Richtere21fcf72009-10-08 00:41:38 +02001237 e1->iso_resource.closure = request->closure;
1238 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1239 e2->iso_resource.closure = request->closure;
1240 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001241
Stefan Richter1ec3c022009-01-04 16:23:29 +01001242 if (todo == ISO_RES_ALLOC) {
1243 r->resource.release = release_iso_resource;
1244 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001245 if (ret < 0)
1246 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001247 } else {
1248 r->resource.release = NULL;
1249 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001250 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001251 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001252 request->handle = r->resource.handle;
1253
1254 return 0;
1255 fail:
1256 kfree(r);
1257 kfree(e1);
1258 kfree(e2);
1259
1260 return ret;
1261}
1262
Stefan Richter6e95dea2010-02-21 17:56:21 +01001263static int ioctl_allocate_iso_resource(struct client *client,
1264 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001265{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001266 return init_iso_resource(client,
1267 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001268}
1269
Stefan Richter6e95dea2010-02-21 17:56:21 +01001270static int ioctl_deallocate_iso_resource(struct client *client,
1271 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001272{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001273 return release_client_resource(client,
1274 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001275}
1276
Stefan Richter6e95dea2010-02-21 17:56:21 +01001277static int ioctl_allocate_iso_resource_once(struct client *client,
1278 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001279{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001280 return init_iso_resource(client,
1281 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001282}
1283
Stefan Richter6e95dea2010-02-21 17:56:21 +01001284static int ioctl_deallocate_iso_resource_once(struct client *client,
1285 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001286{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001287 return init_iso_resource(client,
1288 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001289}
1290
Stefan Richterc8a25902009-03-10 20:59:16 +01001291/*
1292 * Returns a speed code: Maximum speed to or from this device,
1293 * limited by the device's link speed, the local node's link speed,
1294 * and all PHY port speeds between the two links.
1295 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001296static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001297{
Stefan Richterc8a25902009-03-10 20:59:16 +01001298 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001299}
1300
Stefan Richter6e95dea2010-02-21 17:56:21 +01001301static int ioctl_send_broadcast_request(struct client *client,
1302 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001303{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001304 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001305
Stefan Richter6e95dea2010-02-21 17:56:21 +01001306 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001307 case TCODE_WRITE_QUADLET_REQUEST:
1308 case TCODE_WRITE_BLOCK_REQUEST:
1309 break;
1310 default:
1311 return -EINVAL;
1312 }
1313
Stefan Richter1566f3d2009-01-04 16:23:29 +01001314 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001315 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001316 return -EACCES;
1317
Stefan Richter6e95dea2010-02-21 17:56:21 +01001318 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001319}
1320
Stefan Richter6e95dea2010-02-21 17:56:21 +01001321static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001322{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001323 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001324 struct fw_cdev_send_request request;
1325 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001326
Stefan Richter6e95dea2010-02-21 17:56:21 +01001327 if (a->speed > client->device->card->link_speed ||
1328 a->length > 1024 << a->speed)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001329 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001330
Stefan Richter6e95dea2010-02-21 17:56:21 +01001331 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001332 return -EINVAL;
1333
Stefan Richter6e95dea2010-02-21 17:56:21 +01001334 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001335 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001336 request.length = a->length;
1337 request.closure = a->closure;
1338 request.data = a->data;
1339 request.generation = a->generation;
Stefan Richter18e9b10f2009-03-10 21:02:21 +01001340
Stefan Richter6e95dea2010-02-21 17:56:21 +01001341 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001342}
1343
Stefan Richter6e95dea2010-02-21 17:56:21 +01001344static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001345 ioctl_get_info,
1346 ioctl_send_request,
1347 ioctl_allocate,
1348 ioctl_deallocate,
1349 ioctl_send_response,
1350 ioctl_initiate_bus_reset,
1351 ioctl_add_descriptor,
1352 ioctl_remove_descriptor,
1353 ioctl_create_iso_context,
1354 ioctl_queue_iso,
1355 ioctl_start_iso,
1356 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +02001357 ioctl_get_cycle_timer,
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001358 ioctl_allocate_iso_resource,
1359 ioctl_deallocate_iso_resource,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001360 ioctl_allocate_iso_resource_once,
1361 ioctl_deallocate_iso_resource_once,
Stefan Richter33580a32009-01-04 16:23:29 +01001362 ioctl_get_speed,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001363 ioctl_send_broadcast_request,
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001364 ioctl_send_stream_packet,
Stefan Richterabfe5a02010-02-20 12:13:49 +01001365 ioctl_get_cycle_timer2,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001366};
1367
Stefan Richter53dca512008-12-14 21:47:04 +01001368static int dispatch_ioctl(struct client *client,
1369 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001370{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001371 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001372 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001373
Stefan Richter64582292010-02-21 17:56:42 +01001374 if (fw_device_is_shutdown(client->device))
1375 return -ENODEV;
1376
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001377 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001378 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1379 _IOC_SIZE(cmd) > sizeof(buffer))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001380 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001381
Stefan Richter9cac00b2010-04-07 08:30:50 +02001382 if (_IOC_DIR(cmd) == _IOC_READ)
1383 memset(&buffer, 0, _IOC_SIZE(cmd));
1384
1385 if (_IOC_DIR(cmd) & _IOC_WRITE)
1386 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001387 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001388
Stefan Richter6e95dea2010-02-21 17:56:21 +01001389 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001390 if (ret < 0)
1391 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001392
Stefan Richter9cac00b2010-04-07 08:30:50 +02001393 if (_IOC_DIR(cmd) & _IOC_READ)
1394 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001395 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001396
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001397 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001398}
1399
Stefan Richter53dca512008-12-14 21:47:04 +01001400static long fw_device_op_ioctl(struct file *file,
1401 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001402{
Stefan Richter64582292010-02-21 17:56:42 +01001403 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001404}
1405
1406#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001407static long fw_device_op_compat_ioctl(struct file *file,
1408 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001409{
Stefan Richter64582292010-02-21 17:56:42 +01001410 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001411}
1412#endif
1413
1414static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1415{
1416 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001417 enum dma_data_direction direction;
1418 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001419 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001420
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001421 if (fw_device_is_shutdown(client->device))
1422 return -ENODEV;
1423
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001424 /* FIXME: We could support multiple buffers, but we don't. */
1425 if (client->buffer.pages != NULL)
1426 return -EBUSY;
1427
1428 if (!(vma->vm_flags & VM_SHARED))
1429 return -EINVAL;
1430
1431 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001432 return -EINVAL;
1433
1434 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001435 size = vma->vm_end - vma->vm_start;
1436 page_count = size >> PAGE_SHIFT;
1437 if (size & ~PAGE_MASK)
1438 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001439
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001440 if (vma->vm_flags & VM_WRITE)
1441 direction = DMA_TO_DEVICE;
1442 else
1443 direction = DMA_FROM_DEVICE;
1444
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001445 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1446 page_count, direction);
1447 if (ret < 0)
1448 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001449
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001450 ret = fw_iso_buffer_map(&client->buffer, vma);
1451 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001452 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1453
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001454 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001455}
1456
Jay Fenlason45ee3192008-12-21 16:47:17 +01001457static int shutdown_resource(int id, void *p, void *data)
1458{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001459 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001460 struct client *client = data;
1461
Stefan Richtere21fcf72009-10-08 00:41:38 +02001462 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001463 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001464
1465 return 0;
1466}
1467
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001468static int fw_device_op_release(struct inode *inode, struct file *file)
1469{
1470 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001471 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001472
Stefan Richter97811e32008-12-14 19:19:23 +01001473 mutex_lock(&client->device->client_list_mutex);
1474 list_del(&client->link);
1475 mutex_unlock(&client->device->client_list_mutex);
1476
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001477 if (client->iso_context)
1478 fw_iso_context_destroy(client->iso_context);
1479
Stefan Richter36a755c2009-01-05 20:28:10 +01001480 if (client->buffer.pages)
1481 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1482
Jay Fenlason45ee3192008-12-21 16:47:17 +01001483 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001484 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001485 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001486 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001487
Jay Fenlason45ee3192008-12-21 16:47:17 +01001488 idr_for_each(&client->resource_idr, shutdown_resource, client);
1489 idr_remove_all(&client->resource_idr);
1490 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001491
Stefan Richtere21fcf72009-10-08 00:41:38 +02001492 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1493 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001494
Stefan Richterfb443032009-01-04 16:23:29 +01001495 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001496
1497 return 0;
1498}
1499
1500static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1501{
1502 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001503 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001504
1505 poll_wait(file, &client->wait, pt);
1506
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001507 if (fw_device_is_shutdown(client->device))
1508 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001509 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001510 mask |= POLLIN | POLLRDNORM;
1511
1512 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001513}
1514
Stefan Richter21ebcd12007-01-14 15:29:07 +01001515const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001516 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001517 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001518 .open = fw_device_op_open,
1519 .read = fw_device_op_read,
1520 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001521 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001522 .release = fw_device_op_release,
1523 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001524#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001525 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001526#endif
1527};