fw-device-cdev.c 23 KB

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