Commit | Line | Data |
---|---|---|
c781c06d KH |
1 | /* |
2 | * Char device for device raw access | |
19a15b93 | 3 | * |
c781c06d | 4 | * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net> |
19a15b93 KH |
5 | * |
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> | |
27 | #include <linux/poll.h> | |
a64408b9 SR |
28 | #include <linux/preempt.h> |
29 | #include <linux/time.h> | |
19a15b93 KH |
30 | #include <linux/delay.h> |
31 | #include <linux/mm.h> | |
a3aca3da | 32 | #include <linux/idr.h> |
19a15b93 | 33 | #include <linux/compat.h> |
9640d3d7 | 34 | #include <linux/firewire-cdev.h> |
a64408b9 | 35 | #include <asm/system.h> |
19a15b93 KH |
36 | #include <asm/uaccess.h> |
37 | #include "fw-transaction.h" | |
38 | #include "fw-topology.h" | |
39 | #include "fw-device.h" | |
19a15b93 | 40 | |
3964a449 KH |
41 | struct client; |
42 | struct client_resource { | |
43 | struct list_head link; | |
44 | void (*release)(struct client *client, struct client_resource *r); | |
45 | u32 handle; | |
46 | }; | |
47 | ||
c781c06d KH |
48 | /* |
49 | * dequeue_event() just kfree()'s the event, so the event has to be | |
50 | * the first field in the struct. | |
51 | */ | |
52 | ||
19a15b93 KH |
53 | struct event { |
54 | struct { void *data; size_t size; } v[2]; | |
55 | struct list_head link; | |
56 | }; | |
57 | ||
97bd9efa KH |
58 | struct bus_reset { |
59 | struct event event; | |
60 | struct fw_cdev_event_bus_reset reset; | |
61 | }; | |
62 | ||
19a15b93 KH |
63 | struct response { |
64 | struct event event; | |
65 | struct fw_transaction transaction; | |
66 | struct client *client; | |
3964a449 | 67 | struct client_resource resource; |
19a15b93 KH |
68 | struct fw_cdev_event_response response; |
69 | }; | |
70 | ||
71 | struct iso_interrupt { | |
72 | struct event event; | |
73 | struct fw_cdev_event_iso_interrupt interrupt; | |
74 | }; | |
75 | ||
76 | struct client { | |
344bbc4d | 77 | u32 version; |
19a15b93 KH |
78 | struct fw_device *device; |
79 | spinlock_t lock; | |
66dea3e5 | 80 | u32 resource_handle; |
3964a449 | 81 | struct list_head resource_list; |
19a15b93 | 82 | struct list_head event_list; |
19a15b93 | 83 | wait_queue_head_t wait; |
da8ecffa | 84 | u64 bus_reset_closure; |
9aad8125 | 85 | |
19a15b93 | 86 | struct fw_iso_context *iso_context; |
abaa5743 | 87 | u64 iso_closure; |
9aad8125 KH |
88 | struct fw_iso_buffer buffer; |
89 | unsigned long vm_start; | |
97bd9efa KH |
90 | |
91 | struct list_head link; | |
19a15b93 KH |
92 | }; |
93 | ||
94 | static inline void __user * | |
95 | u64_to_uptr(__u64 value) | |
96 | { | |
97 | return (void __user *)(unsigned long)value; | |
98 | } | |
99 | ||
100 | static inline __u64 | |
101 | uptr_to_u64(void __user *ptr) | |
102 | { | |
103 | return (__u64)(unsigned long)ptr; | |
104 | } | |
105 | ||
106 | static int fw_device_op_open(struct inode *inode, struct file *file) | |
107 | { | |
108 | struct fw_device *device; | |
109 | struct client *client; | |
97bd9efa | 110 | unsigned long flags; |
19a15b93 | 111 | |
96b19062 | 112 | device = fw_device_get_by_devt(inode->i_rdev); |
a3aca3da KH |
113 | if (device == NULL) |
114 | return -ENODEV; | |
19a15b93 | 115 | |
2d826cc5 | 116 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
96b19062 SR |
117 | if (client == NULL) { |
118 | fw_device_put(device); | |
19a15b93 | 119 | return -ENOMEM; |
96b19062 | 120 | } |
19a15b93 | 121 | |
96b19062 | 122 | client->device = device; |
19a15b93 | 123 | INIT_LIST_HEAD(&client->event_list); |
3964a449 | 124 | INIT_LIST_HEAD(&client->resource_list); |
19a15b93 KH |
125 | spin_lock_init(&client->lock); |
126 | init_waitqueue_head(&client->wait); | |
127 | ||
128 | file->private_data = client; | |
129 | ||
97bd9efa KH |
130 | spin_lock_irqsave(&device->card->lock, flags); |
131 | list_add_tail(&client->link, &device->client_list); | |
132 | spin_unlock_irqrestore(&device->card->lock, flags); | |
133 | ||
19a15b93 KH |
134 | return 0; |
135 | } | |
136 | ||
137 | static void queue_event(struct client *client, struct event *event, | |
138 | void *data0, size_t size0, void *data1, size_t size1) | |
139 | { | |
140 | unsigned long flags; | |
141 | ||
142 | event->v[0].data = data0; | |
143 | event->v[0].size = size0; | |
144 | event->v[1].data = data1; | |
145 | event->v[1].size = size1; | |
146 | ||
147 | spin_lock_irqsave(&client->lock, flags); | |
19a15b93 | 148 | list_add_tail(&event->link, &client->event_list); |
19a15b93 | 149 | spin_unlock_irqrestore(&client->lock, flags); |
83431cba JF |
150 | |
151 | wake_up_interruptible(&client->wait); | |
19a15b93 KH |
152 | } |
153 | ||
2603bf21 KH |
154 | static int |
155 | dequeue_event(struct client *client, char __user *buffer, size_t count) | |
19a15b93 KH |
156 | { |
157 | unsigned long flags; | |
158 | struct event *event; | |
159 | size_t size, total; | |
2603bf21 | 160 | int i, retval; |
19a15b93 | 161 | |
2603bf21 KH |
162 | retval = wait_event_interruptible(client->wait, |
163 | !list_empty(&client->event_list) || | |
164 | fw_device_is_shutdown(client->device)); | |
165 | if (retval < 0) | |
166 | return retval; | |
19a15b93 | 167 | |
2603bf21 KH |
168 | if (list_empty(&client->event_list) && |
169 | fw_device_is_shutdown(client->device)) | |
170 | return -ENODEV; | |
19a15b93 | 171 | |
2603bf21 | 172 | spin_lock_irqsave(&client->lock, flags); |
19a15b93 KH |
173 | event = container_of(client->event_list.next, struct event, link); |
174 | list_del(&event->link); | |
19a15b93 KH |
175 | spin_unlock_irqrestore(&client->lock, flags); |
176 | ||
19a15b93 KH |
177 | total = 0; |
178 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { | |
179 | size = min(event->v[i].size, count - total); | |
2603bf21 KH |
180 | if (copy_to_user(buffer + total, event->v[i].data, size)) { |
181 | retval = -EFAULT; | |
19a15b93 | 182 | goto out; |
2603bf21 | 183 | } |
19a15b93 KH |
184 | total += size; |
185 | } | |
186 | retval = total; | |
187 | ||
188 | out: | |
189 | kfree(event); | |
190 | ||
191 | return retval; | |
192 | } | |
193 | ||
194 | static ssize_t | |
195 | fw_device_op_read(struct file *file, | |
196 | char __user *buffer, size_t count, loff_t *offset) | |
197 | { | |
198 | struct client *client = file->private_data; | |
199 | ||
200 | return dequeue_event(client, buffer, count); | |
201 | } | |
202 | ||
344bbc4d KH |
203 | static void |
204 | fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, | |
da8ecffa | 205 | struct client *client) |
344bbc4d | 206 | { |
da8ecffa | 207 | struct fw_card *card = client->device->card; |
344bbc4d | 208 | |
da8ecffa | 209 | event->closure = client->bus_reset_closure; |
344bbc4d | 210 | event->type = FW_CDEV_EVENT_BUS_RESET; |
cf5a56ac | 211 | event->generation = client->device->generation; |
b5d2a5e0 | 212 | smp_rmb(); /* node_id must not be older than generation */ |
da8ecffa | 213 | event->node_id = client->device->node_id; |
344bbc4d KH |
214 | event->local_node_id = card->local_node->node_id; |
215 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ | |
216 | event->irm_node_id = card->irm_node->node_id; | |
217 | event->root_node_id = card->root_node->node_id; | |
344bbc4d KH |
218 | } |
219 | ||
2603bf21 KH |
220 | static void |
221 | for_each_client(struct fw_device *device, | |
222 | void (*callback)(struct client *client)) | |
223 | { | |
224 | struct fw_card *card = device->card; | |
225 | struct client *c; | |
226 | unsigned long flags; | |
227 | ||
228 | spin_lock_irqsave(&card->lock, flags); | |
229 | ||
230 | list_for_each_entry(c, &device->client_list, link) | |
231 | callback(c); | |
232 | ||
233 | spin_unlock_irqrestore(&card->lock, flags); | |
234 | } | |
235 | ||
97bd9efa KH |
236 | static void |
237 | queue_bus_reset_event(struct client *client) | |
238 | { | |
239 | struct bus_reset *bus_reset; | |
97bd9efa | 240 | |
2d826cc5 | 241 | bus_reset = kzalloc(sizeof(*bus_reset), GFP_ATOMIC); |
97bd9efa KH |
242 | if (bus_reset == NULL) { |
243 | fw_notify("Out of memory when allocating bus reset event\n"); | |
244 | return; | |
245 | } | |
246 | ||
da8ecffa | 247 | fill_bus_reset_event(&bus_reset->reset, client); |
97bd9efa KH |
248 | |
249 | queue_event(client, &bus_reset->event, | |
2d826cc5 | 250 | &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0); |
97bd9efa KH |
251 | } |
252 | ||
253 | void fw_device_cdev_update(struct fw_device *device) | |
254 | { | |
2603bf21 KH |
255 | for_each_client(device, queue_bus_reset_event); |
256 | } | |
97bd9efa | 257 | |
2603bf21 KH |
258 | static void wake_up_client(struct client *client) |
259 | { | |
260 | wake_up_interruptible(&client->wait); | |
261 | } | |
97bd9efa | 262 | |
2603bf21 KH |
263 | void fw_device_cdev_remove(struct fw_device *device) |
264 | { | |
265 | for_each_client(device, wake_up_client); | |
97bd9efa KH |
266 | } |
267 | ||
4f259223 | 268 | static int ioctl_get_info(struct client *client, void *buffer) |
19a15b93 | 269 | { |
4f259223 | 270 | struct fw_cdev_get_info *get_info = buffer; |
344bbc4d | 271 | struct fw_cdev_event_bus_reset bus_reset; |
c9755e14 | 272 | unsigned long ret = 0; |
344bbc4d | 273 | |
4f259223 KH |
274 | client->version = get_info->version; |
275 | get_info->version = FW_CDEV_VERSION; | |
344bbc4d | 276 | |
c9755e14 SR |
277 | down_read(&fw_device_rwsem); |
278 | ||
4f259223 KH |
279 | if (get_info->rom != 0) { |
280 | void __user *uptr = u64_to_uptr(get_info->rom); | |
281 | size_t want = get_info->rom_length; | |
d84702a5 | 282 | size_t have = client->device->config_rom_length * 4; |
344bbc4d | 283 | |
c9755e14 SR |
284 | ret = copy_to_user(uptr, client->device->config_rom, |
285 | min(want, have)); | |
344bbc4d | 286 | } |
4f259223 | 287 | get_info->rom_length = client->device->config_rom_length * 4; |
344bbc4d | 288 | |
c9755e14 SR |
289 | up_read(&fw_device_rwsem); |
290 | ||
291 | if (ret != 0) | |
292 | return -EFAULT; | |
293 | ||
4f259223 KH |
294 | client->bus_reset_closure = get_info->bus_reset_closure; |
295 | if (get_info->bus_reset != 0) { | |
296 | void __user *uptr = u64_to_uptr(get_info->bus_reset); | |
344bbc4d | 297 | |
da8ecffa | 298 | fill_bus_reset_event(&bus_reset, client); |
2d826cc5 | 299 | if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset))) |
344bbc4d KH |
300 | return -EFAULT; |
301 | } | |
19a15b93 | 302 | |
4f259223 | 303 | get_info->card = client->device->card->index; |
19a15b93 KH |
304 | |
305 | return 0; | |
306 | } | |
307 | ||
3964a449 KH |
308 | static void |
309 | add_client_resource(struct client *client, struct client_resource *resource) | |
310 | { | |
311 | unsigned long flags; | |
312 | ||
313 | spin_lock_irqsave(&client->lock, flags); | |
314 | list_add_tail(&resource->link, &client->resource_list); | |
315 | resource->handle = client->resource_handle++; | |
316 | spin_unlock_irqrestore(&client->lock, flags); | |
317 | } | |
318 | ||
319 | static int | |
320 | release_client_resource(struct client *client, u32 handle, | |
321 | struct client_resource **resource) | |
322 | { | |
323 | struct client_resource *r; | |
324 | unsigned long flags; | |
325 | ||
326 | spin_lock_irqsave(&client->lock, flags); | |
327 | list_for_each_entry(r, &client->resource_list, link) { | |
328 | if (r->handle == handle) { | |
329 | list_del(&r->link); | |
330 | break; | |
331 | } | |
332 | } | |
333 | spin_unlock_irqrestore(&client->lock, flags); | |
334 | ||
335 | if (&r->link == &client->resource_list) | |
336 | return -EINVAL; | |
337 | ||
338 | if (resource) | |
339 | *resource = r; | |
340 | else | |
341 | r->release(client, r); | |
342 | ||
343 | return 0; | |
344 | } | |
345 | ||
346 | static void | |
347 | release_transaction(struct client *client, struct client_resource *resource) | |
348 | { | |
349 | struct response *response = | |
350 | container_of(resource, struct response, resource); | |
351 | ||
352 | fw_cancel_transaction(client->device->card, &response->transaction); | |
353 | } | |
354 | ||
19a15b93 KH |
355 | static void |
356 | complete_transaction(struct fw_card *card, int rcode, | |
357 | void *payload, size_t length, void *data) | |
358 | { | |
359 | struct response *response = data; | |
360 | struct client *client = response->client; | |
28cf6a04 | 361 | unsigned long flags; |
19a15b93 KH |
362 | |
363 | if (length < response->response.length) | |
364 | response->response.length = length; | |
365 | if (rcode == RCODE_COMPLETE) | |
366 | memcpy(response->response.data, payload, | |
367 | response->response.length); | |
368 | ||
28cf6a04 | 369 | spin_lock_irqsave(&client->lock, flags); |
3964a449 | 370 | list_del(&response->resource.link); |
28cf6a04 KH |
371 | spin_unlock_irqrestore(&client->lock, flags); |
372 | ||
19a15b93 KH |
373 | response->response.type = FW_CDEV_EVENT_RESPONSE; |
374 | response->response.rcode = rcode; | |
375 | queue_event(client, &response->event, | |
2d826cc5 | 376 | &response->response, sizeof(response->response), |
19a15b93 KH |
377 | response->response.data, response->response.length); |
378 | } | |
379 | ||
350958f9 | 380 | static int ioctl_send_request(struct client *client, void *buffer) |
19a15b93 KH |
381 | { |
382 | struct fw_device *device = client->device; | |
4f259223 | 383 | struct fw_cdev_send_request *request = buffer; |
19a15b93 KH |
384 | struct response *response; |
385 | ||
19a15b93 | 386 | /* What is the biggest size we'll accept, really? */ |
4f259223 | 387 | if (request->length > 4096) |
19a15b93 KH |
388 | return -EINVAL; |
389 | ||
2d826cc5 | 390 | response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL); |
19a15b93 KH |
391 | if (response == NULL) |
392 | return -ENOMEM; | |
393 | ||
394 | response->client = client; | |
4f259223 KH |
395 | response->response.length = request->length; |
396 | response->response.closure = request->closure; | |
19a15b93 | 397 | |
4f259223 | 398 | if (request->data && |
19a15b93 | 399 | copy_from_user(response->response.data, |
4f259223 | 400 | u64_to_uptr(request->data), request->length)) { |
19a15b93 KH |
401 | kfree(response); |
402 | return -EFAULT; | |
403 | } | |
404 | ||
3964a449 KH |
405 | response->resource.release = release_transaction; |
406 | add_client_resource(client, &response->resource); | |
28cf6a04 | 407 | |
19a15b93 | 408 | fw_send_request(device->card, &response->transaction, |
4f259223 | 409 | request->tcode & 0x1f, |
907293d7 | 410 | device->node->node_id, |
4f259223 | 411 | request->generation, |
f1397490 | 412 | device->max_speed, |
4f259223 KH |
413 | request->offset, |
414 | response->response.data, request->length, | |
19a15b93 KH |
415 | complete_transaction, response); |
416 | ||
4f259223 | 417 | if (request->data) |
2d826cc5 | 418 | return sizeof(request) + request->length; |
19a15b93 | 419 | else |
2d826cc5 | 420 | return sizeof(request); |
19a15b93 KH |
421 | } |
422 | ||
423 | struct address_handler { | |
424 | struct fw_address_handler handler; | |
425 | __u64 closure; | |
426 | struct client *client; | |
3964a449 | 427 | struct client_resource resource; |
19a15b93 KH |
428 | }; |
429 | ||
430 | struct request { | |
431 | struct fw_request *request; | |
432 | void *data; | |
433 | size_t length; | |
3964a449 | 434 | struct client_resource resource; |
19a15b93 KH |
435 | }; |
436 | ||
437 | struct request_event { | |
438 | struct event event; | |
439 | struct fw_cdev_event_request request; | |
440 | }; | |
441 | ||
3964a449 KH |
442 | static void |
443 | release_request(struct client *client, struct client_resource *resource) | |
444 | { | |
445 | struct request *request = | |
446 | container_of(resource, struct request, resource); | |
447 | ||
448 | fw_send_response(client->device->card, request->request, | |
449 | RCODE_CONFLICT_ERROR); | |
450 | kfree(request); | |
451 | } | |
452 | ||
19a15b93 KH |
453 | static void |
454 | handle_request(struct fw_card *card, struct fw_request *r, | |
455 | int tcode, int destination, int source, | |
456 | int generation, int speed, | |
457 | unsigned long long offset, | |
458 | void *payload, size_t length, void *callback_data) | |
459 | { | |
460 | struct address_handler *handler = callback_data; | |
461 | struct request *request; | |
462 | struct request_event *e; | |
19a15b93 KH |
463 | struct client *client = handler->client; |
464 | ||
2d826cc5 KH |
465 | request = kmalloc(sizeof(*request), GFP_ATOMIC); |
466 | e = kmalloc(sizeof(*e), GFP_ATOMIC); | |
19a15b93 KH |
467 | if (request == NULL || e == NULL) { |
468 | kfree(request); | |
469 | kfree(e); | |
470 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); | |
471 | return; | |
472 | } | |
473 | ||
474 | request->request = r; | |
475 | request->data = payload; | |
476 | request->length = length; | |
477 | ||
3964a449 KH |
478 | request->resource.release = release_request; |
479 | add_client_resource(client, &request->resource); | |
19a15b93 KH |
480 | |
481 | e->request.type = FW_CDEV_EVENT_REQUEST; | |
482 | e->request.tcode = tcode; | |
483 | e->request.offset = offset; | |
484 | e->request.length = length; | |
3964a449 | 485 | e->request.handle = request->resource.handle; |
19a15b93 KH |
486 | e->request.closure = handler->closure; |
487 | ||
488 | queue_event(client, &e->event, | |
2d826cc5 | 489 | &e->request, sizeof(e->request), payload, length); |
19a15b93 KH |
490 | } |
491 | ||
3964a449 KH |
492 | static void |
493 | release_address_handler(struct client *client, | |
494 | struct client_resource *resource) | |
495 | { | |
496 | struct address_handler *handler = | |
497 | container_of(resource, struct address_handler, resource); | |
498 | ||
499 | fw_core_remove_address_handler(&handler->handler); | |
500 | kfree(handler); | |
501 | } | |
502 | ||
4f259223 | 503 | static int ioctl_allocate(struct client *client, void *buffer) |
19a15b93 | 504 | { |
4f259223 | 505 | struct fw_cdev_allocate *request = buffer; |
19a15b93 | 506 | struct address_handler *handler; |
19a15b93 KH |
507 | struct fw_address_region region; |
508 | ||
2d826cc5 | 509 | handler = kmalloc(sizeof(*handler), GFP_KERNEL); |
19a15b93 KH |
510 | if (handler == NULL) |
511 | return -ENOMEM; | |
512 | ||
4f259223 KH |
513 | region.start = request->offset; |
514 | region.end = request->offset + request->length; | |
515 | handler->handler.length = request->length; | |
19a15b93 KH |
516 | handler->handler.address_callback = handle_request; |
517 | handler->handler.callback_data = handler; | |
4f259223 | 518 | handler->closure = request->closure; |
19a15b93 KH |
519 | handler->client = client; |
520 | ||
521 | if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) { | |
522 | kfree(handler); | |
523 | return -EBUSY; | |
524 | } | |
525 | ||
3964a449 KH |
526 | handler->resource.release = release_address_handler; |
527 | add_client_resource(client, &handler->resource); | |
4f259223 | 528 | request->handle = handler->resource.handle; |
19a15b93 KH |
529 | |
530 | return 0; | |
531 | } | |
532 | ||
4f259223 | 533 | static int ioctl_deallocate(struct client *client, void *buffer) |
9472316b | 534 | { |
4f259223 | 535 | struct fw_cdev_deallocate *request = buffer; |
9472316b | 536 | |
4f259223 | 537 | return release_client_resource(client, request->handle, NULL); |
9472316b KH |
538 | } |
539 | ||
4f259223 | 540 | static int ioctl_send_response(struct client *client, void *buffer) |
19a15b93 | 541 | { |
4f259223 | 542 | struct fw_cdev_send_response *request = buffer; |
3964a449 | 543 | struct client_resource *resource; |
19a15b93 | 544 | struct request *r; |
19a15b93 | 545 | |
4f259223 | 546 | if (release_client_resource(client, request->handle, &resource) < 0) |
19a15b93 | 547 | return -EINVAL; |
3964a449 | 548 | r = container_of(resource, struct request, resource); |
4f259223 KH |
549 | if (request->length < r->length) |
550 | r->length = request->length; | |
551 | if (copy_from_user(r->data, u64_to_uptr(request->data), r->length)) | |
19a15b93 KH |
552 | return -EFAULT; |
553 | ||
4f259223 | 554 | fw_send_response(client->device->card, r->request, request->rcode); |
19a15b93 KH |
555 | kfree(r); |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
4f259223 | 560 | static int ioctl_initiate_bus_reset(struct client *client, void *buffer) |
5371842b | 561 | { |
4f259223 | 562 | struct fw_cdev_initiate_bus_reset *request = buffer; |
5371842b KH |
563 | int short_reset; |
564 | ||
4f259223 | 565 | short_reset = (request->type == FW_CDEV_SHORT_RESET); |
5371842b KH |
566 | |
567 | return fw_core_initiate_bus_reset(client->device->card, short_reset); | |
568 | } | |
569 | ||
66dea3e5 KH |
570 | struct descriptor { |
571 | struct fw_descriptor d; | |
3964a449 | 572 | struct client_resource resource; |
66dea3e5 KH |
573 | u32 data[0]; |
574 | }; | |
575 | ||
3964a449 KH |
576 | static void release_descriptor(struct client *client, |
577 | struct client_resource *resource) | |
578 | { | |
579 | struct descriptor *descriptor = | |
580 | container_of(resource, struct descriptor, resource); | |
581 | ||
582 | fw_core_remove_descriptor(&descriptor->d); | |
583 | kfree(descriptor); | |
584 | } | |
585 | ||
4f259223 | 586 | static int ioctl_add_descriptor(struct client *client, void *buffer) |
66dea3e5 | 587 | { |
4f259223 | 588 | struct fw_cdev_add_descriptor *request = buffer; |
66dea3e5 | 589 | struct descriptor *descriptor; |
66dea3e5 KH |
590 | int retval; |
591 | ||
4f259223 | 592 | if (request->length > 256) |
66dea3e5 KH |
593 | return -EINVAL; |
594 | ||
595 | descriptor = | |
2d826cc5 | 596 | kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL); |
66dea3e5 KH |
597 | if (descriptor == NULL) |
598 | return -ENOMEM; | |
599 | ||
600 | if (copy_from_user(descriptor->data, | |
4f259223 | 601 | u64_to_uptr(request->data), request->length * 4)) { |
66dea3e5 KH |
602 | kfree(descriptor); |
603 | return -EFAULT; | |
604 | } | |
605 | ||
4f259223 KH |
606 | descriptor->d.length = request->length; |
607 | descriptor->d.immediate = request->immediate; | |
608 | descriptor->d.key = request->key; | |
66dea3e5 KH |
609 | descriptor->d.data = descriptor->data; |
610 | ||
611 | retval = fw_core_add_descriptor(&descriptor->d); | |
612 | if (retval < 0) { | |
613 | kfree(descriptor); | |
614 | return retval; | |
615 | } | |
616 | ||
3964a449 KH |
617 | descriptor->resource.release = release_descriptor; |
618 | add_client_resource(client, &descriptor->resource); | |
4f259223 | 619 | request->handle = descriptor->resource.handle; |
66dea3e5 KH |
620 | |
621 | return 0; | |
622 | } | |
623 | ||
4f259223 | 624 | static int ioctl_remove_descriptor(struct client *client, void *buffer) |
66dea3e5 | 625 | { |
4f259223 | 626 | struct fw_cdev_remove_descriptor *request = buffer; |
66dea3e5 | 627 | |
4f259223 | 628 | return release_client_resource(client, request->handle, NULL); |
66dea3e5 KH |
629 | } |
630 | ||
19a15b93 | 631 | static void |
9b32d5f3 KH |
632 | iso_callback(struct fw_iso_context *context, u32 cycle, |
633 | size_t header_length, void *header, void *data) | |
19a15b93 KH |
634 | { |
635 | struct client *client = data; | |
930e4b7f | 636 | struct iso_interrupt *irq; |
19a15b93 | 637 | |
930e4b7f SR |
638 | irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC); |
639 | if (irq == NULL) | |
19a15b93 KH |
640 | return; |
641 | ||
930e4b7f SR |
642 | irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; |
643 | irq->interrupt.closure = client->iso_closure; | |
644 | irq->interrupt.cycle = cycle; | |
645 | irq->interrupt.header_length = header_length; | |
646 | memcpy(irq->interrupt.header, header, header_length); | |
647 | queue_event(client, &irq->event, &irq->interrupt, | |
648 | sizeof(irq->interrupt) + header_length, NULL, 0); | |
19a15b93 KH |
649 | } |
650 | ||
4f259223 | 651 | static int ioctl_create_iso_context(struct client *client, void *buffer) |
19a15b93 | 652 | { |
4f259223 | 653 | struct fw_cdev_create_iso_context *request = buffer; |
24315c5e | 654 | struct fw_iso_context *context; |
19a15b93 | 655 | |
fae60312 SR |
656 | /* We only support one context at this time. */ |
657 | if (client->iso_context != NULL) | |
658 | return -EBUSY; | |
659 | ||
4f259223 | 660 | if (request->channel > 63) |
21efb3cf KH |
661 | return -EINVAL; |
662 | ||
4f259223 | 663 | switch (request->type) { |
c70dc788 | 664 | case FW_ISO_CONTEXT_RECEIVE: |
4f259223 | 665 | if (request->header_size < 4 || (request->header_size & 3)) |
c70dc788 | 666 | return -EINVAL; |
98b6cbe8 | 667 | |
c70dc788 KH |
668 | break; |
669 | ||
670 | case FW_ISO_CONTEXT_TRANSMIT: | |
4f259223 | 671 | if (request->speed > SCODE_3200) |
c70dc788 KH |
672 | return -EINVAL; |
673 | ||
674 | break; | |
675 | ||
676 | default: | |
21efb3cf | 677 | return -EINVAL; |
c70dc788 KH |
678 | } |
679 | ||
24315c5e KH |
680 | context = fw_iso_context_create(client->device->card, |
681 | request->type, | |
682 | request->channel, | |
683 | request->speed, | |
684 | request->header_size, | |
685 | iso_callback, client); | |
686 | if (IS_ERR(context)) | |
687 | return PTR_ERR(context); | |
688 | ||
abaa5743 | 689 | client->iso_closure = request->closure; |
24315c5e | 690 | client->iso_context = context; |
19a15b93 | 691 | |
abaa5743 KH |
692 | /* We only support one context at this time. */ |
693 | request->handle = 0; | |
694 | ||
19a15b93 KH |
695 | return 0; |
696 | } | |
697 | ||
1ca31ae7 KH |
698 | /* Macros for decoding the iso packet control header. */ |
699 | #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff) | |
700 | #define GET_INTERRUPT(v) (((v) >> 16) & 0x01) | |
701 | #define GET_SKIP(v) (((v) >> 17) & 0x01) | |
702 | #define GET_TAG(v) (((v) >> 18) & 0x02) | |
703 | #define GET_SY(v) (((v) >> 20) & 0x04) | |
704 | #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff) | |
705 | ||
4f259223 | 706 | static int ioctl_queue_iso(struct client *client, void *buffer) |
19a15b93 | 707 | { |
4f259223 | 708 | struct fw_cdev_queue_iso *request = buffer; |
19a15b93 | 709 | struct fw_cdev_iso_packet __user *p, *end, *next; |
9b32d5f3 | 710 | struct fw_iso_context *ctx = client->iso_context; |
ef370ee7 | 711 | unsigned long payload, buffer_end, header_length; |
1ca31ae7 | 712 | u32 control; |
19a15b93 KH |
713 | int count; |
714 | struct { | |
715 | struct fw_iso_packet packet; | |
716 | u8 header[256]; | |
717 | } u; | |
718 | ||
abaa5743 | 719 | if (ctx == NULL || request->handle != 0) |
19a15b93 | 720 | return -EINVAL; |
19a15b93 | 721 | |
c781c06d KH |
722 | /* |
723 | * If the user passes a non-NULL data pointer, has mmap()'ed | |
19a15b93 KH |
724 | * the iso buffer, and the pointer points inside the buffer, |
725 | * we setup the payload pointers accordingly. Otherwise we | |
9aad8125 | 726 | * set them both to 0, which will still let packets with |
19a15b93 KH |
727 | * payload_length == 0 through. In other words, if no packets |
728 | * use the indirect payload, the iso buffer need not be mapped | |
c781c06d KH |
729 | * and the request->data pointer is ignored. |
730 | */ | |
19a15b93 | 731 | |
4f259223 | 732 | payload = (unsigned long)request->data - client->vm_start; |
ef370ee7 | 733 | buffer_end = client->buffer.page_count << PAGE_SHIFT; |
4f259223 | 734 | if (request->data == 0 || client->buffer.pages == NULL || |
ef370ee7 | 735 | payload >= buffer_end) { |
9aad8125 | 736 | payload = 0; |
ef370ee7 | 737 | buffer_end = 0; |
19a15b93 KH |
738 | } |
739 | ||
1ccc9147 AV |
740 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets); |
741 | ||
742 | if (!access_ok(VERIFY_READ, p, request->size)) | |
19a15b93 KH |
743 | return -EFAULT; |
744 | ||
4f259223 | 745 | end = (void __user *)p + request->size; |
19a15b93 KH |
746 | count = 0; |
747 | while (p < end) { | |
1ca31ae7 | 748 | if (get_user(control, &p->control)) |
19a15b93 | 749 | return -EFAULT; |
1ca31ae7 KH |
750 | u.packet.payload_length = GET_PAYLOAD_LENGTH(control); |
751 | u.packet.interrupt = GET_INTERRUPT(control); | |
752 | u.packet.skip = GET_SKIP(control); | |
753 | u.packet.tag = GET_TAG(control); | |
754 | u.packet.sy = GET_SY(control); | |
755 | u.packet.header_length = GET_HEADER_LENGTH(control); | |
295e3feb | 756 | |
9b32d5f3 | 757 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
295e3feb KH |
758 | header_length = u.packet.header_length; |
759 | } else { | |
c781c06d KH |
760 | /* |
761 | * We require that header_length is a multiple of | |
762 | * the fixed header size, ctx->header_size. | |
763 | */ | |
9b32d5f3 KH |
764 | if (ctx->header_size == 0) { |
765 | if (u.packet.header_length > 0) | |
766 | return -EINVAL; | |
767 | } else if (u.packet.header_length % ctx->header_size != 0) { | |
295e3feb | 768 | return -EINVAL; |
9b32d5f3 | 769 | } |
295e3feb KH |
770 | header_length = 0; |
771 | } | |
772 | ||
19a15b93 | 773 | next = (struct fw_cdev_iso_packet __user *) |
295e3feb | 774 | &p->header[header_length / 4]; |
19a15b93 KH |
775 | if (next > end) |
776 | return -EINVAL; | |
777 | if (__copy_from_user | |
295e3feb | 778 | (u.packet.header, p->header, header_length)) |
19a15b93 | 779 | return -EFAULT; |
98b6cbe8 | 780 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
19a15b93 KH |
781 | u.packet.header_length + u.packet.payload_length > 0) |
782 | return -EINVAL; | |
ef370ee7 | 783 | if (payload + u.packet.payload_length > buffer_end) |
19a15b93 KH |
784 | return -EINVAL; |
785 | ||
9b32d5f3 KH |
786 | if (fw_iso_context_queue(ctx, &u.packet, |
787 | &client->buffer, payload)) | |
19a15b93 KH |
788 | break; |
789 | ||
790 | p = next; | |
791 | payload += u.packet.payload_length; | |
792 | count++; | |
793 | } | |
794 | ||
4f259223 KH |
795 | request->size -= uptr_to_u64(p) - request->packets; |
796 | request->packets = uptr_to_u64(p); | |
797 | request->data = client->vm_start + payload; | |
19a15b93 KH |
798 | |
799 | return count; | |
800 | } | |
801 | ||
4f259223 | 802 | static int ioctl_start_iso(struct client *client, void *buffer) |
19a15b93 | 803 | { |
4f259223 | 804 | struct fw_cdev_start_iso *request = buffer; |
19a15b93 | 805 | |
fae60312 | 806 | if (client->iso_context == NULL || request->handle != 0) |
abaa5743 | 807 | return -EINVAL; |
fae60312 | 808 | |
eb0306ea | 809 | if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) { |
4f259223 | 810 | if (request->tags == 0 || request->tags > 15) |
eb0306ea KH |
811 | return -EINVAL; |
812 | ||
4f259223 | 813 | if (request->sync > 15) |
eb0306ea KH |
814 | return -EINVAL; |
815 | } | |
816 | ||
4f259223 KH |
817 | return fw_iso_context_start(client->iso_context, request->cycle, |
818 | request->sync, request->tags); | |
19a15b93 KH |
819 | } |
820 | ||
4f259223 | 821 | static int ioctl_stop_iso(struct client *client, void *buffer) |
b8295668 | 822 | { |
abaa5743 KH |
823 | struct fw_cdev_stop_iso *request = buffer; |
824 | ||
fae60312 | 825 | if (client->iso_context == NULL || request->handle != 0) |
abaa5743 KH |
826 | return -EINVAL; |
827 | ||
b8295668 KH |
828 | return fw_iso_context_stop(client->iso_context); |
829 | } | |
830 | ||
a64408b9 SR |
831 | static int ioctl_get_cycle_timer(struct client *client, void *buffer) |
832 | { | |
833 | struct fw_cdev_get_cycle_timer *request = buffer; | |
834 | struct fw_card *card = client->device->card; | |
835 | unsigned long long bus_time; | |
836 | struct timeval tv; | |
837 | unsigned long flags; | |
838 | ||
839 | preempt_disable(); | |
840 | local_irq_save(flags); | |
841 | ||
842 | bus_time = card->driver->get_bus_time(card); | |
843 | do_gettimeofday(&tv); | |
844 | ||
845 | local_irq_restore(flags); | |
846 | preempt_enable(); | |
847 | ||
848 | request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec; | |
849 | request->cycle_timer = bus_time & 0xffffffff; | |
850 | return 0; | |
851 | } | |
852 | ||
4f259223 KH |
853 | static int (* const ioctl_handlers[])(struct client *client, void *buffer) = { |
854 | ioctl_get_info, | |
855 | ioctl_send_request, | |
856 | ioctl_allocate, | |
857 | ioctl_deallocate, | |
858 | ioctl_send_response, | |
859 | ioctl_initiate_bus_reset, | |
860 | ioctl_add_descriptor, | |
861 | ioctl_remove_descriptor, | |
862 | ioctl_create_iso_context, | |
863 | ioctl_queue_iso, | |
864 | ioctl_start_iso, | |
865 | ioctl_stop_iso, | |
a64408b9 | 866 | ioctl_get_cycle_timer, |
4f259223 KH |
867 | }; |
868 | ||
19a15b93 KH |
869 | static int |
870 | dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg) | |
871 | { | |
4f259223 KH |
872 | char buffer[256]; |
873 | int retval; | |
874 | ||
875 | if (_IOC_TYPE(cmd) != '#' || | |
876 | _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers)) | |
19a15b93 | 877 | return -EINVAL; |
4f259223 KH |
878 | |
879 | if (_IOC_DIR(cmd) & _IOC_WRITE) { | |
2d826cc5 | 880 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
4f259223 KH |
881 | copy_from_user(buffer, arg, _IOC_SIZE(cmd))) |
882 | return -EFAULT; | |
883 | } | |
884 | ||
885 | retval = ioctl_handlers[_IOC_NR(cmd)](client, buffer); | |
886 | if (retval < 0) | |
887 | return retval; | |
888 | ||
889 | if (_IOC_DIR(cmd) & _IOC_READ) { | |
2d826cc5 | 890 | if (_IOC_SIZE(cmd) > sizeof(buffer) || |
4f259223 KH |
891 | copy_to_user(arg, buffer, _IOC_SIZE(cmd))) |
892 | return -EFAULT; | |
19a15b93 | 893 | } |
4f259223 KH |
894 | |
895 | return 0; | |
19a15b93 KH |
896 | } |
897 | ||
898 | static long | |
899 | fw_device_op_ioctl(struct file *file, | |
900 | unsigned int cmd, unsigned long arg) | |
901 | { | |
902 | struct client *client = file->private_data; | |
903 | ||
904 | return dispatch_ioctl(client, cmd, (void __user *) arg); | |
905 | } | |
906 | ||
907 | #ifdef CONFIG_COMPAT | |
908 | static long | |
909 | fw_device_op_compat_ioctl(struct file *file, | |
910 | unsigned int cmd, unsigned long arg) | |
911 | { | |
912 | struct client *client = file->private_data; | |
913 | ||
914 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); | |
915 | } | |
916 | #endif | |
917 | ||
918 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) | |
919 | { | |
920 | struct client *client = file->private_data; | |
9aad8125 KH |
921 | enum dma_data_direction direction; |
922 | unsigned long size; | |
923 | int page_count, retval; | |
924 | ||
925 | /* FIXME: We could support multiple buffers, but we don't. */ | |
926 | if (client->buffer.pages != NULL) | |
927 | return -EBUSY; | |
928 | ||
929 | if (!(vma->vm_flags & VM_SHARED)) | |
930 | return -EINVAL; | |
19a15b93 | 931 | |
9aad8125 | 932 | if (vma->vm_start & ~PAGE_MASK) |
19a15b93 KH |
933 | return -EINVAL; |
934 | ||
935 | client->vm_start = vma->vm_start; | |
9aad8125 KH |
936 | size = vma->vm_end - vma->vm_start; |
937 | page_count = size >> PAGE_SHIFT; | |
938 | if (size & ~PAGE_MASK) | |
939 | return -EINVAL; | |
940 | ||
941 | if (vma->vm_flags & VM_WRITE) | |
942 | direction = DMA_TO_DEVICE; | |
943 | else | |
944 | direction = DMA_FROM_DEVICE; | |
945 | ||
946 | retval = fw_iso_buffer_init(&client->buffer, client->device->card, | |
947 | page_count, direction); | |
948 | if (retval < 0) | |
949 | return retval; | |
19a15b93 | 950 | |
9aad8125 KH |
951 | retval = fw_iso_buffer_map(&client->buffer, vma); |
952 | if (retval < 0) | |
953 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
954 | ||
955 | return retval; | |
19a15b93 KH |
956 | } |
957 | ||
958 | static int fw_device_op_release(struct inode *inode, struct file *file) | |
959 | { | |
960 | struct client *client = file->private_data; | |
2603bf21 | 961 | struct event *e, *next_e; |
3964a449 | 962 | struct client_resource *r, *next_r; |
97bd9efa | 963 | unsigned long flags; |
19a15b93 | 964 | |
9aad8125 KH |
965 | if (client->buffer.pages) |
966 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
967 | ||
19a15b93 KH |
968 | if (client->iso_context) |
969 | fw_iso_context_destroy(client->iso_context); | |
970 | ||
3964a449 KH |
971 | list_for_each_entry_safe(r, next_r, &client->resource_list, link) |
972 | r->release(client, r); | |
66dea3e5 | 973 | |
c781c06d KH |
974 | /* |
975 | * FIXME: We should wait for the async tasklets to stop | |
976 | * running before freeing the memory. | |
977 | */ | |
28cf6a04 | 978 | |
2603bf21 KH |
979 | list_for_each_entry_safe(e, next_e, &client->event_list, link) |
980 | kfree(e); | |
19a15b93 | 981 | |
97bd9efa KH |
982 | spin_lock_irqsave(&client->device->card->lock, flags); |
983 | list_del(&client->link); | |
984 | spin_unlock_irqrestore(&client->device->card->lock, flags); | |
985 | ||
19a15b93 KH |
986 | fw_device_put(client->device); |
987 | kfree(client); | |
988 | ||
989 | return 0; | |
990 | } | |
991 | ||
992 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) | |
993 | { | |
994 | struct client *client = file->private_data; | |
2603bf21 | 995 | unsigned int mask = 0; |
19a15b93 KH |
996 | |
997 | poll_wait(file, &client->wait, pt); | |
998 | ||
2603bf21 KH |
999 | if (fw_device_is_shutdown(client->device)) |
1000 | mask |= POLLHUP | POLLERR; | |
19a15b93 | 1001 | if (!list_empty(&client->event_list)) |
2603bf21 KH |
1002 | mask |= POLLIN | POLLRDNORM; |
1003 | ||
1004 | return mask; | |
19a15b93 KH |
1005 | } |
1006 | ||
21ebcd12 | 1007 | const struct file_operations fw_device_ops = { |
19a15b93 KH |
1008 | .owner = THIS_MODULE, |
1009 | .open = fw_device_op_open, | |
1010 | .read = fw_device_op_read, | |
1011 | .unlocked_ioctl = fw_device_op_ioctl, | |
1012 | .poll = fw_device_op_poll, | |
1013 | .release = fw_device_op_release, | |
1014 | .mmap = fw_device_op_mmap, | |
1015 | ||
1016 | #ifdef CONFIG_COMPAT | |
5af4e5ea | 1017 | .compat_ioctl = fw_device_op_compat_ioctl, |
19a15b93 KH |
1018 | #endif |
1019 | }; |