goldfish_pipe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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, &page, NULL);
  274. up_read(&current->mm->mmap_sem);
  275. if (ret < 0)
  276. break;
  277. if (dev->version) {
  278. /* Device version 1 or newer (qemu-android) expects the
  279. * physical address.
  280. */
  281. xaddr = page_to_phys(page) | (address & ~PAGE_MASK);
  282. } else {
  283. /* Device version 0 (classic emulator) expects the
  284. * virtual address.
  285. */
  286. xaddr = address;
  287. }
  288. /* Now, try to transfer the bytes in the current page */
  289. spin_lock_irqsave(&dev->lock, irq_flags);
  290. if (access_with_param(dev,
  291. is_write ? CMD_WRITE_BUFFER : CMD_READ_BUFFER,
  292. xaddr, avail, pipe, &status)) {
  293. gf_write_ptr(pipe, dev->base + PIPE_REG_CHANNEL,
  294. dev->base + PIPE_REG_CHANNEL_HIGH);
  295. writel(avail, dev->base + PIPE_REG_SIZE);
  296. gf_write_ptr((void *)xaddr,
  297. dev->base + PIPE_REG_ADDRESS,
  298. dev->base + PIPE_REG_ADDRESS_HIGH);
  299. writel(is_write ? CMD_WRITE_BUFFER : CMD_READ_BUFFER,
  300. dev->base + PIPE_REG_COMMAND);
  301. status = readl(dev->base + PIPE_REG_STATUS);
  302. }
  303. spin_unlock_irqrestore(&dev->lock, irq_flags);
  304. if (status > 0 && !is_write)
  305. set_page_dirty(page);
  306. put_page(page);
  307. if (status > 0) { /* Correct transfer */
  308. count += status;
  309. address += status;
  310. continue;
  311. } else if (status == 0) { /* EOF */
  312. ret = 0;
  313. break;
  314. } else if (status < 0 && count > 0) {
  315. /*
  316. * An error occurred and we already transferred
  317. * something on one of the previous pages.
  318. * Just return what we already copied and log this
  319. * err.
  320. *
  321. * Note: This seems like an incorrect approach but
  322. * cannot change it until we check if any user space
  323. * ABI relies on this behavior.
  324. */
  325. if (status != PIPE_ERROR_AGAIN)
  326. pr_info_ratelimited("goldfish_pipe: backend returned error %d on %s\n",
  327. status, is_write ? "write" : "read");
  328. ret = 0;
  329. break;
  330. }
  331. /*
  332. * If the error is not PIPE_ERROR_AGAIN, or if we are not in
  333. * non-blocking mode, just return the error code.
  334. */
  335. if (status != PIPE_ERROR_AGAIN ||
  336. (filp->f_flags & O_NONBLOCK) != 0) {
  337. ret = goldfish_pipe_error_convert(status);
  338. break;
  339. }
  340. /*
  341. * The backend blocked the read/write, wait until the backend
  342. * tells us it's ready to process more data.
  343. */
  344. wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
  345. set_bit(wakeBit, &pipe->flags);
  346. /* Tell the emulator we're going to wait for a wake event */
  347. goldfish_cmd(pipe,
  348. is_write ? CMD_WAKE_ON_WRITE : CMD_WAKE_ON_READ);
  349. /* Unlock the pipe, then wait for the wake signal */
  350. mutex_unlock(&pipe->lock);
  351. while (test_bit(wakeBit, &pipe->flags)) {
  352. if (wait_event_interruptible(
  353. pipe->wake_queue,
  354. !test_bit(wakeBit, &pipe->flags)))
  355. return -ERESTARTSYS;
  356. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  357. return -EIO;
  358. }
  359. /* Try to re-acquire the lock */
  360. if (mutex_lock_interruptible(&pipe->lock))
  361. return -ERESTARTSYS;
  362. }
  363. mutex_unlock(&pipe->lock);
  364. if (ret < 0)
  365. return ret;
  366. else
  367. return count;
  368. }
  369. static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
  370. size_t bufflen, loff_t *ppos)
  371. {
  372. return goldfish_pipe_read_write(filp, buffer, bufflen, 0);
  373. }
  374. static ssize_t goldfish_pipe_write(struct file *filp,
  375. const char __user *buffer, size_t bufflen,
  376. loff_t *ppos)
  377. {
  378. return goldfish_pipe_read_write(filp, (char __user *)buffer,
  379. bufflen, 1);
  380. }
  381. static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
  382. {
  383. struct goldfish_pipe *pipe = filp->private_data;
  384. unsigned int mask = 0;
  385. int status;
  386. mutex_lock(&pipe->lock);
  387. poll_wait(filp, &pipe->wake_queue, wait);
  388. status = goldfish_cmd_status(pipe, CMD_POLL);
  389. mutex_unlock(&pipe->lock);
  390. if (status & PIPE_POLL_IN)
  391. mask |= POLLIN | POLLRDNORM;
  392. if (status & PIPE_POLL_OUT)
  393. mask |= POLLOUT | POLLWRNORM;
  394. if (status & PIPE_POLL_HUP)
  395. mask |= POLLHUP;
  396. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  397. mask |= POLLERR;
  398. return mask;
  399. }
  400. static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
  401. {
  402. struct goldfish_pipe_dev *dev = dev_id;
  403. unsigned long irq_flags;
  404. int count = 0;
  405. /*
  406. * We're going to read from the emulator a list of (channel,flags)
  407. * pairs corresponding to the wake events that occurred on each
  408. * blocked pipe (i.e. channel).
  409. */
  410. spin_lock_irqsave(&dev->lock, irq_flags);
  411. for (;;) {
  412. /* First read the channel, 0 means the end of the list */
  413. struct goldfish_pipe *pipe;
  414. unsigned long wakes;
  415. unsigned long channel = 0;
  416. #ifdef CONFIG_64BIT
  417. channel = (u64)readl(dev->base + PIPE_REG_CHANNEL_HIGH) << 32;
  418. if (channel == 0)
  419. break;
  420. #endif
  421. channel |= readl(dev->base + PIPE_REG_CHANNEL);
  422. if (channel == 0)
  423. break;
  424. /* Convert channel to struct pipe pointer + read wake flags */
  425. wakes = readl(dev->base + PIPE_REG_WAKES);
  426. pipe = (struct goldfish_pipe *)(ptrdiff_t)channel;
  427. /* Did the emulator just closed a pipe? */
  428. if (wakes & PIPE_WAKE_CLOSED) {
  429. set_bit(BIT_CLOSED_ON_HOST, &pipe->flags);
  430. wakes |= PIPE_WAKE_READ | PIPE_WAKE_WRITE;
  431. }
  432. if (wakes & PIPE_WAKE_READ)
  433. clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
  434. if (wakes & PIPE_WAKE_WRITE)
  435. clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
  436. wake_up_interruptible(&pipe->wake_queue);
  437. count++;
  438. }
  439. spin_unlock_irqrestore(&dev->lock, irq_flags);
  440. return (count == 0) ? IRQ_NONE : IRQ_HANDLED;
  441. }
  442. /**
  443. * goldfish_pipe_open - open a channel to the AVD
  444. * @inode: inode of device
  445. * @file: file struct of opener
  446. *
  447. * Create a new pipe link between the emulator and the use application.
  448. * Each new request produces a new pipe.
  449. *
  450. * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit
  451. * right now so this is fine. A move to 64bit will need this addressing
  452. */
  453. static int goldfish_pipe_open(struct inode *inode, struct file *file)
  454. {
  455. struct goldfish_pipe *pipe;
  456. struct goldfish_pipe_dev *dev = pipe_dev;
  457. int32_t status;
  458. /* Allocate new pipe kernel object */
  459. pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
  460. if (pipe == NULL)
  461. return -ENOMEM;
  462. pipe->dev = dev;
  463. mutex_init(&pipe->lock);
  464. init_waitqueue_head(&pipe->wake_queue);
  465. /*
  466. * Now, tell the emulator we're opening a new pipe. We use the
  467. * pipe object's address as the channel identifier for simplicity.
  468. */
  469. status = goldfish_cmd_status(pipe, CMD_OPEN);
  470. if (status < 0) {
  471. kfree(pipe);
  472. return status;
  473. }
  474. /* All is done, save the pipe into the file's private data field */
  475. file->private_data = pipe;
  476. return 0;
  477. }
  478. static int goldfish_pipe_release(struct inode *inode, struct file *filp)
  479. {
  480. struct goldfish_pipe *pipe = filp->private_data;
  481. /* The guest is closing the channel, so tell the emulator right now */
  482. goldfish_cmd(pipe, CMD_CLOSE);
  483. kfree(pipe);
  484. filp->private_data = NULL;
  485. return 0;
  486. }
  487. static const struct file_operations goldfish_pipe_fops = {
  488. .owner = THIS_MODULE,
  489. .read = goldfish_pipe_read,
  490. .write = goldfish_pipe_write,
  491. .poll = goldfish_pipe_poll,
  492. .open = goldfish_pipe_open,
  493. .release = goldfish_pipe_release,
  494. };
  495. static struct miscdevice goldfish_pipe_device = {
  496. .minor = MISC_DYNAMIC_MINOR,
  497. .name = "goldfish_pipe",
  498. .fops = &goldfish_pipe_fops,
  499. };
  500. static int goldfish_pipe_probe(struct platform_device *pdev)
  501. {
  502. int err;
  503. struct resource *r;
  504. struct goldfish_pipe_dev *dev = pipe_dev;
  505. /* not thread safe, but this should not happen */
  506. WARN_ON(dev->base != NULL);
  507. spin_lock_init(&dev->lock);
  508. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  509. if (r == NULL || resource_size(r) < PAGE_SIZE) {
  510. dev_err(&pdev->dev, "can't allocate i/o page\n");
  511. return -EINVAL;
  512. }
  513. dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
  514. if (dev->base == NULL) {
  515. dev_err(&pdev->dev, "ioremap failed\n");
  516. return -EINVAL;
  517. }
  518. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  519. if (r == NULL) {
  520. err = -EINVAL;
  521. goto error;
  522. }
  523. dev->irq = r->start;
  524. err = devm_request_irq(&pdev->dev, dev->irq, goldfish_pipe_interrupt,
  525. IRQF_SHARED, "goldfish_pipe", dev);
  526. if (err) {
  527. dev_err(&pdev->dev, "unable to allocate IRQ\n");
  528. goto error;
  529. }
  530. err = misc_register(&goldfish_pipe_device);
  531. if (err) {
  532. dev_err(&pdev->dev, "unable to register device\n");
  533. goto error;
  534. }
  535. setup_access_params_addr(pdev, dev);
  536. /* Although the pipe device in the classic Android emulator does not
  537. * recognize the 'version' register, it won't treat this as an error
  538. * either and will simply return 0, which is fine.
  539. */
  540. dev->version = readl(dev->base + PIPE_REG_VERSION);
  541. return 0;
  542. error:
  543. dev->base = NULL;
  544. return err;
  545. }
  546. static int goldfish_pipe_remove(struct platform_device *pdev)
  547. {
  548. struct goldfish_pipe_dev *dev = pipe_dev;
  549. misc_deregister(&goldfish_pipe_device);
  550. dev->base = NULL;
  551. return 0;
  552. }
  553. static const struct acpi_device_id goldfish_pipe_acpi_match[] = {
  554. { "GFSH0003", 0 },
  555. { },
  556. };
  557. MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match);
  558. static const struct of_device_id goldfish_pipe_of_match[] = {
  559. { .compatible = "google,android-pipe", },
  560. {},
  561. };
  562. MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match);
  563. static struct platform_driver goldfish_pipe = {
  564. .probe = goldfish_pipe_probe,
  565. .remove = goldfish_pipe_remove,
  566. .driver = {
  567. .name = "goldfish_pipe",
  568. .owner = THIS_MODULE,
  569. .of_match_table = goldfish_pipe_of_match,
  570. .acpi_match_table = ACPI_PTR(goldfish_pipe_acpi_match),
  571. }
  572. };
  573. module_platform_driver(goldfish_pipe);
  574. MODULE_AUTHOR("David Turner <digit@google.com>");
  575. MODULE_LICENSE("GPL");