spidev.c 22 KB

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