audio-pkt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/platform_device.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/ipc_logging.h>
  16. #include <linux/refcount.h>
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/cdev.h>
  21. #include <linux/slab.h>
  22. #include <linux/poll.h>
  23. #include <linux/idr.h>
  24. #include <linux/of.h>
  25. #include <linux/fs.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/termios.h>
  28. #include <ipc/gpr-lite.h>
  29. #include <dsp/spf-core.h>
  30. #include <dsp/msm_audio_ion.h>
  31. /* Define IPC Logging Macros */
  32. #define AUDIO_PKT_IPC_LOG_PAGE_CNT 2
  33. static void *audio_pkt_ilctxt;
  34. static int audio_pkt_debug_mask;
  35. module_param_named(debug_mask, audio_pkt_debug_mask, int, 0664);
  36. #define APM_CMD_SHARED_MEM_MAP_REGIONS 0x0100100C
  37. #define APM_MEMORY_MAP_BIT_MASK_IS_OFFSET_MODE 0x00000004UL
  38. enum {
  39. AUDIO_PKT_INFO = 1U << 0,
  40. };
  41. #define AUDIO_PKT_INFO(x, ...) \
  42. do { \
  43. if (audio_pkt_debug_mask & AUDIO_PKT_INFO) { \
  44. ipc_log_string(audio_pkt_ilctxt, \
  45. "[%s]: "x, __func__, ##__VA_ARGS__); \
  46. } \
  47. } while (0)
  48. #define AUDIO_PKT_ERR(x, ...) \
  49. do { \
  50. pr_err_ratelimited("[%s]: "x, __func__, ##__VA_ARGS__); \
  51. ipc_log_string(audio_pkt_ilctxt, "[%s]: "x, __func__, ##__VA_ARGS__); \
  52. } while (0)
  53. #define MODULE_NAME "audio-pkt"
  54. #define MINOR_NUMBER_COUNT 1
  55. #define AUDPKT_DRIVER_NAME "aud_pasthru_adsp"
  56. #define CHANNEL_NAME "adsp_apps"
  57. enum audio_pkt_state {
  58. AUDIO_PKT_INIT,
  59. AUDIO_PKT_PROBED,
  60. AUDIO_PKT_REMOVED,
  61. AUDIO_PKT_DEINIT,
  62. };
  63. /**
  64. * struct audio_pkt_device - driver context, relates to platform dev
  65. * @dev: audio pkt device
  66. * @cdev: cdev for the audio pkt device
  67. * @lock: synchronization of @dev
  68. * @queue_lock: synchronization of @queue operations
  69. * @queue: incoming message queue
  70. * @readq: wait object for incoming queue
  71. * @dev_name: /dev/@dev_name for audio_pkt device
  72. * @ch_name: audio channel to match to
  73. * @audio_pkt_major: Major number of audio pkt driver
  74. * @audio_pkt_class: audio pkt class pointer
  75. */
  76. struct audio_pkt_device {
  77. struct device *dev;
  78. struct cdev cdev;
  79. struct mutex lock;
  80. spinlock_t queue_lock;
  81. struct sk_buff_head queue;
  82. wait_queue_head_t readq;
  83. char dev_name[20];
  84. char ch_name[20];
  85. dev_t audio_pkt_major;
  86. struct class *audio_pkt_class;
  87. };
  88. struct audio_pkt_priv {
  89. struct gpr_device *adev;
  90. struct device *dev;
  91. struct audio_pkt_device *ap_dev;
  92. struct mutex lock;
  93. enum audio_pkt_state status;
  94. };
  95. static struct audio_pkt_priv *ap_priv;
  96. struct audio_pkt_apm_cmd_shared_mem_map_regions_t {
  97. uint16_t mem_pool_id;
  98. uint16_t num_regions;
  99. uint32_t property_flag;
  100. };
  101. struct audio_pkt_apm_shared_map_region_payload_t {
  102. uint32_t shm_addr_lsw;
  103. uint32_t shm_addr_msw;
  104. uint32_t mem_size_bytes;
  105. };
  106. struct audio_pkt_apm_mem_map {
  107. struct audio_pkt_apm_cmd_shared_mem_map_regions_t mmap_header;
  108. struct audio_pkt_apm_shared_map_region_payload_t mmap_payload;
  109. };
  110. struct audio_gpr_pkt {
  111. struct gpr_hdr audpkt_hdr;
  112. struct audio_pkt_apm_mem_map audpkt_mem_map;
  113. };
  114. typedef void (*audio_pkt_clnt_cb_fn)(void *buf, int len, void *priv);
  115. struct audio_pkt_clnt_ch {
  116. int client_id;
  117. audio_pkt_clnt_cb_fn func;
  118. };
  119. /**
  120. * audio_pkt_open() - open() syscall for the audio_pkt device
  121. * inode: Pointer to the inode structure.
  122. * file: Pointer to the file structure.
  123. *
  124. * This function is used to open the audio pkt device when
  125. * userspace client do a open() system call. All input arguments are
  126. * validated by the virtual file system before calling this function.
  127. */
  128. int audio_pkt_open(struct inode *inode, struct file *file)
  129. {
  130. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  131. AUDIO_PKT_INFO("%s: for %s \n", __func__,audpkt_dev->ch_name);
  132. file->private_data = ap_priv;
  133. return 0;
  134. }
  135. /**
  136. * audio_pkt_release() - release operation on audio_pkt device
  137. * inode: Pointer to the inode structure.
  138. * file: Pointer to the file structure.
  139. *
  140. * This function is used to release the audio pkt device when
  141. * userspace client do a close() system call. All input arguments are
  142. * validated by the virtual file system before calling this function.
  143. */
  144. int audio_pkt_release(struct inode *inode, struct file *file)
  145. {
  146. struct audio_pkt_priv *ap_priv = file->private_data;
  147. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  148. struct sk_buff *skb;
  149. unsigned long flags;
  150. if ((!audpkt_dev)) {
  151. AUDIO_PKT_ERR("invalid device handle\n");
  152. return -EINVAL;
  153. }
  154. AUDIO_PKT_INFO("%s: for %s \n", __func__,audpkt_dev->ch_name);
  155. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  156. /* Discard all SKBs */
  157. while (!skb_queue_empty(&audpkt_dev->queue)) {
  158. skb = skb_dequeue(&audpkt_dev->queue);
  159. kfree_skb(skb);
  160. }
  161. wake_up_interruptible(&audpkt_dev->readq);
  162. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  163. file->private_data = NULL;
  164. spf_core_apm_close_all();
  165. msm_audio_ion_crash_handler();
  166. return 0;
  167. }
  168. static int audio_pkt_internal_release(struct platform_device *adev)
  169. {
  170. struct audio_pkt_priv *ap_priv = platform_get_drvdata(adev);
  171. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  172. struct sk_buff *skb;
  173. unsigned long flags;
  174. if ((!audpkt_dev)) {
  175. AUDIO_PKT_ERR("invalid device handle\n");
  176. return -EINVAL;
  177. }
  178. AUDIO_PKT_INFO("%s: for %s\n", __func__,audpkt_dev->ch_name);
  179. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  180. /* Discard all SKBs */
  181. while (!skb_queue_empty(&audpkt_dev->queue)) {
  182. skb = skb_dequeue(&audpkt_dev->queue);
  183. kfree_skb(skb);
  184. }
  185. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  186. wake_up_interruptible(&audpkt_dev->readq);
  187. return 0;
  188. }
  189. /**
  190. * audio_pkt_read() - read() syscall for the audio_pkt device
  191. * file: Pointer to the file structure.
  192. * buf: Pointer to the userspace buffer.
  193. * count: Number bytes to read from the file.
  194. * ppos: Pointer to the position into the file.
  195. *
  196. * This function is used to Read the data from audio pkt device when
  197. * userspace client do a read() system call. All input arguments are
  198. * validated by the virtual file system before calling this function.
  199. */
  200. ssize_t audio_pkt_read(struct file *file, char __user *buf,
  201. size_t count, loff_t *ppos)
  202. {
  203. struct audio_pkt_priv *ap_priv = file->private_data;
  204. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  205. unsigned long flags;
  206. struct sk_buff *skb;
  207. int use;
  208. uint32_t *temp;
  209. if (!audpkt_dev) {
  210. AUDIO_PKT_ERR("invalid device handle\n");
  211. return -EINVAL;
  212. }
  213. mutex_lock(&ap_priv->lock);
  214. if (AUDIO_PKT_PROBED != ap_priv->status)
  215. {
  216. mutex_unlock(&ap_priv->lock);
  217. AUDIO_PKT_ERR("dev is in reset\n");
  218. return -ENETRESET;
  219. }
  220. mutex_unlock(&ap_priv->lock);
  221. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  222. /* Wait for data in the queue */
  223. if (skb_queue_empty(&audpkt_dev->queue)) {
  224. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  225. if (file->f_flags & O_NONBLOCK)
  226. return -EAGAIN;
  227. /* Wait until we get data or the endpoint goes away */
  228. if (wait_event_interruptible(audpkt_dev->readq,
  229. !skb_queue_empty(&audpkt_dev->queue)))
  230. return -ERESTARTSYS;
  231. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  232. }
  233. skb = skb_dequeue(&audpkt_dev->queue);
  234. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  235. if (!skb)
  236. return -EFAULT;
  237. use = min_t(size_t, count, skb->len);
  238. if (copy_to_user(buf, skb->data, use))
  239. use = -EFAULT;
  240. temp = (uint32_t *) skb->data;
  241. kfree_skb(skb);
  242. return use;
  243. }
  244. /**
  245. * audpkt_update_physical_addr - Update physical address
  246. * audpkt_hdr: Pointer to the file structure.
  247. */
  248. int audpkt_chk_and_update_physical_addr(struct audio_gpr_pkt *gpr_pkt)
  249. {
  250. int ret = 0;
  251. dma_addr_t paddr;
  252. if (gpr_pkt->audpkt_mem_map.mmap_header.property_flag &
  253. APM_MEMORY_MAP_BIT_MASK_IS_OFFSET_MODE) {
  254. ret = msm_audio_get_phy_addr(
  255. (int) gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_lsw,
  256. &paddr);
  257. if (ret < 0) {
  258. AUDIO_PKT_ERR("%s Get phy. address failed, ret %d\n",
  259. __func__, ret);
  260. return ret;
  261. }
  262. AUDIO_PKT_INFO("%s physical address %pK", __func__,
  263. (void *) paddr);
  264. gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_lsw = (uint32_t) paddr;
  265. gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_msw = (uint64_t) paddr >> 32;
  266. }
  267. return ret;
  268. }
  269. /**
  270. * audio_pkt_write() - write() syscall for the audio_pkt device
  271. * file: Pointer to the file structure.
  272. * buf: Pointer to the userspace buffer.
  273. * count: Number bytes to read from the file.
  274. * ppos: Pointer to the position into the file.
  275. *
  276. * This function is used to write the data to audio pkt device when
  277. * userspace client do a write() system call. All input arguments are
  278. * validated by the virtual file system before calling this function.
  279. */
  280. ssize_t audio_pkt_write(struct file *file, const char __user *buf,
  281. size_t count, loff_t *ppos)
  282. {
  283. struct audio_pkt_priv *ap_priv = file->private_data;
  284. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  285. struct gpr_hdr *audpkt_hdr = NULL;
  286. void *kbuf;
  287. int ret;
  288. if (!audpkt_dev) {
  289. AUDIO_PKT_ERR("invalid device handle\n");
  290. return -EINVAL;
  291. }
  292. mutex_lock(&ap_priv->lock);
  293. if (AUDIO_PKT_PROBED != ap_priv->status)
  294. {
  295. mutex_unlock(&ap_priv->lock);
  296. AUDIO_PKT_ERR("dev is in reset\n");
  297. return -ENETRESET;
  298. }
  299. mutex_unlock(&ap_priv->lock);
  300. kbuf = memdup_user(buf, count);
  301. if (IS_ERR(kbuf))
  302. return PTR_ERR(kbuf);
  303. audpkt_hdr = (struct gpr_hdr *) kbuf;
  304. if (audpkt_hdr->opcode == APM_CMD_SHARED_MEM_MAP_REGIONS) {
  305. ret = audpkt_chk_and_update_physical_addr((struct audio_gpr_pkt *) audpkt_hdr);
  306. if (ret < 0) {
  307. AUDIO_PKT_ERR("Update Physical Address Failed -%d\n", ret);
  308. return ret;
  309. }
  310. }
  311. if (mutex_lock_interruptible(&audpkt_dev->lock)) {
  312. ret = -ERESTARTSYS;
  313. goto free_kbuf;
  314. }
  315. ret = gpr_send_pkt(ap_priv->adev,(struct gpr_pkt *) kbuf);
  316. if (ret < 0) {
  317. AUDIO_PKT_ERR("APR Send Packet Failed ret -%d\n", ret);
  318. mutex_unlock(&audpkt_dev->lock);
  319. return ret;
  320. }
  321. mutex_unlock(&audpkt_dev->lock);
  322. free_kbuf:
  323. kfree(kbuf);
  324. return ret < 0 ? ret : count;
  325. }
  326. /**
  327. * audio_pkt_poll() - poll() syscall for the audio_pkt device
  328. * file: Pointer to the file structure.
  329. * wait: pointer to Poll table.
  330. *
  331. * This function is used to poll on the audio pkt device when
  332. * userspace client do a poll() system call. All input arguments are
  333. * validated by the virtual file system before calling this function.
  334. */
  335. static unsigned int audio_pkt_poll(struct file *file, poll_table *wait)
  336. {
  337. struct audio_pkt_priv *ap_priv = file->private_data;
  338. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  339. unsigned int mask = 0;
  340. unsigned long flags;
  341. if (!audpkt_dev) {
  342. AUDIO_PKT_ERR("invalid device handle\n");
  343. return POLLERR;
  344. }
  345. poll_wait(file, &audpkt_dev->readq, wait);
  346. mutex_lock(&audpkt_dev->lock);
  347. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  348. if (!skb_queue_empty(&audpkt_dev->queue))
  349. mask |= POLLIN | POLLRDNORM;
  350. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  351. mutex_unlock(&audpkt_dev->lock);
  352. return mask;
  353. }
  354. static const struct file_operations audio_pkt_fops = {
  355. .owner = THIS_MODULE,
  356. .open = audio_pkt_open,
  357. .release = audio_pkt_release,
  358. .read = audio_pkt_read,
  359. .write = audio_pkt_write,
  360. .poll = audio_pkt_poll,
  361. };
  362. /**
  363. * audio_pkt_srvc_callback() - Callback from gpr driver
  364. * adev: pointer to the gpr device of this audio packet device
  365. * data: APR response data packet
  366. *
  367. * return: 0 for success, Standard Linux errors
  368. */
  369. static int audio_pkt_srvc_callback(struct gpr_device *adev,
  370. void *data)
  371. {
  372. struct audio_pkt_priv *ap_priv = dev_get_drvdata(&adev->dev);
  373. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  374. unsigned long flags;
  375. struct sk_buff *skb;
  376. struct gpr_hdr *hdr = (struct gpr_hdr *)data;
  377. uint16_t hdr_size, pkt_size;
  378. hdr_size = GPR_PKT_GET_HEADER_BYTE_SIZE(hdr->header);
  379. pkt_size = GPR_PKT_GET_PACKET_BYTE_SIZE(hdr->header);
  380. AUDIO_PKT_INFO("%s: header %d packet %d \n",
  381. __func__,hdr_size, pkt_size);
  382. skb = alloc_skb(pkt_size, GFP_ATOMIC);
  383. if (!skb)
  384. return -ENOMEM;
  385. skb_put_data(skb, data, pkt_size);
  386. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  387. skb_queue_tail(&audpkt_dev->queue, skb);
  388. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  389. /* wake up any blocking processes, waiting for new data */
  390. wake_up_interruptible(&audpkt_dev->readq);
  391. return 0;
  392. }
  393. /**
  394. * audio_pkt_probe() - Probe a AUDIO packet device
  395. *
  396. * adev: Pointer to gpr device.
  397. *
  398. * return: 0 on success, standard Linux error codes on error.
  399. *
  400. * This function is called when the underlying device tree driver registers
  401. * a gpr device, mapped to a Audio packet device.
  402. */
  403. static int audio_pkt_probe(struct gpr_device *adev)
  404. {
  405. if(ap_priv)
  406. {
  407. mutex_lock(&ap_priv->lock);
  408. ap_priv->adev = adev;
  409. ap_priv->status = AUDIO_PKT_PROBED;
  410. mutex_unlock(&ap_priv->lock);
  411. dev_set_drvdata(&adev->dev, ap_priv);
  412. dev_dbg(&adev->dev, "%s: Driver[%s] Probed\n",
  413. __func__, adev->name);
  414. }
  415. else
  416. {
  417. dev_err(&adev->dev, "%s: Driver[%s] Probe Failed\n",
  418. __func__, adev->name);
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. /**
  424. * audio_pkt_remove() - Remove a AUDIO packet device
  425. *
  426. * adev: Pointer to gpr device.
  427. *
  428. * return: 0 on success, standard Linux error codes on error.
  429. *
  430. * This function is called when the underlying device tree driver
  431. * removeds a gpr device, mapped to a Audio packet device.
  432. */
  433. static int audio_pkt_remove(struct gpr_device *adev)
  434. {
  435. if(ap_priv)
  436. {
  437. mutex_lock(&ap_priv->lock);
  438. ap_priv->adev = NULL;
  439. ap_priv->status = AUDIO_PKT_REMOVED;
  440. mutex_unlock(&ap_priv->lock);
  441. dev_dbg(&adev->dev, "%s: Driver[%s] Removing\n",
  442. __func__, adev->name);
  443. dev_set_drvdata(&adev->dev, NULL);
  444. }
  445. else
  446. {
  447. dev_err(&adev->dev, "%s: Driver[%s] Remove Failed\n",
  448. __func__, adev->name);
  449. return -EINVAL;
  450. }
  451. return 0;
  452. }
  453. static const struct of_device_id audio_pkt_match_table[] = {
  454. { .compatible = "qcom,audio-pkt" },
  455. {}
  456. };
  457. MODULE_DEVICE_TABLE(of, audio_pkt_match_table);
  458. static struct gpr_driver audio_pkt_driver = {
  459. .probe = audio_pkt_probe,
  460. .remove = audio_pkt_remove,
  461. .callback = audio_pkt_srvc_callback,
  462. .driver = {
  463. .name = MODULE_NAME,
  464. .of_match_table = of_match_ptr(audio_pkt_match_table),
  465. },
  466. };
  467. static int audio_pkt_plaform_driver_register_gpr(struct platform_device *pdev,
  468. struct audio_pkt_device *audpkt_dev)
  469. {
  470. int ret = 0;
  471. ap_priv = devm_kzalloc(&pdev->dev,
  472. sizeof(*ap_priv), GFP_KERNEL);
  473. if (!ap_priv)
  474. return -ENOMEM;
  475. ret = gpr_driver_register(&audio_pkt_driver);
  476. if (ret < 0) {
  477. dev_err(&pdev->dev, "%s: registering to gpr driver failed, err = %d\n",
  478. __func__, ret);
  479. goto err;
  480. }
  481. mutex_init(&ap_priv->lock);
  482. ap_priv->status = AUDIO_PKT_INIT;
  483. ap_priv->ap_dev = audpkt_dev;
  484. ap_priv->dev = audpkt_dev->dev;
  485. err:
  486. return ret;
  487. }
  488. /**
  489. * audio_pkt_platform_driver_probe() - Probe a AUDIO packet device
  490. *
  491. * adev: Pointer to platform device.
  492. *
  493. * return: 0 on success, standard Linux error codes on error.
  494. *
  495. * This function is called when the underlying device tree driver registers
  496. * a platform device, mapped to a Audio packet device.
  497. */
  498. static int audio_pkt_platform_driver_probe(struct platform_device *pdev)
  499. {
  500. int ret;
  501. struct audio_pkt_device *audpkt_dev;
  502. audpkt_dev = devm_kzalloc(&pdev->dev, sizeof(*audpkt_dev), GFP_KERNEL);
  503. if (!audpkt_dev)
  504. return -ENOMEM;
  505. ret = alloc_chrdev_region(&audpkt_dev->audio_pkt_major, 0,
  506. MINOR_NUMBER_COUNT,AUDPKT_DRIVER_NAME);
  507. if (ret < 0) {
  508. AUDIO_PKT_ERR("alloc_chrdev_region failed ret:%d\n", ret);
  509. goto err_chrdev;
  510. }
  511. audpkt_dev->audio_pkt_class = class_create(THIS_MODULE,
  512. AUDPKT_DRIVER_NAME);
  513. if (IS_ERR(audpkt_dev->audio_pkt_class)) {
  514. ret = PTR_ERR(audpkt_dev->audio_pkt_class);
  515. AUDIO_PKT_ERR("class_create failed ret:%ld\n",
  516. PTR_ERR(audpkt_dev->audio_pkt_class));
  517. goto err_class;
  518. }
  519. audpkt_dev->dev = device_create(audpkt_dev->audio_pkt_class, NULL,
  520. audpkt_dev->audio_pkt_major, NULL,
  521. AUDPKT_DRIVER_NAME);
  522. if (IS_ERR(audpkt_dev->dev)) {
  523. ret = PTR_ERR(audpkt_dev->dev);
  524. AUDIO_PKT_ERR("device_create failed ret:%ld\n",
  525. PTR_ERR(audpkt_dev->dev));
  526. goto err_device;
  527. }
  528. strlcpy(audpkt_dev->dev_name, AUDPKT_DRIVER_NAME, 20);
  529. strlcpy(audpkt_dev->ch_name, CHANNEL_NAME, 20);
  530. dev_set_name(audpkt_dev->dev, audpkt_dev->dev_name);
  531. mutex_init(&audpkt_dev->lock);
  532. spin_lock_init(&audpkt_dev->queue_lock);
  533. skb_queue_head_init(&audpkt_dev->queue);
  534. init_waitqueue_head(&audpkt_dev->readq);
  535. cdev_init(&audpkt_dev->cdev, &audio_pkt_fops);
  536. audpkt_dev->cdev.owner = THIS_MODULE;
  537. ret = cdev_add(&audpkt_dev->cdev, audpkt_dev->audio_pkt_major,
  538. MINOR_NUMBER_COUNT);
  539. if (ret) {
  540. AUDIO_PKT_ERR("cdev_add failed for %s ret:%d\n",
  541. audpkt_dev->dev_name, ret);
  542. goto free_dev;
  543. }
  544. ret = audio_pkt_plaform_driver_register_gpr(pdev, audpkt_dev);
  545. if (ret < 0) {
  546. dev_err(&pdev->dev, "%s: Failed to register with gpr, err = %d\n",
  547. __func__, ret);
  548. goto free_dev;
  549. }
  550. platform_set_drvdata(pdev, ap_priv);
  551. AUDIO_PKT_INFO("Audio Packet Port Driver Initialized\n");
  552. goto done;
  553. //return of_platform_populate(dev->of_node, NULL, NULL, dev);
  554. free_dev:
  555. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  556. err_device:
  557. class_destroy(audpkt_dev->audio_pkt_class);
  558. err_class:
  559. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  560. MINOR_NUMBER_COUNT);
  561. err_chrdev:
  562. done:
  563. return ret;
  564. }
  565. /**
  566. * audio_pkt_platform_driver_remove() - Remove a AUDIO packet platform device
  567. *
  568. * adev: Pointer to platform device.
  569. *
  570. * return: 0 on success, standard Linux error codes on error.
  571. *
  572. * This function is called when the underlying device tree driver
  573. * removes a platform device, mapped to a Audio packet device.
  574. */
  575. static int audio_pkt_platform_driver_remove(struct platform_device *adev)
  576. {
  577. struct audio_pkt_priv *ap_priv = platform_get_drvdata(adev);
  578. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  579. gpr_driver_unregister(&audio_pkt_driver);
  580. audio_pkt_internal_release(adev);
  581. if (audpkt_dev) {
  582. cdev_del(&audpkt_dev->cdev);
  583. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  584. class_destroy(audpkt_dev->audio_pkt_class);
  585. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  586. MINOR_NUMBER_COUNT);
  587. }
  588. //of_platform_depopulate(&adev->dev);
  589. AUDIO_PKT_INFO("Audio Packet Port Driver Removed\n");
  590. return 0;
  591. }
  592. static const struct of_device_id audio_pkt_platform_match_table[] = {
  593. { .compatible = "qcom,audio-pkt-core-platform"},
  594. {}
  595. };
  596. MODULE_DEVICE_TABLE(of, audio_pkt_platform_match_table);
  597. static struct platform_driver audio_pkt_core_platform_driver = {
  598. .probe = audio_pkt_platform_driver_probe,
  599. .remove = audio_pkt_platform_driver_remove,
  600. .driver = {
  601. .name = MODULE_NAME,
  602. .of_match_table = of_match_ptr(audio_pkt_platform_match_table),
  603. },
  604. };
  605. static int __init audio_pkt_init(void)
  606. {
  607. return platform_driver_register(&audio_pkt_core_platform_driver);
  608. }
  609. static void __exit audio_pkt_exit(void)
  610. {
  611. platform_driver_unregister(&audio_pkt_core_platform_driver);
  612. }
  613. module_init(audio_pkt_init);
  614. module_exit(audio_pkt_exit);
  615. MODULE_DESCRIPTION("MSM Audio Packet Driver");
  616. MODULE_LICENSE("GPL v2");