spidev.c 22 KB

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