hsi_char.c 19 KB

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