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