btfm_codec.c 22 KB

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