core-cdev.c 46 KB

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