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.sync > 15)
  446. return -EINVAL;
  447. if (request.tags == 0 || request.tags > 15)
  448. return -EINVAL;
  449. if (request.header_size < 4 || (request.header_size & 3))
  450. return -EINVAL;
  451. break;
  452. case FW_ISO_CONTEXT_TRANSMIT:
  453. if (request.speed > SCODE_3200)
  454. return -EINVAL;
  455. break;
  456. default:
  457. return -EINVAL;
  458. }
  459. client->iso_context = fw_iso_context_create(client->device->card,
  460. request.type,
  461. request.channel,
  462. request.speed,
  463. request.sync,
  464. request.tags,
  465. request.header_size,
  466. iso_callback, client);
  467. if (IS_ERR(client->iso_context))
  468. return PTR_ERR(client->iso_context);
  469. return 0;
  470. }
  471. static int ioctl_queue_iso(struct client *client, void __user *arg)
  472. {
  473. struct fw_cdev_queue_iso request;
  474. struct fw_cdev_iso_packet __user *p, *end, *next;
  475. struct fw_iso_context *ctx = client->iso_context;
  476. unsigned long payload, payload_end, header_length;
  477. int count;
  478. struct {
  479. struct fw_iso_packet packet;
  480. u8 header[256];
  481. } u;
  482. if (ctx == NULL)
  483. return -EINVAL;
  484. if (copy_from_user(&request, arg, sizeof request))
  485. return -EFAULT;
  486. /* If the user passes a non-NULL data pointer, has mmap()'ed
  487. * the iso buffer, and the pointer points inside the buffer,
  488. * we setup the payload pointers accordingly. Otherwise we
  489. * set them both to 0, which will still let packets with
  490. * payload_length == 0 through. In other words, if no packets
  491. * use the indirect payload, the iso buffer need not be mapped
  492. * and the request.data pointer is ignored.*/
  493. payload = (unsigned long)request.data - client->vm_start;
  494. payload_end = payload + (client->buffer.page_count << PAGE_SHIFT);
  495. if (request.data == 0 || client->buffer.pages == NULL ||
  496. payload >= payload_end) {
  497. payload = 0;
  498. payload_end = 0;
  499. }
  500. if (!access_ok(VERIFY_READ, request.packets, request.size))
  501. return -EFAULT;
  502. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request.packets);
  503. end = (void __user *)p + request.size;
  504. count = 0;
  505. while (p < end) {
  506. if (__copy_from_user(&u.packet, p, sizeof *p))
  507. return -EFAULT;
  508. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  509. header_length = u.packet.header_length;
  510. } else {
  511. /* We require that header_length is a multiple of
  512. * the fixed header size, ctx->header_size */
  513. if (ctx->header_size == 0) {
  514. if (u.packet.header_length > 0)
  515. return -EINVAL;
  516. } else if (u.packet.header_length % ctx->header_size != 0) {
  517. return -EINVAL;
  518. }
  519. header_length = 0;
  520. }
  521. next = (struct fw_cdev_iso_packet __user *)
  522. &p->header[header_length / 4];
  523. if (next > end)
  524. return -EINVAL;
  525. if (__copy_from_user
  526. (u.packet.header, p->header, header_length))
  527. return -EFAULT;
  528. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  529. u.packet.header_length + u.packet.payload_length > 0)
  530. return -EINVAL;
  531. if (payload + u.packet.payload_length > payload_end)
  532. return -EINVAL;
  533. if (fw_iso_context_queue(ctx, &u.packet,
  534. &client->buffer, payload))
  535. break;
  536. p = next;
  537. payload += u.packet.payload_length;
  538. count++;
  539. }
  540. request.size -= uptr_to_u64(p) - request.packets;
  541. request.packets = uptr_to_u64(p);
  542. request.data = client->vm_start + payload;
  543. if (copy_to_user(arg, &request, sizeof request))
  544. return -EFAULT;
  545. return count;
  546. }
  547. static int ioctl_start_iso(struct client *client, void __user *arg)
  548. {
  549. struct fw_cdev_start_iso request;
  550. if (copy_from_user(&request, arg, sizeof request))
  551. return -EFAULT;
  552. return fw_iso_context_start(client->iso_context, request.cycle);
  553. }
  554. static int ioctl_stop_iso(struct client *client, void __user *arg)
  555. {
  556. return fw_iso_context_stop(client->iso_context);
  557. }
  558. static int
  559. dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
  560. {
  561. switch (cmd) {
  562. case FW_CDEV_IOC_GET_INFO:
  563. return ioctl_get_info(client, arg);
  564. case FW_CDEV_IOC_SEND_REQUEST:
  565. return ioctl_send_request(client, arg);
  566. case FW_CDEV_IOC_ALLOCATE:
  567. return ioctl_allocate(client, arg);
  568. case FW_CDEV_IOC_SEND_RESPONSE:
  569. return ioctl_send_response(client, arg);
  570. case FW_CDEV_IOC_INITIATE_BUS_RESET:
  571. return ioctl_initiate_bus_reset(client, arg);
  572. case FW_CDEV_IOC_CREATE_ISO_CONTEXT:
  573. return ioctl_create_iso_context(client, arg);
  574. case FW_CDEV_IOC_QUEUE_ISO:
  575. return ioctl_queue_iso(client, arg);
  576. case FW_CDEV_IOC_START_ISO:
  577. return ioctl_start_iso(client, arg);
  578. case FW_CDEV_IOC_STOP_ISO:
  579. return ioctl_stop_iso(client, arg);
  580. default:
  581. return -EINVAL;
  582. }
  583. }
  584. static long
  585. fw_device_op_ioctl(struct file *file,
  586. unsigned int cmd, unsigned long arg)
  587. {
  588. struct client *client = file->private_data;
  589. return dispatch_ioctl(client, cmd, (void __user *) arg);
  590. }
  591. #ifdef CONFIG_COMPAT
  592. static long
  593. fw_device_op_compat_ioctl(struct file *file,
  594. unsigned int cmd, unsigned long arg)
  595. {
  596. struct client *client = file->private_data;
  597. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  598. }
  599. #endif
  600. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  601. {
  602. struct client *client = file->private_data;
  603. enum dma_data_direction direction;
  604. unsigned long size;
  605. int page_count, retval;
  606. /* FIXME: We could support multiple buffers, but we don't. */
  607. if (client->buffer.pages != NULL)
  608. return -EBUSY;
  609. if (!(vma->vm_flags & VM_SHARED))
  610. return -EINVAL;
  611. if (vma->vm_start & ~PAGE_MASK)
  612. return -EINVAL;
  613. client->vm_start = vma->vm_start;
  614. size = vma->vm_end - vma->vm_start;
  615. page_count = size >> PAGE_SHIFT;
  616. if (size & ~PAGE_MASK)
  617. return -EINVAL;
  618. if (vma->vm_flags & VM_WRITE)
  619. direction = DMA_TO_DEVICE;
  620. else
  621. direction = DMA_FROM_DEVICE;
  622. retval = fw_iso_buffer_init(&client->buffer, client->device->card,
  623. page_count, direction);
  624. if (retval < 0)
  625. return retval;
  626. retval = fw_iso_buffer_map(&client->buffer, vma);
  627. if (retval < 0)
  628. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  629. return retval;
  630. }
  631. static int fw_device_op_release(struct inode *inode, struct file *file)
  632. {
  633. struct client *client = file->private_data;
  634. struct address_handler *h, *next_h;
  635. struct request *r, *next_r;
  636. struct event *e, *next_e;
  637. struct response *t, *next_t;
  638. unsigned long flags;
  639. if (client->buffer.pages)
  640. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  641. if (client->iso_context)
  642. fw_iso_context_destroy(client->iso_context);
  643. list_for_each_entry_safe(h, next_h, &client->handler_list, link) {
  644. fw_core_remove_address_handler(&h->handler);
  645. kfree(h);
  646. }
  647. list_for_each_entry_safe(r, next_r, &client->request_list, link) {
  648. fw_send_response(client->device->card, r->request,
  649. RCODE_CONFLICT_ERROR);
  650. kfree(r);
  651. }
  652. list_for_each_entry_safe(t, next_t, &client->transaction_list, link)
  653. fw_cancel_transaction(client->device->card, &t->transaction);
  654. /* FIXME: We should wait for the async tasklets to stop
  655. * running before freeing the memory. */
  656. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  657. kfree(e);
  658. spin_lock_irqsave(&client->device->card->lock, flags);
  659. list_del(&client->link);
  660. spin_unlock_irqrestore(&client->device->card->lock, flags);
  661. fw_device_put(client->device);
  662. kfree(client);
  663. return 0;
  664. }
  665. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  666. {
  667. struct client *client = file->private_data;
  668. unsigned int mask = 0;
  669. poll_wait(file, &client->wait, pt);
  670. if (fw_device_is_shutdown(client->device))
  671. mask |= POLLHUP | POLLERR;
  672. if (!list_empty(&client->event_list))
  673. mask |= POLLIN | POLLRDNORM;
  674. return mask;
  675. }
  676. const struct file_operations fw_device_ops = {
  677. .owner = THIS_MODULE,
  678. .open = fw_device_op_open,
  679. .read = fw_device_op_read,
  680. .unlocked_ioctl = fw_device_op_ioctl,
  681. .poll = fw_device_op_poll,
  682. .release = fw_device_op_release,
  683. .mmap = fw_device_op_mmap,
  684. #ifdef CONFIG_COMPAT
  685. .compat_ioctl = fw_device_op_compat_ioctl,
  686. #endif
  687. };