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