spidev.c 21 KB

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