core-cdev.c 44 KB

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