spidev.c 19 KB

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