blob: 27ac423ab25e26e3a78defb81fcdad21b1050f9d [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 Richtereb5b35a2010-07-07 14:13:14 +020021#include <linux/bug.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010022#include <linux/compat.h>
23#include <linux/delay.h>
24#include <linux/device.h>
Stefan Richter0b6c4852012-04-09 20:51:18 +020025#include <linux/dma-mapping.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010026#include <linux/errno.h>
Stefan Richter77c9a5d2009-06-05 16:26:18 +020027#include <linux/firewire.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010028#include <linux/firewire-cdev.h>
29#include <linux/idr.h>
Stefan Richter4a9bde92010-02-20 22:24:43 +010030#include <linux/irqflags.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010031#include <linux/jiffies.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050032#include <linux/kernel.h>
Stefan Richterfb443032009-01-04 16:23:29 +010033#include <linux/kref.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010034#include <linux/mm.h>
35#include <linux/module.h>
Stefan Richterd67cfb92008-10-05 10:37:11 +020036#include <linux/mutex.h>
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050037#include <linux/poll.h>
Stefan Richterae2a97662010-07-29 09:31:56 +020038#include <linux/sched.h> /* required for linux/wait.h */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Jay Fenlasoncf417e542008-10-03 11:19:09 -040040#include <linux/spinlock.h>
Stefan Richter281e2032010-01-24 16:45:03 +010041#include <linux/string.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010042#include <linux/time.h>
Stefan Richtere034d242009-06-06 18:36:24 +020043#include <linux/uaccess.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010044#include <linux/vmalloc.h>
45#include <linux/wait.h>
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +010046#include <linux/workqueue.h>
Stefan Richterbe5bbd62009-01-04 16:23:29 +010047
Stefan Richterbe5bbd62009-01-04 16:23:29 +010048
Stefan Richter77c9a5d2009-06-05 16:26:18 +020049#include "core.h"
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050050
Stefan Richter604f4512010-06-20 22:52:55 +020051/*
52 * ABI version history is documented in linux/firewire-cdev.h.
53 */
Clemens Ladisch18d62712012-03-18 19:05:29 +010054#define FW_CDEV_KERNEL_VERSION 5
Stefan Richter8e2b2b42010-07-23 13:05:39 +020055#define FW_CDEV_VERSION_EVENT_REQUEST2 4
56#define FW_CDEV_VERSION_ALLOCATE_REGION_END 4
Stefan Richter604f4512010-06-20 22:52:55 +020057
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050058struct client {
Kristian Høgsberg344bbc42007-03-07 12:12:43 -050059 u32 version;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050060 struct fw_device *device;
Jay Fenlason45ee3192008-12-21 16:47:17 +010061
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050062 spinlock_t lock;
Jay Fenlason45ee3192008-12-21 16:47:17 +010063 bool in_shutdown;
64 struct idr resource_idr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050065 struct list_head event_list;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050066 wait_queue_head_t wait;
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +010067 wait_queue_head_t tx_flush_wait;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -040068 u64 bus_reset_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050069
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050070 struct fw_iso_context *iso_context;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -040071 u64 iso_closure;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -050072 struct fw_iso_buffer buffer;
73 unsigned long vm_start;
Stefan Richter0b6c4852012-04-09 20:51:18 +020074 bool buffer_is_mapped;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050075
Stefan Richterbf54e142010-07-16 22:25:51 +020076 struct list_head phy_receiver_link;
77 u64 phy_receiver_closure;
78
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -050079 struct list_head link;
Stefan Richterfb443032009-01-04 16:23:29 +010080 struct kref kref;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -050081};
82
Stefan Richterfb443032009-01-04 16:23:29 +010083static inline void client_get(struct client *client)
84{
85 kref_get(&client->kref);
86}
87
88static void client_release(struct kref *kref)
89{
90 struct client *client = container_of(kref, struct client, kref);
91
92 fw_device_put(client->device);
93 kfree(client);
94}
95
96static void client_put(struct client *client)
97{
98 kref_put(&client->kref, client_release);
99}
100
Stefan Richter97c18b72009-01-04 16:23:29 +0100101struct client_resource;
102typedef void (*client_resource_release_fn_t)(struct client *,
103 struct client_resource *);
104struct client_resource {
105 client_resource_release_fn_t release;
106 int handle;
107};
108
109struct address_handler_resource {
110 struct client_resource resource;
111 struct fw_address_handler handler;
112 __u64 closure;
113 struct client *client;
114};
115
116struct outbound_transaction_resource {
117 struct client_resource resource;
118 struct fw_transaction transaction;
119};
120
121struct inbound_transaction_resource {
122 struct client_resource resource;
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400123 struct fw_card *card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100124 struct fw_request *request;
125 void *data;
126 size_t length;
127};
128
129struct descriptor_resource {
130 struct client_resource resource;
131 struct fw_descriptor descriptor;
132 u32 data[0];
133};
134
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100135struct iso_resource {
136 struct client_resource resource;
137 struct client *client;
138 /* Schedule work and access todo only with client->lock held. */
139 struct delayed_work work;
Stefan Richter1ec3c022009-01-04 16:23:29 +0100140 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
141 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100142 int generation;
143 u64 channels;
144 s32 bandwidth;
145 struct iso_resource_event *e_alloc, *e_dealloc;
146};
147
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100148static void release_iso_resource(struct client *, struct client_resource *);
149
Stefan Richter9fb551b2009-10-08 00:41:10 +0200150static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
151{
152 client_get(r->client);
Stefan Richter105e53f2011-05-01 20:50:31 +0200153 if (!queue_delayed_work(fw_workqueue, &r->work, delay))
Stefan Richter9fb551b2009-10-08 00:41:10 +0200154 client_put(r->client);
155}
156
157static void schedule_if_iso_resource(struct client_resource *resource)
158{
159 if (resource->release == release_iso_resource)
160 schedule_iso_resource(container_of(resource,
161 struct iso_resource, resource), 0);
162}
163
Stefan Richter97c18b72009-01-04 16:23:29 +0100164/*
165 * dequeue_event() just kfree()'s the event, so the event has to be
166 * the first field in a struct XYZ_event.
167 */
168struct event {
169 struct { void *data; size_t size; } v[2];
170 struct list_head link;
171};
172
173struct bus_reset_event {
174 struct event event;
175 struct fw_cdev_event_bus_reset reset;
176};
177
178struct outbound_transaction_event {
179 struct event event;
180 struct client *client;
181 struct outbound_transaction_resource r;
182 struct fw_cdev_event_response response;
183};
184
185struct inbound_transaction_event {
186 struct event event;
Stefan Richtere2055972010-06-20 22:53:55 +0200187 union {
188 struct fw_cdev_event_request request;
189 struct fw_cdev_event_request2 request2;
190 } req;
Stefan Richter97c18b72009-01-04 16:23:29 +0100191};
192
193struct iso_interrupt_event {
194 struct event event;
195 struct fw_cdev_event_iso_interrupt interrupt;
196};
197
Stefan Richter872e3302010-07-29 18:19:22 +0200198struct iso_interrupt_mc_event {
199 struct event event;
200 struct fw_cdev_event_iso_interrupt_mc interrupt;
201};
202
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100203struct iso_resource_event {
204 struct event event;
Stefan Richtere21fcf72009-10-08 00:41:38 +0200205 struct fw_cdev_event_iso_resource iso_resource;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100206};
207
Stefan Richter850bb6f2010-07-16 22:25:14 +0200208struct outbound_phy_packet_event {
209 struct event event;
210 struct client *client;
211 struct fw_packet p;
212 struct fw_cdev_event_phy_packet phy_packet;
213};
214
Stefan Richterbf54e142010-07-16 22:25:51 +0200215struct inbound_phy_packet_event {
216 struct event event;
217 struct fw_cdev_event_phy_packet phy_packet;
218};
219
Stefan Richter9c1176b2011-08-11 00:06:04 +0200220#ifdef CONFIG_COMPAT
221static void __user *u64_to_uptr(u64 value)
222{
223 if (is_compat_task())
224 return compat_ptr(value);
225 else
226 return (void __user *)(unsigned long)value;
227}
228
229static u64 uptr_to_u64(void __user *ptr)
230{
231 if (is_compat_task())
232 return ptr_to_compat(ptr);
233 else
234 return (u64)(unsigned long)ptr;
235}
236#else
237static inline void __user *u64_to_uptr(u64 value)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500238{
239 return (void __user *)(unsigned long)value;
240}
241
Stefan Richter9c1176b2011-08-11 00:06:04 +0200242static inline u64 uptr_to_u64(void __user *ptr)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500243{
Stefan Richter9c1176b2011-08-11 00:06:04 +0200244 return (u64)(unsigned long)ptr;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500245}
Stefan Richter9c1176b2011-08-11 00:06:04 +0200246#endif /* CONFIG_COMPAT */
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500247
248static int fw_device_op_open(struct inode *inode, struct file *file)
249{
250 struct fw_device *device;
251 struct client *client;
252
Stefan Richter96b19062008-02-02 15:01:09 +0100253 device = fw_device_get_by_devt(inode->i_rdev);
Kristian Høgsberga3aca3d2007-03-07 12:12:44 -0500254 if (device == NULL)
255 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500256
Jay Fenlason551f4cb2008-05-16 11:15:23 -0400257 if (fw_device_is_shutdown(device)) {
258 fw_device_put(device);
259 return -ENODEV;
260 }
261
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400262 client = kzalloc(sizeof(*client), GFP_KERNEL);
Stefan Richter96b19062008-02-02 15:01:09 +0100263 if (client == NULL) {
264 fw_device_put(device);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500265 return -ENOMEM;
Stefan Richter96b19062008-02-02 15:01:09 +0100266 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500267
Stefan Richter96b19062008-02-02 15:01:09 +0100268 client->device = device;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500269 spin_lock_init(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100270 idr_init(&client->resource_idr);
271 INIT_LIST_HEAD(&client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500272 init_waitqueue_head(&client->wait);
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100273 init_waitqueue_head(&client->tx_flush_wait);
Stefan Richterbf54e142010-07-16 22:25:51 +0200274 INIT_LIST_HEAD(&client->phy_receiver_link);
Stefan Richter93b37902011-07-09 16:43:22 +0200275 INIT_LIST_HEAD(&client->link);
Stefan Richterfb443032009-01-04 16:23:29 +0100276 kref_init(&client->kref);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500277
278 file->private_data = client;
279
Stefan Richter3ac26b22010-04-10 16:38:05 +0100280 return nonseekable_open(inode, file);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500281}
282
283static void queue_event(struct client *client, struct event *event,
284 void *data0, size_t size0, void *data1, size_t size1)
285{
286 unsigned long flags;
287
288 event->v[0].data = data0;
289 event->v[0].size = size0;
290 event->v[1].data = data1;
291 event->v[1].size = size1;
292
293 spin_lock_irqsave(&client->lock, flags);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100294 if (client->in_shutdown)
295 kfree(event);
296 else
297 list_add_tail(&event->link, &client->event_list);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500298 spin_unlock_irqrestore(&client->lock, flags);
Jay Fenlason83431cb2007-10-08 17:00:29 -0400299
300 wake_up_interruptible(&client->wait);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500301}
302
Stefan Richter53dca512008-12-14 21:47:04 +0100303static int dequeue_event(struct client *client,
304 char __user *buffer, size_t count)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500305{
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500306 struct event *event;
307 size_t size, total;
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100308 int i, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500309
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100310 ret = wait_event_interruptible(client->wait,
311 !list_empty(&client->event_list) ||
312 fw_device_is_shutdown(client->device));
313 if (ret < 0)
314 return ret;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500315
316 if (list_empty(&client->event_list) &&
317 fw_device_is_shutdown(client->device))
318 return -ENODEV;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500319
Stefan Richter3ba94982009-01-04 16:23:29 +0100320 spin_lock_irq(&client->lock);
Stefan Richtera459b8a2008-12-21 16:49:57 +0100321 event = list_first_entry(&client->event_list, struct event, link);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500322 list_del(&event->link);
Stefan Richter3ba94982009-01-04 16:23:29 +0100323 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500324
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500325 total = 0;
326 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
327 size = min(event->v[i].size, count - total);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500328 if (copy_to_user(buffer + total, event->v[i].data, size)) {
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100329 ret = -EFAULT;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500330 goto out;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500331 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500332 total += size;
333 }
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100334 ret = total;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500335
336 out:
337 kfree(event);
338
Stefan Richter2dbd7d72008-12-14 21:45:45 +0100339 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500340}
341
Stefan Richter53dca512008-12-14 21:47:04 +0100342static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
343 size_t count, loff_t *offset)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500344{
345 struct client *client = file->private_data;
346
347 return dequeue_event(client, buffer, count);
348}
349
Stefan Richter53dca512008-12-14 21:47:04 +0100350static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
351 struct client *client)
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500352{
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400353 struct fw_card *card = client->device->card;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400354
Stefan Richter3ba94982009-01-04 16:23:29 +0100355 spin_lock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500356
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400357 event->closure = client->bus_reset_closure;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500358 event->type = FW_CDEV_EVENT_BUS_RESET;
Stefan Richtercf5a56a2008-01-24 01:53:51 +0100359 event->generation = client->device->generation;
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400360 event->node_id = client->device->node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500361 event->local_node_id = card->local_node->node_id;
Stefan Richter250b2b62010-06-21 23:24:35 +0200362 event->bm_node_id = card->bm_node_id;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500363 event->irm_node_id = card->irm_node->node_id;
364 event->root_node_id = card->root_node->node_id;
Jay Fenlasoncf417e542008-10-03 11:19:09 -0400365
Stefan Richter3ba94982009-01-04 16:23:29 +0100366 spin_unlock_irq(&card->lock);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500367}
368
Stefan Richter53dca512008-12-14 21:47:04 +0100369static void for_each_client(struct fw_device *device,
370 void (*callback)(struct client *client))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500371{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500372 struct client *c;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500373
Stefan Richterd67cfb92008-10-05 10:37:11 +0200374 mutex_lock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500375 list_for_each_entry(c, &device->client_list, link)
376 callback(c);
Stefan Richterd67cfb92008-10-05 10:37:11 +0200377 mutex_unlock(&device->client_list_mutex);
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500378}
379
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100380static int schedule_reallocations(int id, void *p, void *data)
381{
Stefan Richter9fb551b2009-10-08 00:41:10 +0200382 schedule_if_iso_resource(p);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100383
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100384 return 0;
385}
386
Stefan Richter53dca512008-12-14 21:47:04 +0100387static void queue_bus_reset_event(struct client *client)
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500388{
Stefan Richter97c18b72009-01-04 16:23:29 +0100389 struct bus_reset_event *e;
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500390
Stefan Richter97c18b72009-01-04 16:23:29 +0100391 e = kzalloc(sizeof(*e), GFP_KERNEL);
392 if (e == NULL) {
Stefan Richter26b49502012-02-18 22:03:14 +0100393 fw_notice(client->device->card, "out of memory when allocating event\n");
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500394 return;
395 }
396
Stefan Richter97c18b72009-01-04 16:23:29 +0100397 fill_bus_reset_event(&e->reset, client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500398
Stefan Richter97c18b72009-01-04 16:23:29 +0100399 queue_event(client, &e->event,
400 &e->reset, sizeof(e->reset), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100401
402 spin_lock_irq(&client->lock);
403 idr_for_each(&client->resource_idr, schedule_reallocations, client);
404 spin_unlock_irq(&client->lock);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500405}
406
407void fw_device_cdev_update(struct fw_device *device)
408{
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500409 for_each_client(device, queue_bus_reset_event);
410}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500411
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500412static void wake_up_client(struct client *client)
413{
414 wake_up_interruptible(&client->wait);
415}
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500416
Kristian Høgsberg2603bf22007-03-07 12:12:48 -0500417void fw_device_cdev_remove(struct fw_device *device)
418{
419 for_each_client(device, wake_up_client);
Kristian Høgsberg97bd9ef2007-03-07 12:12:41 -0500420}
421
Stefan Richter6e95dea2010-02-21 17:56:21 +0100422union ioctl_arg {
423 struct fw_cdev_get_info get_info;
424 struct fw_cdev_send_request send_request;
425 struct fw_cdev_allocate allocate;
426 struct fw_cdev_deallocate deallocate;
427 struct fw_cdev_send_response send_response;
428 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
429 struct fw_cdev_add_descriptor add_descriptor;
430 struct fw_cdev_remove_descriptor remove_descriptor;
431 struct fw_cdev_create_iso_context create_iso_context;
432 struct fw_cdev_queue_iso queue_iso;
433 struct fw_cdev_start_iso start_iso;
434 struct fw_cdev_stop_iso stop_iso;
435 struct fw_cdev_get_cycle_timer get_cycle_timer;
436 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
437 struct fw_cdev_send_stream_packet send_stream_packet;
438 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
Stefan Richter850bb6f2010-07-16 22:25:14 +0200439 struct fw_cdev_send_phy_packet send_phy_packet;
Stefan Richterbf54e142010-07-16 22:25:51 +0200440 struct fw_cdev_receive_phy_packets receive_phy_packets;
Stefan Richter872e3302010-07-29 18:19:22 +0200441 struct fw_cdev_set_iso_channels set_iso_channels;
Clemens Ladischd1bbd202012-03-18 19:06:39 +0100442 struct fw_cdev_flush_iso flush_iso;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100443};
444
445static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500446{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100447 struct fw_cdev_get_info *a = &arg->get_info;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500448 struct fw_cdev_event_bus_reset bus_reset;
Stefan Richterc9755e12008-03-24 20:54:28 +0100449 unsigned long ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500450
Stefan Richter6e95dea2010-02-21 17:56:21 +0100451 client->version = a->version;
Stefan Richter604f4512010-06-20 22:52:55 +0200452 a->version = FW_CDEV_KERNEL_VERSION;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100453 a->card = client->device->card->index;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500454
Stefan Richterc9755e12008-03-24 20:54:28 +0100455 down_read(&fw_device_rwsem);
456
Stefan Richter6e95dea2010-02-21 17:56:21 +0100457 if (a->rom != 0) {
458 size_t want = a->rom_length;
Stefan Richterd84702a2007-03-20 19:42:15 +0100459 size_t have = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500460
Stefan Richter6e95dea2010-02-21 17:56:21 +0100461 ret = copy_to_user(u64_to_uptr(a->rom),
462 client->device->config_rom, min(want, have));
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500463 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100464 a->rom_length = client->device->config_rom_length * 4;
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500465
Stefan Richterc9755e12008-03-24 20:54:28 +0100466 up_read(&fw_device_rwsem);
467
468 if (ret != 0)
469 return -EFAULT;
470
Stefan Richter93b37902011-07-09 16:43:22 +0200471 mutex_lock(&client->device->client_list_mutex);
472
Stefan Richter6e95dea2010-02-21 17:56:21 +0100473 client->bus_reset_closure = a->bus_reset_closure;
474 if (a->bus_reset != 0) {
Kristian Høgsbergda8ecff2007-03-27 01:43:39 -0400475 fill_bus_reset_event(&bus_reset, client);
Stefan Richter790198f2012-10-06 14:12:56 +0200476 /* unaligned size of bus_reset is 36 bytes */
477 ret = copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500478 }
Stefan Richter93b37902011-07-09 16:43:22 +0200479 if (ret == 0 && list_empty(&client->link))
480 list_add_tail(&client->link, &client->device->client_list);
Kristian Høgsberg344bbc42007-03-07 12:12:43 -0500481
Stefan Richter93b37902011-07-09 16:43:22 +0200482 mutex_unlock(&client->device->client_list_mutex);
483
484 return ret ? -EFAULT : 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500485}
486
Stefan Richter53dca512008-12-14 21:47:04 +0100487static int add_client_resource(struct client *client,
488 struct client_resource *resource, gfp_t gfp_mask)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400489{
Tejun Heo37b61892013-02-27 17:04:05 -0800490 bool preload = gfp_mask & __GFP_WAIT;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400491 unsigned long flags;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100492 int ret;
493
Tejun Heo37b61892013-02-27 17:04:05 -0800494 if (preload)
495 idr_preload(gfp_mask);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400496 spin_lock_irqsave(&client->lock, flags);
Tejun Heo37b61892013-02-27 17:04:05 -0800497
Jay Fenlason45ee3192008-12-21 16:47:17 +0100498 if (client->in_shutdown)
499 ret = -ECANCELED;
500 else
Tejun Heo37b61892013-02-27 17:04:05 -0800501 ret = idr_alloc(&client->resource_idr, resource, 0, 0,
502 GFP_NOWAIT);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100503 if (ret >= 0) {
Tejun Heo37b61892013-02-27 17:04:05 -0800504 resource->handle = ret;
Stefan Richterfb443032009-01-04 16:23:29 +0100505 client_get(client);
Stefan Richter9fb551b2009-10-08 00:41:10 +0200506 schedule_if_iso_resource(resource);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +0100507 }
Jay Fenlason45ee3192008-12-21 16:47:17 +0100508
Tejun Heo37b61892013-02-27 17:04:05 -0800509 spin_unlock_irqrestore(&client->lock, flags);
510 if (preload)
511 idr_preload_end();
Jay Fenlason45ee3192008-12-21 16:47:17 +0100512
513 return ret < 0 ? ret : 0;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400514}
515
Stefan Richter53dca512008-12-14 21:47:04 +0100516static int release_client_resource(struct client *client, u32 handle,
517 client_resource_release_fn_t release,
Stefan Richtere21fcf72009-10-08 00:41:38 +0200518 struct client_resource **return_resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400519{
Stefan Richtere21fcf72009-10-08 00:41:38 +0200520 struct client_resource *resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400521
Stefan Richter3ba94982009-01-04 16:23:29 +0100522 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100523 if (client->in_shutdown)
Stefan Richtere21fcf72009-10-08 00:41:38 +0200524 resource = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100525 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200526 resource = idr_find(&client->resource_idr, handle);
527 if (resource && resource->release == release)
Jay Fenlason45ee3192008-12-21 16:47:17 +0100528 idr_remove(&client->resource_idr, handle);
Stefan Richter3ba94982009-01-04 16:23:29 +0100529 spin_unlock_irq(&client->lock);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400530
Stefan Richtere21fcf72009-10-08 00:41:38 +0200531 if (!(resource && resource->release == release))
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400532 return -EINVAL;
533
Stefan Richtere21fcf72009-10-08 00:41:38 +0200534 if (return_resource)
535 *return_resource = resource;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400536 else
Stefan Richtere21fcf72009-10-08 00:41:38 +0200537 resource->release(client, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400538
Stefan Richterfb443032009-01-04 16:23:29 +0100539 client_put(client);
540
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400541 return 0;
542}
543
Stefan Richter53dca512008-12-14 21:47:04 +0100544static void release_transaction(struct client *client,
545 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400546{
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400547}
548
Stefan Richter53dca512008-12-14 21:47:04 +0100549static void complete_transaction(struct fw_card *card, int rcode,
550 void *payload, size_t length, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500551{
Stefan Richter97c18b72009-01-04 16:23:29 +0100552 struct outbound_transaction_event *e = data;
553 struct fw_cdev_event_response *rsp = &e->response;
554 struct client *client = e->client;
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500555 unsigned long flags;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500556
Stefan Richter97c18b72009-01-04 16:23:29 +0100557 if (length < rsp->length)
558 rsp->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500559 if (rcode == RCODE_COMPLETE)
Stefan Richter97c18b72009-01-04 16:23:29 +0100560 memcpy(rsp->data, payload, rsp->length);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500561
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500562 spin_lock_irqsave(&client->lock, flags);
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100563 idr_remove(&client->resource_idr, e->r.resource.handle);
564 if (client->in_shutdown)
565 wake_up(&client->tx_flush_wait);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -0500566 spin_unlock_irqrestore(&client->lock, flags);
567
Stefan Richter97c18b72009-01-04 16:23:29 +0100568 rsp->type = FW_CDEV_EVENT_RESPONSE;
569 rsp->rcode = rcode;
David Moore8401d922008-07-29 23:46:25 -0700570
571 /*
Stefan Richter97c18b72009-01-04 16:23:29 +0100572 * In the case that sizeof(*rsp) doesn't align with the position of the
David Moore8401d922008-07-29 23:46:25 -0700573 * data, and the read is short, preserve an extra copy of the data
574 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
575 * for short reads and some apps depended on it, this is both safe
576 * and prudent for compatibility.
577 */
Stefan Richter97c18b72009-01-04 16:23:29 +0100578 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
579 queue_event(client, &e->event, rsp, sizeof(*rsp),
580 rsp->data, rsp->length);
David Moore8401d922008-07-29 23:46:25 -0700581 else
Stefan Richter97c18b72009-01-04 16:23:29 +0100582 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
David Moore8401d922008-07-29 23:46:25 -0700583 NULL, 0);
Stefan Richterfb443032009-01-04 16:23:29 +0100584
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +0100585 /* Drop the idr's reference */
586 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500587}
588
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100589static int init_request(struct client *client,
590 struct fw_cdev_send_request *request,
591 int destination_id, int speed)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500592{
Stefan Richter97c18b72009-01-04 16:23:29 +0100593 struct outbound_transaction_event *e;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100594 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500595
Stefan Richter18e9b102009-03-10 21:02:21 +0100596 if (request->tcode != TCODE_STREAM_DATA &&
597 (request->length > 4096 || request->length > 512 << speed))
Stefan Richter5d3fd692009-01-04 16:23:29 +0100598 return -EIO;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500599
Clemens Ladischa8e93f32010-07-07 14:37:30 +0200600 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
601 request->length < 4)
602 return -EINVAL;
603
Stefan Richter97c18b72009-01-04 16:23:29 +0100604 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
605 if (e == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500606 return -ENOMEM;
607
Stefan Richter97c18b72009-01-04 16:23:29 +0100608 e->client = client;
609 e->response.length = request->length;
610 e->response.closure = request->closure;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500611
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400612 if (request->data &&
Stefan Richter97c18b72009-01-04 16:23:29 +0100613 copy_from_user(e->response.data,
Kristian Høgsberg4f259222007-04-30 15:03:13 -0400614 u64_to_uptr(request->data), request->length)) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100615 ret = -EFAULT;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100616 goto failed;
Stefan Richter1f3125a2008-12-05 22:44:42 +0100617 }
618
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100619 e->r.resource.release = release_transaction;
620 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
621 if (ret < 0)
622 goto failed;
623
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100624 fw_send_request(client->device->card, &e->r.transaction,
Stefan Richter664d8012009-03-10 21:01:54 +0100625 request->tcode, destination_id, request->generation,
626 speed, request->offset, e->response.data,
627 request->length, complete_transaction, e);
628 return 0;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100629
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100630 failed:
631 kfree(e);
632
633 return ret;
634}
635
Stefan Richter6e95dea2010-02-21 17:56:21 +0100636static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100637{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100638 switch (arg->send_request.tcode) {
Stefan Richter1f3125a2008-12-05 22:44:42 +0100639 case TCODE_WRITE_QUADLET_REQUEST:
640 case TCODE_WRITE_BLOCK_REQUEST:
641 case TCODE_READ_QUADLET_REQUEST:
642 case TCODE_READ_BLOCK_REQUEST:
643 case TCODE_LOCK_MASK_SWAP:
644 case TCODE_LOCK_COMPARE_SWAP:
645 case TCODE_LOCK_FETCH_ADD:
646 case TCODE_LOCK_LITTLE_ADD:
647 case TCODE_LOCK_BOUNDED_ADD:
648 case TCODE_LOCK_WRAP_ADD:
649 case TCODE_LOCK_VENDOR_DEPENDENT:
650 break;
651 default:
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100652 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500653 }
654
Stefan Richter6e95dea2010-02-21 17:56:21 +0100655 return init_request(client, &arg->send_request, client->device->node_id,
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +0100656 client->device->max_speed);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500657}
658
Stefan Richter281e2032010-01-24 16:45:03 +0100659static inline bool is_fcp_request(struct fw_request *request)
660{
661 return request == NULL;
662}
663
Stefan Richter53dca512008-12-14 21:47:04 +0100664static void release_request(struct client *client,
665 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400666{
Stefan Richter97c18b72009-01-04 16:23:29 +0100667 struct inbound_transaction_resource *r = container_of(resource,
668 struct inbound_transaction_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400669
Stefan Richter281e2032010-01-24 16:45:03 +0100670 if (is_fcp_request(r->request))
671 kfree(r->data);
672 else
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400673 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200674
675 fw_card_put(r->card);
Stefan Richter97c18b72009-01-04 16:23:29 +0100676 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400677}
678
Stefan Richter97c18b72009-01-04 16:23:29 +0100679static void handle_request(struct fw_card *card, struct fw_request *request,
Stefan Richter53dca512008-12-14 21:47:04 +0100680 int tcode, int destination, int source,
Stefan Richter33e553f2010-06-20 22:50:35 +0200681 int generation, unsigned long long offset,
Stefan Richter53dca512008-12-14 21:47:04 +0100682 void *payload, size_t length, void *callback_data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500683{
Stefan Richter97c18b72009-01-04 16:23:29 +0100684 struct address_handler_resource *handler = callback_data;
685 struct inbound_transaction_resource *r;
686 struct inbound_transaction_event *e;
Stefan Richtere2055972010-06-20 22:53:55 +0200687 size_t event_size0;
Stefan Richter281e2032010-01-24 16:45:03 +0100688 void *fcp_frame = NULL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100689 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500690
Stefan Richter0244f572010-06-20 22:52:27 +0200691 /* card may be different from handler->client->device->card */
692 fw_card_get(card);
693
Stefan Richter97c18b72009-01-04 16:23:29 +0100694 r = kmalloc(sizeof(*r), GFP_ATOMIC);
Kristian Høgsberg2d826cc2007-05-09 19:23:14 -0400695 e = kmalloc(sizeof(*e), GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200696 if (r == NULL || e == NULL) {
Stefan Richter26b49502012-02-18 22:03:14 +0100697 fw_notice(card, "out of memory when allocating event\n");
Jay Fenlason45ee3192008-12-21 16:47:17 +0100698 goto failed;
Stefan Richterbf54e142010-07-16 22:25:51 +0200699 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400700 r->card = card;
Stefan Richter97c18b72009-01-04 16:23:29 +0100701 r->request = request;
702 r->data = payload;
703 r->length = length;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500704
Stefan Richter281e2032010-01-24 16:45:03 +0100705 if (is_fcp_request(request)) {
706 /*
707 * FIXME: Let core-transaction.c manage a
708 * single reference-counted copy?
709 */
710 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
711 if (fcp_frame == NULL)
712 goto failed;
713
714 r->data = fcp_frame;
715 }
716
Stefan Richter97c18b72009-01-04 16:23:29 +0100717 r->resource.release = release_request;
718 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100719 if (ret < 0)
720 goto failed;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500721
Stefan Richtere2055972010-06-20 22:53:55 +0200722 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
723 struct fw_cdev_event_request *req = &e->req.request;
724
725 if (tcode & 0x10)
726 tcode = TCODE_LOCK_REQUEST;
727
728 req->type = FW_CDEV_EVENT_REQUEST;
729 req->tcode = tcode;
730 req->offset = offset;
731 req->length = length;
732 req->handle = r->resource.handle;
733 req->closure = handler->closure;
734 event_size0 = sizeof(*req);
735 } else {
736 struct fw_cdev_event_request2 *req = &e->req.request2;
737
738 req->type = FW_CDEV_EVENT_REQUEST2;
739 req->tcode = tcode;
740 req->offset = offset;
741 req->source_node_id = source;
742 req->destination_node_id = destination;
743 req->card = card->index;
744 req->generation = generation;
745 req->length = length;
746 req->handle = r->resource.handle;
747 req->closure = handler->closure;
748 event_size0 = sizeof(*req);
749 }
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500750
Stefan Richter97c18b72009-01-04 16:23:29 +0100751 queue_event(handler->client, &e->event,
Stefan Richtere2055972010-06-20 22:53:55 +0200752 &e->req, event_size0, r->data, length);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100753 return;
754
755 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100756 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100757 kfree(e);
Stefan Richter281e2032010-01-24 16:45:03 +0100758 kfree(fcp_frame);
759
760 if (!is_fcp_request(request))
Clemens Ladischdb5d2472009-12-24 12:05:58 +0100761 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
Stefan Richter0244f572010-06-20 22:52:27 +0200762
763 fw_card_put(card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500764}
765
Stefan Richter53dca512008-12-14 21:47:04 +0100766static void release_address_handler(struct client *client,
767 struct client_resource *resource)
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400768{
Stefan Richter97c18b72009-01-04 16:23:29 +0100769 struct address_handler_resource *r =
770 container_of(resource, struct address_handler_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400771
Stefan Richter97c18b72009-01-04 16:23:29 +0100772 fw_core_remove_address_handler(&r->handler);
773 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400774}
775
Stefan Richter6e95dea2010-02-21 17:56:21 +0100776static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500777{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100778 struct fw_cdev_allocate *a = &arg->allocate;
Stefan Richter97c18b72009-01-04 16:23:29 +0100779 struct address_handler_resource *r;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500780 struct fw_address_region region;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100781 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500782
Stefan Richter97c18b72009-01-04 16:23:29 +0100783 r = kmalloc(sizeof(*r), GFP_KERNEL);
784 if (r == NULL)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500785 return -ENOMEM;
786
Stefan Richter6e95dea2010-02-21 17:56:21 +0100787 region.start = a->offset;
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200788 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
789 region.end = a->offset + a->length;
790 else
791 region.end = a->region_end;
792
Stefan Richter6e95dea2010-02-21 17:56:21 +0100793 r->handler.length = a->length;
Stefan Richter97c18b72009-01-04 16:23:29 +0100794 r->handler.address_callback = handle_request;
Stefan Richter6e95dea2010-02-21 17:56:21 +0100795 r->handler.callback_data = r;
796 r->closure = a->closure;
797 r->client = client;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500798
Stefan Richter97c18b72009-01-04 16:23:29 +0100799 ret = fw_core_add_address_handler(&r->handler, &region);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100800 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100801 kfree(r);
Stefan Richter3e0b5f02008-12-14 19:21:01 +0100802 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500803 }
Stefan Richter8e2b2b42010-07-23 13:05:39 +0200804 a->offset = r->handler.offset;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500805
Stefan Richter97c18b72009-01-04 16:23:29 +0100806 r->resource.release = release_address_handler;
807 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100808 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100809 release_address_handler(client, &r->resource);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100810 return ret;
811 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100812 a->handle = r->resource.handle;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500813
814 return 0;
815}
816
Stefan Richter6e95dea2010-02-21 17:56:21 +0100817static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg94723162007-03-14 17:34:55 -0400818{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100819 return release_client_resource(client, arg->deallocate.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100820 release_address_handler, NULL);
Kristian Høgsberg94723162007-03-14 17:34:55 -0400821}
822
Stefan Richter6e95dea2010-02-21 17:56:21 +0100823static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500824{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100825 struct fw_cdev_send_response *a = &arg->send_response;
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400826 struct client_resource *resource;
Stefan Richter97c18b72009-01-04 16:23:29 +0100827 struct inbound_transaction_resource *r;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200828 int ret = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500829
Stefan Richter6e95dea2010-02-21 17:56:21 +0100830 if (release_client_resource(client, a->handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100831 release_request, &resource) < 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500832 return -EINVAL;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100833
Stefan Richter97c18b72009-01-04 16:23:29 +0100834 r = container_of(resource, struct inbound_transaction_resource,
835 resource);
Stefan Richter281e2032010-01-24 16:45:03 +0100836 if (is_fcp_request(r->request))
837 goto out;
838
Clemens Ladischa10c0ce72010-05-19 08:28:32 +0200839 if (a->length != fw_get_response_length(r->request)) {
840 ret = -EINVAL;
841 kfree(r->request);
842 goto out;
843 }
844 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
Stefan Richter281e2032010-01-24 16:45:03 +0100845 ret = -EFAULT;
846 kfree(r->request);
847 goto out;
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200848 }
Jay Fenlason08bd34c2010-05-18 14:02:45 -0400849 fw_send_response(r->card, r->request, a->rcode);
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200850 out:
Stefan Richter0244f572010-06-20 22:52:27 +0200851 fw_card_put(r->card);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500852 kfree(r);
853
Stefan Richter7e44c0b2009-10-08 00:39:56 +0200854 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500855}
856
Stefan Richter6e95dea2010-02-21 17:56:21 +0100857static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg53718422007-03-07 12:12:42 -0500858{
Stefan Richter02d37be2010-07-08 16:09:06 +0200859 fw_schedule_bus_reset(client->device->card, true,
Stefan Richter6e95dea2010-02-21 17:56:21 +0100860 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
Stefan Richter02d37be2010-07-08 16:09:06 +0200861 return 0;
Kristian Høgsberg53718422007-03-07 12:12:42 -0500862}
863
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400864static void release_descriptor(struct client *client,
865 struct client_resource *resource)
866{
Stefan Richter97c18b72009-01-04 16:23:29 +0100867 struct descriptor_resource *r =
868 container_of(resource, struct descriptor_resource, resource);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400869
Stefan Richter97c18b72009-01-04 16:23:29 +0100870 fw_core_remove_descriptor(&r->descriptor);
871 kfree(r);
Kristian Høgsberg3964a442007-03-27 01:43:41 -0400872}
873
Stefan Richter6e95dea2010-02-21 17:56:21 +0100874static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200875{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100876 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
Stefan Richter97c18b72009-01-04 16:23:29 +0100877 struct descriptor_resource *r;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100878 int ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200879
Stefan Richterde487da2009-03-10 21:00:23 +0100880 /* Access policy: Allow this ioctl only on local nodes' device files. */
Stefan Richter92368892009-05-13 21:42:14 +0200881 if (!client->device->is_local)
Stefan Richterde487da2009-03-10 21:00:23 +0100882 return -ENOSYS;
883
Stefan Richter6e95dea2010-02-21 17:56:21 +0100884 if (a->length > 256)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200885 return -EINVAL;
886
Stefan Richter6e95dea2010-02-21 17:56:21 +0100887 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
Stefan Richter97c18b72009-01-04 16:23:29 +0100888 if (r == NULL)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200889 return -ENOMEM;
890
Stefan Richter6e95dea2010-02-21 17:56:21 +0100891 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
Jay Fenlason45ee3192008-12-21 16:47:17 +0100892 ret = -EFAULT;
893 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200894 }
895
Stefan Richter6e95dea2010-02-21 17:56:21 +0100896 r->descriptor.length = a->length;
897 r->descriptor.immediate = a->immediate;
898 r->descriptor.key = a->key;
Stefan Richter97c18b72009-01-04 16:23:29 +0100899 r->descriptor.data = r->data;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200900
Stefan Richter97c18b72009-01-04 16:23:29 +0100901 ret = fw_core_add_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100902 if (ret < 0)
903 goto failed;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200904
Stefan Richter97c18b72009-01-04 16:23:29 +0100905 r->resource.release = release_descriptor;
906 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100907 if (ret < 0) {
Stefan Richter97c18b72009-01-04 16:23:29 +0100908 fw_core_remove_descriptor(&r->descriptor);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100909 goto failed;
910 }
Stefan Richter6e95dea2010-02-21 17:56:21 +0100911 a->handle = r->resource.handle;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200912
913 return 0;
Jay Fenlason45ee3192008-12-21 16:47:17 +0100914 failed:
Stefan Richter97c18b72009-01-04 16:23:29 +0100915 kfree(r);
Jay Fenlason45ee3192008-12-21 16:47:17 +0100916
917 return ret;
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200918}
919
Stefan Richter6e95dea2010-02-21 17:56:21 +0100920static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200921{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100922 return release_client_resource(client, arg->remove_descriptor.handle,
Jay Fenlason45ee3192008-12-21 16:47:17 +0100923 release_descriptor, NULL);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +0200924}
925
Stefan Richter53dca512008-12-14 21:47:04 +0100926static void iso_callback(struct fw_iso_context *context, u32 cycle,
927 size_t header_length, void *header, void *data)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500928{
929 struct client *client = data;
Stefan Richter97c18b72009-01-04 16:23:29 +0100930 struct iso_interrupt_event *e;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500931
Stefan Richter56d04cb2010-06-08 00:20:10 +0200932 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
Stefan Richterbf54e142010-07-16 22:25:51 +0200933 if (e == NULL) {
Stefan Richter26b49502012-02-18 22:03:14 +0100934 fw_notice(context->card, "out of memory when allocating event\n");
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500935 return;
Stefan Richterbf54e142010-07-16 22:25:51 +0200936 }
Stefan Richter97c18b72009-01-04 16:23:29 +0100937 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
938 e->interrupt.closure = client->iso_closure;
939 e->interrupt.cycle = cycle;
940 e->interrupt.header_length = header_length;
941 memcpy(e->interrupt.header, header, header_length);
942 queue_event(client, &e->event, &e->interrupt,
943 sizeof(e->interrupt) + header_length, NULL, 0);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500944}
945
Stefan Richter872e3302010-07-29 18:19:22 +0200946static void iso_mc_callback(struct fw_iso_context *context,
947 dma_addr_t completed, void *data)
948{
949 struct client *client = data;
950 struct iso_interrupt_mc_event *e;
951
952 e = kmalloc(sizeof(*e), GFP_ATOMIC);
953 if (e == NULL) {
Stefan Richter26b49502012-02-18 22:03:14 +0100954 fw_notice(context->card, "out of memory when allocating event\n");
Stefan Richter872e3302010-07-29 18:19:22 +0200955 return;
956 }
957 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
958 e->interrupt.closure = client->iso_closure;
959 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
960 completed);
961 queue_event(client, &e->event, &e->interrupt,
962 sizeof(e->interrupt), NULL, 0);
963}
964
Stefan Richter0b6c4852012-04-09 20:51:18 +0200965static enum dma_data_direction iso_dma_direction(struct fw_iso_context *context)
966{
967 if (context->type == FW_ISO_CONTEXT_TRANSMIT)
968 return DMA_TO_DEVICE;
969 else
970 return DMA_FROM_DEVICE;
971}
972
Stefan Richter6e95dea2010-02-21 17:56:21 +0100973static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500974{
Stefan Richter6e95dea2010-02-21 17:56:21 +0100975 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
Kristian Høgsberg24315c52007-06-20 17:48:07 -0400976 struct fw_iso_context *context;
Stefan Richter872e3302010-07-29 18:19:22 +0200977 fw_iso_callback_t cb;
Stefan Richter0b6c4852012-04-09 20:51:18 +0200978 int ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -0500979
Stefan Richtereb5b35a2010-07-07 14:13:14 +0200980 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
Stefan Richter872e3302010-07-29 18:19:22 +0200981 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
982 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
983 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -0500984
Stefan Richter6e95dea2010-02-21 17:56:21 +0100985 switch (a->type) {
Stefan Richter872e3302010-07-29 18:19:22 +0200986 case FW_ISO_CONTEXT_TRANSMIT:
987 if (a->speed > SCODE_3200 || a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400988 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200989
990 cb = iso_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400991 break;
992
Stefan Richter872e3302010-07-29 18:19:22 +0200993 case FW_ISO_CONTEXT_RECEIVE:
994 if (a->header_size < 4 || (a->header_size & 3) ||
995 a->channel > 63)
Kristian Høgsbergc70dc782007-03-14 17:34:53 -0400996 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +0200997
998 cb = iso_callback;
999 break;
1000
1001 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1002 cb = (fw_iso_callback_t)iso_mc_callback;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001003 break;
1004
1005 default:
Kristian Høgsberg21efb3c2007-02-16 17:34:50 -05001006 return -EINVAL;
Kristian Høgsbergc70dc782007-03-14 17:34:53 -04001007 }
1008
Stefan Richter6e95dea2010-02-21 17:56:21 +01001009 context = fw_iso_context_create(client->device->card, a->type,
Stefan Richter872e3302010-07-29 18:19:22 +02001010 a->channel, a->speed, a->header_size, cb, client);
Kristian Høgsberg24315c52007-06-20 17:48:07 -04001011 if (IS_ERR(context))
1012 return PTR_ERR(context);
1013
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001014 /* We only support one context at this time. */
1015 spin_lock_irq(&client->lock);
1016 if (client->iso_context != NULL) {
1017 spin_unlock_irq(&client->lock);
1018 fw_iso_context_destroy(context);
Stefan Richter0b6c4852012-04-09 20:51:18 +02001019
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001020 return -EBUSY;
1021 }
Stefan Richter0b6c4852012-04-09 20:51:18 +02001022 if (!client->buffer_is_mapped) {
1023 ret = fw_iso_buffer_map_dma(&client->buffer,
1024 client->device->card,
1025 iso_dma_direction(context));
1026 if (ret < 0) {
1027 spin_unlock_irq(&client->lock);
1028 fw_iso_context_destroy(context);
1029
1030 return ret;
1031 }
1032 client->buffer_is_mapped = true;
1033 }
Stefan Richter6e95dea2010-02-21 17:56:21 +01001034 client->iso_closure = a->closure;
Kristian Høgsberg24315c52007-06-20 17:48:07 -04001035 client->iso_context = context;
Clemens Ladischbdfe2732010-06-14 11:46:25 +02001036 spin_unlock_irq(&client->lock);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001037
Stefan Richter6e95dea2010-02-21 17:56:21 +01001038 a->handle = 0;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001039
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001040 return 0;
1041}
1042
Stefan Richter872e3302010-07-29 18:19:22 +02001043static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
1044{
1045 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
1046 struct fw_iso_context *ctx = client->iso_context;
1047
1048 if (ctx == NULL || a->handle != 0)
1049 return -EINVAL;
1050
1051 return fw_iso_context_set_channels(ctx, &a->channels);
1052}
1053
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001054/* Macros for decoding the iso packet control header. */
1055#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
1056#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
1057#define GET_SKIP(v) (((v) >> 17) & 0x01)
Stefan Richter7a100342008-09-12 18:09:55 +02001058#define GET_TAG(v) (((v) >> 18) & 0x03)
1059#define GET_SY(v) (((v) >> 20) & 0x0f)
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001060#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
1061
Stefan Richter6e95dea2010-02-21 17:56:21 +01001062static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001063{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001064 struct fw_cdev_queue_iso *a = &arg->queue_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001065 struct fw_cdev_iso_packet __user *p, *end, *next;
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001066 struct fw_iso_context *ctx = client->iso_context;
Stefan Richter872e3302010-07-29 18:19:22 +02001067 unsigned long payload, buffer_end, transmit_header_bytes = 0;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001068 u32 control;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001069 int count;
1070 struct {
1071 struct fw_iso_packet packet;
1072 u8 header[256];
1073 } u;
1074
Stefan Richter6e95dea2010-02-21 17:56:21 +01001075 if (ctx == NULL || a->handle != 0)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001076 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001077
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001078 /*
1079 * If the user passes a non-NULL data pointer, has mmap()'ed
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001080 * the iso buffer, and the pointer points inside the buffer,
1081 * we setup the payload pointers accordingly. Otherwise we
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001082 * set them both to 0, which will still let packets with
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001083 * payload_length == 0 through. In other words, if no packets
1084 * use the indirect payload, the iso buffer need not be mapped
Stefan Richter6e95dea2010-02-21 17:56:21 +01001085 * and the a->data pointer is ignored.
Kristian Høgsbergc781c062007-05-07 20:33:32 -04001086 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001087 payload = (unsigned long)a->data - client->vm_start;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001088 buffer_end = client->buffer.page_count << PAGE_SHIFT;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001089 if (a->data == 0 || client->buffer.pages == NULL ||
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001090 payload >= buffer_end) {
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001091 payload = 0;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001092 buffer_end = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001093 }
1094
Stefan Richter872e3302010-07-29 18:19:22 +02001095 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1096 return -EINVAL;
Al Viro1ccc9142007-10-14 19:34:40 +01001097
Stefan Richter872e3302010-07-29 18:19:22 +02001098 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
Stefan Richter6e95dea2010-02-21 17:56:21 +01001099 if (!access_ok(VERIFY_READ, p, a->size))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001100 return -EFAULT;
1101
Stefan Richter6e95dea2010-02-21 17:56:21 +01001102 end = (void __user *)p + a->size;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001103 count = 0;
1104 while (p < end) {
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001105 if (get_user(control, &p->control))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001106 return -EFAULT;
Kristian Høgsberg1ca31ae2007-05-31 11:16:43 -04001107 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1108 u.packet.interrupt = GET_INTERRUPT(control);
1109 u.packet.skip = GET_SKIP(control);
1110 u.packet.tag = GET_TAG(control);
1111 u.packet.sy = GET_SY(control);
1112 u.packet.header_length = GET_HEADER_LENGTH(control);
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001113
Stefan Richter872e3302010-07-29 18:19:22 +02001114 switch (ctx->type) {
1115 case FW_ISO_CONTEXT_TRANSMIT:
1116 if (u.packet.header_length & 3)
Clemens Ladisch385ab5bc2010-03-31 16:26:46 +02001117 return -EINVAL;
Stefan Richterae2a97662010-07-29 09:31:56 +02001118 transmit_header_bytes = u.packet.header_length;
Stefan Richter872e3302010-07-29 18:19:22 +02001119 break;
1120
1121 case FW_ISO_CONTEXT_RECEIVE:
Stefan Richter69e61d02010-07-28 23:49:45 +02001122 if (u.packet.header_length == 0 ||
1123 u.packet.header_length % ctx->header_size != 0)
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001124 return -EINVAL;
Stefan Richter872e3302010-07-29 18:19:22 +02001125 break;
1126
1127 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1128 if (u.packet.payload_length == 0 ||
1129 u.packet.payload_length & 3)
1130 return -EINVAL;
1131 break;
Kristian Høgsberg295e3fe2007-02-16 17:34:40 -05001132 }
1133
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001134 next = (struct fw_cdev_iso_packet __user *)
Stefan Richterae2a97662010-07-29 09:31:56 +02001135 &p->header[transmit_header_bytes / 4];
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001136 if (next > end)
1137 return -EINVAL;
1138 if (__copy_from_user
Stefan Richterae2a97662010-07-29 09:31:56 +02001139 (u.packet.header, p->header, transmit_header_bytes))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001140 return -EFAULT;
Kristian Høgsberg98b6cbe2007-02-16 17:34:51 -05001141 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001142 u.packet.header_length + u.packet.payload_length > 0)
1143 return -EINVAL;
Kristian Høgsbergef370ee2007-03-28 20:46:23 +02001144 if (payload + u.packet.payload_length > buffer_end)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001145 return -EINVAL;
1146
Kristian Høgsberg9b32d5f2007-02-16 17:34:44 -05001147 if (fw_iso_context_queue(ctx, &u.packet,
1148 &client->buffer, payload))
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001149 break;
1150
1151 p = next;
1152 payload += u.packet.payload_length;
1153 count++;
1154 }
Clemens Ladisch13882a82011-05-02 09:33:56 +02001155 fw_iso_context_queue_flush(ctx);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001156
Stefan Richter6e95dea2010-02-21 17:56:21 +01001157 a->size -= uptr_to_u64(p) - a->packets;
1158 a->packets = uptr_to_u64(p);
1159 a->data = client->vm_start + payload;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001160
1161 return count;
1162}
1163
Stefan Richter6e95dea2010-02-21 17:56:21 +01001164static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001165{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001166 struct fw_cdev_start_iso *a = &arg->start_iso;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001167
Stefan Richtereb5b35a2010-07-07 14:13:14 +02001168 BUILD_BUG_ON(
1169 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1170 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1171 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1172 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1173 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1174
Stefan Richter6e95dea2010-02-21 17:56:21 +01001175 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001176 return -EINVAL;
Stefan Richterfae60312008-02-20 21:10:06 +01001177
Stefan Richter6e95dea2010-02-21 17:56:21 +01001178 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1179 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1180 return -EINVAL;
Kristian Høgsbergeb0306e2007-03-14 17:34:54 -04001181
Stefan Richter6e95dea2010-02-21 17:56:21 +01001182 return fw_iso_context_start(client->iso_context,
1183 a->cycle, a->sync, a->tags);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001184}
1185
Stefan Richter6e95dea2010-02-21 17:56:21 +01001186static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001187{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001188 struct fw_cdev_stop_iso *a = &arg->stop_iso;
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001189
Stefan Richter6e95dea2010-02-21 17:56:21 +01001190 if (client->iso_context == NULL || a->handle != 0)
Kristian Høgsbergabaa5742007-04-30 15:03:14 -04001191 return -EINVAL;
1192
Kristian Høgsbergb8295662007-02-16 17:34:42 -05001193 return fw_iso_context_stop(client->iso_context);
1194}
1195
Clemens Ladischd1bbd202012-03-18 19:06:39 +01001196static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg)
1197{
1198 struct fw_cdev_flush_iso *a = &arg->flush_iso;
1199
1200 if (client->iso_context == NULL || a->handle != 0)
1201 return -EINVAL;
1202
1203 return fw_iso_context_flush_completions(client->iso_context);
1204}
1205
Stefan Richter6e95dea2010-02-21 17:56:21 +01001206static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
Stefan Richtera64408b2007-09-29 10:41:58 +02001207{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001208 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
Stefan Richtera64408b2007-09-29 10:41:58 +02001209 struct fw_card *card = client->device->card;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001210 struct timespec ts = {0, 0};
Stefan Richter4a9bde92010-02-20 22:24:43 +01001211 u32 cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001212 int ret = 0;
Stefan Richtera64408b2007-09-29 10:41:58 +02001213
Stefan Richter4a9bde92010-02-20 22:24:43 +01001214 local_irq_disable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001215
Stefan Richter0fcff4e2010-06-12 20:35:52 +02001216 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001217
Stefan Richter6e95dea2010-02-21 17:56:21 +01001218 switch (a->clk_id) {
Stefan Richterabfe5a02010-02-20 12:13:49 +01001219 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1220 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1221 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1222 default:
1223 ret = -EINVAL;
1224 }
Stefan Richtera64408b2007-09-29 10:41:58 +02001225
Stefan Richter4a9bde92010-02-20 22:24:43 +01001226 local_irq_enable();
Stefan Richtera64408b2007-09-29 10:41:58 +02001227
Stefan Richter6e95dea2010-02-21 17:56:21 +01001228 a->tv_sec = ts.tv_sec;
1229 a->tv_nsec = ts.tv_nsec;
1230 a->cycle_timer = cycle_time;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001231
1232 return ret;
1233}
1234
Stefan Richter6e95dea2010-02-21 17:56:21 +01001235static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
Stefan Richterabfe5a02010-02-20 12:13:49 +01001236{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001237 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
Stefan Richterabfe5a02010-02-20 12:13:49 +01001238 struct fw_cdev_get_cycle_timer2 ct2;
1239
1240 ct2.clk_id = CLOCK_REALTIME;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001241 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
Stefan Richterabfe5a02010-02-20 12:13:49 +01001242
Stefan Richter6e95dea2010-02-21 17:56:21 +01001243 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1244 a->cycle_timer = ct2.cycle_timer;
Stefan Richter4a9bde92010-02-20 22:24:43 +01001245
Stefan Richtera64408b2007-09-29 10:41:58 +02001246 return 0;
1247}
1248
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001249static void iso_resource_work(struct work_struct *work)
1250{
1251 struct iso_resource_event *e;
1252 struct iso_resource *r =
1253 container_of(work, struct iso_resource, work.work);
1254 struct client *client = r->client;
1255 int generation, channel, bandwidth, todo;
1256 bool skip, free, success;
1257
1258 spin_lock_irq(&client->lock);
1259 generation = client->device->generation;
1260 todo = r->todo;
1261 /* Allow 1000ms grace period for other reallocations. */
1262 if (todo == ISO_RES_ALLOC &&
Clemens Ladische71084a2011-01-22 15:05:03 +01001263 time_before64(get_jiffies_64(),
1264 client->device->card->reset_jiffies + HZ)) {
Stefan Richter9fb551b2009-10-08 00:41:10 +02001265 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001266 skip = true;
1267 } else {
1268 /* We could be called twice within the same generation. */
1269 skip = todo == ISO_RES_REALLOC &&
1270 r->generation == generation;
1271 }
Stefan Richter1ec3c022009-01-04 16:23:29 +01001272 free = todo == ISO_RES_DEALLOC ||
1273 todo == ISO_RES_ALLOC_ONCE ||
1274 todo == ISO_RES_DEALLOC_ONCE;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001275 r->generation = generation;
1276 spin_unlock_irq(&client->lock);
1277
1278 if (skip)
1279 goto out;
1280
1281 bandwidth = r->bandwidth;
1282
1283 fw_iso_resource_manage(client->device->card, generation,
1284 r->channels, &channel, &bandwidth,
Stefan Richter1ec3c022009-01-04 16:23:29 +01001285 todo == ISO_RES_ALLOC ||
1286 todo == ISO_RES_REALLOC ||
Stefan Richterf30e6d32011-04-22 15:13:54 +02001287 todo == ISO_RES_ALLOC_ONCE);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001288 /*
1289 * Is this generation outdated already? As long as this resource sticks
1290 * in the idr, it will be scheduled again for a newer generation or at
1291 * shutdown.
1292 */
1293 if (channel == -EAGAIN &&
1294 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1295 goto out;
1296
1297 success = channel >= 0 || bandwidth > 0;
1298
1299 spin_lock_irq(&client->lock);
1300 /*
1301 * Transit from allocation to reallocation, except if the client
1302 * requested deallocation in the meantime.
1303 */
1304 if (r->todo == ISO_RES_ALLOC)
1305 r->todo = ISO_RES_REALLOC;
1306 /*
1307 * Allocation or reallocation failure? Pull this resource out of the
1308 * idr and prepare for deletion, unless the client is shutting down.
1309 */
1310 if (r->todo == ISO_RES_REALLOC && !success &&
1311 !client->in_shutdown &&
1312 idr_find(&client->resource_idr, r->resource.handle)) {
1313 idr_remove(&client->resource_idr, r->resource.handle);
1314 client_put(client);
1315 free = true;
1316 }
1317 spin_unlock_irq(&client->lock);
1318
1319 if (todo == ISO_RES_ALLOC && channel >= 0)
Stefan Richter5d9cb7d272009-01-08 23:07:40 +01001320 r->channels = 1ULL << channel;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001321
1322 if (todo == ISO_RES_REALLOC && success)
1323 goto out;
1324
Stefan Richter1ec3c022009-01-04 16:23:29 +01001325 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001326 e = r->e_alloc;
1327 r->e_alloc = NULL;
1328 } else {
1329 e = r->e_dealloc;
1330 r->e_dealloc = NULL;
1331 }
Stefan Richtere21fcf72009-10-08 00:41:38 +02001332 e->iso_resource.handle = r->resource.handle;
1333 e->iso_resource.channel = channel;
1334 e->iso_resource.bandwidth = bandwidth;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001335
1336 queue_event(client, &e->event,
Stefan Richtere21fcf72009-10-08 00:41:38 +02001337 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001338
1339 if (free) {
1340 cancel_delayed_work(&r->work);
1341 kfree(r->e_alloc);
1342 kfree(r->e_dealloc);
1343 kfree(r);
1344 }
1345 out:
1346 client_put(client);
1347}
1348
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001349static void release_iso_resource(struct client *client,
1350 struct client_resource *resource)
1351{
1352 struct iso_resource *r =
1353 container_of(resource, struct iso_resource, resource);
1354
1355 spin_lock_irq(&client->lock);
1356 r->todo = ISO_RES_DEALLOC;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001357 schedule_iso_resource(r, 0);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001358 spin_unlock_irq(&client->lock);
1359}
1360
Stefan Richter1ec3c022009-01-04 16:23:29 +01001361static int init_iso_resource(struct client *client,
1362 struct fw_cdev_allocate_iso_resource *request, int todo)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001363{
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001364 struct iso_resource_event *e1, *e2;
1365 struct iso_resource *r;
1366 int ret;
1367
1368 if ((request->channels == 0 && request->bandwidth == 0) ||
1369 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1370 request->bandwidth < 0)
1371 return -EINVAL;
1372
1373 r = kmalloc(sizeof(*r), GFP_KERNEL);
1374 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1375 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1376 if (r == NULL || e1 == NULL || e2 == NULL) {
1377 ret = -ENOMEM;
1378 goto fail;
1379 }
1380
1381 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1382 r->client = client;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001383 r->todo = todo;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001384 r->generation = -1;
1385 r->channels = request->channels;
1386 r->bandwidth = request->bandwidth;
1387 r->e_alloc = e1;
1388 r->e_dealloc = e2;
1389
Stefan Richtere21fcf72009-10-08 00:41:38 +02001390 e1->iso_resource.closure = request->closure;
1391 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1392 e2->iso_resource.closure = request->closure;
1393 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001394
Stefan Richter1ec3c022009-01-04 16:23:29 +01001395 if (todo == ISO_RES_ALLOC) {
1396 r->resource.release = release_iso_resource;
1397 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
Stefan Richter81610b82009-01-11 13:44:46 +01001398 if (ret < 0)
1399 goto fail;
Stefan Richter1ec3c022009-01-04 16:23:29 +01001400 } else {
1401 r->resource.release = NULL;
1402 r->resource.handle = -1;
Stefan Richter9fb551b2009-10-08 00:41:10 +02001403 schedule_iso_resource(r, 0);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001404 }
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001405 request->handle = r->resource.handle;
1406
1407 return 0;
1408 fail:
1409 kfree(r);
1410 kfree(e1);
1411 kfree(e2);
1412
1413 return ret;
1414}
1415
Stefan Richter6e95dea2010-02-21 17:56:21 +01001416static int ioctl_allocate_iso_resource(struct client *client,
1417 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001418{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001419 return init_iso_resource(client,
1420 &arg->allocate_iso_resource, ISO_RES_ALLOC);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001421}
1422
Stefan Richter6e95dea2010-02-21 17:56:21 +01001423static int ioctl_deallocate_iso_resource(struct client *client,
1424 union ioctl_arg *arg)
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001425{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001426 return release_client_resource(client,
1427 arg->deallocate.handle, release_iso_resource, NULL);
Jay Fenlason, Stefan Richterb1bda4c2009-01-04 16:23:29 +01001428}
1429
Stefan Richter6e95dea2010-02-21 17:56:21 +01001430static int ioctl_allocate_iso_resource_once(struct client *client,
1431 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001432{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001433 return init_iso_resource(client,
1434 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001435}
1436
Stefan Richter6e95dea2010-02-21 17:56:21 +01001437static int ioctl_deallocate_iso_resource_once(struct client *client,
1438 union ioctl_arg *arg)
Stefan Richter1ec3c022009-01-04 16:23:29 +01001439{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001440 return init_iso_resource(client,
1441 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
Stefan Richter1ec3c022009-01-04 16:23:29 +01001442}
1443
Stefan Richterc8a25902009-03-10 20:59:16 +01001444/*
1445 * Returns a speed code: Maximum speed to or from this device,
1446 * limited by the device's link speed, the local node's link speed,
1447 * and all PHY port speeds between the two links.
1448 */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001449static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
Stefan Richter33580a32009-01-04 16:23:29 +01001450{
Stefan Richterc8a25902009-03-10 20:59:16 +01001451 return client->device->max_speed;
Stefan Richter33580a32009-01-04 16:23:29 +01001452}
1453
Stefan Richter6e95dea2010-02-21 17:56:21 +01001454static int ioctl_send_broadcast_request(struct client *client,
1455 union ioctl_arg *arg)
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001456{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001457 struct fw_cdev_send_request *a = &arg->send_request;
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001458
Stefan Richter6e95dea2010-02-21 17:56:21 +01001459 switch (a->tcode) {
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001460 case TCODE_WRITE_QUADLET_REQUEST:
1461 case TCODE_WRITE_BLOCK_REQUEST:
1462 break;
1463 default:
1464 return -EINVAL;
1465 }
1466
Stefan Richter1566f3d2009-01-04 16:23:29 +01001467 /* Security policy: Only allow accesses to Units Space. */
Stefan Richter6e95dea2010-02-21 17:56:21 +01001468 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
Stefan Richter1566f3d2009-01-04 16:23:29 +01001469 return -EACCES;
1470
Stefan Richter6e95dea2010-02-21 17:56:21 +01001471 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
Jay Fenlason, Stefan Richteracfe8332009-01-04 16:23:29 +01001472}
1473
Stefan Richter6e95dea2010-02-21 17:56:21 +01001474static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001475{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001476 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
Stefan Richter18e9b102009-03-10 21:02:21 +01001477 struct fw_cdev_send_request request;
1478 int dest;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001479
Stefan Richter6e95dea2010-02-21 17:56:21 +01001480 if (a->speed > client->device->card->link_speed ||
1481 a->length > 1024 << a->speed)
Stefan Richter18e9b102009-03-10 21:02:21 +01001482 return -EIO;
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001483
Stefan Richter6e95dea2010-02-21 17:56:21 +01001484 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
Stefan Richter18e9b102009-03-10 21:02:21 +01001485 return -EINVAL;
1486
Stefan Richter6e95dea2010-02-21 17:56:21 +01001487 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
Stefan Richter18e9b102009-03-10 21:02:21 +01001488 request.tcode = TCODE_STREAM_DATA;
Stefan Richter6e95dea2010-02-21 17:56:21 +01001489 request.length = a->length;
1490 request.closure = a->closure;
1491 request.data = a->data;
1492 request.generation = a->generation;
Stefan Richter18e9b102009-03-10 21:02:21 +01001493
Stefan Richter6e95dea2010-02-21 17:56:21 +01001494 return init_request(client, &request, dest, a->speed);
Jay Fenlasonf8c22872009-03-05 19:08:40 +01001495}
1496
Stefan Richter850bb6f2010-07-16 22:25:14 +02001497static void outbound_phy_packet_callback(struct fw_packet *packet,
1498 struct fw_card *card, int status)
1499{
1500 struct outbound_phy_packet_event *e =
1501 container_of(packet, struct outbound_phy_packet_event, p);
1502
1503 switch (status) {
1504 /* expected: */
1505 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1506 /* should never happen with PHY packets: */
1507 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1508 case ACK_BUSY_X:
1509 case ACK_BUSY_A:
1510 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1511 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1512 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1513 /* stale generation; cancelled; on certain controllers: no ack */
1514 default: e->phy_packet.rcode = status; break;
1515 }
Stefan Richtercc550212010-07-18 13:00:50 +02001516 e->phy_packet.data[0] = packet->timestamp;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001517
Stefan Richtercc550212010-07-18 13:00:50 +02001518 queue_event(e->client, &e->event, &e->phy_packet,
1519 sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001520 client_put(e->client);
1521}
1522
1523static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1524{
1525 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1526 struct fw_card *card = client->device->card;
1527 struct outbound_phy_packet_event *e;
1528
1529 /* Access policy: Allow this ioctl only on local nodes' device files. */
1530 if (!client->device->is_local)
1531 return -ENOSYS;
1532
Stefan Richtercc550212010-07-18 13:00:50 +02001533 e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
Stefan Richter850bb6f2010-07-16 22:25:14 +02001534 if (e == NULL)
1535 return -ENOMEM;
1536
1537 client_get(client);
1538 e->client = client;
1539 e->p.speed = SCODE_100;
1540 e->p.generation = a->generation;
Clemens Ladisch5b06db12010-11-30 08:24:47 +01001541 e->p.header[0] = TCODE_LINK_INTERNAL << 4;
1542 e->p.header[1] = a->data[0];
1543 e->p.header[2] = a->data[1];
1544 e->p.header_length = 12;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001545 e->p.callback = outbound_phy_packet_callback;
1546 e->phy_packet.closure = a->closure;
1547 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
Stefan Richtercc550212010-07-18 13:00:50 +02001548 if (is_ping_packet(a->data))
1549 e->phy_packet.length = 4;
Stefan Richter850bb6f2010-07-16 22:25:14 +02001550
1551 card->driver->send_request(card, &e->p);
1552
1553 return 0;
1554}
1555
Stefan Richterbf54e142010-07-16 22:25:51 +02001556static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
1557{
1558 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
1559 struct fw_card *card = client->device->card;
1560
1561 /* Access policy: Allow this ioctl only on local nodes' device files. */
1562 if (!client->device->is_local)
1563 return -ENOSYS;
1564
1565 spin_lock_irq(&card->lock);
1566
1567 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
1568 client->phy_receiver_closure = a->closure;
1569
1570 spin_unlock_irq(&card->lock);
1571
1572 return 0;
1573}
1574
1575void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
1576{
1577 struct client *client;
1578 struct inbound_phy_packet_event *e;
1579 unsigned long flags;
1580
1581 spin_lock_irqsave(&card->lock, flags);
1582
1583 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
1584 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
1585 if (e == NULL) {
Stefan Richter26b49502012-02-18 22:03:14 +01001586 fw_notice(card, "out of memory when allocating event\n");
Stefan Richterbf54e142010-07-16 22:25:51 +02001587 break;
1588 }
1589 e->phy_packet.closure = client->phy_receiver_closure;
1590 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
1591 e->phy_packet.rcode = RCODE_COMPLETE;
1592 e->phy_packet.length = 8;
1593 e->phy_packet.data[0] = p->header[1];
1594 e->phy_packet.data[1] = p->header[2];
1595 queue_event(client, &e->event,
1596 &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
1597 }
1598
1599 spin_unlock_irqrestore(&card->lock, flags);
1600}
1601
Stefan Richter6e95dea2010-02-21 17:56:21 +01001602static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
Stefan Richterb9dc61cf2010-07-16 22:24:29 +02001603 [0x00] = ioctl_get_info,
1604 [0x01] = ioctl_send_request,
1605 [0x02] = ioctl_allocate,
1606 [0x03] = ioctl_deallocate,
1607 [0x04] = ioctl_send_response,
1608 [0x05] = ioctl_initiate_bus_reset,
1609 [0x06] = ioctl_add_descriptor,
1610 [0x07] = ioctl_remove_descriptor,
1611 [0x08] = ioctl_create_iso_context,
1612 [0x09] = ioctl_queue_iso,
1613 [0x0a] = ioctl_start_iso,
1614 [0x0b] = ioctl_stop_iso,
1615 [0x0c] = ioctl_get_cycle_timer,
1616 [0x0d] = ioctl_allocate_iso_resource,
1617 [0x0e] = ioctl_deallocate_iso_resource,
1618 [0x0f] = ioctl_allocate_iso_resource_once,
1619 [0x10] = ioctl_deallocate_iso_resource_once,
1620 [0x11] = ioctl_get_speed,
1621 [0x12] = ioctl_send_broadcast_request,
1622 [0x13] = ioctl_send_stream_packet,
1623 [0x14] = ioctl_get_cycle_timer2,
Stefan Richter850bb6f2010-07-16 22:25:14 +02001624 [0x15] = ioctl_send_phy_packet,
Stefan Richterbf54e142010-07-16 22:25:51 +02001625 [0x16] = ioctl_receive_phy_packets,
Stefan Richter872e3302010-07-29 18:19:22 +02001626 [0x17] = ioctl_set_iso_channels,
Clemens Ladischd1bbd202012-03-18 19:06:39 +01001627 [0x18] = ioctl_flush_iso,
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001628};
1629
Stefan Richter53dca512008-12-14 21:47:04 +01001630static int dispatch_ioctl(struct client *client,
1631 unsigned int cmd, void __user *arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001632{
Stefan Richter6e95dea2010-02-21 17:56:21 +01001633 union ioctl_arg buffer;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001634 int ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001635
Stefan Richter64582292010-02-21 17:56:42 +01001636 if (fw_device_is_shutdown(client->device))
1637 return -ENODEV;
1638
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001639 if (_IOC_TYPE(cmd) != '#' ||
Stefan Richter9cac00b2010-04-07 08:30:50 +02001640 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1641 _IOC_SIZE(cmd) > sizeof(buffer))
Stefan Richterd873d792011-07-09 16:42:26 +02001642 return -ENOTTY;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001643
Stefan Richter9cac00b2010-04-07 08:30:50 +02001644 if (_IOC_DIR(cmd) == _IOC_READ)
1645 memset(&buffer, 0, _IOC_SIZE(cmd));
1646
1647 if (_IOC_DIR(cmd) & _IOC_WRITE)
1648 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001649 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001650
Stefan Richter6e95dea2010-02-21 17:56:21 +01001651 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001652 if (ret < 0)
1653 return ret;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001654
Stefan Richter9cac00b2010-04-07 08:30:50 +02001655 if (_IOC_DIR(cmd) & _IOC_READ)
1656 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001657 return -EFAULT;
Kristian Høgsberg4f259222007-04-30 15:03:13 -04001658
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001659 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001660}
1661
Stefan Richter53dca512008-12-14 21:47:04 +01001662static long fw_device_op_ioctl(struct file *file,
1663 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001664{
Stefan Richter64582292010-02-21 17:56:42 +01001665 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001666}
1667
1668#ifdef CONFIG_COMPAT
Stefan Richter53dca512008-12-14 21:47:04 +01001669static long fw_device_op_compat_ioctl(struct file *file,
1670 unsigned int cmd, unsigned long arg)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001671{
Stefan Richter64582292010-02-21 17:56:42 +01001672 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001673}
1674#endif
1675
1676static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1677{
1678 struct client *client = file->private_data;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001679 unsigned long size;
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001680 int page_count, ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001681
Jay Fenlason551f4cb2008-05-16 11:15:23 -04001682 if (fw_device_is_shutdown(client->device))
1683 return -ENODEV;
1684
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001685 /* FIXME: We could support multiple buffers, but we don't. */
1686 if (client->buffer.pages != NULL)
1687 return -EBUSY;
1688
1689 if (!(vma->vm_flags & VM_SHARED))
1690 return -EINVAL;
1691
1692 if (vma->vm_start & ~PAGE_MASK)
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001693 return -EINVAL;
1694
1695 client->vm_start = vma->vm_start;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001696 size = vma->vm_end - vma->vm_start;
1697 page_count = size >> PAGE_SHIFT;
1698 if (size & ~PAGE_MASK)
1699 return -EINVAL;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001700
Stefan Richter0b6c4852012-04-09 20:51:18 +02001701 ret = fw_iso_buffer_alloc(&client->buffer, page_count);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001702 if (ret < 0)
1703 return ret;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001704
Stefan Richter0b6c4852012-04-09 20:51:18 +02001705 spin_lock_irq(&client->lock);
1706 if (client->iso_context) {
1707 ret = fw_iso_buffer_map_dma(&client->buffer,
1708 client->device->card,
1709 iso_dma_direction(client->iso_context));
1710 client->buffer_is_mapped = (ret == 0);
1711 }
1712 spin_unlock_irq(&client->lock);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001713 if (ret < 0)
Stefan Richter0b6c4852012-04-09 20:51:18 +02001714 goto fail;
Kristian Høgsberg9aad8122007-02-16 17:34:38 -05001715
Stefan Richter0b6c4852012-04-09 20:51:18 +02001716 ret = fw_iso_buffer_map_vma(&client->buffer, vma);
1717 if (ret < 0)
1718 goto fail;
1719
1720 return 0;
1721 fail:
1722 fw_iso_buffer_destroy(&client->buffer, client->device->card);
Stefan Richter2dbd7d72008-12-14 21:45:45 +01001723 return ret;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001724}
1725
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +01001726static int is_outbound_transaction_resource(int id, void *p, void *data)
1727{
1728 struct client_resource *resource = p;
1729
1730 return resource->release == release_transaction;
1731}
1732
1733static int has_outbound_transactions(struct client *client)
1734{
1735 int ret;
1736
1737 spin_lock_irq(&client->lock);
1738 ret = idr_for_each(&client->resource_idr,
1739 is_outbound_transaction_resource, NULL);
1740 spin_unlock_irq(&client->lock);
1741
1742 return ret;
1743}
1744
Jay Fenlason45ee3192008-12-21 16:47:17 +01001745static int shutdown_resource(int id, void *p, void *data)
1746{
Stefan Richtere21fcf72009-10-08 00:41:38 +02001747 struct client_resource *resource = p;
Jay Fenlason45ee3192008-12-21 16:47:17 +01001748 struct client *client = data;
1749
Stefan Richtere21fcf72009-10-08 00:41:38 +02001750 resource->release(client, resource);
Stefan Richterfb443032009-01-04 16:23:29 +01001751 client_put(client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001752
1753 return 0;
1754}
1755
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001756static int fw_device_op_release(struct inode *inode, struct file *file)
1757{
1758 struct client *client = file->private_data;
Stefan Richtere21fcf72009-10-08 00:41:38 +02001759 struct event *event, *next_event;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001760
Stefan Richterbf54e142010-07-16 22:25:51 +02001761 spin_lock_irq(&client->device->card->lock);
1762 list_del(&client->phy_receiver_link);
1763 spin_unlock_irq(&client->device->card->lock);
1764
Stefan Richter97811e32008-12-14 19:19:23 +01001765 mutex_lock(&client->device->client_list_mutex);
1766 list_del(&client->link);
1767 mutex_unlock(&client->device->client_list_mutex);
1768
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001769 if (client->iso_context)
1770 fw_iso_context_destroy(client->iso_context);
1771
Stefan Richter36a755c2009-01-05 20:28:10 +01001772 if (client->buffer.pages)
1773 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1774
Jay Fenlason45ee3192008-12-21 16:47:17 +01001775 /* Freeze client->resource_idr and client->event_list */
Stefan Richter3ba94982009-01-04 16:23:29 +01001776 spin_lock_irq(&client->lock);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001777 client->in_shutdown = true;
Stefan Richter3ba94982009-01-04 16:23:29 +01001778 spin_unlock_irq(&client->lock);
Kristian Høgsberg66dea3e2007-03-28 21:26:42 +02001779
Clemens Ladisch5a5e62d2011-01-10 17:28:39 +01001780 wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1781
Jay Fenlason45ee3192008-12-21 16:47:17 +01001782 idr_for_each(&client->resource_idr, shutdown_resource, client);
Jay Fenlason45ee3192008-12-21 16:47:17 +01001783 idr_destroy(&client->resource_idr);
Kristian Høgsberg28cf6a02007-03-07 12:12:50 -05001784
Stefan Richtere21fcf72009-10-08 00:41:38 +02001785 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1786 kfree(event);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001787
Stefan Richterfb443032009-01-04 16:23:29 +01001788 client_put(client);
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001789
1790 return 0;
1791}
1792
1793static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1794{
1795 struct client *client = file->private_data;
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001796 unsigned int mask = 0;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001797
1798 poll_wait(file, &client->wait, pt);
1799
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001800 if (fw_device_is_shutdown(client->device))
1801 mask |= POLLHUP | POLLERR;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001802 if (!list_empty(&client->event_list))
Kristian Høgsberg2603bf22007-03-07 12:12:48 -05001803 mask |= POLLIN | POLLRDNORM;
1804
1805 return mask;
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001806}
1807
Stefan Richter21ebcd12007-01-14 15:29:07 +01001808const struct file_operations fw_device_ops = {
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001809 .owner = THIS_MODULE,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001810 .llseek = no_llseek,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001811 .open = fw_device_op_open,
1812 .read = fw_device_op_read,
1813 .unlocked_ioctl = fw_device_op_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001814 .mmap = fw_device_op_mmap,
Stefan Richter3ac26b22010-04-10 16:38:05 +01001815 .release = fw_device_op_release,
1816 .poll = fw_device_op_poll,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001817#ifdef CONFIG_COMPAT
Stefan Richter5af4e5e2007-01-21 20:45:32 +01001818 .compat_ioctl = fw_device_op_compat_ioctl,
Kristian Høgsberg19a15b92006-12-19 19:58:31 -05001819#endif
1820};