core-cdev.c 45 KB

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