audio-pkt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. if (gpr_get_q6_state() != GPR_SUBSYS_DOWN)
  346. ret = gpr_send_pkt(ap_priv->adev, (struct gpr_pkt *) kbuf);
  347. else
  348. AUDIO_PKT_ERR("q6 is down\n");
  349. if (ret < 0) {
  350. AUDIO_PKT_ERR("APR Send Packet Failed ret -%d\n", ret);
  351. if (ret == -ECONNRESET)
  352. ret = -ENETRESET;
  353. }
  354. mutex_unlock(&audpkt_dev->lock);
  355. free_kbuf:
  356. kfree(kbuf);
  357. return ret < 0 ? ret : count;
  358. }
  359. /**
  360. * audio_pkt_poll() - poll() syscall for the audio_pkt device
  361. * file: Pointer to the file structure.
  362. * wait: pointer to Poll table.
  363. *
  364. * This function is used to poll on the audio pkt device when
  365. * userspace client do a poll() system call. All input arguments are
  366. * validated by the virtual file system before calling this function.
  367. */
  368. static unsigned int audio_pkt_poll(struct file *file, poll_table *wait)
  369. {
  370. struct audio_pkt_priv *ap_priv = file->private_data;
  371. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  372. unsigned int mask = 0;
  373. unsigned long flags;
  374. if (!audpkt_dev) {
  375. AUDIO_PKT_ERR("invalid device handle\n");
  376. return POLLERR;
  377. }
  378. poll_wait(file, &audpkt_dev->readq, wait);
  379. mutex_lock(&audpkt_dev->lock);
  380. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  381. if (!skb_queue_empty(&audpkt_dev->queue))
  382. mask |= POLLIN | POLLRDNORM;
  383. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  384. mutex_unlock(&audpkt_dev->lock);
  385. return mask;
  386. }
  387. static const struct file_operations audio_pkt_fops = {
  388. .owner = THIS_MODULE,
  389. .open = audio_pkt_open,
  390. .release = audio_pkt_release,
  391. .read = audio_pkt_read,
  392. .write = audio_pkt_write,
  393. .poll = audio_pkt_poll,
  394. };
  395. /**
  396. * audio_pkt_srvc_callback() - Callback from gpr driver
  397. * adev: pointer to the gpr device of this audio packet device
  398. * data: APR response data packet
  399. *
  400. * return: 0 for success, Standard Linux errors
  401. */
  402. static int audio_pkt_srvc_callback(struct gpr_device *adev,
  403. void *data)
  404. {
  405. struct audio_pkt_priv *ap_priv = dev_get_drvdata(&adev->dev);
  406. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  407. unsigned long flags;
  408. struct sk_buff *skb;
  409. struct gpr_hdr *hdr = (struct gpr_hdr *)data;
  410. uint16_t hdr_size, pkt_size;
  411. hdr_size = GPR_PKT_GET_HEADER_BYTE_SIZE(hdr->header);
  412. pkt_size = GPR_PKT_GET_PACKET_BYTE_SIZE(hdr->header);
  413. AUDIO_PKT_INFO("%s: header %d packet %d \n",
  414. __func__,hdr_size, pkt_size);
  415. skb = alloc_skb(pkt_size, GFP_ATOMIC);
  416. if (!skb)
  417. return -ENOMEM;
  418. skb_put_data(skb, data, pkt_size);
  419. spin_lock_irqsave(&audpkt_dev->queue_lock, flags);
  420. skb_queue_tail(&audpkt_dev->queue, skb);
  421. spin_unlock_irqrestore(&audpkt_dev->queue_lock, flags);
  422. /* wake up any blocking processes, waiting for new data */
  423. wake_up_interruptible(&audpkt_dev->readq);
  424. return 0;
  425. }
  426. /**
  427. * audio_pkt_probe() - Probe a AUDIO packet device
  428. *
  429. * adev: Pointer to gpr device.
  430. *
  431. * return: 0 on success, standard Linux error codes on error.
  432. *
  433. * This function is called when the underlying device tree driver registers
  434. * a gpr device, mapped to a Audio packet device.
  435. */
  436. static int audio_pkt_probe(struct gpr_device *adev)
  437. {
  438. if(ap_priv)
  439. {
  440. mutex_lock(&ap_priv->lock);
  441. ap_priv->adev = adev;
  442. ap_priv->status = AUDIO_PKT_PROBED;
  443. mutex_unlock(&ap_priv->lock);
  444. dev_set_drvdata(&adev->dev, ap_priv);
  445. dev_dbg(&adev->dev, "%s: Driver[%s] Probed\n",
  446. __func__, adev->name);
  447. }
  448. else
  449. {
  450. dev_err(&adev->dev, "%s: Driver[%s] Probe Failed\n",
  451. __func__, adev->name);
  452. return -EINVAL;
  453. }
  454. return 0;
  455. }
  456. /**
  457. * audio_pkt_remove() - Remove a AUDIO packet device
  458. *
  459. * adev: Pointer to gpr device.
  460. *
  461. * return: 0 on success, standard Linux error codes on error.
  462. *
  463. * This function is called when the underlying device tree driver
  464. * removeds a gpr device, mapped to a Audio packet device.
  465. */
  466. static int audio_pkt_remove(struct gpr_device *adev)
  467. {
  468. if(ap_priv)
  469. {
  470. mutex_lock(&ap_priv->lock);
  471. ap_priv->adev = NULL;
  472. ap_priv->status = AUDIO_PKT_REMOVED;
  473. mutex_unlock(&ap_priv->lock);
  474. dev_dbg(&adev->dev, "%s: Driver[%s] Removing\n",
  475. __func__, adev->name);
  476. dev_set_drvdata(&adev->dev, NULL);
  477. }
  478. else
  479. {
  480. dev_err(&adev->dev, "%s: Driver[%s] Remove Failed\n",
  481. __func__, adev->name);
  482. return -EINVAL;
  483. }
  484. return 0;
  485. }
  486. static const struct of_device_id audio_pkt_match_table[] = {
  487. { .compatible = "qcom,audio-pkt" },
  488. {}
  489. };
  490. MODULE_DEVICE_TABLE(of, audio_pkt_match_table);
  491. static struct gpr_driver audio_pkt_driver = {
  492. .probe = audio_pkt_probe,
  493. .remove = audio_pkt_remove,
  494. .callback = audio_pkt_srvc_callback,
  495. .driver = {
  496. .name = MODULE_NAME,
  497. .of_match_table = of_match_ptr(audio_pkt_match_table),
  498. },
  499. };
  500. static int audio_pkt_plaform_driver_register_gpr(struct platform_device *pdev,
  501. struct audio_pkt_device *audpkt_dev)
  502. {
  503. int ret = 0;
  504. ap_priv = devm_kzalloc(&pdev->dev,
  505. sizeof(*ap_priv), GFP_KERNEL);
  506. if (!ap_priv)
  507. return -ENOMEM;
  508. ret = gpr_driver_register(&audio_pkt_driver);
  509. if (ret < 0) {
  510. dev_err(&pdev->dev, "%s: registering to gpr driver failed, err = %d\n",
  511. __func__, ret);
  512. goto err;
  513. }
  514. mutex_init(&ap_priv->lock);
  515. ap_priv->status = AUDIO_PKT_INIT;
  516. ap_priv->ap_dev = audpkt_dev;
  517. ap_priv->dev = audpkt_dev->dev;
  518. err:
  519. return ret;
  520. }
  521. /**
  522. * audio_pkt_platform_driver_probe() - Probe a AUDIO packet device
  523. *
  524. * adev: Pointer to platform device.
  525. *
  526. * return: 0 on success, standard Linux error codes on error.
  527. *
  528. * This function is called when the underlying device tree driver registers
  529. * a platform device, mapped to a Audio packet device.
  530. */
  531. static int audio_pkt_platform_driver_probe(struct platform_device *pdev)
  532. {
  533. int ret;
  534. struct audio_pkt_device *audpkt_dev;
  535. audpkt_dev = devm_kzalloc(&pdev->dev, sizeof(*audpkt_dev), GFP_KERNEL);
  536. if (!audpkt_dev)
  537. return -ENOMEM;
  538. ret = alloc_chrdev_region(&audpkt_dev->audio_pkt_major, 0,
  539. MINOR_NUMBER_COUNT,AUDPKT_DRIVER_NAME);
  540. if (ret < 0) {
  541. AUDIO_PKT_ERR("alloc_chrdev_region failed ret:%d\n", ret);
  542. goto err_chrdev;
  543. }
  544. audpkt_dev->audio_pkt_class = class_create(THIS_MODULE,
  545. AUDPKT_DRIVER_NAME);
  546. if (IS_ERR(audpkt_dev->audio_pkt_class)) {
  547. ret = PTR_ERR(audpkt_dev->audio_pkt_class);
  548. AUDIO_PKT_ERR("class_create failed ret:%ld\n",
  549. PTR_ERR(audpkt_dev->audio_pkt_class));
  550. goto err_class;
  551. }
  552. audpkt_dev->dev = device_create(audpkt_dev->audio_pkt_class, NULL,
  553. audpkt_dev->audio_pkt_major, NULL,
  554. AUDPKT_DRIVER_NAME);
  555. if (IS_ERR(audpkt_dev->dev)) {
  556. ret = PTR_ERR(audpkt_dev->dev);
  557. AUDIO_PKT_ERR("device_create failed ret:%ld\n",
  558. PTR_ERR(audpkt_dev->dev));
  559. goto err_device;
  560. }
  561. strlcpy(audpkt_dev->dev_name, AUDPKT_DRIVER_NAME, 20);
  562. strlcpy(audpkt_dev->ch_name, CHANNEL_NAME, 20);
  563. dev_set_name(audpkt_dev->dev, audpkt_dev->dev_name);
  564. mutex_init(&audpkt_dev->lock);
  565. spin_lock_init(&audpkt_dev->queue_lock);
  566. skb_queue_head_init(&audpkt_dev->queue);
  567. init_waitqueue_head(&audpkt_dev->readq);
  568. cdev_init(&audpkt_dev->cdev, &audio_pkt_fops);
  569. audpkt_dev->cdev.owner = THIS_MODULE;
  570. ret = cdev_add(&audpkt_dev->cdev, audpkt_dev->audio_pkt_major,
  571. MINOR_NUMBER_COUNT);
  572. if (ret) {
  573. AUDIO_PKT_ERR("cdev_add failed for %s ret:%d\n",
  574. audpkt_dev->dev_name, ret);
  575. goto free_dev;
  576. }
  577. ret = audio_pkt_plaform_driver_register_gpr(pdev, audpkt_dev);
  578. if (ret < 0) {
  579. dev_err(&pdev->dev, "%s: Failed to register with gpr, err = %d\n",
  580. __func__, ret);
  581. goto free_dev;
  582. }
  583. platform_set_drvdata(pdev, ap_priv);
  584. AUDIO_PKT_INFO("Audio Packet Port Driver Initialized\n");
  585. goto done;
  586. //return of_platform_populate(dev->of_node, NULL, NULL, dev);
  587. free_dev:
  588. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  589. err_device:
  590. class_destroy(audpkt_dev->audio_pkt_class);
  591. err_class:
  592. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  593. MINOR_NUMBER_COUNT);
  594. err_chrdev:
  595. done:
  596. return ret;
  597. }
  598. /**
  599. * audio_pkt_platform_driver_remove() - Remove a AUDIO packet platform device
  600. *
  601. * adev: Pointer to platform device.
  602. *
  603. * return: 0 on success, standard Linux error codes on error.
  604. *
  605. * This function is called when the underlying device tree driver
  606. * removes a platform device, mapped to a Audio packet device.
  607. */
  608. static int audio_pkt_platform_driver_remove(struct platform_device *adev)
  609. {
  610. struct audio_pkt_priv *ap_priv = platform_get_drvdata(adev);
  611. struct audio_pkt_device *audpkt_dev = ap_priv->ap_dev;
  612. gpr_driver_unregister(&audio_pkt_driver);
  613. audio_pkt_internal_release(adev);
  614. if (audpkt_dev) {
  615. cdev_del(&audpkt_dev->cdev);
  616. device_destroy(audpkt_dev->audio_pkt_class,audpkt_dev->audio_pkt_major);
  617. class_destroy(audpkt_dev->audio_pkt_class);
  618. unregister_chrdev_region(MAJOR(audpkt_dev->audio_pkt_major),
  619. MINOR_NUMBER_COUNT);
  620. }
  621. //of_platform_depopulate(&adev->dev);
  622. AUDIO_PKT_INFO("Audio Packet Port Driver Removed\n");
  623. return 0;
  624. }
  625. static const struct of_device_id audio_pkt_platform_match_table[] = {
  626. { .compatible = "qcom,audio-pkt-core-platform"},
  627. {}
  628. };
  629. MODULE_DEVICE_TABLE(of, audio_pkt_platform_match_table);
  630. static struct platform_driver audio_pkt_core_platform_driver = {
  631. .probe = audio_pkt_platform_driver_probe,
  632. .remove = audio_pkt_platform_driver_remove,
  633. .driver = {
  634. .name = MODULE_NAME,
  635. .of_match_table = of_match_ptr(audio_pkt_platform_match_table),
  636. },
  637. };
  638. static int __init audio_pkt_init(void)
  639. {
  640. return platform_driver_register(&audio_pkt_core_platform_driver);
  641. }
  642. static void __exit audio_pkt_exit(void)
  643. {
  644. platform_driver_unregister(&audio_pkt_core_platform_driver);
  645. }
  646. module_init(audio_pkt_init);
  647. module_exit(audio_pkt_exit);
  648. MODULE_DESCRIPTION("MSM Audio Packet Driver");
  649. MODULE_LICENSE("GPL v2");