fw-cdev.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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/mutex.h>
  27. #include <linux/poll.h>
  28. #include <linux/preempt.h>
  29. #include <linux/time.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/delay.h>
  32. #include <linux/mm.h>
  33. #include <linux/idr.h>
  34. #include <linux/compat.h>
  35. #include <linux/firewire-cdev.h>
  36. #include <asm/system.h>
  37. #include <asm/uaccess.h>
  38. #include "fw-transaction.h"
  39. #include "fw-topology.h"
  40. #include "fw-device.h"
  41. struct client;
  42. struct client_resource;
  43. typedef void (*client_resource_release_fn_t)(struct client *,
  44. struct client_resource *);
  45. struct client_resource {
  46. client_resource_release_fn_t release;
  47. int handle;
  48. };
  49. /*
  50. * dequeue_event() just kfree()'s the event, so the event has to be
  51. * the first field in the struct.
  52. */
  53. struct event {
  54. struct { void *data; size_t size; } v[2];
  55. struct list_head link;
  56. };
  57. struct bus_reset {
  58. struct event event;
  59. struct fw_cdev_event_bus_reset reset;
  60. };
  61. struct response {
  62. struct event event;
  63. struct fw_transaction transaction;
  64. struct client *client;
  65. struct client_resource resource;
  66. struct fw_cdev_event_response response;
  67. };
  68. struct iso_interrupt {
  69. struct event event;
  70. struct fw_cdev_event_iso_interrupt interrupt;
  71. };
  72. struct client {
  73. u32 version;
  74. struct fw_device *device;
  75. spinlock_t lock;
  76. bool in_shutdown;
  77. struct idr resource_idr;
  78. struct list_head event_list;
  79. wait_queue_head_t wait;
  80. u64 bus_reset_closure;
  81. struct fw_iso_context *iso_context;
  82. u64 iso_closure;
  83. struct fw_iso_buffer buffer;
  84. unsigned long vm_start;
  85. struct list_head link;
  86. };
  87. static inline void __user *u64_to_uptr(__u64 value)
  88. {
  89. return (void __user *)(unsigned long)value;
  90. }
  91. static inline __u64 uptr_to_u64(void __user *ptr)
  92. {
  93. return (__u64)(unsigned long)ptr;
  94. }
  95. static int fw_device_op_open(struct inode *inode, struct file *file)
  96. {
  97. struct fw_device *device;
  98. struct client *client;
  99. device = fw_device_get_by_devt(inode->i_rdev);
  100. if (device == NULL)
  101. return -ENODEV;
  102. if (fw_device_is_shutdown(device)) {
  103. fw_device_put(device);
  104. return -ENODEV;
  105. }
  106. client = kzalloc(sizeof(*client), GFP_KERNEL);
  107. if (client == NULL) {
  108. fw_device_put(device);
  109. return -ENOMEM;
  110. }
  111. client->device = device;
  112. spin_lock_init(&client->lock);
  113. idr_init(&client->resource_idr);
  114. INIT_LIST_HEAD(&client->event_list);
  115. init_waitqueue_head(&client->wait);
  116. file->private_data = client;
  117. mutex_lock(&device->client_list_mutex);
  118. list_add_tail(&client->link, &device->client_list);
  119. mutex_unlock(&device->client_list_mutex);
  120. return 0;
  121. }
  122. static void queue_event(struct client *client, struct event *event,
  123. void *data0, size_t size0, void *data1, size_t size1)
  124. {
  125. unsigned long flags;
  126. event->v[0].data = data0;
  127. event->v[0].size = size0;
  128. event->v[1].data = data1;
  129. event->v[1].size = size1;
  130. spin_lock_irqsave(&client->lock, flags);
  131. if (client->in_shutdown)
  132. kfree(event);
  133. else
  134. list_add_tail(&event->link, &client->event_list);
  135. spin_unlock_irqrestore(&client->lock, flags);
  136. wake_up_interruptible(&client->wait);
  137. }
  138. static int dequeue_event(struct client *client,
  139. char __user *buffer, size_t count)
  140. {
  141. unsigned long flags;
  142. struct event *event;
  143. size_t size, total;
  144. int i, ret;
  145. ret = wait_event_interruptible(client->wait,
  146. !list_empty(&client->event_list) ||
  147. fw_device_is_shutdown(client->device));
  148. if (ret < 0)
  149. return ret;
  150. if (list_empty(&client->event_list) &&
  151. fw_device_is_shutdown(client->device))
  152. return -ENODEV;
  153. spin_lock_irqsave(&client->lock, flags);
  154. event = list_first_entry(&client->event_list, struct event, link);
  155. list_del(&event->link);
  156. spin_unlock_irqrestore(&client->lock, flags);
  157. total = 0;
  158. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  159. size = min(event->v[i].size, count - total);
  160. if (copy_to_user(buffer + total, event->v[i].data, size)) {
  161. ret = -EFAULT;
  162. goto out;
  163. }
  164. total += size;
  165. }
  166. ret = total;
  167. out:
  168. kfree(event);
  169. return ret;
  170. }
  171. static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
  172. size_t count, loff_t *offset)
  173. {
  174. struct client *client = file->private_data;
  175. return dequeue_event(client, buffer, count);
  176. }
  177. static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
  178. struct client *client)
  179. {
  180. struct fw_card *card = client->device->card;
  181. unsigned long flags;
  182. spin_lock_irqsave(&card->lock, flags);
  183. event->closure = client->bus_reset_closure;
  184. event->type = FW_CDEV_EVENT_BUS_RESET;
  185. event->generation = client->device->generation;
  186. event->node_id = client->device->node_id;
  187. event->local_node_id = card->local_node->node_id;
  188. event->bm_node_id = 0; /* FIXME: We don't track the BM. */
  189. event->irm_node_id = card->irm_node->node_id;
  190. event->root_node_id = card->root_node->node_id;
  191. spin_unlock_irqrestore(&card->lock, flags);
  192. }
  193. static void for_each_client(struct fw_device *device,
  194. void (*callback)(struct client *client))
  195. {
  196. struct client *c;
  197. mutex_lock(&device->client_list_mutex);
  198. list_for_each_entry(c, &device->client_list, link)
  199. callback(c);
  200. mutex_unlock(&device->client_list_mutex);
  201. }
  202. static void queue_bus_reset_event(struct client *client)
  203. {
  204. struct bus_reset *bus_reset;
  205. bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
  206. if (bus_reset == NULL) {
  207. fw_notify("Out of memory when allocating bus reset event\n");
  208. return;
  209. }
  210. fill_bus_reset_event(&bus_reset->reset, client);
  211. queue_event(client, &bus_reset->event,
  212. &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
  213. }
  214. void fw_device_cdev_update(struct fw_device *device)
  215. {
  216. for_each_client(device, queue_bus_reset_event);
  217. }
  218. static void wake_up_client(struct client *client)
  219. {
  220. wake_up_interruptible(&client->wait);
  221. }
  222. void fw_device_cdev_remove(struct fw_device *device)
  223. {
  224. for_each_client(device, wake_up_client);
  225. }
  226. static int ioctl_get_info(struct client *client, void *buffer)
  227. {
  228. struct fw_cdev_get_info *get_info = buffer;
  229. struct fw_cdev_event_bus_reset bus_reset;
  230. unsigned long ret = 0;
  231. client->version = get_info->version;
  232. get_info->version = FW_CDEV_VERSION;
  233. get_info->card = client->device->card->index;
  234. down_read(&fw_device_rwsem);
  235. if (get_info->rom != 0) {
  236. void __user *uptr = u64_to_uptr(get_info->rom);
  237. size_t want = get_info->rom_length;
  238. size_t have = client->device->config_rom_length * 4;
  239. ret = copy_to_user(uptr, client->device->config_rom,
  240. min(want, have));
  241. }
  242. get_info->rom_length = client->device->config_rom_length * 4;
  243. up_read(&fw_device_rwsem);
  244. if (ret != 0)
  245. return -EFAULT;
  246. client->bus_reset_closure = get_info->bus_reset_closure;
  247. if (get_info->bus_reset != 0) {
  248. void __user *uptr = u64_to_uptr(get_info->bus_reset);
  249. fill_bus_reset_event(&bus_reset, client);
  250. if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
  251. return -EFAULT;
  252. }
  253. return 0;
  254. }
  255. static int add_client_resource(struct client *client,
  256. struct client_resource *resource, gfp_t gfp_mask)
  257. {
  258. unsigned long flags;
  259. int ret;
  260. retry:
  261. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  262. return -ENOMEM;
  263. spin_lock_irqsave(&client->lock, flags);
  264. if (client->in_shutdown)
  265. ret = -ECANCELED;
  266. else
  267. ret = idr_get_new(&client->resource_idr, resource,
  268. &resource->handle);
  269. spin_unlock_irqrestore(&client->lock, flags);
  270. if (ret == -EAGAIN)
  271. goto retry;
  272. return ret < 0 ? ret : 0;
  273. }
  274. static int release_client_resource(struct client *client, u32 handle,
  275. client_resource_release_fn_t release,
  276. struct client_resource **resource)
  277. {
  278. struct client_resource *r;
  279. unsigned long flags;
  280. spin_lock_irqsave(&client->lock, flags);
  281. if (client->in_shutdown)
  282. r = NULL;
  283. else
  284. r = idr_find(&client->resource_idr, handle);
  285. if (r && r->release == release)
  286. idr_remove(&client->resource_idr, handle);
  287. spin_unlock_irqrestore(&client->lock, flags);
  288. if (!(r && r->release == release))
  289. return -EINVAL;
  290. if (resource)
  291. *resource = r;
  292. else
  293. r->release(client, r);
  294. return 0;
  295. }
  296. static void release_transaction(struct client *client,
  297. struct client_resource *resource)
  298. {
  299. struct response *response =
  300. container_of(resource, struct response, resource);
  301. fw_cancel_transaction(client->device->card, &response->transaction);
  302. }
  303. static void complete_transaction(struct fw_card *card, int rcode,
  304. void *payload, size_t length, void *data)
  305. {
  306. struct response *response = data;
  307. struct client *client = response->client;
  308. unsigned long flags;
  309. struct fw_cdev_event_response *r = &response->response;
  310. if (length < r->length)
  311. r->length = length;
  312. if (rcode == RCODE_COMPLETE)
  313. memcpy(r->data, payload, r->length);
  314. spin_lock_irqsave(&client->lock, flags);
  315. /*
  316. * If called while in shutdown, the idr tree must be left untouched.
  317. * The idr handle will be removed later.
  318. */
  319. if (!client->in_shutdown)
  320. idr_remove(&client->resource_idr, response->resource.handle);
  321. spin_unlock_irqrestore(&client->lock, flags);
  322. r->type = FW_CDEV_EVENT_RESPONSE;
  323. r->rcode = rcode;
  324. /*
  325. * In the case that sizeof(*r) doesn't align with the position of the
  326. * data, and the read is short, preserve an extra copy of the data
  327. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  328. * for short reads and some apps depended on it, this is both safe
  329. * and prudent for compatibility.
  330. */
  331. if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
  332. queue_event(client, &response->event, r, sizeof(*r),
  333. r->data, r->length);
  334. else
  335. queue_event(client, &response->event, r, sizeof(*r) + r->length,
  336. NULL, 0);
  337. }
  338. static int ioctl_send_request(struct client *client, void *buffer)
  339. {
  340. struct fw_device *device = client->device;
  341. struct fw_cdev_send_request *request = buffer;
  342. struct response *response;
  343. int ret;
  344. /* What is the biggest size we'll accept, really? */
  345. if (request->length > 4096)
  346. return -EINVAL;
  347. response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
  348. if (response == NULL)
  349. return -ENOMEM;
  350. response->client = client;
  351. response->response.length = request->length;
  352. response->response.closure = request->closure;
  353. if (request->data &&
  354. copy_from_user(response->response.data,
  355. u64_to_uptr(request->data), request->length)) {
  356. ret = -EFAULT;
  357. goto failed;
  358. }
  359. switch (request->tcode) {
  360. case TCODE_WRITE_QUADLET_REQUEST:
  361. case TCODE_WRITE_BLOCK_REQUEST:
  362. case TCODE_READ_QUADLET_REQUEST:
  363. case TCODE_READ_BLOCK_REQUEST:
  364. case TCODE_LOCK_MASK_SWAP:
  365. case TCODE_LOCK_COMPARE_SWAP:
  366. case TCODE_LOCK_FETCH_ADD:
  367. case TCODE_LOCK_LITTLE_ADD:
  368. case TCODE_LOCK_BOUNDED_ADD:
  369. case TCODE_LOCK_WRAP_ADD:
  370. case TCODE_LOCK_VENDOR_DEPENDENT:
  371. break;
  372. default:
  373. ret = -EINVAL;
  374. goto failed;
  375. }
  376. response->resource.release = release_transaction;
  377. ret = add_client_resource(client, &response->resource, GFP_KERNEL);
  378. if (ret < 0)
  379. goto failed;
  380. fw_send_request(device->card, &response->transaction,
  381. request->tcode & 0x1f,
  382. device->node->node_id,
  383. request->generation,
  384. device->max_speed,
  385. request->offset,
  386. response->response.data, request->length,
  387. complete_transaction, response);
  388. if (request->data)
  389. return sizeof(request) + request->length;
  390. else
  391. return sizeof(request);
  392. failed:
  393. kfree(response);
  394. return ret;
  395. }
  396. struct address_handler {
  397. struct fw_address_handler handler;
  398. __u64 closure;
  399. struct client *client;
  400. struct client_resource resource;
  401. };
  402. struct request {
  403. struct fw_request *request;
  404. void *data;
  405. size_t length;
  406. struct client_resource resource;
  407. };
  408. struct request_event {
  409. struct event event;
  410. struct fw_cdev_event_request request;
  411. };
  412. static void release_request(struct client *client,
  413. struct client_resource *resource)
  414. {
  415. struct request *request =
  416. container_of(resource, struct request, resource);
  417. fw_send_response(client->device->card, request->request,
  418. RCODE_CONFLICT_ERROR);
  419. kfree(request);
  420. }
  421. static void handle_request(struct fw_card *card, struct fw_request *r,
  422. int tcode, int destination, int source,
  423. int generation, int speed,
  424. unsigned long long offset,
  425. void *payload, size_t length, void *callback_data)
  426. {
  427. struct address_handler *handler = callback_data;
  428. struct request *request;
  429. struct request_event *e;
  430. struct client *client = handler->client;
  431. int ret;
  432. request = kmalloc(sizeof(*request), GFP_ATOMIC);
  433. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  434. if (request == NULL || e == NULL)
  435. goto failed;
  436. request->request = r;
  437. request->data = payload;
  438. request->length = length;
  439. request->resource.release = release_request;
  440. ret = add_client_resource(client, &request->resource, GFP_ATOMIC);
  441. if (ret < 0)
  442. goto failed;
  443. e->request.type = FW_CDEV_EVENT_REQUEST;
  444. e->request.tcode = tcode;
  445. e->request.offset = offset;
  446. e->request.length = length;
  447. e->request.handle = request->resource.handle;
  448. e->request.closure = handler->closure;
  449. queue_event(client, &e->event,
  450. &e->request, sizeof(e->request), payload, length);
  451. return;
  452. failed:
  453. kfree(request);
  454. kfree(e);
  455. fw_send_response(card, r, RCODE_CONFLICT_ERROR);
  456. }
  457. static void release_address_handler(struct client *client,
  458. struct client_resource *resource)
  459. {
  460. struct address_handler *handler =
  461. container_of(resource, struct address_handler, resource);
  462. fw_core_remove_address_handler(&handler->handler);
  463. kfree(handler);
  464. }
  465. static int ioctl_allocate(struct client *client, void *buffer)
  466. {
  467. struct fw_cdev_allocate *request = buffer;
  468. struct address_handler *handler;
  469. struct fw_address_region region;
  470. int ret;
  471. handler = kmalloc(sizeof(*handler), GFP_KERNEL);
  472. if (handler == NULL)
  473. return -ENOMEM;
  474. region.start = request->offset;
  475. region.end = request->offset + request->length;
  476. handler->handler.length = request->length;
  477. handler->handler.address_callback = handle_request;
  478. handler->handler.callback_data = handler;
  479. handler->closure = request->closure;
  480. handler->client = client;
  481. ret = fw_core_add_address_handler(&handler->handler, &region);
  482. if (ret < 0) {
  483. kfree(handler);
  484. return ret;
  485. }
  486. handler->resource.release = release_address_handler;
  487. ret = add_client_resource(client, &handler->resource, GFP_KERNEL);
  488. if (ret < 0) {
  489. release_address_handler(client, &handler->resource);
  490. return ret;
  491. }
  492. request->handle = handler->resource.handle;
  493. return 0;
  494. }
  495. static int ioctl_deallocate(struct client *client, void *buffer)
  496. {
  497. struct fw_cdev_deallocate *request = buffer;
  498. return release_client_resource(client, request->handle,
  499. release_address_handler, NULL);
  500. }
  501. static int ioctl_send_response(struct client *client, void *buffer)
  502. {
  503. struct fw_cdev_send_response *request = buffer;
  504. struct client_resource *resource;
  505. struct request *r;
  506. if (release_client_resource(client, request->handle,
  507. release_request, &resource) < 0)
  508. return -EINVAL;
  509. r = container_of(resource, struct request, resource);
  510. if (request->length < r->length)
  511. r->length = request->length;
  512. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
  513. return -EFAULT;
  514. fw_send_response(client->device->card, r->request, request->rcode);
  515. kfree(r);
  516. return 0;
  517. }
  518. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  519. {
  520. struct fw_cdev_initiate_bus_reset *request = buffer;
  521. int short_reset;
  522. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  523. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  524. }
  525. struct descriptor {
  526. struct fw_descriptor d;
  527. struct client_resource resource;
  528. u32 data[0];
  529. };
  530. static void release_descriptor(struct client *client,
  531. struct client_resource *resource)
  532. {
  533. struct descriptor *descriptor =
  534. container_of(resource, struct descriptor, resource);
  535. fw_core_remove_descriptor(&descriptor->d);
  536. kfree(descriptor);
  537. }
  538. static int ioctl_add_descriptor(struct client *client, void *buffer)
  539. {
  540. struct fw_cdev_add_descriptor *request = buffer;
  541. struct descriptor *descriptor;
  542. int ret;
  543. if (request->length > 256)
  544. return -EINVAL;
  545. descriptor =
  546. kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
  547. if (descriptor == NULL)
  548. return -ENOMEM;
  549. if (copy_from_user(descriptor->data,
  550. u64_to_uptr(request->data), request->length * 4)) {
  551. ret = -EFAULT;
  552. goto failed;
  553. }
  554. descriptor->d.length = request->length;
  555. descriptor->d.immediate = request->immediate;
  556. descriptor->d.key = request->key;
  557. descriptor->d.data = descriptor->data;
  558. ret = fw_core_add_descriptor(&descriptor->d);
  559. if (ret < 0)
  560. goto failed;
  561. descriptor->resource.release = release_descriptor;
  562. ret = add_client_resource(client, &descriptor->resource, GFP_KERNEL);
  563. if (ret < 0) {
  564. fw_core_remove_descriptor(&descriptor->d);
  565. goto failed;
  566. }
  567. request->handle = descriptor->resource.handle;
  568. return 0;
  569. failed:
  570. kfree(descriptor);
  571. return ret;
  572. }
  573. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  574. {
  575. struct fw_cdev_remove_descriptor *request = buffer;
  576. return release_client_resource(client, request->handle,
  577. release_descriptor, NULL);
  578. }
  579. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  580. size_t header_length, void *header, void *data)
  581. {
  582. struct client *client = data;
  583. struct iso_interrupt *irq;
  584. irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
  585. if (irq == NULL)
  586. return;
  587. irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  588. irq->interrupt.closure = client->iso_closure;
  589. irq->interrupt.cycle = cycle;
  590. irq->interrupt.header_length = header_length;
  591. memcpy(irq->interrupt.header, header, header_length);
  592. queue_event(client, &irq->event, &irq->interrupt,
  593. sizeof(irq->interrupt) + header_length, NULL, 0);
  594. }
  595. static int ioctl_create_iso_context(struct client *client, void *buffer)
  596. {
  597. struct fw_cdev_create_iso_context *request = buffer;
  598. struct fw_iso_context *context;
  599. /* We only support one context at this time. */
  600. if (client->iso_context != NULL)
  601. return -EBUSY;
  602. if (request->channel > 63)
  603. return -EINVAL;
  604. switch (request->type) {
  605. case FW_ISO_CONTEXT_RECEIVE:
  606. if (request->header_size < 4 || (request->header_size & 3))
  607. return -EINVAL;
  608. break;
  609. case FW_ISO_CONTEXT_TRANSMIT:
  610. if (request->speed > SCODE_3200)
  611. return -EINVAL;
  612. break;
  613. default:
  614. return -EINVAL;
  615. }
  616. context = fw_iso_context_create(client->device->card,
  617. request->type,
  618. request->channel,
  619. request->speed,
  620. request->header_size,
  621. iso_callback, client);
  622. if (IS_ERR(context))
  623. return PTR_ERR(context);
  624. client->iso_closure = request->closure;
  625. client->iso_context = context;
  626. /* We only support one context at this time. */
  627. request->handle = 0;
  628. return 0;
  629. }
  630. /* Macros for decoding the iso packet control header. */
  631. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  632. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  633. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  634. #define GET_TAG(v) (((v) >> 18) & 0x03)
  635. #define GET_SY(v) (((v) >> 20) & 0x0f)
  636. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  637. static int ioctl_queue_iso(struct client *client, void *buffer)
  638. {
  639. struct fw_cdev_queue_iso *request = buffer;
  640. struct fw_cdev_iso_packet __user *p, *end, *next;
  641. struct fw_iso_context *ctx = client->iso_context;
  642. unsigned long payload, buffer_end, header_length;
  643. u32 control;
  644. int count;
  645. struct {
  646. struct fw_iso_packet packet;
  647. u8 header[256];
  648. } u;
  649. if (ctx == NULL || request->handle != 0)
  650. return -EINVAL;
  651. /*
  652. * If the user passes a non-NULL data pointer, has mmap()'ed
  653. * the iso buffer, and the pointer points inside the buffer,
  654. * we setup the payload pointers accordingly. Otherwise we
  655. * set them both to 0, which will still let packets with
  656. * payload_length == 0 through. In other words, if no packets
  657. * use the indirect payload, the iso buffer need not be mapped
  658. * and the request->data pointer is ignored.
  659. */
  660. payload = (unsigned long)request->data - client->vm_start;
  661. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  662. if (request->data == 0 || client->buffer.pages == NULL ||
  663. payload >= buffer_end) {
  664. payload = 0;
  665. buffer_end = 0;
  666. }
  667. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  668. if (!access_ok(VERIFY_READ, p, request->size))
  669. return -EFAULT;
  670. end = (void __user *)p + request->size;
  671. count = 0;
  672. while (p < end) {
  673. if (get_user(control, &p->control))
  674. return -EFAULT;
  675. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  676. u.packet.interrupt = GET_INTERRUPT(control);
  677. u.packet.skip = GET_SKIP(control);
  678. u.packet.tag = GET_TAG(control);
  679. u.packet.sy = GET_SY(control);
  680. u.packet.header_length = GET_HEADER_LENGTH(control);
  681. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  682. header_length = u.packet.header_length;
  683. } else {
  684. /*
  685. * We require that header_length is a multiple of
  686. * the fixed header size, ctx->header_size.
  687. */
  688. if (ctx->header_size == 0) {
  689. if (u.packet.header_length > 0)
  690. return -EINVAL;
  691. } else if (u.packet.header_length % ctx->header_size != 0) {
  692. return -EINVAL;
  693. }
  694. header_length = 0;
  695. }
  696. next = (struct fw_cdev_iso_packet __user *)
  697. &p->header[header_length / 4];
  698. if (next > end)
  699. return -EINVAL;
  700. if (__copy_from_user
  701. (u.packet.header, p->header, header_length))
  702. return -EFAULT;
  703. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  704. u.packet.header_length + u.packet.payload_length > 0)
  705. return -EINVAL;
  706. if (payload + u.packet.payload_length > buffer_end)
  707. return -EINVAL;
  708. if (fw_iso_context_queue(ctx, &u.packet,
  709. &client->buffer, payload))
  710. break;
  711. p = next;
  712. payload += u.packet.payload_length;
  713. count++;
  714. }
  715. request->size -= uptr_to_u64(p) - request->packets;
  716. request->packets = uptr_to_u64(p);
  717. request->data = client->vm_start + payload;
  718. return count;
  719. }
  720. static int ioctl_start_iso(struct client *client, void *buffer)
  721. {
  722. struct fw_cdev_start_iso *request = buffer;
  723. if (client->iso_context == NULL || request->handle != 0)
  724. return -EINVAL;
  725. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  726. if (request->tags == 0 || request->tags > 15)
  727. return -EINVAL;
  728. if (request->sync > 15)
  729. return -EINVAL;
  730. }
  731. return fw_iso_context_start(client->iso_context, request->cycle,
  732. request->sync, request->tags);
  733. }
  734. static int ioctl_stop_iso(struct client *client, void *buffer)
  735. {
  736. struct fw_cdev_stop_iso *request = buffer;
  737. if (client->iso_context == NULL || request->handle != 0)
  738. return -EINVAL;
  739. return fw_iso_context_stop(client->iso_context);
  740. }
  741. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  742. {
  743. struct fw_cdev_get_cycle_timer *request = buffer;
  744. struct fw_card *card = client->device->card;
  745. unsigned long long bus_time;
  746. struct timeval tv;
  747. unsigned long flags;
  748. preempt_disable();
  749. local_irq_save(flags);
  750. bus_time = card->driver->get_bus_time(card);
  751. do_gettimeofday(&tv);
  752. local_irq_restore(flags);
  753. preempt_enable();
  754. request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
  755. request->cycle_timer = bus_time & 0xffffffff;
  756. return 0;
  757. }
  758. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  759. ioctl_get_info,
  760. ioctl_send_request,
  761. ioctl_allocate,
  762. ioctl_deallocate,
  763. ioctl_send_response,
  764. ioctl_initiate_bus_reset,
  765. ioctl_add_descriptor,
  766. ioctl_remove_descriptor,
  767. ioctl_create_iso_context,
  768. ioctl_queue_iso,
  769. ioctl_start_iso,
  770. ioctl_stop_iso,
  771. ioctl_get_cycle_timer,
  772. };
  773. static int dispatch_ioctl(struct client *client,
  774. unsigned int cmd, void __user *arg)
  775. {
  776. char buffer[256];
  777. int ret;
  778. if (_IOC_TYPE(cmd) != '#' ||
  779. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  780. return -EINVAL;
  781. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  782. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  783. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  784. return -EFAULT;
  785. }
  786. ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  787. if (ret < 0)
  788. return ret;
  789. if (_IOC_DIR(cmd) & _IOC_READ) {
  790. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  791. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  792. return -EFAULT;
  793. }
  794. return ret;
  795. }
  796. static long fw_device_op_ioctl(struct file *file,
  797. unsigned int cmd, unsigned long arg)
  798. {
  799. struct client *client = file->private_data;
  800. if (fw_device_is_shutdown(client->device))
  801. return -ENODEV;
  802. return dispatch_ioctl(client, cmd, (void __user *) arg);
  803. }
  804. #ifdef CONFIG_COMPAT
  805. static long fw_device_op_compat_ioctl(struct file *file,
  806. unsigned int cmd, unsigned long arg)
  807. {
  808. struct client *client = file->private_data;
  809. if (fw_device_is_shutdown(client->device))
  810. return -ENODEV;
  811. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  812. }
  813. #endif
  814. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  815. {
  816. struct client *client = file->private_data;
  817. enum dma_data_direction direction;
  818. unsigned long size;
  819. int page_count, ret;
  820. if (fw_device_is_shutdown(client->device))
  821. return -ENODEV;
  822. /* FIXME: We could support multiple buffers, but we don't. */
  823. if (client->buffer.pages != NULL)
  824. return -EBUSY;
  825. if (!(vma->vm_flags & VM_SHARED))
  826. return -EINVAL;
  827. if (vma->vm_start & ~PAGE_MASK)
  828. return -EINVAL;
  829. client->vm_start = vma->vm_start;
  830. size = vma->vm_end - vma->vm_start;
  831. page_count = size >> PAGE_SHIFT;
  832. if (size & ~PAGE_MASK)
  833. return -EINVAL;
  834. if (vma->vm_flags & VM_WRITE)
  835. direction = DMA_TO_DEVICE;
  836. else
  837. direction = DMA_FROM_DEVICE;
  838. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  839. page_count, direction);
  840. if (ret < 0)
  841. return ret;
  842. ret = fw_iso_buffer_map(&client->buffer, vma);
  843. if (ret < 0)
  844. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  845. return ret;
  846. }
  847. static int shutdown_resource(int id, void *p, void *data)
  848. {
  849. struct client_resource *r = p;
  850. struct client *client = data;
  851. r->release(client, r);
  852. return 0;
  853. }
  854. static int fw_device_op_release(struct inode *inode, struct file *file)
  855. {
  856. struct client *client = file->private_data;
  857. struct event *e, *next_e;
  858. unsigned long flags;
  859. mutex_lock(&client->device->client_list_mutex);
  860. list_del(&client->link);
  861. mutex_unlock(&client->device->client_list_mutex);
  862. if (client->buffer.pages)
  863. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  864. if (client->iso_context)
  865. fw_iso_context_destroy(client->iso_context);
  866. /* Freeze client->resource_idr and client->event_list */
  867. spin_lock_irqsave(&client->lock, flags);
  868. client->in_shutdown = true;
  869. spin_unlock_irqrestore(&client->lock, flags);
  870. idr_for_each(&client->resource_idr, shutdown_resource, client);
  871. idr_remove_all(&client->resource_idr);
  872. idr_destroy(&client->resource_idr);
  873. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  874. kfree(e);
  875. /*
  876. * FIXME: client should be reference-counted. It's extremely unlikely
  877. * but there may still be transactions being completed at this point.
  878. */
  879. fw_device_put(client->device);
  880. kfree(client);
  881. return 0;
  882. }
  883. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  884. {
  885. struct client *client = file->private_data;
  886. unsigned int mask = 0;
  887. poll_wait(file, &client->wait, pt);
  888. if (fw_device_is_shutdown(client->device))
  889. mask |= POLLHUP | POLLERR;
  890. if (!list_empty(&client->event_list))
  891. mask |= POLLIN | POLLRDNORM;
  892. return mask;
  893. }
  894. const struct file_operations fw_device_ops = {
  895. .owner = THIS_MODULE,
  896. .open = fw_device_op_open,
  897. .read = fw_device_op_read,
  898. .unlocked_ioctl = fw_device_op_ioctl,
  899. .poll = fw_device_op_poll,
  900. .release = fw_device_op_release,
  901. .mmap = fw_device_op_mmap,
  902. #ifdef CONFIG_COMPAT
  903. .compat_ioctl = fw_device_op_compat_ioctl,
  904. #endif
  905. };