core-cdev.c 44 KB

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