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