btfm_codec.c 20 KB

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