fw-device-cdev.c 21 KB

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