spidev.c 22 KB

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