goldfish_pipe.c 19 KB

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