btfm_codec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/kdev_t.h>
  7. #include <linux/refcount.h>
  8. #include <linux/idr.h>
  9. #include <linux/cdev.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/module.h>
  14. #include "btfm_codec.h"
  15. #include "btfm_codec_pkt.h"
  16. #define dev_to_btfmcodec(_dev) container_of(_dev, struct btfmcodec_data, dev)
  17. static DEFINE_IDR(dev_minor);
  18. static struct class *dev_class;
  19. static dev_t dev_major;
  20. struct btfmcodec_data *btfmcodec;
  21. struct device_driver driver = {.name = "btfmcodec-driver", .owner = THIS_MODULE};
  22. struct btfmcodec_char_device *btfmcodec_dev;
  23. bool is_cp_supported = true;
  24. #define cdev_to_btfmchardev(_cdev) container_of(_cdev, struct btfmcodec_char_device, cdev)
  25. #define MIN_PKT_LEN 0x9
  26. char *coverttostring(enum btfmcodec_states state) {
  27. switch (state) {
  28. case IDLE:
  29. return "IDLE";
  30. break;
  31. case BT_Connected:
  32. return "BT_CONNECTED";
  33. break;
  34. case BT_Connecting:
  35. return "BT_CONNECTING";
  36. break;
  37. case BTADV_AUDIO_Connected:
  38. return "BTADV_AUDIO_CONNECTED";
  39. break;
  40. case BTADV_AUDIO_Connecting:
  41. return "BTADV_AUDIO_CONNECTING";
  42. break;
  43. default:
  44. return "INVALID_STATE";
  45. break;
  46. }
  47. }
  48. /*
  49. * btfmcodec_dev_open() - open() syscall for the btfmcodec dev node
  50. * inode: Pointer to the inode structure.
  51. * file: Pointer to the file structure.
  52. *
  53. * This function is used to open the btfmcodec char device when
  54. * userspace client do a open() system call. All input arguments are
  55. * validated by the virtual file system before calling this function.
  56. * Note: btfmcodec dev node works on nonblocking mode.
  57. */
  58. static int btfmcodec_dev_open(struct inode *inode, struct file *file)
  59. {
  60. struct btfmcodec_char_device *btfmcodec_dev = cdev_to_btfmchardev(inode->i_cdev);
  61. struct btfmcodec_data *btfmcodec = (struct btfmcodec_data *)btfmcodec_dev->btfmcodec;
  62. unsigned int active_clients = refcount_read(&btfmcodec_dev->active_clients);
  63. btfmcodec->states.current_state = IDLE; /* Just a temp*/
  64. btfmcodec->states.next_state = IDLE;
  65. BTFMCODEC_INFO("for %s by %s:%d active_clients[%d]\n",
  66. btfmcodec_dev->dev_name, current->comm,
  67. task_pid_nr(current), refcount_read(&btfmcodec_dev->active_clients));
  68. /* Don't allow a new client if already one is active. */
  69. if (active_clients > 1) {
  70. BTFMCODEC_WARN("%s: Not honoring open as other client is active", __func__);
  71. return EACCES;
  72. }
  73. /* for now have btfmcodec and later we can think of having it btfmcodec_dev */
  74. file->private_data = btfmcodec;
  75. refcount_inc(&btfmcodec_dev->active_clients);
  76. return 0;
  77. }
  78. /*
  79. * btfmcodec_pkt_release() - release operation on btfmcodec device
  80. * inode: Pointer to the inode structure.
  81. * file: Pointer to the file structure.
  82. *
  83. * This function is used to release the btfmcodec dev node when
  84. * userspace client do a close() system call. All input arguments are
  85. * validated by the virtual file system before calling this function.
  86. */
  87. static int btfmcodec_dev_release(struct inode *inode, struct file *file)
  88. {
  89. struct btfmcodec_char_device *btfmcodec_dev = cdev_to_btfmchardev(inode->i_cdev);
  90. unsigned long flags;
  91. int idx;
  92. BTFMCODEC_INFO("for %s by %s:%d active_clients[%u]\n",
  93. btfmcodec_dev->dev_name, current->comm,
  94. task_pid_nr(current), refcount_read(&btfmcodec_dev->active_clients));
  95. refcount_dec(&btfmcodec_dev->active_clients);
  96. if (refcount_read(&btfmcodec_dev->active_clients) == 1) {
  97. spin_lock_irqsave(&btfmcodec_dev->tx_queue_lock, flags);
  98. skb_queue_purge(&btfmcodec_dev->txq);
  99. /* Wakeup the device if waiting for the data */
  100. wake_up_interruptible(&btfmcodec_dev->readq);
  101. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  102. /* we need to have separte rx lock for below buff */
  103. skb_queue_purge(&btfmcodec_dev->rxq);
  104. }
  105. /* Notify waiting clients that client is closed or killed */
  106. for (idx = 0; idx < BTM_PKT_TYPE_MAX; idx++) {
  107. btfmcodec_dev->status[idx] = BTM_RSP_NOT_RECV_CLIENT_KILLED;
  108. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  109. }
  110. if (btfmcodec_dev->wq_hwep_shutdown.func)
  111. cancel_work_sync(&btfmcodec_dev->wq_hwep_shutdown);
  112. if (btfmcodec_dev->wq_hwep_configure.func)
  113. cancel_work_sync(&btfmcodec_dev->wq_hwep_configure);
  114. if (btfmcodec_dev->wq_prepare_bearer.func)
  115. cancel_work_sync(&btfmcodec_dev->wq_prepare_bearer);
  116. btfmcodec->states.current_state = IDLE;
  117. btfmcodec->states.next_state = IDLE;
  118. return 0;
  119. }
  120. btm_opcode STREAM_TO_UINT32 (struct sk_buff *skb)
  121. {
  122. return (skb->data[0] | (skb->data[1] << 8) |
  123. (skb->data[2] << 16) | (skb->data[3] << 24));
  124. }
  125. static void btfmcodec_dev_rxwork(struct work_struct *work)
  126. {
  127. struct btfmcodec_char_device *btfmcodec_dev = container_of(work, struct btfmcodec_char_device, rx_work);
  128. struct sk_buff *skb;
  129. uint32_t len;
  130. uint8_t status;
  131. int idx;
  132. uint8_t *bearer_switch_ind;
  133. BTFMCODEC_DBG("start");
  134. while ((skb = skb_dequeue(&btfmcodec_dev->rxq))) {
  135. btm_opcode opcode = STREAM_TO_UINT32(skb);
  136. skb_pull(skb, sizeof(btm_opcode));
  137. len = STREAM_TO_UINT32(skb);
  138. skb_pull(skb, sizeof(len));
  139. switch (opcode) {
  140. case BTM_BTFMCODEC_PREPARE_AUDIO_BEARER_SWITCH_REQ:
  141. idx = BTM_PKT_TYPE_PREPARE_REQ;
  142. BTFMCODEC_DBG("BTM_BTFMCODEC_PREPARE_AUDIO_BEARER_SWITCH_REQ");
  143. if (len == BTM_PREPARE_AUDIO_BEARER_SWITCH_REQ_LEN) {
  144. /* there are chances where bearer indication is not recevied,
  145. * So inform waiting thread to unblock itself and move to
  146. * previous state.
  147. */
  148. if (btfmcodec_dev->status[BTM_PKT_TYPE_BEARER_SWITCH_IND] == BTM_WAITING_RSP) {
  149. BTFMCODEC_DBG("Notifying waiting beare indications");
  150. btfmcodec_dev->status[BTM_PKT_TYPE_BEARER_SWITCH_IND] = BTM_FAIL_RESP_RECV;
  151. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[BTM_PKT_TYPE_BEARER_SWITCH_IND]);
  152. }
  153. btfmcodec_dev->status[idx] = skb->data[0];
  154. /* Reset bearer switch ind flag */
  155. bearer_switch_ind =
  156. &btfmcodec_dev->status[BTM_PKT_TYPE_BEARER_SWITCH_IND];
  157. *bearer_switch_ind = BTM_WAITING_RSP;
  158. queue_work(btfmcodec_dev->workqueue, &btfmcodec_dev->wq_prepare_bearer);
  159. } else {
  160. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  161. }
  162. break;
  163. case BTM_BTFMCODEC_MASTER_CONFIG_RSP:
  164. idx = BTM_PKT_TYPE_MASTER_CONFIG_RSP;
  165. if (len == BTM_MASTER_CONFIG_RSP_LEN) {
  166. status = skb->data[1];
  167. if (status == MSG_SUCCESS)
  168. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  169. else
  170. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  171. } else {
  172. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  173. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  174. }
  175. BTFMCODEC_INFO("Rx BTM_BTFMCODEC_MASTER_CONFIG_RSP status:%d",
  176. status);
  177. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  178. break;
  179. case BTM_BTFMCODEC_CODEC_CONFIG_DMA_RSP:
  180. idx = BTM_PKT_TYPE_DMA_CONFIG_RSP;
  181. if (len == BTM_CODEC_CONFIG_DMA_RSP_LEN) {
  182. status = skb->data[1];
  183. if (status == MSG_SUCCESS)
  184. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  185. else
  186. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  187. } else {
  188. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  189. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  190. }
  191. BTFMCODEC_INFO("Rx BTM_BTFMCODEC_CODEC_CONFIG_DMA_RSP status:%d",
  192. status);
  193. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  194. break;
  195. case BTM_BTFMCODEC_CTRL_MASTER_SHUTDOWN_RSP:
  196. idx = BTM_PKT_TYPE_MASTER_SHUTDOWN_RSP;
  197. if (len == BTM_MASTER_CONFIG_RSP_LEN) {
  198. status = skb->data[1];
  199. if (status == MSG_SUCCESS)
  200. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  201. else
  202. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  203. } else {
  204. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  205. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  206. }
  207. BTFMCODEC_INFO("Rx BTM_BTFMCODEC_CTRL_MASTER_SHUTDOWN_RSP status:%d",
  208. status);
  209. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  210. BTFMCODEC_INFO("%s: waiting to cancel prepare bearer wq", __func__);
  211. cancel_work_sync(&btfmcodec_dev->wq_prepare_bearer);
  212. BTFMCODEC_INFO("%s: prepare bearer wq canceled", __func__);
  213. break;
  214. case BTM_BTFMCODEC_BEARER_SWITCH_IND:
  215. idx = BTM_PKT_TYPE_BEARER_SWITCH_IND;
  216. if (len == BTM_BEARER_SWITCH_IND_LEN) {
  217. status = skb->data[0];
  218. if (status == MSG_SUCCESS)
  219. btfmcodec_dev->status[idx] = BTM_RSP_RECV;
  220. else
  221. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  222. } else {
  223. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  224. btfmcodec_dev->status[idx] = BTM_FAIL_RESP_RECV;
  225. }
  226. BTFMCODEC_INFO("Rx BTM_BTFMCODEC_BEARER_SWITCH_IND status:%d",
  227. status);
  228. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  229. break;
  230. case BTM_BTFMCODEC_CTRL_LOG_LVL_IND:
  231. if (len == BTM_LOG_LVL_IND_LEN) {
  232. log_lvl = skb->data[0];
  233. } else {
  234. BTFMCODEC_ERR("wrong packet format with len:%d", len);
  235. }
  236. BTFMCODEC_INFO("Rx BTM_BTFMCODEC_CTRL_LOG_LVL_IND status:%d",
  237. log_lvl);
  238. wake_up_interruptible(&btfmcodec_dev->rsp_wait_q[idx]);
  239. break;
  240. default:
  241. BTFMCODEC_ERR("wrong opcode:%08x", opcode);
  242. }
  243. kfree_skb(skb);
  244. }
  245. BTFMCODEC_DBG("end");
  246. }
  247. /*
  248. * btfmcodec_pkt_write() - write() syscall for the btfmcodec_pkt device
  249. * file: Pointer to the file structure.
  250. * buf: Pointer to the userspace buffer.
  251. * count: Number bytes to read from the file.
  252. * ppos: Pointer to the position into the file.
  253. *
  254. * This function is used to write the data to btfmcodec dev node when
  255. * userspace client do a write() system call. All input arguments are
  256. * validated by the virtual file system before calling this function.
  257. */
  258. static ssize_t btfmcodec_dev_write(struct file *file,
  259. const char __user *buf, size_t count, loff_t *ppos)
  260. {
  261. struct btfmcodec_data *btfmcodec = file->private_data;
  262. struct btfmcodec_char_device *btfmcodec_dev= NULL;
  263. struct sk_buff *skb;
  264. void *kbuf;
  265. int ret = 0;
  266. if (!btfmcodec || !btfmcodec->btfmcodec_dev || refcount_read(&btfmcodec->btfmcodec_dev->active_clients) == 1) {
  267. BTFMCODEC_INFO("%s: %d\n", current->comm, task_pid_nr(current));
  268. return -EINVAL;
  269. } else {
  270. btfmcodec_dev = btfmcodec->btfmcodec_dev;
  271. }
  272. if (mutex_lock_interruptible(&btfmcodec_dev->lock)) {
  273. ret = -ERESTARTSYS;
  274. goto free_kbuf;
  275. }
  276. /* Hack for Now */
  277. if (count < MIN_PKT_LEN) {
  278. BTFMCODEC_ERR("minimum packet len should be greater than 3 bytes");
  279. goto free_kbuf;
  280. }
  281. if (refcount_read(&btfmcodec->btfmcodec_dev->active_clients) == 0) {
  282. BTFMCODEC_WARN("Client disconnected");
  283. ret = -ENETRESET;
  284. goto free_kbuf;
  285. }
  286. BTFMCODEC_DBG("begin to %s buffer_size %zu\n", btfmcodec_dev->dev_name, count);
  287. kbuf = memdup_user(buf, count);
  288. if (IS_ERR(kbuf)) {
  289. ret = PTR_ERR(kbuf);
  290. goto free_kbuf;
  291. }
  292. /* Check whether we need a dedicated chunk of memory for this driver */
  293. skb = alloc_skb(count* sizeof(size_t), GFP_KERNEL);
  294. if (!skb) {
  295. BTFMCODEC_ERR("failed to allocate memory for recevied packet");
  296. ret = -ENOMEM;
  297. goto free_kbuf;
  298. }
  299. skb_put_data(skb, (uint8_t *)kbuf, count);
  300. skb_queue_tail(&btfmcodec_dev->rxq, skb);
  301. schedule_work(&btfmcodec_dev->rx_work);
  302. kfree(kbuf);
  303. free_kbuf:
  304. mutex_unlock(&btfmcodec_dev->lock);
  305. BTFMCODEC_DBG("finish to %s ret %d\n", btfmcodec_dev->dev_name, ret);
  306. return ret < 0 ? ret : count;
  307. }
  308. int btfmcodec_dev_enqueue_pkt(struct btfmcodec_char_device *btfmcodec_dev, void *buf, int len)
  309. {
  310. struct sk_buff *skb;
  311. unsigned long flags;
  312. uint8_t *cmd = buf;
  313. BTFMCODEC_DBG("start");
  314. spin_lock_irqsave(&btfmcodec_dev->tx_queue_lock, flags);
  315. if (refcount_read(&btfmcodec_dev->active_clients) == 1) {
  316. BTFMCODEC_WARN("no active clients discarding the packet");
  317. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  318. return -EINVAL;
  319. }
  320. skb = alloc_skb(len, GFP_ATOMIC);
  321. if (!skb) {
  322. BTFMCODEC_ERR("failed to allocate memory");
  323. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  324. return -ENOMEM;
  325. }
  326. skb_put_data(skb, cmd, len);
  327. skb_queue_tail(&btfmcodec_dev->txq, skb);
  328. wake_up_interruptible(&btfmcodec_dev->readq);
  329. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  330. BTFMCODEC_DBG("end");
  331. return 0;
  332. }
  333. /*
  334. * btfmcodec_pkt_poll() - poll() syscall for the btfmcodec device
  335. * file: Pointer to the file structure.
  336. * wait: pointer to Poll table.
  337. *
  338. * This function is used to poll on the btfmcodec dev node when
  339. * userspace client do a poll() system call. All input arguments are
  340. * validated by the virtual file system before calling this function.
  341. */
  342. static __poll_t btfmcodec_dev_poll(struct file *file, poll_table *wait)
  343. {
  344. struct btfmcodec_data *btfmcodec = file->private_data;
  345. struct btfmcodec_char_device *btfmcodec_dev= NULL;
  346. __poll_t mask = 0;
  347. unsigned long flags;
  348. BTFMCODEC_DBG("start");
  349. if (!btfmcodec || !btfmcodec->btfmcodec_dev || refcount_read(&btfmcodec->btfmcodec_dev->active_clients) == 1) {
  350. BTFMCODEC_INFO("%s: %d\n", current->comm, task_pid_nr(current));
  351. return -EINVAL;
  352. } else {
  353. btfmcodec_dev = btfmcodec->btfmcodec_dev;
  354. }
  355. /* Wait here for timeout or for a wakeup signal on readq */
  356. poll_wait(file, &btfmcodec_dev->readq, wait);
  357. mutex_lock(&btfmcodec_dev->lock);
  358. /* recheck if the client has released by the driver */
  359. if (refcount_read(&btfmcodec_dev->active_clients) == 1) {
  360. BTFMCODEC_WARN("port has been closed already");
  361. mutex_unlock(&btfmcodec_dev->lock);
  362. return POLLHUP;
  363. }
  364. spin_lock_irqsave(&btfmcodec_dev->tx_queue_lock, flags);
  365. /* Set flags if data is avilable to read */
  366. if (!skb_queue_empty(&btfmcodec_dev->txq))
  367. mask |= POLLIN | POLLRDNORM;
  368. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  369. mutex_unlock(&btfmcodec_dev->lock);
  370. BTFMCODEC_DBG("end with reason %d", mask);
  371. return mask;
  372. }
  373. /*
  374. * btfmcodec_dev_read() - read() syscall for the btfmcodec dev node
  375. * file: Pointer to the file structure.
  376. * buf: Pointer to the userspace buffer.
  377. * count: Number bytes to read from the file.
  378. * ppos: Pointer to the position into the file.
  379. *
  380. * This function is used to Read the data from btfmcodec pkt device when
  381. * userspace client do a read() system call. All input arguments are
  382. * validated by the virtual file system before calling this function.
  383. */
  384. static ssize_t btfmcodec_dev_read(struct file *file,
  385. char __user *buf, size_t count, loff_t *ppos)
  386. {
  387. struct btfmcodec_data *btfmcodec = file->private_data;
  388. struct btfmcodec_char_device *btfmcodec_dev= NULL;
  389. unsigned long flags;
  390. struct sk_buff *skb;
  391. int use;
  392. BTFMCODEC_DBG("start");
  393. if (!btfmcodec || !btfmcodec->btfmcodec_dev || refcount_read(&btfmcodec->btfmcodec_dev->active_clients) == 1) {
  394. BTFMCODEC_INFO("%s: %d\n", current->comm, task_pid_nr(current));
  395. return -EINVAL;
  396. } else {
  397. btfmcodec_dev = btfmcodec->btfmcodec_dev;
  398. }
  399. spin_lock_irqsave(&btfmcodec_dev->tx_queue_lock, flags);
  400. /* Wait for data in the queue */
  401. if (skb_queue_empty(&btfmcodec_dev->txq)) {
  402. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  403. if (file->f_flags & O_NONBLOCK)
  404. return -EAGAIN;
  405. /* Wait until we get data*/
  406. if (wait_event_interruptible(btfmcodec_dev->readq,
  407. !skb_queue_empty(&btfmcodec_dev->txq)))
  408. return -ERESTARTSYS;
  409. /* We lost the client while waiting */
  410. if (refcount_read(&btfmcodec->btfmcodec_dev->active_clients) == 1)
  411. return -ENETRESET;
  412. spin_lock_irqsave(&btfmcodec_dev->tx_queue_lock, flags);
  413. }
  414. skb = skb_dequeue(&btfmcodec_dev->txq);
  415. spin_unlock_irqrestore(&btfmcodec_dev->tx_queue_lock, flags);
  416. if (!skb)
  417. return -EFAULT;
  418. use = min_t(size_t, count, skb->len);
  419. if (copy_to_user(buf, skb->data, use))
  420. use = -EFAULT;
  421. kfree_skb(skb);
  422. BTFMCODEC_DBG("end for %s by %s:%d ret[%d]\n", btfmcodec_dev->dev_name,
  423. current->comm, task_pid_nr(current), use);
  424. return use;
  425. }
  426. bool isCpSupported(void)
  427. {
  428. return is_cp_supported;
  429. }
  430. static long btfmcodec_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  431. {
  432. struct btfmcodec_data *btfmcodec = file->private_data;
  433. struct hwep_data *hwep_info;
  434. BTFMCODEC_INFO("%s: command %04x", __func__, cmd);
  435. mutex_lock(&btfmcodec->hwep_drv_lock);
  436. hwep_info = btfmcodec->hwep_info;
  437. if (!hwep_info) {
  438. BTFMCODEC_WARN("%s: HWEP is not registered with btfmcodec", __func__);
  439. BTFMCODEC_WARN("%s: caching required info", __func__);
  440. is_cp_supported = ((int)arg == 1) ? true : false;
  441. mutex_unlock(&btfmcodec->hwep_drv_lock);
  442. return 0;
  443. }
  444. mutex_unlock(&btfmcodec->hwep_drv_lock);
  445. switch (cmd) {
  446. case BTM_CP_UPDATE: {
  447. if ((int)arg == 1) {
  448. if (!strcmp(hwep_info->driver_name, "btfmslim"))
  449. set_bit(BTADV_AUDIO_MASTER_CONFIG, &hwep_info->flags);
  450. else if (!strcmp(hwep_info->driver_name, "btfmswr_slave"))
  451. set_bit(BTADV_CONFIGURE_DMA, &hwep_info->flags);
  452. BTFMCODEC_INFO("%s: This target support CP hwep %s",
  453. __func__, hwep_info->driver_name);
  454. } else {
  455. clear_bit(BTADV_AUDIO_MASTER_CONFIG, &hwep_info->flags);
  456. clear_bit(BTADV_CONFIGURE_DMA, &hwep_info->flags);
  457. BTFMCODEC_INFO("%s: This target support doesn't CP", __func__);
  458. }
  459. BTFMCODEC_INFO("%s: mastr %d dma codec %d", __func__,
  460. (int)test_bit(BTADV_AUDIO_MASTER_CONFIG, &hwep_info->flags),
  461. (int)test_bit(BTADV_CONFIGURE_DMA, &hwep_info->flags));
  462. break;
  463. } default: {
  464. BTFMCODEC_ERR("%s unhandled cmd %04x", __func__, cmd);
  465. }
  466. }
  467. return 0;
  468. }
  469. static const struct file_operations btfmcodec_fops = {
  470. .owner = THIS_MODULE,
  471. .open = btfmcodec_dev_open,
  472. .release = btfmcodec_dev_release,
  473. .write = btfmcodec_dev_write,
  474. .poll = btfmcodec_dev_poll,
  475. .read = btfmcodec_dev_read,
  476. /* For Now add no hookups for below callbacks */
  477. .unlocked_ioctl = btfmcodec_ioctl,
  478. .compat_ioctl = btfmcodec_ioctl,
  479. };
  480. static ssize_t btfmcodec_attributes_store(struct device *dev,
  481. struct device_attribute *attr,
  482. const char *buf, size_t n)
  483. {
  484. struct btfmcodec_data *btfmcodec = dev_to_btfmcodec(dev);
  485. struct btfmcodec_char_device *btfmcodec_dev = btfmcodec->btfmcodec_dev;
  486. long tmp;
  487. mutex_lock(&btfmcodec_dev->lock);
  488. if (kstrtol(buf, 0, &tmp)) {
  489. mutex_unlock(&btfmcodec_dev->lock);
  490. return -EINVAL;
  491. }
  492. mutex_unlock(&btfmcodec_dev->lock);
  493. return n;
  494. }
  495. static ssize_t btfmcodec_attributes_show(struct device *dev,
  496. struct device_attribute *attr,
  497. char *buf)
  498. {
  499. // struct btfmcodec_get_current_transport *btfmcodec_dev = dev_to_btfmcodec(dev);
  500. return 0;
  501. }
  502. struct btfmcodec_data* btfm_get_btfmcodec(void)
  503. {
  504. return btfmcodec;
  505. }
  506. EXPORT_SYMBOL(btfm_get_btfmcodec);
  507. static DEVICE_ATTR_RW(btfmcodec_attributes);
  508. static int __init btfmcodec_init(void)
  509. {
  510. struct btfmcodec_state_machine *states;
  511. struct btfmcodec_char_device *btfmcodec_dev;
  512. struct device *dev;
  513. int ret, i;
  514. BTFMCODEC_INFO("starting up the module");
  515. btfmcodec = kzalloc(sizeof(struct btfmcodec_data), GFP_KERNEL);
  516. if (!btfmcodec) {
  517. BTFMCODEC_ERR("failed to allocate memory");
  518. return -ENOMEM;
  519. }
  520. mutex_init(&btfmcodec->hwep_drv_lock);
  521. states = &btfmcodec->states;
  522. states->current_state = IDLE;
  523. states->next_state = IDLE;
  524. BTFMCODEC_INFO("creating device node");
  525. /* create device node for communication between userspace and kernel */
  526. btfmcodec_dev = kzalloc(sizeof(struct btfmcodec_char_device), GFP_KERNEL);
  527. if (!btfmcodec_dev) {
  528. BTFMCODEC_ERR("failed to allocate memory");
  529. ret = -ENOMEM;
  530. goto info_cleanup;
  531. }
  532. BTFMCODEC_INFO("trying to get major number\n");
  533. ret = alloc_chrdev_region(&dev_major, 0, 0, "btfmcodec");
  534. if (ret < 0) {
  535. BTFMCODEC_ERR("failed to allocate character device region");
  536. goto dev_cleanup;
  537. }
  538. BTFMCODEC_INFO("creating btfm codec class");
  539. dev_class = class_create(THIS_MODULE, "btfmcodec");
  540. if (IS_ERR(dev_class)) {
  541. ret = PTR_ERR(dev_class);
  542. BTFMCODEC_ERR("class_create failed ret:%d\n", ret);
  543. goto deinit_chrdev;
  544. }
  545. btfmcodec_dev->reuse_minor = idr_alloc(&dev_minor, btfmcodec, 1, 0, GFP_KERNEL);
  546. if (ret < 0) {
  547. BTFMCODEC_ERR("failed to allocated minor number");
  548. goto deinit_class;
  549. }
  550. dev = &btfmcodec->dev;
  551. dev->driver = &driver;
  552. // ToDo Rethink of having btfmcodec alone instead of btfmcodec
  553. btfmcodec->btfmcodec_dev = btfmcodec_dev;
  554. refcount_set(&btfmcodec_dev->active_clients, 1);
  555. mutex_init(&btfmcodec_dev->lock);
  556. strlcpy(btfmcodec_dev->dev_name, "btfmcodec_dev", DEVICE_NAME_MAX_LEN);
  557. device_initialize(dev);
  558. dev->class = dev_class;
  559. dev->devt = MKDEV(MAJOR(dev_major), btfmcodec_dev->reuse_minor);
  560. dev_set_drvdata(dev, btfmcodec);
  561. cdev_init(&btfmcodec_dev->cdev, &btfmcodec_fops);
  562. btfmcodec_dev->cdev.owner = THIS_MODULE;
  563. btfmcodec_dev->btfmcodec = (struct btfmcodec_data *)btfmcodec;
  564. dev_set_name(dev, btfmcodec_dev->dev_name, btfmcodec_dev->reuse_minor);
  565. ret = cdev_add(&btfmcodec_dev->cdev, dev->devt, 1);
  566. if (ret) {
  567. BTFMCODEC_ERR("cdev_add failed with error no %d", ret);
  568. goto idr_cleanup;
  569. }
  570. // ToDo to handler HIDL abrupt kill
  571. dev->release = NULL;
  572. ret = device_add(dev);
  573. if (ret) {
  574. BTFMCODEC_ERR("Failed to add device error no %d", ret);
  575. goto free_device;
  576. }
  577. BTFMCODEC_ERR("Creating a sysfs entry with name: %s", btfmcodec_dev->dev_name);
  578. ret = device_create_file(dev, &dev_attr_btfmcodec_attributes);
  579. if (ret) {
  580. BTFMCODEC_ERR("Failed to create a devicd node: %s", btfmcodec_dev->dev_name);
  581. goto free_device;
  582. }
  583. BTFMCODEC_INFO("created a node at /dev/%s with %u:%u\n",
  584. btfmcodec_dev->dev_name, dev_major, btfmcodec_dev->reuse_minor);
  585. skb_queue_head_init(&btfmcodec_dev->rxq);
  586. mutex_init(&btfmcodec_dev->lock);
  587. INIT_WORK(&btfmcodec_dev->rx_work, btfmcodec_dev_rxwork);
  588. init_waitqueue_head(&btfmcodec_dev->readq);
  589. spin_lock_init(&btfmcodec_dev->tx_queue_lock);
  590. skb_queue_head_init(&btfmcodec_dev->txq);
  591. INIT_LIST_HEAD(&btfmcodec->config_head);
  592. for (i = 0; i < BTM_PKT_TYPE_MAX; i++) {
  593. init_waitqueue_head(&btfmcodec_dev->rsp_wait_q[i]);
  594. }
  595. mutex_init(&states->state_machine_lock);
  596. btfmcodec_dev->workqueue = alloc_ordered_workqueue("btfmcodec_wq", 0);
  597. if (!btfmcodec_dev->workqueue) {
  598. BTFMCODEC_ERR("btfmcodec_dev Workqueue not initialized properly");
  599. ret = -ENOMEM;
  600. goto free_device;
  601. }
  602. return ret;
  603. free_device:
  604. put_device(dev);
  605. idr_cleanup:
  606. idr_remove(&dev_minor, btfmcodec_dev->reuse_minor);
  607. deinit_class:
  608. class_destroy(dev_class);
  609. deinit_chrdev:
  610. unregister_chrdev_region(MAJOR(dev_major), 0);
  611. dev_cleanup:
  612. kfree(btfmcodec_dev);
  613. info_cleanup:
  614. kfree(btfmcodec);
  615. return ret;
  616. }
  617. static void __exit btfmcodec_deinit(void)
  618. {
  619. struct btfmcodec_char_device *btfmcodec_dev;
  620. struct device *dev;
  621. BTFMCODEC_INFO("%s: cleaning up btfm codec driver", __func__);
  622. if (!btfmcodec) {
  623. BTFMCODEC_ERR("%s: skiping driver cleanup", __func__);
  624. goto info_cleanup;
  625. }
  626. dev = &btfmcodec->dev;
  627. device_remove_file(dev, &dev_attr_btfmcodec_attributes);
  628. put_device(dev);
  629. if (!btfmcodec->btfmcodec_dev) {
  630. BTFMCODEC_ERR("%s: skiping device node cleanup", __func__);
  631. goto info_cleanup;
  632. }
  633. btfmcodec_dev = btfmcodec->btfmcodec_dev;
  634. skb_queue_purge(&btfmcodec_dev->rxq);
  635. idr_remove(&dev_minor, btfmcodec_dev->reuse_minor);
  636. class_destroy(dev_class);
  637. unregister_chrdev_region(MAJOR(dev_major), 0);
  638. kfree(btfmcodec_dev);
  639. info_cleanup:
  640. kfree(btfmcodec);
  641. BTFMCODEC_INFO("%s: btfm codec driver cleanup completed", __func__);
  642. return;
  643. }
  644. MODULE_LICENSE("GPL v2");
  645. MODULE_DESCRIPTION("MSM Bluetooth FM CODEC driver");
  646. module_init(btfmcodec_init);
  647. module_exit(btfmcodec_deinit);