core-cdev.c 45 KB

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