core-cdev.c 45 KB

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