core-cdev.c 42 KB

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