fw-cdev.c 24 KB

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