fw-device-cdev.c 24 KB

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