btfm_codec.c 19 KB

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