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