goldfish_pipe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. * Copyright (C) 2012 Intel, Inc.
  4. * Copyright (C) 2013 Intel, Inc.
  5. * Copyright (C) 2014 Linaro Limited
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  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. */
  17. /* This source file contains the implementation of a special device driver
  18. * that intends to provide a *very* fast communication channel between the
  19. * guest system and the QEMU emulator.
  20. *
  21. * Usage from the guest is simply the following (error handling simplified):
  22. *
  23. * int fd = open("/dev/qemu_pipe",O_RDWR);
  24. * .... write() or read() through the pipe.
  25. *
  26. * This driver doesn't deal with the exact protocol used during the session.
  27. * It is intended to be as simple as something like:
  28. *
  29. * // do this _just_ after opening the fd to connect to a specific
  30. * // emulator service.
  31. * const char* msg = "<pipename>";
  32. * if (write(fd, msg, strlen(msg)+1) < 0) {
  33. * ... could not connect to <pipename> service
  34. * close(fd);
  35. * }
  36. *
  37. * // after this, simply read() and write() to communicate with the
  38. * // service. Exact protocol details left as an exercise to the reader.
  39. *
  40. * This driver is very fast because it doesn't copy any data through
  41. * intermediate buffers, since the emulator is capable of translating
  42. * guest user addresses into host ones.
  43. *
  44. * Note that we must however ensure that each user page involved in the
  45. * exchange is properly mapped during a transfer.
  46. */
  47. #include <linux/module.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/kernel.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/poll.h>
  54. #include <linux/sched.h>
  55. #include <linux/bitops.h>
  56. #include <linux/slab.h>
  57. #include <linux/io.h>
  58. #include <linux/goldfish.h>
  59. #include <linux/dma-mapping.h>
  60. #include <linux/mm.h>
  61. #include <linux/acpi.h>
  62. /*
  63. * IMPORTANT: The following constants must match the ones used and defined
  64. * in external/qemu/hw/goldfish_pipe.c in the Android source tree.
  65. */
  66. /* pipe device registers */
  67. #define PIPE_REG_COMMAND 0x00 /* write: value = command */
  68. #define PIPE_REG_STATUS 0x04 /* read */
  69. #define PIPE_REG_CHANNEL 0x08 /* read/write: channel id */
  70. #define PIPE_REG_CHANNEL_HIGH 0x30 /* read/write: channel id */
  71. #define PIPE_REG_SIZE 0x0c /* read/write: buffer size */
  72. #define PIPE_REG_ADDRESS 0x10 /* write: physical address */
  73. #define PIPE_REG_ADDRESS_HIGH 0x34 /* write: physical address */
  74. #define PIPE_REG_WAKES 0x14 /* read: wake flags */
  75. #define PIPE_REG_PARAMS_ADDR_LOW 0x18 /* read/write: batch data address */
  76. #define PIPE_REG_PARAMS_ADDR_HIGH 0x1c /* read/write: batch data address */
  77. #define PIPE_REG_ACCESS_PARAMS 0x20 /* write: batch access */
  78. #define PIPE_REG_VERSION 0x24 /* read: device version */
  79. /* list of commands for PIPE_REG_COMMAND */
  80. #define CMD_OPEN 1 /* open new channel */
  81. #define CMD_CLOSE 2 /* close channel (from guest) */
  82. #define CMD_POLL 3 /* poll read/write status */
  83. /* List of bitflags returned in status of CMD_POLL command */
  84. #define PIPE_POLL_IN (1 << 0)
  85. #define PIPE_POLL_OUT (1 << 1)
  86. #define PIPE_POLL_HUP (1 << 2)
  87. /* The following commands are related to write operations */
  88. #define CMD_WRITE_BUFFER 4 /* send a user buffer to the emulator */
  89. #define CMD_WAKE_ON_WRITE 5 /* tell the emulator to wake us when writing
  90. is possible */
  91. #define CMD_READ_BUFFER 6 /* receive a user buffer from the emulator */
  92. #define CMD_WAKE_ON_READ 7 /* tell the emulator to wake us when reading
  93. * is possible */
  94. /* Possible status values used to signal errors - see goldfish_pipe_error_convert */
  95. #define PIPE_ERROR_INVAL -1
  96. #define PIPE_ERROR_AGAIN -2
  97. #define PIPE_ERROR_NOMEM -3
  98. #define PIPE_ERROR_IO -4
  99. /* Bit-flags used to signal events from the emulator */
  100. #define PIPE_WAKE_CLOSED (1 << 0) /* emulator closed pipe */
  101. #define PIPE_WAKE_READ (1 << 1) /* pipe can now be read from */
  102. #define PIPE_WAKE_WRITE (1 << 2) /* pipe can now be written to */
  103. struct access_params {
  104. unsigned long channel;
  105. u32 size;
  106. unsigned long address;
  107. u32 cmd;
  108. u32 result;
  109. /* reserved for future extension */
  110. u32 flags;
  111. };
  112. /* The global driver data. Holds a reference to the i/o page used to
  113. * communicate with the emulator, and a wake queue for blocked tasks
  114. * waiting to be awoken.
  115. */
  116. struct goldfish_pipe_dev {
  117. spinlock_t lock;
  118. unsigned char __iomem *base;
  119. struct access_params *aps;
  120. int irq;
  121. u32 version;
  122. };
  123. static struct goldfish_pipe_dev pipe_dev[1];
  124. /* This data type models a given pipe instance */
  125. struct goldfish_pipe {
  126. struct goldfish_pipe_dev *dev;
  127. struct mutex lock;
  128. unsigned long flags;
  129. wait_queue_head_t wake_queue;
  130. };
  131. /* Bit flags for the 'flags' field */
  132. enum {
  133. BIT_CLOSED_ON_HOST = 0, /* pipe closed by host */
  134. BIT_WAKE_ON_WRITE = 1, /* want to be woken on writes */
  135. BIT_WAKE_ON_READ = 2, /* want to be woken on reads */
  136. };
  137. static u32 goldfish_cmd_status(struct goldfish_pipe *pipe, u32 cmd)
  138. {
  139. unsigned long flags;
  140. u32 status;
  141. struct goldfish_pipe_dev *dev = pipe->dev;
  142. spin_lock_irqsave(&dev->lock, flags);
  143. gf_write_ptr(pipe, dev->base + PIPE_REG_CHANNEL,
  144. dev->base + PIPE_REG_CHANNEL_HIGH);
  145. writel(cmd, dev->base + PIPE_REG_COMMAND);
  146. status = readl(dev->base + PIPE_REG_STATUS);
  147. spin_unlock_irqrestore(&dev->lock, flags);
  148. return status;
  149. }
  150. static void goldfish_cmd(struct goldfish_pipe *pipe, u32 cmd)
  151. {
  152. unsigned long flags;
  153. struct goldfish_pipe_dev *dev = pipe->dev;
  154. spin_lock_irqsave(&dev->lock, flags);
  155. gf_write_ptr(pipe, dev->base + PIPE_REG_CHANNEL,
  156. dev->base + PIPE_REG_CHANNEL_HIGH);
  157. writel(cmd, dev->base + PIPE_REG_COMMAND);
  158. spin_unlock_irqrestore(&dev->lock, flags);
  159. }
  160. /* This function converts an error code returned by the emulator through
  161. * the PIPE_REG_STATUS i/o register into a valid negative errno value.
  162. */
  163. static int goldfish_pipe_error_convert(int status)
  164. {
  165. switch (status) {
  166. case PIPE_ERROR_AGAIN:
  167. return -EAGAIN;
  168. case PIPE_ERROR_NOMEM:
  169. return -ENOMEM;
  170. case PIPE_ERROR_IO:
  171. return -EIO;
  172. default:
  173. return -EINVAL;
  174. }
  175. }
  176. /*
  177. * Notice: QEMU will return 0 for un-known register access, indicating
  178. * param_acess is supported or not
  179. */
  180. static int valid_batchbuffer_addr(struct goldfish_pipe_dev *dev,
  181. struct access_params *aps)
  182. {
  183. u32 aph, apl;
  184. u64 paddr;
  185. aph = readl(dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
  186. apl = readl(dev->base + PIPE_REG_PARAMS_ADDR_LOW);
  187. paddr = ((u64)aph << 32) | apl;
  188. if (paddr != (__pa(aps)))
  189. return 0;
  190. return 1;
  191. }
  192. /* 0 on success */
  193. static int setup_access_params_addr(struct platform_device *pdev,
  194. struct goldfish_pipe_dev *dev)
  195. {
  196. dma_addr_t dma_handle;
  197. struct access_params *aps;
  198. aps = dmam_alloc_coherent(&pdev->dev, sizeof(struct access_params),
  199. &dma_handle, GFP_KERNEL);
  200. if (!aps)
  201. return -ENOMEM;
  202. writel(upper_32_bits(dma_handle), dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
  203. writel(lower_32_bits(dma_handle), dev->base + PIPE_REG_PARAMS_ADDR_LOW);
  204. if (valid_batchbuffer_addr(dev, aps)) {
  205. dev->aps = aps;
  206. return 0;
  207. } else
  208. return -1;
  209. }
  210. /* A value that will not be set by qemu emulator */
  211. #define INITIAL_BATCH_RESULT (0xdeadbeaf)
  212. static int access_with_param(struct goldfish_pipe_dev *dev, const int cmd,
  213. unsigned long address, unsigned long avail,
  214. struct goldfish_pipe *pipe, int *status)
  215. {
  216. struct access_params *aps = dev->aps;
  217. if (aps == NULL)
  218. return -1;
  219. aps->result = INITIAL_BATCH_RESULT;
  220. aps->channel = (unsigned long)pipe;
  221. aps->size = avail;
  222. aps->address = address;
  223. aps->cmd = cmd;
  224. writel(cmd, dev->base + PIPE_REG_ACCESS_PARAMS);
  225. /*
  226. * If the aps->result has not changed, that means
  227. * that the batch command failed
  228. */
  229. if (aps->result == INITIAL_BATCH_RESULT)
  230. return -1;
  231. *status = aps->result;
  232. return 0;
  233. }
  234. static ssize_t goldfish_pipe_read_write(struct file *filp, char __user *buffer,
  235. size_t bufflen, int is_write)
  236. {
  237. unsigned long irq_flags;
  238. struct goldfish_pipe *pipe = filp->private_data;
  239. struct goldfish_pipe_dev *dev = pipe->dev;
  240. unsigned long address, address_end;
  241. int count = 0, ret = -EINVAL;
  242. /* If the emulator already closed the pipe, no need to go further */
  243. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  244. return -EIO;
  245. /* Null reads or writes succeeds */
  246. if (unlikely(bufflen == 0))
  247. return 0;
  248. /* Check the buffer range for access */
  249. if (!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ,
  250. buffer, bufflen))
  251. return -EFAULT;
  252. /* Serialize access to the pipe */
  253. if (mutex_lock_interruptible(&pipe->lock))
  254. return -ERESTARTSYS;
  255. address = (unsigned long)(void *)buffer;
  256. address_end = address + bufflen;
  257. while (address < address_end) {
  258. unsigned long page_end = (address & PAGE_MASK) + PAGE_SIZE;
  259. unsigned long next = page_end < address_end ? page_end
  260. : address_end;
  261. unsigned long avail = next - address;
  262. int status, wakeBit;
  263. struct page *page;
  264. /* Either vaddr or paddr depending on the device version */
  265. unsigned long xaddr;
  266. /*
  267. * We grab the pages on a page-by-page basis in case user
  268. * space gives us a potentially huge buffer but the read only
  269. * returns a small amount, then there's no need to pin that
  270. * much memory to the process.
  271. */
  272. down_read(&current->mm->mmap_sem);
  273. ret = get_user_pages(address, 1, is_write ? 0 : FOLL_WRITE,
  274. &page, NULL);
  275. up_read(&current->mm->mmap_sem);
  276. if (ret < 0)
  277. break;
  278. if (dev->version) {
  279. /* Device version 1 or newer (qemu-android) expects the
  280. * physical address.
  281. */
  282. xaddr = page_to_phys(page) | (address & ~PAGE_MASK);
  283. } else {
  284. /* Device version 0 (classic emulator) expects the
  285. * virtual address.
  286. */
  287. xaddr = address;
  288. }
  289. /* Now, try to transfer the bytes in the current page */
  290. spin_lock_irqsave(&dev->lock, irq_flags);
  291. if (access_with_param(dev,
  292. is_write ? CMD_WRITE_BUFFER : CMD_READ_BUFFER,
  293. xaddr, avail, pipe, &status)) {
  294. gf_write_ptr(pipe, dev->base + PIPE_REG_CHANNEL,
  295. dev->base + PIPE_REG_CHANNEL_HIGH);
  296. writel(avail, dev->base + PIPE_REG_SIZE);
  297. gf_write_ptr((void *)xaddr,
  298. dev->base + PIPE_REG_ADDRESS,
  299. dev->base + PIPE_REG_ADDRESS_HIGH);
  300. writel(is_write ? CMD_WRITE_BUFFER : CMD_READ_BUFFER,
  301. dev->base + PIPE_REG_COMMAND);
  302. status = readl(dev->base + PIPE_REG_STATUS);
  303. }
  304. spin_unlock_irqrestore(&dev->lock, irq_flags);
  305. if (status > 0 && !is_write)
  306. set_page_dirty(page);
  307. put_page(page);
  308. if (status > 0) { /* Correct transfer */
  309. count += status;
  310. address += status;
  311. continue;
  312. } else if (status == 0) { /* EOF */
  313. ret = 0;
  314. break;
  315. } else if (status < 0 && count > 0) {
  316. /*
  317. * An error occurred and we already transferred
  318. * something on one of the previous pages.
  319. * Just return what we already copied and log this
  320. * err.
  321. *
  322. * Note: This seems like an incorrect approach but
  323. * cannot change it until we check if any user space
  324. * ABI relies on this behavior.
  325. */
  326. if (status != PIPE_ERROR_AGAIN)
  327. pr_info_ratelimited("goldfish_pipe: backend returned error %d on %s\n",
  328. status, is_write ? "write" : "read");
  329. ret = 0;
  330. break;
  331. }
  332. /*
  333. * If the error is not PIPE_ERROR_AGAIN, or if we are not in
  334. * non-blocking mode, just return the error code.
  335. */
  336. if (status != PIPE_ERROR_AGAIN ||
  337. (filp->f_flags & O_NONBLOCK) != 0) {
  338. ret = goldfish_pipe_error_convert(status);
  339. break;
  340. }
  341. /*
  342. * The backend blocked the read/write, wait until the backend
  343. * tells us it's ready to process more data.
  344. */
  345. wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
  346. set_bit(wakeBit, &pipe->flags);
  347. /* Tell the emulator we're going to wait for a wake event */
  348. goldfish_cmd(pipe,
  349. is_write ? CMD_WAKE_ON_WRITE : CMD_WAKE_ON_READ);
  350. /* Unlock the pipe, then wait for the wake signal */
  351. mutex_unlock(&pipe->lock);
  352. while (test_bit(wakeBit, &pipe->flags)) {
  353. if (wait_event_interruptible(
  354. pipe->wake_queue,
  355. !test_bit(wakeBit, &pipe->flags)))
  356. return -ERESTARTSYS;
  357. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  358. return -EIO;
  359. }
  360. /* Try to re-acquire the lock */
  361. if (mutex_lock_interruptible(&pipe->lock))
  362. return -ERESTARTSYS;
  363. }
  364. mutex_unlock(&pipe->lock);
  365. if (ret < 0)
  366. return ret;
  367. else
  368. return count;
  369. }
  370. static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
  371. size_t bufflen, loff_t *ppos)
  372. {
  373. return goldfish_pipe_read_write(filp, buffer, bufflen, 0);
  374. }
  375. static ssize_t goldfish_pipe_write(struct file *filp,
  376. const char __user *buffer, size_t bufflen,
  377. loff_t *ppos)
  378. {
  379. return goldfish_pipe_read_write(filp, (char __user *)buffer,
  380. bufflen, 1);
  381. }
  382. static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
  383. {
  384. struct goldfish_pipe *pipe = filp->private_data;
  385. unsigned int mask = 0;
  386. int status;
  387. mutex_lock(&pipe->lock);
  388. poll_wait(filp, &pipe->wake_queue, wait);
  389. status = goldfish_cmd_status(pipe, CMD_POLL);
  390. mutex_unlock(&pipe->lock);
  391. if (status & PIPE_POLL_IN)
  392. mask |= POLLIN | POLLRDNORM;
  393. if (status & PIPE_POLL_OUT)
  394. mask |= POLLOUT | POLLWRNORM;
  395. if (status & PIPE_POLL_HUP)
  396. mask |= POLLHUP;
  397. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  398. mask |= POLLERR;
  399. return mask;
  400. }
  401. static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
  402. {
  403. struct goldfish_pipe_dev *dev = dev_id;
  404. unsigned long irq_flags;
  405. int count = 0;
  406. /*
  407. * We're going to read from the emulator a list of (channel,flags)
  408. * pairs corresponding to the wake events that occurred on each
  409. * blocked pipe (i.e. channel).
  410. */
  411. spin_lock_irqsave(&dev->lock, irq_flags);
  412. for (;;) {
  413. /* First read the channel, 0 means the end of the list */
  414. struct goldfish_pipe *pipe;
  415. unsigned long wakes;
  416. unsigned long channel = 0;
  417. #ifdef CONFIG_64BIT
  418. channel = (u64)readl(dev->base + PIPE_REG_CHANNEL_HIGH) << 32;
  419. if (channel == 0)
  420. break;
  421. #endif
  422. channel |= readl(dev->base + PIPE_REG_CHANNEL);
  423. if (channel == 0)
  424. break;
  425. /* Convert channel to struct pipe pointer + read wake flags */
  426. wakes = readl(dev->base + PIPE_REG_WAKES);
  427. pipe = (struct goldfish_pipe *)(ptrdiff_t)channel;
  428. /* Did the emulator just closed a pipe? */
  429. if (wakes & PIPE_WAKE_CLOSED) {
  430. set_bit(BIT_CLOSED_ON_HOST, &pipe->flags);
  431. wakes |= PIPE_WAKE_READ | PIPE_WAKE_WRITE;
  432. }
  433. if (wakes & PIPE_WAKE_READ)
  434. clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
  435. if (wakes & PIPE_WAKE_WRITE)
  436. clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
  437. wake_up_interruptible(&pipe->wake_queue);
  438. count++;
  439. }
  440. spin_unlock_irqrestore(&dev->lock, irq_flags);
  441. return (count == 0) ? IRQ_NONE : IRQ_HANDLED;
  442. }
  443. /**
  444. * goldfish_pipe_open - open a channel to the AVD
  445. * @inode: inode of device
  446. * @file: file struct of opener
  447. *
  448. * Create a new pipe link between the emulator and the use application.
  449. * Each new request produces a new pipe.
  450. *
  451. * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit
  452. * right now so this is fine. A move to 64bit will need this addressing
  453. */
  454. static int goldfish_pipe_open(struct inode *inode, struct file *file)
  455. {
  456. struct goldfish_pipe *pipe;
  457. struct goldfish_pipe_dev *dev = pipe_dev;
  458. int32_t status;
  459. /* Allocate new pipe kernel object */
  460. pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
  461. if (pipe == NULL)
  462. return -ENOMEM;
  463. pipe->dev = dev;
  464. mutex_init(&pipe->lock);
  465. init_waitqueue_head(&pipe->wake_queue);
  466. /*
  467. * Now, tell the emulator we're opening a new pipe. We use the
  468. * pipe object's address as the channel identifier for simplicity.
  469. */
  470. status = goldfish_cmd_status(pipe, CMD_OPEN);
  471. if (status < 0) {
  472. kfree(pipe);
  473. return status;
  474. }
  475. /* All is done, save the pipe into the file's private data field */
  476. file->private_data = pipe;
  477. return 0;
  478. }
  479. static int goldfish_pipe_release(struct inode *inode, struct file *filp)
  480. {
  481. struct goldfish_pipe *pipe = filp->private_data;
  482. /* The guest is closing the channel, so tell the emulator right now */
  483. goldfish_cmd(pipe, CMD_CLOSE);
  484. kfree(pipe);
  485. filp->private_data = NULL;
  486. return 0;
  487. }
  488. static const struct file_operations goldfish_pipe_fops = {
  489. .owner = THIS_MODULE,
  490. .read = goldfish_pipe_read,
  491. .write = goldfish_pipe_write,
  492. .poll = goldfish_pipe_poll,
  493. .open = goldfish_pipe_open,
  494. .release = goldfish_pipe_release,
  495. };
  496. static struct miscdevice goldfish_pipe_device = {
  497. .minor = MISC_DYNAMIC_MINOR,
  498. .name = "goldfish_pipe",
  499. .fops = &goldfish_pipe_fops,
  500. };
  501. static int goldfish_pipe_probe(struct platform_device *pdev)
  502. {
  503. int err;
  504. struct resource *r;
  505. struct goldfish_pipe_dev *dev = pipe_dev;
  506. /* not thread safe, but this should not happen */
  507. WARN_ON(dev->base != NULL);
  508. spin_lock_init(&dev->lock);
  509. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  510. if (r == NULL || resource_size(r) < PAGE_SIZE) {
  511. dev_err(&pdev->dev, "can't allocate i/o page\n");
  512. return -EINVAL;
  513. }
  514. dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
  515. if (dev->base == NULL) {
  516. dev_err(&pdev->dev, "ioremap failed\n");
  517. return -EINVAL;
  518. }
  519. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  520. if (r == NULL) {
  521. err = -EINVAL;
  522. goto error;
  523. }
  524. dev->irq = r->start;
  525. err = devm_request_irq(&pdev->dev, dev->irq, goldfish_pipe_interrupt,
  526. IRQF_SHARED, "goldfish_pipe", dev);
  527. if (err) {
  528. dev_err(&pdev->dev, "unable to allocate IRQ\n");
  529. goto error;
  530. }
  531. err = misc_register(&goldfish_pipe_device);
  532. if (err) {
  533. dev_err(&pdev->dev, "unable to register device\n");
  534. goto error;
  535. }
  536. setup_access_params_addr(pdev, dev);
  537. /* Although the pipe device in the classic Android emulator does not
  538. * recognize the 'version' register, it won't treat this as an error
  539. * either and will simply return 0, which is fine.
  540. */
  541. dev->version = readl(dev->base + PIPE_REG_VERSION);
  542. return 0;
  543. error:
  544. dev->base = NULL;
  545. return err;
  546. }
  547. static int goldfish_pipe_remove(struct platform_device *pdev)
  548. {
  549. struct goldfish_pipe_dev *dev = pipe_dev;
  550. misc_deregister(&goldfish_pipe_device);
  551. dev->base = NULL;
  552. return 0;
  553. }
  554. static const struct acpi_device_id goldfish_pipe_acpi_match[] = {
  555. { "GFSH0003", 0 },
  556. { },
  557. };
  558. MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match);
  559. static const struct of_device_id goldfish_pipe_of_match[] = {
  560. { .compatible = "google,android-pipe", },
  561. {},
  562. };
  563. MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match);
  564. static struct platform_driver goldfish_pipe = {
  565. .probe = goldfish_pipe_probe,
  566. .remove = goldfish_pipe_remove,
  567. .driver = {
  568. .name = "goldfish_pipe",
  569. .owner = THIS_MODULE,
  570. .of_match_table = goldfish_pipe_of_match,
  571. .acpi_match_table = ACPI_PTR(goldfish_pipe_acpi_match),
  572. }
  573. };
  574. module_platform_driver(goldfish_pipe);
  575. MODULE_AUTHOR("David Turner <digit@google.com>");
  576. MODULE_LICENSE("GPL");