audio-pkt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* Copyright (c) 2019-2021, 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. size_t pa_len = 0;
  252. dma_addr_t paddr;
  253. if (gpr_pkt->audpkt_mem_map.mmap_header.property_flag &
  254. APM_MEMORY_MAP_BIT_MASK_IS_OFFSET_MODE) {
  255. ret = msm_audio_get_phy_addr(
  256. (int) gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_lsw,
  257. &paddr, &pa_len);
  258. if (ret < 0) {
  259. AUDIO_PKT_ERR("%s Get phy. address failed, ret %d\n",
  260. __func__, ret);
  261. return ret;
  262. }
  263. AUDIO_PKT_INFO("%s physical address %pK", __func__,
  264. (void *) paddr);
  265. gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_lsw = (uint32_t) paddr;
  266. gpr_pkt->audpkt_mem_map.mmap_payload.shm_addr_msw = (uint64_t) paddr >> 32;
  267. }
  268. return ret;
  269. }
  270. /**
  271. * audio_pkt_write() - write() syscall for the audio_pkt device
  272. * file: Pointer to the file structure.
  273. * buf: Pointer to the userspace buffer.
  274. * count: Number bytes to read from the file.
  275. * ppos: Pointer to the position into the file.
  276. *
  277. * This function is used to write the data to audio pkt device when
  278. * userspace client do a write() system call. All input arguments are
  279. * validated by the virtual file system before calling this function.
  280. */
  281. ssize_t audio_pkt_write(struct file *file, const char __user *buf,
  282. size_t count, loff_t *ppos)
  283. {
  284. struct audio_pkt_priv *ap_priv = file->private_data;
  285. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  286. struct gpr_hdr *audpkt_hdr = NULL;
  287. void *kbuf;
  288. int ret;
  289. if (!audpkt_dev) {
  290. AUDIO_PKT_ERR("invalid device handle\n");
  291. return -EINVAL;
  292. }
  293. mutex_lock(&ap_priv->lock);
  294. if (AUDIO_PKT_PROBED != ap_priv->status)
  295. {
  296. mutex_unlock(&ap_priv->lock);
  297. AUDIO_PKT_ERR("dev is in reset\n");
  298. return -ENETRESET;
  299. }
  300. mutex_unlock(&ap_priv->lock);
  301. kbuf = memdup_user(buf, count);
  302. if (IS_ERR(kbuf))
  303. return PTR_ERR(kbuf);
  304. audpkt_hdr = (struct gpr_hdr *) kbuf;
  305. if (audpkt_hdr->opcode == APM_CMD_SHARED_MEM_MAP_REGIONS) {
  306. ret = audpkt_chk_and_update_physical_addr((struct audio_gpr_pkt *) audpkt_hdr);
  307. if (ret < 0) {
  308. AUDIO_PKT_ERR("Update Physical Address Failed -%d\n", ret);
  309. return ret;
  310. }
  311. }
  312. if (mutex_lock_interruptible(&audpkt_dev->lock)) {
  313. ret = -ERESTARTSYS;
  314. goto free_kbuf;
  315. }
  316. ret = gpr_send_pkt(ap_priv->adev,(struct gpr_pkt *) kbuf);
  317. if (ret < 0) {
  318. AUDIO_PKT_ERR("APR Send Packet Failed ret -%d\n", ret);
  319. mutex_unlock(&audpkt_dev->lock);
  320. return ret;
  321. }
  322. mutex_unlock(&audpkt_dev->lock);
  323. free_kbuf:
  324. kfree(kbuf);
  325. return ret < 0 ? ret : count;
  326. }
  327. /**
  328. * audio_pkt_poll() - poll() syscall for the audio_pkt device
  329. * file: Pointer to the file structure.
  330. * wait: pointer to Poll table.
  331. *
  332. * This function is used to poll on the audio pkt device when
  333. * userspace client do a poll() system call. All input arguments are
  334. * validated by the virtual file system before calling this function.
  335. */
  336. static unsigned int audio_pkt_poll(struct file *file, poll_table *wait)
  337. {
  338. struct audio_pkt_priv *ap_priv = file->private_data;
  339. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  340. unsigned int mask = 0;
  341. unsigned long flags;
  342. if (!audpkt_dev) {
  343. AUDIO_PKT_ERR("invalid device handle\n");
  344. return POLLERR;
  345. }
  346. poll_wait(file, &audpkt_dev->readq, wait);
  347. mutex_lock(&audpkt_dev->lock);
  348. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  349. if (!skb_queue_empty(&audpkt_dev->queue))
  350. mask |= POLLIN | POLLRDNORM;
  351. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  352. mutex_unlock(&audpkt_dev->lock);
  353. return mask;
  354. }
  355. static const struct file_operations audio_pkt_fops = {
  356. .owner = THIS_MODULE,
  357. .open = audio_pkt_open,
  358. .release = audio_pkt_release,
  359. .read = audio_pkt_read,
  360. .write = audio_pkt_write,
  361. .poll = audio_pkt_poll,
  362. };
  363. /**
  364. * audio_pkt_srvc_callback() - Callback from gpr driver
  365. * adev: pointer to the gpr device of this audio packet device
  366. * data: APR response data packet
  367. *
  368. * return: 0 for success, Standard Linux errors
  369. */
  370. static int audio_pkt_srvc_callback(struct gpr_device *adev,
  371. void *data)
  372. {
  373. struct audio_pkt_priv *ap_priv = dev_get_drvdata(&adev->dev);
  374. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  375. unsigned long flags;
  376. struct sk_buff *skb;
  377. struct gpr_hdr *hdr = (struct gpr_hdr *)data;
  378. uint16_t hdr_size, pkt_size;
  379. hdr_size = GPR_PKT_GET_HEADER_BYTE_SIZE(hdr->header);
  380. pkt_size = GPR_PKT_GET_PACKET_BYTE_SIZE(hdr->header);
  381. AUDIO_PKT_INFO("%s: header %d packet %d \n",
  382. __func__,hdr_size, pkt_size);
  383. skb = alloc_skb(pkt_size, GFP_ATOMIC);
  384. if (!skb)
  385. return -ENOMEM;
  386. skb_put_data(skb, data, pkt_size);
  387. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  388. skb_queue_tail(&audpkt_dev->queue, skb);
  389. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  390. /* wake up any blocking processes, waiting for new data */
  391. wake_up_interruptible(&audpkt_dev->readq);
  392. return 0;
  393. }
  394. /**
  395. * audio_pkt_probe() - Probe a AUDIO packet device
  396. *
  397. * adev: Pointer to gpr device.
  398. *
  399. * return: 0 on success, standard Linux error codes on error.
  400. *
  401. * This function is called when the underlying device tree driver registers
  402. * a gpr device, mapped to a Audio packet device.
  403. */
  404. static int audio_pkt_probe(struct gpr_device *adev)
  405. {
  406. if(ap_priv)
  407. {
  408. mutex_lock(&ap_priv->lock);
  409. ap_priv->adev = adev;
  410. ap_priv->status = AUDIO_PKT_PROBED;
  411. mutex_unlock(&ap_priv->lock);
  412. dev_set_drvdata(&adev->dev, ap_priv);
  413. dev_dbg(&adev->dev, "%s: Driver[%s] Probed\n",
  414. __func__, adev->name);
  415. }
  416. else
  417. {
  418. dev_err(&adev->dev, "%s: Driver[%s] Probe Failed\n",
  419. __func__, adev->name);
  420. return -EINVAL;
  421. }
  422. return 0;
  423. }
  424. /**
  425. * audio_pkt_remove() - Remove a AUDIO packet device
  426. *
  427. * adev: Pointer to gpr device.
  428. *
  429. * return: 0 on success, standard Linux error codes on error.
  430. *
  431. * This function is called when the underlying device tree driver
  432. * removeds a gpr device, mapped to a Audio packet device.
  433. */
  434. static int audio_pkt_remove(struct gpr_device *adev)
  435. {
  436. if(ap_priv)
  437. {
  438. mutex_lock(&ap_priv->lock);
  439. ap_priv->adev = NULL;
  440. ap_priv->status = AUDIO_PKT_REMOVED;
  441. mutex_unlock(&ap_priv->lock);
  442. dev_dbg(&adev->dev, "%s: Driver[%s] Removing\n",
  443. __func__, adev->name);
  444. dev_set_drvdata(&adev->dev, NULL);
  445. }
  446. else
  447. {
  448. dev_err(&adev->dev, "%s: Driver[%s] Remove Failed\n",
  449. __func__, adev->name);
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static const struct of_device_id audio_pkt_match_table[] = {
  455. { .compatible = "qcom,audio-pkt" },
  456. {}
  457. };
  458. MODULE_DEVICE_TABLE(of, audio_pkt_match_table);
  459. static struct gpr_driver audio_pkt_driver = {
  460. .probe = audio_pkt_probe,
  461. .remove = audio_pkt_remove,
  462. .callback = audio_pkt_srvc_callback,
  463. .driver = {
  464. .name = MODULE_NAME,
  465. .of_match_table = of_match_ptr(audio_pkt_match_table),
  466. },
  467. };
  468. static int audio_pkt_plaform_driver_register_gpr(struct platform_device *pdev,
  469. struct audio_pkt_device *audpkt_dev)
  470. {
  471. int ret = 0;
  472. ap_priv = devm_kzalloc(&pdev->dev,
  473. sizeof(*ap_priv), GFP_KERNEL);
  474. if (!ap_priv)
  475. return -ENOMEM;
  476. ret = gpr_driver_register(&audio_pkt_driver);
  477. if (ret < 0) {
  478. dev_err(&pdev->dev, "%s: registering to gpr driver failed, err = %d\n",
  479. __func__, ret);
  480. goto err;
  481. }
  482. mutex_init(&ap_priv->lock);
  483. ap_priv->status = AUDIO_PKT_INIT;
  484. ap_priv->ap_dev = audpkt_dev;
  485. ap_priv->dev = audpkt_dev->dev;
  486. err:
  487. return ret;
  488. }
  489. /**
  490. * audio_pkt_platform_driver_probe() - Probe a AUDIO packet device
  491. *
  492. * adev: Pointer to platform device.
  493. *
  494. * return: 0 on success, standard Linux error codes on error.
  495. *
  496. * This function is called when the underlying device tree driver registers
  497. * a platform device, mapped to a Audio packet device.
  498. */
  499. static int audio_pkt_platform_driver_probe(struct platform_device *pdev)
  500. {
  501. int ret;
  502. struct audio_pkt_device *audpkt_dev;
  503. audpkt_dev = devm_kzalloc(&pdev->dev, sizeof(*audpkt_dev), GFP_KERNEL);
  504. if (!audpkt_dev)
  505. return -ENOMEM;
  506. ret = alloc_chrdev_region(&audpkt_dev->audio_pkt_major, 0,
  507. MINOR_NUMBER_COUNT,AUDPKT_DRIVER_NAME);
  508. if (ret < 0) {
  509. AUDIO_PKT_ERR("alloc_chrdev_region failed ret:%d\n", ret);
  510. goto err_chrdev;
  511. }
  512. audpkt_dev->audio_pkt_class = class_create(THIS_MODULE,
  513. AUDPKT_DRIVER_NAME);
  514. if (IS_ERR(audpkt_dev->audio_pkt_class)) {
  515. ret = PTR_ERR(audpkt_dev->audio_pkt_class);
  516. AUDIO_PKT_ERR("class_create failed ret:%ld\n",
  517. PTR_ERR(audpkt_dev->audio_pkt_class));
  518. goto err_class;
  519. }
  520. audpkt_dev->dev = device_create(audpkt_dev->audio_pkt_class, NULL,
  521. audpkt_dev->audio_pkt_major, NULL,
  522. AUDPKT_DRIVER_NAME);
  523. if (IS_ERR(audpkt_dev->dev)) {
  524. ret = PTR_ERR(audpkt_dev->dev);
  525. AUDIO_PKT_ERR("device_create failed ret:%ld\n",
  526. PTR_ERR(audpkt_dev->dev));
  527. goto err_device;
  528. }
  529. strlcpy(audpkt_dev->dev_name, AUDPKT_DRIVER_NAME, 20);
  530. strlcpy(audpkt_dev->ch_name, CHANNEL_NAME, 20);
  531. dev_set_name(audpkt_dev->dev, audpkt_dev->dev_name);
  532. mutex_init(&audpkt_dev->lock);
  533. spin_lock_init(&audpkt_dev->queue_lock);
  534. skb_queue_head_init(&audpkt_dev->queue);
  535. init_waitqueue_head(&audpkt_dev->readq);
  536. cdev_init(&audpkt_dev->cdev, &audio_pkt_fops);
  537. audpkt_dev->cdev.owner = THIS_MODULE;
  538. ret = cdev_add(&audpkt_dev->cdev, audpkt_dev->audio_pkt_major,
  539. MINOR_NUMBER_COUNT);
  540. if (ret) {
  541. AUDIO_PKT_ERR("cdev_add failed for %s ret:%d\n",
  542. audpkt_dev->dev_name, ret);
  543. goto free_dev;
  544. }
  545. ret = audio_pkt_plaform_driver_register_gpr(pdev, audpkt_dev);
  546. if (ret < 0) {
  547. dev_err(&pdev->dev, "%s: Failed to register with gpr, err = %d\n",
  548. __func__, ret);
  549. goto free_dev;
  550. }
  551. platform_set_drvdata(pdev, ap_priv);
  552. AUDIO_PKT_INFO("Audio Packet Port Driver Initialized\n");
  553. goto done;
  554. //return of_platform_populate(dev->of_node, NULL, NULL, dev);
  555. free_dev:
  556. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  557. err_device:
  558. class_destroy(audpkt_dev->audio_pkt_class);
  559. err_class:
  560. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  561. MINOR_NUMBER_COUNT);
  562. err_chrdev:
  563. done:
  564. return ret;
  565. }
  566. /**
  567. * audio_pkt_platform_driver_remove() - Remove a AUDIO packet platform device
  568. *
  569. * adev: Pointer to platform device.
  570. *
  571. * return: 0 on success, standard Linux error codes on error.
  572. *
  573. * This function is called when the underlying device tree driver
  574. * removes a platform device, mapped to a Audio packet device.
  575. */
  576. static int audio_pkt_platform_driver_remove(struct platform_device *adev)
  577. {
  578. struct audio_pkt_priv *ap_priv = platform_get_drvdata(adev);
  579. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  580. gpr_driver_unregister(&audio_pkt_driver);
  581. audio_pkt_internal_release(adev);
  582. if (audpkt_dev) {
  583. cdev_del(&audpkt_dev->cdev);
  584. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  585. class_destroy(audpkt_dev->audio_pkt_class);
  586. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  587. MINOR_NUMBER_COUNT);
  588. }
  589. //of_platform_depopulate(&adev->dev);
  590. AUDIO_PKT_INFO("Audio Packet Port Driver Removed\n");
  591. return 0;
  592. }
  593. static const struct of_device_id audio_pkt_platform_match_table[] = {
  594. { .compatible = "qcom,audio-pkt-core-platform"},
  595. {}
  596. };
  597. MODULE_DEVICE_TABLE(of, audio_pkt_platform_match_table);
  598. static struct platform_driver audio_pkt_core_platform_driver = {
  599. .probe = audio_pkt_platform_driver_probe,
  600. .remove = audio_pkt_platform_driver_remove,
  601. .driver = {
  602. .name = MODULE_NAME,
  603. .of_match_table = of_match_ptr(audio_pkt_platform_match_table),
  604. },
  605. };
  606. static int __init audio_pkt_init(void)
  607. {
  608. return platform_driver_register(&audio_pkt_core_platform_driver);
  609. }
  610. static void __exit audio_pkt_exit(void)
  611. {
  612. platform_driver_unregister(&audio_pkt_core_platform_driver);
  613. }
  614. module_init(audio_pkt_init);
  615. module_exit(audio_pkt_exit);
  616. MODULE_DESCRIPTION("MSM Audio Packet Driver");
  617. MODULE_LICENSE("GPL v2");