spidev.c 19 KB

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