core-cdev.c 46 KB

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