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> | |
31 | #include <linux/compat.h> | |
32 | #include <asm/uaccess.h> | |
33 | #include "fw-transaction.h" | |
34 | #include "fw-topology.h" | |
35 | #include "fw-device.h" | |
36 | #include "fw-device-cdev.h" | |
37 | ||
38 | /* | |
39 | * todo | |
40 | * | |
41 | * - bus resets sends a new packet with new generation and node id | |
42 | * | |
43 | */ | |
44 | ||
45 | /* dequeue_event() just kfree()'s the event, so the event has to be | |
46 | * the first field in the struct. */ | |
47 | ||
48 | struct event { | |
49 | struct { void *data; size_t size; } v[2]; | |
50 | struct list_head link; | |
51 | }; | |
52 | ||
97bd9efa KH |
53 | struct bus_reset { |
54 | struct event event; | |
55 | struct fw_cdev_event_bus_reset reset; | |
56 | }; | |
57 | ||
19a15b93 KH |
58 | struct response { |
59 | struct event event; | |
60 | struct fw_transaction transaction; | |
61 | struct client *client; | |
62 | struct fw_cdev_event_response response; | |
63 | }; | |
64 | ||
65 | struct iso_interrupt { | |
66 | struct event event; | |
67 | struct fw_cdev_event_iso_interrupt interrupt; | |
68 | }; | |
69 | ||
70 | struct client { | |
344bbc4d | 71 | u32 version; |
19a15b93 KH |
72 | struct fw_device *device; |
73 | spinlock_t lock; | |
74 | struct list_head handler_list; | |
75 | struct list_head request_list; | |
76 | u32 request_serial; | |
77 | struct list_head event_list; | |
78 | struct semaphore event_list_sem; | |
79 | wait_queue_head_t wait; | |
9aad8125 | 80 | |
19a15b93 | 81 | struct fw_iso_context *iso_context; |
9aad8125 KH |
82 | struct fw_iso_buffer buffer; |
83 | unsigned long vm_start; | |
97bd9efa KH |
84 | |
85 | struct list_head link; | |
19a15b93 KH |
86 | }; |
87 | ||
88 | static inline void __user * | |
89 | u64_to_uptr(__u64 value) | |
90 | { | |
91 | return (void __user *)(unsigned long)value; | |
92 | } | |
93 | ||
94 | static inline __u64 | |
95 | uptr_to_u64(void __user *ptr) | |
96 | { | |
97 | return (__u64)(unsigned long)ptr; | |
98 | } | |
99 | ||
100 | static int fw_device_op_open(struct inode *inode, struct file *file) | |
101 | { | |
102 | struct fw_device *device; | |
103 | struct client *client; | |
97bd9efa | 104 | unsigned long flags; |
19a15b93 KH |
105 | |
106 | device = container_of(inode->i_cdev, struct fw_device, cdev); | |
107 | ||
108 | client = kzalloc(sizeof *client, GFP_KERNEL); | |
109 | if (client == NULL) | |
110 | return -ENOMEM; | |
111 | ||
112 | client->device = fw_device_get(device); | |
113 | INIT_LIST_HEAD(&client->event_list); | |
114 | sema_init(&client->event_list_sem, 0); | |
115 | INIT_LIST_HEAD(&client->handler_list); | |
116 | INIT_LIST_HEAD(&client->request_list); | |
117 | spin_lock_init(&client->lock); | |
118 | init_waitqueue_head(&client->wait); | |
119 | ||
120 | file->private_data = client; | |
121 | ||
97bd9efa KH |
122 | spin_lock_irqsave(&device->card->lock, flags); |
123 | list_add_tail(&client->link, &device->client_list); | |
124 | spin_unlock_irqrestore(&device->card->lock, flags); | |
125 | ||
19a15b93 KH |
126 | return 0; |
127 | } | |
128 | ||
129 | static void queue_event(struct client *client, struct event *event, | |
130 | void *data0, size_t size0, void *data1, size_t size1) | |
131 | { | |
132 | unsigned long flags; | |
133 | ||
134 | event->v[0].data = data0; | |
135 | event->v[0].size = size0; | |
136 | event->v[1].data = data1; | |
137 | event->v[1].size = size1; | |
138 | ||
139 | spin_lock_irqsave(&client->lock, flags); | |
140 | ||
141 | list_add_tail(&event->link, &client->event_list); | |
142 | ||
143 | up(&client->event_list_sem); | |
144 | wake_up_interruptible(&client->wait); | |
145 | ||
146 | spin_unlock_irqrestore(&client->lock, flags); | |
147 | } | |
148 | ||
149 | static int dequeue_event(struct client *client, char __user *buffer, size_t count) | |
150 | { | |
151 | unsigned long flags; | |
152 | struct event *event; | |
153 | size_t size, total; | |
154 | int i, retval = -EFAULT; | |
155 | ||
156 | if (down_interruptible(&client->event_list_sem) < 0) | |
157 | return -EINTR; | |
158 | ||
159 | spin_lock_irqsave(&client->lock, flags); | |
160 | ||
161 | event = container_of(client->event_list.next, struct event, link); | |
162 | list_del(&event->link); | |
163 | ||
164 | spin_unlock_irqrestore(&client->lock, flags); | |
165 | ||
166 | if (buffer == NULL) | |
167 | goto out; | |
168 | ||
169 | total = 0; | |
170 | for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { | |
171 | size = min(event->v[i].size, count - total); | |
172 | if (copy_to_user(buffer + total, event->v[i].data, size)) | |
173 | goto out; | |
174 | total += size; | |
175 | } | |
176 | retval = total; | |
177 | ||
178 | out: | |
179 | kfree(event); | |
180 | ||
181 | return retval; | |
182 | } | |
183 | ||
184 | static ssize_t | |
185 | fw_device_op_read(struct file *file, | |
186 | char __user *buffer, size_t count, loff_t *offset) | |
187 | { | |
188 | struct client *client = file->private_data; | |
189 | ||
190 | return dequeue_event(client, buffer, count); | |
191 | } | |
192 | ||
344bbc4d KH |
193 | static void |
194 | fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, | |
195 | struct fw_device *device) | |
196 | { | |
197 | struct fw_card *card = device->card; | |
198 | ||
199 | event->type = FW_CDEV_EVENT_BUS_RESET; | |
200 | event->node_id = device->node_id; | |
201 | event->local_node_id = card->local_node->node_id; | |
202 | event->bm_node_id = 0; /* FIXME: We don't track the BM. */ | |
203 | event->irm_node_id = card->irm_node->node_id; | |
204 | event->root_node_id = card->root_node->node_id; | |
205 | event->generation = card->generation; | |
206 | } | |
207 | ||
97bd9efa KH |
208 | static void |
209 | queue_bus_reset_event(struct client *client) | |
210 | { | |
211 | struct bus_reset *bus_reset; | |
212 | struct fw_device *device = client->device; | |
97bd9efa KH |
213 | |
214 | bus_reset = kzalloc(sizeof *bus_reset, GFP_ATOMIC); | |
215 | if (bus_reset == NULL) { | |
216 | fw_notify("Out of memory when allocating bus reset event\n"); | |
217 | return; | |
218 | } | |
219 | ||
344bbc4d | 220 | fill_bus_reset_event(&bus_reset->reset, device); |
97bd9efa KH |
221 | |
222 | queue_event(client, &bus_reset->event, | |
223 | &bus_reset->reset, sizeof bus_reset->reset, NULL, 0); | |
224 | } | |
225 | ||
226 | void fw_device_cdev_update(struct fw_device *device) | |
227 | { | |
228 | struct fw_card *card = device->card; | |
229 | struct client *c; | |
230 | unsigned long flags; | |
231 | ||
232 | spin_lock_irqsave(&card->lock, flags); | |
233 | ||
234 | list_for_each_entry(c, &device->client_list, link) | |
235 | queue_bus_reset_event(c); | |
236 | ||
237 | spin_unlock_irqrestore(&card->lock, flags); | |
238 | } | |
239 | ||
344bbc4d | 240 | static int ioctl_get_info(struct client *client, void __user *arg) |
19a15b93 | 241 | { |
344bbc4d KH |
242 | struct fw_cdev_get_info get_info; |
243 | struct fw_cdev_event_bus_reset bus_reset; | |
244 | ||
245 | if (copy_from_user(&get_info, arg, sizeof get_info)) | |
246 | return -EFAULT; | |
247 | ||
248 | client->version = get_info.version; | |
249 | get_info.version = FW_CDEV_VERSION; | |
250 | ||
251 | if (get_info.rom != 0) { | |
252 | void __user *uptr = u64_to_uptr(get_info.rom); | |
253 | size_t length = min(get_info.rom_length, | |
254 | client->device->config_rom_length * 4); | |
255 | ||
256 | if (copy_to_user(uptr, client->device->config_rom, length)) | |
257 | return -EFAULT; | |
258 | } | |
259 | get_info.rom_length = client->device->config_rom_length * 4; | |
260 | ||
261 | if (get_info.bus_reset != 0) { | |
262 | void __user *uptr = u64_to_uptr(get_info.bus_reset); | |
263 | ||
264 | fill_bus_reset_event(&bus_reset, client->device); | |
265 | if (copy_to_user(uptr, &bus_reset, sizeof bus_reset)) | |
266 | return -EFAULT; | |
267 | } | |
19a15b93 | 268 | |
344bbc4d | 269 | if (copy_to_user(arg, &get_info, sizeof get_info)) |
19a15b93 KH |
270 | return -EFAULT; |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | static void | |
276 | complete_transaction(struct fw_card *card, int rcode, | |
277 | void *payload, size_t length, void *data) | |
278 | { | |
279 | struct response *response = data; | |
280 | struct client *client = response->client; | |
281 | ||
282 | if (length < response->response.length) | |
283 | response->response.length = length; | |
284 | if (rcode == RCODE_COMPLETE) | |
285 | memcpy(response->response.data, payload, | |
286 | response->response.length); | |
287 | ||
288 | response->response.type = FW_CDEV_EVENT_RESPONSE; | |
289 | response->response.rcode = rcode; | |
290 | queue_event(client, &response->event, | |
291 | &response->response, sizeof response->response, | |
292 | response->response.data, response->response.length); | |
293 | } | |
294 | ||
295 | static ssize_t ioctl_send_request(struct client *client, void __user *arg) | |
296 | { | |
297 | struct fw_device *device = client->device; | |
298 | struct fw_cdev_send_request request; | |
299 | struct response *response; | |
300 | ||
301 | if (copy_from_user(&request, arg, sizeof request)) | |
302 | return -EFAULT; | |
303 | ||
304 | /* What is the biggest size we'll accept, really? */ | |
305 | if (request.length > 4096) | |
306 | return -EINVAL; | |
307 | ||
308 | response = kmalloc(sizeof *response + request.length, GFP_KERNEL); | |
309 | if (response == NULL) | |
310 | return -ENOMEM; | |
311 | ||
312 | response->client = client; | |
313 | response->response.length = request.length; | |
314 | response->response.closure = request.closure; | |
315 | ||
316 | if (request.data && | |
317 | copy_from_user(response->response.data, | |
318 | u64_to_uptr(request.data), request.length)) { | |
319 | kfree(response); | |
320 | return -EFAULT; | |
321 | } | |
322 | ||
323 | fw_send_request(device->card, &response->transaction, | |
344bbc4d | 324 | request.tcode & 0x1f, |
907293d7 | 325 | device->node->node_id, |
19a15b93 KH |
326 | device->card->generation, |
327 | device->node->max_speed, | |
328 | request.offset, | |
329 | response->response.data, request.length, | |
330 | complete_transaction, response); | |
331 | ||
332 | if (request.data) | |
333 | return sizeof request + request.length; | |
334 | else | |
335 | return sizeof request; | |
336 | } | |
337 | ||
338 | struct address_handler { | |
339 | struct fw_address_handler handler; | |
340 | __u64 closure; | |
341 | struct client *client; | |
342 | struct list_head link; | |
343 | }; | |
344 | ||
345 | struct request { | |
346 | struct fw_request *request; | |
347 | void *data; | |
348 | size_t length; | |
349 | u32 serial; | |
350 | struct list_head link; | |
351 | }; | |
352 | ||
353 | struct request_event { | |
354 | struct event event; | |
355 | struct fw_cdev_event_request request; | |
356 | }; | |
357 | ||
358 | static void | |
359 | handle_request(struct fw_card *card, struct fw_request *r, | |
360 | int tcode, int destination, int source, | |
361 | int generation, int speed, | |
362 | unsigned long long offset, | |
363 | void *payload, size_t length, void *callback_data) | |
364 | { | |
365 | struct address_handler *handler = callback_data; | |
366 | struct request *request; | |
367 | struct request_event *e; | |
368 | unsigned long flags; | |
369 | struct client *client = handler->client; | |
370 | ||
371 | request = kmalloc(sizeof *request, GFP_ATOMIC); | |
372 | e = kmalloc(sizeof *e, GFP_ATOMIC); | |
373 | if (request == NULL || e == NULL) { | |
374 | kfree(request); | |
375 | kfree(e); | |
376 | fw_send_response(card, r, RCODE_CONFLICT_ERROR); | |
377 | return; | |
378 | } | |
379 | ||
380 | request->request = r; | |
381 | request->data = payload; | |
382 | request->length = length; | |
383 | ||
384 | spin_lock_irqsave(&client->lock, flags); | |
385 | request->serial = client->request_serial++; | |
386 | list_add_tail(&request->link, &client->request_list); | |
387 | spin_unlock_irqrestore(&client->lock, flags); | |
388 | ||
389 | e->request.type = FW_CDEV_EVENT_REQUEST; | |
390 | e->request.tcode = tcode; | |
391 | e->request.offset = offset; | |
392 | e->request.length = length; | |
393 | e->request.serial = request->serial; | |
394 | e->request.closure = handler->closure; | |
395 | ||
396 | queue_event(client, &e->event, | |
397 | &e->request, sizeof e->request, payload, length); | |
398 | } | |
399 | ||
400 | static int ioctl_allocate(struct client *client, void __user *arg) | |
401 | { | |
402 | struct fw_cdev_allocate request; | |
403 | struct address_handler *handler; | |
404 | unsigned long flags; | |
405 | struct fw_address_region region; | |
406 | ||
407 | if (copy_from_user(&request, arg, sizeof request)) | |
408 | return -EFAULT; | |
409 | ||
410 | handler = kmalloc(sizeof *handler, GFP_KERNEL); | |
411 | if (handler == NULL) | |
412 | return -ENOMEM; | |
413 | ||
414 | region.start = request.offset; | |
415 | region.end = request.offset + request.length; | |
416 | handler->handler.length = request.length; | |
417 | handler->handler.address_callback = handle_request; | |
418 | handler->handler.callback_data = handler; | |
419 | handler->closure = request.closure; | |
420 | handler->client = client; | |
421 | ||
422 | if (fw_core_add_address_handler(&handler->handler, ®ion) < 0) { | |
423 | kfree(handler); | |
424 | return -EBUSY; | |
425 | } | |
426 | ||
427 | spin_lock_irqsave(&client->lock, flags); | |
428 | list_add_tail(&handler->link, &client->handler_list); | |
429 | spin_unlock_irqrestore(&client->lock, flags); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | static int ioctl_send_response(struct client *client, void __user *arg) | |
435 | { | |
436 | struct fw_cdev_send_response request; | |
437 | struct request *r; | |
438 | unsigned long flags; | |
439 | ||
440 | if (copy_from_user(&request, arg, sizeof request)) | |
441 | return -EFAULT; | |
442 | ||
443 | spin_lock_irqsave(&client->lock, flags); | |
444 | list_for_each_entry(r, &client->request_list, link) { | |
445 | if (r->serial == request.serial) { | |
446 | list_del(&r->link); | |
447 | break; | |
448 | } | |
449 | } | |
450 | spin_unlock_irqrestore(&client->lock, flags); | |
451 | ||
452 | if (&r->link == &client->request_list) | |
453 | return -EINVAL; | |
454 | ||
455 | if (request.length < r->length) | |
456 | r->length = request.length; | |
457 | if (copy_from_user(r->data, u64_to_uptr(request.data), r->length)) | |
458 | return -EFAULT; | |
459 | ||
460 | fw_send_response(client->device->card, r->request, request.rcode); | |
461 | ||
462 | kfree(r); | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
5371842b KH |
467 | static int ioctl_initiate_bus_reset(struct client *client, void __user *arg) |
468 | { | |
469 | struct fw_cdev_initiate_bus_reset request; | |
470 | int short_reset; | |
471 | ||
472 | if (copy_from_user(&request, arg, sizeof request)) | |
473 | return -EFAULT; | |
474 | ||
475 | short_reset = (request.type == FW_CDEV_SHORT_RESET); | |
476 | ||
477 | return fw_core_initiate_bus_reset(client->device->card, short_reset); | |
478 | } | |
479 | ||
19a15b93 | 480 | static void |
9b32d5f3 KH |
481 | iso_callback(struct fw_iso_context *context, u32 cycle, |
482 | size_t header_length, void *header, void *data) | |
19a15b93 KH |
483 | { |
484 | struct client *client = data; | |
485 | struct iso_interrupt *interrupt; | |
486 | ||
9b32d5f3 | 487 | interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC); |
19a15b93 KH |
488 | if (interrupt == NULL) |
489 | return; | |
490 | ||
491 | interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; | |
492 | interrupt->interrupt.closure = 0; | |
493 | interrupt->interrupt.cycle = cycle; | |
9b32d5f3 KH |
494 | interrupt->interrupt.header_length = header_length; |
495 | memcpy(interrupt->interrupt.header, header, header_length); | |
19a15b93 | 496 | queue_event(client, &interrupt->event, |
9b32d5f3 KH |
497 | &interrupt->interrupt, |
498 | sizeof interrupt->interrupt + header_length, NULL, 0); | |
19a15b93 KH |
499 | } |
500 | ||
501 | static int ioctl_create_iso_context(struct client *client, void __user *arg) | |
502 | { | |
503 | struct fw_cdev_create_iso_context request; | |
504 | ||
505 | if (copy_from_user(&request, arg, sizeof request)) | |
506 | return -EFAULT; | |
507 | ||
295e3feb KH |
508 | if (request.type > FW_ISO_CONTEXT_RECEIVE) |
509 | return -EINVAL; | |
510 | ||
21efb3cf KH |
511 | if (request.channel > 63) |
512 | return -EINVAL; | |
513 | ||
98b6cbe8 KH |
514 | if (request.sync > 15) |
515 | return -EINVAL; | |
516 | ||
517 | if (request.tags == 0 || request.tags > 15) | |
518 | return -EINVAL; | |
519 | ||
21efb3cf KH |
520 | if (request.speed > SCODE_3200) |
521 | return -EINVAL; | |
522 | ||
19a15b93 | 523 | client->iso_context = fw_iso_context_create(client->device->card, |
295e3feb | 524 | request.type, |
21efb3cf KH |
525 | request.channel, |
526 | request.speed, | |
295e3feb | 527 | request.header_size, |
98b6cbe8 KH |
528 | request.sync, |
529 | request.tags, | |
19a15b93 KH |
530 | iso_callback, client); |
531 | if (IS_ERR(client->iso_context)) | |
532 | return PTR_ERR(client->iso_context); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | static int ioctl_queue_iso(struct client *client, void __user *arg) | |
538 | { | |
539 | struct fw_cdev_queue_iso request; | |
540 | struct fw_cdev_iso_packet __user *p, *end, *next; | |
9b32d5f3 | 541 | struct fw_iso_context *ctx = client->iso_context; |
295e3feb | 542 | unsigned long payload, payload_end, header_length; |
19a15b93 KH |
543 | int count; |
544 | struct { | |
545 | struct fw_iso_packet packet; | |
546 | u8 header[256]; | |
547 | } u; | |
548 | ||
9b32d5f3 | 549 | if (ctx == NULL) |
19a15b93 KH |
550 | return -EINVAL; |
551 | if (copy_from_user(&request, arg, sizeof request)) | |
552 | return -EFAULT; | |
553 | ||
554 | /* If the user passes a non-NULL data pointer, has mmap()'ed | |
555 | * the iso buffer, and the pointer points inside the buffer, | |
556 | * we setup the payload pointers accordingly. Otherwise we | |
9aad8125 | 557 | * set them both to 0, which will still let packets with |
19a15b93 KH |
558 | * payload_length == 0 through. In other words, if no packets |
559 | * use the indirect payload, the iso buffer need not be mapped | |
560 | * and the request.data pointer is ignored.*/ | |
561 | ||
9aad8125 KH |
562 | payload = (unsigned long)request.data - client->vm_start; |
563 | payload_end = payload + (client->buffer.page_count << PAGE_SHIFT); | |
564 | if (request.data == 0 || client->buffer.pages == NULL || | |
565 | payload >= payload_end) { | |
566 | payload = 0; | |
567 | payload_end = 0; | |
19a15b93 KH |
568 | } |
569 | ||
570 | if (!access_ok(VERIFY_READ, request.packets, request.size)) | |
571 | return -EFAULT; | |
572 | ||
573 | p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets); | |
574 | end = (void __user *)p + request.size; | |
575 | count = 0; | |
576 | while (p < end) { | |
577 | if (__copy_from_user(&u.packet, p, sizeof *p)) | |
578 | return -EFAULT; | |
295e3feb | 579 | |
9b32d5f3 | 580 | if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) { |
295e3feb KH |
581 | header_length = u.packet.header_length; |
582 | } else { | |
583 | /* We require that header_length is a multiple of | |
584 | * the fixed header size, ctx->header_size */ | |
9b32d5f3 KH |
585 | if (ctx->header_size == 0) { |
586 | if (u.packet.header_length > 0) | |
587 | return -EINVAL; | |
588 | } else if (u.packet.header_length % ctx->header_size != 0) { | |
295e3feb | 589 | return -EINVAL; |
9b32d5f3 | 590 | } |
295e3feb KH |
591 | header_length = 0; |
592 | } | |
593 | ||
19a15b93 | 594 | next = (struct fw_cdev_iso_packet __user *) |
295e3feb | 595 | &p->header[header_length / 4]; |
19a15b93 KH |
596 | if (next > end) |
597 | return -EINVAL; | |
598 | if (__copy_from_user | |
295e3feb | 599 | (u.packet.header, p->header, header_length)) |
19a15b93 | 600 | return -EFAULT; |
98b6cbe8 | 601 | if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && |
19a15b93 KH |
602 | u.packet.header_length + u.packet.payload_length > 0) |
603 | return -EINVAL; | |
604 | if (payload + u.packet.payload_length > payload_end) | |
605 | return -EINVAL; | |
606 | ||
9b32d5f3 KH |
607 | if (fw_iso_context_queue(ctx, &u.packet, |
608 | &client->buffer, payload)) | |
19a15b93 KH |
609 | break; |
610 | ||
611 | p = next; | |
612 | payload += u.packet.payload_length; | |
613 | count++; | |
614 | } | |
615 | ||
616 | request.size -= uptr_to_u64(p) - request.packets; | |
617 | request.packets = uptr_to_u64(p); | |
9aad8125 | 618 | request.data = client->vm_start + payload; |
19a15b93 KH |
619 | |
620 | if (copy_to_user(arg, &request, sizeof request)) | |
621 | return -EFAULT; | |
622 | ||
623 | return count; | |
624 | } | |
625 | ||
69cdb726 | 626 | static int ioctl_start_iso(struct client *client, void __user *arg) |
19a15b93 | 627 | { |
69cdb726 | 628 | struct fw_cdev_start_iso request; |
19a15b93 KH |
629 | |
630 | if (copy_from_user(&request, arg, sizeof request)) | |
631 | return -EFAULT; | |
632 | ||
21efb3cf | 633 | return fw_iso_context_start(client->iso_context, request.cycle); |
19a15b93 KH |
634 | } |
635 | ||
b8295668 KH |
636 | static int ioctl_stop_iso(struct client *client, void __user *arg) |
637 | { | |
638 | return fw_iso_context_stop(client->iso_context); | |
639 | } | |
640 | ||
19a15b93 KH |
641 | static int |
642 | dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg) | |
643 | { | |
644 | switch (cmd) { | |
344bbc4d KH |
645 | case FW_CDEV_IOC_GET_INFO: |
646 | return ioctl_get_info(client, arg); | |
19a15b93 KH |
647 | case FW_CDEV_IOC_SEND_REQUEST: |
648 | return ioctl_send_request(client, arg); | |
649 | case FW_CDEV_IOC_ALLOCATE: | |
650 | return ioctl_allocate(client, arg); | |
651 | case FW_CDEV_IOC_SEND_RESPONSE: | |
652 | return ioctl_send_response(client, arg); | |
5371842b KH |
653 | case FW_CDEV_IOC_INITIATE_BUS_RESET: |
654 | return ioctl_initiate_bus_reset(client, arg); | |
19a15b93 KH |
655 | case FW_CDEV_IOC_CREATE_ISO_CONTEXT: |
656 | return ioctl_create_iso_context(client, arg); | |
657 | case FW_CDEV_IOC_QUEUE_ISO: | |
658 | return ioctl_queue_iso(client, arg); | |
69cdb726 KH |
659 | case FW_CDEV_IOC_START_ISO: |
660 | return ioctl_start_iso(client, arg); | |
b8295668 KH |
661 | case FW_CDEV_IOC_STOP_ISO: |
662 | return ioctl_stop_iso(client, arg); | |
19a15b93 KH |
663 | default: |
664 | return -EINVAL; | |
665 | } | |
666 | } | |
667 | ||
668 | static long | |
669 | fw_device_op_ioctl(struct file *file, | |
670 | unsigned int cmd, unsigned long arg) | |
671 | { | |
672 | struct client *client = file->private_data; | |
673 | ||
674 | return dispatch_ioctl(client, cmd, (void __user *) arg); | |
675 | } | |
676 | ||
677 | #ifdef CONFIG_COMPAT | |
678 | static long | |
679 | fw_device_op_compat_ioctl(struct file *file, | |
680 | unsigned int cmd, unsigned long arg) | |
681 | { | |
682 | struct client *client = file->private_data; | |
683 | ||
684 | return dispatch_ioctl(client, cmd, compat_ptr(arg)); | |
685 | } | |
686 | #endif | |
687 | ||
688 | static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) | |
689 | { | |
690 | struct client *client = file->private_data; | |
9aad8125 KH |
691 | enum dma_data_direction direction; |
692 | unsigned long size; | |
693 | int page_count, retval; | |
694 | ||
695 | /* FIXME: We could support multiple buffers, but we don't. */ | |
696 | if (client->buffer.pages != NULL) | |
697 | return -EBUSY; | |
698 | ||
699 | if (!(vma->vm_flags & VM_SHARED)) | |
700 | return -EINVAL; | |
19a15b93 | 701 | |
9aad8125 | 702 | if (vma->vm_start & ~PAGE_MASK) |
19a15b93 KH |
703 | return -EINVAL; |
704 | ||
705 | client->vm_start = vma->vm_start; | |
9aad8125 KH |
706 | size = vma->vm_end - vma->vm_start; |
707 | page_count = size >> PAGE_SHIFT; | |
708 | if (size & ~PAGE_MASK) | |
709 | return -EINVAL; | |
710 | ||
711 | if (vma->vm_flags & VM_WRITE) | |
712 | direction = DMA_TO_DEVICE; | |
713 | else | |
714 | direction = DMA_FROM_DEVICE; | |
715 | ||
716 | retval = fw_iso_buffer_init(&client->buffer, client->device->card, | |
717 | page_count, direction); | |
718 | if (retval < 0) | |
719 | return retval; | |
19a15b93 | 720 | |
9aad8125 KH |
721 | retval = fw_iso_buffer_map(&client->buffer, vma); |
722 | if (retval < 0) | |
723 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
724 | ||
725 | return retval; | |
19a15b93 KH |
726 | } |
727 | ||
728 | static int fw_device_op_release(struct inode *inode, struct file *file) | |
729 | { | |
730 | struct client *client = file->private_data; | |
731 | struct address_handler *h, *next; | |
732 | struct request *r, *next_r; | |
97bd9efa | 733 | unsigned long flags; |
19a15b93 | 734 | |
9aad8125 KH |
735 | if (client->buffer.pages) |
736 | fw_iso_buffer_destroy(&client->buffer, client->device->card); | |
737 | ||
19a15b93 KH |
738 | if (client->iso_context) |
739 | fw_iso_context_destroy(client->iso_context); | |
740 | ||
741 | list_for_each_entry_safe(h, next, &client->handler_list, link) { | |
742 | fw_core_remove_address_handler(&h->handler); | |
743 | kfree(h); | |
744 | } | |
745 | ||
746 | list_for_each_entry_safe(r, next_r, &client->request_list, link) { | |
747 | fw_send_response(client->device->card, r->request, | |
748 | RCODE_CONFLICT_ERROR); | |
749 | kfree(r); | |
750 | } | |
751 | ||
752 | /* TODO: wait for all transactions to finish so | |
753 | * complete_transaction doesn't try to queue up responses | |
754 | * after we free client. */ | |
755 | while (!list_empty(&client->event_list)) | |
756 | dequeue_event(client, NULL, 0); | |
757 | ||
97bd9efa KH |
758 | spin_lock_irqsave(&client->device->card->lock, flags); |
759 | list_del(&client->link); | |
760 | spin_unlock_irqrestore(&client->device->card->lock, flags); | |
761 | ||
19a15b93 KH |
762 | fw_device_put(client->device); |
763 | kfree(client); | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | static unsigned int fw_device_op_poll(struct file *file, poll_table * pt) | |
769 | { | |
770 | struct client *client = file->private_data; | |
771 | ||
772 | poll_wait(file, &client->wait, pt); | |
773 | ||
774 | if (!list_empty(&client->event_list)) | |
775 | return POLLIN | POLLRDNORM; | |
776 | else | |
777 | return 0; | |
778 | } | |
779 | ||
21ebcd12 | 780 | const struct file_operations fw_device_ops = { |
19a15b93 KH |
781 | .owner = THIS_MODULE, |
782 | .open = fw_device_op_open, | |
783 | .read = fw_device_op_read, | |
784 | .unlocked_ioctl = fw_device_op_ioctl, | |
785 | .poll = fw_device_op_poll, | |
786 | .release = fw_device_op_release, | |
787 | .mmap = fw_device_op_mmap, | |
788 | ||
789 | #ifdef CONFIG_COMPAT | |
5af4e5ea | 790 | .compat_ioctl = fw_device_op_compat_ioctl, |
19a15b93 KH |
791 | #endif |
792 | }; |