spidev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. u32 speed_hz;
  81. };
  82. static LIST_HEAD(device_list);
  83. static DEFINE_MUTEX(device_list_lock);
  84. static unsigned bufsiz = 4096;
  85. module_param(bufsiz, uint, S_IRUGO);
  86. MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
  87. /*-------------------------------------------------------------------------*/
  88. /*
  89. * We can't use the standard synchronous wrappers for file I/O; we
  90. * need to protect against async removal of the underlying spi_device.
  91. */
  92. static void spidev_complete(void *arg)
  93. {
  94. complete(arg);
  95. }
  96. static ssize_t
  97. spidev_sync(struct spidev_data *spidev, struct spi_message *message)
  98. {
  99. DECLARE_COMPLETION_ONSTACK(done);
  100. int status;
  101. message->complete = spidev_complete;
  102. message->context = &done;
  103. spin_lock_irq(&spidev->spi_lock);
  104. if (spidev->spi == NULL)
  105. status = -ESHUTDOWN;
  106. else
  107. status = spi_async(spidev->spi, message);
  108. spin_unlock_irq(&spidev->spi_lock);
  109. if (status == 0) {
  110. wait_for_completion(&done);
  111. status = message->status;
  112. if (status == 0)
  113. status = message->actual_length;
  114. }
  115. return status;
  116. }
  117. static inline ssize_t
  118. spidev_sync_write(struct spidev_data *spidev, size_t len)
  119. {
  120. struct spi_transfer t = {
  121. .tx_buf = spidev->tx_buffer,
  122. .len = len,
  123. .speed_hz = spidev->speed_hz,
  124. };
  125. struct spi_message m;
  126. spi_message_init(&m);
  127. spi_message_add_tail(&t, &m);
  128. return spidev_sync(spidev, &m);
  129. }
  130. static inline ssize_t
  131. spidev_sync_read(struct spidev_data *spidev, size_t len)
  132. {
  133. struct spi_transfer t = {
  134. .rx_buf = spidev->rx_buffer,
  135. .len = len,
  136. .speed_hz = spidev->speed_hz,
  137. };
  138. struct spi_message m;
  139. spi_message_init(&m);
  140. spi_message_add_tail(&t, &m);
  141. return spidev_sync(spidev, &m);
  142. }
  143. /*-------------------------------------------------------------------------*/
  144. /* Read-only message with current device setup */
  145. static ssize_t
  146. spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  147. {
  148. struct spidev_data *spidev;
  149. ssize_t status = 0;
  150. /* chipselect only toggles at start or end of operation */
  151. if (count > bufsiz)
  152. return -EMSGSIZE;
  153. spidev = filp->private_data;
  154. mutex_lock(&spidev->buf_lock);
  155. status = spidev_sync_read(spidev, count);
  156. if (status > 0) {
  157. unsigned long missing;
  158. missing = copy_to_user(buf, spidev->rx_buffer, status);
  159. if (missing == status)
  160. status = -EFAULT;
  161. else
  162. status = status - missing;
  163. }
  164. mutex_unlock(&spidev->buf_lock);
  165. return status;
  166. }
  167. /* Write-only message with current device setup */
  168. static ssize_t
  169. spidev_write(struct file *filp, const char __user *buf,
  170. size_t count, loff_t *f_pos)
  171. {
  172. struct spidev_data *spidev;
  173. ssize_t status = 0;
  174. unsigned long missing;
  175. /* chipselect only toggles at start or end of operation */
  176. if (count > bufsiz)
  177. return -EMSGSIZE;
  178. spidev = filp->private_data;
  179. mutex_lock(&spidev->buf_lock);
  180. missing = copy_from_user(spidev->tx_buffer, buf, count);
  181. if (missing == 0)
  182. status = spidev_sync_write(spidev, count);
  183. else
  184. status = -EFAULT;
  185. mutex_unlock(&spidev->buf_lock);
  186. return status;
  187. }
  188. static int spidev_message(struct spidev_data *spidev,
  189. struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
  190. {
  191. struct spi_message msg;
  192. struct spi_transfer *k_xfers;
  193. struct spi_transfer *k_tmp;
  194. struct spi_ioc_transfer *u_tmp;
  195. unsigned n, total;
  196. u8 *tx_buf, *rx_buf;
  197. int status = -EFAULT;
  198. spi_message_init(&msg);
  199. k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
  200. if (k_xfers == NULL)
  201. return -ENOMEM;
  202. /* Construct spi_message, copying any tx data to bounce buffer.
  203. * We walk the array of user-provided transfers, using each one
  204. * to initialize a kernel version of the same transfer.
  205. */
  206. tx_buf = spidev->tx_buffer;
  207. rx_buf = spidev->rx_buffer;
  208. total = 0;
  209. for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
  210. n;
  211. n--, k_tmp++, u_tmp++) {
  212. k_tmp->len = u_tmp->len;
  213. total += k_tmp->len;
  214. if (total > 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. if (!k_tmp->speed_hz)
  241. k_tmp->speed_hz = spidev->speed_hz;
  242. #ifdef VERBOSE
  243. dev_dbg(&spidev->spi->dev,
  244. " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
  245. u_tmp->len,
  246. u_tmp->rx_buf ? "rx " : "",
  247. u_tmp->tx_buf ? "tx " : "",
  248. u_tmp->cs_change ? "cs " : "",
  249. u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
  250. u_tmp->delay_usecs,
  251. u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
  252. #endif
  253. spi_message_add_tail(k_tmp, &msg);
  254. }
  255. status = spidev_sync(spidev, &msg);
  256. if (status < 0)
  257. goto done;
  258. /* copy any rx data out of bounce buffer */
  259. rx_buf = spidev->rx_buffer;
  260. for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
  261. if (u_tmp->rx_buf) {
  262. if (__copy_to_user((u8 __user *)
  263. (uintptr_t) u_tmp->rx_buf, rx_buf,
  264. u_tmp->len)) {
  265. status = -EFAULT;
  266. goto done;
  267. }
  268. }
  269. rx_buf += u_tmp->len;
  270. }
  271. status = total;
  272. done:
  273. kfree(k_xfers);
  274. return status;
  275. }
  276. static long
  277. spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  278. {
  279. int err = 0;
  280. int retval = 0;
  281. struct spidev_data *spidev;
  282. struct spi_device *spi;
  283. u32 tmp;
  284. unsigned n_ioc;
  285. struct spi_ioc_transfer *ioc;
  286. /* Check type and command number */
  287. if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
  288. return -ENOTTY;
  289. /* Check access direction once here; don't repeat below.
  290. * IOC_DIR is from the user perspective, while access_ok is
  291. * from the kernel perspective; so they look reversed.
  292. */
  293. if (_IOC_DIR(cmd) & _IOC_READ)
  294. err = !access_ok(VERIFY_WRITE,
  295. (void __user *)arg, _IOC_SIZE(cmd));
  296. if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
  297. err = !access_ok(VERIFY_READ,
  298. (void __user *)arg, _IOC_SIZE(cmd));
  299. if (err)
  300. return -EFAULT;
  301. /* guard against device removal before, or while,
  302. * we issue this ioctl.
  303. */
  304. spidev = filp->private_data;
  305. spin_lock_irq(&spidev->spi_lock);
  306. spi = spi_dev_get(spidev->spi);
  307. spin_unlock_irq(&spidev->spi_lock);
  308. if (spi == NULL)
  309. return -ESHUTDOWN;
  310. /* use the buffer lock here for triple duty:
  311. * - prevent I/O (from us) so calling spi_setup() is safe;
  312. * - prevent concurrent SPI_IOC_WR_* from morphing
  313. * data fields while SPI_IOC_RD_* reads them;
  314. * - SPI_IOC_MESSAGE needs the buffer locked "normally".
  315. */
  316. mutex_lock(&spidev->buf_lock);
  317. switch (cmd) {
  318. /* read requests */
  319. case SPI_IOC_RD_MODE:
  320. retval = __put_user(spi->mode & SPI_MODE_MASK,
  321. (__u8 __user *)arg);
  322. break;
  323. case SPI_IOC_RD_MODE32:
  324. retval = __put_user(spi->mode & SPI_MODE_MASK,
  325. (__u32 __user *)arg);
  326. break;
  327. case SPI_IOC_RD_LSB_FIRST:
  328. retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
  329. (__u8 __user *)arg);
  330. break;
  331. case SPI_IOC_RD_BITS_PER_WORD:
  332. retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
  333. break;
  334. case SPI_IOC_RD_MAX_SPEED_HZ:
  335. retval = __put_user(spidev->speed_hz, (__u32 __user *)arg);
  336. break;
  337. /* write requests */
  338. case SPI_IOC_WR_MODE:
  339. case SPI_IOC_WR_MODE32:
  340. if (cmd == SPI_IOC_WR_MODE)
  341. retval = __get_user(tmp, (u8 __user *)arg);
  342. else
  343. retval = __get_user(tmp, (u32 __user *)arg);
  344. if (retval == 0) {
  345. u32 save = spi->mode;
  346. if (tmp & ~SPI_MODE_MASK) {
  347. retval = -EINVAL;
  348. break;
  349. }
  350. tmp |= spi->mode & ~SPI_MODE_MASK;
  351. spi->mode = (u16)tmp;
  352. retval = spi_setup(spi);
  353. if (retval < 0)
  354. spi->mode = save;
  355. else
  356. dev_dbg(&spi->dev, "spi mode %x\n", tmp);
  357. }
  358. break;
  359. case SPI_IOC_WR_LSB_FIRST:
  360. retval = __get_user(tmp, (__u8 __user *)arg);
  361. if (retval == 0) {
  362. u32 save = spi->mode;
  363. if (tmp)
  364. spi->mode |= SPI_LSB_FIRST;
  365. else
  366. spi->mode &= ~SPI_LSB_FIRST;
  367. retval = spi_setup(spi);
  368. if (retval < 0)
  369. spi->mode = save;
  370. else
  371. dev_dbg(&spi->dev, "%csb first\n",
  372. tmp ? 'l' : 'm');
  373. }
  374. break;
  375. case SPI_IOC_WR_BITS_PER_WORD:
  376. retval = __get_user(tmp, (__u8 __user *)arg);
  377. if (retval == 0) {
  378. u8 save = spi->bits_per_word;
  379. spi->bits_per_word = tmp;
  380. retval = spi_setup(spi);
  381. if (retval < 0)
  382. spi->bits_per_word = save;
  383. else
  384. dev_dbg(&spi->dev, "%d bits per word\n", tmp);
  385. }
  386. break;
  387. case SPI_IOC_WR_MAX_SPEED_HZ:
  388. retval = __get_user(tmp, (__u32 __user *)arg);
  389. if (retval == 0) {
  390. u32 save = spi->max_speed_hz;
  391. spi->max_speed_hz = tmp;
  392. retval = spi_setup(spi);
  393. if (retval >= 0)
  394. spidev->speed_hz = tmp;
  395. else
  396. dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
  397. spi->max_speed_hz = save;
  398. }
  399. break;
  400. default:
  401. /* segmented and/or full-duplex I/O request */
  402. if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
  403. || _IOC_DIR(cmd) != _IOC_WRITE) {
  404. retval = -ENOTTY;
  405. break;
  406. }
  407. tmp = _IOC_SIZE(cmd);
  408. if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
  409. retval = -EINVAL;
  410. break;
  411. }
  412. n_ioc = tmp / sizeof(struct spi_ioc_transfer);
  413. if (n_ioc == 0)
  414. break;
  415. /* copy into scratch area */
  416. ioc = kmalloc(tmp, GFP_KERNEL);
  417. if (!ioc) {
  418. retval = -ENOMEM;
  419. break;
  420. }
  421. if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
  422. kfree(ioc);
  423. retval = -EFAULT;
  424. break;
  425. }
  426. /* translate to spi_message, execute */
  427. retval = spidev_message(spidev, ioc, n_ioc);
  428. kfree(ioc);
  429. break;
  430. }
  431. mutex_unlock(&spidev->buf_lock);
  432. spi_dev_put(spi);
  433. return retval;
  434. }
  435. #ifdef CONFIG_COMPAT
  436. static long
  437. spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  438. {
  439. return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  440. }
  441. #else
  442. #define spidev_compat_ioctl NULL
  443. #endif /* CONFIG_COMPAT */
  444. static int spidev_open(struct inode *inode, struct file *filp)
  445. {
  446. struct spidev_data *spidev;
  447. int status = -ENXIO;
  448. mutex_lock(&device_list_lock);
  449. list_for_each_entry(spidev, &device_list, device_entry) {
  450. if (spidev->devt == inode->i_rdev) {
  451. status = 0;
  452. break;
  453. }
  454. }
  455. if (status) {
  456. pr_debug("spidev: nothing for minor %d\n", iminor(inode));
  457. goto err_find_dev;
  458. }
  459. if (!spidev->tx_buffer) {
  460. spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
  461. if (!spidev->tx_buffer) {
  462. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  463. status = -ENOMEM;
  464. goto err_find_dev;
  465. }
  466. }
  467. if (!spidev->rx_buffer) {
  468. spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
  469. if (!spidev->rx_buffer) {
  470. dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
  471. status = -ENOMEM;
  472. goto err_alloc_rx_buf;
  473. }
  474. }
  475. spidev->users++;
  476. filp->private_data = spidev;
  477. nonseekable_open(inode, filp);
  478. mutex_unlock(&device_list_lock);
  479. return 0;
  480. err_alloc_rx_buf:
  481. kfree(spidev->tx_buffer);
  482. spidev->tx_buffer = NULL;
  483. err_find_dev:
  484. mutex_unlock(&device_list_lock);
  485. return status;
  486. }
  487. static int spidev_release(struct inode *inode, struct file *filp)
  488. {
  489. struct spidev_data *spidev;
  490. int status = 0;
  491. mutex_lock(&device_list_lock);
  492. spidev = filp->private_data;
  493. filp->private_data = NULL;
  494. /* last close? */
  495. spidev->users--;
  496. if (!spidev->users) {
  497. int dofree;
  498. kfree(spidev->tx_buffer);
  499. spidev->tx_buffer = NULL;
  500. kfree(spidev->rx_buffer);
  501. spidev->rx_buffer = NULL;
  502. spidev->speed_hz = spidev->spi->max_speed_hz;
  503. /* ... after we unbound from the underlying device? */
  504. spin_lock_irq(&spidev->spi_lock);
  505. dofree = (spidev->spi == NULL);
  506. spin_unlock_irq(&spidev->spi_lock);
  507. if (dofree)
  508. kfree(spidev);
  509. }
  510. mutex_unlock(&device_list_lock);
  511. return status;
  512. }
  513. static const struct file_operations spidev_fops = {
  514. .owner = THIS_MODULE,
  515. /* REVISIT switch to aio primitives, so that userspace
  516. * gets more complete API coverage. It'll simplify things
  517. * too, except for the locking.
  518. */
  519. .write = spidev_write,
  520. .read = spidev_read,
  521. .unlocked_ioctl = spidev_ioctl,
  522. .compat_ioctl = spidev_compat_ioctl,
  523. .open = spidev_open,
  524. .release = spidev_release,
  525. .llseek = no_llseek,
  526. };
  527. /*-------------------------------------------------------------------------*/
  528. /* The main reason to have this class is to make mdev/udev create the
  529. * /dev/spidevB.C character device nodes exposing our userspace API.
  530. * It also simplifies memory management.
  531. */
  532. static struct class *spidev_class;
  533. /*-------------------------------------------------------------------------*/
  534. static int spidev_probe(struct spi_device *spi)
  535. {
  536. struct spidev_data *spidev;
  537. int status;
  538. unsigned long minor;
  539. /* Allocate driver data */
  540. spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
  541. if (!spidev)
  542. return -ENOMEM;
  543. /* Initialize the driver data */
  544. spidev->spi = spi;
  545. spin_lock_init(&spidev->spi_lock);
  546. mutex_init(&spidev->buf_lock);
  547. INIT_LIST_HEAD(&spidev->device_entry);
  548. /* If we can allocate a minor number, hook up this device.
  549. * Reusing minors is fine so long as udev or mdev is working.
  550. */
  551. mutex_lock(&device_list_lock);
  552. minor = find_first_zero_bit(minors, N_SPI_MINORS);
  553. if (minor < N_SPI_MINORS) {
  554. struct device *dev;
  555. spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
  556. dev = device_create(spidev_class, &spi->dev, spidev->devt,
  557. spidev, "spidev%d.%d",
  558. spi->master->bus_num, spi->chip_select);
  559. status = PTR_ERR_OR_ZERO(dev);
  560. } else {
  561. dev_dbg(&spi->dev, "no minor number available!\n");
  562. status = -ENODEV;
  563. }
  564. if (status == 0) {
  565. set_bit(minor, minors);
  566. list_add(&spidev->device_entry, &device_list);
  567. }
  568. mutex_unlock(&device_list_lock);
  569. spidev->speed_hz = spi->max_speed_hz;
  570. if (status == 0)
  571. spi_set_drvdata(spi, spidev);
  572. else
  573. kfree(spidev);
  574. return status;
  575. }
  576. static int spidev_remove(struct spi_device *spi)
  577. {
  578. struct spidev_data *spidev = spi_get_drvdata(spi);
  579. /* make sure ops on existing fds can abort cleanly */
  580. spin_lock_irq(&spidev->spi_lock);
  581. spidev->spi = NULL;
  582. spin_unlock_irq(&spidev->spi_lock);
  583. /* prevent new opens */
  584. mutex_lock(&device_list_lock);
  585. list_del(&spidev->device_entry);
  586. device_destroy(spidev_class, spidev->devt);
  587. clear_bit(MINOR(spidev->devt), minors);
  588. if (spidev->users == 0)
  589. kfree(spidev);
  590. mutex_unlock(&device_list_lock);
  591. return 0;
  592. }
  593. static const struct of_device_id spidev_dt_ids[] = {
  594. { .compatible = "rohm,dh2228fv" },
  595. {},
  596. };
  597. MODULE_DEVICE_TABLE(of, spidev_dt_ids);
  598. static struct spi_driver spidev_spi_driver = {
  599. .driver = {
  600. .name = "spidev",
  601. .owner = THIS_MODULE,
  602. .of_match_table = of_match_ptr(spidev_dt_ids),
  603. },
  604. .probe = spidev_probe,
  605. .remove = spidev_remove,
  606. /* NOTE: suspend/resume methods are not necessary here.
  607. * We don't do anything except pass the requests to/from
  608. * the underlying controller. The refrigerator handles
  609. * most issues; the controller driver handles the rest.
  610. */
  611. };
  612. /*-------------------------------------------------------------------------*/
  613. static int __init spidev_init(void)
  614. {
  615. int status;
  616. /* Claim our 256 reserved device numbers. Then register a class
  617. * that will key udev/mdev to add/remove /dev nodes. Last, register
  618. * the driver which manages those device numbers.
  619. */
  620. BUILD_BUG_ON(N_SPI_MINORS > 256);
  621. status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
  622. if (status < 0)
  623. return status;
  624. spidev_class = class_create(THIS_MODULE, "spidev");
  625. if (IS_ERR(spidev_class)) {
  626. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  627. return PTR_ERR(spidev_class);
  628. }
  629. status = spi_register_driver(&spidev_spi_driver);
  630. if (status < 0) {
  631. class_destroy(spidev_class);
  632. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  633. }
  634. return status;
  635. }
  636. module_init(spidev_init);
  637. static void __exit spidev_exit(void)
  638. {
  639. spi_unregister_driver(&spidev_spi_driver);
  640. class_destroy(spidev_class);
  641. unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
  642. }
  643. module_exit(spidev_exit);
  644. MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
  645. MODULE_DESCRIPTION("User mode SPI device interface");
  646. MODULE_LICENSE("GPL");
  647. MODULE_ALIAS("spi:spidev");