btfm_codec.c 20 KB

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