spidev.c 18 KB

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