spidev.c 22 KB

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