ssegment.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/cdev.h>
  5. #include <linux/slab.h>
  6. #include <linux/uaccess.h>
  7. #include <linux/device.h>
  8. #include <linux/stat.h>
  9. #include <linux/sched.h>
  10. #include <linux/timer.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include "gpioregs.h"
  15. #define DEVICE_NAME "ssegment"
  16. #define CLASS_NAME "ssegment_class"
  17. #define DRIVER_NAME "port80_seg7"
  18. #define BUFFER_SIZE 4
  19. static int major = 56;
  20. static int minor = 40;
  21. module_param(major, int, S_IRUGO);
  22. module_param(minor, int, S_IRUGO);
  23. MODULE_PARM_DESC(major, "Major device number");
  24. MODULE_PARM_DESC(minor, "Minor device number");
  25. static struct class *char_class = NULL;
  26. static struct device *char_device = NULL;
  27. // static struct cdev my_cdev;
  28. static dev_t dev_num;
  29. struct ssegment_dev
  30. {
  31. unsigned char *buffer;
  32. size_t size;
  33. struct mutex lock;
  34. struct cdev cdev;
  35. struct delayed_work delay_work1;
  36. };
  37. static struct ssegment_dev *dev = NULL;
  38. static int wait_ibf(void)
  39. {
  40. int i = 0;
  41. while (inb(EC_CMD_PORT) & EC_IBF)
  42. {
  43. if (++i > TIMEOUT_LOOPS)
  44. {
  45. return -1;
  46. }
  47. udelay(1);
  48. }
  49. return 0;
  50. }
  51. static int wait_obf(void)
  52. {
  53. int i = 0;
  54. while (!(inb(EC_CMD_PORT) & EC_OBF))
  55. {
  56. if (++i > TIMEOUT_LOOPS)
  57. {
  58. return -1;
  59. }
  60. udelay(1);
  61. }
  62. return 0;
  63. }
  64. #if 0
  65. static int ec_read_ram(uint8_t offset, uint8_t *data)
  66. {
  67. if (wait_ibf() < 0)
  68. return -1;
  69. outb(CMD_READ_RAM, EC_CMD_PORT);
  70. if (wait_ibf() < 0)
  71. return -1;
  72. outb(offset, EC_DATA_PORT);
  73. if (wait_obf() < 0)
  74. return -1;
  75. *data = inb(EC_DATA_PORT);
  76. return 0;
  77. }
  78. static int ec_write_ram(uint8_t offset, uint8_t data)
  79. {
  80. if (wait_ibf() < 0)
  81. return -1;
  82. outb(CMD_WRITE_RAM, EC_CMD_PORT);
  83. if (wait_ibf() < 0)
  84. return -1;
  85. outb(offset, EC_DATA_PORT);
  86. if (wait_ibf() < 0)
  87. return -1;
  88. outb(data, EC_DATA_PORT);
  89. return 0;
  90. }
  91. #endif
  92. static int oem_ec_read_ram(uint8_t page, uint8_t offset, uint8_t *data)
  93. {
  94. unsigned char WEC, REC;
  95. switch(page)
  96. {
  97. case 0:
  98. {
  99. WEC = 0x96;
  100. REC = 0x95;
  101. break;
  102. }
  103. case 1:
  104. {
  105. WEC = 0x98;
  106. REC = 0x97;
  107. break;
  108. }
  109. default:
  110. {
  111. WEC = 0x81;
  112. REC = 0x80;
  113. break;
  114. }
  115. }
  116. if (wait_ibf() < 0)
  117. return -1;
  118. outb(REC, EC_CMD_PORT);
  119. if (wait_ibf() < 0)
  120. return -1;
  121. outb(offset, EC_DATA_PORT);
  122. if (wait_obf() < 0)
  123. return -1;
  124. *data = inb(EC_DATA_PORT);
  125. return 0;
  126. }
  127. static int oem_ec_write_ram(uint8_t page, uint8_t offset, uint8_t data)
  128. {
  129. unsigned char WEC, REC;
  130. switch(page)
  131. {
  132. case 0:
  133. {
  134. WEC = 0x96;
  135. REC = 0x95;
  136. break;
  137. }
  138. case 1:
  139. {
  140. WEC = 0x98;
  141. REC = 0x97;
  142. break;
  143. }
  144. default:
  145. {
  146. WEC = 0x81;
  147. REC = 0x80;
  148. break;
  149. }
  150. }
  151. if (wait_ibf() < 0)
  152. return -1;
  153. outb(WEC, EC_CMD_PORT);
  154. if (wait_ibf() < 0)
  155. return -1;
  156. outb(offset, EC_DATA_PORT);
  157. if (wait_ibf() < 0)
  158. return -1;
  159. outb(data, EC_DATA_PORT);
  160. return 0;
  161. }
  162. unsigned int readl_cust(unsigned int addr)
  163. {
  164. unsigned int val;
  165. void __iomem *reg_base;
  166. reg_base = ioremap(addr, 4);
  167. val = readl(reg_base);
  168. iounmap(reg_base);
  169. return val;
  170. }
  171. int writel_cust(unsigned int addr, unsigned int val)
  172. {
  173. void __iomem *reg_base;
  174. reg_base = ioremap(addr, 4);
  175. writel(val, reg_base);
  176. iounmap(reg_base);
  177. return 0;
  178. }
  179. static void delay_work_func(struct work_struct *work)
  180. {
  181. int ret = 0;
  182. unsigned int val = 0;
  183. unsigned char display_value = 0;
  184. static unsigned int flag = 0;
  185. if(dev->size != 0)
  186. {
  187. if((dev->buffer[1] & 0x80))
  188. {
  189. // blink
  190. flag++;
  191. if(flag % 2 == 1)
  192. {
  193. // blink on
  194. if((dev->buffer[1] & 0x02))
  195. {
  196. val = readl_cust(SSEGMENT_POINT);
  197. val = val | 0x01;
  198. ret = writel_cust(SSEGMENT_POINT, val);
  199. }
  200. else
  201. {
  202. val = readl_cust(SSEGMENT_POINT);
  203. val = val & 0xFFFFFFFE;
  204. ret = writel_cust(SSEGMENT_POINT, val);
  205. }
  206. display_value = dev->buffer[0];
  207. outb(display_value, PORT_80);
  208. oem_ec_write_ram(1, 0x06, 0);
  209. }
  210. else
  211. {
  212. // blink off
  213. display_value = 0;
  214. // off point
  215. val = readl_cust(SSEGMENT_POINT);
  216. val = val & 0xFFFFFFFE;
  217. ret = writel_cust(SSEGMENT_POINT, val);
  218. // off segment
  219. oem_ec_write_ram(1, 0x06, 1);
  220. }
  221. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(500));
  222. }
  223. else
  224. {
  225. if((dev->buffer[1] & 0x02))
  226. {
  227. val = readl_cust(SSEGMENT_POINT);
  228. val = val | 0x01;
  229. ret = writel_cust(SSEGMENT_POINT, val);
  230. }
  231. else
  232. {
  233. val = readl_cust(SSEGMENT_POINT);
  234. val = val & 0xFFFFFFFE;
  235. ret = writel_cust(SSEGMENT_POINT, val);
  236. }
  237. flag = 0;
  238. display_value = dev->buffer[0];
  239. outb(display_value, PORT_80);
  240. oem_ec_write_ram(1, 0x06, 0);
  241. }
  242. }
  243. // printk(KERN_INFO "delay_work_func\n");
  244. }
  245. static int ssegment_open(struct inode *inode, struct file *filp)
  246. {
  247. struct ssegment_dev *dev;
  248. // if (!request_region(PORT_80, 1, DRIVER_NAME))
  249. // {
  250. // pr_err("Port 80 I/O region busy\n");
  251. // return -EBUSY;
  252. // }
  253. dev = container_of(inode->i_cdev, struct ssegment_dev, cdev);
  254. filp->private_data = dev;
  255. printk(KERN_INFO "ssegment: Device opened (major=%d, minor=%d)\n",
  256. imajor(inode), iminor(inode));
  257. return 0;
  258. }
  259. static int ssegment_release(struct inode *inode, struct file *filp)
  260. {
  261. // release_region(PORT_80, 1);
  262. printk(KERN_INFO "ssegment: Device closed\n");
  263. return 0;
  264. }
  265. static ssize_t ssegment_read(struct file *filp, char __user *buf,
  266. size_t count, loff_t *f_pos)
  267. {
  268. struct ssegment_dev *dev = filp->private_data;
  269. // ssize_t retval = 0;
  270. // size_t available;
  271. int read_count = 0;
  272. int ret = 0;
  273. if (mutex_lock_interruptible(&dev->lock))
  274. return -ERESTARTSYS;
  275. if (count > dev->size)
  276. {
  277. read_count = dev->size;
  278. }
  279. else
  280. {
  281. read_count = count;
  282. }
  283. ret = copy_to_user(buf, dev->buffer, read_count);
  284. if (ret != 0)
  285. {
  286. printk(KERN_INFO "ssegment: copy_to_user failed\n");
  287. goto out;
  288. }
  289. printk(KERN_INFO "ssegment: Read %zu bytes\n", count);
  290. out:
  291. mutex_unlock(&dev->lock);
  292. return ret;
  293. }
  294. static ssize_t ssegment_write(struct file *filp, const char __user *buf,
  295. size_t count, loff_t *f_pos)
  296. {
  297. struct ssegment_dev *dev = filp->private_data;
  298. // ssize_t retval = 0;
  299. // size_t available;
  300. int ret = 0;
  301. if (mutex_lock_interruptible(&dev->lock))
  302. {
  303. return -ERESTARTSYS;
  304. }
  305. if (count > BUFFER_SIZE)
  306. {
  307. count = BUFFER_SIZE;
  308. }
  309. ret = copy_from_user(dev->buffer, buf, count);
  310. if (ret != 0)
  311. {
  312. printk(KERN_INFO "ssegment: copy_from_user failed\n");
  313. goto out;
  314. }
  315. printk("%02x %02x %02x %02x\n", dev->buffer[0], dev->buffer[1], dev->buffer[2], dev->buffer[3]);
  316. dev->size = count;
  317. printk(KERN_INFO "ssegment: Written %zu bytes\n", count);
  318. out:
  319. mutex_unlock(&dev->lock);
  320. if (ret == 0)
  321. {
  322. schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(10));
  323. }
  324. return count;
  325. }
  326. static struct file_operations fops = {
  327. .owner = THIS_MODULE,
  328. .open = ssegment_open,
  329. .release = ssegment_release,
  330. .read = ssegment_read,
  331. .write = ssegment_write,
  332. };
  333. static char *my_devnode(struct device *dev, umode_t *mode) {
  334. if (mode) {
  335. *mode = 0666;
  336. }
  337. return NULL;
  338. }
  339. int ssegment_init(void)
  340. {
  341. int result;
  342. printk(KERN_INFO "ssegment: Initializing driver with major=%d, minor=%d\n",
  343. major, minor);
  344. if (major <= 0)
  345. {
  346. printk(KERN_ALERT "ssegment: Invalid major number %d\n", major);
  347. return -EINVAL;
  348. }
  349. dev_num = MKDEV(major, minor);
  350. result = register_chrdev_region(dev_num, 1, DEVICE_NAME);
  351. if (result < 0)
  352. {
  353. printk(KERN_ALERT "ssegment: Failed to register major number %d\n", major);
  354. printk(KERN_ALERT "ssegment: Try using a different major number\n");
  355. return result;
  356. }
  357. printk(KERN_INFO "ssegment: Registered with major=%d, minor=%d\n",
  358. MAJOR(dev_num), MINOR(dev_num));
  359. dev = kmalloc(sizeof(struct ssegment_dev), GFP_KERNEL);
  360. if (!dev)
  361. {
  362. result = -ENOMEM;
  363. goto fail_malloc;
  364. }
  365. memset(dev, 0, sizeof(struct ssegment_dev));
  366. dev->buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  367. if (!dev->buffer)
  368. {
  369. result = -ENOMEM;
  370. goto fail_buffer;
  371. }
  372. dev->buffer[0] = inb(PORT_80);
  373. mutex_init(&dev->lock);
  374. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  375. cdev_init(&dev->cdev, &fops);
  376. dev->cdev.owner = THIS_MODULE;
  377. result = cdev_add(&dev->cdev, dev_num, 1);
  378. if (result)
  379. {
  380. printk(KERN_ALERT "ssegment: Failed to add cdev\n");
  381. goto fail_cdev;
  382. }
  383. char_class = class_create(THIS_MODULE, CLASS_NAME);
  384. if (IS_ERR(char_class))
  385. {
  386. result = PTR_ERR(char_class);
  387. printk(KERN_ALERT "ssegment: Failed to create class\n");
  388. goto fail_class;
  389. }
  390. char_class->devnode = my_devnode;
  391. char_device = device_create(char_class, NULL, dev_num, NULL, DEVICE_NAME);
  392. if (IS_ERR(char_device))
  393. {
  394. result = PTR_ERR(char_device);
  395. printk(KERN_ALERT "ssegment: Failed to create device\n");
  396. goto fail_device;
  397. }
  398. printk(KERN_INFO "ssegment: Driver initialized successfully\n");
  399. printk(KERN_INFO "ssegment: Device node: /dev/%s (major=%d, minor=%d)\n",
  400. DEVICE_NAME, major, minor);
  401. return 0;
  402. fail_device:
  403. class_destroy(char_class);
  404. fail_class:
  405. cdev_del(&dev->cdev);
  406. fail_cdev:
  407. kfree(dev->buffer);
  408. fail_buffer:
  409. kfree(dev);
  410. fail_malloc:
  411. unregister_chrdev_region(dev_num, 1);
  412. return result;
  413. }
  414. void ssegment_exit(void)
  415. {
  416. cancel_delayed_work_sync(&dev->delay_work1);
  417. device_destroy(char_class, dev_num);
  418. class_destroy(char_class);
  419. if (dev)
  420. {
  421. cdev_del(&dev->cdev);
  422. if (dev->buffer)
  423. kfree(dev->buffer);
  424. kfree(dev);
  425. }
  426. unregister_chrdev_region(dev_num, 1);
  427. printk(KERN_INFO "ssegment: Driver removed (major=%d, minor=%d)\n",
  428. major, minor);
  429. }