hsi_char.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HSI character device driver, implements the character device
  4. * interface.
  5. *
  6. * Copyright (C) 2010 Nokia Corporation. All rights reserved.
  7. *
  8. * Contact: Andras Domokos <[email protected]>
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/types.h>
  12. #include <linux/atomic.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/list.h>
  18. #include <linux/slab.h>
  19. #include <linux/kmemleak.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/wait.h>
  22. #include <linux/fs.h>
  23. #include <linux/sched.h>
  24. #include <linux/device.h>
  25. #include <linux/cdev.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/scatterlist.h>
  28. #include <linux/stat.h>
  29. #include <linux/hsi/hsi.h>
  30. #include <linux/hsi/hsi_char.h>
  31. #define HSC_DEVS 16 /* Num of channels */
  32. #define HSC_MSGS 4
  33. #define HSC_RXBREAK 0
  34. #define HSC_ID_BITS 6
  35. #define HSC_PORT_ID_BITS 4
  36. #define HSC_ID_MASK 3
  37. #define HSC_PORT_ID_MASK 3
  38. #define HSC_CH_MASK 0xf
  39. /*
  40. * We support up to 4 controllers that can have up to 4
  41. * ports, which should currently be more than enough.
  42. */
  43. #define HSC_BASEMINOR(id, port_id) \
  44. ((((id) & HSC_ID_MASK) << HSC_ID_BITS) | \
  45. (((port_id) & HSC_PORT_ID_MASK) << HSC_PORT_ID_BITS))
  46. enum {
  47. HSC_CH_OPEN,
  48. HSC_CH_READ,
  49. HSC_CH_WRITE,
  50. HSC_CH_WLINE,
  51. };
  52. enum {
  53. HSC_RX,
  54. HSC_TX,
  55. };
  56. struct hsc_client_data;
  57. /**
  58. * struct hsc_channel - hsi_char internal channel data
  59. * @ch: channel number
  60. * @flags: Keeps state of the channel (open/close, reading, writing)
  61. * @free_msgs_list: List of free HSI messages/requests
  62. * @rx_msgs_queue: List of pending RX requests
  63. * @tx_msgs_queue: List of pending TX requests
  64. * @lock: Serialize access to the lists
  65. * @cl: reference to the associated hsi_client
  66. * @cl_data: reference to the client data that this channels belongs to
  67. * @rx_wait: RX requests wait queue
  68. * @tx_wait: TX requests wait queue
  69. */
  70. struct hsc_channel {
  71. unsigned int ch;
  72. unsigned long flags;
  73. struct list_head free_msgs_list;
  74. struct list_head rx_msgs_queue;
  75. struct list_head tx_msgs_queue;
  76. spinlock_t lock;
  77. struct hsi_client *cl;
  78. struct hsc_client_data *cl_data;
  79. wait_queue_head_t rx_wait;
  80. wait_queue_head_t tx_wait;
  81. };
  82. /**
  83. * struct hsc_client_data - hsi_char internal client data
  84. * @cdev: Characther device associated to the hsi_client
  85. * @lock: Lock to serialize open/close access
  86. * @flags: Keeps track of port state (rx hwbreak armed)
  87. * @usecnt: Use count for claiming the HSI port (mutex protected)
  88. * @cl: Referece to the HSI client
  89. * @channels: Array of channels accessible by the client
  90. */
  91. struct hsc_client_data {
  92. struct cdev cdev;
  93. struct mutex lock;
  94. unsigned long flags;
  95. unsigned int usecnt;
  96. struct hsi_client *cl;
  97. struct hsc_channel channels[HSC_DEVS];
  98. };
  99. /* Stores the major number dynamically allocated for hsi_char */
  100. static unsigned int hsc_major;
  101. /* Maximum buffer size that hsi_char will accept from userspace */
  102. static unsigned int max_data_size = 0x1000;
  103. module_param(max_data_size, uint, 0);
  104. MODULE_PARM_DESC(max_data_size, "max read/write data size [4,8..65536] (^2)");
  105. static void hsc_add_tail(struct hsc_channel *channel, struct hsi_msg *msg,
  106. struct list_head *queue)
  107. {
  108. unsigned long flags;
  109. spin_lock_irqsave(&channel->lock, flags);
  110. list_add_tail(&msg->link, queue);
  111. spin_unlock_irqrestore(&channel->lock, flags);
  112. }
  113. static struct hsi_msg *hsc_get_first_msg(struct hsc_channel *channel,
  114. struct list_head *queue)
  115. {
  116. struct hsi_msg *msg = NULL;
  117. unsigned long flags;
  118. spin_lock_irqsave(&channel->lock, flags);
  119. if (list_empty(queue))
  120. goto out;
  121. msg = list_first_entry(queue, struct hsi_msg, link);
  122. list_del(&msg->link);
  123. out:
  124. spin_unlock_irqrestore(&channel->lock, flags);
  125. return msg;
  126. }
  127. static inline void hsc_msg_free(struct hsi_msg *msg)
  128. {
  129. kfree(sg_virt(msg->sgt.sgl));
  130. hsi_free_msg(msg);
  131. }
  132. static void hsc_free_list(struct list_head *list)
  133. {
  134. struct hsi_msg *msg, *tmp;
  135. list_for_each_entry_safe(msg, tmp, list, link) {
  136. list_del(&msg->link);
  137. hsc_msg_free(msg);
  138. }
  139. }
  140. static void hsc_reset_list(struct hsc_channel *channel, struct list_head *l)
  141. {
  142. unsigned long flags;
  143. LIST_HEAD(list);
  144. spin_lock_irqsave(&channel->lock, flags);
  145. list_splice_init(l, &list);
  146. spin_unlock_irqrestore(&channel->lock, flags);
  147. hsc_free_list(&list);
  148. }
  149. static inline struct hsi_msg *hsc_msg_alloc(unsigned int alloc_size)
  150. {
  151. struct hsi_msg *msg;
  152. void *buf;
  153. msg = hsi_alloc_msg(1, GFP_KERNEL);
  154. if (!msg)
  155. goto out;
  156. buf = kmalloc(alloc_size, GFP_KERNEL);
  157. if (!buf) {
  158. hsi_free_msg(msg);
  159. goto out;
  160. }
  161. sg_init_one(msg->sgt.sgl, buf, alloc_size);
  162. /* Ignore false positive, due to sg pointer handling */
  163. kmemleak_ignore(buf);
  164. return msg;
  165. out:
  166. return NULL;
  167. }
  168. static inline int hsc_msgs_alloc(struct hsc_channel *channel)
  169. {
  170. struct hsi_msg *msg;
  171. int i;
  172. for (i = 0; i < HSC_MSGS; i++) {
  173. msg = hsc_msg_alloc(max_data_size);
  174. if (!msg)
  175. goto out;
  176. msg->channel = channel->ch;
  177. list_add_tail(&msg->link, &channel->free_msgs_list);
  178. }
  179. return 0;
  180. out:
  181. hsc_free_list(&channel->free_msgs_list);
  182. return -ENOMEM;
  183. }
  184. static inline unsigned int hsc_msg_len_get(struct hsi_msg *msg)
  185. {
  186. return msg->sgt.sgl->length;
  187. }
  188. static inline void hsc_msg_len_set(struct hsi_msg *msg, unsigned int len)
  189. {
  190. msg->sgt.sgl->length = len;
  191. }
  192. static void hsc_rx_completed(struct hsi_msg *msg)
  193. {
  194. struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
  195. struct hsc_channel *channel = cl_data->channels + msg->channel;
  196. if (test_bit(HSC_CH_READ, &channel->flags)) {
  197. hsc_add_tail(channel, msg, &channel->rx_msgs_queue);
  198. wake_up(&channel->rx_wait);
  199. } else {
  200. hsc_add_tail(channel, msg, &channel->free_msgs_list);
  201. }
  202. }
  203. static void hsc_rx_msg_destructor(struct hsi_msg *msg)
  204. {
  205. msg->status = HSI_STATUS_ERROR;
  206. hsc_msg_len_set(msg, 0);
  207. hsc_rx_completed(msg);
  208. }
  209. static void hsc_tx_completed(struct hsi_msg *msg)
  210. {
  211. struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
  212. struct hsc_channel *channel = cl_data->channels + msg->channel;
  213. if (test_bit(HSC_CH_WRITE, &channel->flags)) {
  214. hsc_add_tail(channel, msg, &channel->tx_msgs_queue);
  215. wake_up(&channel->tx_wait);
  216. } else {
  217. hsc_add_tail(channel, msg, &channel->free_msgs_list);
  218. }
  219. }
  220. static void hsc_tx_msg_destructor(struct hsi_msg *msg)
  221. {
  222. msg->status = HSI_STATUS_ERROR;
  223. hsc_msg_len_set(msg, 0);
  224. hsc_tx_completed(msg);
  225. }
  226. static void hsc_break_req_destructor(struct hsi_msg *msg)
  227. {
  228. struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
  229. hsi_free_msg(msg);
  230. clear_bit(HSC_RXBREAK, &cl_data->flags);
  231. }
  232. static void hsc_break_received(struct hsi_msg *msg)
  233. {
  234. struct hsc_client_data *cl_data = hsi_client_drvdata(msg->cl);
  235. struct hsc_channel *channel = cl_data->channels;
  236. int i, ret;
  237. /* Broadcast HWBREAK on all channels */
  238. for (i = 0; i < HSC_DEVS; i++, channel++) {
  239. struct hsi_msg *msg2;
  240. if (!test_bit(HSC_CH_READ, &channel->flags))
  241. continue;
  242. msg2 = hsc_get_first_msg(channel, &channel->free_msgs_list);
  243. if (!msg2)
  244. continue;
  245. clear_bit(HSC_CH_READ, &channel->flags);
  246. hsc_msg_len_set(msg2, 0);
  247. msg2->status = HSI_STATUS_COMPLETED;
  248. hsc_add_tail(channel, msg2, &channel->rx_msgs_queue);
  249. wake_up(&channel->rx_wait);
  250. }
  251. hsi_flush(msg->cl);
  252. ret = hsi_async_read(msg->cl, msg);
  253. if (ret < 0)
  254. hsc_break_req_destructor(msg);
  255. }
  256. static int hsc_break_request(struct hsi_client *cl)
  257. {
  258. struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
  259. struct hsi_msg *msg;
  260. int ret;
  261. if (test_and_set_bit(HSC_RXBREAK, &cl_data->flags))
  262. return -EBUSY;
  263. msg = hsi_alloc_msg(0, GFP_KERNEL);
  264. if (!msg) {
  265. clear_bit(HSC_RXBREAK, &cl_data->flags);
  266. return -ENOMEM;
  267. }
  268. msg->break_frame = 1;
  269. msg->complete = hsc_break_received;
  270. msg->destructor = hsc_break_req_destructor;
  271. ret = hsi_async_read(cl, msg);
  272. if (ret < 0)
  273. hsc_break_req_destructor(msg);
  274. return ret;
  275. }
  276. static int hsc_break_send(struct hsi_client *cl)
  277. {
  278. struct hsi_msg *msg;
  279. int ret;
  280. msg = hsi_alloc_msg(0, GFP_ATOMIC);
  281. if (!msg)
  282. return -ENOMEM;
  283. msg->break_frame = 1;
  284. msg->complete = hsi_free_msg;
  285. msg->destructor = hsi_free_msg;
  286. ret = hsi_async_write(cl, msg);
  287. if (ret < 0)
  288. hsi_free_msg(msg);
  289. return ret;
  290. }
  291. static int hsc_rx_set(struct hsi_client *cl, struct hsc_rx_config *rxc)
  292. {
  293. struct hsi_config tmp;
  294. int ret;
  295. if ((rxc->mode != HSI_MODE_STREAM) && (rxc->mode != HSI_MODE_FRAME))
  296. return -EINVAL;
  297. if ((rxc->channels == 0) || (rxc->channels > HSC_DEVS))
  298. return -EINVAL;
  299. if (rxc->channels & (rxc->channels - 1))
  300. return -EINVAL;
  301. if ((rxc->flow != HSI_FLOW_SYNC) && (rxc->flow != HSI_FLOW_PIPE))
  302. return -EINVAL;
  303. tmp = cl->rx_cfg;
  304. cl->rx_cfg.mode = rxc->mode;
  305. cl->rx_cfg.num_hw_channels = rxc->channels;
  306. cl->rx_cfg.flow = rxc->flow;
  307. ret = hsi_setup(cl);
  308. if (ret < 0) {
  309. cl->rx_cfg = tmp;
  310. return ret;
  311. }
  312. if (rxc->mode == HSI_MODE_FRAME)
  313. hsc_break_request(cl);
  314. return ret;
  315. }
  316. static inline void hsc_rx_get(struct hsi_client *cl, struct hsc_rx_config *rxc)
  317. {
  318. rxc->mode = cl->rx_cfg.mode;
  319. rxc->channels = cl->rx_cfg.num_hw_channels;
  320. rxc->flow = cl->rx_cfg.flow;
  321. }
  322. static int hsc_tx_set(struct hsi_client *cl, struct hsc_tx_config *txc)
  323. {
  324. struct hsi_config tmp;
  325. int ret;
  326. if ((txc->mode != HSI_MODE_STREAM) && (txc->mode != HSI_MODE_FRAME))
  327. return -EINVAL;
  328. if ((txc->channels == 0) || (txc->channels > HSC_DEVS))
  329. return -EINVAL;
  330. if (txc->channels & (txc->channels - 1))
  331. return -EINVAL;
  332. if ((txc->arb_mode != HSI_ARB_RR) && (txc->arb_mode != HSI_ARB_PRIO))
  333. return -EINVAL;
  334. tmp = cl->tx_cfg;
  335. cl->tx_cfg.mode = txc->mode;
  336. cl->tx_cfg.num_hw_channels = txc->channels;
  337. cl->tx_cfg.speed = txc->speed;
  338. cl->tx_cfg.arb_mode = txc->arb_mode;
  339. ret = hsi_setup(cl);
  340. if (ret < 0) {
  341. cl->tx_cfg = tmp;
  342. return ret;
  343. }
  344. return ret;
  345. }
  346. static inline void hsc_tx_get(struct hsi_client *cl, struct hsc_tx_config *txc)
  347. {
  348. txc->mode = cl->tx_cfg.mode;
  349. txc->channels = cl->tx_cfg.num_hw_channels;
  350. txc->speed = cl->tx_cfg.speed;
  351. txc->arb_mode = cl->tx_cfg.arb_mode;
  352. }
  353. static ssize_t hsc_read(struct file *file, char __user *buf, size_t len,
  354. loff_t *ppos __maybe_unused)
  355. {
  356. struct hsc_channel *channel = file->private_data;
  357. struct hsi_msg *msg;
  358. ssize_t ret;
  359. if (len == 0)
  360. return 0;
  361. if (!IS_ALIGNED(len, sizeof(u32)))
  362. return -EINVAL;
  363. if (len > max_data_size)
  364. len = max_data_size;
  365. if (channel->ch >= channel->cl->rx_cfg.num_hw_channels)
  366. return -ECHRNG;
  367. if (test_and_set_bit(HSC_CH_READ, &channel->flags))
  368. return -EBUSY;
  369. msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
  370. if (!msg) {
  371. ret = -ENOSPC;
  372. goto out;
  373. }
  374. hsc_msg_len_set(msg, len);
  375. msg->complete = hsc_rx_completed;
  376. msg->destructor = hsc_rx_msg_destructor;
  377. ret = hsi_async_read(channel->cl, msg);
  378. if (ret < 0) {
  379. hsc_add_tail(channel, msg, &channel->free_msgs_list);
  380. goto out;
  381. }
  382. ret = wait_event_interruptible(channel->rx_wait,
  383. !list_empty(&channel->rx_msgs_queue));
  384. if (ret < 0) {
  385. clear_bit(HSC_CH_READ, &channel->flags);
  386. hsi_flush(channel->cl);
  387. return -EINTR;
  388. }
  389. msg = hsc_get_first_msg(channel, &channel->rx_msgs_queue);
  390. if (msg) {
  391. if (msg->status != HSI_STATUS_ERROR) {
  392. ret = copy_to_user((void __user *)buf,
  393. sg_virt(msg->sgt.sgl), hsc_msg_len_get(msg));
  394. if (ret)
  395. ret = -EFAULT;
  396. else
  397. ret = hsc_msg_len_get(msg);
  398. } else {
  399. ret = -EIO;
  400. }
  401. hsc_add_tail(channel, msg, &channel->free_msgs_list);
  402. }
  403. out:
  404. clear_bit(HSC_CH_READ, &channel->flags);
  405. return ret;
  406. }
  407. static ssize_t hsc_write(struct file *file, const char __user *buf, size_t len,
  408. loff_t *ppos __maybe_unused)
  409. {
  410. struct hsc_channel *channel = file->private_data;
  411. struct hsi_msg *msg;
  412. ssize_t ret;
  413. if ((len == 0) || !IS_ALIGNED(len, sizeof(u32)))
  414. return -EINVAL;
  415. if (len > max_data_size)
  416. len = max_data_size;
  417. if (channel->ch >= channel->cl->tx_cfg.num_hw_channels)
  418. return -ECHRNG;
  419. if (test_and_set_bit(HSC_CH_WRITE, &channel->flags))
  420. return -EBUSY;
  421. msg = hsc_get_first_msg(channel, &channel->free_msgs_list);
  422. if (!msg) {
  423. clear_bit(HSC_CH_WRITE, &channel->flags);
  424. return -ENOSPC;
  425. }
  426. if (copy_from_user(sg_virt(msg->sgt.sgl), (void __user *)buf, len)) {
  427. ret = -EFAULT;
  428. goto out;
  429. }
  430. hsc_msg_len_set(msg, len);
  431. msg->complete = hsc_tx_completed;
  432. msg->destructor = hsc_tx_msg_destructor;
  433. ret = hsi_async_write(channel->cl, msg);
  434. if (ret < 0)
  435. goto out;
  436. ret = wait_event_interruptible(channel->tx_wait,
  437. !list_empty(&channel->tx_msgs_queue));
  438. if (ret < 0) {
  439. clear_bit(HSC_CH_WRITE, &channel->flags);
  440. hsi_flush(channel->cl);
  441. return -EINTR;
  442. }
  443. msg = hsc_get_first_msg(channel, &channel->tx_msgs_queue);
  444. if (msg) {
  445. if (msg->status == HSI_STATUS_ERROR)
  446. ret = -EIO;
  447. else
  448. ret = hsc_msg_len_get(msg);
  449. hsc_add_tail(channel, msg, &channel->free_msgs_list);
  450. }
  451. out:
  452. clear_bit(HSC_CH_WRITE, &channel->flags);
  453. return ret;
  454. }
  455. static long hsc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  456. {
  457. struct hsc_channel *channel = file->private_data;
  458. unsigned int state;
  459. struct hsc_rx_config rxc;
  460. struct hsc_tx_config txc;
  461. long ret = 0;
  462. switch (cmd) {
  463. case HSC_RESET:
  464. hsi_flush(channel->cl);
  465. break;
  466. case HSC_SET_PM:
  467. if (copy_from_user(&state, (void __user *)arg, sizeof(state)))
  468. return -EFAULT;
  469. if (state == HSC_PM_DISABLE) {
  470. if (test_and_set_bit(HSC_CH_WLINE, &channel->flags))
  471. return -EINVAL;
  472. ret = hsi_start_tx(channel->cl);
  473. } else if (state == HSC_PM_ENABLE) {
  474. if (!test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
  475. return -EINVAL;
  476. ret = hsi_stop_tx(channel->cl);
  477. } else {
  478. ret = -EINVAL;
  479. }
  480. break;
  481. case HSC_SEND_BREAK:
  482. return hsc_break_send(channel->cl);
  483. case HSC_SET_RX:
  484. if (copy_from_user(&rxc, (void __user *)arg, sizeof(rxc)))
  485. return -EFAULT;
  486. return hsc_rx_set(channel->cl, &rxc);
  487. case HSC_GET_RX:
  488. hsc_rx_get(channel->cl, &rxc);
  489. if (copy_to_user((void __user *)arg, &rxc, sizeof(rxc)))
  490. return -EFAULT;
  491. break;
  492. case HSC_SET_TX:
  493. if (copy_from_user(&txc, (void __user *)arg, sizeof(txc)))
  494. return -EFAULT;
  495. return hsc_tx_set(channel->cl, &txc);
  496. case HSC_GET_TX:
  497. hsc_tx_get(channel->cl, &txc);
  498. if (copy_to_user((void __user *)arg, &txc, sizeof(txc)))
  499. return -EFAULT;
  500. break;
  501. default:
  502. return -ENOIOCTLCMD;
  503. }
  504. return ret;
  505. }
  506. static inline void __hsc_port_release(struct hsc_client_data *cl_data)
  507. {
  508. BUG_ON(cl_data->usecnt == 0);
  509. if (--cl_data->usecnt == 0) {
  510. hsi_flush(cl_data->cl);
  511. hsi_release_port(cl_data->cl);
  512. }
  513. }
  514. static int hsc_open(struct inode *inode, struct file *file)
  515. {
  516. struct hsc_client_data *cl_data;
  517. struct hsc_channel *channel;
  518. int ret = 0;
  519. pr_debug("open, minor = %d\n", iminor(inode));
  520. cl_data = container_of(inode->i_cdev, struct hsc_client_data, cdev);
  521. mutex_lock(&cl_data->lock);
  522. channel = cl_data->channels + (iminor(inode) & HSC_CH_MASK);
  523. if (test_and_set_bit(HSC_CH_OPEN, &channel->flags)) {
  524. ret = -EBUSY;
  525. goto out;
  526. }
  527. /*
  528. * Check if we have already claimed the port associated to the HSI
  529. * client. If not then try to claim it, else increase its refcount
  530. */
  531. if (cl_data->usecnt == 0) {
  532. ret = hsi_claim_port(cl_data->cl, 0);
  533. if (ret < 0)
  534. goto out;
  535. hsi_setup(cl_data->cl);
  536. }
  537. cl_data->usecnt++;
  538. ret = hsc_msgs_alloc(channel);
  539. if (ret < 0) {
  540. __hsc_port_release(cl_data);
  541. goto out;
  542. }
  543. file->private_data = channel;
  544. mutex_unlock(&cl_data->lock);
  545. return ret;
  546. out:
  547. mutex_unlock(&cl_data->lock);
  548. return ret;
  549. }
  550. static int hsc_release(struct inode *inode __maybe_unused, struct file *file)
  551. {
  552. struct hsc_channel *channel = file->private_data;
  553. struct hsc_client_data *cl_data = channel->cl_data;
  554. mutex_lock(&cl_data->lock);
  555. file->private_data = NULL;
  556. if (test_and_clear_bit(HSC_CH_WLINE, &channel->flags))
  557. hsi_stop_tx(channel->cl);
  558. __hsc_port_release(cl_data);
  559. hsc_reset_list(channel, &channel->rx_msgs_queue);
  560. hsc_reset_list(channel, &channel->tx_msgs_queue);
  561. hsc_reset_list(channel, &channel->free_msgs_list);
  562. clear_bit(HSC_CH_READ, &channel->flags);
  563. clear_bit(HSC_CH_WRITE, &channel->flags);
  564. clear_bit(HSC_CH_OPEN, &channel->flags);
  565. wake_up(&channel->rx_wait);
  566. wake_up(&channel->tx_wait);
  567. mutex_unlock(&cl_data->lock);
  568. return 0;
  569. }
  570. static const struct file_operations hsc_fops = {
  571. .owner = THIS_MODULE,
  572. .read = hsc_read,
  573. .write = hsc_write,
  574. .unlocked_ioctl = hsc_ioctl,
  575. .open = hsc_open,
  576. .release = hsc_release,
  577. };
  578. static void hsc_channel_init(struct hsc_channel *channel)
  579. {
  580. init_waitqueue_head(&channel->rx_wait);
  581. init_waitqueue_head(&channel->tx_wait);
  582. spin_lock_init(&channel->lock);
  583. INIT_LIST_HEAD(&channel->free_msgs_list);
  584. INIT_LIST_HEAD(&channel->rx_msgs_queue);
  585. INIT_LIST_HEAD(&channel->tx_msgs_queue);
  586. }
  587. static int hsc_probe(struct device *dev)
  588. {
  589. const char devname[] = "hsi_char";
  590. struct hsc_client_data *cl_data;
  591. struct hsc_channel *channel;
  592. struct hsi_client *cl = to_hsi_client(dev);
  593. unsigned int hsc_baseminor;
  594. dev_t hsc_dev;
  595. int ret;
  596. int i;
  597. cl_data = kzalloc(sizeof(*cl_data), GFP_KERNEL);
  598. if (!cl_data)
  599. return -ENOMEM;
  600. hsc_baseminor = HSC_BASEMINOR(hsi_id(cl), hsi_port_id(cl));
  601. if (!hsc_major) {
  602. ret = alloc_chrdev_region(&hsc_dev, hsc_baseminor,
  603. HSC_DEVS, devname);
  604. if (ret == 0)
  605. hsc_major = MAJOR(hsc_dev);
  606. } else {
  607. hsc_dev = MKDEV(hsc_major, hsc_baseminor);
  608. ret = register_chrdev_region(hsc_dev, HSC_DEVS, devname);
  609. }
  610. if (ret < 0) {
  611. dev_err(dev, "Device %s allocation failed %d\n",
  612. hsc_major ? "minor" : "major", ret);
  613. goto out1;
  614. }
  615. mutex_init(&cl_data->lock);
  616. hsi_client_set_drvdata(cl, cl_data);
  617. cdev_init(&cl_data->cdev, &hsc_fops);
  618. cl_data->cdev.owner = THIS_MODULE;
  619. cl_data->cl = cl;
  620. for (i = 0, channel = cl_data->channels; i < HSC_DEVS; i++, channel++) {
  621. hsc_channel_init(channel);
  622. channel->ch = i;
  623. channel->cl = cl;
  624. channel->cl_data = cl_data;
  625. }
  626. /* 1 hsi client -> N char devices (one for each channel) */
  627. ret = cdev_add(&cl_data->cdev, hsc_dev, HSC_DEVS);
  628. if (ret) {
  629. dev_err(dev, "Could not add char device %d\n", ret);
  630. goto out2;
  631. }
  632. return 0;
  633. out2:
  634. unregister_chrdev_region(hsc_dev, HSC_DEVS);
  635. out1:
  636. kfree(cl_data);
  637. return ret;
  638. }
  639. static int hsc_remove(struct device *dev)
  640. {
  641. struct hsi_client *cl = to_hsi_client(dev);
  642. struct hsc_client_data *cl_data = hsi_client_drvdata(cl);
  643. dev_t hsc_dev = cl_data->cdev.dev;
  644. cdev_del(&cl_data->cdev);
  645. unregister_chrdev_region(hsc_dev, HSC_DEVS);
  646. hsi_client_set_drvdata(cl, NULL);
  647. kfree(cl_data);
  648. return 0;
  649. }
  650. static struct hsi_client_driver hsc_driver = {
  651. .driver = {
  652. .name = "hsi_char",
  653. .owner = THIS_MODULE,
  654. .probe = hsc_probe,
  655. .remove = hsc_remove,
  656. },
  657. };
  658. static int __init hsc_init(void)
  659. {
  660. int ret;
  661. if ((max_data_size < 4) || (max_data_size > 0x10000) ||
  662. (max_data_size & (max_data_size - 1))) {
  663. pr_err("Invalid max read/write data size\n");
  664. return -EINVAL;
  665. }
  666. ret = hsi_register_client_driver(&hsc_driver);
  667. if (ret) {
  668. pr_err("Error while registering HSI/SSI driver %d\n", ret);
  669. return ret;
  670. }
  671. pr_info("HSI/SSI char device loaded\n");
  672. return 0;
  673. }
  674. module_init(hsc_init);
  675. static void __exit hsc_exit(void)
  676. {
  677. hsi_unregister_client_driver(&hsc_driver);
  678. pr_info("HSI char device removed\n");
  679. }
  680. module_exit(hsc_exit);
  681. MODULE_AUTHOR("Andras Domokos <[email protected]>");
  682. MODULE_ALIAS("hsi:hsi_char");
  683. MODULE_DESCRIPTION("HSI character device");
  684. MODULE_LICENSE("GPL v2");