fw-device-cdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. get_info.card = client->device->card->index;
  240. if (copy_to_user(arg, &get_info, sizeof get_info))
  241. return -EFAULT;
  242. return 0;
  243. }
  244. static void
  245. complete_transaction(struct fw_card *card, int rcode,
  246. void *payload, size_t length, void *data)
  247. {
  248. struct response *response = data;
  249. struct client *client = response->client;
  250. unsigned long flags;
  251. if (length < response->response.length)
  252. response->response.length = length;
  253. if (rcode == RCODE_COMPLETE)
  254. memcpy(response->response.data, payload,
  255. response->response.length);
  256. spin_lock_irqsave(&client->lock, flags);
  257. list_del(&response->link);
  258. spin_unlock_irqrestore(&client->lock, flags);
  259. response->response.type = FW_CDEV_EVENT_RESPONSE;
  260. response->response.rcode = rcode;
  261. queue_event(client, &response->event,
  262. &response->response, sizeof response->response,
  263. response->response.data, response->response.length);
  264. }
  265. static ssize_t ioctl_send_request(struct client *client, void __user *arg)
  266. {
  267. struct fw_device *device = client->device;
  268. struct fw_cdev_send_request request;
  269. struct response *response;
  270. unsigned long flags;
  271. if (copy_from_user(&request, arg, sizeof request))
  272. return -EFAULT;
  273. /* What is the biggest size we'll accept, really? */
  274. if (request.length > 4096)
  275. return -EINVAL;
  276. response = kmalloc(sizeof *response + request.length, GFP_KERNEL);
  277. if (response == NULL)
  278. return -ENOMEM;
  279. response->client = client;
  280. response->response.length = request.length;
  281. response->response.closure = request.closure;
  282. if (request.data &&
  283. copy_from_user(response->response.data,
  284. u64_to_uptr(request.data), request.length)) {
  285. kfree(response);
  286. return -EFAULT;
  287. }
  288. spin_lock_irqsave(&client->lock, flags);
  289. list_add_tail(&response->link, &client->transaction_list);
  290. spin_unlock_irqrestore(&client->lock, flags);
  291. fw_send_request(device->card, &response->transaction,
  292. request.tcode & 0x1f,
  293. device->node->node_id,
  294. request.generation,
  295. device->node->max_speed,
  296. request.offset,
  297. response->response.data, request.length,
  298. complete_transaction, response);
  299. if (request.data)
  300. return sizeof request + request.length;
  301. else
  302. return sizeof request;
  303. }
  304. struct address_handler {
  305. struct fw_address_handler handler;
  306. __u64 closure;
  307. struct client *client;
  308. struct list_head link;
  309. };
  310. struct request {
  311. struct fw_request *request;
  312. void *data;
  313. size_t length;
  314. u32 serial;
  315. struct list_head link;
  316. };
  317. struct request_event {
  318. struct event event;
  319. struct fw_cdev_event_request request;
  320. };
  321. static void
  322. handle_request(struct fw_card *card, struct fw_request *r,
  323. int tcode, int destination, int source,
  324. int generation, int speed,
  325. unsigned long long offset,
  326. void *payload, size_t length, void *callback_data)
  327. {
  328. struct address_handler *handler = callback_data;
  329. struct request *request;
  330. struct request_event *e;
  331. unsigned long flags;
  332. struct client *client = handler->client;
  333. request = kmalloc(sizeof *request, GFP_ATOMIC);
  334. e = kmalloc(sizeof *e, GFP_ATOMIC);
  335. if (request == NULL || e == NULL) {
  336. kfree(request);
  337. kfree(e);
  338. fw_send_response(card, r, RCODE_CONFLICT_ERROR);
  339. return;
  340. }
  341. request->request = r;
  342. request->data = payload;
  343. request->length = length;
  344. spin_lock_irqsave(&client->lock, flags);
  345. request->serial = client->request_serial++;
  346. list_add_tail(&request->link, &client->request_list);
  347. spin_unlock_irqrestore(&client->lock, flags);
  348. e->request.type = FW_CDEV_EVENT_REQUEST;
  349. e->request.tcode = tcode;
  350. e->request.offset = offset;
  351. e->request.length = length;
  352. e->request.serial = request->serial;
  353. e->request.closure = handler->closure;
  354. queue_event(client, &e->event,
  355. &e->request, sizeof e->request, payload, length);
  356. }
  357. static int ioctl_allocate(struct client *client, void __user *arg)
  358. {
  359. struct fw_cdev_allocate request;
  360. struct address_handler *handler;
  361. unsigned long flags;
  362. struct fw_address_region region;
  363. if (copy_from_user(&request, arg, sizeof request))
  364. return -EFAULT;
  365. handler = kmalloc(sizeof *handler, GFP_KERNEL);
  366. if (handler == NULL)
  367. return -ENOMEM;
  368. region.start = request.offset;
  369. region.end = request.offset + request.length;
  370. handler->handler.length = request.length;
  371. handler->handler.address_callback = handle_request;
  372. handler->handler.callback_data = handler;
  373. handler->closure = request.closure;
  374. handler->client = client;
  375. if (fw_core_add_address_handler(&handler->handler, &region) < 0) {
  376. kfree(handler);
  377. return -EBUSY;
  378. }
  379. spin_lock_irqsave(&client->lock, flags);
  380. list_add_tail(&handler->link, &client->handler_list);
  381. spin_unlock_irqrestore(&client->lock, flags);
  382. return 0;
  383. }
  384. static int ioctl_send_response(struct client *client, void __user *arg)
  385. {
  386. struct fw_cdev_send_response request;
  387. struct request *r;
  388. unsigned long flags;
  389. if (copy_from_user(&request, arg, sizeof request))
  390. return -EFAULT;
  391. spin_lock_irqsave(&client->lock, flags);
  392. list_for_each_entry(r, &client->request_list, link) {
  393. if (r->serial == request.serial) {
  394. list_del(&r->link);
  395. break;
  396. }
  397. }
  398. spin_unlock_irqrestore(&client->lock, flags);
  399. if (&r->link == &client->request_list)
  400. return -EINVAL;
  401. if (request.length < r->length)
  402. r->length = request.length;
  403. if (copy_from_user(r->data, u64_to_uptr(request.data), r->length))
  404. return -EFAULT;
  405. fw_send_response(client->device->card, r->request, request.rcode);
  406. kfree(r);
  407. return 0;
  408. }
  409. static int ioctl_initiate_bus_reset(struct client *client, void __user *arg)
  410. {
  411. struct fw_cdev_initiate_bus_reset request;
  412. int short_reset;
  413. if (copy_from_user(&request, arg, sizeof request))
  414. return -EFAULT;
  415. short_reset = (request.type == FW_CDEV_SHORT_RESET);
  416. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  417. }
  418. static void
  419. iso_callback(struct fw_iso_context *context, u32 cycle,
  420. size_t header_length, void *header, void *data)
  421. {
  422. struct client *client = data;
  423. struct iso_interrupt *interrupt;
  424. interrupt = kzalloc(sizeof *interrupt + header_length, GFP_ATOMIC);
  425. if (interrupt == NULL)
  426. return;
  427. interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  428. interrupt->interrupt.closure = 0;
  429. interrupt->interrupt.cycle = cycle;
  430. interrupt->interrupt.header_length = header_length;
  431. memcpy(interrupt->interrupt.header, header, header_length);
  432. queue_event(client, &interrupt->event,
  433. &interrupt->interrupt,
  434. sizeof interrupt->interrupt + header_length, NULL, 0);
  435. }
  436. static int ioctl_create_iso_context(struct client *client, void __user *arg)
  437. {
  438. struct fw_cdev_create_iso_context request;
  439. if (copy_from_user(&request, arg, sizeof request))
  440. return -EFAULT;
  441. if (request.channel > 63)
  442. return -EINVAL;
  443. switch (request.type) {
  444. case FW_ISO_CONTEXT_RECEIVE:
  445. if (request.header_size < 4 || (request.header_size & 3))
  446. return -EINVAL;
  447. break;
  448. case FW_ISO_CONTEXT_TRANSMIT:
  449. if (request.speed > SCODE_3200)
  450. return -EINVAL;
  451. break;
  452. default:
  453. return -EINVAL;
  454. }
  455. client->iso_context = fw_iso_context_create(client->device->card,
  456. request.type,
  457. request.channel,
  458. request.speed,
  459. request.header_size,
  460. iso_callback, client);
  461. if (IS_ERR(client->iso_context))
  462. return PTR_ERR(client->iso_context);
  463. return 0;
  464. }
  465. static int ioctl_queue_iso(struct client *client, void __user *arg)
  466. {
  467. struct fw_cdev_queue_iso request;
  468. struct fw_cdev_iso_packet __user *p, *end, *next;
  469. struct fw_iso_context *ctx = client->iso_context;
  470. unsigned long payload, payload_end, header_length;
  471. int count;
  472. struct {
  473. struct fw_iso_packet packet;
  474. u8 header[256];
  475. } u;
  476. if (ctx == NULL)
  477. return -EINVAL;
  478. if (copy_from_user(&request, arg, sizeof request))
  479. return -EFAULT;
  480. /* If the user passes a non-NULL data pointer, has mmap()'ed
  481. * the iso buffer, and the pointer points inside the buffer,
  482. * we setup the payload pointers accordingly. Otherwise we
  483. * set them both to 0, which will still let packets with
  484. * payload_length == 0 through. In other words, if no packets
  485. * use the indirect payload, the iso buffer need not be mapped
  486. * and the request.data pointer is ignored.*/
  487. payload = (unsigned long)request.data - client->vm_start;
  488. payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
  489. if (request.data == 0 || client->buffer.pages == NULL ||
  490. payload >= payload_end) {
  491. payload = 0;
  492. payload_end = 0;
  493. }
  494. if (!access_ok(VERIFY_READ, request.packets, request.size))
  495. return -EFAULT;
  496. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
  497. end = (void __user *)p + request.size;
  498. count = 0;
  499. while (p < end) {
  500. if (__copy_from_user(&u.packet, p, sizeof *p))
  501. return -EFAULT;
  502. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  503. header_length = u.packet.header_length;
  504. } else {
  505. /* We require that header_length is a multiple of
  506. * the fixed header size, ctx->header_size */
  507. if (ctx->header_size == 0) {
  508. if (u.packet.header_length > 0)
  509. return -EINVAL;
  510. } else if (u.packet.header_length % ctx->header_size != 0) {
  511. return -EINVAL;
  512. }
  513. header_length = 0;
  514. }
  515. next = (struct fw_cdev_iso_packet __user *)
  516. &p->header[header_length / 4];
  517. if (next > end)
  518. return -EINVAL;
  519. if (__copy_from_user
  520. (u.packet.header, p->header, header_length))
  521. return -EFAULT;
  522. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  523. u.packet.header_length + u.packet.payload_length > 0)
  524. return -EINVAL;
  525. if (payload + u.packet.payload_length > payload_end)
  526. return -EINVAL;
  527. if (fw_iso_context_queue(ctx, &u.packet,
  528. &client->buffer, payload))
  529. break;
  530. p = next;
  531. payload += u.packet.payload_length;
  532. count++;
  533. }
  534. request.size -= uptr_to_u64(p) - request.packets;
  535. request.packets = uptr_to_u64(p);
  536. request.data = client->vm_start + payload;
  537. if (copy_to_user(arg, &request, sizeof request))
  538. return -EFAULT;
  539. return count;
  540. }
  541. static int ioctl_start_iso(struct client *client, void __user *arg)
  542. {
  543. struct fw_cdev_start_iso request;
  544. if (copy_from_user(&request, arg, sizeof request))
  545. return -EFAULT;
  546. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  547. if (request.tags == 0 || request.tags > 15)
  548. return -EINVAL;
  549. if (request.sync > 15)
  550. return -EINVAL;
  551. }
  552. return fw_iso_context_start(client->iso_context,
  553. request.cycle, request.sync, request.tags);
  554. }
  555. static int ioctl_stop_iso(struct client *client, void __user *arg)
  556. {
  557. return fw_iso_context_stop(client->iso_context);
  558. }
  559. static int
  560. dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
  561. {
  562. switch (cmd) {
  563. case FW_CDEV_IOC_GET_INFO:
  564. return ioctl_get_info(client, arg);
  565. case FW_CDEV_IOC_SEND_REQUEST:
  566. return ioctl_send_request(client, arg);
  567. case FW_CDEV_IOC_ALLOCATE:
  568. return ioctl_allocate(client, arg);
  569. case FW_CDEV_IOC_SEND_RESPONSE:
  570. return ioctl_send_response(client, arg);
  571. case FW_CDEV_IOC_INITIATE_BUS_RESET:
  572. return ioctl_initiate_bus_reset(client, arg);
  573. case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
  574. return ioctl_create_iso_context(client, arg);
  575. case FW_CDEV_IOC_QUEUE_ISO:
  576. return ioctl_queue_iso(client, arg);
  577. case FW_CDEV_IOC_START_ISO:
  578. return ioctl_start_iso(client, arg);
  579. case FW_CDEV_IOC_STOP_ISO:
  580. return ioctl_stop_iso(client, arg);
  581. default:
  582. return -EINVAL;
  583. }
  584. }
  585. static long
  586. fw_device_op_ioctl(struct file *file,
  587. unsigned int cmd, unsigned long arg)
  588. {
  589. struct client *client = file->private_data;
  590. return dispatch_ioctl(client, cmd, (void __user *) arg);
  591. }
  592. #ifdef CONFIG_COMPAT
  593. static long
  594. fw_device_op_compat_ioctl(struct file *file,
  595. unsigned int cmd, unsigned long arg)
  596. {
  597. struct client *client = file->private_data;
  598. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  599. }
  600. #endif
  601. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  602. {
  603. struct client *client = file->private_data;
  604. enum dma_data_direction direction;
  605. unsigned long size;
  606. int page_count, retval;
  607. /* FIXME: We could support multiple buffers, but we don't. */
  608. if (client->buffer.pages != NULL)
  609. return -EBUSY;
  610. if (!(vma->vm_flags & VM_SHARED))
  611. return -EINVAL;
  612. if (vma->vm_start & ~PAGE_MASK)
  613. return -EINVAL;
  614. client->vm_start = vma->vm_start;
  615. size = vma->vm_end - vma->vm_start;
  616. page_count = size >> PAGE_SHIFT;
  617. if (size & ~PAGE_MASK)
  618. return -EINVAL;
  619. if (vma->vm_flags & VM_WRITE)
  620. direction = DMA_TO_DEVICE;
  621. else
  622. direction = DMA_FROM_DEVICE;
  623. retval = fw_iso_buffer_init(&client->buffer, client->device->card,
  624. page_count, direction);
  625. if (retval < 0)
  626. return retval;
  627. retval = fw_iso_buffer_map(&client->buffer, vma);
  628. if (retval < 0)
  629. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  630. return retval;
  631. }
  632. static int fw_device_op_release(struct inode *inode, struct file *file)
  633. {
  634. struct client *client = file->private_data;
  635. struct address_handler *h, *next_h;
  636. struct request *r, *next_r;
  637. struct event *e, *next_e;
  638. struct response *t, *next_t;
  639. unsigned long flags;
  640. if (client->buffer.pages)
  641. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  642. if (client->iso_context)
  643. fw_iso_context_destroy(client->iso_context);
  644. list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
  645. fw_core_remove_address_handler(&h->handler);
  646. kfree(h);
  647. }
  648. list_for_each_entry_safe(r, next_r, &client->request_list, link) {
  649. fw_send_response(client->device->card, r->request,
  650. RCODE_CONFLICT_ERROR);
  651. kfree(r);
  652. }
  653. list_for_each_entry_safe(t, next_t, &client->transaction_list, link)
  654. fw_cancel_transaction(client->device->card, &t->transaction);
  655. /* FIXME: We should wait for the async tasklets to stop
  656. * running before freeing the memory. */
  657. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  658. kfree(e);
  659. spin_lock_irqsave(&client->device->card->lock, flags);
  660. list_del(&client->link);
  661. spin_unlock_irqrestore(&client->device->card->lock, flags);
  662. fw_device_put(client->device);
  663. kfree(client);
  664. return 0;
  665. }
  666. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  667. {
  668. struct client *client = file->private_data;
  669. unsigned int mask = 0;
  670. poll_wait(file, &client->wait, pt);
  671. if (fw_device_is_shutdown(client->device))
  672. mask |= POLLHUP | POLLERR;
  673. if (!list_empty(&client->event_list))
  674. mask |= POLLIN | POLLRDNORM;
  675. return mask;
  676. }
  677. const struct file_operations fw_device_ops = {
  678. .owner = THIS_MODULE,
  679. .open = fw_device_op_open,
  680. .read = fw_device_op_read,
  681. .unlocked_ioctl = fw_device_op_ioctl,
  682. .poll = fw_device_op_poll,
  683. .release = fw_device_op_release,
  684. .mmap = fw_device_op_mmap,
  685. #ifdef CONFIG_COMPAT
  686. .compat_ioctl = fw_device_op_compat_ioctl,
  687. #endif
  688. };