core-cdev.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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/bug.h>
  21. #include <linux/compat.h>
  22. #include <linux/delay.h>
  23. #include <linux/device.h>
  24. #include <linux/errno.h>
  25. #include <linux/firewire.h>
  26. #include <linux/firewire-cdev.h>
  27. #include <linux/idr.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kref.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/mutex.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/string.h>
  39. #include <linux/time.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/vmalloc.h>
  42. #include <linux/wait.h>
  43. #include <linux/workqueue.h>
  44. #include <asm/system.h>
  45. #include "core.h"
  46. /*
  47. * ABI version history is documented in linux/firewire-cdev.h.
  48. */
  49. #define FW_CDEV_KERNEL_VERSION 4
  50. #define FW_CDEV_VERSION_EVENT_REQUEST2 4
  51. struct client {
  52. u32 version;
  53. struct fw_device *device;
  54. spinlock_t lock;
  55. bool in_shutdown;
  56. struct idr resource_idr;
  57. struct list_head event_list;
  58. wait_queue_head_t wait;
  59. u64 bus_reset_closure;
  60. struct fw_iso_context *iso_context;
  61. u64 iso_closure;
  62. struct fw_iso_buffer buffer;
  63. unsigned long vm_start;
  64. struct list_head link;
  65. struct kref kref;
  66. };
  67. static inline void client_get(struct client *client)
  68. {
  69. kref_get(&client->kref);
  70. }
  71. static void client_release(struct kref *kref)
  72. {
  73. struct client *client = container_of(kref, struct client, kref);
  74. fw_device_put(client->device);
  75. kfree(client);
  76. }
  77. static void client_put(struct client *client)
  78. {
  79. kref_put(&client->kref, client_release);
  80. }
  81. struct client_resource;
  82. typedef void (*client_resource_release_fn_t)(struct client *,
  83. struct client_resource *);
  84. struct client_resource {
  85. client_resource_release_fn_t release;
  86. int handle;
  87. };
  88. struct address_handler_resource {
  89. struct client_resource resource;
  90. struct fw_address_handler handler;
  91. __u64 closure;
  92. struct client *client;
  93. };
  94. struct outbound_transaction_resource {
  95. struct client_resource resource;
  96. struct fw_transaction transaction;
  97. };
  98. struct inbound_transaction_resource {
  99. struct client_resource resource;
  100. struct fw_card *card;
  101. struct fw_request *request;
  102. void *data;
  103. size_t length;
  104. };
  105. struct descriptor_resource {
  106. struct client_resource resource;
  107. struct fw_descriptor descriptor;
  108. u32 data[0];
  109. };
  110. struct iso_resource {
  111. struct client_resource resource;
  112. struct client *client;
  113. /* Schedule work and access todo only with client->lock held. */
  114. struct delayed_work work;
  115. enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
  116. ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
  117. int generation;
  118. u64 channels;
  119. s32 bandwidth;
  120. __be32 transaction_data[2];
  121. struct iso_resource_event *e_alloc, *e_dealloc;
  122. };
  123. static void release_iso_resource(struct client *, struct client_resource *);
  124. static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
  125. {
  126. client_get(r->client);
  127. if (!schedule_delayed_work(&r->work, delay))
  128. client_put(r->client);
  129. }
  130. static void schedule_if_iso_resource(struct client_resource *resource)
  131. {
  132. if (resource->release == release_iso_resource)
  133. schedule_iso_resource(container_of(resource,
  134. struct iso_resource, resource), 0);
  135. }
  136. /*
  137. * dequeue_event() just kfree()'s the event, so the event has to be
  138. * the first field in a struct XYZ_event.
  139. */
  140. struct event {
  141. struct { void *data; size_t size; } v[2];
  142. struct list_head link;
  143. };
  144. struct bus_reset_event {
  145. struct event event;
  146. struct fw_cdev_event_bus_reset reset;
  147. };
  148. struct outbound_transaction_event {
  149. struct event event;
  150. struct client *client;
  151. struct outbound_transaction_resource r;
  152. struct fw_cdev_event_response response;
  153. };
  154. struct inbound_transaction_event {
  155. struct event event;
  156. union {
  157. struct fw_cdev_event_request request;
  158. struct fw_cdev_event_request2 request2;
  159. } req;
  160. };
  161. struct iso_interrupt_event {
  162. struct event event;
  163. struct fw_cdev_event_iso_interrupt interrupt;
  164. };
  165. struct iso_resource_event {
  166. struct event event;
  167. struct fw_cdev_event_iso_resource iso_resource;
  168. };
  169. static inline void __user *u64_to_uptr(__u64 value)
  170. {
  171. return (void __user *)(unsigned long)value;
  172. }
  173. static inline __u64 uptr_to_u64(void __user *ptr)
  174. {
  175. return (__u64)(unsigned long)ptr;
  176. }
  177. static int fw_device_op_open(struct inode *inode, struct file *file)
  178. {
  179. struct fw_device *device;
  180. struct client *client;
  181. device = fw_device_get_by_devt(inode->i_rdev);
  182. if (device == NULL)
  183. return -ENODEV;
  184. if (fw_device_is_shutdown(device)) {
  185. fw_device_put(device);
  186. return -ENODEV;
  187. }
  188. client = kzalloc(sizeof(*client), GFP_KERNEL);
  189. if (client == NULL) {
  190. fw_device_put(device);
  191. return -ENOMEM;
  192. }
  193. client->device = device;
  194. spin_lock_init(&client->lock);
  195. idr_init(&client->resource_idr);
  196. INIT_LIST_HEAD(&client->event_list);
  197. init_waitqueue_head(&client->wait);
  198. kref_init(&client->kref);
  199. file->private_data = client;
  200. mutex_lock(&device->client_list_mutex);
  201. list_add_tail(&client->link, &device->client_list);
  202. mutex_unlock(&device->client_list_mutex);
  203. return nonseekable_open(inode, file);
  204. }
  205. static void queue_event(struct client *client, struct event *event,
  206. void *data0, size_t size0, void *data1, size_t size1)
  207. {
  208. unsigned long flags;
  209. event->v[0].data = data0;
  210. event->v[0].size = size0;
  211. event->v[1].data = data1;
  212. event->v[1].size = size1;
  213. spin_lock_irqsave(&client->lock, flags);
  214. if (client->in_shutdown)
  215. kfree(event);
  216. else
  217. list_add_tail(&event->link, &client->event_list);
  218. spin_unlock_irqrestore(&client->lock, flags);
  219. wake_up_interruptible(&client->wait);
  220. }
  221. static int dequeue_event(struct client *client,
  222. char __user *buffer, size_t count)
  223. {
  224. struct event *event;
  225. size_t size, total;
  226. int i, ret;
  227. ret = wait_event_interruptible(client->wait,
  228. !list_empty(&client->event_list) ||
  229. fw_device_is_shutdown(client->device));
  230. if (ret < 0)
  231. return ret;
  232. if (list_empty(&client->event_list) &&
  233. fw_device_is_shutdown(client->device))
  234. return -ENODEV;
  235. spin_lock_irq(&client->lock);
  236. event = list_first_entry(&client->event_list, struct event, link);
  237. list_del(&event->link);
  238. spin_unlock_irq(&client->lock);
  239. total = 0;
  240. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  241. size = min(event->v[i].size, count - total);
  242. if (copy_to_user(buffer + total, event->v[i].data, size)) {
  243. ret = -EFAULT;
  244. goto out;
  245. }
  246. total += size;
  247. }
  248. ret = total;
  249. out:
  250. kfree(event);
  251. return ret;
  252. }
  253. static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
  254. size_t count, loff_t *offset)
  255. {
  256. struct client *client = file->private_data;
  257. return dequeue_event(client, buffer, count);
  258. }
  259. static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
  260. struct client *client)
  261. {
  262. struct fw_card *card = client->device->card;
  263. spin_lock_irq(&card->lock);
  264. event->closure = client->bus_reset_closure;
  265. event->type = FW_CDEV_EVENT_BUS_RESET;
  266. event->generation = client->device->generation;
  267. event->node_id = client->device->node_id;
  268. event->local_node_id = card->local_node->node_id;
  269. event->bm_node_id = card->bm_node_id;
  270. event->irm_node_id = card->irm_node->node_id;
  271. event->root_node_id = card->root_node->node_id;
  272. spin_unlock_irq(&card->lock);
  273. }
  274. static void for_each_client(struct fw_device *device,
  275. void (*callback)(struct client *client))
  276. {
  277. struct client *c;
  278. mutex_lock(&device->client_list_mutex);
  279. list_for_each_entry(c, &device->client_list, link)
  280. callback(c);
  281. mutex_unlock(&device->client_list_mutex);
  282. }
  283. static int schedule_reallocations(int id, void *p, void *data)
  284. {
  285. schedule_if_iso_resource(p);
  286. return 0;
  287. }
  288. static void queue_bus_reset_event(struct client *client)
  289. {
  290. struct bus_reset_event *e;
  291. e = kzalloc(sizeof(*e), GFP_KERNEL);
  292. if (e == NULL) {
  293. fw_notify("Out of memory when allocating bus reset event\n");
  294. return;
  295. }
  296. fill_bus_reset_event(&e->reset, client);
  297. queue_event(client, &e->event,
  298. &e->reset, sizeof(e->reset), NULL, 0);
  299. spin_lock_irq(&client->lock);
  300. idr_for_each(&client->resource_idr, schedule_reallocations, client);
  301. spin_unlock_irq(&client->lock);
  302. }
  303. void fw_device_cdev_update(struct fw_device *device)
  304. {
  305. for_each_client(device, queue_bus_reset_event);
  306. }
  307. static void wake_up_client(struct client *client)
  308. {
  309. wake_up_interruptible(&client->wait);
  310. }
  311. void fw_device_cdev_remove(struct fw_device *device)
  312. {
  313. for_each_client(device, wake_up_client);
  314. }
  315. union ioctl_arg {
  316. struct fw_cdev_get_info get_info;
  317. struct fw_cdev_send_request send_request;
  318. struct fw_cdev_allocate allocate;
  319. struct fw_cdev_deallocate deallocate;
  320. struct fw_cdev_send_response send_response;
  321. struct fw_cdev_initiate_bus_reset initiate_bus_reset;
  322. struct fw_cdev_add_descriptor add_descriptor;
  323. struct fw_cdev_remove_descriptor remove_descriptor;
  324. struct fw_cdev_create_iso_context create_iso_context;
  325. struct fw_cdev_queue_iso queue_iso;
  326. struct fw_cdev_start_iso start_iso;
  327. struct fw_cdev_stop_iso stop_iso;
  328. struct fw_cdev_get_cycle_timer get_cycle_timer;
  329. struct fw_cdev_allocate_iso_resource allocate_iso_resource;
  330. struct fw_cdev_send_stream_packet send_stream_packet;
  331. struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
  332. };
  333. static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
  334. {
  335. struct fw_cdev_get_info *a = &arg->get_info;
  336. struct fw_cdev_event_bus_reset bus_reset;
  337. unsigned long ret = 0;
  338. client->version = a->version;
  339. a->version = FW_CDEV_KERNEL_VERSION;
  340. a->card = client->device->card->index;
  341. down_read(&fw_device_rwsem);
  342. if (a->rom != 0) {
  343. size_t want = a->rom_length;
  344. size_t have = client->device->config_rom_length * 4;
  345. ret = copy_to_user(u64_to_uptr(a->rom),
  346. client->device->config_rom, min(want, have));
  347. }
  348. a->rom_length = client->device->config_rom_length * 4;
  349. up_read(&fw_device_rwsem);
  350. if (ret != 0)
  351. return -EFAULT;
  352. client->bus_reset_closure = a->bus_reset_closure;
  353. if (a->bus_reset != 0) {
  354. fill_bus_reset_event(&bus_reset, client);
  355. if (copy_to_user(u64_to_uptr(a->bus_reset),
  356. &bus_reset, sizeof(bus_reset)))
  357. return -EFAULT;
  358. }
  359. return 0;
  360. }
  361. static int add_client_resource(struct client *client,
  362. struct client_resource *resource, gfp_t gfp_mask)
  363. {
  364. unsigned long flags;
  365. int ret;
  366. retry:
  367. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  368. return -ENOMEM;
  369. spin_lock_irqsave(&client->lock, flags);
  370. if (client->in_shutdown)
  371. ret = -ECANCELED;
  372. else
  373. ret = idr_get_new(&client->resource_idr, resource,
  374. &resource->handle);
  375. if (ret >= 0) {
  376. client_get(client);
  377. schedule_if_iso_resource(resource);
  378. }
  379. spin_unlock_irqrestore(&client->lock, flags);
  380. if (ret == -EAGAIN)
  381. goto retry;
  382. return ret < 0 ? ret : 0;
  383. }
  384. static int release_client_resource(struct client *client, u32 handle,
  385. client_resource_release_fn_t release,
  386. struct client_resource **return_resource)
  387. {
  388. struct client_resource *resource;
  389. spin_lock_irq(&client->lock);
  390. if (client->in_shutdown)
  391. resource = NULL;
  392. else
  393. resource = idr_find(&client->resource_idr, handle);
  394. if (resource && resource->release == release)
  395. idr_remove(&client->resource_idr, handle);
  396. spin_unlock_irq(&client->lock);
  397. if (!(resource && resource->release == release))
  398. return -EINVAL;
  399. if (return_resource)
  400. *return_resource = resource;
  401. else
  402. resource->release(client, resource);
  403. client_put(client);
  404. return 0;
  405. }
  406. static void release_transaction(struct client *client,
  407. struct client_resource *resource)
  408. {
  409. struct outbound_transaction_resource *r = container_of(resource,
  410. struct outbound_transaction_resource, resource);
  411. fw_cancel_transaction(client->device->card, &r->transaction);
  412. }
  413. static void complete_transaction(struct fw_card *card, int rcode,
  414. void *payload, size_t length, void *data)
  415. {
  416. struct outbound_transaction_event *e = data;
  417. struct fw_cdev_event_response *rsp = &e->response;
  418. struct client *client = e->client;
  419. unsigned long flags;
  420. if (length < rsp->length)
  421. rsp->length = length;
  422. if (rcode == RCODE_COMPLETE)
  423. memcpy(rsp->data, payload, rsp->length);
  424. spin_lock_irqsave(&client->lock, flags);
  425. /*
  426. * 1. If called while in shutdown, the idr tree must be left untouched.
  427. * The idr handle will be removed and the client reference will be
  428. * dropped later.
  429. * 2. If the call chain was release_client_resource ->
  430. * release_transaction -> complete_transaction (instead of a normal
  431. * conclusion of the transaction), i.e. if this resource was already
  432. * unregistered from the idr, the client reference will be dropped
  433. * by release_client_resource and we must not drop it here.
  434. */
  435. if (!client->in_shutdown &&
  436. idr_find(&client->resource_idr, e->r.resource.handle)) {
  437. idr_remove(&client->resource_idr, e->r.resource.handle);
  438. /* Drop the idr's reference */
  439. client_put(client);
  440. }
  441. spin_unlock_irqrestore(&client->lock, flags);
  442. rsp->type = FW_CDEV_EVENT_RESPONSE;
  443. rsp->rcode = rcode;
  444. /*
  445. * In the case that sizeof(*rsp) doesn't align with the position of the
  446. * data, and the read is short, preserve an extra copy of the data
  447. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  448. * for short reads and some apps depended on it, this is both safe
  449. * and prudent for compatibility.
  450. */
  451. if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
  452. queue_event(client, &e->event, rsp, sizeof(*rsp),
  453. rsp->data, rsp->length);
  454. else
  455. queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
  456. NULL, 0);
  457. /* Drop the transaction callback's reference */
  458. client_put(client);
  459. }
  460. static int init_request(struct client *client,
  461. struct fw_cdev_send_request *request,
  462. int destination_id, int speed)
  463. {
  464. struct outbound_transaction_event *e;
  465. int ret;
  466. if (request->tcode != TCODE_STREAM_DATA &&
  467. (request->length > 4096 || request->length > 512 << speed))
  468. return -EIO;
  469. if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
  470. request->length < 4)
  471. return -EINVAL;
  472. e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
  473. if (e == NULL)
  474. return -ENOMEM;
  475. e->client = client;
  476. e->response.length = request->length;
  477. e->response.closure = request->closure;
  478. if (request->data &&
  479. copy_from_user(e->response.data,
  480. u64_to_uptr(request->data), request->length)) {
  481. ret = -EFAULT;
  482. goto failed;
  483. }
  484. e->r.resource.release = release_transaction;
  485. ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
  486. if (ret < 0)
  487. goto failed;
  488. /* Get a reference for the transaction callback */
  489. client_get(client);
  490. fw_send_request(client->device->card, &e->r.transaction,
  491. request->tcode, destination_id, request->generation,
  492. speed, request->offset, e->response.data,
  493. request->length, complete_transaction, e);
  494. return 0;
  495. failed:
  496. kfree(e);
  497. return ret;
  498. }
  499. static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
  500. {
  501. switch (arg->send_request.tcode) {
  502. case TCODE_WRITE_QUADLET_REQUEST:
  503. case TCODE_WRITE_BLOCK_REQUEST:
  504. case TCODE_READ_QUADLET_REQUEST:
  505. case TCODE_READ_BLOCK_REQUEST:
  506. case TCODE_LOCK_MASK_SWAP:
  507. case TCODE_LOCK_COMPARE_SWAP:
  508. case TCODE_LOCK_FETCH_ADD:
  509. case TCODE_LOCK_LITTLE_ADD:
  510. case TCODE_LOCK_BOUNDED_ADD:
  511. case TCODE_LOCK_WRAP_ADD:
  512. case TCODE_LOCK_VENDOR_DEPENDENT:
  513. break;
  514. default:
  515. return -EINVAL;
  516. }
  517. return init_request(client, &arg->send_request, client->device->node_id,
  518. client->device->max_speed);
  519. }
  520. static inline bool is_fcp_request(struct fw_request *request)
  521. {
  522. return request == NULL;
  523. }
  524. static void release_request(struct client *client,
  525. struct client_resource *resource)
  526. {
  527. struct inbound_transaction_resource *r = container_of(resource,
  528. struct inbound_transaction_resource, resource);
  529. if (is_fcp_request(r->request))
  530. kfree(r->data);
  531. else
  532. fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
  533. fw_card_put(r->card);
  534. kfree(r);
  535. }
  536. static void handle_request(struct fw_card *card, struct fw_request *request,
  537. int tcode, int destination, int source,
  538. int generation, unsigned long long offset,
  539. void *payload, size_t length, void *callback_data)
  540. {
  541. struct address_handler_resource *handler = callback_data;
  542. struct inbound_transaction_resource *r;
  543. struct inbound_transaction_event *e;
  544. size_t event_size0;
  545. void *fcp_frame = NULL;
  546. int ret;
  547. /* card may be different from handler->client->device->card */
  548. fw_card_get(card);
  549. r = kmalloc(sizeof(*r), GFP_ATOMIC);
  550. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  551. if (r == NULL || e == NULL)
  552. goto failed;
  553. r->card = card;
  554. r->request = request;
  555. r->data = payload;
  556. r->length = length;
  557. if (is_fcp_request(request)) {
  558. /*
  559. * FIXME: Let core-transaction.c manage a
  560. * single reference-counted copy?
  561. */
  562. fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
  563. if (fcp_frame == NULL)
  564. goto failed;
  565. r->data = fcp_frame;
  566. }
  567. r->resource.release = release_request;
  568. ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
  569. if (ret < 0)
  570. goto failed;
  571. if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
  572. struct fw_cdev_event_request *req = &e->req.request;
  573. if (tcode & 0x10)
  574. tcode = TCODE_LOCK_REQUEST;
  575. req->type = FW_CDEV_EVENT_REQUEST;
  576. req->tcode = tcode;
  577. req->offset = offset;
  578. req->length = length;
  579. req->handle = r->resource.handle;
  580. req->closure = handler->closure;
  581. event_size0 = sizeof(*req);
  582. } else {
  583. struct fw_cdev_event_request2 *req = &e->req.request2;
  584. req->type = FW_CDEV_EVENT_REQUEST2;
  585. req->tcode = tcode;
  586. req->offset = offset;
  587. req->source_node_id = source;
  588. req->destination_node_id = destination;
  589. req->card = card->index;
  590. req->generation = generation;
  591. req->length = length;
  592. req->handle = r->resource.handle;
  593. req->closure = handler->closure;
  594. event_size0 = sizeof(*req);
  595. }
  596. queue_event(handler->client, &e->event,
  597. &e->req, event_size0, r->data, length);
  598. return;
  599. failed:
  600. kfree(r);
  601. kfree(e);
  602. kfree(fcp_frame);
  603. if (!is_fcp_request(request))
  604. fw_send_response(card, request, RCODE_CONFLICT_ERROR);
  605. fw_card_put(card);
  606. }
  607. static void release_address_handler(struct client *client,
  608. struct client_resource *resource)
  609. {
  610. struct address_handler_resource *r =
  611. container_of(resource, struct address_handler_resource, resource);
  612. fw_core_remove_address_handler(&r->handler);
  613. kfree(r);
  614. }
  615. static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
  616. {
  617. struct fw_cdev_allocate *a = &arg->allocate;
  618. struct address_handler_resource *r;
  619. struct fw_address_region region;
  620. int ret;
  621. r = kmalloc(sizeof(*r), GFP_KERNEL);
  622. if (r == NULL)
  623. return -ENOMEM;
  624. region.start = a->offset;
  625. region.end = a->offset + a->length;
  626. r->handler.length = a->length;
  627. r->handler.address_callback = handle_request;
  628. r->handler.callback_data = r;
  629. r->closure = a->closure;
  630. r->client = client;
  631. ret = fw_core_add_address_handler(&r->handler, &region);
  632. if (ret < 0) {
  633. kfree(r);
  634. return ret;
  635. }
  636. r->resource.release = release_address_handler;
  637. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  638. if (ret < 0) {
  639. release_address_handler(client, &r->resource);
  640. return ret;
  641. }
  642. a->handle = r->resource.handle;
  643. return 0;
  644. }
  645. static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
  646. {
  647. return release_client_resource(client, arg->deallocate.handle,
  648. release_address_handler, NULL);
  649. }
  650. static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
  651. {
  652. struct fw_cdev_send_response *a = &arg->send_response;
  653. struct client_resource *resource;
  654. struct inbound_transaction_resource *r;
  655. int ret = 0;
  656. if (release_client_resource(client, a->handle,
  657. release_request, &resource) < 0)
  658. return -EINVAL;
  659. r = container_of(resource, struct inbound_transaction_resource,
  660. resource);
  661. if (is_fcp_request(r->request))
  662. goto out;
  663. if (a->length != fw_get_response_length(r->request)) {
  664. ret = -EINVAL;
  665. kfree(r->request);
  666. goto out;
  667. }
  668. if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
  669. ret = -EFAULT;
  670. kfree(r->request);
  671. goto out;
  672. }
  673. fw_send_response(r->card, r->request, a->rcode);
  674. out:
  675. fw_card_put(r->card);
  676. kfree(r);
  677. return ret;
  678. }
  679. static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
  680. {
  681. return fw_core_initiate_bus_reset(client->device->card,
  682. arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
  683. }
  684. static void release_descriptor(struct client *client,
  685. struct client_resource *resource)
  686. {
  687. struct descriptor_resource *r =
  688. container_of(resource, struct descriptor_resource, resource);
  689. fw_core_remove_descriptor(&r->descriptor);
  690. kfree(r);
  691. }
  692. static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
  693. {
  694. struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
  695. struct descriptor_resource *r;
  696. int ret;
  697. /* Access policy: Allow this ioctl only on local nodes' device files. */
  698. if (!client->device->is_local)
  699. return -ENOSYS;
  700. if (a->length > 256)
  701. return -EINVAL;
  702. r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
  703. if (r == NULL)
  704. return -ENOMEM;
  705. if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
  706. ret = -EFAULT;
  707. goto failed;
  708. }
  709. r->descriptor.length = a->length;
  710. r->descriptor.immediate = a->immediate;
  711. r->descriptor.key = a->key;
  712. r->descriptor.data = r->data;
  713. ret = fw_core_add_descriptor(&r->descriptor);
  714. if (ret < 0)
  715. goto failed;
  716. r->resource.release = release_descriptor;
  717. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  718. if (ret < 0) {
  719. fw_core_remove_descriptor(&r->descriptor);
  720. goto failed;
  721. }
  722. a->handle = r->resource.handle;
  723. return 0;
  724. failed:
  725. kfree(r);
  726. return ret;
  727. }
  728. static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
  729. {
  730. return release_client_resource(client, arg->remove_descriptor.handle,
  731. release_descriptor, NULL);
  732. }
  733. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  734. size_t header_length, void *header, void *data)
  735. {
  736. struct client *client = data;
  737. struct iso_interrupt_event *e;
  738. e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
  739. if (e == NULL)
  740. return;
  741. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  742. e->interrupt.closure = client->iso_closure;
  743. e->interrupt.cycle = cycle;
  744. e->interrupt.header_length = header_length;
  745. memcpy(e->interrupt.header, header, header_length);
  746. queue_event(client, &e->event, &e->interrupt,
  747. sizeof(e->interrupt) + header_length, NULL, 0);
  748. }
  749. static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
  750. {
  751. struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
  752. struct fw_iso_context *context;
  753. BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
  754. FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE);
  755. if (a->channel > 63)
  756. return -EINVAL;
  757. switch (a->type) {
  758. case FW_ISO_CONTEXT_RECEIVE:
  759. if (a->header_size < 4 || (a->header_size & 3))
  760. return -EINVAL;
  761. break;
  762. case FW_ISO_CONTEXT_TRANSMIT:
  763. if (a->speed > SCODE_3200)
  764. return -EINVAL;
  765. break;
  766. default:
  767. return -EINVAL;
  768. }
  769. context = fw_iso_context_create(client->device->card, a->type,
  770. a->channel, a->speed, a->header_size,
  771. iso_callback, client);
  772. if (IS_ERR(context))
  773. return PTR_ERR(context);
  774. /* We only support one context at this time. */
  775. spin_lock_irq(&client->lock);
  776. if (client->iso_context != NULL) {
  777. spin_unlock_irq(&client->lock);
  778. fw_iso_context_destroy(context);
  779. return -EBUSY;
  780. }
  781. client->iso_closure = a->closure;
  782. client->iso_context = context;
  783. spin_unlock_irq(&client->lock);
  784. a->handle = 0;
  785. return 0;
  786. }
  787. /* Macros for decoding the iso packet control header. */
  788. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  789. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  790. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  791. #define GET_TAG(v) (((v) >> 18) & 0x03)
  792. #define GET_SY(v) (((v) >> 20) & 0x0f)
  793. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  794. static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
  795. {
  796. struct fw_cdev_queue_iso *a = &arg->queue_iso;
  797. struct fw_cdev_iso_packet __user *p, *end, *next;
  798. struct fw_iso_context *ctx = client->iso_context;
  799. unsigned long payload, buffer_end, header_length;
  800. u32 control;
  801. int count;
  802. struct {
  803. struct fw_iso_packet packet;
  804. u8 header[256];
  805. } u;
  806. if (ctx == NULL || a->handle != 0)
  807. return -EINVAL;
  808. /*
  809. * If the user passes a non-NULL data pointer, has mmap()'ed
  810. * the iso buffer, and the pointer points inside the buffer,
  811. * we setup the payload pointers accordingly. Otherwise we
  812. * set them both to 0, which will still let packets with
  813. * payload_length == 0 through. In other words, if no packets
  814. * use the indirect payload, the iso buffer need not be mapped
  815. * and the a->data pointer is ignored.
  816. */
  817. payload = (unsigned long)a->data - client->vm_start;
  818. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  819. if (a->data == 0 || client->buffer.pages == NULL ||
  820. payload >= buffer_end) {
  821. payload = 0;
  822. buffer_end = 0;
  823. }
  824. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
  825. if (!access_ok(VERIFY_READ, p, a->size))
  826. return -EFAULT;
  827. end = (void __user *)p + a->size;
  828. count = 0;
  829. while (p < end) {
  830. if (get_user(control, &p->control))
  831. return -EFAULT;
  832. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  833. u.packet.interrupt = GET_INTERRUPT(control);
  834. u.packet.skip = GET_SKIP(control);
  835. u.packet.tag = GET_TAG(control);
  836. u.packet.sy = GET_SY(control);
  837. u.packet.header_length = GET_HEADER_LENGTH(control);
  838. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  839. if (u.packet.header_length % 4 != 0)
  840. return -EINVAL;
  841. header_length = u.packet.header_length;
  842. } else {
  843. /*
  844. * We require that header_length is a multiple of
  845. * the fixed header size, ctx->header_size.
  846. */
  847. if (ctx->header_size == 0) {
  848. if (u.packet.header_length > 0)
  849. return -EINVAL;
  850. } else if (u.packet.header_length == 0 ||
  851. u.packet.header_length % ctx->header_size != 0) {
  852. return -EINVAL;
  853. }
  854. header_length = 0;
  855. }
  856. next = (struct fw_cdev_iso_packet __user *)
  857. &p->header[header_length / 4];
  858. if (next > end)
  859. return -EINVAL;
  860. if (__copy_from_user
  861. (u.packet.header, p->header, header_length))
  862. return -EFAULT;
  863. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  864. u.packet.header_length + u.packet.payload_length > 0)
  865. return -EINVAL;
  866. if (payload + u.packet.payload_length > buffer_end)
  867. return -EINVAL;
  868. if (fw_iso_context_queue(ctx, &u.packet,
  869. &client->buffer, payload))
  870. break;
  871. p = next;
  872. payload += u.packet.payload_length;
  873. count++;
  874. }
  875. a->size -= uptr_to_u64(p) - a->packets;
  876. a->packets = uptr_to_u64(p);
  877. a->data = client->vm_start + payload;
  878. return count;
  879. }
  880. static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
  881. {
  882. struct fw_cdev_start_iso *a = &arg->start_iso;
  883. BUILD_BUG_ON(
  884. FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
  885. FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
  886. FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
  887. FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
  888. FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
  889. if (client->iso_context == NULL || a->handle != 0)
  890. return -EINVAL;
  891. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
  892. (a->tags == 0 || a->tags > 15 || a->sync > 15))
  893. return -EINVAL;
  894. return fw_iso_context_start(client->iso_context,
  895. a->cycle, a->sync, a->tags);
  896. }
  897. static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
  898. {
  899. struct fw_cdev_stop_iso *a = &arg->stop_iso;
  900. if (client->iso_context == NULL || a->handle != 0)
  901. return -EINVAL;
  902. return fw_iso_context_stop(client->iso_context);
  903. }
  904. static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
  905. {
  906. struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
  907. struct fw_card *card = client->device->card;
  908. struct timespec ts = {0, 0};
  909. u32 cycle_time;
  910. int ret = 0;
  911. local_irq_disable();
  912. cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
  913. switch (a->clk_id) {
  914. case CLOCK_REALTIME: getnstimeofday(&ts); break;
  915. case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
  916. case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
  917. default:
  918. ret = -EINVAL;
  919. }
  920. local_irq_enable();
  921. a->tv_sec = ts.tv_sec;
  922. a->tv_nsec = ts.tv_nsec;
  923. a->cycle_timer = cycle_time;
  924. return ret;
  925. }
  926. static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
  927. {
  928. struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
  929. struct fw_cdev_get_cycle_timer2 ct2;
  930. ct2.clk_id = CLOCK_REALTIME;
  931. ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
  932. a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
  933. a->cycle_timer = ct2.cycle_timer;
  934. return 0;
  935. }
  936. static void iso_resource_work(struct work_struct *work)
  937. {
  938. struct iso_resource_event *e;
  939. struct iso_resource *r =
  940. container_of(work, struct iso_resource, work.work);
  941. struct client *client = r->client;
  942. int generation, channel, bandwidth, todo;
  943. bool skip, free, success;
  944. spin_lock_irq(&client->lock);
  945. generation = client->device->generation;
  946. todo = r->todo;
  947. /* Allow 1000ms grace period for other reallocations. */
  948. if (todo == ISO_RES_ALLOC &&
  949. time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
  950. schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
  951. skip = true;
  952. } else {
  953. /* We could be called twice within the same generation. */
  954. skip = todo == ISO_RES_REALLOC &&
  955. r->generation == generation;
  956. }
  957. free = todo == ISO_RES_DEALLOC ||
  958. todo == ISO_RES_ALLOC_ONCE ||
  959. todo == ISO_RES_DEALLOC_ONCE;
  960. r->generation = generation;
  961. spin_unlock_irq(&client->lock);
  962. if (skip)
  963. goto out;
  964. bandwidth = r->bandwidth;
  965. fw_iso_resource_manage(client->device->card, generation,
  966. r->channels, &channel, &bandwidth,
  967. todo == ISO_RES_ALLOC ||
  968. todo == ISO_RES_REALLOC ||
  969. todo == ISO_RES_ALLOC_ONCE,
  970. r->transaction_data);
  971. /*
  972. * Is this generation outdated already? As long as this resource sticks
  973. * in the idr, it will be scheduled again for a newer generation or at
  974. * shutdown.
  975. */
  976. if (channel == -EAGAIN &&
  977. (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
  978. goto out;
  979. success = channel >= 0 || bandwidth > 0;
  980. spin_lock_irq(&client->lock);
  981. /*
  982. * Transit from allocation to reallocation, except if the client
  983. * requested deallocation in the meantime.
  984. */
  985. if (r->todo == ISO_RES_ALLOC)
  986. r->todo = ISO_RES_REALLOC;
  987. /*
  988. * Allocation or reallocation failure? Pull this resource out of the
  989. * idr and prepare for deletion, unless the client is shutting down.
  990. */
  991. if (r->todo == ISO_RES_REALLOC && !success &&
  992. !client->in_shutdown &&
  993. idr_find(&client->resource_idr, r->resource.handle)) {
  994. idr_remove(&client->resource_idr, r->resource.handle);
  995. client_put(client);
  996. free = true;
  997. }
  998. spin_unlock_irq(&client->lock);
  999. if (todo == ISO_RES_ALLOC && channel >= 0)
  1000. r->channels = 1ULL << channel;
  1001. if (todo == ISO_RES_REALLOC && success)
  1002. goto out;
  1003. if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
  1004. e = r->e_alloc;
  1005. r->e_alloc = NULL;
  1006. } else {
  1007. e = r->e_dealloc;
  1008. r->e_dealloc = NULL;
  1009. }
  1010. e->iso_resource.handle = r->resource.handle;
  1011. e->iso_resource.channel = channel;
  1012. e->iso_resource.bandwidth = bandwidth;
  1013. queue_event(client, &e->event,
  1014. &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
  1015. if (free) {
  1016. cancel_delayed_work(&r->work);
  1017. kfree(r->e_alloc);
  1018. kfree(r->e_dealloc);
  1019. kfree(r);
  1020. }
  1021. out:
  1022. client_put(client);
  1023. }
  1024. static void release_iso_resource(struct client *client,
  1025. struct client_resource *resource)
  1026. {
  1027. struct iso_resource *r =
  1028. container_of(resource, struct iso_resource, resource);
  1029. spin_lock_irq(&client->lock);
  1030. r->todo = ISO_RES_DEALLOC;
  1031. schedule_iso_resource(r, 0);
  1032. spin_unlock_irq(&client->lock);
  1033. }
  1034. static int init_iso_resource(struct client *client,
  1035. struct fw_cdev_allocate_iso_resource *request, int todo)
  1036. {
  1037. struct iso_resource_event *e1, *e2;
  1038. struct iso_resource *r;
  1039. int ret;
  1040. if ((request->channels == 0 && request->bandwidth == 0) ||
  1041. request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
  1042. request->bandwidth < 0)
  1043. return -EINVAL;
  1044. r = kmalloc(sizeof(*r), GFP_KERNEL);
  1045. e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
  1046. e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
  1047. if (r == NULL || e1 == NULL || e2 == NULL) {
  1048. ret = -ENOMEM;
  1049. goto fail;
  1050. }
  1051. INIT_DELAYED_WORK(&r->work, iso_resource_work);
  1052. r->client = client;
  1053. r->todo = todo;
  1054. r->generation = -1;
  1055. r->channels = request->channels;
  1056. r->bandwidth = request->bandwidth;
  1057. r->e_alloc = e1;
  1058. r->e_dealloc = e2;
  1059. e1->iso_resource.closure = request->closure;
  1060. e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
  1061. e2->iso_resource.closure = request->closure;
  1062. e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
  1063. if (todo == ISO_RES_ALLOC) {
  1064. r->resource.release = release_iso_resource;
  1065. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  1066. if (ret < 0)
  1067. goto fail;
  1068. } else {
  1069. r->resource.release = NULL;
  1070. r->resource.handle = -1;
  1071. schedule_iso_resource(r, 0);
  1072. }
  1073. request->handle = r->resource.handle;
  1074. return 0;
  1075. fail:
  1076. kfree(r);
  1077. kfree(e1);
  1078. kfree(e2);
  1079. return ret;
  1080. }
  1081. static int ioctl_allocate_iso_resource(struct client *client,
  1082. union ioctl_arg *arg)
  1083. {
  1084. return init_iso_resource(client,
  1085. &arg->allocate_iso_resource, ISO_RES_ALLOC);
  1086. }
  1087. static int ioctl_deallocate_iso_resource(struct client *client,
  1088. union ioctl_arg *arg)
  1089. {
  1090. return release_client_resource(client,
  1091. arg->deallocate.handle, release_iso_resource, NULL);
  1092. }
  1093. static int ioctl_allocate_iso_resource_once(struct client *client,
  1094. union ioctl_arg *arg)
  1095. {
  1096. return init_iso_resource(client,
  1097. &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
  1098. }
  1099. static int ioctl_deallocate_iso_resource_once(struct client *client,
  1100. union ioctl_arg *arg)
  1101. {
  1102. return init_iso_resource(client,
  1103. &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
  1104. }
  1105. /*
  1106. * Returns a speed code: Maximum speed to or from this device,
  1107. * limited by the device's link speed, the local node's link speed,
  1108. * and all PHY port speeds between the two links.
  1109. */
  1110. static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
  1111. {
  1112. return client->device->max_speed;
  1113. }
  1114. static int ioctl_send_broadcast_request(struct client *client,
  1115. union ioctl_arg *arg)
  1116. {
  1117. struct fw_cdev_send_request *a = &arg->send_request;
  1118. switch (a->tcode) {
  1119. case TCODE_WRITE_QUADLET_REQUEST:
  1120. case TCODE_WRITE_BLOCK_REQUEST:
  1121. break;
  1122. default:
  1123. return -EINVAL;
  1124. }
  1125. /* Security policy: Only allow accesses to Units Space. */
  1126. if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
  1127. return -EACCES;
  1128. return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
  1129. }
  1130. static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
  1131. {
  1132. struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
  1133. struct fw_cdev_send_request request;
  1134. int dest;
  1135. if (a->speed > client->device->card->link_speed ||
  1136. a->length > 1024 << a->speed)
  1137. return -EIO;
  1138. if (a->tag > 3 || a->channel > 63 || a->sy > 15)
  1139. return -EINVAL;
  1140. dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
  1141. request.tcode = TCODE_STREAM_DATA;
  1142. request.length = a->length;
  1143. request.closure = a->closure;
  1144. request.data = a->data;
  1145. request.generation = a->generation;
  1146. return init_request(client, &request, dest, a->speed);
  1147. }
  1148. static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
  1149. ioctl_get_info,
  1150. ioctl_send_request,
  1151. ioctl_allocate,
  1152. ioctl_deallocate,
  1153. ioctl_send_response,
  1154. ioctl_initiate_bus_reset,
  1155. ioctl_add_descriptor,
  1156. ioctl_remove_descriptor,
  1157. ioctl_create_iso_context,
  1158. ioctl_queue_iso,
  1159. ioctl_start_iso,
  1160. ioctl_stop_iso,
  1161. ioctl_get_cycle_timer,
  1162. ioctl_allocate_iso_resource,
  1163. ioctl_deallocate_iso_resource,
  1164. ioctl_allocate_iso_resource_once,
  1165. ioctl_deallocate_iso_resource_once,
  1166. ioctl_get_speed,
  1167. ioctl_send_broadcast_request,
  1168. ioctl_send_stream_packet,
  1169. ioctl_get_cycle_timer2,
  1170. };
  1171. static int dispatch_ioctl(struct client *client,
  1172. unsigned int cmd, void __user *arg)
  1173. {
  1174. union ioctl_arg buffer;
  1175. int ret;
  1176. if (fw_device_is_shutdown(client->device))
  1177. return -ENODEV;
  1178. if (_IOC_TYPE(cmd) != '#' ||
  1179. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
  1180. _IOC_SIZE(cmd) > sizeof(buffer))
  1181. return -EINVAL;
  1182. if (_IOC_DIR(cmd) == _IOC_READ)
  1183. memset(&buffer, 0, _IOC_SIZE(cmd));
  1184. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1185. if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
  1186. return -EFAULT;
  1187. ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
  1188. if (ret < 0)
  1189. return ret;
  1190. if (_IOC_DIR(cmd) & _IOC_READ)
  1191. if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
  1192. return -EFAULT;
  1193. return ret;
  1194. }
  1195. static long fw_device_op_ioctl(struct file *file,
  1196. unsigned int cmd, unsigned long arg)
  1197. {
  1198. return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
  1199. }
  1200. #ifdef CONFIG_COMPAT
  1201. static long fw_device_op_compat_ioctl(struct file *file,
  1202. unsigned int cmd, unsigned long arg)
  1203. {
  1204. return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
  1205. }
  1206. #endif
  1207. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  1208. {
  1209. struct client *client = file->private_data;
  1210. enum dma_data_direction direction;
  1211. unsigned long size;
  1212. int page_count, ret;
  1213. if (fw_device_is_shutdown(client->device))
  1214. return -ENODEV;
  1215. /* FIXME: We could support multiple buffers, but we don't. */
  1216. if (client->buffer.pages != NULL)
  1217. return -EBUSY;
  1218. if (!(vma->vm_flags & VM_SHARED))
  1219. return -EINVAL;
  1220. if (vma->vm_start & ~PAGE_MASK)
  1221. return -EINVAL;
  1222. client->vm_start = vma->vm_start;
  1223. size = vma->vm_end - vma->vm_start;
  1224. page_count = size >> PAGE_SHIFT;
  1225. if (size & ~PAGE_MASK)
  1226. return -EINVAL;
  1227. if (vma->vm_flags & VM_WRITE)
  1228. direction = DMA_TO_DEVICE;
  1229. else
  1230. direction = DMA_FROM_DEVICE;
  1231. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  1232. page_count, direction);
  1233. if (ret < 0)
  1234. return ret;
  1235. ret = fw_iso_buffer_map(&client->buffer, vma);
  1236. if (ret < 0)
  1237. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1238. return ret;
  1239. }
  1240. static int shutdown_resource(int id, void *p, void *data)
  1241. {
  1242. struct client_resource *resource = p;
  1243. struct client *client = data;
  1244. resource->release(client, resource);
  1245. client_put(client);
  1246. return 0;
  1247. }
  1248. static int fw_device_op_release(struct inode *inode, struct file *file)
  1249. {
  1250. struct client *client = file->private_data;
  1251. struct event *event, *next_event;
  1252. mutex_lock(&client->device->client_list_mutex);
  1253. list_del(&client->link);
  1254. mutex_unlock(&client->device->client_list_mutex);
  1255. if (client->iso_context)
  1256. fw_iso_context_destroy(client->iso_context);
  1257. if (client->buffer.pages)
  1258. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1259. /* Freeze client->resource_idr and client->event_list */
  1260. spin_lock_irq(&client->lock);
  1261. client->in_shutdown = true;
  1262. spin_unlock_irq(&client->lock);
  1263. idr_for_each(&client->resource_idr, shutdown_resource, client);
  1264. idr_remove_all(&client->resource_idr);
  1265. idr_destroy(&client->resource_idr);
  1266. list_for_each_entry_safe(event, next_event, &client->event_list, link)
  1267. kfree(event);
  1268. client_put(client);
  1269. return 0;
  1270. }
  1271. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  1272. {
  1273. struct client *client = file->private_data;
  1274. unsigned int mask = 0;
  1275. poll_wait(file, &client->wait, pt);
  1276. if (fw_device_is_shutdown(client->device))
  1277. mask |= POLLHUP | POLLERR;
  1278. if (!list_empty(&client->event_list))
  1279. mask |= POLLIN | POLLRDNORM;
  1280. return mask;
  1281. }
  1282. const struct file_operations fw_device_ops = {
  1283. .owner = THIS_MODULE,
  1284. .llseek = no_llseek,
  1285. .open = fw_device_op_open,
  1286. .read = fw_device_op_read,
  1287. .unlocked_ioctl = fw_device_op_ioctl,
  1288. .mmap = fw_device_op_mmap,
  1289. .release = fw_device_op_release,
  1290. .poll = fw_device_op_poll,
  1291. #ifdef CONFIG_COMPAT
  1292. .compat_ioctl = fw_device_op_compat_ioctl,
  1293. #endif
  1294. };