]>
Commit | Line | Data |
---|---|---|
19a15b93 KH |
1 | /* -*- c-basic-offset: 8 -*- |
2 | * | |
3 | * fw-device-cdev.c - Char device for device raw access | |
4 | * | |
5 | * Copyright (C) 2005-2006 Kristian Hoegsberg <krh@bitplanet.net> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software Foundation, | |
19 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/wait.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/device.h> | |
27 | #include <linux/vmalloc.h> | |
28 | #include <linux/poll.h> | |
29 | #include <linux/delay.h> | |
30 | #include <linux/mm.h> | |
a3aca3da | 31 | #include <linux/idr.h> |
19a15b93 KH |
32 | #include <linux/compat.h> |
33 | #include <asm/uaccess.h> | |
34 | #include "fw-transaction.h" | |
35 | #include "fw-topology.h" | |
36 | #include "fw-device.h" | |
37 | #include "fw-device-cdev.h" | |
38 | ||
39 | /* | |
40 | * todo | |
41 | * | |
42 | * - bus resets sends a new packet with new generation and node id | |
43 | * | |
44 | */ | |
45 | ||
46 | /* dequeue_event() just kfree()'s the event, so the event has to be | |
47 | * the first field in the struct. */ | |
48 | ||
49 | struct event { | |
50 | struct { void *data; size_t size; } v[2]; | |
51 | struct list_head link; | |
52 | }; | |
53 | ||
97bd9efa KH |
54 | struct bus_reset { |
55 | struct event event; | |
56 | struct fw_cdev_event_bus_reset reset; | |
57 | }; | |
58 | ||
19a15b93 KH |
59 | struct response { |
60 | struct event event; | |
61 | struct fw_transaction transaction; | |
62 | struct client *client; | |
28cf6a04 | 63 | struct list_head link; |
19a15b93 KH |
64 | struct fw_cdev_event_response response; |
65 | }; | |
66 | ||
67 | struct iso_interrupt { | |
68 | struct event event; | |
69 | struct fw_cdev_event_iso_interrupt interrupt; | |
70 | }; | |
71 | ||
72 | struct client { | |
344bbc4d | 73 | u32 version; |
19a15b93 KH |
74 | struct fw_device *device; |
75 | spinlock_t lock; | |
66dea3e5 | 76 | u32 resource_handle; |
19a15b93 KH |
77 | struct list_head handler_list; |
78 | struct list_head request_list; | |
28cf6a04 | 79 | struct list_head transaction_list; |
66dea3e5 | 80 | struct list_head descriptor_list; |
19a15b93 KH |
81 | u32 request_serial; |
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; |
9aad8125 KH |
87 | struct fw_iso_buffer buffer; |
88 | unsigned long vm_start; | |
97bd9efa KH |
89 | |
90 | struct list_head link; | |
19a15b93 KH |
91 | }; |
92 | ||
93 | static inline void __user * | |
94 | u64_to_uptr(__u64 value) | |
95 | { | |
96 | return (void __user *)(unsigned long)value; | |
97 | } | |
98 | ||
99 | static inline __u64 | |
100 | uptr_to_u64(void __user *ptr) | |
101 | { | |
102 | return (__u64)(unsigned long)ptr; | |
103 | } | |
104 | ||
105 | static int fw_device_op_open(struct inode *inode, struct file *file) | |
106 | { | |
107 | struct fw_device *device; | |
108 | struct client *client; | |
97bd9efa | 109 | unsigned long flags; |
19a15b93 | 110 | |
a3aca3da KH |
111 | device = fw_device_from_devt(inode->i_rdev); |
112 | if (device == NULL) | |
113 | return -ENODEV; | |
19a15b93 KH |
114 | |
115 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
116 | if (client == NULL) | |
117 | return -ENOMEM; | |
118 | ||
119 | client->device = fw_device_get(device); | |
120 | INIT_LIST_HEAD(&client->event_list); | |
19a15b93 KH |
121 | INIT_LIST_HEAD(&client->handler_list); |
122 | INIT_LIST_HEAD(&client->request_list); | |
28cf6a04 | 123 | INIT_LIST_HEAD(&client->transaction_list); |
66dea3e5 | 124 | INIT_LIST_HEAD(&client->descriptor_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); | |
148 | ||
149 | list_add_tail(&event->link, &client->event_list); | |
19a15b93 KH |
150 | wake_up_interruptible(&client->wait); |
151 | ||
152 | spin_unlock_irqrestore(&client->lock, flags); | |
153 | } | |
154 | ||
2603bf21 KH |
155 | static int |
156 | dequeue_event(struct client *client, char __user *buffer, size_t count) | |
19a15b93 KH |
157 | { |
158 | unsigned long flags; | |
159 | struct event *event; | |
160 | size_t size, total; | |
2603bf21 | 161 | int i, retval; |
19a15b93 | 162 | |
2603bf21 KH |
163 | retval = wait_event_interruptible(client->wait, |
164 | !list_empty(&client->event_list) || | |
165 | fw_device_is_shutdown(client->device)); | |
166 | if (retval < 0) | |
167 | return retval; | |
19a15b93 | 168 | |
2603bf21 KH |
169 | if (list_empty(&client->event_list) && |
170 | fw_device_is_shutdown(client->device)) | |
171 | return -ENODEV; | |
19a15b93 | 172 | |
2603bf21 | 173 | spin_lock_irqsave(&client->lock, flags); |
19a15b93 KH |
174 | event = container_of(client->event_list.next, struct event, link); |
175 | list_del(&event->link); | |
19a15b93 KH |
176 | spin_unlock_irqrestore(&client->lock, flags); |
177 | ||
19a15b93 KH |
178 | total = 0; |
179 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { | |
180 | size = min(event->v[i].size, count - total); | |
2603bf21 KH |
181 | if (copy_to_user(buffer + total, event->v[i].data, size)) { |
182 | retval = -EFAULT; | |
19a15b93 | 183 | goto out; |
2603bf21 | 184 | } |
19a15b93 KH |
185 | total += size; |
186 | } | |
187 | retval = total; | |
188 | ||
189 | out: | |
190 | kfree(event); | |
191 | ||
192 | return retval; | |
193 | } | |
194 | ||
195 | static ssize_t | |
196 | fw_device_op_read(struct file *file, | |
197 | char __user *buffer, size_t count, loff_t *offset) | |
198 | { | |
199 | struct client *client = file->private_data; | |
200 | ||
201 | return dequeue_event(client, buffer, count); | |
202 | } | |
203 | ||
344bbc4d KH |
204 | static void |
205 | fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, | |
da8ecffa | 206 | struct client *client) |
344bbc4d | 207 | { |
da8ecffa | 208 | struct fw_card *card = client->device->card; |
344bbc4d | 209 | |
da8ecffa | 210 | event->closure = client->bus_reset_closure; |
344bbc4d | 211 | event->type = FW_CDEV_EVENT_BUS_RESET; |
da8ecffa | 212 | event->node_id = client->device->node_id; |
344bbc4d KH |
213 | event->local_node_id = card->local_node->node_id; |
214 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ | |
215 | event->irm_node_id = card->irm_node->node_id; | |
216 | event->root_node_id = card->root_node->node_id; | |
217 | event->generation = card->generation; | |
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 KH |
240 | |
241 | bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC); | |
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, | |
250 | &bus_reset->reset, sizeof bus_reset->reset, NULL, 0); | |
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 | ||
344bbc4d | 268 | static int ioctl_get_info(struct client *client, void __user *arg) |
19a15b93 | 269 | { |
344bbc4d KH |
270 | struct fw_cdev_get_info get_info; |
271 | struct fw_cdev_event_bus_reset bus_reset; | |
272 | ||
273 | if (copy_from_user(&get_info, arg, sizeof get_info)) | |
274 | return -EFAULT; | |
275 | ||
276 | client->version = get_info.version; | |
277 | get_info.version = FW_CDEV_VERSION; | |
278 | ||
279 | if (get_info.rom != 0) { | |
280 | void __user *uptr = u64_to_uptr(get_info.rom); | |
d84702a5 SR |
281 | size_t want = get_info.rom_length; |
282 | size_t have = client->device->config_rom_length * 4; | |
344bbc4d | 283 | |
d84702a5 SR |
284 | if (copy_to_user(uptr, client->device->config_rom, |
285 | min(want, have))) | |
344bbc4d KH |
286 | return -EFAULT; |
287 | } | |
288 | get_info.rom_length = client->device->config_rom_length * 4; | |
289 | ||
da8ecffa | 290 | client->bus_reset_closure = get_info.bus_reset_closure; |
344bbc4d KH |
291 | if (get_info.bus_reset != 0) { |
292 | void __user *uptr = u64_to_uptr(get_info.bus_reset); | |
293 | ||
da8ecffa | 294 | fill_bus_reset_event(&bus_reset, client); |
344bbc4d KH |
295 | if (copy_to_user(uptr, &bus_reset, sizeof bus_reset)) |
296 | return -EFAULT; | |
297 | } | |
19a15b93 | 298 | |
e7533505 KH |
299 | get_info.card = client->device->card->index; |
300 | ||
344bbc4d | 301 | if (copy_to_user(arg, &get_info, sizeof get_info)) |
19a15b93 KH |
302 | return -EFAULT; |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | static void | |
308 | complete_transaction(struct fw_card *card, int rcode, | |
309 | void *payload, size_t length, void *data) | |
310 | { | |
311 | struct response *response = data; | |
312 | struct client *client = response->client; | |
28cf6a04 | 313 | unsigned long flags; |
19a15b93 KH |
314 | |
315 | if (length < response->response.length) | |
316 | response->response.length = length; | |
317 | if (rcode == RCODE_COMPLETE) | |
318 | memcpy(response->response.data, payload, | |
319 | response->response.length); | |
320 | ||
28cf6a04 KH |
321 | spin_lock_irqsave(&client->lock, flags); |
322 | list_del(&response->link); | |
323 | spin_unlock_irqrestore(&client->lock, flags); | |
324 | ||
19a15b93 KH |
325 | response->response.type = FW_CDEV_EVENT_RESPONSE; |
326 | response->response.rcode = rcode; | |
327 | queue_event(client, &response->event, | |
328 | &response->response, sizeof response->response, | |
329 | response->response.data, response->response.length); | |
330 | } | |
331 | ||
332 | static ssize_t ioctl_send_request(struct client *client, void __user *arg) | |
333 | { | |
334 | struct fw_device *device = client->device; | |
335 | struct fw_cdev_send_request request; | |
336 | struct response *response; | |
28cf6a04 | 337 | unsigned long flags; |
19a15b93 KH |
338 | |
339 | if (copy_from_user(&request, arg, sizeof request)) | |
340 | return -EFAULT; | |
341 | ||
342 | /* What is the biggest size we'll accept, really? */ | |
343 | if (request.length > 4096) | |
344 | return -EINVAL; | |
345 | ||
346 | response = kmalloc(sizeof *response + request.length, GFP_KERNEL); | |
347 | if (response == NULL) | |
348 | return -ENOMEM; | |
349 | ||
350 | response->client = client; | |
351 | response->response.length = request.length; | |
352 | response->response.closure = request.closure; | |
353 | ||
354 | if (request.data && | |
355 | copy_from_user(response->response.data, | |
356 | u64_to_uptr(request.data), request.length)) { | |
357 | kfree(response); | |
358 | return -EFAULT; | |
359 | } | |
360 | ||
28cf6a04 KH |
361 | spin_lock_irqsave(&client->lock, flags); |
362 | list_add_tail(&response->link, &client->transaction_list); | |
363 | spin_unlock_irqrestore(&client->lock, flags); | |
364 | ||
19a15b93 | 365 | fw_send_request(device->card, &response->transaction, |
344bbc4d | 366 | request.tcode & 0x1f, |
907293d7 | 367 | device->node->node_id, |
97e35275 | 368 | request.generation, |
19a15b93 KH |
369 | device->node->max_speed, |
370 | request.offset, | |
371 | response->response.data, request.length, | |
372 | complete_transaction, response); | |
373 | ||
374 | if (request.data) | |
375 | return sizeof request + request.length; | |
376 | else | |
377 | return sizeof request; | |
378 | } | |
379 | ||
380 | struct address_handler { | |
381 | struct fw_address_handler handler; | |
382 | __u64 closure; | |
383 | struct client *client; | |
384 | struct list_head link; | |
385 | }; | |
386 | ||
387 | struct request { | |
388 | struct fw_request *request; | |
389 | void *data; | |
390 | size_t length; | |
391 | u32 serial; | |
392 | struct list_head link; | |
393 | }; | |
394 | ||
395 | struct request_event { | |
396 | struct event event; | |
397 | struct fw_cdev_event_request request; | |
398 | }; | |
399 | ||
400 | static void | |
401 | handle_request(struct fw_card *card, struct fw_request *r, | |
402 | int tcode, int destination, int source, | |
403 | int generation, int speed, | |
404 | unsigned long long offset, | |
405 | void *payload, size_t length, void *callback_data) | |
406 | { | |
407 | struct address_handler *handler = callback_data; | |
408 | struct request *request; | |
409 | struct request_event *e; | |
410 | unsigned long flags; | |
411 | struct client *client = handler->client; | |
412 | ||
413 | request = kmalloc(sizeof *request, GFP_ATOMIC); | |
414 | e = kmalloc(sizeof *e, GFP_ATOMIC); | |
415 | if (request == NULL || e == NULL) { | |
416 | kfree(request); | |
417 | kfree(e); | |
418 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); | |
419 | return; | |
420 | } | |
421 | ||
422 | request->request = r; | |
423 | request->data = payload; | |
424 | request->length = length; | |
425 | ||
426 | spin_lock_irqsave(&client->lock, flags); | |
427 | request->serial = client->request_serial++; | |
428 | list_add_tail(&request->link, &client->request_list); | |
429 | spin_unlock_irqrestore(&client->lock, flags); | |
430 | ||
431 | e->request.type = FW_CDEV_EVENT_REQUEST; | |
432 | e->request.tcode = tcode; | |
433 | e->request.offset = offset; | |
434 | e->request.length = length; | |
435 | e->request.serial = request->serial; | |
436 | e->request.closure = handler->closure; | |
437 | ||
438 | queue_event(client, &e->event, | |
439 | &e->request, sizeof e->request, payload, length); | |
440 | } | |
441 | ||
442 | static int ioctl_allocate(struct client *client, void __user *arg) | |
443 | { | |
444 | struct fw_cdev_allocate request; | |
445 | struct address_handler *handler; | |
446 | unsigned long flags; | |
447 | struct fw_address_region region; | |
448 | ||
449 | if (copy_from_user(&request, arg, sizeof request)) | |
450 | return -EFAULT; | |
451 | ||
452 | handler = kmalloc(sizeof *handler, GFP_KERNEL); | |
453 | if (handler == NULL) | |
454 | return -ENOMEM; | |
455 | ||
456 | region.start = request.offset; | |
457 | region.end = request.offset + request.length; | |
458 | handler->handler.length = request.length; | |
459 | handler->handler.address_callback = handle_request; | |
460 | handler->handler.callback_data = handler; | |
461 | handler->closure = request.closure; | |
462 | handler->client = client; | |
463 | ||
464 | if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) { | |
465 | kfree(handler); | |
466 | return -EBUSY; | |
467 | } | |
468 | ||
469 | spin_lock_irqsave(&client->lock, flags); | |
470 | list_add_tail(&handler->link, &client->handler_list); | |
471 | spin_unlock_irqrestore(&client->lock, flags); | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
9472316b KH |
476 | static int ioctl_deallocate(struct client *client, void __user *arg) |
477 | { | |
478 | struct fw_cdev_deallocate request; | |
479 | struct address_handler *handler; | |
480 | unsigned long flags; | |
481 | ||
482 | if (copy_from_user(&request, arg, sizeof request)) | |
483 | return -EFAULT; | |
484 | ||
485 | spin_lock_irqsave(&client->lock, flags); | |
486 | list_for_each_entry(handler, &client->handler_list, link) { | |
487 | if (handler->handler.offset == request.offset) { | |
488 | list_del(&handler->link); | |
489 | break; | |
490 | } | |
491 | } | |
492 | spin_unlock_irqrestore(&client->lock, flags); | |
493 | ||
494 | if (&handler->link == &client->handler_list) | |
495 | return -EINVAL; | |
496 | ||
497 | fw_core_remove_address_handler(&handler->handler); | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
19a15b93 KH |
502 | static int ioctl_send_response(struct client *client, void __user *arg) |
503 | { | |
504 | struct fw_cdev_send_response request; | |
505 | struct request *r; | |
506 | unsigned long flags; | |
507 | ||
508 | if (copy_from_user(&request, arg, sizeof request)) | |
509 | return -EFAULT; | |
510 | ||
511 | spin_lock_irqsave(&client->lock, flags); | |
512 | list_for_each_entry(r, &client->request_list, link) { | |
513 | if (r->serial == request.serial) { | |
514 | list_del(&r->link); | |
515 | break; | |
516 | } | |
517 | } | |
518 | spin_unlock_irqrestore(&client->lock, flags); | |
519 | ||
520 | if (&r->link == &client->request_list) | |
521 | return -EINVAL; | |
522 | ||
523 | if (request.length < r->length) | |
524 | r->length = request.length; | |
525 | if (copy_from_user(r->data, u64_to_uptr(request.data), r->length)) | |
526 | return -EFAULT; | |
527 | ||
528 | fw_send_response(client->device->card, r->request, request.rcode); | |
529 | ||
530 | kfree(r); | |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
5371842b KH |
535 | static int ioctl_initiate_bus_reset(struct client *client, void __user *arg) |
536 | { | |
537 | struct fw_cdev_initiate_bus_reset request; | |
538 | int short_reset; | |
539 | ||
540 | if (copy_from_user(&request, arg, sizeof request)) | |
541 | return -EFAULT; | |
542 | ||
543 | short_reset = (request.type == FW_CDEV_SHORT_RESET); | |
544 | ||
545 | return fw_core_initiate_bus_reset(client->device->card, short_reset); | |
546 | } | |
547 | ||
66dea3e5 KH |
548 | struct descriptor { |
549 | struct fw_descriptor d; | |
550 | struct list_head link; | |
551 | u32 handle; | |
552 | u32 data[0]; | |
553 | }; | |
554 | ||
555 | static int ioctl_add_descriptor(struct client *client, void __user *arg) | |
556 | { | |
557 | struct fw_cdev_add_descriptor request; | |
558 | struct descriptor *descriptor; | |
559 | unsigned long flags; | |
560 | int retval; | |
561 | ||
562 | if (copy_from_user(&request, arg, sizeof request)) | |
563 | return -EFAULT; | |
564 | ||
565 | if (request.length > 256) | |
566 | return -EINVAL; | |
567 | ||
568 | descriptor = | |
569 | kmalloc(sizeof *descriptor + request.length * 4, GFP_KERNEL); | |
570 | if (descriptor == NULL) | |
571 | return -ENOMEM; | |
572 | ||
573 | if (copy_from_user(descriptor->data, | |
574 | u64_to_uptr(request.data), request.length * 4)) { | |
575 | kfree(descriptor); | |
576 | return -EFAULT; | |
577 | } | |
578 | ||
579 | descriptor->d.length = request.length; | |
580 | descriptor->d.immediate = request.immediate; | |
581 | descriptor->d.key = request.key; | |
582 | descriptor->d.data = descriptor->data; | |
583 | ||
584 | retval = fw_core_add_descriptor(&descriptor->d); | |
585 | if (retval < 0) { | |
586 | kfree(descriptor); | |
587 | return retval; | |
588 | } | |
589 | ||
590 | spin_lock_irqsave(&client->lock, flags); | |
591 | list_add_tail(&descriptor->link, &client->descriptor_list); | |
592 | descriptor->handle = client->resource_handle++; | |
593 | spin_unlock_irqrestore(&client->lock, flags); | |
594 | ||
595 | request.handle = descriptor->handle; | |
596 | if (copy_to_user(arg, &request, sizeof request)) | |
597 | return -EFAULT; | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | static int ioctl_remove_descriptor(struct client *client, void __user *arg) | |
603 | { | |
604 | struct fw_cdev_remove_descriptor request; | |
605 | struct descriptor *d; | |
606 | unsigned long flags; | |
607 | ||
608 | if (copy_from_user(&request, arg, sizeof request)) | |
609 | return -EFAULT; | |
610 | ||
611 | spin_lock_irqsave(&client->lock, flags); | |
612 | list_for_each_entry(d, &client->descriptor_list, link) { | |
613 | if (d->handle == request.handle) { | |
614 | list_del(&d->link); | |
615 | break; | |
616 | } | |
617 | } | |
618 | spin_unlock_irqrestore(&client->lock, flags); | |
619 | ||
620 | if (&d->link == &client->descriptor_list) | |
621 | return -EINVAL; | |
622 | ||
623 | fw_core_remove_descriptor(&d->d); | |
624 | kfree(d); | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
19a15b93 | 629 | static void |
9b32d5f3 KH |
630 | iso_callback(struct fw_iso_context *context, u32 cycle, |
631 | size_t header_length, void *header, void *data) | |
19a15b93 KH |
632 | { |
633 | struct client *client = data; | |
634 | struct iso_interrupt *interrupt; | |
635 | ||
9b32d5f3 | 636 | interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC); |
19a15b93 KH |
637 | if (interrupt == NULL) |
638 | return; | |
639 | ||
640 | interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; | |
641 | interrupt->interrupt.closure = 0; | |
642 | interrupt->interrupt.cycle = cycle; | |
9b32d5f3 KH |
643 | interrupt->interrupt.header_length = header_length; |
644 | memcpy(interrupt->interrupt.header, header, header_length); | |
19a15b93 | 645 | queue_event(client, &interrupt->event, |
9b32d5f3 KH |
646 | &interrupt->interrupt, |
647 | sizeof interrupt->interrupt + header_length, NULL, 0); | |
19a15b93 KH |
648 | } |
649 | ||
650 | static int ioctl_create_iso_context(struct client *client, void __user *arg) | |
651 | { | |
652 | struct fw_cdev_create_iso_context request; | |
653 | ||
654 | if (copy_from_user(&request, arg, sizeof request)) | |
655 | return -EFAULT; | |
656 | ||
21efb3cf KH |
657 | if (request.channel > 63) |
658 | return -EINVAL; | |
659 | ||
c70dc788 KH |
660 | switch (request.type) { |
661 | case FW_ISO_CONTEXT_RECEIVE: | |
c70dc788 KH |
662 | if (request.header_size < 4 || (request.header_size & 3)) |
663 | return -EINVAL; | |
98b6cbe8 | 664 | |
c70dc788 KH |
665 | break; |
666 | ||
667 | case FW_ISO_CONTEXT_TRANSMIT: | |
668 | if (request.speed > SCODE_3200) | |
669 | return -EINVAL; | |
670 | ||
671 | break; | |
672 | ||
673 | default: | |
21efb3cf | 674 | return -EINVAL; |
c70dc788 KH |
675 | } |
676 | ||
19a15b93 | 677 | client->iso_context = fw_iso_context_create(client->device->card, |
295e3feb | 678 | request.type, |
21efb3cf KH |
679 | request.channel, |
680 | request.speed, | |
8fbdbb36 | 681 | request.header_size, |
19a15b93 KH |
682 | iso_callback, client); |
683 | if (IS_ERR(client->iso_context)) | |
684 | return PTR_ERR(client->iso_context); | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
689 | static int ioctl_queue_iso(struct client *client, void __user *arg) | |
690 | { | |
691 | struct fw_cdev_queue_iso request; | |
692 | struct fw_cdev_iso_packet __user *p, *end, *next; | |
9b32d5f3 | 693 | struct fw_iso_context *ctx = client->iso_context; |
295e3feb | 694 | unsigned long payload, payload_end, header_length; |
19a15b93 KH |
695 | int count; |
696 | struct { | |
697 | struct fw_iso_packet packet; | |
698 | u8 header[256]; | |
699 | } u; | |
700 | ||
9b32d5f3 | 701 | if (ctx == NULL) |
19a15b93 KH |
702 | return -EINVAL; |
703 | if (copy_from_user(&request, arg, sizeof request)) | |
704 | return -EFAULT; | |
705 | ||
706 | /* If the user passes a non-NULL data pointer, has mmap()'ed | |
707 | * the iso buffer, and the pointer points inside the buffer, | |
708 | * we setup the payload pointers accordingly. Otherwise we | |
9aad8125 | 709 | * set them both to 0, which will still let packets with |
19a15b93 KH |
710 | * payload_length == 0 through. In other words, if no packets |
711 | * use the indirect payload, the iso buffer need not be mapped | |
712 | * and the request.data pointer is ignored.*/ | |
713 | ||
9aad8125 KH |
714 | payload = (unsigned long)request.data - client->vm_start; |
715 | payload_end = payload + (client->buffer.page_count << PAGE_SHIFT); | |
716 | if (request.data == 0 || client->buffer.pages == NULL || | |
717 | payload >= payload_end) { | |
718 | payload = 0; | |
719 | payload_end = 0; | |
19a15b93 KH |
720 | } |
721 | ||
722 | if (!access_ok(VERIFY_READ, request.packets, request.size)) | |
723 | return -EFAULT; | |
724 | ||
725 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets); | |
726 | end = (void __user *)p + request.size; | |
727 | count = 0; | |
728 | while (p < end) { | |
729 | if (__copy_from_user(&u.packet, p, sizeof *p)) | |
730 | return -EFAULT; | |
295e3feb | 731 | |
9b32d5f3 | 732 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
295e3feb KH |
733 | header_length = u.packet.header_length; |
734 | } else { | |
735 | /* We require that header_length is a multiple of | |
736 | * the fixed header size, ctx->header_size */ | |
9b32d5f3 KH |
737 | if (ctx->header_size == 0) { |
738 | if (u.packet.header_length > 0) | |
739 | return -EINVAL; | |
740 | } else if (u.packet.header_length % ctx->header_size != 0) { | |
295e3feb | 741 | return -EINVAL; |
9b32d5f3 | 742 | } |
295e3feb KH |
743 | header_length = 0; |
744 | } | |
745 | ||
19a15b93 | 746 | next = (struct fw_cdev_iso_packet __user *) |
295e3feb | 747 | &p->header[header_length / 4]; |
19a15b93 KH |
748 | if (next > end) |
749 | return -EINVAL; | |
750 | if (__copy_from_user | |
295e3feb | 751 | (u.packet.header, p->header, header_length)) |
19a15b93 | 752 | return -EFAULT; |
98b6cbe8 | 753 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
19a15b93 KH |
754 | u.packet.header_length + u.packet.payload_length > 0) |
755 | return -EINVAL; | |
756 | if (payload + u.packet.payload_length > payload_end) | |
757 | return -EINVAL; | |
758 | ||
9b32d5f3 KH |
759 | if (fw_iso_context_queue(ctx, &u.packet, |
760 | &client->buffer, payload)) | |
19a15b93 KH |
761 | break; |
762 | ||
763 | p = next; | |
764 | payload += u.packet.payload_length; | |
765 | count++; | |
766 | } | |
767 | ||
768 | request.size -= uptr_to_u64(p) - request.packets; | |
769 | request.packets = uptr_to_u64(p); | |
9aad8125 | 770 | request.data = client->vm_start + payload; |
19a15b93 KH |
771 | |
772 | if (copy_to_user(arg, &request, sizeof request)) | |
773 | return -EFAULT; | |
774 | ||
775 | return count; | |
776 | } | |
777 | ||
69cdb726 | 778 | static int ioctl_start_iso(struct client *client, void __user *arg) |
19a15b93 | 779 | { |
69cdb726 | 780 | struct fw_cdev_start_iso request; |
19a15b93 KH |
781 | |
782 | if (copy_from_user(&request, arg, sizeof request)) | |
783 | return -EFAULT; | |
784 | ||
eb0306ea KH |
785 | if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) { |
786 | if (request.tags == 0 || request.tags > 15) | |
787 | return -EINVAL; | |
788 | ||
789 | if (request.sync > 15) | |
790 | return -EINVAL; | |
791 | } | |
792 | ||
793 | return fw_iso_context_start(client->iso_context, | |
794 | request.cycle, request.sync, request.tags); | |
19a15b93 KH |
795 | } |
796 | ||
b8295668 KH |
797 | static int ioctl_stop_iso(struct client *client, void __user *arg) |
798 | { | |
799 | return fw_iso_context_stop(client->iso_context); | |
800 | } | |
801 | ||
19a15b93 KH |
802 | static int |
803 | dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg) | |
804 | { | |
805 | switch (cmd) { | |
344bbc4d KH |
806 | case FW_CDEV_IOC_GET_INFO: |
807 | return ioctl_get_info(client, arg); | |
19a15b93 KH |
808 | case FW_CDEV_IOC_SEND_REQUEST: |
809 | return ioctl_send_request(client, arg); | |
810 | case FW_CDEV_IOC_ALLOCATE: | |
811 | return ioctl_allocate(client, arg); | |
9472316b KH |
812 | case FW_CDEV_IOC_DEALLOCATE: |
813 | return ioctl_deallocate(client, arg); | |
19a15b93 KH |
814 | case FW_CDEV_IOC_SEND_RESPONSE: |
815 | return ioctl_send_response(client, arg); | |
5371842b KH |
816 | case FW_CDEV_IOC_INITIATE_BUS_RESET: |
817 | return ioctl_initiate_bus_reset(client, arg); | |
66dea3e5 KH |
818 | case FW_CDEV_IOC_ADD_DESCRIPTOR: |
819 | return ioctl_add_descriptor(client, arg); | |
820 | case FW_CDEV_IOC_REMOVE_DESCRIPTOR: | |
821 | return ioctl_remove_descriptor(client, arg); | |
19a15b93 KH |
822 | case FW_CDEV_IOC_CREATE_ISO_CONTEXT: |
823 | return ioctl_create_iso_context(client, arg); | |
824 | case FW_CDEV_IOC_QUEUE_ISO: | |
825 | return ioctl_queue_iso(client, arg); | |
69cdb726 KH |
826 | case FW_CDEV_IOC_START_ISO: |
827 | return ioctl_start_iso(client, arg); | |
b8295668 KH |
828 | case FW_CDEV_IOC_STOP_ISO: |
829 | return ioctl_stop_iso(client, arg); | |
19a15b93 KH |
830 | default: |
831 | return -EINVAL; | |
832 | } | |
833 | } | |
834 | ||
835 | static long | |
836 | fw_device_op_ioctl(struct file *file, | |
837 | unsigned int cmd, unsigned long arg) | |
838 | { | |
839 | struct client *client = file->private_data; | |
840 | ||
841 | return dispatch_ioctl(client, cmd, (void __user *) arg); | |
842 | } | |
843 | ||
844 | #ifdef CONFIG_COMPAT | |
845 | static long | |
846 | fw_device_op_compat_ioctl(struct file *file, | |
847 | unsigned int cmd, unsigned long arg) | |
848 | { | |
849 | struct client *client = file->private_data; | |
850 | ||
851 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); | |
852 | } | |
853 | #endif | |
854 | ||
855 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) | |
856 | { | |
857 | struct client *client = file->private_data; | |
9aad8125 KH |
858 | enum dma_data_direction direction; |
859 | unsigned long size; | |
860 | int page_count, retval; | |
861 | ||
862 | /* FIXME: We could support multiple buffers, but we don't. */ | |
863 | if (client->buffer.pages != NULL) | |
864 | return -EBUSY; | |
865 | ||
866 | if (!(vma->vm_flags & VM_SHARED)) | |
867 | return -EINVAL; | |
19a15b93 | 868 | |
9aad8125 | 869 | if (vma->vm_start & ~PAGE_MASK) |
19a15b93 KH |
870 | return -EINVAL; |
871 | ||
872 | client->vm_start = vma->vm_start; | |
9aad8125 KH |
873 | size = vma->vm_end - vma->vm_start; |
874 | page_count = size >> PAGE_SHIFT; | |
875 | if (size & ~PAGE_MASK) | |
876 | return -EINVAL; | |
877 | ||
878 | if (vma->vm_flags & VM_WRITE) | |
879 | direction = DMA_TO_DEVICE; | |
880 | else | |
881 | direction = DMA_FROM_DEVICE; | |
882 | ||
883 | retval = fw_iso_buffer_init(&client->buffer, client->device->card, | |
884 | page_count, direction); | |
885 | if (retval < 0) | |
886 | return retval; | |
19a15b93 | 887 | |
9aad8125 KH |
888 | retval = fw_iso_buffer_map(&client->buffer, vma); |
889 | if (retval < 0) | |
890 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
891 | ||
892 | return retval; | |
19a15b93 KH |
893 | } |
894 | ||
895 | static int fw_device_op_release(struct inode *inode, struct file *file) | |
896 | { | |
897 | struct client *client = file->private_data; | |
2603bf21 | 898 | struct address_handler *h, *next_h; |
19a15b93 | 899 | struct request *r, *next_r; |
2603bf21 | 900 | struct event *e, *next_e; |
28cf6a04 | 901 | struct response *t, *next_t; |
66dea3e5 | 902 | struct descriptor *d, *next_d; |
97bd9efa | 903 | unsigned long flags; |
19a15b93 | 904 | |
9aad8125 KH |
905 | if (client->buffer.pages) |
906 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
907 | ||
19a15b93 KH |
908 | if (client->iso_context) |
909 | fw_iso_context_destroy(client->iso_context); | |
910 | ||
2603bf21 | 911 | list_for_each_entry_safe(h, next_h, &client->handler_list, link) { |
19a15b93 KH |
912 | fw_core_remove_address_handler(&h->handler); |
913 | kfree(h); | |
914 | } | |
915 | ||
916 | list_for_each_entry_safe(r, next_r, &client->request_list, link) { | |
917 | fw_send_response(client->device->card, r->request, | |
918 | RCODE_CONFLICT_ERROR); | |
919 | kfree(r); | |
920 | } | |
921 | ||
7e35f7f3 | 922 | list_for_each_entry_safe(t, next_t, &client->transaction_list, link) { |
28cf6a04 | 923 | fw_cancel_transaction(client->device->card, &t->transaction); |
7e35f7f3 KH |
924 | kfree(t); |
925 | } | |
28cf6a04 | 926 | |
66dea3e5 KH |
927 | list_for_each_entry_safe(d, next_d, &client->descriptor_list, link) { |
928 | fw_core_remove_descriptor(&d->d); | |
929 | kfree(d); | |
930 | } | |
931 | ||
28cf6a04 KH |
932 | /* FIXME: We should wait for the async tasklets to stop |
933 | * running before freeing the memory. */ | |
934 | ||
2603bf21 KH |
935 | list_for_each_entry_safe(e, next_e, &client->event_list, link) |
936 | kfree(e); | |
19a15b93 | 937 | |
97bd9efa KH |
938 | spin_lock_irqsave(&client->device->card->lock, flags); |
939 | list_del(&client->link); | |
940 | spin_unlock_irqrestore(&client->device->card->lock, flags); | |
941 | ||
19a15b93 KH |
942 | fw_device_put(client->device); |
943 | kfree(client); | |
944 | ||
945 | return 0; | |
946 | } | |
947 | ||
948 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) | |
949 | { | |
950 | struct client *client = file->private_data; | |
2603bf21 | 951 | unsigned int mask = 0; |
19a15b93 KH |
952 | |
953 | poll_wait(file, &client->wait, pt); | |
954 | ||
2603bf21 KH |
955 | if (fw_device_is_shutdown(client->device)) |
956 | mask |= POLLHUP | POLLERR; | |
19a15b93 | 957 | if (!list_empty(&client->event_list)) |
2603bf21 KH |
958 | mask |= POLLIN | POLLRDNORM; |
959 | ||
960 | return mask; | |
19a15b93 KH |
961 | } |
962 | ||
21ebcd12 | 963 | const struct file_operations fw_device_ops = { |
19a15b93 KH |
964 | .owner = THIS_MODULE, |
965 | .open = fw_device_op_open, | |
966 | .read = fw_device_op_read, | |
967 | .unlocked_ioctl = fw_device_op_ioctl, | |
968 | .poll = fw_device_op_poll, | |
969 | .release = fw_device_op_release, | |
970 | .mmap = fw_device_op_mmap, | |
971 | ||
972 | #ifdef CONFIG_COMPAT | |
5af4e5ea | 973 | .compat_ioctl = fw_device_op_compat_ioctl, |
19a15b93 KH |
974 | #endif |
975 | }; |