core-cdev.c 45 KB

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