qfs4008_spidev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright (C) 2018 Samsung Electronics. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include "fingerprint.h"
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/ioctl.h>
  17. #include <linux/fs.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/list.h>
  21. #include <linux/errno.h>
  22. #include <linux/mutex.h>
  23. #include <linux/slab.h>
  24. #include <linux/compat.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/acpi.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/spidev.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/version.h>
  32. #define QFSSPI_MAJOR 232
  33. #define N_SPI_MINORS 32
  34. static DECLARE_BITMAP(minors, N_SPI_MINORS);
  35. #define QFSSPI_DEV "qbtspi"
  36. /* Bit masks for spi_device.mode management. Note that incorrect
  37. * settings for some settings can cause *lots* of trouble for other
  38. * devices on a shared bus:
  39. *
  40. * - CS_HIGH ... this device will be active when it shouldn't be
  41. * - 3WIRE ... when active, it won't behave as it should
  42. * - NO_CS ... there will be no explicit message boundaries; this
  43. * is completely incompatible with the shared bus model
  44. * - READY ... transfers may proceed when they shouldn't.
  45. *
  46. * REVISIT should changing those flags be privileged?
  47. */
  48. #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
  49. | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
  50. | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
  51. | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)
  52. struct qfsspi_data {
  53. dev_t devt;
  54. spinlock_t spi_lock;
  55. struct spi_device *spi;
  56. struct list_head device_entry;
  57. /* TX/RX buffers are NULL unless this device is open (users > 0) */
  58. struct mutex buf_lock;
  59. unsigned int users;
  60. u8 *tx_buffer;
  61. u8 *rx_buffer;
  62. u32 speed_hz;
  63. };
  64. static LIST_HEAD(device_list);
  65. static DEFINE_MUTEX(device_list_lock);
  66. static unsigned int bufsiz = 1024 * 256;
  67. module_param(bufsiz, uint, 0444);
  68. MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
  69. /*-------------------------------------------------------------------------*/
  70. static ssize_t
  71. qfsspi_sync(struct qfsspi_data *spidev, struct spi_message *message)
  72. {
  73. int status;
  74. struct spi_device *spi;
  75. spin_lock_irq(&spidev->spi_lock);
  76. spi = spidev->spi;
  77. spin_unlock_irq(&spidev->spi_lock);
  78. if (spi == NULL)
  79. status = -ESHUTDOWN;
  80. else
  81. status = spi_sync(spi, message);
  82. if (status == 0)
  83. status = message->actual_length;
  84. return status;
  85. }
  86. static inline ssize_t
  87. qfsspi_sync_write(struct qfsspi_data *spidev, size_t len)
  88. {
  89. struct spi_transfer t = {
  90. .tx_buf = spidev->tx_buffer,
  91. .len = len,
  92. .speed_hz = spidev->speed_hz,
  93. };
  94. struct spi_message m;
  95. spi_message_init(&m);
  96. spi_message_add_tail(&t, &m);
  97. return qfsspi_sync(spidev, &m);
  98. }
  99. static inline ssize_t
  100. qfsspi_sync_read(struct qfsspi_data *spidev, size_t len)
  101. {
  102. struct spi_transfer t = {
  103. .rx_buf = spidev->rx_buffer,
  104. .len = len,
  105. .speed_hz = spidev->speed_hz,
  106. };
  107. struct spi_message m;
  108. spi_message_init(&m);
  109. spi_message_add_tail(&t, &m);
  110. return qfsspi_sync(spidev, &m);
  111. }
  112. /*-------------------------------------------------------------------------*/
  113. /* Read-only message with current device setup */
  114. static ssize_t
  115. qfsspi_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  116. {
  117. struct qfsspi_data *spidev;
  118. ssize_t status = 0;
  119. /* chipselect only toggles at start or end of operation */
  120. if (count > bufsiz)
  121. return -EMSGSIZE;
  122. spidev = filp->private_data;
  123. mutex_lock(&spidev->buf_lock);
  124. status = qfsspi_sync_read(spidev, count);
  125. if (status > 0) {
  126. unsigned long missing;
  127. missing = copy_to_user(buf, spidev->rx_buffer, status);
  128. if (missing == status)
  129. status = -EFAULT;
  130. else
  131. status = status - missing;
  132. }
  133. mutex_unlock(&spidev->buf_lock);
  134. return status;
  135. }
  136. /* Write-only message with current device setup */
  137. static ssize_t
  138. qfsspi_write(struct file *filp, const char __user *buf,
  139. size_t count, loff_t *f_pos)
  140. {
  141. struct qfsspi_data *spidev;
  142. ssize_t status = 0;
  143. unsigned long missing;
  144. /* chipselect only toggles at start or end of operation */
  145. if (count > bufsiz)
  146. return -EMSGSIZE;
  147. spidev = filp->private_data;
  148. mutex_lock(&spidev->buf_lock);
  149. missing = copy_from_user(spidev->tx_buffer, buf, count);
  150. if (missing == 0)
  151. status = qfsspi_sync_write(spidev, count);
  152. else
  153. status = -EFAULT;
  154. mutex_unlock(&spidev->buf_lock);
  155. return status;
  156. }
  157. static int qfsspi_message(struct qfsspi_data *spidev,
  158. struct spi_ioc_transfer *u_xfers, unsigned int n_xfers)
  159. {
  160. struct spi_message msg;
  161. struct spi_transfer *k_xfers;
  162. struct spi_transfer *k_tmp;
  163. struct spi_ioc_transfer *u_tmp;
  164. unsigned int n, total, tx_total, rx_total;
  165. u8 *tx_buf, *rx_buf;
  166. int status = -EFAULT;
  167. spi_message_init(&msg);
  168. k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
  169. if (k_xfers == NULL)
  170. return -ENOMEM;
  171. /* Construct spi_message, copying any tx data to bounce buffer.
  172. * We walk the array of user-provided transfers, using each one
  173. * to initialize a kernel version of the same transfer.
  174. */
  175. tx_buf = spidev->tx_buffer;
  176. rx_buf = spidev->rx_buffer;
  177. total = 0;
  178. tx_total = 0;
  179. rx_total = 0;
  180. for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  181. n;
  182. n--, k_tmp++, u_tmp++) {
  183. k_tmp->len = u_tmp->len;
  184. total += k_tmp->len;
  185. /* Since the function returns the total length of transfers
  186. * on success, restrict the total to positive int values to
  187. * avoid the return value looking like an error. Also check
  188. * each transfer length to avoid arithmetic overflow.
  189. */
  190. if (total > INT_MAX || k_tmp->len > INT_MAX) {
  191. status = -EMSGSIZE;
  192. goto done;
  193. }
  194. if (u_tmp->rx_buf) {
  195. /* this transfer needs space in RX bounce buffer */
  196. rx_total += k_tmp->len;
  197. if (rx_total > bufsiz) {
  198. status = -EMSGSIZE;
  199. goto done;
  200. }
  201. k_tmp->rx_buf = rx_buf;
  202. rx_buf += k_tmp->len;
  203. }
  204. if (u_tmp->tx_buf) {
  205. /* this transfer needs space in TX bounce buffer */
  206. tx_total += k_tmp->len;
  207. if (tx_total > bufsiz) {
  208. status = -EMSGSIZE;
  209. goto done;
  210. }
  211. k_tmp->tx_buf = tx_buf;
  212. if (copy_from_user(tx_buf, (const u8 __user *)
  213. (uintptr_t) u_tmp->tx_buf,
  214. u_tmp->len))
  215. goto done;
  216. tx_buf += k_tmp->len;
  217. }
  218. k_tmp->cs_change = !!u_tmp->cs_change;
  219. k_tmp->tx_nbits = u_tmp->tx_nbits;
  220. k_tmp->rx_nbits = u_tmp->rx_nbits;
  221. k_tmp->bits_per_word = u_tmp->bits_per_word;
  222. k_tmp->speed_hz = u_tmp->speed_hz;
  223. if (!k_tmp->speed_hz)
  224. k_tmp->speed_hz = spidev->speed_hz;
  225. spi_message_add_tail(k_tmp, &msg);
  226. }
  227. status = qfsspi_sync(spidev, &msg);
  228. if (status < 0)
  229. goto done;
  230. /* copy any rx data out of bounce buffer */
  231. rx_buf = spidev->rx_buffer;
  232. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  233. if (u_tmp->rx_buf) {
  234. if (copy_to_user((u8 __user *)
  235. (uintptr_t) u_tmp->rx_buf, rx_buf,
  236. u_tmp->len)) {
  237. status = -EFAULT;
  238. goto done;
  239. }
  240. rx_buf += u_tmp->len;
  241. }
  242. }
  243. status = total;
  244. done:
  245. kfree(k_xfers);
  246. return status;
  247. }
  248. static struct spi_ioc_transfer *
  249. qfsspi_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
  250. unsigned int *n_ioc)
  251. {
  252. u32 tmp;
  253. /* Check type, command number and direction */
  254. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
  255. || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  256. || _IOC_DIR(cmd) != _IOC_WRITE)
  257. return ERR_PTR(-ENOTTY);
  258. tmp = _IOC_SIZE(cmd);
  259. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
  260. return ERR_PTR(-EINVAL);
  261. *n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  262. if (*n_ioc == 0)
  263. return NULL;
  264. /* copy into scratch area */
  265. return memdup_user(u_ioc, tmp);
  266. }
  267. static long
  268. qfsspi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  269. {
  270. int retval = 0;
  271. struct qfsspi_data *spidev;
  272. struct spi_device *spi;
  273. u32 tmp;
  274. unsigned int n_ioc;
  275. struct spi_ioc_transfer *ioc;
  276. /* Check type and command number */
  277. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  278. return -ENOTTY;
  279. /* guard against device removal before, or while,
  280. * we issue this ioctl.
  281. */
  282. spidev = filp->private_data;
  283. spin_lock_irq(&spidev->spi_lock);
  284. spi = spi_dev_get(spidev->spi);
  285. spin_unlock_irq(&spidev->spi_lock);
  286. if (spi == NULL)
  287. return -ESHUTDOWN;
  288. /* use the buffer lock here for triple duty:
  289. * - prevent I/O (from us) so calling spi_setup() is safe;
  290. * - prevent concurrent SPI_IOC_WR_* from morphing
  291. * data fields while SPI_IOC_RD_* reads them;
  292. * - SPI_IOC_MESSAGE needs the buffer locked "normally".
  293. */
  294. mutex_lock(&spidev->buf_lock);
  295. switch (cmd) {
  296. /* read requests */
  297. case SPI_IOC_RD_MODE:
  298. retval = put_user(spi->mode & SPI_MODE_MASK,
  299. (__u8 __user *)arg);
  300. break;
  301. case SPI_IOC_RD_MODE32:
  302. retval = put_user(spi->mode & SPI_MODE_MASK,
  303. (__u32 __user *)arg);
  304. break;
  305. case SPI_IOC_RD_BITS_PER_WORD:
  306. pr_info("%s: SPI_IOC_RD_BITS_PER_WORD\n", __func__);
  307. retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
  308. break;
  309. case SPI_IOC_RD_MAX_SPEED_HZ:
  310. pr_info("%s: SPI_IOC_RD_MAX_SPEED_HZ\n", __func__);
  311. retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
  312. break;
  313. /* write requests */
  314. case SPI_IOC_WR_MODE:
  315. case SPI_IOC_WR_MODE32:
  316. if (cmd == SPI_IOC_WR_MODE)
  317. retval = get_user(tmp, (u8 __user *)arg);
  318. else
  319. retval = get_user(tmp, (u32 __user *)arg);
  320. if (retval == 0) {
  321. u32 save = spi->mode;
  322. if (tmp & ~SPI_MODE_MASK) {
  323. retval = -EINVAL;
  324. break;
  325. }
  326. tmp |= spi->mode & ~SPI_MODE_MASK;
  327. spi->mode = (u16)tmp;
  328. retval = spi_setup(spi);
  329. if (retval < 0)
  330. spi->mode = save;
  331. else
  332. pr_debug("%s: spi mode %x\n", __func__, tmp);
  333. }
  334. break;
  335. case SPI_IOC_WR_BITS_PER_WORD:
  336. retval = get_user(tmp, (__u8 __user *)arg);
  337. if (retval == 0) {
  338. u8 save = spi->bits_per_word;
  339. spi->bits_per_word = tmp;
  340. retval = spi_setup(spi);
  341. if (retval < 0)
  342. spi->bits_per_word = save;
  343. else
  344. pr_debug("%s: %d bits per word\n", __func__, tmp);
  345. }
  346. break;
  347. case SPI_IOC_WR_MAX_SPEED_HZ:
  348. retval = get_user(tmp, (__u32 __user *)arg);
  349. break;
  350. default:
  351. /* segmented and/or full-duplex I/O request */
  352. /* Check message and copy into scratch area */
  353. ioc = qfsspi_get_ioc_message(cmd,
  354. (struct spi_ioc_transfer __user *)arg, &n_ioc);
  355. if (IS_ERR(ioc)) {
  356. retval = PTR_ERR(ioc);
  357. break;
  358. }
  359. if (!ioc)
  360. break; /* n_ioc is also 0 */
  361. /* translate to spi_message, execute */
  362. retval = qfsspi_message(spidev, ioc, n_ioc);
  363. kfree(ioc);
  364. break;
  365. }
  366. mutex_unlock(&spidev->buf_lock);
  367. spi_dev_put(spi);
  368. return retval;
  369. }
  370. #ifdef CONFIG_COMPAT
  371. static long
  372. qfsspi_compat_ioc_message(struct file *filp, unsigned int cmd,
  373. unsigned long arg)
  374. {
  375. struct spi_ioc_transfer __user *u_ioc;
  376. int retval = 0;
  377. struct qfsspi_data *spidev;
  378. struct spi_device *spi;
  379. unsigned int n_ioc, n;
  380. struct spi_ioc_transfer *ioc;
  381. u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
  382. /* guard against device removal before, or while,
  383. * we issue this ioctl.
  384. */
  385. spidev = filp->private_data;
  386. spin_lock_irq(&spidev->spi_lock);
  387. spi = spi_dev_get(spidev->spi);
  388. spin_unlock_irq(&spidev->spi_lock);
  389. if (spi == NULL)
  390. return -ESHUTDOWN;
  391. /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
  392. mutex_lock(&spidev->buf_lock);
  393. /* Check message and copy into scratch area */
  394. ioc = qfsspi_get_ioc_message(cmd, u_ioc, &n_ioc);
  395. if (IS_ERR(ioc)) {
  396. retval = PTR_ERR(ioc);
  397. goto done;
  398. }
  399. if (!ioc)
  400. goto done; /* n_ioc is also 0 */
  401. /* Convert buffer pointers */
  402. for (n = 0; n < n_ioc; n++) {
  403. ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
  404. ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
  405. }
  406. /* translate to spi_message, execute */
  407. retval = qfsspi_message(spidev, ioc, n_ioc);
  408. kfree(ioc);
  409. done:
  410. mutex_unlock(&spidev->buf_lock);
  411. spi_dev_put(spi);
  412. return retval;
  413. }
  414. static long
  415. qfsspi_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  416. {
  417. if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
  418. && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
  419. && _IOC_DIR(cmd) == _IOC_WRITE)
  420. return qfsspi_compat_ioc_message(filp, cmd, arg);
  421. return qfsspi_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  422. }
  423. #else
  424. #define qfsspi_compat_ioctl NULL
  425. #endif /* CONFIG_COMPAT */
  426. static int qfsspi_open(struct inode *inode, struct file *filp)
  427. {
  428. struct qfsspi_data *spidev;
  429. int status = -ENXIO;
  430. mutex_lock(&device_list_lock);
  431. list_for_each_entry(spidev, &device_list, device_entry) {
  432. if (spidev->devt == inode->i_rdev) {
  433. status = 0;
  434. break;
  435. }
  436. }
  437. if (status) {
  438. pr_debug("%s: nothing for minor %d\n", __func__, iminor(inode));
  439. goto err_find_dev;
  440. }
  441. if (!spidev->tx_buffer) {
  442. spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
  443. if (!spidev->tx_buffer) {
  444. pr_debug("%s: open/ENOMEM\n", __func__);
  445. status = -ENOMEM;
  446. goto err_find_dev;
  447. }
  448. }
  449. if (!spidev->rx_buffer) {
  450. spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
  451. if (!spidev->rx_buffer) {
  452. pr_debug("%s: open/ENOMEM\n", __func__);
  453. status = -ENOMEM;
  454. goto err_alloc_rx_buf;
  455. }
  456. }
  457. spidev->users++;
  458. filp->private_data = spidev;
  459. nonseekable_open(inode, filp);
  460. mutex_unlock(&device_list_lock);
  461. return 0;
  462. err_alloc_rx_buf:
  463. kfree(spidev->tx_buffer);
  464. spidev->tx_buffer = NULL;
  465. err_find_dev:
  466. mutex_unlock(&device_list_lock);
  467. return status;
  468. }
  469. static int qfsspi_release(struct inode *inode, struct file *filp)
  470. {
  471. struct qfsspi_data *spidev;
  472. mutex_lock(&device_list_lock);
  473. spidev = filp->private_data;
  474. filp->private_data = NULL;
  475. /* last close? */
  476. spidev->users--;
  477. if (!spidev->users) {
  478. int dofree;
  479. kfree(spidev->tx_buffer);
  480. spidev->tx_buffer = NULL;
  481. kfree(spidev->rx_buffer);
  482. spidev->rx_buffer = NULL;
  483. spin_lock_irq(&spidev->spi_lock);
  484. if (spidev->spi)
  485. spidev->speed_hz = spidev->spi->max_speed_hz;
  486. /* ... after we unbound from the underlying device? */
  487. dofree = (spidev->spi == NULL);
  488. spin_unlock_irq(&spidev->spi_lock);
  489. if (dofree)
  490. kfree(spidev);
  491. }
  492. mutex_unlock(&device_list_lock);
  493. return 0;
  494. }
  495. static const struct file_operations qfsspi_fops = {
  496. .owner = THIS_MODULE,
  497. /* REVISIT switch to aio primitives, so that userspace
  498. * gets more complete API coverage. It'll simplify things
  499. * too, except for the locking.
  500. */
  501. .write = qfsspi_write,
  502. .read = qfsspi_read,
  503. .unlocked_ioctl = qfsspi_ioctl,
  504. .compat_ioctl = qfsspi_compat_ioctl,
  505. .open = qfsspi_open,
  506. .release = qfsspi_release,
  507. .llseek = no_llseek,
  508. };
  509. /*-------------------------------------------------------------------------*/
  510. /* The main reason to have this class is to make mdev/udev create the
  511. * /dev/spidevB.C character device nodes exposing our userspace API.
  512. * It also simplifies memory management.
  513. */
  514. static struct class *qfsspi_class;
  515. static const struct of_device_id qfsspi_dt_ids[] = {
  516. #if !defined(ENABLE_SENSORS_FPRINT_SECURE)
  517. { .compatible = "qcom,qfsspi" },
  518. #endif
  519. {},
  520. };
  521. /*-------------------------------------------------------------------------*/
  522. static int qfsspi_probe(struct spi_device *spi)
  523. {
  524. struct qfsspi_data *spidev;
  525. int status;
  526. unsigned long minor;
  527. pr_info("%s: start\n", __func__);
  528. /* Allocate driver data */
  529. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  530. if (!spidev)
  531. return -ENOMEM;
  532. /* Initialize the driver data */
  533. spidev->spi = spi;
  534. spin_lock_init(&spidev->spi_lock);
  535. mutex_init(&spidev->buf_lock);
  536. INIT_LIST_HEAD(&spidev->device_entry);
  537. /* If we can allocate a minor number, hook up this device.
  538. * Reusing minors is fine so long as udev or mdev is working.
  539. */
  540. mutex_lock(&device_list_lock);
  541. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  542. if (minor < N_SPI_MINORS) {
  543. struct device *dev;
  544. spidev->devt = MKDEV(QFSSPI_MAJOR, minor);
  545. dev = device_create(qfsspi_class, &spi->dev, spidev->devt,
  546. spidev, QFSSPI_DEV);
  547. status = PTR_ERR_OR_ZERO(dev);
  548. } else {
  549. pr_err("%s: no minor number available!\n", __func__);
  550. status = -ENODEV;
  551. }
  552. if (status == 0) {
  553. set_bit(minor, minors);
  554. list_add(&spidev->device_entry, &device_list);
  555. }
  556. mutex_unlock(&device_list_lock);
  557. spidev->speed_hz = spi->max_speed_hz;
  558. if (status == 0)
  559. spi_set_drvdata(spi, spidev);
  560. else
  561. kfree(spidev);
  562. pr_info("%s: finish %d\n", __func__, status);
  563. return status;
  564. }
  565. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
  566. static void qfsspi_remove(struct spi_device *spi)
  567. #else
  568. static int qfsspi_remove(struct spi_device *spi)
  569. #endif
  570. {
  571. struct qfsspi_data *spidev = spi_get_drvdata(spi);
  572. /* make sure ops on existing fds can abort cleanly */
  573. spin_lock_irq(&spidev->spi_lock);
  574. spidev->spi = NULL;
  575. spin_unlock_irq(&spidev->spi_lock);
  576. /* prevent new opens */
  577. mutex_lock(&device_list_lock);
  578. list_del(&spidev->device_entry);
  579. device_destroy(qfsspi_class, spidev->devt);
  580. clear_bit(MINOR(spidev->devt), minors);
  581. if (spidev->users == 0)
  582. kfree(spidev);
  583. mutex_unlock(&device_list_lock);
  584. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0))
  585. return;
  586. #else
  587. return 0;
  588. #endif
  589. }
  590. static struct spi_driver qfsspi_spi_driver = {
  591. .driver = {
  592. .name = QFSSPI_DEV,
  593. .owner = THIS_MODULE,
  594. .of_match_table = qfsspi_dt_ids,
  595. },
  596. .probe = qfsspi_probe,
  597. .remove = qfsspi_remove,
  598. /* NOTE: suspend/resume methods are not necessary here.
  599. * We don't do anything except pass the requests to/from
  600. * the underlying controller. The refrigerator handles
  601. * most issues; the controller driver handles the rest.
  602. */
  603. };
  604. /*-------------------------------------------------------------------------*/
  605. static int __init qfsspi_init(void)
  606. {
  607. int status = 0;
  608. #if !defined(ENABLE_SENSORS_FPRINT_SECURE)
  609. pr_info("%s\n", __func__);
  610. /* Claim our 256 reserved device numbers. Then register a class
  611. * that will key udev/mdev to add/remove /dev nodes. Last, register
  612. * the driver which manages those device numbers.
  613. */
  614. BUILD_BUG_ON(N_SPI_MINORS > 256);
  615. status = register_chrdev(QFSSPI_MAJOR, QFSSPI_DEV, &qfsspi_fops);
  616. if (status < 0) {
  617. pr_err("%s: register_chrdev failed %d\n", __func__, status);
  618. return status;
  619. }
  620. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  621. qfsspi_class = class_create(QFSSPI_DEV);
  622. #else
  623. qfsspi_class = class_create(THIS_MODULE, QFSSPI_DEV);
  624. #endif
  625. if (IS_ERR(qfsspi_class)) {
  626. unregister_chrdev(QFSSPI_MAJOR, QFSSPI_DEV);
  627. pr_err("%s: class_create failed\n", __func__);
  628. return PTR_ERR(qfsspi_class);
  629. }
  630. status = spi_register_driver(&qfsspi_spi_driver);
  631. if (status < 0) {
  632. class_destroy(qfsspi_class);
  633. unregister_chrdev(QFSSPI_MAJOR, qfsspi_spi_driver.driver.name);
  634. pr_err("%s: spi_register_driver failed\n", __func__);
  635. }
  636. pr_info("%s: finish %d\n", __func__, status);
  637. #endif
  638. return status;
  639. }
  640. module_init(qfsspi_init);
  641. static void __exit qfsspi_exit(void)
  642. {
  643. spi_unregister_driver(&qfsspi_spi_driver);
  644. class_destroy(qfsspi_class);
  645. unregister_chrdev(QFSSPI_MAJOR, qfsspi_spi_driver.driver.name);
  646. }
  647. module_exit(qfsspi_exit);
  648. MODULE_AUTHOR("Kangwook.Her");
  649. MODULE_DESCRIPTION("Samsung Electronics Inc. QBT2000 spi driver");
  650. MODULE_LICENSE("GPL v2");