landisk_pwb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * arch/sh/boards/landisk/landisk_pwb.c -- driver for the Power control switch.
  3. *
  4. * This driver will also support the I-O DATA Device, Inc. LANDISK Board.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copylight (C) 2002 Atom Create Engineering Co., Ltd.
  11. *
  12. * LED control drive function added by kogiidena
  13. */
  14. #include <linux/config.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/signal.h>
  18. #include <linux/major.h>
  19. #include <linux/poll.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/timer.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/system.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/landisk/iodata_landisk.h>
  30. #define SHUTDOWN_BTN_MINOR 1 /* Shutdown button device minor no. */
  31. #define LED_MINOR 21 /* LED minor no. */
  32. #define BTN_MINOR 22 /* BUTTON minor no. */
  33. #define GIO_MINOR 40 /* GIO minor no. */
  34. static int openCnt;
  35. static int openCntLED;
  36. static int openCntGio;
  37. static int openCntBtn;
  38. static int landisk_btn;
  39. static int landisk_btnctrlpid;
  40. /*
  41. * Functions prototypes
  42. */
  43. static int gio_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  44. unsigned long arg);
  45. static int swdrv_open(struct inode *inode, struct file *filp)
  46. {
  47. int minor;
  48. minor = MINOR(inode->i_rdev);
  49. filp->private_data = (void *)minor;
  50. if (minor == SHUTDOWN_BTN_MINOR) {
  51. if (openCnt > 0) {
  52. return -EALREADY;
  53. } else {
  54. openCnt++;
  55. return 0;
  56. }
  57. } else if (minor == LED_MINOR) {
  58. if (openCntLED > 0) {
  59. return -EALREADY;
  60. } else {
  61. openCntLED++;
  62. return 0;
  63. }
  64. } else if (minor == BTN_MINOR) {
  65. if (openCntBtn > 0) {
  66. return -EALREADY;
  67. } else {
  68. openCntBtn++;
  69. return 0;
  70. }
  71. } else if (minor == GIO_MINOR) {
  72. if (openCntGio > 0) {
  73. return -EALREADY;
  74. } else {
  75. openCntGio++;
  76. return 0;
  77. }
  78. }
  79. return -ENOENT;
  80. }
  81. static int swdrv_close(struct inode *inode, struct file *filp)
  82. {
  83. int minor;
  84. minor = MINOR(inode->i_rdev);
  85. if (minor == SHUTDOWN_BTN_MINOR) {
  86. openCnt--;
  87. } else if (minor == LED_MINOR) {
  88. openCntLED--;
  89. } else if (minor == BTN_MINOR) {
  90. openCntBtn--;
  91. } else if (minor == GIO_MINOR) {
  92. openCntGio--;
  93. }
  94. return 0;
  95. }
  96. static int swdrv_read(struct file *filp, char *buff, size_t count,
  97. loff_t * ppos)
  98. {
  99. int minor;
  100. minor = (int)(filp->private_data);
  101. if (!access_ok(VERIFY_WRITE, (void *)buff, count))
  102. return -EFAULT;
  103. if (minor == SHUTDOWN_BTN_MINOR) {
  104. if (landisk_btn & 0x10) {
  105. put_user(1, buff);
  106. return 1;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. return 0;
  112. }
  113. static int swdrv_write(struct file *filp, const char *buff, size_t count,
  114. loff_t * ppos)
  115. {
  116. int minor;
  117. minor = (int)(filp->private_data);
  118. if (minor == SHUTDOWN_BTN_MINOR) {
  119. return count;
  120. }
  121. return count;
  122. }
  123. static irqreturn_t sw_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  124. {
  125. landisk_btn = (0x0ff & (~ctrl_inb(PA_STATUS)));
  126. disable_irq(IRQ_BUTTON);
  127. disable_irq(IRQ_POWER);
  128. ctrl_outb(0x00, PA_PWRINT_CLR);
  129. if (landisk_btnctrlpid != 0) {
  130. kill_proc(landisk_btnctrlpid, SIGUSR1, 1);
  131. landisk_btnctrlpid = 0;
  132. }
  133. return IRQ_HANDLED;
  134. }
  135. static struct file_operations swdrv_fops = {
  136. .read = swdrv_read, /* read */
  137. .write = swdrv_write, /* write */
  138. .open = swdrv_open, /* open */
  139. .release = swdrv_close, /* release */
  140. .ioctl = gio_ioctl, /* ioctl */
  141. };
  142. static char banner[] __initdata =
  143. KERN_INFO "LANDISK and USL-5P Button, LED and GIO driver initialized\n";
  144. int __init swdrv_init(void)
  145. {
  146. int error;
  147. printk("%s", banner);
  148. openCnt = 0;
  149. openCntLED = 0;
  150. openCntBtn = 0;
  151. openCntGio = 0;
  152. landisk_btn = 0;
  153. landisk_btnctrlpid = 0;
  154. if ((error = register_chrdev(SHUTDOWN_BTN_MAJOR, "swdrv", &swdrv_fops))) {
  155. printk(KERN_ERR
  156. "Button, LED and GIO driver:Couldn't register driver, error=%d\n",
  157. error);
  158. return 1;
  159. }
  160. if (request_irq(IRQ_POWER, sw_interrupt, 0, "SHUTDOWNSWITCH", NULL)) {
  161. printk(KERN_ERR "Unable to get IRQ 11.\n");
  162. return 1;
  163. }
  164. if (request_irq(IRQ_BUTTON, sw_interrupt, 0, "USL-5P BUTTON", NULL)) {
  165. printk(KERN_ERR "Unable to get IRQ 12.\n");
  166. return 1;
  167. }
  168. ctrl_outb(0x00, PA_PWRINT_CLR);
  169. return 0;
  170. }
  171. module_init(swdrv_init);
  172. /*
  173. * gio driver
  174. *
  175. */
  176. #include <asm/landisk/gio.h>
  177. static int gio_ioctl(struct inode *inode, struct file *filp,
  178. unsigned int cmd, unsigned long arg)
  179. {
  180. int minor;
  181. unsigned int data, mask;
  182. static unsigned int addr = 0;
  183. minor = (int)(filp->private_data);
  184. /* access control */
  185. if (minor == GIO_MINOR) {
  186. ;
  187. } else if (minor == LED_MINOR) {
  188. if (((cmd & 0x0ff) >= 9) && ((cmd & 0x0ff) < 20)) {
  189. ;
  190. } else {
  191. return -EINVAL;
  192. }
  193. } else if (minor == BTN_MINOR) {
  194. if (((cmd & 0x0ff) >= 20) && ((cmd & 0x0ff) < 30)) {
  195. ;
  196. } else {
  197. return -EINVAL;
  198. }
  199. } else {
  200. return -EINVAL;
  201. }
  202. if (cmd & 0x01) { /* write */
  203. if (copy_from_user(&data, (int *)arg, sizeof(int))) {
  204. return -EFAULT;
  205. }
  206. }
  207. switch (cmd) {
  208. case GIODRV_IOCSGIOSETADDR: /* addres set */
  209. addr = data;
  210. break;
  211. case GIODRV_IOCSGIODATA1: /* write byte */
  212. ctrl_outb((unsigned char)(0x0ff & data), addr);
  213. break;
  214. case GIODRV_IOCSGIODATA2: /* write word */
  215. if (addr & 0x01) {
  216. return -EFAULT;
  217. }
  218. ctrl_outw((unsigned short int)(0x0ffff & data), addr);
  219. break;
  220. case GIODRV_IOCSGIODATA4: /* write long */
  221. if (addr & 0x03) {
  222. return -EFAULT;
  223. }
  224. ctrl_outl(data, addr);
  225. break;
  226. case GIODRV_IOCGGIODATA1: /* read byte */
  227. data = ctrl_inb(addr);
  228. break;
  229. case GIODRV_IOCGGIODATA2: /* read word */
  230. if (addr & 0x01) {
  231. return -EFAULT;
  232. }
  233. data = ctrl_inw(addr);
  234. break;
  235. case GIODRV_IOCGGIODATA4: /* read long */
  236. if (addr & 0x03) {
  237. return -EFAULT;
  238. }
  239. data = ctrl_inl(addr);
  240. break;
  241. case GIODRV_IOCSGIO_LED: /* write */
  242. mask = ((data & 0x00ffffff) << 8)
  243. | ((data & 0x0000ffff) << 16)
  244. | ((data & 0x000000ff) << 24);
  245. landisk_ledparam = data & (~mask);
  246. if (landisk_arch == 0) { /* arch == landisk */
  247. landisk_ledparam &= 0x03030303;
  248. mask = (~(landisk_ledparam >> 22)) & 0x000c;
  249. landisk_ledparam |= mask;
  250. } else { /* arch == usl-5p */
  251. mask = (landisk_ledparam >> 24) & 0x0001;
  252. landisk_ledparam |= mask;
  253. landisk_ledparam &= 0x007f7f7f;
  254. }
  255. landisk_ledparam |= 0x80;
  256. break;
  257. case GIODRV_IOCGGIO_LED: /* read */
  258. data = landisk_ledparam;
  259. if (landisk_arch == 0) { /* arch == landisk */
  260. data &= 0x03030303;
  261. } else { /* arch == usl-5p */
  262. ;
  263. }
  264. data &= (~0x080);
  265. break;
  266. case GIODRV_IOCSGIO_BUZZER: /* write */
  267. landisk_buzzerparam = data;
  268. landisk_ledparam |= 0x80;
  269. break;
  270. case GIODRV_IOCGGIO_LANDISK: /* read */
  271. data = landisk_arch & 0x01;
  272. break;
  273. case GIODRV_IOCGGIO_BTN: /* read */
  274. data = (0x0ff & ctrl_inb(PA_PWRINT_CLR));
  275. data <<= 8;
  276. data |= (0x0ff & ctrl_inb(PA_IMASK));
  277. data <<= 8;
  278. data |= (0x0ff & landisk_btn);
  279. data <<= 8;
  280. data |= (0x0ff & (~ctrl_inb(PA_STATUS)));
  281. break;
  282. case GIODRV_IOCSGIO_BTNPID: /* write */
  283. landisk_btnctrlpid = data;
  284. landisk_btn = 0;
  285. if (irq_desc[IRQ_BUTTON].depth) {
  286. enable_irq(IRQ_BUTTON);
  287. }
  288. if (irq_desc[IRQ_POWER].depth) {
  289. enable_irq(IRQ_POWER);
  290. }
  291. break;
  292. case GIODRV_IOCGGIO_BTNPID: /* read */
  293. data = landisk_btnctrlpid;
  294. break;
  295. default:
  296. return -EFAULT;
  297. break;
  298. }
  299. if ((cmd & 0x01) == 0) { /* read */
  300. if (copy_to_user((int *)arg, &data, sizeof(int))) {
  301. return -EFAULT;
  302. }
  303. }
  304. return 0;
  305. }