blob: 3c075b2eedf94be31fde6850abdc295b2d3e780a [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
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/wait.h>
24#include <linux/errno.h>
25#include <linux/device.h>
26#include <linux/vmalloc.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020027#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050028#include <linux/poll.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020029#include <linux/preempt.h>
30#include <linux/time.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040031#include <linux/spinlock.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050032#include <linux/delay.h>
33#include <linux/mm.h>
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -050034#include <linux/idr.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050035#include <linux/compat.h>
Kristian Høgsberg9640d3d2007-04-30 15:03:15 -040036#include <linux/firewire-cdev.h>
Stefan Richtera64408b2007-09-29 10:41:58 +020037#include <asm/system.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050038#include <asm/uaccess.h>
39#include "fw-transaction.h"
40#include "fw-topology.h"
41#include "fw-device.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050042
Kristian Høgsberg3964a442007-03-27 01:43:41 -040043struct client;
Jay Fenlason45ee3192008-12-21 16:47:17 +010044struct client_resource;
45typedef void (*client_resource_release_fn_t)(struct client *,
46 struct client_resource *);
Kristian Høgsberg3964a442007-03-27 01:43:41 -040047struct client_resource {
Jay Fenlason45ee3192008-12-21 16:47:17 +010048 client_resource_release_fn_t release;
49 int handle;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040050};
51
Kristian Høgsbergc781c062007-05-07 20:33:32 -040052/*
53 * dequeue_event() just kfree()'s the event, so the event has to be
54 * the first field in the struct.
55 */
56
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050057struct event {
58 struct { void *data; size_t size; } v[2];
59 struct list_head link;
60};
61
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050062struct bus_reset {
63 struct event event;
64 struct fw_cdev_event_bus_reset reset;
65};
66
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050067struct response {
68 struct event event;
69 struct fw_transaction transaction;
70 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -040071 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050072 struct fw_cdev_event_response response;
73};
74
75struct iso_interrupt {
76 struct event event;
77 struct fw_cdev_event_iso_interrupt interrupt;
78};
79
80struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050081 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050082 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010083
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050084 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010085 bool in_shutdown;
86 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050087 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050088 wait_queue_head_t wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040089 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050090
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050091 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040092 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050093 struct fw_iso_buffer buffer;
94 unsigned long vm_start;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050095
96 struct list_head link;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050097};
98
Stefan Richter53dca512008-12-14 21:47:04 +010099static inline void __user *u64_to_uptr(__u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500100{
101 return (void __user *)(unsigned long)value;
102}
103
Stefan Richter53dca512008-12-14 21:47:04 +0100104static inline __u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500105{
106 return (__u64)(unsigned long)ptr;
107}
108
109static int fw_device_op_open(struct inode *inode, struct file *file)
110{
111 struct fw_device *device;
112 struct client *client;
113
Stefan Richter96b19062008-02-02 15:01:09 +0100114 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500115 if (device == NULL)
116 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500117
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400118 if (fw_device_is_shutdown(device)) {
119 fw_device_put(device);
120 return -ENODEV;
121 }
122
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400123 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100124 if (client == NULL) {
125 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500126 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100127 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500128
Stefan Richter96b19062008-02-02 15:01:09 +0100129 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500130 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100131 idr_init(&client->resource_idr);
132 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500133 init_waitqueue_head(&client->wait);
134
135 file->private_data = client;
136
Stefan Richterd67cfb92008-10-05 10:37:11 +0200137 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500138 list_add_tail(&client->link, &device->client_list);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200139 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500140
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500141 return 0;
142}
143
144static void queue_event(struct client *client, struct event *event,
145 void *data0, size_t size0, void *data1, size_t size1)
146{
147 unsigned long flags;
148
149 event->v[0].data = data0;
150 event->v[0].size = size0;
151 event->v[1].data = data1;
152 event->v[1].size = size1;
153
154 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100155 if (client->in_shutdown)
156 kfree(event);
157 else
158 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500159 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400160
161 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500162}
163
Stefan Richter53dca512008-12-14 21:47:04 +0100164static int dequeue_event(struct client *client,
165 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500166{
167 unsigned long flags;
168 struct event *event;
169 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100170 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500171
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100172 ret = wait_event_interruptible(client->wait,
173 !list_empty(&client->event_list) ||
174 fw_device_is_shutdown(client->device));
175 if (ret < 0)
176 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500177
178 if (list_empty(&client->event_list) &&
179 fw_device_is_shutdown(client->device))
180 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500181
182 spin_lock_irqsave(&client->lock, flags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500183 event = container_of(client->event_list.next, struct event, link);
184 list_del(&event->link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500185 spin_unlock_irqrestore(&client->lock, flags);
186
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500187 total = 0;
188 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
189 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500190 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100191 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500192 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500193 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500194 total += size;
195 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100196 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500197
198 out:
199 kfree(event);
200
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100201 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500202}
203
Stefan Richter53dca512008-12-14 21:47:04 +0100204static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
205 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500206{
207 struct client *client = file->private_data;
208
209 return dequeue_event(client, buffer, count);
210}
211
Stefan Richter53dca512008-12-14 21:47:04 +0100212static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
213 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500214{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400215 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400216 unsigned long flags;
217
218 spin_lock_irqsave(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500219
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400220 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500221 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100222 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400223 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500224 event->local_node_id = card->local_node->node_id;
225 event->bm_node_id = 0; /* FIXME: We don't track the BM. */
226 event->irm_node_id = card->irm_node->node_id;
227 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400228
229 spin_unlock_irqrestore(&card->lock, flags);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500230}
231
Stefan Richter53dca512008-12-14 21:47:04 +0100232static void for_each_client(struct fw_device *device,
233 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500234{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500235 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500236
Stefan Richterd67cfb92008-10-05 10:37:11 +0200237 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500238 list_for_each_entry(c, &device->client_list, link)
239 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200240 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500241}
242
Stefan Richter53dca512008-12-14 21:47:04 +0100243static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500244{
245 struct bus_reset *bus_reset;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500246
Stefan Richterd67cfb92008-10-05 10:37:11 +0200247 bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500248 if (bus_reset == NULL) {
249 fw_notify("Out of memory when allocating bus reset event\n");
250 return;
251 }
252
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400253 fill_bus_reset_event(&bus_reset->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500254
255 queue_event(client, &bus_reset->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400256 &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500257}
258
259void fw_device_cdev_update(struct fw_device *device)
260{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500261 for_each_client(device, queue_bus_reset_event);
262}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500263
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500264static void wake_up_client(struct client *client)
265{
266 wake_up_interruptible(&client->wait);
267}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500268
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500269void fw_device_cdev_remove(struct fw_device *device)
270{
271 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500272}
273
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400274static int ioctl_get_info(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500275{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400276 struct fw_cdev_get_info *get_info = buffer;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500277 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100278 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500279
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400280 client->version = get_info->version;
281 get_info->version = FW_CDEV_VERSION;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400282 get_info->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500283
Stefan Richterc9755e12008-03-24 20:54:28 +0100284 down_read(&fw_device_rwsem);
285
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400286 if (get_info->rom != 0) {
287 void __user *uptr = u64_to_uptr(get_info->rom);
288 size_t want = get_info->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100289 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500290
Stefan Richterc9755e12008-03-24 20:54:28 +0100291 ret = copy_to_user(uptr, client->device->config_rom,
292 min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500293 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400294 get_info->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500295
Stefan Richterc9755e12008-03-24 20:54:28 +0100296 up_read(&fw_device_rwsem);
297
298 if (ret != 0)
299 return -EFAULT;
300
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400301 client->bus_reset_closure = get_info->bus_reset_closure;
302 if (get_info->bus_reset != 0) {
303 void __user *uptr = u64_to_uptr(get_info->bus_reset);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500304
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400305 fill_bus_reset_event(&bus_reset, client);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400306 if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500307 return -EFAULT;
308 }
309
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500310 return 0;
311}
312
Stefan Richter53dca512008-12-14 21:47:04 +0100313static int add_client_resource(struct client *client,
314 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400315{
316 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100317 int ret;
318
319 retry:
320 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
321 return -ENOMEM;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400322
323 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100324 if (client->in_shutdown)
325 ret = -ECANCELED;
326 else
327 ret = idr_get_new(&client->resource_idr, resource,
328 &resource->handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400329 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100330
331 if (ret == -EAGAIN)
332 goto retry;
333
334 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400335}
336
Stefan Richter53dca512008-12-14 21:47:04 +0100337static int release_client_resource(struct client *client, u32 handle,
338 client_resource_release_fn_t release,
339 struct client_resource **resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400340{
341 struct client_resource *r;
342 unsigned long flags;
343
344 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100345 if (client->in_shutdown)
346 r = NULL;
347 else
348 r = idr_find(&client->resource_idr, handle);
349 if (r && r->release == release)
350 idr_remove(&client->resource_idr, handle);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400351 spin_unlock_irqrestore(&client->lock, flags);
352
Jay Fenlason45ee3192008-12-21 16:47:17 +0100353 if (!(r && r->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400354 return -EINVAL;
355
356 if (resource)
357 *resource = r;
358 else
359 r->release(client, r);
360
361 return 0;
362}
363
Stefan Richter53dca512008-12-14 21:47:04 +0100364static void release_transaction(struct client *client,
365 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400366{
367 struct response *response =
368 container_of(resource, struct response, resource);
369
370 fw_cancel_transaction(client->device->card, &response->transaction);
371}
372
Stefan Richter53dca512008-12-14 21:47:04 +0100373static void complete_transaction(struct fw_card *card, int rcode,
374 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500375{
376 struct response *response = data;
377 struct client *client = response->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500378 unsigned long flags;
David Moore8401d922008-07-29 23:46:25 -0700379 struct fw_cdev_event_response *r = &response->response;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500380
David Moore8401d922008-07-29 23:46:25 -0700381 if (length < r->length)
382 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500383 if (rcode == RCODE_COMPLETE)
David Moore8401d922008-07-29 23:46:25 -0700384 memcpy(r->data, payload, r->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500385
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500386 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100387 /*
388 * If called while in shutdown, the idr tree must be left untouched.
389 * The idr handle will be removed later.
390 */
391 if (!client->in_shutdown)
392 idr_remove(&client->resource_idr, response->resource.handle);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500393 spin_unlock_irqrestore(&client->lock, flags);
394
David Moore8401d922008-07-29 23:46:25 -0700395 r->type = FW_CDEV_EVENT_RESPONSE;
396 r->rcode = rcode;
397
398 /*
399 * In the case that sizeof(*r) doesn't align with the position of the
400 * data, and the read is short, preserve an extra copy of the data
401 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
402 * for short reads and some apps depended on it, this is both safe
403 * and prudent for compatibility.
404 */
405 if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
406 queue_event(client, &response->event, r, sizeof(*r),
407 r->data, r->length);
408 else
409 queue_event(client, &response->event, r, sizeof(*r) + r->length,
410 NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500411}
412
Jeff Garzik350958f2007-05-27 07:09:18 -0400413static int ioctl_send_request(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500414{
415 struct fw_device *device = client->device;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400416 struct fw_cdev_send_request *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500417 struct response *response;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100418 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500419
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500420 /* What is the biggest size we'll accept, really? */
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400421 if (request->length > 4096)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500422 return -EINVAL;
423
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400424 response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500425 if (response == NULL)
426 return -ENOMEM;
427
428 response->client = client;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400429 response->response.length = request->length;
430 response->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500431
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400432 if (request->data &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500433 copy_from_user(response->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400434 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100435 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100436 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100437 }
438
439 switch (request->tcode) {
440 case TCODE_WRITE_QUADLET_REQUEST:
441 case TCODE_WRITE_BLOCK_REQUEST:
442 case TCODE_READ_QUADLET_REQUEST:
443 case TCODE_READ_BLOCK_REQUEST:
444 case TCODE_LOCK_MASK_SWAP:
445 case TCODE_LOCK_COMPARE_SWAP:
446 case TCODE_LOCK_FETCH_ADD:
447 case TCODE_LOCK_LITTLE_ADD:
448 case TCODE_LOCK_BOUNDED_ADD:
449 case TCODE_LOCK_WRAP_ADD:
450 case TCODE_LOCK_VENDOR_DEPENDENT:
451 break;
452 default:
453 ret = -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100454 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500455 }
456
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400457 response->resource.release = release_transaction;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100458 ret = add_client_resource(client, &response->resource, GFP_KERNEL);
459 if (ret < 0)
460 goto failed;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500461
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500462 fw_send_request(device->card, &response->transaction,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400463 request->tcode & 0x1f,
Stefan Richter907293d2007-01-23 21:11:43 +0100464 device->node->node_id,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400465 request->generation,
Stefan Richterf1397492007-06-10 21:31:36 +0200466 device->max_speed,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400467 request->offset,
468 response->response.data, request->length,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500469 complete_transaction, response);
470
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400471 if (request->data)
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400472 return sizeof(request) + request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500473 else
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400474 return sizeof(request);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100475 failed:
Stefan Richter1f3125a2008-12-05 22:44:42 +0100476 kfree(response);
477
478 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500479}
480
481struct address_handler {
482 struct fw_address_handler handler;
483 __u64 closure;
484 struct client *client;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400485 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500486};
487
488struct request {
489 struct fw_request *request;
490 void *data;
491 size_t length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400492 struct client_resource resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500493};
494
495struct request_event {
496 struct event event;
497 struct fw_cdev_event_request request;
498};
499
Stefan Richter53dca512008-12-14 21:47:04 +0100500static void release_request(struct client *client,
501 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400502{
503 struct request *request =
504 container_of(resource, struct request, resource);
505
506 fw_send_response(client->device->card, request->request,
507 RCODE_CONFLICT_ERROR);
508 kfree(request);
509}
510
Stefan Richter53dca512008-12-14 21:47:04 +0100511static void handle_request(struct fw_card *card, struct fw_request *r,
512 int tcode, int destination, int source,
513 int generation, int speed,
514 unsigned long long offset,
515 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500516{
517 struct address_handler *handler = callback_data;
518 struct request *request;
519 struct request_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500520 struct client *client = handler->client;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100521 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500522
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400523 request = kmalloc(sizeof(*request), GFP_ATOMIC);
524 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100525 if (request == NULL || e == NULL)
526 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500527
528 request->request = r;
529 request->data = payload;
530 request->length = length;
531
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400532 request->resource.release = release_request;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100533 ret = add_client_resource(client, &request->resource, GFP_ATOMIC);
534 if (ret < 0)
535 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500536
537 e->request.type = FW_CDEV_EVENT_REQUEST;
538 e->request.tcode = tcode;
539 e->request.offset = offset;
540 e->request.length = length;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400541 e->request.handle = request->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500542 e->request.closure = handler->closure;
543
544 queue_event(client, &e->event,
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400545 &e->request, sizeof(e->request), payload, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100546 return;
547
548 failed:
549 kfree(request);
550 kfree(e);
551 fw_send_response(card, r, RCODE_CONFLICT_ERROR);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500552}
553
Stefan Richter53dca512008-12-14 21:47:04 +0100554static void release_address_handler(struct client *client,
555 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400556{
557 struct address_handler *handler =
558 container_of(resource, struct address_handler, resource);
559
560 fw_core_remove_address_handler(&handler->handler);
561 kfree(handler);
562}
563
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400564static int ioctl_allocate(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500565{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400566 struct fw_cdev_allocate *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500567 struct address_handler *handler;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500568 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100569 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500570
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400571 handler = kmalloc(sizeof(*handler), GFP_KERNEL);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500572 if (handler == NULL)
573 return -ENOMEM;
574
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400575 region.start = request->offset;
576 region.end = request->offset + request->length;
577 handler->handler.length = request->length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500578 handler->handler.address_callback = handle_request;
579 handler->handler.callback_data = handler;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400580 handler->closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500581 handler->client = client;
582
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100583 ret = fw_core_add_address_handler(&handler->handler, &region);
584 if (ret < 0) {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500585 kfree(handler);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100586 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500587 }
588
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400589 handler->resource.release = release_address_handler;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100590 ret = add_client_resource(client, &handler->resource, GFP_KERNEL);
591 if (ret < 0) {
592 release_address_handler(client, &handler->resource);
593 return ret;
594 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400595 request->handle = handler->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500596
597 return 0;
598}
599
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400600static int ioctl_deallocate(struct client *client, void *buffer)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400601{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400602 struct fw_cdev_deallocate *request = buffer;
Kristian Høgsberg94723162007-03-14 17:34:55 -0400603
Jay Fenlason45ee3192008-12-21 16:47:17 +0100604 return release_client_resource(client, request->handle,
605 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400606}
607
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400608static int ioctl_send_response(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500609{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400610 struct fw_cdev_send_response *request = buffer;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400611 struct client_resource *resource;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500612 struct request *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500613
Jay Fenlason45ee3192008-12-21 16:47:17 +0100614 if (release_client_resource(client, request->handle,
615 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500616 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100617
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400618 r = container_of(resource, struct request, resource);
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400619 if (request->length < r->length)
620 r->length = request->length;
621 if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500622 return -EFAULT;
623
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400624 fw_send_response(client->device->card, r->request, request->rcode);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500625 kfree(r);
626
627 return 0;
628}
629
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400630static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500631{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400632 struct fw_cdev_initiate_bus_reset *request = buffer;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500633 int short_reset;
634
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400635 short_reset = (request->type == FW_CDEV_SHORT_RESET);
Kristian Høgsberg53718422007-03-07 12:12:42 -0500636
637 return fw_core_initiate_bus_reset(client->device->card, short_reset);
638}
639
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200640struct descriptor {
641 struct fw_descriptor d;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400642 struct client_resource resource;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200643 u32 data[0];
644};
645
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400646static void release_descriptor(struct client *client,
647 struct client_resource *resource)
648{
649 struct descriptor *descriptor =
650 container_of(resource, struct descriptor, resource);
651
652 fw_core_remove_descriptor(&descriptor->d);
653 kfree(descriptor);
654}
655
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400656static int ioctl_add_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200657{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400658 struct fw_cdev_add_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200659 struct descriptor *descriptor;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100660 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200661
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400662 if (request->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200663 return -EINVAL;
664
665 descriptor =
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400666 kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200667 if (descriptor == NULL)
668 return -ENOMEM;
669
670 if (copy_from_user(descriptor->data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400671 u64_to_uptr(request->data), request->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100672 ret = -EFAULT;
673 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200674 }
675
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400676 descriptor->d.length = request->length;
677 descriptor->d.immediate = request->immediate;
678 descriptor->d.key = request->key;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200679 descriptor->d.data = descriptor->data;
680
Jay Fenlason45ee3192008-12-21 16:47:17 +0100681 ret = fw_core_add_descriptor(&descriptor->d);
682 if (ret < 0)
683 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200684
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400685 descriptor->resource.release = release_descriptor;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100686 ret = add_client_resource(client, &descriptor->resource, GFP_KERNEL);
687 if (ret < 0) {
688 fw_core_remove_descriptor(&descriptor->d);
689 goto failed;
690 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400691 request->handle = descriptor->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200692
693 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100694 failed:
695 kfree(descriptor);
696
697 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200698}
699
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400700static int ioctl_remove_descriptor(struct client *client, void *buffer)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200701{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400702 struct fw_cdev_remove_descriptor *request = buffer;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200703
Jay Fenlason45ee3192008-12-21 16:47:17 +0100704 return release_client_resource(client, request->handle,
705 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200706}
707
Stefan Richter53dca512008-12-14 21:47:04 +0100708static void iso_callback(struct fw_iso_context *context, u32 cycle,
709 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500710{
711 struct client *client = data;
Stefan Richter930e4b72007-08-03 20:56:31 +0200712 struct iso_interrupt *irq;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500713
Stefan Richter930e4b72007-08-03 20:56:31 +0200714 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
715 if (irq == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500716 return;
717
Stefan Richter930e4b72007-08-03 20:56:31 +0200718 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
719 irq->interrupt.closure = client->iso_closure;
720 irq->interrupt.cycle = cycle;
721 irq->interrupt.header_length = header_length;
722 memcpy(irq->interrupt.header, header, header_length);
723 queue_event(client, &irq->event, &irq->interrupt,
724 sizeof(irq->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500725}
726
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400727static int ioctl_create_iso_context(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500728{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400729 struct fw_cdev_create_iso_context *request = buffer;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400730 struct fw_iso_context *context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500731
Stefan Richterfae60312008-02-20 21:10:06 +0100732 /* We only support one context at this time. */
733 if (client->iso_context != NULL)
734 return -EBUSY;
735
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400736 if (request->channel > 63)
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500737 return -EINVAL;
738
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400739 switch (request->type) {
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400740 case FW_ISO_CONTEXT_RECEIVE:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400741 if (request->header_size < 4 || (request->header_size & 3))
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400742 return -EINVAL;
743
744 break;
745
746 case FW_ISO_CONTEXT_TRANSMIT:
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400747 if (request->speed > SCODE_3200)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400748 return -EINVAL;
749
750 break;
751
752 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500753 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400754 }
755
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400756 context = fw_iso_context_create(client->device->card,
757 request->type,
758 request->channel,
759 request->speed,
760 request->header_size,
761 iso_callback, client);
762 if (IS_ERR(context))
763 return PTR_ERR(context);
764
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400765 client->iso_closure = request->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400766 client->iso_context = context;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500767
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400768 /* We only support one context at this time. */
769 request->handle = 0;
770
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500771 return 0;
772}
773
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400774/* Macros for decoding the iso packet control header. */
775#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
776#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
777#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +0200778#define GET_TAG(v) (((v) >> 18) & 0x03)
779#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400780#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
781
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400782static int ioctl_queue_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500783{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400784 struct fw_cdev_queue_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500785 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500786 struct fw_iso_context *ctx = client->iso_context;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200787 unsigned long payload, buffer_end, header_length;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400788 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500789 int count;
790 struct {
791 struct fw_iso_packet packet;
792 u8 header[256];
793 } u;
794
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400795 if (ctx == NULL || request->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500796 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500797
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400798 /*
799 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500800 * the iso buffer, and the pointer points inside the buffer,
801 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500802 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803 * payload_length == 0 through. In other words, if no packets
804 * use the indirect payload, the iso buffer need not be mapped
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400805 * and the request->data pointer is ignored.
806 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500807
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400808 payload = (unsigned long)request->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200809 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400810 if (request->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200811 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -0500812 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200813 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500814 }
815
Al Viro1ccc9142007-10-14 19:34:40 +0100816 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
817
818 if (!access_ok(VERIFY_READ, p, request->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500819 return -EFAULT;
820
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400821 end = (void __user *)p + request->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500822 count = 0;
823 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400824 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500825 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -0400826 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
827 u.packet.interrupt = GET_INTERRUPT(control);
828 u.packet.skip = GET_SKIP(control);
829 u.packet.tag = GET_TAG(control);
830 u.packet.sy = GET_SY(control);
831 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500832
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500833 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500834 header_length = u.packet.header_length;
835 } else {
Kristian Høgsbergc781c062007-05-07 20:33:32 -0400836 /*
837 * We require that header_length is a multiple of
838 * the fixed header size, ctx->header_size.
839 */
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500840 if (ctx->header_size == 0) {
841 if (u.packet.header_length > 0)
842 return -EINVAL;
843 } else if (u.packet.header_length % ctx->header_size != 0) {
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500844 return -EINVAL;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500845 }
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500846 header_length = 0;
847 }
848
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500849 next = (struct fw_cdev_iso_packet __user *)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500850 &p->header[header_length / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500851 if (next > end)
852 return -EINVAL;
853 if (__copy_from_user
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -0500854 (u.packet.header, p->header, header_length))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -0500856 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500857 u.packet.header_length + u.packet.payload_length > 0)
858 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +0200859 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500860 return -EINVAL;
861
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -0500862 if (fw_iso_context_queue(ctx, &u.packet,
863 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500864 break;
865
866 p = next;
867 payload += u.packet.payload_length;
868 count++;
869 }
870
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400871 request->size -= uptr_to_u64(p) - request->packets;
872 request->packets = uptr_to_u64(p);
873 request->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500874
875 return count;
876}
877
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400878static int ioctl_start_iso(struct client *client, void *buffer)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500879{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400880 struct fw_cdev_start_iso *request = buffer;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500881
Stefan Richterfae60312008-02-20 21:10:06 +0100882 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400883 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +0100884
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400885 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400886 if (request->tags == 0 || request->tags > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400887 return -EINVAL;
888
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400889 if (request->sync > 15)
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -0400890 return -EINVAL;
891 }
892
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400893 return fw_iso_context_start(client->iso_context, request->cycle,
894 request->sync, request->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500895}
896
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400897static int ioctl_stop_iso(struct client *client, void *buffer)
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500898{
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400899 struct fw_cdev_stop_iso *request = buffer;
900
Stefan Richterfae60312008-02-20 21:10:06 +0100901 if (client->iso_context == NULL || request->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -0400902 return -EINVAL;
903
Kristian Høgsbergb8295662007-02-16 17:34:42 -0500904 return fw_iso_context_stop(client->iso_context);
905}
906
Stefan Richtera64408b2007-09-29 10:41:58 +0200907static int ioctl_get_cycle_timer(struct client *client, void *buffer)
908{
909 struct fw_cdev_get_cycle_timer *request = buffer;
910 struct fw_card *card = client->device->card;
911 unsigned long long bus_time;
912 struct timeval tv;
913 unsigned long flags;
914
915 preempt_disable();
916 local_irq_save(flags);
917
918 bus_time = card->driver->get_bus_time(card);
919 do_gettimeofday(&tv);
920
921 local_irq_restore(flags);
922 preempt_enable();
923
924 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
925 request->cycle_timer = bus_time & 0xffffffff;
926 return 0;
927}
928
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400929static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
930 ioctl_get_info,
931 ioctl_send_request,
932 ioctl_allocate,
933 ioctl_deallocate,
934 ioctl_send_response,
935 ioctl_initiate_bus_reset,
936 ioctl_add_descriptor,
937 ioctl_remove_descriptor,
938 ioctl_create_iso_context,
939 ioctl_queue_iso,
940 ioctl_start_iso,
941 ioctl_stop_iso,
Stefan Richtera64408b2007-09-29 10:41:58 +0200942 ioctl_get_cycle_timer,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400943};
944
Stefan Richter53dca512008-12-14 21:47:04 +0100945static int dispatch_ioctl(struct client *client,
946 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500947{
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400948 char buffer[256];
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100949 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400950
951 if (_IOC_TYPE(cmd) != '#' ||
952 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500953 return -EINVAL;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400954
955 if (_IOC_DIR(cmd) & _IOC_WRITE) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400956 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400957 copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
958 return -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500959 }
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400960
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100961 ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
962 if (ret < 0)
963 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400964
965 if (_IOC_DIR(cmd) & _IOC_READ) {
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400966 if (_IOC_SIZE(cmd) > sizeof(buffer) ||
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400967 copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
968 return -EFAULT;
969 }
970
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100971 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500972}
973
Stefan Richter53dca512008-12-14 21:47:04 +0100974static long fw_device_op_ioctl(struct file *file,
975 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500976{
977 struct client *client = file->private_data;
978
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400979 if (fw_device_is_shutdown(client->device))
980 return -ENODEV;
981
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500982 return dispatch_ioctl(client, cmd, (void __user *) arg);
983}
984
985#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +0100986static long fw_device_op_compat_ioctl(struct file *file,
987 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500988{
989 struct client *client = file->private_data;
990
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400991 if (fw_device_is_shutdown(client->device))
992 return -ENODEV;
993
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500994 return dispatch_ioctl(client, cmd, compat_ptr(arg));
995}
996#endif
997
998static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
999{
1000 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001001 enum dma_data_direction direction;
1002 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001003 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001004
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001005 if (fw_device_is_shutdown(client->device))
1006 return -ENODEV;
1007
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001008 /* FIXME: We could support multiple buffers, but we don't. */
1009 if (client->buffer.pages != NULL)
1010 return -EBUSY;
1011
1012 if (!(vma->vm_flags & VM_SHARED))
1013 return -EINVAL;
1014
1015 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001016 return -EINVAL;
1017
1018 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001019 size = vma->vm_end - vma->vm_start;
1020 page_count = size >> PAGE_SHIFT;
1021 if (size & ~PAGE_MASK)
1022 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001023
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001024 if (vma->vm_flags & VM_WRITE)
1025 direction = DMA_TO_DEVICE;
1026 else
1027 direction = DMA_FROM_DEVICE;
1028
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001029 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1030 page_count, direction);
1031 if (ret < 0)
1032 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001033
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001034 ret = fw_iso_buffer_map(&client->buffer, vma);
1035 if (ret < 0)
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001036 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1037
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001038 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001039}
1040
Jay Fenlason45ee3192008-12-21 16:47:17 +01001041static int shutdown_resource(int id, void *p, void *data)
1042{
1043 struct client_resource *r = p;
1044 struct client *client = data;
1045
1046 r->release(client, r);
1047
1048 return 0;
1049}
1050
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001051static int fw_device_op_release(struct inode *inode, struct file *file)
1052{
1053 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001054 struct event *e, *next_e;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001055 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001056
Stefan Richter97811e32008-12-14 19:19:23 +01001057 mutex_lock(&client->device->client_list_mutex);
1058 list_del(&client->link);
1059 mutex_unlock(&client->device->client_list_mutex);
1060
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001061 if (client->buffer.pages)
1062 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1063
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001064 if (client->iso_context)
1065 fw_iso_context_destroy(client->iso_context);
1066
Jay Fenlason45ee3192008-12-21 16:47:17 +01001067 /* Freeze client->resource_idr and client->event_list */
1068 spin_lock_irqsave(&client->lock, flags);
1069 client->in_shutdown = true;
1070 spin_unlock_irqrestore(&client->lock, flags);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001071
Jay Fenlason45ee3192008-12-21 16:47:17 +01001072 idr_for_each(&client->resource_idr, shutdown_resource, client);
1073 idr_remove_all(&client->resource_idr);
1074 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001075
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001076 list_for_each_entry_safe(e, next_e, &client->event_list, link)
1077 kfree(e);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001078
Jay Fenlason45ee3192008-12-21 16:47:17 +01001079 /*
1080 * FIXME: client should be reference-counted. It's extremely unlikely
1081 * but there may still be transactions being completed at this point.
1082 */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001083 fw_device_put(client->device);
1084 kfree(client);
1085
1086 return 0;
1087}
1088
1089static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1090{
1091 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001092 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001093
1094 poll_wait(file, &client->wait, pt);
1095
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001096 if (fw_device_is_shutdown(client->device))
1097 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001098 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001099 mask |= POLLIN | POLLRDNORM;
1100
1101 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001102}
1103
Stefan Richter21ebcd12007-01-14 15:29:07 +01001104const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001105 .owner = THIS_MODULE,
1106 .open = fw_device_op_open,
1107 .read = fw_device_op_read,
1108 .unlocked_ioctl = fw_device_op_ioctl,
1109 .poll = fw_device_op_poll,
1110 .release = fw_device_op_release,
1111 .mmap = fw_device_op_mmap,
1112
1113#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001114 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001115#endif
1116};