fw-device-cdev.c 20 KB

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