audio-pkt.c 20 KB

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