fw-cdev.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/wait.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/mutex.h>
  27. #include <linux/poll.h>
  28. #include <linux/preempt.h>
  29. #include <linux/time.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/delay.h>
  32. #include <linux/mm.h>
  33. #include <linux/idr.h>
  34. #include <linux/compat.h>
  35. #include <linux/firewire-cdev.h>
  36. #include <asm/system.h>
  37. #include <asm/uaccess.h>
  38. #include "fw-transaction.h"
  39. #include "fw-topology.h"
  40. #include "fw-device.h"
  41. struct client;
  42. struct client_resource;
  43. typedef void (*client_resource_release_fn_t)(struct client *,
  44. struct client_resource *);
  45. struct client_resource {
  46. client_resource_release_fn_t release;
  47. int handle;
  48. };
  49. /*
  50. * dequeue_event() just kfree()'s the event, so the event has to be
  51. * the first field in the struct.
  52. */
  53. struct event {
  54. struct { void *data; size_t size; } v[2];
  55. struct list_head link;
  56. };
  57. struct bus_reset {
  58. struct event event;
  59. struct fw_cdev_event_bus_reset reset;
  60. };
  61. struct response {
  62. struct event event;
  63. struct fw_transaction transaction;
  64. struct client *client;
  65. struct client_resource resource;
  66. struct fw_cdev_event_response response;
  67. };
  68. struct iso_interrupt {
  69. struct event event;
  70. struct fw_cdev_event_iso_interrupt interrupt;
  71. };
  72. struct client {
  73. u32 version;
  74. struct fw_device *device;
  75. spinlock_t lock;
  76. bool in_shutdown;
  77. struct idr resource_idr;
  78. struct list_head event_list;
  79. wait_queue_head_t wait;
  80. u64 bus_reset_closure;
  81. struct fw_iso_context *iso_context;
  82. u64 iso_closure;
  83. struct fw_iso_buffer buffer;
  84. unsigned long vm_start;
  85. struct list_head link;
  86. };
  87. static inline void __user *
  88. u64_to_uptr(__u64 value)
  89. {
  90. return (void __user *)(unsigned long)value;
  91. }
  92. static inline __u64
  93. uptr_to_u64(void __user *ptr)
  94. {
  95. return (__u64)(unsigned long)ptr;
  96. }
  97. static int fw_device_op_open(struct inode *inode, struct file *file)
  98. {
  99. struct fw_device *device;
  100. struct client *client;
  101. device = fw_device_get_by_devt(inode->i_rdev);
  102. if (device == NULL)
  103. return -ENODEV;
  104. if (fw_device_is_shutdown(device)) {
  105. fw_device_put(device);
  106. return -ENODEV;
  107. }
  108. client = kzalloc(sizeof(*client), GFP_KERNEL);
  109. if (client == NULL) {
  110. fw_device_put(device);
  111. return -ENOMEM;
  112. }
  113. client->device = device;
  114. spin_lock_init(&client->lock);
  115. idr_init(&client->resource_idr);
  116. INIT_LIST_HEAD(&client->event_list);
  117. init_waitqueue_head(&client->wait);
  118. file->private_data = client;
  119. mutex_lock(&device->client_list_mutex);
  120. list_add_tail(&client->link, &device->client_list);
  121. mutex_unlock(&device->client_list_mutex);
  122. return 0;
  123. }
  124. static void queue_event(struct client *client, struct event *event,
  125. void *data0, size_t size0, void *data1, size_t size1)
  126. {
  127. unsigned long flags;
  128. event->v[0].data = data0;
  129. event->v[0].size = size0;
  130. event->v[1].data = data1;
  131. event->v[1].size = size1;
  132. spin_lock_irqsave(&client->lock, flags);
  133. if (client->in_shutdown)
  134. kfree(event);
  135. else
  136. list_add_tail(&event->link, &client->event_list);
  137. spin_unlock_irqrestore(&client->lock, flags);
  138. wake_up_interruptible(&client->wait);
  139. }
  140. static int
  141. dequeue_event(struct client *client, char __user *buffer, size_t count)
  142. {
  143. unsigned long flags;
  144. struct event *event;
  145. size_t size, total;
  146. int i, ret;
  147. ret = wait_event_interruptible(client->wait,
  148. !list_empty(&client->event_list) ||
  149. fw_device_is_shutdown(client->device));
  150. if (ret < 0)
  151. return ret;
  152. if (list_empty(&client->event_list) &&
  153. fw_device_is_shutdown(client->device))
  154. return -ENODEV;
  155. spin_lock_irqsave(&client->lock, flags);
  156. event = container_of(client->event_list.next, struct event, link);
  157. list_del(&event->link);
  158. spin_unlock_irqrestore(&client->lock, flags);
  159. total = 0;
  160. for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
  161. size = min(event->v[i].size, count - total);
  162. if (copy_to_user(buffer + total, event->v[i].data, size)) {
  163. ret = -EFAULT;
  164. goto out;
  165. }
  166. total += size;
  167. }
  168. ret = total;
  169. out:
  170. kfree(event);
  171. return ret;
  172. }
  173. static ssize_t
  174. fw_device_op_read(struct file *file,
  175. char __user *buffer, size_t count, loff_t *offset)
  176. {
  177. struct client *client = file->private_data;
  178. return dequeue_event(client, buffer, count);
  179. }
  180. static void
  181. fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
  182. struct client *client)
  183. {
  184. struct fw_card *card = client->device->card;
  185. unsigned long flags;
  186. spin_lock_irqsave(&card->lock, flags);
  187. event->closure = client->bus_reset_closure;
  188. event->type = FW_CDEV_EVENT_BUS_RESET;
  189. event->generation = client->device->generation;
  190. event->node_id = client->device->node_id;
  191. event->local_node_id = card->local_node->node_id;
  192. event->bm_node_id = 0; /* FIXME: We don't track the BM. */
  193. event->irm_node_id = card->irm_node->node_id;
  194. event->root_node_id = card->root_node->node_id;
  195. spin_unlock_irqrestore(&card->lock, flags);
  196. }
  197. static void
  198. for_each_client(struct fw_device *device,
  199. void (*callback)(struct client *client))
  200. {
  201. struct client *c;
  202. mutex_lock(&device->client_list_mutex);
  203. list_for_each_entry(c, &device->client_list, link)
  204. callback(c);
  205. mutex_unlock(&device->client_list_mutex);
  206. }
  207. static void
  208. queue_bus_reset_event(struct client *client)
  209. {
  210. struct bus_reset *bus_reset;
  211. bus_reset = kzalloc(sizeof(*bus_reset), GFP_KERNEL);
  212. if (bus_reset == NULL) {
  213. fw_notify("Out of memory when allocating bus reset event\n");
  214. return;
  215. }
  216. fill_bus_reset_event(&bus_reset->reset, client);
  217. queue_event(client, &bus_reset->event,
  218. &bus_reset->reset, sizeof(bus_reset->reset), NULL, 0);
  219. }
  220. void fw_device_cdev_update(struct fw_device *device)
  221. {
  222. for_each_client(device, queue_bus_reset_event);
  223. }
  224. static void wake_up_client(struct client *client)
  225. {
  226. wake_up_interruptible(&client->wait);
  227. }
  228. void fw_device_cdev_remove(struct fw_device *device)
  229. {
  230. for_each_client(device, wake_up_client);
  231. }
  232. static int ioctl_get_info(struct client *client, void *buffer)
  233. {
  234. struct fw_cdev_get_info *get_info = buffer;
  235. struct fw_cdev_event_bus_reset bus_reset;
  236. unsigned long ret = 0;
  237. client->version = get_info->version;
  238. get_info->version = FW_CDEV_VERSION;
  239. get_info->card = client->device->card->index;
  240. down_read(&fw_device_rwsem);
  241. if (get_info->rom != 0) {
  242. void __user *uptr = u64_to_uptr(get_info->rom);
  243. size_t want = get_info->rom_length;
  244. size_t have = client->device->config_rom_length * 4;
  245. ret = copy_to_user(uptr, client->device->config_rom,
  246. min(want, have));
  247. }
  248. get_info->rom_length = client->device->config_rom_length * 4;
  249. up_read(&fw_device_rwsem);
  250. if (ret != 0)
  251. return -EFAULT;
  252. client->bus_reset_closure = get_info->bus_reset_closure;
  253. if (get_info->bus_reset != 0) {
  254. void __user *uptr = u64_to_uptr(get_info->bus_reset);
  255. fill_bus_reset_event(&bus_reset, client);
  256. if (copy_to_user(uptr, &bus_reset, sizeof(bus_reset)))
  257. return -EFAULT;
  258. }
  259. return 0;
  260. }
  261. static int
  262. add_client_resource(struct client *client, struct client_resource *resource,
  263. gfp_t gfp_mask)
  264. {
  265. unsigned long flags;
  266. int ret;
  267. retry:
  268. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  269. return -ENOMEM;
  270. spin_lock_irqsave(&client->lock, flags);
  271. if (client->in_shutdown)
  272. ret = -ECANCELED;
  273. else
  274. ret = idr_get_new(&client->resource_idr, resource,
  275. &resource->handle);
  276. spin_unlock_irqrestore(&client->lock, flags);
  277. if (ret == -EAGAIN)
  278. goto retry;
  279. return ret < 0 ? ret : 0;
  280. }
  281. static int
  282. release_client_resource(struct client *client, u32 handle,
  283. client_resource_release_fn_t release,
  284. struct client_resource **resource)
  285. {
  286. struct client_resource *r;
  287. unsigned long flags;
  288. spin_lock_irqsave(&client->lock, flags);
  289. if (client->in_shutdown)
  290. r = NULL;
  291. else
  292. r = idr_find(&client->resource_idr, handle);
  293. if (r && r->release == release)
  294. idr_remove(&client->resource_idr, handle);
  295. spin_unlock_irqrestore(&client->lock, flags);
  296. if (!(r && r->release == release))
  297. return -EINVAL;
  298. if (resource)
  299. *resource = r;
  300. else
  301. r->release(client, r);
  302. return 0;
  303. }
  304. static void
  305. release_transaction(struct client *client, struct client_resource *resource)
  306. {
  307. struct response *response =
  308. container_of(resource, struct response, resource);
  309. fw_cancel_transaction(client->device->card, &response->transaction);
  310. }
  311. static void
  312. complete_transaction(struct fw_card *card, int rcode,
  313. void *payload, size_t length, void *data)
  314. {
  315. struct response *response = data;
  316. struct client *client = response->client;
  317. unsigned long flags;
  318. struct fw_cdev_event_response *r = &response->response;
  319. if (length < r->length)
  320. r->length = length;
  321. if (rcode == RCODE_COMPLETE)
  322. memcpy(r->data, payload, r->length);
  323. spin_lock_irqsave(&client->lock, flags);
  324. /*
  325. * If called while in shutdown, the idr tree must be left untouched.
  326. * The idr handle will be removed later.
  327. */
  328. if (!client->in_shutdown)
  329. idr_remove(&client->resource_idr, response->resource.handle);
  330. spin_unlock_irqrestore(&client->lock, flags);
  331. r->type = FW_CDEV_EVENT_RESPONSE;
  332. r->rcode = rcode;
  333. /*
  334. * In the case that sizeof(*r) doesn't align with the position of the
  335. * data, and the read is short, preserve an extra copy of the data
  336. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  337. * for short reads and some apps depended on it, this is both safe
  338. * and prudent for compatibility.
  339. */
  340. if (r->length <= sizeof(*r) - offsetof(typeof(*r), data))
  341. queue_event(client, &response->event, r, sizeof(*r),
  342. r->data, r->length);
  343. else
  344. queue_event(client, &response->event, r, sizeof(*r) + r->length,
  345. NULL, 0);
  346. }
  347. static int ioctl_send_request(struct client *client, void *buffer)
  348. {
  349. struct fw_device *device = client->device;
  350. struct fw_cdev_send_request *request = buffer;
  351. struct response *response;
  352. int ret;
  353. /* What is the biggest size we'll accept, really? */
  354. if (request->length > 4096)
  355. return -EINVAL;
  356. response = kmalloc(sizeof(*response) + request->length, GFP_KERNEL);
  357. if (response == NULL)
  358. return -ENOMEM;
  359. response->client = client;
  360. response->response.length = request->length;
  361. response->response.closure = request->closure;
  362. if (request->data &&
  363. copy_from_user(response->response.data,
  364. u64_to_uptr(request->data), request->length)) {
  365. ret = -EFAULT;
  366. goto failed;
  367. }
  368. switch (request->tcode) {
  369. case TCODE_WRITE_QUADLET_REQUEST:
  370. case TCODE_WRITE_BLOCK_REQUEST:
  371. case TCODE_READ_QUADLET_REQUEST:
  372. case TCODE_READ_BLOCK_REQUEST:
  373. case TCODE_LOCK_MASK_SWAP:
  374. case TCODE_LOCK_COMPARE_SWAP:
  375. case TCODE_LOCK_FETCH_ADD:
  376. case TCODE_LOCK_LITTLE_ADD:
  377. case TCODE_LOCK_BOUNDED_ADD:
  378. case TCODE_LOCK_WRAP_ADD:
  379. case TCODE_LOCK_VENDOR_DEPENDENT:
  380. break;
  381. default:
  382. ret = -EINVAL;
  383. goto failed;
  384. }
  385. response->resource.release = release_transaction;
  386. ret = add_client_resource(client, &response->resource, GFP_KERNEL);
  387. if (ret < 0)
  388. goto failed;
  389. fw_send_request(device->card, &response->transaction,
  390. request->tcode & 0x1f,
  391. device->node->node_id,
  392. request->generation,
  393. device->max_speed,
  394. request->offset,
  395. response->response.data, request->length,
  396. complete_transaction, response);
  397. if (request->data)
  398. return sizeof(request) + request->length;
  399. else
  400. return sizeof(request);
  401. failed:
  402. kfree(response);
  403. return ret;
  404. }
  405. struct address_handler {
  406. struct fw_address_handler handler;
  407. __u64 closure;
  408. struct client *client;
  409. struct client_resource resource;
  410. };
  411. struct request {
  412. struct fw_request *request;
  413. void *data;
  414. size_t length;
  415. struct client_resource resource;
  416. };
  417. struct request_event {
  418. struct event event;
  419. struct fw_cdev_event_request request;
  420. };
  421. static void
  422. release_request(struct client *client, struct client_resource *resource)
  423. {
  424. struct request *request =
  425. container_of(resource, struct request, resource);
  426. fw_send_response(client->device->card, request->request,
  427. RCODE_CONFLICT_ERROR);
  428. kfree(request);
  429. }
  430. static void
  431. handle_request(struct fw_card *card, struct fw_request *r,
  432. int tcode, int destination, int source,
  433. int generation, int speed,
  434. unsigned long long offset,
  435. void *payload, size_t length, void *callback_data)
  436. {
  437. struct address_handler *handler = callback_data;
  438. struct request *request;
  439. struct request_event *e;
  440. struct client *client = handler->client;
  441. int ret;
  442. request = kmalloc(sizeof(*request), GFP_ATOMIC);
  443. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  444. if (request == NULL || e == NULL)
  445. goto failed;
  446. request->request = r;
  447. request->data = payload;
  448. request->length = length;
  449. request->resource.release = release_request;
  450. ret = add_client_resource(client, &request->resource, GFP_ATOMIC);
  451. if (ret < 0)
  452. goto failed;
  453. e->request.type = FW_CDEV_EVENT_REQUEST;
  454. e->request.tcode = tcode;
  455. e->request.offset = offset;
  456. e->request.length = length;
  457. e->request.handle = request->resource.handle;
  458. e->request.closure = handler->closure;
  459. queue_event(client, &e->event,
  460. &e->request, sizeof(e->request), payload, length);
  461. return;
  462. failed:
  463. kfree(request);
  464. kfree(e);
  465. fw_send_response(card, r, RCODE_CONFLICT_ERROR);
  466. }
  467. static void
  468. release_address_handler(struct client *client,
  469. struct client_resource *resource)
  470. {
  471. struct address_handler *handler =
  472. container_of(resource, struct address_handler, resource);
  473. fw_core_remove_address_handler(&handler->handler);
  474. kfree(handler);
  475. }
  476. static int ioctl_allocate(struct client *client, void *buffer)
  477. {
  478. struct fw_cdev_allocate *request = buffer;
  479. struct address_handler *handler;
  480. struct fw_address_region region;
  481. int ret;
  482. handler = kmalloc(sizeof(*handler), GFP_KERNEL);
  483. if (handler == NULL)
  484. return -ENOMEM;
  485. region.start = request->offset;
  486. region.end = request->offset + request->length;
  487. handler->handler.length = request->length;
  488. handler->handler.address_callback = handle_request;
  489. handler->handler.callback_data = handler;
  490. handler->closure = request->closure;
  491. handler->client = client;
  492. ret = fw_core_add_address_handler(&handler->handler, &region);
  493. if (ret < 0) {
  494. kfree(handler);
  495. return ret;
  496. }
  497. handler->resource.release = release_address_handler;
  498. ret = add_client_resource(client, &handler->resource, GFP_KERNEL);
  499. if (ret < 0) {
  500. release_address_handler(client, &handler->resource);
  501. return ret;
  502. }
  503. request->handle = handler->resource.handle;
  504. return 0;
  505. }
  506. static int ioctl_deallocate(struct client *client, void *buffer)
  507. {
  508. struct fw_cdev_deallocate *request = buffer;
  509. return release_client_resource(client, request->handle,
  510. release_address_handler, NULL);
  511. }
  512. static int ioctl_send_response(struct client *client, void *buffer)
  513. {
  514. struct fw_cdev_send_response *request = buffer;
  515. struct client_resource *resource;
  516. struct request *r;
  517. if (release_client_resource(client, request->handle,
  518. release_request, &resource) < 0)
  519. return -EINVAL;
  520. r = container_of(resource, struct request, resource);
  521. if (request->length < r->length)
  522. r->length = request->length;
  523. if (copy_from_user(r->data, u64_to_uptr(request->data), r->length))
  524. return -EFAULT;
  525. fw_send_response(client->device->card, r->request, request->rcode);
  526. kfree(r);
  527. return 0;
  528. }
  529. static int ioctl_initiate_bus_reset(struct client *client, void *buffer)
  530. {
  531. struct fw_cdev_initiate_bus_reset *request = buffer;
  532. int short_reset;
  533. short_reset = (request->type == FW_CDEV_SHORT_RESET);
  534. return fw_core_initiate_bus_reset(client->device->card, short_reset);
  535. }
  536. struct descriptor {
  537. struct fw_descriptor d;
  538. struct client_resource resource;
  539. u32 data[0];
  540. };
  541. static void release_descriptor(struct client *client,
  542. struct client_resource *resource)
  543. {
  544. struct descriptor *descriptor =
  545. container_of(resource, struct descriptor, resource);
  546. fw_core_remove_descriptor(&descriptor->d);
  547. kfree(descriptor);
  548. }
  549. static int ioctl_add_descriptor(struct client *client, void *buffer)
  550. {
  551. struct fw_cdev_add_descriptor *request = buffer;
  552. struct descriptor *descriptor;
  553. int ret;
  554. if (request->length > 256)
  555. return -EINVAL;
  556. descriptor =
  557. kmalloc(sizeof(*descriptor) + request->length * 4, GFP_KERNEL);
  558. if (descriptor == NULL)
  559. return -ENOMEM;
  560. if (copy_from_user(descriptor->data,
  561. u64_to_uptr(request->data), request->length * 4)) {
  562. ret = -EFAULT;
  563. goto failed;
  564. }
  565. descriptor->d.length = request->length;
  566. descriptor->d.immediate = request->immediate;
  567. descriptor->d.key = request->key;
  568. descriptor->d.data = descriptor->data;
  569. ret = fw_core_add_descriptor(&descriptor->d);
  570. if (ret < 0)
  571. goto failed;
  572. descriptor->resource.release = release_descriptor;
  573. ret = add_client_resource(client, &descriptor->resource, GFP_KERNEL);
  574. if (ret < 0) {
  575. fw_core_remove_descriptor(&descriptor->d);
  576. goto failed;
  577. }
  578. request->handle = descriptor->resource.handle;
  579. return 0;
  580. failed:
  581. kfree(descriptor);
  582. return ret;
  583. }
  584. static int ioctl_remove_descriptor(struct client *client, void *buffer)
  585. {
  586. struct fw_cdev_remove_descriptor *request = buffer;
  587. return release_client_resource(client, request->handle,
  588. release_descriptor, NULL);
  589. }
  590. static void
  591. iso_callback(struct fw_iso_context *context, u32 cycle,
  592. size_t header_length, void *header, void *data)
  593. {
  594. struct client *client = data;
  595. struct iso_interrupt *irq;
  596. irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
  597. if (irq == NULL)
  598. return;
  599. irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  600. irq->interrupt.closure = client->iso_closure;
  601. irq->interrupt.cycle = cycle;
  602. irq->interrupt.header_length = header_length;
  603. memcpy(irq->interrupt.header, header, header_length);
  604. queue_event(client, &irq->event, &irq->interrupt,
  605. sizeof(irq->interrupt) + header_length, NULL, 0);
  606. }
  607. static int ioctl_create_iso_context(struct client *client, void *buffer)
  608. {
  609. struct fw_cdev_create_iso_context *request = buffer;
  610. struct fw_iso_context *context;
  611. /* We only support one context at this time. */
  612. if (client->iso_context != NULL)
  613. return -EBUSY;
  614. if (request->channel > 63)
  615. return -EINVAL;
  616. switch (request->type) {
  617. case FW_ISO_CONTEXT_RECEIVE:
  618. if (request->header_size < 4 || (request->header_size & 3))
  619. return -EINVAL;
  620. break;
  621. case FW_ISO_CONTEXT_TRANSMIT:
  622. if (request->speed > SCODE_3200)
  623. return -EINVAL;
  624. break;
  625. default:
  626. return -EINVAL;
  627. }
  628. context = fw_iso_context_create(client->device->card,
  629. request->type,
  630. request->channel,
  631. request->speed,
  632. request->header_size,
  633. iso_callback, client);
  634. if (IS_ERR(context))
  635. return PTR_ERR(context);
  636. client->iso_closure = request->closure;
  637. client->iso_context = context;
  638. /* We only support one context at this time. */
  639. request->handle = 0;
  640. return 0;
  641. }
  642. /* Macros for decoding the iso packet control header. */
  643. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  644. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  645. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  646. #define GET_TAG(v) (((v) >> 18) & 0x03)
  647. #define GET_SY(v) (((v) >> 20) & 0x0f)
  648. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  649. static int ioctl_queue_iso(struct client *client, void *buffer)
  650. {
  651. struct fw_cdev_queue_iso *request = buffer;
  652. struct fw_cdev_iso_packet __user *p, *end, *next;
  653. struct fw_iso_context *ctx = client->iso_context;
  654. unsigned long payload, buffer_end, header_length;
  655. u32 control;
  656. int count;
  657. struct {
  658. struct fw_iso_packet packet;
  659. u8 header[256];
  660. } u;
  661. if (ctx == NULL || request->handle != 0)
  662. return -EINVAL;
  663. /*
  664. * If the user passes a non-NULL data pointer, has mmap()'ed
  665. * the iso buffer, and the pointer points inside the buffer,
  666. * we setup the payload pointers accordingly. Otherwise we
  667. * set them both to 0, which will still let packets with
  668. * payload_length == 0 through. In other words, if no packets
  669. * use the indirect payload, the iso buffer need not be mapped
  670. * and the request->data pointer is ignored.
  671. */
  672. payload = (unsigned long)request->data - client->vm_start;
  673. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  674. if (request->data == 0 || client->buffer.pages == NULL ||
  675. payload >= buffer_end) {
  676. payload = 0;
  677. buffer_end = 0;
  678. }
  679. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(request->packets);
  680. if (!access_ok(VERIFY_READ, p, request->size))
  681. return -EFAULT;
  682. end = (void __user *)p + request->size;
  683. count = 0;
  684. while (p < end) {
  685. if (get_user(control, &p->control))
  686. return -EFAULT;
  687. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  688. u.packet.interrupt = GET_INTERRUPT(control);
  689. u.packet.skip = GET_SKIP(control);
  690. u.packet.tag = GET_TAG(control);
  691. u.packet.sy = GET_SY(control);
  692. u.packet.header_length = GET_HEADER_LENGTH(control);
  693. if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
  694. header_length = u.packet.header_length;
  695. } else {
  696. /*
  697. * We require that header_length is a multiple of
  698. * the fixed header size, ctx->header_size.
  699. */
  700. if (ctx->header_size == 0) {
  701. if (u.packet.header_length > 0)
  702. return -EINVAL;
  703. } else if (u.packet.header_length % ctx->header_size != 0) {
  704. return -EINVAL;
  705. }
  706. header_length = 0;
  707. }
  708. next = (struct fw_cdev_iso_packet __user *)
  709. &p->header[header_length / 4];
  710. if (next > end)
  711. return -EINVAL;
  712. if (__copy_from_user
  713. (u.packet.header, p->header, header_length))
  714. return -EFAULT;
  715. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  716. u.packet.header_length + u.packet.payload_length > 0)
  717. return -EINVAL;
  718. if (payload + u.packet.payload_length > buffer_end)
  719. return -EINVAL;
  720. if (fw_iso_context_queue(ctx, &u.packet,
  721. &client->buffer, payload))
  722. break;
  723. p = next;
  724. payload += u.packet.payload_length;
  725. count++;
  726. }
  727. request->size -= uptr_to_u64(p) - request->packets;
  728. request->packets = uptr_to_u64(p);
  729. request->data = client->vm_start + payload;
  730. return count;
  731. }
  732. static int ioctl_start_iso(struct client *client, void *buffer)
  733. {
  734. struct fw_cdev_start_iso *request = buffer;
  735. if (client->iso_context == NULL || request->handle != 0)
  736. return -EINVAL;
  737. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE) {
  738. if (request->tags == 0 || request->tags > 15)
  739. return -EINVAL;
  740. if (request->sync > 15)
  741. return -EINVAL;
  742. }
  743. return fw_iso_context_start(client->iso_context, request->cycle,
  744. request->sync, request->tags);
  745. }
  746. static int ioctl_stop_iso(struct client *client, void *buffer)
  747. {
  748. struct fw_cdev_stop_iso *request = buffer;
  749. if (client->iso_context == NULL || request->handle != 0)
  750. return -EINVAL;
  751. return fw_iso_context_stop(client->iso_context);
  752. }
  753. static int ioctl_get_cycle_timer(struct client *client, void *buffer)
  754. {
  755. struct fw_cdev_get_cycle_timer *request = buffer;
  756. struct fw_card *card = client->device->card;
  757. unsigned long long bus_time;
  758. struct timeval tv;
  759. unsigned long flags;
  760. preempt_disable();
  761. local_irq_save(flags);
  762. bus_time = card->driver->get_bus_time(card);
  763. do_gettimeofday(&tv);
  764. local_irq_restore(flags);
  765. preempt_enable();
  766. request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
  767. request->cycle_timer = bus_time & 0xffffffff;
  768. return 0;
  769. }
  770. static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
  771. ioctl_get_info,
  772. ioctl_send_request,
  773. ioctl_allocate,
  774. ioctl_deallocate,
  775. ioctl_send_response,
  776. ioctl_initiate_bus_reset,
  777. ioctl_add_descriptor,
  778. ioctl_remove_descriptor,
  779. ioctl_create_iso_context,
  780. ioctl_queue_iso,
  781. ioctl_start_iso,
  782. ioctl_stop_iso,
  783. ioctl_get_cycle_timer,
  784. };
  785. static int
  786. dispatch_ioctl(struct client *client, unsigned int cmd, void __user *arg)
  787. {
  788. char buffer[256];
  789. int ret;
  790. if (_IOC_TYPE(cmd) != '#' ||
  791. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers))
  792. return -EINVAL;
  793. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  794. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  795. copy_from_user(buffer, arg, _IOC_SIZE(cmd)))
  796. return -EFAULT;
  797. }
  798. ret = ioctl_handlers[_IOC_NR(cmd)](client, buffer);
  799. if (ret < 0)
  800. return ret;
  801. if (_IOC_DIR(cmd) & _IOC_READ) {
  802. if (_IOC_SIZE(cmd) > sizeof(buffer) ||
  803. copy_to_user(arg, buffer, _IOC_SIZE(cmd)))
  804. return -EFAULT;
  805. }
  806. return ret;
  807. }
  808. static long
  809. fw_device_op_ioctl(struct file *file,
  810. unsigned int cmd, unsigned long arg)
  811. {
  812. struct client *client = file->private_data;
  813. if (fw_device_is_shutdown(client->device))
  814. return -ENODEV;
  815. return dispatch_ioctl(client, cmd, (void __user *) arg);
  816. }
  817. #ifdef CONFIG_COMPAT
  818. static long
  819. fw_device_op_compat_ioctl(struct file *file,
  820. unsigned int cmd, unsigned long arg)
  821. {
  822. struct client *client = file->private_data;
  823. if (fw_device_is_shutdown(client->device))
  824. return -ENODEV;
  825. return dispatch_ioctl(client, cmd, compat_ptr(arg));
  826. }
  827. #endif
  828. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  829. {
  830. struct client *client = file->private_data;
  831. enum dma_data_direction direction;
  832. unsigned long size;
  833. int page_count, ret;
  834. if (fw_device_is_shutdown(client->device))
  835. return -ENODEV;
  836. /* FIXME: We could support multiple buffers, but we don't. */
  837. if (client->buffer.pages != NULL)
  838. return -EBUSY;
  839. if (!(vma->vm_flags & VM_SHARED))
  840. return -EINVAL;
  841. if (vma->vm_start & ~PAGE_MASK)
  842. return -EINVAL;
  843. client->vm_start = vma->vm_start;
  844. size = vma->vm_end - vma->vm_start;
  845. page_count = size >> PAGE_SHIFT;
  846. if (size & ~PAGE_MASK)
  847. return -EINVAL;
  848. if (vma->vm_flags & VM_WRITE)
  849. direction = DMA_TO_DEVICE;
  850. else
  851. direction = DMA_FROM_DEVICE;
  852. ret = fw_iso_buffer_init(&client->buffer, client->device->card,
  853. page_count, direction);
  854. if (ret < 0)
  855. return ret;
  856. ret = fw_iso_buffer_map(&client->buffer, vma);
  857. if (ret < 0)
  858. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  859. return ret;
  860. }
  861. static int shutdown_resource(int id, void *p, void *data)
  862. {
  863. struct client_resource *r = p;
  864. struct client *client = data;
  865. r->release(client, r);
  866. return 0;
  867. }
  868. static int fw_device_op_release(struct inode *inode, struct file *file)
  869. {
  870. struct client *client = file->private_data;
  871. struct event *e, *next_e;
  872. unsigned long flags;
  873. mutex_lock(&client->device->client_list_mutex);
  874. list_del(&client->link);
  875. mutex_unlock(&client->device->client_list_mutex);
  876. if (client->buffer.pages)
  877. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  878. if (client->iso_context)
  879. fw_iso_context_destroy(client->iso_context);
  880. /* Freeze client->resource_idr and client->event_list */
  881. spin_lock_irqsave(&client->lock, flags);
  882. client->in_shutdown = true;
  883. spin_unlock_irqrestore(&client->lock, flags);
  884. idr_for_each(&client->resource_idr, shutdown_resource, client);
  885. idr_remove_all(&client->resource_idr);
  886. idr_destroy(&client->resource_idr);
  887. list_for_each_entry_safe(e, next_e, &client->event_list, link)
  888. kfree(e);
  889. /*
  890. * FIXME: client should be reference-counted. It's extremely unlikely
  891. * but there may still be transactions being completed at this point.
  892. */
  893. fw_device_put(client->device);
  894. kfree(client);
  895. return 0;
  896. }
  897. static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
  898. {
  899. struct client *client = file->private_data;
  900. unsigned int mask = 0;
  901. poll_wait(file, &client->wait, pt);
  902. if (fw_device_is_shutdown(client->device))
  903. mask |= POLLHUP | POLLERR;
  904. if (!list_empty(&client->event_list))
  905. mask |= POLLIN | POLLRDNORM;
  906. return mask;
  907. }
  908. const struct file_operations fw_device_ops = {
  909. .owner = THIS_MODULE,
  910. .open = fw_device_op_open,
  911. .read = fw_device_op_read,
  912. .unlocked_ioctl = fw_device_op_ioctl,
  913. .poll = fw_device_op_poll,
  914. .release = fw_device_op_release,
  915. .mmap = fw_device_op_mmap,
  916. #ifdef CONFIG_COMPAT
  917. .compat_ioctl = fw_device_op_compat_ioctl,
  918. #endif
  919. };